PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Controller/Component/MenuManagerComponent.php

https://bitbucket.org/FuscaSoftware/candycane
PHP | 162 lines | 141 code | 17 blank | 4 comment | 25 complexity | 4a2b62d1463d5cbc3bac7fae08cbfe6d MD5 | raw file
  1. <?php
  2. class MenuManagerComponent extends Component
  3. {
  4. var $project_menu = array();
  5. var $application_menu = array();
  6. var $symbol_link = array();
  7. var $__selected = false;
  8. public function initialize(Controller $controller) {
  9. // saving the controller reference for later use
  10. $this->controller = $controller;
  11. $this->project_menu = $this->_getProjectMenu();
  12. $this->application_menu = $this->_getApplicationMenu();
  13. }
  14. public function startup(Controller $controller) {
  15. }
  16. function _detectProjectId()
  17. {
  18. $project_id = null;
  19. if ( isset($this->controller->params['project_id'])) {
  20. $project_id = $this->controller->params['project_id'];
  21. }
  22. if ( $this->controller->name == 'Versions') {
  23. $version_id = $this->controller->params['pass'][0];
  24. App::uses('Version', 'Model');
  25. $version = new Version();
  26. $bind = array(
  27. 'belongsTo' => array(
  28. 'Project' => array(
  29. 'className' => 'Project'
  30. )
  31. )
  32. );
  33. $version->bindModel($bind);
  34. $version_row = $version->find('first',array(
  35. 'condtions' => array(
  36. 'id' => $version_id
  37. )
  38. )
  39. );
  40. $project_id = $version_row['Project']['identifier'];
  41. }
  42. return $project_id;
  43. }
  44. public function beforeRender(Controller $controller) {
  45. $this->_prepareSelect();
  46. $this->_prepareMainmenu();
  47. $controller->set('main_menu', $this->menu_items);
  48. }
  49. function menu_item($id, $options = array()) {
  50. // TODO : now support only project menu
  51. $actions = $this->controller->params['action'];
  52. if (array_key_exists('only', $options)) {
  53. $actions = $options['only'];
  54. }
  55. if (!is_array($actions)) {
  56. $actions = array($actions);
  57. }
  58. foreach ($actions as $action) {
  59. $this->symbol_link[$this->controller->params['controller']][$action] = $id;
  60. }
  61. }
  62. function _prepareSelect() {
  63. // TODO : now support only project menu
  64. if (isset($this->symbol_link[$this->controller->params['controller']][$this->controller->params['action']])) {
  65. $symbol = $this->symbol_link[$this->controller->params['controller']][$this->controller->params['action']];
  66. $this->_select($this->project_menu[$symbol]);
  67. }
  68. }
  69. function _prepareMainmenu()
  70. {
  71. $meta_data = array();
  72. $project_id = $this->_detectProjectId();
  73. if ( $project_id ) {
  74. $meta_data = $this->_getProjectMenu($project_id);
  75. }
  76. if (isset($this->controller->request->params['project_id'])) {
  77. $meta_data = $this->_allowed_items($this->project_menu);
  78. } else {
  79. $meta_data = $this->application_menu;
  80. }
  81. $menu_data = array();
  82. foreach ($meta_data as $val) {
  83. if ( $val['controller'] == $this->controller->request->params['controller'] && $val['action'] == $this->controller->request->params['action'] && !$this->__selected ) {
  84. $this->_select($val);
  85. }
  86. if (array_key_exists('params', $val)) {
  87. $params = $val['params'];
  88. if (!is_array($params)) {
  89. $params = array($params);
  90. }
  91. foreach ($params as $param) {
  92. if (array_key_exists($param, $this->controller->request->params)) {
  93. $val[$param] = $this->controller->request->params[$param];
  94. }
  95. }
  96. unset($val['params']);
  97. }
  98. $menu_data[] = $val;
  99. }
  100. $this->menu_items = $menu_data;
  101. }
  102. function _getProjectMenu(){
  103. $menuContainer = ClassRegistry::getObject('MenuContainer');
  104. return $menuContainer->getProjectMenu();
  105. }
  106. function _getApplicationMenu()
  107. {
  108. return array();
  109. }
  110. function _select(&$item) {
  111. $item['class'] .= " selected";
  112. $this->__selected = true;
  113. }
  114. protected function _allowed_items($menu_items) {
  115. $allows = array();
  116. $User = & ClassRegistry::init('User');
  117. foreach ($menu_items as $key => $menu_item) {
  118. $allow = false;
  119. if (!empty($this->controller->current_user) && $User->is_allowed_to($this->controller->current_user, $this->__url($this->__to_allowed_action($menu_item)), $this->controller->_project)) {
  120. $allow = true;
  121. }
  122. if (isset($menu_item['_allowed']) && $menu_item['_allowed']) {
  123. unset($menu_item['_allowed']);
  124. $allow = true;
  125. }
  126. if($allow) {
  127. $allows[$key] = $menu_item;
  128. }
  129. }
  130. // for wiki existing check
  131. if ( is_null($this->controller->_project['Wiki']['start_page']) ) {
  132. unset($allows['wiki']);
  133. }
  134. return $allows;
  135. }
  136. function __url($menu_item) {
  137. return array_intersect_key($menu_item, array('controller'=>true,'action'=>true));
  138. }
  139. function __to_allowed_action($menu_item) {
  140. if ($menu_item['action'] == 'add') {
  141. $menu_item['action'] = 'new';
  142. }
  143. return $menu_item;
  144. }
  145. }