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.
lnapp/classes/lnApp/Controller/TemplateDefault.php

196 lines
6.2 KiB
PHP
Raw Normal View History

2013-04-22 05:50:28 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides the default template controller for rendering pages.
*
* @package lnApp
2013-10-08 23:24:11 +00:00
* @category Controllers
2013-04-22 05:50:28 +00:00
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Controller_TemplateDefault extends Kohana_Controller_Template {
2013-04-22 05:50:28 +00:00
/**
* @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();
2014-02-17 00:29:11 +00:00
// Our acccount object
protected $ao;
public function __construct(Request $request, Response $response) {
2014-08-22 06:50:01 +00:00
if (Site::Theme())
$this->template = Site::Theme().'/page';
return parent::__construct($request,$response);
}
2013-04-22 05:50:28 +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() {
2014-09-29 12:06:38 +00:00
return (($this->auth_required !== FALSE && Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__) === FALSE) ||
(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__)));
2013-04-22 05:50:28 +00:00
}
/**
* Loads the template [View] object.
*
* Page information is provided by [meta].
* @uses meta
*/
public function before() {
2014-10-02 05:33:07 +00:00
$this->ao = Auth::instance()->get_user();
2013-04-22 05:50:28 +00:00
// Actions that start with ajax, should only be ajax
if (! Kohana::$config->load('debug')->ajax AND preg_match('/^ajax/',Request::current()->action()) AND ! Request::current()->is_ajax())
throw HTTP_Exception::factory(412,_('Unable to fulfil request.'));
2013-04-22 05:50:28 +00:00
parent::before();
// Do not template media files
if ($this->request->action() === 'media') {
$this->auto_render = FALSE;
return;
}
2014-09-29 04:47:51 +00:00
if ($this->ao AND $this->ao->loaded() AND ! $this->ao->activated() AND ($this->request->controller() != 'Account' OR $this->request->action() != 'activate'))
HTTP::redirect('login/activate');
2013-04-22 05:50:28 +00:00
// Check user auth and role
if ($this->_auth_required()) {
if (PHP_SAPI === 'cli')
2013-04-22 05:50:28 +00:00
throw new Kohana_Exception('Cant run :method, authentication not possible',array(':method'=>$this->request->action()));
// 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 ($this->request->is_ajax())
throw HTTP_Exception::factory(403,_('You dont have enough permissions.'));
else
2014-09-08 13:42:05 +00:00
if (! Kohana::$config->load('config')->disabled_noaccess_redirect)
2013-10-09 05:41:24 +00:00
HTTP::redirect('login/noaccess');
else
throw HTTP_Exception::factory(501,'I would redirect you here - no ACCESS');
2013-04-22 05:50:28 +00:00
} else {
Session::instance()->set('afterlogin',Request::detect_uri());
HTTP::redirect($this->noauth_redirect);
}
}
2013-04-25 00:22:36 +00:00
if (! $this->auto_render)
return;
2013-04-22 05:50:28 +00:00
// For AJAX calls, we dont need to render the complete page.
if ($this->request->is_ajax() OR (Kohana::$config->load('debug')->ajax AND preg_match('/^ajax/',Request::current()->action()))) {
2013-04-22 05:50:28 +00:00
$this->auto_render = FALSE;
2013-04-22 05:50:28 +00:00
return;
}
// Bind our template meta variable
$this->meta = new Meta;
View::bind_global('meta',$this->meta);
// Our default script(s)
foreach (array('file'=>array_reverse(array(
))) as $type => $datas) {
foreach ($datas as $data) {
Script::add(array(
'type'=>$type,
'data'=>$data,
),TRUE);
}
}
// Initialise our content
2013-04-25 00:22:36 +00:00
$this->template->shownavbar = TRUE;
2013-04-22 05:50:28 +00:00
$this->template->content = '';
2013-04-25 00:22:36 +00:00
$this->template->footer = '';
2013-04-22 05:50:28 +00:00
}
public function after() {
if ($this->auto_render) {
2014-08-22 06:50:01 +00:00
$this->template->navbar = $this->template->shownavbar ? View::factory(Site::Theme().'/navbar') : '';
2013-04-25 00:22:36 +00:00
if (empty($this->template->content))
$this->template->content = Block::factory()->render_all();
// Adjust our breadcrumb
if (isset(URL::$method_directory[strtolower($this->request->directory())]))
BreadCrumb::name(URL::$method_directory[strtolower($this->request->directory())],$this->request->directory());
2013-04-22 05:50:28 +00:00
// Description
$this->meta->description = sprintf('%s::%s',$this->request->controller(),$this->request->action());
// For any ajax rendered actions, we'll need to capture the content and put it in the response
} elseif ($this->request->is_ajax()) {
2013-05-16 11:50:44 +00:00
$output = Style::factory()->render_all();
$output .= Script::factory()->render_all();
2013-10-09 05:41:24 +00:00
if (! isset($this->template->content))
$output .= $this->response->body() ? $this->response->body() : '';
else
$output .= $this->template->content;
2013-04-22 05:50:28 +00:00
2013-05-16 11:50:44 +00:00
$this->response->body($output);
2013-04-22 05:50:28 +00:00
}
// Used by our javascript to know what the SITE URL is.
Script::factory()
->type('stdin')
->data('var site_url="'.URL::site('',TRUE).'";');
2013-04-22 05:50:28 +00:00
parent::after();
// Generate and check the ETag for this file
2013-04-25 00:22:36 +00:00
if (Kohana::$environment < Kohana::TESTING OR Kohana::$config->load('debug')->etag)
$this->check_cache(sha1($this->response->body()));
2013-04-22 05:50:28 +00:00
}
2014-09-29 04:47:51 +00:00
protected function save(Model $o) {
try {
return $o->save();
2013-04-22 05:50:28 +00:00
2014-09-29 04:47:51 +00:00
} catch (ORM_Validation_Exception $e) {
SystemMessage::factory()
->title('Record NOT updated')
->type('danger')
->body(join('<br/>',array_values($e->errors('models'))));
2013-04-22 05:50:28 +00:00
2014-09-29 04:47:51 +00:00
return FALSE;
}
2013-04-22 05:50:28 +00:00
}
}
?>