PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/components/Controller.php

https://bitbucket.org/rohitrox/hotc
PHP | 187 lines | 110 code | 28 blank | 49 comment | 26 complexity | e95bac25979ee3e96718895275bd0053 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Controller is the customized base controller class.
  4. * All controller classes for this application should extend from this base class.
  5. */
  6. class Controller extends CController {
  7. protected $assetPath;
  8. /**
  9. * @var string the default layout for the controller view. Defaults to '//layouts/column1',
  10. * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
  11. */
  12. public $layout = '//layouts/content';
  13. /**
  14. * @var array context menu items. This property will be assigned to {@link CMenu::items}.
  15. */
  16. public $menu = array();
  17. /**
  18. * @var array the breadcrumbs of the current page. The value of this property will
  19. * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
  20. * for more details on how to specify this property.
  21. */
  22. public $breadcrumbs = array();
  23. public $webpageType = 'WebPage';
  24. public $pageRobotsIndex = true;
  25. public $pageKeywords;
  26. public function filters() {
  27. return array(
  28. 'accessControl - login, logout',
  29. );
  30. }
  31. public function accessRules() {
  32. return array(
  33. array('allow',
  34. 'actions' => array('index', 'view'),
  35. 'users' => array('*'),
  36. ),
  37. array('allow',
  38. 'actions' => array('minicreate', 'create', 'update', 'manage', 'delete', 'toggle'),
  39. 'users' => array('admin'),
  40. ),
  41. array('deny',
  42. 'users' => array('*'),
  43. ),
  44. );
  45. }
  46. // public function accessRules() {
  47. // return array(
  48. // array('allow',
  49. // 'expression' => 'Role::checkAccess()',
  50. // ),
  51. // array('deny',
  52. // 'users' => array('*'),
  53. // ),
  54. // );
  55. // }
  56. public function init() {
  57. //LanguagePicker::setLanguage();
  58. $appName = Settings::get('site', 'name');
  59. if ($appName)
  60. Yii::app()->name = $appName;
  61. //if the request originates from admin module
  62. if (substr(Yii::app()->getRequest()->pathInfo, 0, 6) == 'admin/') {
  63. $this->layout = 'application.modules.admin.views.layouts.main';
  64. //if the controller has admin action, set it to be default action for admin module
  65. // if (method_exists($this, 'actionAdmin'))
  66. // $this->defaultAction = 'admin';
  67. }
  68. parent::init();
  69. }
  70. public function beforeAction($action) {
  71. $separator = ' | ';
  72. $title = '';
  73. if ($action && ($action->id != 'index'))
  74. $title.= ucfirst($action->id) . ' - ';
  75. if ($this->module && ($this->module->getName() != $this->id))
  76. $title.= ucfirst($this->id) . ' ' . $separator;
  77. if ($this->module)
  78. $title.= ucfirst($this->module->getName()) . ' ' . $separator;
  79. $title .= Settings::get('site', 'name');
  80. $this->pageTitle = $title;
  81. return true;
  82. }
  83. public function actionToggle($id, $attribute, $model) {
  84. if (Yii::app()->request->isPostRequest) {
  85. // we only allow deletion via POST request
  86. $model = $this->loadModel($id, $model);
  87. //loadModel($id, $model) from giix
  88. ($model->$attribute == 1) ? $model->$attribute = 0 : $model->$attribute = 1;
  89. $model->save();
  90. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  91. if (!isset($_GET['ajax']))
  92. $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  93. }
  94. else
  95. throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  96. }
  97. protected function performAjaxValidation($model) {
  98. if (isset($_POST['ajax']) && $_POST['ajax'] === 'page-form') {
  99. echo CActiveForm::validate($model);
  100. Yii::app()->end();
  101. }
  102. }
  103. protected function block($name) {
  104. Block::run($name);
  105. }
  106. // public function getaPageTitle() {
  107. //
  108. // $separator = ' >> ';
  109. // $title = Settings::get('site', 'name');
  110. // if ($this->module)
  111. // $title.= $separator . ucfirst($this->module->getName());
  112. // if ($this->module->getName() != $this->id)
  113. // $title.= $separator . ucfirst($this->id);
  114. // if ($this->action->id != 'index')
  115. // $title.= $separator . ucfirst($this->action->id);
  116. // return $title;
  117. // parent::getPageTitle();
  118. // }
  119. protected function publishAssets() {
  120. $this->assetPath = Yii::app()->getAssetManager()->publish($this->viewPath . '/assets') . '/';
  121. Yii::app()->clientScript->registerScript('assetpath', '
  122. window.assetPath = "' . $this->assetPath . '";
  123. ', CClientScript::POS_READY);
  124. }
  125. public function beforeRender($view) {
  126. //some SEO stuffs here
  127. if ($this->pageRobotsIndex == false) {
  128. Yii::app()->clientScript->registerMetaTag('noindex', 'robots');
  129. }
  130. if (Settings::get('SEO', 'enable_meta_description_for_all_pages')) {
  131. $meta_description = Settings::get('SEO', 'meta_description');
  132. if (!empty($meta_description))
  133. Yii::app()->clientScript->registerMetaTag($meta_description, 'description');
  134. }
  135. if (Settings::get('SEO', 'enable_meta_keywords')) {
  136. if (empty($this->pageKeywords))
  137. $this->pageKeywords = Settings::get('SEO', 'meta_keywords');
  138. }
  139. if (Settings::get('SEO', 'enable_open_graph_meta_tags')) {
  140. $site_name = Awecms::getSiteName();
  141. if (!empty($site_name))
  142. Yii::app()->clientScript->registerMetaTag($site_name, NULL, NULL, array('property' => 'og:site_name'));
  143. if (!empty($this->pageTitle))
  144. Yii::app()->clientScript->registerMetaTag($this->pageTitle, NULL, NULL, array('property' => 'og:title'));
  145. $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
  146. Yii::app()->clientScript->registerMetaTag($protocol . $_SERVER['HTTP_HOST'] . Yii::app()->request->requestUri, NULL, NULL, array('property' => 'og:url'));
  147. if (!empty($meta_description))
  148. Yii::app()->clientScript->registerMetaTag($meta_description, NULL, NULL, array('property' => 'og:description'));
  149. }
  150. if ($this->pageKeywords)
  151. Yii::app()->clientScript->registerMetaTag($this->pageKeywords, 'keywords');
  152. Yii::app()->clientScript->registerMetaTag('AweCMS ' . Awecms::version, 'generator');
  153. return parent::beforeRender($view);
  154. }
  155. //this is a wild guess, at least try to show something
  156. public function missingAction($param) {
  157. throw new AweException(404);
  158. }
  159. }