action.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. //permet le log/surcouhe d'une redirection de navigation
  5. case 'navigation_redirect':
  6. $url = base64_decode($_['url']);
  7. Log::put("Ouverture de l'url: ".$url,'navigation');
  8. Plugin::callHook('navigation_redirect',array($url));
  9. header('location: '.$url);
  10. break;
  11. //Récuperation d'une liste de menuitem
  12. case 'navigation_tree_search':
  13. Action::write(function(&$response){
  14. global $myUser,$_;
  15. if (!$myUser->connected()) return;
  16. require_once(__DIR__.SLASH.'MenuItem.class.php');
  17. if(!isset($_['menu']) || !is_numeric($_['menu'])) throw new Exception("Menu non spécifié");
  18. $response['rows'] = array();
  19. $filters = array('menu'=>$_['menu']);
  20. if(!$myUser->can('navigation','configure')) $filters['user'] = $myUser->login;
  21. $items = array();
  22. //recup des catégories principales
  23. foreach(MenuItem::loadAll($filters,array('sort')) as $item){
  24. $row = $item->toArray();
  25. $row['class'] = $item->parent!='' ? 'navigation-line-child' : '';
  26. if(!$row['editable']) $row['class'] .= ' not-editable';
  27. $row['target'] = MenuItem::target($item->target);
  28. $items[$row['id']] = $row;
  29. if(empty($row['parent']))
  30. $response['rows'][$row['id']] = $row;
  31. }
  32. //Classement pas catégories
  33. foreach ($items as $k=>$item) {
  34. if(empty($item['parent'])) continue;
  35. if(!isset($response['rows'][$item['parent']]['childs'])) $response['rows'][$item['parent']]['childs'] = array();
  36. $response['rows'][$item['parent']]['childs'][] = $item;
  37. }
  38. //Tri des sous catégories par ordre de sort
  39. foreach ($response['rows'] as $key=>$parent) {
  40. if(!isset($response['rows'][$key]['childs'])) continue;
  41. usort($response['rows'][$key]['childs'],function($a,$b){
  42. if($a['sort'] > $b['sort']) return 1;
  43. if($a['sort'] < $b['sort']) return -1;
  44. if($a['sort'] == $b['sort']) return 0;
  45. });
  46. }
  47. //le array value permet la réinitialisation des clés qui impactent sur l'ordre final
  48. $response['rows'] = array_values($response['rows']);
  49. });
  50. break;
  51. //Sauvegarde des configurations de navigation
  52. case 'navigation_setting_save':
  53. Action::write(function(&$response){
  54. global $myUser,$_,$conf;
  55. require_once(__DIR__.SLASH.'MenuItem.class.php');
  56. if($conf->get("navigation_allow_custom_menu")==0 && !$myUser->can('navigation','configure')) throw new Exception("Error Processing Request", 403);
  57. if(!isset($_['menu']) || !is_numeric($_['menu'])) throw new Exception("Menu non spécifié");
  58. foreach(Configuration::setting('navigation') as $key=>$value){
  59. if(!is_array($value)) continue;
  60. $allowed[] = $key;
  61. }
  62. foreach ($_['fields'] as $key => $value)
  63. if(in_array($key, $allowed)) $conf->put($key,$value);
  64. $filters = array('menu'=>$_['menu']);
  65. if(!$myUser->can('navigation','configure')) $filters['user']=$myUser->login;
  66. if(!$myUser->superadmin) $filters['editable'] = '1';
  67. MenuItem::delete($filters);
  68. if(isset($_['items'])){
  69. foreach ($_['items'] as $i => $item){
  70. $menuItem = new MenuItem();
  71. $menuItem->menu = $_['menu'];
  72. $menuItem->icon = $item['icon'];
  73. $menuItem->label = $item['label'];
  74. if(isset($item['url'])) $menuItem->url = str_replace(ROOT_URL.'/','',$item['url']);
  75. if(isset($item['target'])) $menuItem->target = $item['target'];
  76. if(isset($item['visibility'])) $menuItem->visibility = $item['visibility'];
  77. $menuItem->user = $myUser->can('navigation','configure') ? '' : $myUser->login;
  78. $menuItem->slug = slugify($menuItem->label);
  79. $menuItem->sort = $i;
  80. if(MenuItem::rowCount(array('slug'=>$menuItem->slug)) <0 )
  81. $item->slug .='_'.time();
  82. $menuItem->save();
  83. if(isset($item['childs'])){
  84. foreach($item['childs'] as $u=>$subItem){
  85. $menuSubItem = new MenuItem();
  86. $menuSubItem->menu = $_['menu'];
  87. $menuSubItem->icon = $subItem['icon'];
  88. $menuSubItem->label = $subItem['label'];
  89. if(isset($subItem['url'])) $menuSubItem->url = str_replace(ROOT_URL.'/','',$subItem['url']);
  90. if(isset($subItem['target'])) $menuSubItem->target = $subItem['target'];
  91. if(isset($subItem['visibility'])) $menuSubItem->visibility = $subItem['visibility'];
  92. $menuSubItem->user = $myUser->can('navigation','configure') ? '' : $myUser->login;
  93. $menuSubItem->sort = $u;
  94. $menuSubItem->slug = $menuItem->slug.'_'.slugify($menuItem->label);
  95. if(MenuItem::rowCount(array('slug'=>$menuSubItem->slug)) <0 )
  96. $menuSubItem->slug .='_'.time();
  97. $menuSubItem->parent = $menuItem->id;
  98. $menuSubItem->save();
  99. }
  100. }
  101. }
  102. }
  103. });
  104. break;
  105. case 'navigation_widget_load':
  106. global $myUser;
  107. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  108. $widget = DashboardWidget::current();
  109. if( $widget->data('title') == ""){
  110. $widget->title = 'Bloc Menu';
  111. }else{
  112. $widget->title = '';
  113. $widget->icon = '';
  114. }
  115. if($widget->data('color') != "") $widget->background = $widget->data('color');
  116. ob_start();
  117. require_once(__DIR__.SLASH.'widget.php');
  118. $widget->content = ob_get_clean();
  119. echo json_encode($widget);
  120. break;
  121. case 'navigation_widget_configure_save':
  122. Action::write(function(&$response){
  123. global $myUser,$_;
  124. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  125. $widget = DashboardWidget::getById($_['id']);
  126. $url = str_replace(ROOT_URL.'/','',$_['widget-url']);
  127. $widget->data('icon',$_['widget-icon']);
  128. $widget->data('title',$_['widget-title']);
  129. $widget->data('color',$_['widget-color']);
  130. $widget->data('url',$url);
  131. $widget->save();
  132. });
  133. break;
  134. case 'navigation_widget_configure':
  135. global $myUser;
  136. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  137. $widget = DashboardWidget::current();
  138. ob_start();
  139. require_once(__DIR__.SLASH.'widget.configure.php');
  140. $content = ob_get_clean();
  141. echo $content ;
  142. break;
  143. case 'navigation_widget_configure_autocomplete':
  144. Action::write(function(&$response){
  145. global $myUser,$_;
  146. require_once(__DIR__.SLASH.'MenuItem.class.php');
  147. if(!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  148. $rows = array();
  149. if($_['keyword'] == '') return;
  150. foreach(MenuItem::staticQuery('SELECT item.* FROM {{table}} item LEFT JOIN {{table}} menu ON item.menu=menu.id WHERE item.label LIKE ? AND item.menu!=0 AND menu.user=?',array('%'.$_['keyword'].'%',$myUser->login),true) as $menu){
  151. $row = $menu->toArray();
  152. $row['name'] = $row['label'];
  153. $rows[] = $row;
  154. }
  155. Plugin::callHook("menu_main", array(&$pages));
  156. foreach($pages as $page){
  157. if( strpos(slugify($page['label']), slugify($_["keyword"])) === false ) continue;
  158. $icon = isset($page['icon'])? $page['icon'] : 'far fa-bookmark';
  159. $row = $page;
  160. $row['name'] = $page['label'];
  161. $rows[] = $row;
  162. }
  163. usort ($rows , function($a,$b){return $a['sort']>$b['sort']?-1:1;});
  164. $response['rows'] = $rows;
  165. });
  166. break;
  167. }
  168. ?>