PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/menu_controller.php

https://github.com/ata/steak
PHP | 148 lines | 124 code | 17 blank | 7 comment | 20 complexity | ecb2280ae5a95edbf76b28d6a874bf8b MD5 | raw file
  1. <?php
  2. class MenuController extends AppController {
  3. var $name = 'Menu';
  4. var $helpers = array('Html', 'Form');
  5. var $uses = array('Link');
  6. function beforeFilter() {
  7. parent :: beforeFilter();
  8. $this->Auth->allowedActions = array (
  9. '*'
  10. );
  11. }
  12. function index(){
  13. $links = $this->Link->find('all', array('conditions'=>array('Link.parent_id'=>NULL)));
  14. $this->set(compact('links'));
  15. }
  16. function add($sub=false){
  17. if($this->data){
  18. $data = $this->data;
  19. if(!$sub){
  20. $data['Link']['is_show'] = 1;
  21. }
  22. if($this->Link->save($data)){
  23. if(!$sub) {
  24. // reload main menu cache
  25. $mainMenu = $this->_getMainMenu();
  26. Cache::write('MainMenu',$mainMenu);
  27. } else {
  28. // clear submenu
  29. Cache::delete('SubMenu'.$this->data['Link']['parent_id']);
  30. }
  31. $this->Session->setFlash(__('Modul berhasil ditambah', true));
  32. }else{
  33. $this->Session->setFlash(__('Gagal', true));
  34. }
  35. $this->redirect('/menu/index');
  36. }
  37. $listGroup = $this->Link->Group->find('list');
  38. $listMenu = $this->_listMenu();
  39. $listParent = $this->Link->find('list', array('conditions'=>array('Link.parent_id'=>NULL)));
  40. $this->set(compact('listMenu','listParent', 'listGroup'));
  41. }
  42. function edit($id=null){
  43. if (!$id && empty($this->data)) {
  44. $this->flash(__('Invalid Module Id', true), array('action'=>'index'));
  45. }
  46. if($this->data){
  47. $data = $this->data;
  48. if(!$sub){
  49. $data['Link']['is_show'] = 1;
  50. }
  51. $oldLink = $this->Link->read(null, $this->data['Link']['id']);
  52. if($this->Link->save($data)){
  53. if(!$sub) {
  54. // reload main menu cache
  55. $mainMenu = $this->_getMainMenu();
  56. Cache::write('MainMenu',$mainMenu);
  57. } else {
  58. // clear submenu
  59. Cache::delete('SubMenu'.$this->data['Link']['parent_id']);
  60. Cache::delete('SubMenu'.$oldLink['Link']['parent_id']);
  61. }
  62. $this->Session->setFlash(__('Modul berhasil ditambah', true));
  63. }else{
  64. $this->Session->setFlash(__('Gagal', true));
  65. }
  66. $this->redirect('/menu/index');
  67. }
  68. if (empty($this->data)) {
  69. $this->data = $this->Link->read(null, $id);
  70. }
  71. $listGroup = $this->Link->Group->find('list');
  72. $listMenu = $this->_listMenu(false);
  73. $listParent = $this->Link->find('list', array('conditions'=>array('Link.parent_id'=>NULL)));
  74. $this->set(compact('listMenu','listParent', 'listGroup'));
  75. }
  76. function delete($id) {
  77. $link = $this->Link->read(null, $id);
  78. if (!$id) {
  79. $this->Session->setFlash(__('Invalid id for Menu', true));
  80. $this->redirect(array('action'=>'index'));
  81. }
  82. if ($this->Link->del($id)) {
  83. // clear submenu
  84. Cache::delete('SubMenu'.$link['Link']['parent_id']);
  85. $this->Session->setFlash(__('Menu deleted', true));
  86. $this->redirect(array('action'=>'index'));
  87. }
  88. }
  89. function _listMenu($exclude = true) {
  90. $menu = array();
  91. $controllers = Configure::listObjects('controller');
  92. $controllers = array_diff($controllers,array('App','Pages'));
  93. $baseMethods = get_class_methods('Controller');
  94. if($exclude) {
  95. $links = $this->Link->find('all');
  96. $excludeLinks = array();
  97. foreach($links as $link) {
  98. $excludeLinks[] = $link['Link']['path'];
  99. }
  100. }
  101. // look at each controller in app/controllers
  102. foreach ($controllers as $ctrlName) {
  103. App::import('Controller', $ctrlName);
  104. $ctrlclass = $ctrlName . 'Controller';
  105. $methods = get_class_methods($ctrlclass);
  106. //clean the methods. to remove those in Controller and private actions.
  107. foreach ($methods as $k => $method) {
  108. if (strpos($method, '_', 0) === 0) {
  109. unset($methods[$k]);
  110. continue;
  111. }
  112. if (in_array($method, $baseMethods)) {
  113. unset($methods[$k]);
  114. continue;
  115. }
  116. if($exclude && in_array("/".Inflector::underscore($ctrlName."/".$method), $excludeLinks)) {
  117. unset($methods[$k]);
  118. continue;
  119. }
  120. $path = '/'.Inflector::underscore($ctrlName).'/'.$method;
  121. $menu[$path] = $path;
  122. }
  123. }
  124. ksort($menu);
  125. return $menu;
  126. }
  127. function check(){
  128. $this->autoRender = false;
  129. }
  130. }
  131. ?>