Guest User

Untitled

a guest
Jul 22nd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. /**
  4. * Template Controller
  5. *
  6. * @package Templates
  7. * @author Sergei Gladkovskiy <smgladkovskiy@gmail.com>
  8. */
  9. abstract class Controller_Template extends Kohana_Controller_Template {
  10.  
  11. /**
  12. * Page template
  13. *
  14. * @var string
  15. */
  16. public $template = 'frontend/template/main';
  17.  
  18. /**
  19. * The need of authorization
  20. *
  21. * @var bool
  22. */
  23. protected $_auth_required = FALSE;
  24.  
  25. /**
  26. * User container
  27. *
  28. * @var boolean
  29. */
  30. protected $_user = FALSE;
  31.  
  32. public function __construct(Request $request, Response $response)
  33. {
  34. // Ajax-like request setting if HMVC call or POST request with param `is_ajax` == `true`
  35. if ($request->is_ajax() OR $request !== Request::initial()
  36. OR ($request->method() === HTTP_Request::POST AND $request->post('is_ajax') === 'true'))
  37. {
  38. $request->requested_with('xmlhttprequest');
  39. }
  40.  
  41. parent::__construct($request, $response);
  42. }
  43.  
  44. public function before()
  45. {
  46. // Setting lang from URL
  47. if($this->request->param('lang'))
  48. I18n::$lang = $this->request->param('lang');
  49.  
  50. parent::before();
  51.  
  52. // Auth require check and setting $this->_user
  53. if ($this->_auth_required AND ! Auth::instance()->logged_in())
  54. {
  55. Session::instance()->set('url', $_SERVER['REQUEST_URI']);
  56. $this->request->redirect('auth/login');
  57. }
  58. elseif(($this->_auth_required AND Auth::instance()->logged_in()) OR Auth::instance()->logged_in())
  59. {
  60. $this->_user = Auth::instance()->get_user();
  61. View::set_global('_user', $this->_user);
  62. }
  63.  
  64. if(Auth::instance()->logged_in())
  65. {
  66. $this->_user = Auth::instance()->get_user();
  67. View::set_global('_user', $this->_user);
  68. }
  69.  
  70. if ($this->auto_render)
  71. {
  72. // default template variables initialization
  73. $this->template->title = ''; // page title
  74. $this->template->content = ''; // page content
  75. }
  76. }
  77.  
  78. public function after()
  79. {
  80. // Using template content on Ajax-like requests
  81. if ($request->is_ajax() === TRUE)
  82. {
  83. $this->response->body($this->template->content);
  84. }
  85. else
  86. {
  87. parent::after();
  88. }
  89. }
  90.  
  91. } // End Controller_Template
Add Comment
Please, Sign In to add comment