dolibarr 20.0.0
webportal.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
3 */
4
17function getNav($Tmenu)
18{
19 $menu = '';
20
21 foreach ($Tmenu as $item) {
22 $menu .= getNavItem($item);
23 }
24
25 return $menu;
26}
27
36function getNavItem($item, $deep = 0)
37{
38 $context = Context::getInstance();
39
40 $menu = '';
41
42 $itemDefault = array(
43 'active' => false,
44 'separator' => false,
45 );
46
47 $item = array_replace($itemDefault, $item); // applique les valeurs par default
48
49 if ($context->menuIsActive($item['id'])) {
50 $item['active'] = true;
51 }
52
53 if (!isset($item['class'])) {
54 $item['class'] = '--item-' . preg_replace('/[^a-z0-9 ]/i', '-', $item['id']);
55 }
56
57
58 if (!empty($item['override'])) {
59 $menu .= $item['override'];
60 } elseif (!empty($item['children'])) {
61 $menuChildren = '';
62 $haveChildActive = false;
63
64 foreach ($item['children'] as $child) {
65 $item = array_replace($itemDefault, $item); // applique les valeurs par default
66 '@phan-var-force array{id:string,rank:int,url:string,name:string,group:string,override?:int<0,1>,active?:bool,separator?:bool} $child';
67
68 if (!empty($child['separator'])) {
69 $menuChildren .= '<li role="separator" class="divider"></li>';
70 }
71
72 if ($context->menuIsActive($child['id'])) {
73 $child['active'] = true;
74 $haveChildActive = true;
75 }
76
77 if (!empty($child['children'])) {
78 $menuChildren .= "\n\r" . '<!-- print sub menu -->' . "\n\r";
79 $menuChildren .= getNavItem($child, $deep + 1);
80 $menuChildren .= "\n\r" . '<!-- print sub menu -->' . "\n\r";
81 } else {
82 $menuChildren .= '<li class="dropdown-item '.$item['class'].'" data-deep="' . $deep . '" ><a href="' . $child['url'] . '" class="' . (!empty($child['active']) ? 'active' : '') . '" ">' . $child['name'] . '</a></li>';
83 }
84 }
85
86 $active = '';
87 if ($haveChildActive || $item['active']) {
88 $active = 'active';
89 }
90
91 $menu .= '<li data-deep="' . $deep . '" class="'.$item['class'].' dropdown ' . ($deep > 0 ? 'dropdown-item dropdown-submenu' : 'nav-item') . ' ' . $active . '">';
92 $menu .= '<a href="#" class="' . ($deep > 0 ? '' : 'nav-link') . ' dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $item['name'] . ' <span class="caret"></span></a>';
93 $menu .= '<ul class="dropdown-menu ">' . $menuChildren . '</ul>';
94 $menu .= '</li>';
95 } else {
96 $menu .= '<li data-deep="' . $deep . '" class="'.$item['class'].' ' . ($deep > 0 ? 'dropdown-item' : 'nav-item ') . ' ' . ($item['active'] ? 'active' : '') . '"><a href="' . $item['url'] . '" class="' . ($deep > 0 ? '' : 'nav-link') . '" >' . $item['name'] . '</a></li>';
97 }
98
99 return $menu;
100}
101
114function menuSortInv($a, $b)
115{
116
117 if (empty($a['rank'])) {
118 $a['rank'] = 0;
119 }
120 if (empty($b['rank'])) {
121 $b['rank'] = 0;
122 }
123 if ($a['rank'] == $b['rank']) {
124 return 0;
125 }
126
127 return ($a['rank'] < $b['rank']) ? -1 : 1;
128}
static getInstance()
Singleton method to create one instance of this object.
getNav($Tmenu)
Get nav menu.
getNavItem($item, $deep=0)
Get nav item.
menuSortInv($a, $b)
Sort menu uasort callback function to Sort menu fields.