navigation.plugin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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::addHook("menu_user", function(&$userMenu){
  203. global $conf, $myUser;
  204. if($conf->get("navigation_allow_custom_menu")==0 && !$myUser->superadmin) return;
  205. $userMenu[]= array(
  206. 'sort' => 0.5,
  207. 'label' => 'Mes raccourcis',
  208. 'icon' => 'far fa-star',
  209. 'url' => 'setting.php?section=global.navigation&menu=mine'
  210. );
  211. });
  212. // function navigation_widget(&$widgets){
  213. // global $myUser;
  214. // require_once(PLUGIN_PATH.'dashboard'.SLASH.'DashboardWidget.class.php');
  215. // $modelWidget = new DashboardWidget();
  216. // $modelWidget->model = 'menu';
  217. // $modelWidget->title = 'Menu';
  218. // $modelWidget->icon = 'far fa-compass';
  219. // $modelWidget->background = '#130f40';
  220. // $modelWidget->load = 'action.php?action=navigation_widget_load';
  221. // $modelWidget->js = [Plugin::url().'/js/widget.js?v=0'];
  222. // $modelWidget->css = [Plugin::url().'/css/widget.css?v=0'];
  223. // $modelWidget->configure = 'action.php?action=navigation_widget_configure';
  224. // $modelWidget->configure_callback = 'navigation_widget_configure_save';
  225. // $modelWidget->configure_init = 'navigation_widget_configure_init';
  226. // $modelWidget->description = "Affiche un menu sélectionné";
  227. // $widgets[] = $modelWidget;
  228. // }
  229. Plugin::addHook('widget',function(&$models){
  230. $model = new DashboardWidget();
  231. $model->icon = 'far fa-compass';
  232. $model->headerBackground = '#130f40';
  233. $model->description = 'Affiche un menu sélectionné';
  234. $model->label = 'Menu';
  235. $model->width = 3;
  236. $model->height = 3;
  237. $model->model = 'menu';
  238. $model->css = array(__DIR__.'/css/widget.css?v='.time());
  239. $model->js = array(__DIR__.'/js/widget.js?v='.time());
  240. $model->content = function(&$widget){
  241. global $myUser;
  242. User::check_access('navigation','read');
  243. Plugin::need('dashboard/DashboardWidget');
  244. if(empty($widget->meta['label'])){
  245. $widget->meta['label'] = 'Bloc Menu';
  246. }else{
  247. $widget->meta['label'] = '';
  248. $widget->meta['icon'] = '';
  249. }
  250. if(empty($widget->meta['url'])) $widget->meta['url'] = '';
  251. ob_start();
  252. $icon = empty($widget->meta['icon']) ? 'far fa-bookmark' : $widget->meta['icon'];
  253. $color = empty($widget->meta['color'])? '#ffffff': $widget->meta['color'];
  254. $url = navigation_meta_link($widget->meta['url']);
  255. $label = navigation_meta_link($widget->meta['label']);
  256. ?>
  257. <div style="background: <?php echo $color; ?>" class="widgetNavigationContainer">
  258. <?php if(!empty($widget->meta['label'])): ?>
  259. <a href="<?php echo $url; ?>" <?php if(!empty($widget->meta['redirect'])) echo 'target="_blank"'; ?> >
  260. <i class="<?php echo $icon; ?>"></i>
  261. <?php echo $label; ?>
  262. </a>
  263. <?php else: ?>
  264. <h4 class="noContent"><i class="far fa-compass"></i> Aucun menu spécifié</h4>
  265. <?php endif; ?>
  266. </div>
  267. <?php
  268. $widget->content = ob_get_clean();
  269. };
  270. $model->configure = function($widget){
  271. if(empty($widget->meta['label'])) $widget->meta['label'] = 'Bloc Menu';
  272. if(empty($widget->meta['url'])) $widget->meta['url'] = '';
  273. ?>
  274. <div id="navigation-widget-form">
  275. <label for="">Icone / Titre / Couleur :</label>
  276. <div class="input-group mb-2">
  277. <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">
  278. <input class="form-control" type="text" value="<?php echo $widget->meta['label']; ?>" id="widget-label">
  279. <input class="form-control" type="text" data-type="color" value="<?php echo empty($widget->meta['color'])?'#000000':$widget->meta['color']; ?>" id="widget-color">
  280. </div>
  281. <label for="">Adresse URL :</label>
  282. <div class="input-group mb-2">
  283. <div class="input-group-prepend">
  284. <div class="input-group-text">
  285. <label for="widget-redirect" class="pointer mb-0">Redirection</label>
  286. <input data-type="checkbox" name="widget-redirect" id="widget-redirect" <?php echo $widget->meta['redirect'] ? 'checked' : ''; ?>>
  287. </div>
  288. </div>
  289. <input class="form-control text-success" type="text" value="<?php echo $widget->meta['url']; ?>" id="widget-url">
  290. <div class="input-group-append">
  291. <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>
  292. </div>
  293. </div>
  294. </div>
  295. <?php
  296. };
  297. $model->save = function($widget,$form){
  298. $url = str_replace(ROOT_URL.'/','',$form['widget-url']);
  299. $widget->meta['url'] = $url;
  300. $widget->meta['redirect'] = $form['widget-redirect'];
  301. $widget->meta['icon'] = $form['widget-icon'];
  302. $widget->meta['label'] = $form['widget-label'];
  303. $widget->meta['color'] = $form['widget-color'];
  304. $widget->save();
  305. };
  306. $models[$model->model] = $model;
  307. });
  308. //Mapping hook / fonctions
  309. //Plugin::addHook("widget", "navigation_widget");
  310. Plugin::addHook("install", "navigation_install");
  311. Plugin::addHook("uninstall", "navigation_uninstall");
  312. Plugin::addHook("header", "navigation_menu");
  313. Plugin::addHook("page", "navigation_page");
  314. Plugin::addHook("menu_setting", "navigation_menu_setting");
  315. Plugin::addHook("content_setting", "navigation_content_setting");
  316. Plugin::addHook("content_setting", "navigation_content_setting");
  317. Plugin::addHook("menu_footer", "navigation_menu_footer");
  318. ?>