dolibarr  20.0.0-beta
eldy_menu.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2007-2009 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
29 class MenuManager
30 {
34  public $db;
35 
36  public $type_user; // Put 0 for internal users, 1 for external users
37  public $atarget = ""; // To store default target to use onto links
38  public $name = "eldy";
39 
43  public $menu;
44 
45  public $menu_array;
46  public $menu_array_after;
47 
48  public $tabMenu;
49 
50 
57  public function __construct($db, $type_user)
58  {
59  $this->type_user = $type_user;
60  $this->db = $db;
61  }
62 
63 
71  public function loadMenu($forcemainmenu = '', $forceleftmenu = '')
72  {
73  global $conf, $user, $langs;
74 
75  // We save into session the main menu selected
76  if (GETPOSTISSET("mainmenu")) {
77  $_SESSION["mainmenu"] = GETPOST("mainmenu", 'aZ09');
78  }
79  if (GETPOSTISSET("idmenu")) {
80  $_SESSION["idmenu"] = GETPOSTINT("idmenu");
81  }
82 
83  // Read now mainmenu and leftmenu that define which menu to show
84  if (GETPOSTISSET("mainmenu")) {
85  // On sauve en session le menu principal choisi
86  $mainmenu = GETPOST("mainmenu", 'aZ09');
87  $_SESSION["mainmenu"] = $mainmenu;
88  $_SESSION["leftmenuopened"] = "";
89  } else {
90  // On va le chercher en session si non defini par le lien
91  $mainmenu = isset($_SESSION["mainmenu"]) ? $_SESSION["mainmenu"] : '';
92  }
93  if (!empty($forcemainmenu)) {
94  $mainmenu = $forcemainmenu;
95  }
96 
97  if (GETPOSTISSET("leftmenu")) {
98  // On sauve en session le menu principal choisi
99  $leftmenu = GETPOST("leftmenu", 'aZ09');
100  $_SESSION["leftmenu"] = $leftmenu;
101 
102  if ($_SESSION["leftmenuopened"] == $leftmenu) { // To collapse
103  //$leftmenu="";
104  $_SESSION["leftmenuopened"] = "";
105  } else {
106  $_SESSION["leftmenuopened"] = $leftmenu;
107  }
108  } else {
109  // On va le chercher en session si non defini par le lien
110  $leftmenu = isset($_SESSION["leftmenu"]) ? $_SESSION["leftmenu"] : '';
111  }
112  if (!empty($forceleftmenu)) {
113  $leftmenu = $forceleftmenu;
114  }
115 
116  require_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
117  $tabMenu = array();
118  $menuArbo = new Menubase($this->db, 'eldy');
119  $menuArbo->menuLoad($mainmenu, $leftmenu, $this->type_user, 'eldy', $tabMenu);
120  $this->tabMenu = $tabMenu;
121  //var_dump($tabMenu);
122 
123  //if ($forcemainmenu == 'all') { var_dump($this->tabMenu); exit; }
124  }
125 
126 
135  public function showmenu($mode, $moredata = null)
136  {
137  global $conf, $langs, $user;
138 
139  //var_dump($this->tabMenu);
140 
141  require_once DOL_DOCUMENT_ROOT.'/core/menus/standard/eldy.lib.php';
142 
143  if ($this->type_user == 1) {
144  $conf->global->MAIN_SEARCHFORM_SOCIETE_DISABLED = 1;
145  $conf->global->MAIN_SEARCHFORM_CONTACT_DISABLED = 1;
146  }
147 
148  require_once DOL_DOCUMENT_ROOT.'/core/class/menu.class.php';
149  $this->menu = new Menu();
150 
151  if (!getDolGlobalString('MAIN_MENU_INVERT')) {
152  if ($mode == 'top') {
153  print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
154  }
155  if ($mode == 'left') {
156  print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata, $this->type_user);
157  }
158  } else {
159  $conf->global->MAIN_SHOW_LOGO = 0;
160  if ($mode == 'top') {
161  print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata, $this->type_user);
162  }
163  if ($mode == 'left') {
164  print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
165  }
166  }
167  if ($mode == 'topnb') {
168  print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode); // no output
169  return $this->menu->getNbOfVisibleMenuEntries();
170  }
171 
172  if ($mode == 'jmobile') { // Used to get menu in xml ul/li
173  print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode); // Fill this->menu that is empty with top menu
174 
175  // $this->menu->liste is top menu
176  //var_dump($this->menu->liste);exit;
177  $lastlevel = array();
178  print '<!-- Generate menu list from menu handler '.$this->name.' -->'."\n";
179  foreach ($this->menu->liste as $key => $val) { // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
180  print '<ul class="ulmenu" data-inset="true">';
181  print '<li class="lilevel0">';
182 
183  if ($val['enabled'] == 1) {
184  $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
185  $substitarray['__USERID__'] = $user->id; // For backward compatibility
186  $val['url'] = make_substitutions($val['url'], $substitarray);
187 
188  if (!preg_match('/^http/', $val['url'])) {
189  $relurl = dol_buildpath($val['url'], 1);
190  } else {
191  $relurl = $val['url'];
192  }
193 
194  $canonurl = preg_replace('/\?.*$/', '', $val['url']);
195 
196  print '<a class="alilevel0" href="#">';
197 
198  // Add font-awesome
199  if ($val['level'] == 0 && !empty($val['prefix'])) {
200  if (preg_match('/^fa\-[a-zA-Z0-9\-_]+$/', $val['prefix'])) {
201  print '<span class="fas '.$val['prefix'].' paddingright pictofixedwidth"></span>';
202  } else {
203  print str_replace('<span class="', '<span class="paddingright pictofixedwidth ', $val['prefix']);
204  }
205  }
206  print $val['titre'];
207  print '</a>'."\n";
208 
209  // Search submenu for this mainmenu entry
210  $tmpmainmenu = $val['mainmenu'];
211  $tmpleftmenu = 'all';
212  $submenu = new Menu();
213  print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $submenu, 1, $tmpmainmenu, $tmpleftmenu, null, $this->type_user); // Fill $submenu (example with tmpmainmenu='home' tmpleftmenu='all', return left menu tree of Home)
214  // Note: $submenu contains menu entry with substitution not yet done
215  //if ($tmpmainmenu.'-'.$tmpleftmenu == 'home-all') {
216  //var_dump($submenu); exit;
217  //}
218  //if ($tmpmainmenu=='accountancy') {
219  //var_dump($submenu->liste); exit;
220  //}
221  $nexturl = dol_buildpath(empty($submenu->liste[0]['url']) ? '' : $submenu->liste[0]['url'], 1);
222 
223  $canonrelurl = preg_replace('/\?.*$/', '', $relurl);
224  $canonnexturl = preg_replace('/\?.*$/', '', $nexturl);
225  //var_dump($canonrelurl);
226  //var_dump($canonnexturl);
227 
228  print '<ul>'."\n";
229  if (($canonrelurl != $canonnexturl && !in_array($val['mainmenu'], array('tools')))
230  || (strpos($canonrelurl, '/product/index.php') !== false || strpos($canonrelurl, '/compta/bank/list.php') !== false)) {
231  // We add sub entry
232  print str_pad('', 1).'<li class="lilevel1 ui-btn-icon-right ui-btn">'; // ui-btn to highlight on clic
233  print '<a href="'.$relurl.'">';
234 
235  if ($val['level'] == 0) {
236  print '<span class="fas fa-home fa-fw paddingright pictofixedwidth" aria-hidden="true"></span>';
237  }
238 
239  if ($langs->trans(ucfirst($val['mainmenu'])."Dashboard") == ucfirst($val['mainmenu'])."Dashboard") { // No translation
240  if (in_array($val['mainmenu'], array('cashdesk', 'externalsite', 'website', 'collab', 'takepos'))) {
241  print $langs->trans("Access");
242  } else {
243  print $langs->trans("Dashboard");
244  }
245  } else {
246  print $langs->trans(ucfirst($val['mainmenu'])."Dashboard");
247  }
248  print '</a>';
249  print '</li>'."\n";
250  }
251 
252  /*
253  if ($val['level'] == 0) {
254  if ($val['enabled']) {
255  $lastlevel[0] = 'enabled';
256  } elseif ($showmenu) {
257  // Not enabled but visible (so greyed)
258  $lastlevel[0] = 'greyed';
259  } else {
260  $lastlevel[0] = 'hidden';
261  }
262  }
263  */
264 
265  $lastlevel2 = array();
266  foreach ($submenu->liste as $key2 => $val2) { // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
267  $showmenu = true;
268  if (getDolGlobalString('MAIN_MENU_HIDE_UNAUTHORIZED') && empty($val2['enabled'])) {
269  $showmenu = false;
270  }
271 
272  // If at least one parent is not enabled, we do not show any menu of all children
273  if ($val2['level'] > 0) {
274  $levelcursor = $val2['level'] - 1;
275  while ($levelcursor >= 0) {
276  if ($lastlevel2[$levelcursor] != 'enabled') {
277  $showmenu = false;
278  }
279  $levelcursor--;
280  }
281  }
282 
283  if ($showmenu) { // Visible (option to hide when not allowed is off or allowed)
284  $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
285  $substitarray['__USERID__'] = $user->id; // For backward compatibility
286  $val2['url'] = make_substitutions($val2['url'], $substitarray); // Make also substitution of __(XXX)__ and __[XXX]__
287 
288  if (!preg_match("/^(http:\/\/|https:\/\/)/i", $val2['url'])) {
289  $relurl2 = dol_buildpath($val2['url'], 1);
290  } else {
291  $relurl2 = $val2['url'];
292  }
293  $canonurl2 = preg_replace('/\?.*$/', '', $val2['url']);
294  //var_dump($val2['url'].' - '.$canonurl2.' - '.$val2['level']);
295  if (in_array($canonurl2, array('/admin/index.php', '/admin/tools/index.php', '/core/tools.php'))) {
296  $relurl2 = '';
297  }
298 
299  $disabled = '';
300  if (!$val2['enabled']) {
301  $disabled = " vsmenudisabled";
302  }
303 
304  // @phan-suppress-next-line PhanParamSuspiciousOrder
305  print str_pad('', $val2['level'] + 1);
306  print '<li class="lilevel'.($val2['level'] + 1);
307  if ($val2['level'] == 0) {
308  print ' ui-btn-icon-right ui-btn'; // ui-btn to highlight on clic
309  }
310  print $disabled.'">'; // ui-btn to highlight on clic
311  if ($relurl2) {
312  if ($val2['enabled']) { // Allowed
313  print '<a href="'.$relurl2.'"';
314  //print ' data-ajax="false"';
315  print '>';
316  $lastlevel2[$val2['level']] = 'enabled';
317  } else { // Not allowed but visible (greyed)
318  print '<a href="#" class="vsmenudisabled">';
319  $lastlevel2[$val2['level']] = 'greyed';
320  }
321  } else {
322  if ($val2['enabled']) { // Allowed
323  $lastlevel2[$val2['level']] = 'enabled';
324  } else {
325  $lastlevel2[$val2['level']] = 'greyed';
326  }
327  }
328 
329  // Add font-awesome (if $val2['level'] == 0, we are on level2
330  if ($val2['level'] == 0 && !empty($val2['prefix'])) {
331  print $val2['prefix']; // the picto must have class="pictofixedwidth paddingright"
332  }
333 
334  print $val2['titre'];
335  if ($relurl2) {
336  print '</a>';
337  }
338  print '</li>'."\n";
339  }
340  }
341  //var_dump($submenu);
342  print '</ul>';
343  }
344  if ($val['enabled'] == 2) {
345  print '<span class="spanlilevel0 vsmenudisabled">';
346 
347  // Add font-awesome
348  if ($val['level'] == 0 && !empty($val['prefix'])) {
349  print $val['prefix'];
350  }
351 
352  print $val['titre'];
353  print '</span>';
354  }
355  print '</li>';
356  print '</ul>'."\n";
357  }
358  }
359 
360  unset($this->menu);
361 
362  //print 'xx'.$mode;
363  return 0;
364  }
365 }
Class to manage left menus.
Definition: menu.class.php:31
Class to manage menu Auguria.
__construct($db, $type_user)
Constructor.
Definition: eldy_menu.php:57
loadMenu($forcemainmenu='', $forceleftmenu='')
Load this->tabMenu.
Definition: eldy_menu.php:71
showmenu($mode, $moredata=null)
Show menu.
Definition: eldy_menu.php:135
Class to manage menu entries.
print_eldy_menu($db, $atarget, $type_user, &$tabMenu, &$menu, $noout=0, $mode='')
Core function to output top menu eldy.
Definition: eldy.lib.php:46
print_left_eldy_menu($db, $menu_array_before, $menu_array_after, &$tabMenu, &$menu, $noout=0, $forcemainmenu='', $forceleftmenu='', $moredata=null, $type_user=0)
Core function to output left menu eldy Fill &$menu (example with $forcemainmenu='home' $forceleftmenu...
Definition: eldy.lib.php:736
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.