action.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. //Permet le log/surcouhe d'une redirection de navigation
  3. Action::register('navigation_redirect',function(&$response){
  4. global $_;
  5. $url = base64_decode($_['url']);
  6. Log::put("Ouverture de l'url: ".$url,'Navigation');
  7. Plugin::callHook('navigation_redirect',array($url));
  8. header('location: '.$url);
  9. });
  10. //Récuperation d'une liste de menuitem
  11. Action::register('navigation_tree_search',function(&$response){
  12. global $myUser,$_,$myFirm;
  13. if (!$myUser->connected()) return;
  14. require_once(__DIR__.SLASH.'MenuItem.class.php');
  15. if(!isset($_['menu']) || !is_numeric($_['menu'])) throw new Exception("Menu non spécifié");
  16. if(!$menu = MenuItem::getById($_['menu'])) throw new Exception("Aucun menu ne correspond en base");
  17. $response['rows'] = array();
  18. $filters = array('menu'=>$_['menu']);
  19. if(!$myUser->can('navigation','configure')) $filters['user'] = $myUser->login;
  20. $items = array();
  21. //affichage des menu de la firm ou des menu inter firm (0)
  22. if(isset($_['firm'])){
  23. if(!$myUser->superadmin && !$myUser->haveFirm($_['firm'])) throw new Exception('Vous ne pouvez pas modifier le menu d\'un établissement auquel vous n\'avez pas accès');
  24. $filters['firm'] = $_['firm'];
  25. }else{
  26. $filters['firm:IN'] = array($myFirm->id,0);
  27. }
  28. //Récupération des catégories principales
  29. foreach(MenuItem::loadAll($filters, array('sort')) as $item){
  30. $row = $item->toArray();
  31. $row['class'] = $item->parent!='' ? 'navigation-line-child' : '';
  32. if(!$row['editable']) $row['class'] .= ' not-editable';
  33. $row['target'] = MenuItem::target(!empty($item->target)?$item->target:'redirect');
  34. $row['shortcut'] = ($menu->slug == MenuItem::MENU_SHORTCUT && !empty($menu->user)) ? true : false;
  35. $items[$row['id']] = $row;
  36. if(empty($row['parent']))
  37. $response['rows'][$row['id']] = $row;
  38. }
  39. //Classement pas catégories
  40. foreach ($items as $k=>$item) {
  41. if(empty($item['parent'])) continue;
  42. if(!isset($response['rows'][$item['parent']]['childs'])) $response['rows'][$item['parent']]['childs'] = array();
  43. $response['rows'][$item['parent']]['childs'][] = $item;
  44. }
  45. //Tri des sous catégories par ordre de sort
  46. foreach ($response['rows'] as $key=>$parent) {
  47. if(!isset($response['rows'][$key]['childs'])) continue;
  48. usort($response['rows'][$key]['childs'], function($a,$b){
  49. return $a['sort'] - $b['sort'];
  50. });
  51. }
  52. //Le array_values permet la réinitialisation des clés qui impactent sur l'ordre final
  53. $response['rows'] = array_values($response['rows']);
  54. });
  55. //Sauvegarde des configurations de navigation
  56. Action::register('navigation_setting_save',function(&$response){
  57. global $myUser,$_,$conf,$myFirm;
  58. User::check_access('navigation','edit');
  59. require_once(__DIR__.SLASH.'MenuItem.class.php');
  60. if($conf->get("navigation_allow_custom_menu")==0 && !$myUser->can('navigation','configure')) throw new Exception("Permissions insuffisantes", 403);
  61. if(!isset($_['menu']) || !is_numeric($_['menu'])) throw new Exception("Menu non spécifié");
  62. if(isset($_['fields'])){
  63. foreach(Configuration::setting('navigation') as $key=>$value){
  64. if(!is_array($value)) continue;
  65. $allowed[] = $key;
  66. }
  67. foreach ($_['fields'] as $key => $value)
  68. if(in_array($key, $allowed)) $conf->put($key,$value);
  69. }
  70. $filters = array('menu'=>$_['menu'],'firm'=>$_['firm']);
  71. if(!$myUser->can('navigation','configure')) $filters['user'] = $myUser->login;
  72. if(!$myUser->superadmin) $filters['editable'] = '1';
  73. if(!$myUser->superadmin && $_['firm']==0) throw new Exception('Seul un superadmin peut définir un menu pour tous les établissements');
  74. if(!$myUser->superadmin && !$myUser->haveFirm($_['firm'])) throw new Exception('Vous ne pouvez pas modifier le menu d\'un établissement auquel vous n\'avez pas accès');
  75. $menuItems = array();
  76. $parameters = array('menu'=>$_['menu'],'firm'=>$_['firm']);
  77. if(!empty($_['items'])){
  78. foreach($_['items'] as $i => $item){
  79. $menuItem = !empty($item['id']) ? MenuItem::getById($item['id']) : new MenuItem();
  80. $menuItem->menu = $_['menu'];
  81. $menuItem->firm = $_['firm'];
  82. $menuItem->icon = $item['icon'];
  83. $menuItem->label = $item['label'];
  84. if(isset($item['url'])) $menuItem->url = str_replace(ROOT_URL.'/','',$item['url']);
  85. $menuItem->target = isset($item['target']) && !empty($item['target']) ? $item['target'] : 'redirect';
  86. if(isset($item['visibility'])) $menuItem->visibility = $item['visibility'];
  87. $menuItem->user = $myUser->can('navigation','configure') ? '' : $myUser->login;
  88. $menuItem->sort = $i;
  89. $menuItem->parent = NULL;
  90. $menuItem->slug = slugify($menuItem->label);
  91. if(MenuItem::rowCount(array('slug'=>$menuItem->slug)) < 0)
  92. $item->slug .='_'.time();
  93. $menuItem->save();
  94. $menuItems[] = $menuItem->id;
  95. if(!empty($item['childs'])){
  96. foreach($item['childs'] as $j => $subItem){
  97. $menuSubItem = !empty($subItem['id']) ? MenuItem::getById($subItem['id']) : new MenuItem();
  98. $menuSubItem->menu = $_['menu'];
  99. $menuSubItem->firm = $_['firm'];
  100. $menuSubItem->icon = $subItem['icon'];
  101. $menuSubItem->label = $subItem['label'];
  102. if(isset($subItem['url'])) $menuSubItem->url = str_replace(ROOT_URL.'/','',$subItem['url']);
  103. if(isset($subItem['target'])) $menuSubItem->target = $subItem['target'];
  104. if(isset($subItem['visibility'])) $menuSubItem->visibility = $subItem['visibility'];
  105. $menuSubItem->user = $myUser->can('navigation','configure') ? '' : $myUser->login;
  106. $menuSubItem->sort = $j;
  107. $menuSubItem->parent = $menuItem->id;
  108. $menuSubItem->slug = $menuItem->slug.'_'.slugify($menuSubItem->label);
  109. if(MenuItem::rowCount(array('slug'=>$menuSubItem->slug)) <0)
  110. $menuSubItem->slug .='_'.time();
  111. $menuSubItem->save();
  112. $menuItems[] = $menuSubItem->id;
  113. }
  114. } else {
  115. MenuItem::change(array('parent'=>NULL), array('parent'=>$item['id']));
  116. }
  117. }
  118. if(!empty($menuItems)) $parameters['id:NOT IN'] = implode(',', $menuItems);
  119. }
  120. MenuItem::delete($parameters);
  121. });
  122. Action::register('navigation_widget_load',function(&$response){
  123. global $myUser;
  124. User::check_access('navigation','read');
  125. Plugin::need('dashboard/DashboardWidget');
  126. $widget = DashboardWidget::current();
  127. if(empty($widget->data('title'))){
  128. $widget->title = 'Bloc Menu';
  129. }else{
  130. $widget->title = '';
  131. $widget->icon = '';
  132. }
  133. if(!empty($widget->data('color'))) $widget->background = $widget->data('color');
  134. ob_start();
  135. require_once(__DIR__.SLASH.'widget.php');
  136. $widget->content = ob_get_clean();
  137. echo json_encode($widget);
  138. exit;
  139. });
  140. Action::register('navigation_widget_configure_save',function(&$response){
  141. global $myUser,$_;
  142. User::check_access('navigation','read');
  143. Plugin::need('dashboard/DashboardWidget');
  144. $widget = DashboardWidget::getById($_['id']);
  145. $url = str_replace(ROOT_URL.'/','',$_['widget-url']);
  146. $widget->data('url',$url);
  147. $widget->data('redirect',$_['widget-redirect']);
  148. $widget->data('icon',$_['widget-icon']);
  149. $widget->data('title',$_['widget-title']);
  150. $widget->data('color',$_['widget-color']);
  151. $widget->save();
  152. });
  153. Action::register('navigation_widget_configure',function(&$response){
  154. global $myUser;
  155. User::check_access('navigation','read');
  156. Plugin::need('dashboard/DashboardWidget');
  157. $widget = DashboardWidget::current();
  158. ob_start();
  159. require_once(__DIR__.SLASH.'widget.configure.php');
  160. $content = ob_get_clean();
  161. echo $content;
  162. exit;
  163. });
  164. Action::register('page_autocomplete',function(&$response){
  165. global $myUser,$_,$myFirm;
  166. User::check_access('navigation','read');
  167. require_once(__DIR__.SLASH.'MenuItem.class.php');
  168. if(!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  169. if(empty($_['keyword'])) return;
  170. $rows = array();
  171. foreach(MenuItem::staticQuery('SELECT item.* FROM {{table}} item LEFT JOIN {{table}} menu ON item.menu=menu.id WHERE item.label LIKE ? AND item.menu!=? AND menu.user=?',array('%'.$_['keyword'].'%',0,$myUser->login),true) as $menu){
  172. $row = $menu->toArray();
  173. $rows[] = $row;
  174. }
  175. Plugin::callHook("menu_main", array(&$pages));
  176. foreach($pages as $page){
  177. if( strpos(slugify($page['label']), slugify($_["keyword"])) === false ) continue;
  178. $icon = isset($page['icon'])? $page['icon'] : 'far fa-bookmark';
  179. $row = $page;
  180. $row['label'] = $page['label'];
  181. $rows[] = $row;
  182. }
  183. usort ($rows , function($a,$b){return $a['sort']>$b['sort']?-1:1;});
  184. $response['rows'] = $rows;
  185. });
  186. Action::register('page_by_uid',function(&$response){
  187. global $myUser,$_;
  188. if (!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  189. require_once(__DIR__.SLASH.'MenuItem.class.php');
  190. $response['items'] = array();
  191. //@TODO
  192. // $query = 'SELECT item.* FROM {{table}} item WHERE item.label LIKE ? AND item.menu!=? AND menu.user=? IN(';
  193. // $query .= implode(',', array_fill(0, count($_['items']), '?'));
  194. // $query .= ')';
  195. // foreach(MenuItem::staticQuery($query,$_['items'],true) as $client) {
  196. // $row = $client->toArray();
  197. // unset($row['parent']);
  198. // if(!empty($client->foreign('parentLabel'))) $row['parent'] = array('label'=>$client->foreign('parentLabel'));
  199. // $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
  200. // if(!empty($row['pseudonym'])) $row['label'].= ' ('.html_entity_decode($row['pseudonym'], ENT_QUOTES).')' ;
  201. // $response['items'][$row['id']] = $row;
  202. // }
  203. });