Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- /**
- * Template Controller
- *
- * @package Templates
- * @author Sergei Gladkovskiy <smgladkovskiy@gmail.com>
- */
- abstract class Controller_Template extends Kohana_Controller_Template {
- /**
- * Page template
- *
- * @var string
- */
- public $template = 'frontend/template/main';
- /**
- * The need of authorization
- *
- * @var bool
- */
- protected $_auth_required = FALSE;
- /**
- * User container
- *
- * @var boolean
- */
- protected $_user = FALSE;
- public function __construct(Request $request, Response $response)
- {
- // Ajax-like request setting if HMVC call or POST request with param `is_ajax` == `true`
- if ($request->is_ajax() OR $request !== Request::initial()
- OR ($request->method() === HTTP_Request::POST AND $request->post('is_ajax') === 'true'))
- {
- $request->requested_with('xmlhttprequest');
- }
- parent::__construct($request, $response);
- }
- public function before()
- {
- // Setting lang from URL
- if($this->request->param('lang'))
- I18n::$lang = $this->request->param('lang');
- parent::before();
- // Auth require check and setting $this->_user
- if ($this->_auth_required AND ! Auth::instance()->logged_in())
- {
- Session::instance()->set('url', $_SERVER['REQUEST_URI']);
- $this->request->redirect('auth/login');
- }
- elseif(($this->_auth_required AND Auth::instance()->logged_in()) OR Auth::instance()->logged_in())
- {
- $this->_user = Auth::instance()->get_user();
- View::set_global('_user', $this->_user);
- }
- if(Auth::instance()->logged_in())
- {
- $this->_user = Auth::instance()->get_user();
- View::set_global('_user', $this->_user);
- }
- if ($this->auto_render)
- {
- // default template variables initialization
- $this->template->title = ''; // page title
- $this->template->content = ''; // page content
- }
- }
- public function after()
- {
- // Using template content on Ajax-like requests
- if ($request->is_ajax() === TRUE)
- {
- $this->response->body($this->template->content);
- }
- else
- {
- parent::after();
- }
- }
- } // End Controller_Template
Add Comment
Please, Sign In to add comment