This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/application/classes/lnapp/controller/templatedefault.php

269 lines
8.3 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides the default template controller for rendering pages.
*
* @package lnApp
* @subpackage Page/Template
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
2012-01-29 10:08:54 +00:00
abstract class lnApp_Controller_TemplateDefault extends Controller_Template {
2010-11-29 22:41:08 +00:00
/**
* @var string page template
*/
public $template = 'lnapp/default';
/**
* @var object meta object information as per [meta]
*/
protected $meta;
/**
* Controls access to this controller.
* Can be set to a string or an array, for example 'login' or array('login', 'admin')
* Note that in second(array) example, user must have both 'login' AND 'admin' roles set in database
*
* @var boolean is authenticate required with this controller
*/
protected $auth_required = FALSE;
/**
* If redirecting to a login page, which page to redirect to
*/
protected $noauth_redirect = 'login';
/**
* Controls access for separate actions, eg:
* 'adminpanel' => 'admin' will only allow users with the role admin to access action_adminpanel
* 'moderatorpanel' => array('login', 'moderator') will only allow users with the roles login and moderator to access action_moderatorpanel
*
* @var array actions that require a valid user
*/
protected $secure_actions = array(
);
2012-01-29 06:23:24 +00:00
public function __construct(Request $request,Response $response) {
2011-12-09 04:10:53 +00:00
// Our Menu's can run without method authentication by default.
if (! isset($this->secure_actions['menu']))
$this->secure_actions['menu'] = FALSE;
return parent::__construct($request,$response);
}
2010-11-29 22:41:08 +00:00
/**
* Check and see if this controller needs authentication
*
* if $this->auth_required is TRUE, then the user must be logged in only.
* if $this->auth_required is FALSE, AND $this->secure_actions has an array of
* methods set to TRUE, then the user must be logged in AND a member of the
* role.
*
* @return boolean
*/
protected function _auth_required() {
// If our global configurable is disabled, then continue
if (! Kohana::Config('config.method_security'))
return FALSE;
2011-05-02 12:20:56 +00:00
return (($this->auth_required !== FALSE && Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__) === FALSE) ||
2011-05-14 07:35:33 +00:00
(is_array($this->secure_actions) && array_key_exists($this->request->action(),$this->secure_actions) &&
Auth::instance()->logged_in($this->secure_actions[$this->request->action()],get_class($this).'|'.__METHOD__) === FALSE));
2010-11-29 22:41:08 +00:00
}
/**
* Loads the template [View] object.
*
* Page information is provided by [meta].
* @uses meta
*/
public function before() {
// Do not template media files
2011-05-14 07:35:33 +00:00
if ($this->request->action() === 'media') {
2010-11-29 22:41:08 +00:00
$this->auto_render = FALSE;
return;
}
parent::before();
// Check user auth and role
if ($this->_auth_required()) {
if (Kohana::$is_cli)
2011-05-14 07:35:33 +00:00
throw new Kohana_Exception('Cant run :method, authentication not possible',array(':method'=>$this->request->action()));
2010-11-29 22:41:08 +00:00
// If auth is required and the user is logged in, then they dont have access.
// (We have already checked authorisation.)
if (Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__)) {
if (Config::sitemode() == Kohana::DEVELOPMENT)
SystemMessage::add(array(
'title'=>_('Insufficient Access'),
'type'=>'debug',
2011-09-17 10:45:08 +00:00
'body'=>Debug::vars(array('required'=>$this->auth_required,'action'=>$this->request->action(),'user'=>Auth::instance()->get_user()->username)),
2010-11-29 22:41:08 +00:00
));
// @todo Login No Access redirects are not handled in JS?
2011-05-14 07:35:33 +00:00
if ($this->request->is_ajax()) {
2010-11-29 22:41:08 +00:00
echo _('You dont have enough permissions.');
die();
} else
2011-05-14 07:35:33 +00:00
Request::current()->redirect('login/noaccess');
2010-11-29 22:41:08 +00:00
} else {
2011-05-14 07:35:33 +00:00
Session::instance()->set('afterlogin',Request::detect_uri());
Request::current()->redirect($this->noauth_redirect);
2010-11-29 22:41:08 +00:00
}
}
// For AJAX calls, we dont need to render the complete page.
2011-05-14 07:35:33 +00:00
if ($this->request->is_ajax()) {
2010-11-29 22:41:08 +00:00
$this->auto_render = FALSE;
return;
}
// Bind our template meta variable
$this->meta = new meta;
View::bind_global('meta',$this->meta);
2012-01-29 06:23:24 +00:00
// Add our logo
Style::add(array(
'type'=>'stdin',
'data'=>'h1 span{background:url('.Config::logo_uri().') no-repeat;}',
));
2012-01-12 08:53:33 +00:00
// Our default script(s)
foreach (array('file'=>array_reverse(array(
'js/jquery-1.6.4.min.js',
2012-01-12 08:53:33 +00:00
'js/jquery.jstree-1.0rc3.js',
'js/jquery.cookie.js',
))) as $type => $datas) {
2010-11-29 22:41:08 +00:00
foreach ($datas as $data) {
Script::add(array(
'type'=>$type,
'data'=>$data,
),TRUE);
}
}
// Initialise our content
$this->template->left = '';
$this->template->content = '';
$this->template->right = '';
}
public function after() {
2011-07-13 22:59:32 +00:00
if (! is_string($this->template) AND empty($this->template->content))
$this->template->content = Block::factory();
2010-11-29 22:41:08 +00:00
if ($this->auto_render) {
// Application Title
2012-03-31 03:33:13 +00:00
if ($mo=ORM::factory('module',array('name'=>Request::current()->controller())) AND $mo->loaded())
$this->meta->title = sprintf('%s: %s',Kohana::Config('config.appname'),$mo->display('name'));
else
$this->meta->title = Kohana::Config('config.appname');
2010-11-29 22:41:08 +00:00
$this->template->title = '';
// Language
$this->meta->language = Config::instance()->so->language_id;
2012-01-29 06:23:24 +00:00
// Description
$this->meta->description = sprintf('%s::%s',$this->request->controller(),$this->request->action());
2010-11-29 22:41:08 +00:00
// Link images on the header line
$this->template->headimages = $this->_headimages();
// System Messages line
$this->template->sysmsg = $this->_sysmsg();
// Left Item
$this->template->left = $this->_left();
// Right Item
$this->template->right = $this->_right();
// Footer
$this->template->footer = $this->_footer();
// For any ajax rendered actions, we'll need to capture the content and put it in the response
2011-05-14 07:35:33 +00:00
} elseif ($this->request->is_ajax() && isset($this->template->content) && ! $this->response->body()) {
2010-11-29 22:41:08 +00:00
// @todo move this formatting to a view?
2011-05-14 07:35:33 +00:00
if ($s = $this->_sysmsg() AND (string)$s)
$this->response->body(sprintf('<table class="sysmsg"><tr><td>%s</td></tr></table>',$s));
2010-11-29 22:41:08 +00:00
// In case there any style sheets for this render.
2011-05-14 07:35:33 +00:00
$this->response->bodyadd(Style::factory());
2010-11-29 22:41:08 +00:00
2011-12-09 04:10:53 +00:00
// Since we are ajax, we should re-render the breadcrumb
Session::instance()->set('breadcrumb',(string)Breadcrumb::factory());
$this->response->bodyadd(Script::add(array('type'=>'stdin','data'=>'$().ready($("#ajCONTROL").load("'.URL::site('welcome/breadcrumb').'",null,function(x,s,r) {}));')));
// In case there any javascript for this render.
$this->response->bodyadd(Script::factory());
// Get the response body
2011-05-14 07:35:33 +00:00
$this->response->bodyadd(sprintf('<table class="content"><tr><td>%s</td></tr></table>',$this->template->content));
2010-11-29 22:41:08 +00:00
}
parent::after();
2011-07-13 22:59:32 +00:00
// Generate and check the ETag for this file
2011-07-14 09:09:03 +00:00
if (Kohana::$environment === Kohana::PRODUCTION)
$this->response->check_cache(NULL,$this->request);
2010-11-29 22:41:08 +00:00
}
/**
* Default Method to call from the tree menu
*/
public function action_menu() {
2011-10-07 00:57:59 +00:00
$this->template->content = _('Please choose from the menu on the left - you may need to expand the items by pressing on the plus.');
2010-11-29 22:41:08 +00:00
}
protected function _headimages() {
HeadImages::add(array(
'url'=>'http://dev.leenooks.net',
'img'=>'img/forum-big.png',
'attrs'=>array('onclick'=>"target='_blank';",'title'=>'Link')
));
return HeadImages::factory();
}
protected function _sysmsg() {
return SystemMessage::factory();
}
protected function _left() {
return empty($this->template->left) ? Controller_Tree::js() : $this->template->left;
}
protected function _right() {
return empty($this->template->right) ? '' : $this->template->right;
}
public function _footer() {
return sprintf('&copy; %s',Config::SiteName());
}
/**
* Generate a view path to help View::factory() calls
*
* The purpose of this method is to ensure that we have a consistant
* layout for our view files, including those that are needed by
* plugins
*
* @param string Plugin Name (optional)
*/
public function viewpath($plugin='') {
$request = Request::current();
$path = '';
$path .= $request->controller();
if ($request->directory())
$path .= ($path ? '/' : '').$request->directory();
if ($plugin)
$path .= ($path ? '/' : '').$plugin;
$path .= ($path ? '/' : '').$request->action();;
return $path;
}
2010-11-29 22:41:08 +00:00
}
?>