navigation.plugin.php 8.5 KB

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