action.php 7.0 KB

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