/protected/components/Controller.php

https://github.com/87central/GUS · PHP · 158 lines · 98 code · 13 blank · 47 comment · 7 complexity · 91c8c154bbd0907ca848905919c7d1e1 MD5 · raw file

  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. {
  8. private $_layout = 'column1';
  9. public function getLayout(){
  10. return $this->layoutDirectory . $this->layoutCore;
  11. }
  12. /**
  13. * Gets the layout name (the very last path segment in the layout path).
  14. * @return String The layout name.
  15. */
  16. protected function getLayoutCore(){
  17. return $this->_layout;
  18. }
  19. public function setLayout($layout){
  20. $this->_layout = $layout;
  21. $this->layout = $this->getLayout();
  22. }
  23. /**
  24. * @var array context menu items. This property will be assigned to {@link CMenu::items}.
  25. */
  26. public $menu=array();
  27. /**
  28. * @var array the breadcrumbs of the current page. The value of this property will
  29. * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
  30. * for more details on how to specify this property.
  31. */
  32. public $breadcrumbs=array();
  33. /**
  34. * @var array Extended context menu items.
  35. */
  36. public $pageOperations = array();
  37. /**
  38. * @var array The array of products menu items and child items. Built assuming
  39. * the EMenu extension is used for the menus.
  40. */
  41. public $products = array();
  42. /**
  43. * Indicates whether the current browser is a "mobile" browser instance.
  44. */
  45. public function getIsMobile(){
  46. /*$cookies = Yii::app()->request->cookies;
  47. $isMobile = Yii::app()->browser->isMobile();
  48. if(isset($cookies['SUPPRESS_MOBILE']) && $cookies['SUPPRESS_MOBILE']->value == 1){
  49. $isMobile = false;
  50. }
  51. //return true;
  52. $isMobile = false; //pending redesign of mobile site
  53. return $isMobile;*/
  54. return false;
  55. }
  56. protected function beforeAction($action){
  57. if(parent::beforeAction($action)){
  58. $this->setLayout('column1');
  59. if(isset($this->actionParams['mobile'])){
  60. $cookies = Yii::app()->request->cookies;
  61. $cookies['SUPPRESS_MOBILE'] = $this->createCookie('SUPPRESS_MOBILE', $this->actionParams['mobile']);
  62. }
  63. //build the menu
  64. if(Yii::app()->user->getState('isAdmin')){
  65. $this->products = $this->buildProductsMenu(Product::model()->findAll('STATUS <> '.Product::DELETED));
  66. }
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * Builds the menu of products given the array of products.
  74. * @param array $products The list of products to be accessible from the menu.
  75. */
  76. protected function buildProductsMenu($products){
  77. $protoMenu = array();
  78. foreach($products as $product){
  79. $protoMenu[$product->VENDOR->NAME][(string) $product->VENDOR_ITEM_ID] = array(
  80. 'url'=>array('/product/update', 'v'=>$product->VENDOR_ID, 'i'=>$product->VENDOR_ITEM_ID),
  81. 'label'=>$product->VENDOR_ITEM_ID,
  82. );
  83. }
  84. $menu = array();
  85. foreach($protoMenu as $label=>$subMenu){
  86. $items = array();
  87. foreach($subMenu as $subMenuItem){
  88. $items[] = $subMenuItem;
  89. }
  90. $menu[] = array('label'=>$label, 'items'=>$items);
  91. }
  92. return $menu;
  93. }
  94. /**
  95. * Creates a cookie.
  96. * @param string $name The name of the cookie.
  97. * @param string $value The value of the cookie.
  98. * @return CHttpCookie The cookie instance.
  99. */
  100. private function createCookie($name, $value){
  101. $cookie = new CHttpCookie($name, $value);
  102. $cookie->secure = false;
  103. $cookie->httpOnly = true;
  104. return $cookie;
  105. }
  106. protected function getLayoutDirectory(){
  107. $directory = '//layouts/desktop/';
  108. if($this->isMobile){
  109. $directory = '//layouts/mobile/';
  110. }
  111. $directory = '//layouts/';
  112. return $directory;
  113. }
  114. public function getStyleDirectory(){
  115. $directory = '/css/';
  116. if($this->isMobile){
  117. $directory = '/css/mobile/';
  118. }
  119. $directory = Yii::app()->request->baseUrl . $directory;
  120. return $directory;
  121. }
  122. public function getScriptDirectory(){
  123. return Yii::app()->request->baseUrl . '/assets/';
  124. }
  125. public function getMessages(){
  126. $userID = Yii::app()->user->id;
  127. $fromDate = strtotime('yesterday');
  128. $toDate = strtotime('+1 week');
  129. //get all events between yesterday and a week from today with which
  130. //the current user is associated, ordered by timestamp
  131. $criteria = new CDbCriteria();
  132. $criteria->condition = '`DATE` BETWEEN FROM_UNIXTIME(' . $fromDate . ') AND FROM_UNIXTIME(' . $toDate . ') AND (USER_ID = '.$userID.' OR USER_ASSIGNED = '.$userID.')';
  133. $criteria->limit = 5;
  134. $criteria->order = '`DATE`, `TIMESTAMP`';
  135. $events = EventLog::model()->findAll($criteria);
  136. $messages = array();
  137. foreach($events as $event){
  138. $message = $event->message;
  139. if($message){
  140. $messages[] = $event->message;
  141. }
  142. }
  143. return $messages;
  144. }
  145. }