navigation.plugin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. require_once(__DIR__.SLASH.'action.php');
  3. //Déclaration d'un item de menu dans le menu principal
  4. function navigation_menu(){
  5. require_once(__DIR__.SLASH.'MenuItem.class.php');
  6. global $myUser,$conf,$myFirm,$menu;
  7. $menu = MenuItem::bySlug(array('footer-menu','main-menu'),'',array($myFirm->id,0));
  8. ?>
  9. <ul class="navbar-nav navigation-menu">
  10. <?php foreach($menu['main-menu'] as $item):
  11. navigation_item_template($item);
  12. endforeach; ?>
  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,array($myFirm->id,0)) as $item):
  18. navigation_item_template($item);
  19. endforeach;
  20. endif; ?>
  21. </ul>
  22. <?php
  23. }
  24. function navigation_menu_footer(&$items){
  25. global $menu;
  26. require_once(__DIR__.SLASH.'MenuItem.class.php');
  27. $items = $menu['footer-menu'];
  28. //$items = array_merge($items,MenuItem::bySlug('footer-menu'));
  29. }
  30. function navigation_item_template($item){
  31. global $myUser,$conf;
  32. require_once(__DIR__.SLASH.'MenuItem.class.php');
  33. $page = basename($_SERVER['REQUEST_URI']);
  34. $id = !empty($item['id']) ? $item['id'] : '';
  35. $target = !empty($item['target']) ? $item['target'] : '';
  36. $url = !empty($item['url']) ? $item['url'] : '';
  37. $label = !empty($item['label']) ? $item['label'] : '';
  38. $html = !empty($item['html']) ? $item['html'] : '';
  39. if(!empty($item['label']) && $item['label'] == 'Réglages') $page = basename($_SERVER['PHP_SELF']);
  40. $classes = !empty($item['classes']) ? $item['classes'] : '';
  41. $linkClasses = array('nav-link');
  42. if(!empty($item['childs'])){
  43. if(count($item['childs'])!=0){
  44. $classes .= ' dropdown';
  45. $url = '';
  46. $linkClasses[] = ' dropdown-toggle';
  47. }
  48. $mustShow = false;
  49. foreach($item['childs'] as $child) {
  50. //Vérification des droits de visibilité sur le menu
  51. $childObject = new MenuItem();
  52. $childObject->fromArray($child);
  53. if($childObject->visibleFor($myUser)){
  54. $mustShow = true;
  55. break;
  56. }
  57. }
  58. if(!$mustShow && empty($item['url'])) return;
  59. }
  60. if(!empty($item['parent'])){
  61. //On supprime la 1ère classe "nav-link" car c'est un sous-item
  62. unset($linkClasses[0]);
  63. $linkClasses[] = ' dropdown-item';
  64. }
  65. $url = navigation_meta_link($url);
  66. $label = navigation_meta_link($label);
  67. $html = navigation_meta_link($html);
  68. $icon = !empty($item['icon']) ? '<i class="'.$item['icon'].'"></i> ' : '';
  69. if($target=='iframe'){
  70. $target = '';
  71. $url = 'index.php?module=navigation&page=iframe&url='.base64_encode($url);
  72. }
  73. if($target=='redirect') $target = '';
  74. //Highlight du menu sélectionné
  75. if((!empty($url) && ((preg_match('|'.preg_quote($url).'[&\?]|i',$page,$match) && $url != 'index.php') || $page == $url)))
  76. $classes .= ' active';
  77. //Vérification des droits de visibilité sur le menu
  78. $itemObject = new MenuItem();
  79. $itemObject->fromArray($item);
  80. if(!$itemObject->visibleFor($myUser)) return;
  81. //Si les logs sont activés, on désactive le direct link pour passer par la surcouche de logs
  82. if($conf->get('navigation_enable_log')) if(!empty($url)) $url = 'action.php?action=navigation_redirect&url='.base64_encode($url);
  83. ?>
  84. <li data-id="<?php echo $id; ?>" class="nav-item <?php echo $classes; ?>">
  85. <a class="<?php echo implode(' ', $linkClasses); ?>"
  86. <?php echo isset($item['childs']) && count($item['childs'])!=0? 'data-toggle="dropdown"':''; ?>
  87. title="<?php echo isset($item['title'])?$item['title']:''; ?>"
  88. target="<?php echo $target; ?>"
  89. onclick="<?php echo isset($item['onclick'])?$item['onclick']:''; ?>"
  90. href="<?php echo $url; ?>">
  91. <?php echo $icon; ?><?php echo $label; ?>
  92. <?php echo $html; ?>
  93. </a>
  94. <?php if(isset($item['childs'])): ?>
  95. <ul class="dropdown-menu py-1" aria-labelledby="navbarDropdown">
  96. <?php foreach($item['childs'] as $child)
  97. navigation_item_template($child);
  98. ?>
  99. </ul>
  100. <?php endif; ?>
  101. </li>
  102. <?php
  103. }
  104. //Application des données de templates sur l'url, libellé...
  105. function navigation_meta_link($string){
  106. global $myUser;
  107. $data = array('user'=>$myUser->toArray());
  108. $data['user']['meta'] = $myUser->meta;
  109. unset($data['password']);
  110. return template($string,$data);
  111. }
  112. //Cette fonction va generer une page quand on clique sur navigation dans menu
  113. function navigation_page(){
  114. global $_;
  115. if(!isset($_['module']) || $_['module'] !='navigation') return;
  116. $page = !isset($_['page']) ? 'list' : $_['page'];
  117. $file = __DIR__.SLASH.'page.'.$page.'.php';
  118. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  119. require_once($file);
  120. }
  121. //Fonction executée lors de l'activation du plugin
  122. function navigation_install($id){
  123. if($id != 'fr.core.navigation') return;
  124. Entity::install(__DIR__);
  125. require_once(__DIR__.SLASH.'MenuItem.class.php');
  126. $mainMenu = new MenuItem();
  127. $mainMenu->label = 'Menu principal';
  128. $mainMenu->slug = 'main-menu';
  129. $mainMenu->user = '';
  130. $mainMenu->menu = 0;
  131. $mainMenu->editable = 0;
  132. $mainMenu->save();
  133. $pages = array();
  134. Plugin::callHook("menu_main", array(&$pages));
  135. uasort ($pages , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
  136. foreach($pages as $i=>$page):
  137. $icon = isset($page['icon'])? $page['icon'] : 'far fa-bookmark';
  138. $menuItem = new MenuItem();
  139. $menuItem->label = $page['label'];
  140. $menuItem->icon = $icon;
  141. $menuItem->url = $page['url'];
  142. $menuItem->user = '';
  143. $menuItem->menu = 0;
  144. $menuItem->sort = $i;
  145. $menuItem->slug = $mainMenu->slug.'_'.slugify($menuItem->label);
  146. $menuItem->menu = $mainMenu->id;
  147. $menuItem->save();
  148. endforeach;
  149. $mainMenu = new MenuItem();
  150. $mainMenu->label = 'Menu pied de page';
  151. $mainMenu->slug = 'footer-menu';
  152. $mainMenu->user = '';
  153. $mainMenu->menu = 0;
  154. $mainMenu->editable = 0;
  155. $mainMenu->save();
  156. }
  157. //Fonction executée lors de la désactivation du plugin
  158. function navigation_uninstall($id){
  159. if($id != 'fr.core.navigation') return;
  160. Entity::uninstall(__DIR__);
  161. }
  162. //Déclaration des sections de droits du plugin
  163. //Déclaration des sections de droits du plugin
  164. Right::register('navigation',array('label'=>'Gestion des droits sur le plugin navigation'));
  165. //Déclaration du menu de réglages
  166. function navigation_menu_setting(&$settingMenu){
  167. global $myUser;
  168. if(!$myUser->can('navigation','configure')) return;
  169. $settingMenu[]= array(
  170. 'sort' =>1,
  171. 'url' => 'setting.php?section=global.navigation',
  172. 'icon' => 'fas fa-angle-right',
  173. 'label' => 'Menu & Navigation'
  174. );
  175. }
  176. //Déclaration des pages de réglages
  177. function navigation_content_setting(){
  178. global $_;
  179. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  180. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  181. }
  182. //Déclaration des settings de base
  183. //Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
  184. Configuration::setting('navigation',array(
  185. "Général",
  186. '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"=>"boolean"),
  187. '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"=>"boolean"),
  188. 'default_menu_visibility' => array(
  189. "label"=>"Visibilité par défaut des nouveaux menus",
  190. "legend"=>"Laisser vide pour ne rien appliquer par défaut aux nouveaux menus",
  191. "type"=>"user",
  192. "placeholder"=>"eg. Administrateur",
  193. "attributes"=>array(
  194. "data-multiple"=>true,
  195. "data-types"=>"user,rank"
  196. )
  197. ),
  198. ));
  199. //Déclation des assets
  200. Plugin::addCss("/css/main.css");
  201. Plugin::addJs("/js/main.js");
  202. Plugin::addJs("/js/component.js");
  203. Plugin::addHook("menu_user", function(&$userMenu){
  204. global $conf, $myUser;
  205. if($conf->get("navigation_allow_custom_menu")==0 && !$myUser->superadmin) return;
  206. $userMenu[]= array(
  207. 'sort' => 0.5,
  208. 'label' => 'Mes raccourcis',
  209. 'icon' => 'far fa-star',
  210. 'url' => 'setting.php?section=global.navigation&menu=mine'
  211. );
  212. });
  213. Plugin::addHook('widget',function(&$models){
  214. $model = new DashboardWidget();
  215. $model->icon = 'far fa-compass';
  216. $model->headerBackground = '#130f40';
  217. $model->description = 'Affiche un menu sélectionné';
  218. $model->label = 'Menu';
  219. $model->width = 3;
  220. $model->height = 3;
  221. $model->model = 'menu';
  222. $model->css = array(__DIR__.'/css/widget.css?v='.time());
  223. $model->js = array(__DIR__.'/js/widget.js?v='.time());
  224. $model->content = function(&$widget){
  225. global $myUser;
  226. User::check_access('navigation','read');
  227. Plugin::need('dashboard/DashboardWidget');
  228. if(empty($widget->meta['label'])) $widget->meta['label'] = 'Bloc Menu';
  229. if(empty($widget->meta['url'])) $widget->meta['url'] = '';
  230. if(empty($widget->meta['color'])) $widget->meta['color'] = '#007bff';
  231. ob_start();
  232. $icon = empty($widget->meta['icon']) ? 'far fa-bookmark' : $widget->meta['icon'];
  233. $color = empty($widget->meta['color'])? '#ffffff': $widget->meta['color'];
  234. $url = navigation_meta_link($widget->meta['url']);
  235. $label = navigation_meta_link($widget->meta['label']);
  236. ?>
  237. <div style="background: <?php echo $color; ?>" class="widgetNavigationContainer">
  238. <?php if(!empty($widget->meta['label'])): ?>
  239. <a href="<?php echo $url; ?>" <?php if(!empty($widget->meta['redirect'])) echo 'target="_blank"'; ?> >
  240. <i class="<?php echo $icon; ?>"></i>
  241. <?php echo $label; ?>
  242. </a>
  243. <?php else: ?>
  244. <h4 class="noContent"><i class="far fa-compass"></i> Aucun menu spécifié</h4>
  245. <?php endif; ?>
  246. </div>
  247. <?php
  248. $widget->content = ob_get_clean();
  249. $widget->headerBackground = $widget->meta['color'];
  250. $widget->label = '';
  251. $widget->icon = '';
  252. };
  253. $model->configure = function($widget){
  254. if(empty($widget->meta['label'])) $widget->meta['label'] = 'Bloc Menu';
  255. if(empty($widget->meta['url'])) $widget->meta['url'] = '';
  256. if(empty($widget->meta['redirect'])) $widget->meta['redirect'] = '';
  257. ?>
  258. <div id="navigation-widget-form">
  259. <label for="">Icone / Titre / Couleur :</label>
  260. <div class="input-group mb-2">
  261. <input class="form-control" type="text" data-type="icon" value="<?php echo empty($widget->meta['icon'])?'far fa-bookmark':$widget->meta['icon']; ?>" id="widget-icon">
  262. <input class="form-control" type="text" value="<?php echo $widget->meta['label']; ?>" id="widget-label">
  263. <input class="form-control" type="text" data-type="color" value="<?php echo empty($widget->meta['color'])?'#000000':$widget->meta['color']; ?>" id="widget-color">
  264. </div>
  265. <label for="">Adresse URL :</label>
  266. <div class="input-group mb-2">
  267. <div class="input-group-prepend">
  268. <div class="input-group-text">
  269. <label for="widget-redirect" class="pointer mb-0">Redirection</label>
  270. <input data-type="checkbox" name="widget-redirect" id="widget-redirect" <?php echo $widget->meta['redirect'] ? 'checked' : ''; ?>>
  271. </div>
  272. </div>
  273. <input class="form-control text-success" data-type="page" type="text" value="<?php echo $widget->meta['url']; ?>" id="widget-url">
  274. <div class="input-group-append">
  275. <a href="<?php echo $widget->meta['url']; ?>" target="_blank" class="text-success text-decoration-none input-group-text"><i class="fas fa-globe"></i></a>
  276. </div>
  277. </div>
  278. </div>
  279. <?php
  280. };
  281. $model->save = function($widget,$form){
  282. $url = str_replace(ROOT_URL.'/','',$form['widget-url']);
  283. $widget->meta['url'] = $url;
  284. $widget->meta['redirect'] = $form['widget-redirect'];
  285. $widget->meta['icon'] = $form['widget-icon'];
  286. $widget->meta['label'] = $form['widget-label'];
  287. $widget->meta['color'] = $form['widget-color'];
  288. $widget->save();
  289. };
  290. $models[$model->model] = $model;
  291. });
  292. //Mapping hook / fonctions
  293. //Plugin::addHook("widget", "navigation_widget");
  294. Plugin::addHook("install", "navigation_install");
  295. Plugin::addHook("uninstall", "navigation_uninstall");
  296. Plugin::addHook("header", "navigation_menu");
  297. Plugin::addHook("page", "navigation_page");
  298. Plugin::addHook("menu_setting", "navigation_menu_setting");
  299. Plugin::addHook("content_setting", "navigation_content_setting");
  300. Plugin::addHook("content_setting", "navigation_content_setting");
  301. Plugin::addHook("menu_footer", "navigation_menu_footer");
  302. ?>