navigation.plugin.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. //Déclaration d'un item de menu dans le menu principal
  3. function navigation_menu(){
  4. require_once(__DIR__.SLASH.'MenuItem.class.php');
  5. global $myUser,$conf;
  6. ?>
  7. <ul class="navbar-nav navigation-menu">
  8. <?php if ($myUser->connected()):
  9. foreach(MenuItem::bySlug('main-menu','') as $item):
  10. navigation_item_template($item);
  11. endforeach;
  12. endif; ?>
  13. </ul>
  14. <?php if($conf->get("navigation_allow_custom_menu")==0) return; ?>
  15. <ul class="navbar-nav navigation-shortcut-menu">
  16. <?php if ($myUser->connected()):
  17. foreach(MenuItem::bySlug('shortcut-menu',$myUser->login) as $item):
  18. navigation_item_template($item);
  19. endforeach;
  20. endif; ?>
  21. </ul>
  22. <?php
  23. }
  24. function navigation_item_template($item){
  25. global $myUser,$myFirm,$conf;
  26. $page = basename($_SERVER['REQUEST_URI']);
  27. $id = isset($item['id'])?$item['id']:'';
  28. $target = isset($item['target'])?$item['target']:'';
  29. $url = isset($item['url'])?$item['url']:'';
  30. $label = isset($item['label'])?$item['label']:'';
  31. $html = isset($item['html'])?$item['html']:'';
  32. if (isset($item['label']) && $item['label'] == 'Réglages') $page = basename($_SERVER['PHP_SELF']);
  33. $linkClasses = '';
  34. $classes = isset($item['classes'])? $item['classes']: '';
  35. if(isset($item['childs']) ){
  36. $linkClasses .= ' nav-link';
  37. if(count($item['childs'])!=0){
  38. $classes .= ' dropdown';
  39. $url = '';
  40. $linkClasses .= ' nav-link dropdown-toggle';
  41. }
  42. $mustShow = false;
  43. foreach ($item['childs'] as $child) {
  44. //Vérification des droits de visibilité sur le menu
  45. $childObject = new MenuItem();
  46. $childObject->fromArray($child);
  47. if($childObject->visibleFor($myUser)){
  48. $mustShow = true;
  49. break;
  50. }
  51. }
  52. if(!$mustShow && $item['url']=='') return;
  53. }
  54. if($item['parent'] && $item['parent']!='') $linkClasses .= 'dropdown-item ';
  55. $url = navigation_meta_link($url);
  56. $label = navigation_meta_link($label);
  57. $html = navigation_meta_link($html);
  58. $icon = isset($item['icon'])?'<i class="'.$item['icon'].'"></i> ':'';
  59. if($target=='iframe'){
  60. $target = '';
  61. $url = 'index.php?module=navigation&page=iframe&url='.base64_encode($url);
  62. }
  63. if($target=='redirect')
  64. $target = '';
  65. //Highlight du menu sélectionné
  66. if((!empty($url) && ((strpos($page, $url) !== false && $url != 'index.php') || $page == $url)))
  67. $classes .= ' active';
  68. //Vérification des droits de visibilité sur le menu
  69. $itemObject = new MenuItem();
  70. $itemObject->fromArray($item);
  71. if(!$itemObject->visibleFor($myUser)) return;
  72. //si les logs sont activés on desactive le direct link pour passer par la surcouche de logs
  73. if($conf->get('navigation_enable_log')) if(!empty($url)) $url = 'action.php?action=navigation_redirect&url='.base64_encode($url);
  74. ?>
  75. <li data-id="<?php echo $id; ?>" class="nav-item <?php echo $classes; ?>">
  76. <a class="<?php echo $linkClasses; ?>"
  77. <?php echo isset($item['childs']) && count($item['childs'])!=0? 'data-toggle="dropdown"':''; ?>
  78. title="<?php echo isset($item['title'])?$item['title']:''; ?>"
  79. target="<?php echo $target; ?>"
  80. onclick="<?php echo isset($item['onclick'])?$item['onclick']:''; ?>"
  81. href="<?php echo $url; ?>" >
  82. <?php echo $icon; ?><?php echo $label; ?>
  83. <?php echo $html; ?>
  84. </a>
  85. <?php if(isset($item['childs'])): ?>
  86. <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
  87. <?php
  88. foreach($item['childs'] as $child):
  89. navigation_item_template($child);
  90. endforeach;
  91. ?>
  92. </ul>
  93. <?php endif; ?>
  94. </li>
  95. <?php
  96. }
  97. //Application des données de templates sur l'url, libellé...
  98. function navigation_meta_link($string){
  99. global $myUser;
  100. $data = array('user'=>$myUser->toArray());
  101. $data['user']['meta'] = $myUser->meta;
  102. unset($data['password']);
  103. return template($string,$data);
  104. }
  105. //Cette fonction va generer une page quand on clique sur navigation dans menu
  106. function navigation_page(){
  107. global $_,$myUser;
  108. if(!isset($_['module']) || $_['module'] !='navigation') return;
  109. $page = !isset($_['page']) ? 'list' : $_['page'];
  110. $file = __DIR__.SLASH.'page.'.$page.'.php';
  111. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  112. require_once($file);
  113. }
  114. //Fonction executée lors de l'activation du plugin
  115. function navigation_install($id){
  116. if($id != 'fr.sys1.navigation') return;
  117. Entity::install(__DIR__);
  118. require_once(__DIR__.SLASH.'MenuItem.class.php');
  119. $mainMenu = new MenuItem();
  120. $mainMenu->label = 'Menu principal';
  121. $mainMenu->slug = 'main-menu';
  122. $mainMenu->user = '';
  123. $mainMenu->menu = 0;
  124. $mainMenu->editable = 0;
  125. $mainMenu->save();
  126. $pages = array();
  127. Plugin::callHook("menu_main", array(&$pages));
  128. uasort ($pages , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
  129. foreach($pages as $i=>$page):
  130. $icon = isset($page['icon'])? $page['icon'] : 'far fa-bookmark';
  131. $menuItem = new MenuItem();
  132. $menuItem->label = $page['label'];
  133. $menuItem->icon = $icon;
  134. $menuItem->url = $page['url'];
  135. $menuItem->user = '';
  136. $menuItem->menu = 0;
  137. $menuItem->sort = $i;
  138. $menuItem->slug = $mainMenu->slug.'_'.slugify($menuItem->label);
  139. $menuItem->menu = $mainMenu->id;
  140. $menuItem->save();
  141. endforeach;
  142. }
  143. //Fonction executée lors de la désactivation du plugin
  144. function navigation_uninstall($id){
  145. if($id != 'fr.sys1.navigation') return;
  146. Entity::uninstall(__DIR__);
  147. }
  148. //Déclaration des sections de droits du plugin
  149. function navigation_section(&$sections){
  150. $sections['navigation'] = "Gestion des droits sur le plugin navigation";
  151. }
  152. //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  153. function navigation_action(){
  154. require_once(__DIR__.SLASH.'action.php');
  155. }
  156. //Déclaration du menu de réglages
  157. function navigation_menu_setting(&$settingMenu){
  158. global $_, $myUser;
  159. if(!$myUser->can('navigation','configure')) return;
  160. $settingMenu[]= array(
  161. 'sort' =>1,
  162. 'url' => 'setting.php?section=global.navigation',
  163. 'icon' => 'fas fa-angle-right',
  164. 'label' => 'Menu & Navigation'
  165. );
  166. }
  167. //Déclaration des pages de réglages
  168. function navigation_content_setting(){
  169. global $_;
  170. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  171. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  172. }
  173. //Déclaration des settings de base
  174. //Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
  175. Configuration::setting('navigation',array(
  176. "Général",
  177. 'navigation_allow_custom_menu' => array("label"=>"Autoriser la création de menu personnalisés","legend"=>"Autorise aux utilisateurs la création de raccourcis dans la barre principale","type"=>"checkbox"),
  178. 'navigation_enable_log' => array("label"=>"Activer les logs au clic sur un menu","legend"=>"Cette option peut considérablement augmenter la taille de la base de données","type"=>"checkbox"),
  179. ));
  180. //Déclation des assets
  181. Plugin::addCss("/css/main.css");
  182. Plugin::addJs("/js/main.js");
  183. Plugin::addHook("menu_user", function(&$userMenu){
  184. global $conf, $myUser;
  185. if($conf->get("navigation_allow_custom_menu")==0 && !$myUser->superadmin) return;
  186. $userMenu[]= array(
  187. 'sort' => 0.5,
  188. 'label' => 'Mes raccourcis',
  189. 'icon' => 'far fa-fw fa-star',
  190. 'url' => 'setting.php?section=global.navigation&menu=mine'
  191. );
  192. });
  193. function navigation_widget(&$widgets){
  194. global $myUser;
  195. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  196. $modelWidget = new DashboardWidget();
  197. $modelWidget->model = 'menu';
  198. $modelWidget->title = 'Menu';
  199. $modelWidget->icon = 'far fa-compass';
  200. $modelWidget->background = '#130f40';
  201. $modelWidget->load = 'action.php?action=navigation_widget_load';
  202. $modelWidget->js = [Plugin::url().'/js/widget.js?v=0'];
  203. $modelWidget->css = [Plugin::url().'/css/widget.css?v=0'];
  204. $modelWidget->configure = 'action.php?action=navigation_widget_configure';
  205. $modelWidget->configure_callback = 'navigation_widget_configure_save';
  206. $modelWidget->configure_init = 'navigation_widget_configure_init';
  207. $modelWidget->description = "Affiche un menu sélectionné";
  208. $widgets[] = $modelWidget;
  209. }
  210. //Mapping hook / fonctions
  211. Plugin::addHook("widget", "navigation_widget");
  212. Plugin::addHook("install", "navigation_install");
  213. Plugin::addHook("uninstall", "navigation_uninstall");
  214. Plugin::addHook("section", "navigation_section");
  215. Plugin::addHook("header", "navigation_menu");
  216. Plugin::addHook("page", "navigation_page");
  217. Plugin::addHook("action", "navigation_action");
  218. Plugin::addHook("menu_setting", "navigation_menu_setting");
  219. Plugin::addHook("content_setting", "navigation_content_setting");
  220. ?>