Last active
September 6, 2019 12:03
-
-
Save smgladkovskiy/832462 to your computer and use it in GitHub Desktop.
template controller for kohana v3.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 View | |
*/ | |
public $template = 'template/main'; | |
/** | |
* Language | |
* | |
* @var string|NULL | |
*/ | |
public $lang = NULL; | |
/** | |
* The need of authorization | |
* | |
* @var bool | |
*/ | |
protected $_auth_required = TRUE; | |
/** | |
* User container | |
* | |
* @var Model_User | |
*/ | |
protected $_user = NULL; | |
/** | |
* User rolse container | |
* | |
* @var array | |
*/ | |
protected $_user_roles = NULL; | |
/** | |
* ACL | |
* | |
* @var Deputy | |
*/ | |
protected $_deputy = NULL; | |
/** | |
* @var Config_Group | |
*/ | |
protected $_site_config; | |
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() AND $request->param('code') === NULL) | |
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')) | |
{ | |
// Setting lang from URL | |
I18n::$lang = $this->request->param('lang'); | |
} | |
parent::before(); | |
// Auth check | |
$this->_auth_check(); | |
$this->_site_config = Kohana::$config->load('site'); | |
View::set_global('site_name', $this->_site_config->site_name); | |
if ($this->auto_render) | |
{ | |
View::set_global('lang', I18n::$lang); | |
StaticJs::instance() | |
# ->add_inline(' | |
# var lang = "'.I18n::$lang.'" | |
# , i18n = {loading: "'.__('Загрузка').'"} | |
# ;') | |
# ->add('js/jquery-2.0.3.min.js') | |
# ->add('js/bootstrap.min.js') | |
; | |
StaticCss::instance() | |
# ->add('css/bootstrap.min.css') | |
# ->add('css/font-awesome.min.css') | |
->add('css/style.css') | |
; | |
// default template variables initialization | |
$this->template->title_delimeter = $this->_site_config->title_delimeter; // разделитель в заголовке страницы | |
$this->template->site_name = $this->_site_config->site_name; // Наименование сайта | |
$this->template->company_name = $this->_site_config->company_name; | |
$this->template->support_email = $this->_site_config->support_email; | |
$this->template->version = $this->_site_config->version; | |
$this->template->title = ''; | |
$this->template->page_title = ''; | |
$this->template->navbar = ''; | |
$this->template->content = ''; | |
$this->template->modals = ''; | |
} | |
} | |
public function after() | |
{ | |
// Using template content on Ajax-like requests | |
if ($this->request->is_ajax() === TRUE) | |
{ | |
$this->response->body($this->template->content); | |
} | |
else | |
{ | |
// Формирование заголовка страницы на основе данных о page_title, title_delimeter и title | |
if(isset($this->template->title) AND $this->template->title != '') | |
$this->template->title = $this->template->title | |
. $this->template->title_delimeter | |
. $this->template->site_name; | |
if( ! $this->template->page_title) | |
$this->template->page_title = $this->template->title; | |
# $menu = new Menu($this->_user, $this->_user_roles); | |
# if($this->request->controller() != 'Auth') | |
# $this->template->navbar = $menu->navbar(); | |
StaticJs::instance()->add('/js/script.js'); | |
StaticCss::instance()->add('css/style.css'); | |
$this->template->modals .= View::factory('modal/loading'); | |
parent::after(); | |
} | |
} | |
protected function _auth_check() | |
{ | |
// Auth require check and setting $this->_user | |
# if ($this->_auth_required AND class_exists('Auth') AND ! Auth::instance()->logged_in()) | |
# { | |
# Session::instance()->set('url', $_SERVER['REQUEST_URI']); | |
# HTTP::redirect(Route::url('auth', array('lang' => I18n::$lang, 'action' => 'login'))); | |
# } | |
# elseif($this->_auth_required AND (class_exists('Auth') AND Auth::instance()->logged_in() OR Auth::instance()->logged_in())) | |
# { | |
# $this->_user = Jelly::query('user', Auth::instance()->get_user()->id)->select(); | |
# $this->_check_activity(); | |
# View::set_global('_user', $this->_user); | |
# } | |
# | |
# if(class_exists('Auth') AND Auth::instance()->logged_in() AND ! $this->_user) | |
# { | |
# $this->_user = Jelly::query('user', Auth::instance()->get_user()->id)->select(); | |
# $this->_check_activity(); | |
# View::set_global('_user', $this->_user); | |
# } | |
} | |
protected function _check_activity() | |
{ | |
# if( ! $this->_user->is_active AND $this->request->controller() != 'Error') | |
# throw HTTP_Exception::factory(403, __('Пользователь не зарегистирован или отключён')); | |
# | |
# $this->_user_roles = $this->_user->roles->as_array('id', 'name'); | |
# $this->_deputy = Deputy::instance(); | |
# $roles = Arr::extract(Kohana::$config->load('deputy.roles'), $this->_user_roles); | |
# $this->_deputy->set_roles($roles); | |
# $resource = array( | |
# $this->request->controller(), | |
# $this->request->action(), | |
# ); | |
# $resource = implode('/', $resource); | |
# | |
# if($this->_deputy->allowed($resource) == FALSE) | |
# throw HTTP_Exception::factory(403, __('Действие запрещено')); | |
// $this->_check_rules_acceptance(); | |
} | |
protected function _check_rules_acceptance() | |
{ | |
# $controller = $this->request->controller(); | |
# $action = $this->request->action(); | |
# | |
# if(($controller == 'User' AND $action == 'help') | |
# OR ($controller == 'User' AND $action == 'rules') | |
# OR $controller == 'Error' | |
# AND $this->_user->accepted_rules !== TRUE OR $this->_user->accepted_rules == TRUE) | |
# { | |
# // Позволяем смотреть данные страницы | |
# } | |
# else | |
# HTTP::redirect(Route::url('user', array('lang' => I18n::$lang, 'action' => 'rules'))); | |
} | |
} // End Controller_Template |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment