<?php
/**
* Base for controllers.
*/
class Controller extends CController {
public $pageHeader;
public $breadcrumbs = array();
public function filters() {
return array(
'siteInitialization',
);
}
/**
* Set default site settings. Language and timezone.
* Use user settings if they are present.
*/
public function filterSiteInitialization($filterChain) {
// site language
Yii::app()->language = !Yii::app()->user->isGuest && !empty($this->webUser->language)
? $this->webUser->language
: Settings::instance()->language;
// site timezone
Yii::app()->timezone = !Yii::app()->user->isGuest && !empty($this->webUser->timezone)
? $this->webUser->timezone
: Settings::instance()->timezone;
// set mysql timezone
Yii::app()->db->createCommand('SET time_zone=:timezone')->execute(array(':timezone' => Yii::app()->timeZone));
// import CDateFormatter behavior
Yii::import('ext.ExtendedDateTimeFormattingBehavior.ExtendedDateTimeFormattingBehavior');
Yii::app()->dateFormatter->attachBehavior('ExtendedDateTimeFormatting', new ExtendedDateTimeFormattingBehavior());
$filterChain->run();
}
/**
* Returns web user database record
*/
public function getWebUser() {
return Yii::app()->user->record;
}
/**
* Returns unique route to this controller/action
*/
public function getUniqueRoute() {
return $this->uniqueId.'/'.$this->action->id;
}
/**
* This method is invoked at the beginning of {@link render()}.
*/
protected function beforeRender($view) {
if ($this->hasEventHandler('onBeforeRender')) {
$event = new ControllerEvent($this);
$event->view = $view;
$this->onBeforeRender($event);
return $event->isValid;
}
return true;
}
/**
* This method is invoked after the specified view is rendered by calling {@link render()}.
*/
protected function afterRender($view, &$output) {
if ($this->hasEventHandler('onAfterRender')) {
$event = new ControllerEvent($this);
$event->view = $view;
$event->output = &$output;
$this->onAfterRender($event);
}
}
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
*/
protected function beforeAction($action) {
if ($this->hasEventHandler('onBeforeAction')) {
$event = new ControllerEvent($this);
$event->action = $action;
$this->onBeforeAction($event);
return $event->isValid;
}
return true;
}
/**
* This method is invoked right after an action is executed.
*/
protected function afterAction($action) {
if ($this->hasEventHandler('onAfterAction')) {
$event = new ControllerEvent($this);
$event->action = $action;
$this->onAfterAction($event);
}
}
/**
* This event is raised before the renderer.
* @param CEvent $event the event parameter
*/
public function onBeforeRender($event) {
$this->raiseEvent('onBeforeRender',$event);
}
/**
* This event is raised after the renderer.
* @param CEvent $event the event parameter
*/
public function onAfterRender($event) {
$this->raiseEvent('onAfterRender',$event);
}
/**
* This event is raised before an action is executed.
* @param CEvent $event the event parameter
*/
public function onBeforeAction($event) {
$this->raiseEvent('onBeforeAction',$event);
}
/**
* This event is raised after an action is executed.
* @param CEvent $event the event parameter
*/
public function onAfterAction($event) {
$this->raiseEvent('onAfterAction',$event);
}
/**
* Creates the action instance based on the action name.
* Additionally, this implementing checks attached behaviors for actions.
*/
public function createAction($actionID) {
$action = parent::createAction($actionID);
// search in behaviors
if ($action === null) {
foreach ($this->behaviors() as $behavior => $data) {
$behavior = $this->{$behavior};
if (is_subclass_of($behavior, 'IBehavior') && method_exists($behavior, 'action'.$actionID)) {
return new CInlineAction($behavior, $actionID);
}
}
}
return $action;
}
public function checkAccess(array $multipleAccess) {
foreach ($multipleAccess as $operation => $params) {
if (is_int($operation)) {
if (Yii::app()->user->checkAccess($params) === true)
return true;
} else {
if (Yii::app()->user->checkAccess($operation, $params) === true)
return true;
}
}
return false;
}
}