1<?php
2/* Copyright (C) 2006-2013 Laurent Destailleur  <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18/**
19 *	    \file       htdocs/core/menus/standard/empty.php
20 *		\brief      This is an example of an empty top menu handler
21 */
22
23/**
24 *	    Class to manage empty menu
25 */
26class MenuManager
27{
28	/**
29	 * @var DoliDB Database handler.
30	 */
31	public $db;
32
33	public $type_user = 0; // Put 0 for internal users, 1 for external users
34	public $atarget = ""; // To store default target to use onto links
35	public $name = "empty";
36
37	public $menu;
38	public $menu_array_after;
39
40	public $tabMenu;
41
42
43	/**
44	 *  Constructor
45	 *
46	 *  @param	DoliDB		$db     		Database handler
47	 *  @param	int			$type_user		Type of user
48	 */
49	public function __construct($db, $type_user)
50	{
51		$this->type_user = $type_user;
52		$this->db = $db;
53	}
54
55
56	/**
57	 * Load this->tabMenu
58	 *
59	 * @param	string	$forcemainmenu		To force mainmenu to load
60	 * @param	string	$forceleftmenu		To force leftmenu to load
61	 * @return	void
62	 */
63	public function loadMenu($forcemainmenu = '', $forceleftmenu = '')
64	{
65		// Do nothing
66		$this->tabMenu = array();
67	}
68
69
70	/**
71	 *  Show menu
72	 *
73	 *	@param	string	$mode			'top', 'left', 'jmobile'
74	 *  @param	array	$moredata		An array with more data to output
75	 *  @return int                     0 or nb of top menu entries if $mode = 'topnb'
76	 */
77	public function showmenu($mode, $moredata = null)
78	{
79		global $user, $conf, $langs, $dolibarr_main_db_name;
80
81		$id = 'mainmenu';
82
83		require_once DOL_DOCUMENT_ROOT.'/core/class/menu.class.php';
84		$this->menu = new Menu();
85
86		$res = 'ErrorBadParameterForMode';
87
88		$noout = 0;
89		//if ($mode == 'jmobile') $noout=1;
90
91		if ($mode == 'topnb') {
92			return 1;
93		}
94
95		if ($mode == 'top') {
96			if (empty($noout)) {
97				print_start_menu_array_empty();
98			}
99
100			$usemenuhider = 1;
101
102			// Show/Hide vertical menu
103			if ($mode != 'jmobile' && $mode != 'topnb' && $usemenuhider && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
104				$showmode = 1;
105				$classname = 'class="tmenu menuhider"';
106				$idsel = 'menu';
107
108				$this->menu->add('#', '', 0, $showmode, $atarget, "xxx", '', 0, $id, $idsel, $classname);
109			}
110
111			// Home
112			$showmode = 1;
113			$classname = 'class="tmenusel"';
114			$idsel = 'home';
115
116			$this->menu->add('/index.php', $langs->trans("Home"), 0, $showmode, $this->atarget, 'home', '', 10, $id, $idsel, $classname);
117
118
119			// Sort on position
120			$this->menu->liste = dol_sort_array($this->menu->liste, 'position');
121
122			// Output menu entries
123			foreach ($this->menu->liste as $menkey => $menuval) {
124				if (empty($noout)) {
125					print_start_menu_entry_empty($menuval['idsel'], $menuval['classname'], $menuval['enabled']);
126				}
127				if (empty($noout)) {
128					print_text_menu_entry_empty($menuval['titre'], $menuval['enabled'], ($menuval['url'] != '#' ?DOL_URL_ROOT:'').$menuval['url'], $menuval['id'], $menuval['idsel'], $menuval['classname'], ($menuval['target'] ? $menuval['target'] : $atarget));
129				}
130				if (empty($noout)) {
131					print_end_menu_entry_empty($menuval['enabled']);
132				}
133			}
134
135			$showmode = 1;
136			if (empty($noout)) {
137				print_start_menu_entry_empty('', 'class="tmenuend"', $showmode);
138			}
139			if (empty($noout)) {
140				print_end_menu_entry_empty($showmode);
141			}
142
143			if (empty($noout)) {
144				print_end_menu_array_empty();
145			}
146
147			if ($mode == 'jmobile') {
148				$this->topmenu = clone $this->menu;
149				unset($this->menu->liste);
150			}
151		}
152
153		if ($mode == 'jmobile') {     // Used to get menu in xml ul/li
154			// Home
155			$showmode = 1;
156			$classname = 'class="tmenusel"';
157			$idsel = 'home';
158
159			$this->menu->add('/index.php', $langs->trans("Home"), 0, $showmode, $this->atarget, 'home', '', 10, $id, $idsel, $classname);
160
161			$substitarray = getCommonSubstitutionArray($langs, 0, null, null);
162
163			// $this->menu->liste is top menu
164			//var_dump($this->menu->liste);exit;
165			$lastlevel = array();
166			print '<!-- Generate menu list from menu handler '.$this->name.' -->'."\n";
167			foreach ($this->menu->liste as $key => $val) {		// $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
168				print '<ul class="ulmenu" data-inset="true">';
169				print '<li class="lilevel0">';
170
171				$val['url'] = make_substitutions($val['url'], $substitarray);
172
173				if ($val['enabled'] == 1) {
174					$relurl = dol_buildpath($val['url'], 1);
175					$canonurl = preg_replace('/\?.*$/', '', $val['url']);
176
177					print '<a class="alilevel0" href="#">';
178
179					// Add font-awesome
180					if ($val['level'] == 0 && $val['mainmenu'] == 'home') {
181						print '<span class="fa fa-home fa-fw paddingright" aria-hidden="true"></span>';
182					}
183
184					print $val['titre'];
185					print '</a>'."\n";
186
187					// Search submenu fot this mainmenu entry
188					$tmpmainmenu = $val['mainmenu'];
189					$tmpleftmenu = 'all';
190					$submenu = new Menu();
191
192					$langs->load("admin"); // Load translation file admin.lang
193					$submenu->add("/admin/index.php?leftmenu=setup", $langs->trans("Setup"), 0);
194					$submenu->add("/admin/company.php", $langs->trans("MenuCompanySetup"), 1);
195					$submenu->add("/admin/modules.php", $langs->trans("Modules"), 1);
196					$submenu->add("/admin/menus.php", $langs->trans("Menus"), 1);
197					$submenu->add("/admin/ihm.php", $langs->trans("GUISetup"), 1);
198					$submenu->add("/admin/translation.php?mainmenu=home", $langs->trans("Translation"), 1);
199					$submenu->add("/admin/defaultvalues.php?mainmenu=home", $langs->trans("DefaultValues"), 1);
200
201					$submenu->add("/admin/boxes.php?mainmenu=home", $langs->trans("Boxes"), 1);
202					$submenu->add("/admin/delais.php?mainmenu=home", $langs->trans("Alerts"), 1);
203					$submenu->add("/admin/proxy.php?mainmenu=home", $langs->trans("Security"), 1);
204					$submenu->add("/admin/limits.php?mainmenu=home", $langs->trans("MenuLimits"), 1);
205					$submenu->add("/admin/pdf.php?mainmenu=home", $langs->trans("PDF"), 1);
206					$submenu->add("/admin/mails.php?mainmenu=home", $langs->trans("Emails"), 1);
207					$submenu->add("/admin/sms.php?mainmenu=home", $langs->trans("SMS"), 1);
208					$submenu->add("/admin/dict.php?mainmenu=home", $langs->trans("DictionarySetup"), 1);
209					$submenu->add("/admin/const.php?mainmenu=home", $langs->trans("OtherSetup"), 1);
210
211					//if ($tmpmainmenu.'-'.$tmpleftmenu == 'home-all') { var_dump($submenu); exit; }
212					//if ($tmpmainmenu=='accountancy') { var_dump($submenu->liste); exit; }
213					$nexturl = dol_buildpath($submenu->liste[0]['url'], 1);
214
215					$canonrelurl = preg_replace('/\?.*$/', '', $relurl);
216					$canonnexturl = preg_replace('/\?.*$/', '', $nexturl);
217					//var_dump($canonrelurl);
218					//var_dump($canonnexturl);
219					print '<ul>'."\n";
220					if (($canonrelurl != $canonnexturl && !in_array($val['mainmenu'], array('tools')))
221						|| (strpos($canonrelurl, '/product/index.php') !== false || strpos($canonrelurl, '/compta/bank/list.php') !== false)) {
222						// We add sub entry
223						print str_pad('', 1).'<li class="lilevel1 ui-btn-icon-right ui-btn">'; // ui-btn to highlight on clic
224						print '<a href="'.$relurl.'">';
225						if ($langs->trans(ucfirst($val['mainmenu'])."Dashboard") == ucfirst($val['mainmenu'])."Dashboard") {  // No translation
226							if (in_array($val['mainmenu'], array('cashdesk', 'websites'))) {
227								print $langs->trans("Access");
228							} else {
229								print $langs->trans("Dashboard");
230							}
231						} else {
232							print $langs->trans(ucfirst($val['mainmenu'])."Dashboard");
233						}
234						print '</a>';
235						print '</li>'."\n";
236					}
237
238					if ($val['level'] == 0) {
239						if ($val['enabled']) {
240							$lastlevel[0] = 'enabled';
241						} elseif ($showmenu) {                 // Not enabled but visible (so greyed)
242							$lastlevel[0] = 'greyed';
243						} else {
244							$lastlevel[0] = 'hidden';
245						}
246					}
247
248					$lastlevel2 = array();
249					foreach ($submenu->liste as $key2 => $val2) {		// $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
250						$showmenu = true;
251						if (!empty($conf->global->MAIN_MENU_HIDE_UNAUTHORIZED) && empty($val2['enabled'])) {
252							$showmenu = false;
253						}
254
255						// If at least one parent is not enabled, we do not show any menu of all children
256						if ($val2['level'] > 0) {
257							$levelcursor = $val2['level'] - 1;
258							while ($levelcursor >= 0) {
259								if ($lastlevel2[$levelcursor] != 'enabled') {
260									$showmenu = false;
261								}
262								$levelcursor--;
263							}
264						}
265
266						if ($showmenu) {		// Visible (option to hide when not allowed is off or allowed)
267							$val2['url'] = make_substitutions($val2['url'], $substitarray);
268
269							$relurl2 = dol_buildpath($val2['url'], 1);
270							$canonurl2 = preg_replace('/\?.*$/', '', $val2['url']);
271							//var_dump($val2['url'].' - '.$canonurl2.' - '.$val2['level']);
272							if (in_array($canonurl2, array('/admin/index.php', '/admin/tools/index.php', '/core/tools.php'))) {
273								$relurl2 = '';
274							}
275
276							$disabled = '';
277							if (!$val2['enabled']) {
278								$disabled = " vsmenudisabled";
279							}
280
281							print str_pad('', $val2['level'] + 1);
282							print '<li class="lilevel'.($val2['level'] + 1);
283							if ($val2['level'] == 0) {
284								print ' ui-btn-icon-right ui-btn'; // ui-btn to highlight on clic
285							}
286							print $disabled.'">'; // ui-btn to highlight on clic
287							if ($relurl2) {
288								if ($val2['enabled']) {	// Allowed
289									print '<a href="'.$relurl2.'"';
290									//print ' data-ajax="false"';
291									print '>';
292									$lastlevel2[$val2['level']] = 'enabled';
293								} else // Not allowed but visible (greyed)
294								{
295									print '<a href="#" class="vsmenudisabled">';
296									$lastlevel2[$val2['level']] = 'greyed';
297								}
298							} else {
299								if ($val2['enabled']) {	// Allowed
300									$lastlevel2[$val2['level']] = 'enabled';
301								} else {
302									$lastlevel2[$val2['level']] = 'greyed';
303								}
304							}
305							//var_dump($val2['level']);
306							//var_dump($lastlevel2);
307							print $val2['titre'];
308							if ($relurl2) {
309								if ($val2['enabled']) {
310									// Allowed
311									print '</a>';
312								} else {
313									print '</a>';
314								}
315							}
316							print '</li>'."\n";
317						}
318					}
319					//var_dump($submenu);
320					print '</ul>';
321				}
322				if ($val['enabled'] == 2) {
323					print '<font class="vsmenudisabled">'.$val['titre'].'</font>';
324				}
325				print '</li>';
326				print '</ul>'."\n";
327			}
328		}
329
330		if ($mode == 'left') {
331			// Put here left menu entries
332			// ***** START *****
333
334			$langs->load("admin"); // Load translation file admin.lang
335			$this->menu->add("/admin/index.php?leftmenu=setup", $langs->trans("Setup"), 0);
336			$this->menu->add("/admin/company.php", $langs->trans("MenuCompanySetup"), 1);
337			$this->menu->add("/admin/modules.php", $langs->trans("Modules"), 1);
338			$this->menu->add("/admin/menus.php", $langs->trans("Menus"), 1);
339			$this->menu->add("/admin/ihm.php", $langs->trans("GUISetup"), 1);
340			$this->menu->add("/admin/translation.php?mainmenu=home", $langs->trans("Translation"), 1);
341			$this->menu->add("/admin/defaultvalues.php?mainmenu=home", $langs->trans("DefaultValues"), 1);
342
343			$this->menu->add("/admin/boxes.php?mainmenu=home", $langs->trans("Boxes"), 1);
344			$this->menu->add("/admin/delais.php?mainmenu=home", $langs->trans("Alerts"), 1);
345			$this->menu->add("/admin/proxy.php?mainmenu=home", $langs->trans("Security"), 1);
346			$this->menu->add("/admin/limits.php?mainmenu=home", $langs->trans("MenuLimits"), 1);
347			$this->menu->add("/admin/pdf.php?mainmenu=home", $langs->trans("PDF"), 1);
348			$this->menu->add("/admin/mails.php?mainmenu=home", $langs->trans("Emails"), 1);
349			$this->menu->add("/admin/sms.php?mainmenu=home", $langs->trans("SMS"), 1);
350			$this->menu->add("/admin/dict.php?mainmenu=home", $langs->trans("DictionarySetup"), 1);
351			$this->menu->add("/admin/const.php?mainmenu=home", $langs->trans("OtherSetup"), 1);
352
353			// ***** END *****
354
355			$menu_array_before = array();
356			$menu_array_after = array();
357
358			// do not change code after this
359
360			$menu_array = $this->menu->liste;
361			if (is_array($menu_array_before)) {
362				$menu_array = array_merge($menu_array_before, $menu_array);
363			}
364			if (is_array($menu_array_after)) {
365				$menu_array = array_merge($menu_array, $menu_array_after);
366			}
367			//var_dump($menu_array);exit;
368			if (!is_array($menu_array)) {
369				return 0;
370			}
371
372			if (empty($noout)) {
373				$alt = 0; $altok = 0; $blockvmenuopened = false;
374				$num = count($menu_array);
375				for ($i = 0; $i < $num; $i++) {
376					$alt++;
377					if (empty($menu_array[$i]['level'])) {
378						$altok++;
379						$blockvmenuopened = true;
380						$lastopened = true;
381						for ($j = ($i + 1); $j < $num; $j++) {
382							if (empty($menu_array[$j]['level'])) {
383								$lastopened = false;
384							}
385						}
386						$alt = 0; // For menu manager "empty", we force to not have blockvmenufirst defined
387						$lastopened = 1; // For menu manager "empty", we force to not have blockvmenulast defined
388						if (($alt % 2 == 0)) {
389							print '<div class="blockvmenub lockvmenuimpair blockvmenuunique'.($lastopened ? ' blockvmenulast' : '').($alt == 1 ? ' blockvmenufirst' : '').'">'."\n";
390						} else {
391							print '<div class="blockvmenu blockvmenupair blockvmenuunique'.($lastopened ? ' blockvmenulast' : '').($alt == 1 ? ' blockvmenufirst' : '').'">'."\n";
392						}
393					}
394
395					// Add tabulation
396					$tabstring = '';
397					$tabul = ($menu_array[$i]['level'] - 1);
398					if ($tabul > 0) {
399						for ($j = 0; $j < $tabul; $j++) {
400							$tabstring .= '&nbsp; &nbsp;';
401						}
402					}
403
404					if ($menu_array[$i]['level'] == 0) {
405						if ($menu_array[$i]['enabled']) {
406							print '<div class="menu_titre">'.$tabstring.'<a class="vmenu" href="'.dol_buildpath($menu_array[$i]['url'], 1).'"'.($menu_array[$i]['target'] ? ' target="'.$menu_array[$i]['target'].'"' : '').'>'.$menu_array[$i]['titre'].'</a></div>'."\n";
407						} else {
408							print '<div class="menu_titre">'.$tabstring.'<font class="vmenudisabled">'.$menu_array[$i]['titre'].'</font></div>'."\n";
409						}
410						print '<div class="menu_top"></div>'."\n";
411					}
412
413					if ($menu_array[$i]['level'] > 0) {
414						$cssmenu = '';
415						if ($menu_array[$i]['url']) {
416							$cssmenu = ' menu_contenu'.dol_string_nospecial(preg_replace('/\.php.*$/', '', $menu_array[$i]['url']));
417						}
418
419						print '<div class="menu_contenu'.$cssmenu.'">';
420
421						if ($menu_array[$i]['enabled']) {
422							print $tabstring;
423							if ($menu_array[$i]['url']) {
424								print '<a class="vsmenu"  itle="'.dol_escape_htmltag($menu_array[$i]['titre']).'" href="'.dol_buildpath($menu_array[$i]['url'], 1).'"'.($menu_array[$i]['target'] ? ' target="'.$menu_array[$i]['target'].'"' : '').'>';
425							} else {
426								print '<span class="vsmenu" title="'.dol_escape_htmltag($menu_array[$i]['titre']).'">';
427							}
428							if ($menu_array[$i]['url']) {
429								print $menu_array[$i]['titre'].'</a>';
430							} else {
431								print '</span>';
432							}
433						} else {
434							print $tabstring.'<font class="vsmenudisabled vsmenudisabledmargin">'.$menu_array[$i]['titre'].'</font>';
435						}
436
437						// If title is not pure text and contains a table, no carriage return added
438						if (!strstr($menu_array[$i]['titre'], '<table')) {
439							print '<br>';
440						}
441						print '</div>'."\n";
442					}
443
444					// If next is a new block or end
445					if (empty($menu_array[$i + 1]['level'])) {
446						print '<div class="menu_end"></div>'."\n";
447						print "</div>\n";
448					}
449				}
450
451				if ($altok) {
452					print '<div class="blockvmenuend"></div>';
453				}
454			}
455
456			if ($mode == 'jmobile') {
457				$this->leftmenu = clone $this->menu;
458				unset($menu_array);
459			}
460		}
461		unset($this->menu);
462
463		return $res;
464	}
465}
466
467
468/**
469 * Output menu entry
470 *
471 * @return	void
472 */
473function print_start_menu_array_empty()
474{
475	global $conf;
476
477	print '<div class="tmenudiv">';
478	print '<ul role="navigation" class="tmenu"'.(empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' title="Top menu"').'>';
479}
480
481/**
482 * Output start menu entry
483 *
484 * @param	string	$idsel		Text
485 * @param	string	$classname	String to add a css class
486 * @param	int		$showmode	0 = hide, 1 = allowed or 2 = not allowed
487 * @return	void
488 */
489function print_start_menu_entry_empty($idsel, $classname, $showmode)
490{
491	if ($showmode) {
492		print '<li '.$classname.' id="mainmenutd_'.$idsel.'">';
493		//print '<div class="tmenuleft tmenusep"></div>';
494		print '<div class="tmenucenter">';
495	}
496}
497
498/**
499 * Output menu entry
500 *
501 * @param	string	$text		Text
502 * @param	int		$showmode	1 or 2
503 * @param	string	$url		Url
504 * @param	string	$id			Id
505 * @param	string	$idsel		Id sel
506 * @param	string	$classname	Class name
507 * @param	string	$atarget	Target
508 * @return	void
509 */
510function print_text_menu_entry_empty($text, $showmode, $url, $id, $idsel, $classname, $atarget)
511{
512	global $conf, $langs;
513
514	if ($showmode == 1) {
515		print '<a class="tmenuimage" tabindex="-1" href="'.$url.'"'.($atarget ? ' target="'.$atarget.'"' : '').'>';
516		print '<div class="'.$id.' '.$idsel.'"><span class="'.$id.' tmenuimage" id="mainmenuspan_'.$idsel.'"></span></div>';
517		print '</a>';
518		print '<a '.$classname.' id="mainmenua_'.$idsel.'" href="'.$url.'"'.($atarget ? ' target="'.$atarget.'"' : '').'>';
519		print '<span class="mainmenuaspan">';
520		print $text;
521		print '</span>';
522		print '</a>';
523	}
524	if ($showmode == 2) {
525		print '<div class="'.$id.' '.$idsel.' tmenudisabled"><span class="'.$id.'" id="mainmenuspan_'.$idsel.'"></span></div>';
526		print '<a class="tmenudisabled" id="mainmenua_'.$idsel.'" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'">';
527	}
528}
529
530/**
531 * Output end menu entry
532 *
533 * @param	int		$showmode	0 = hide, 1 = allowed or 2 = not allowed
534 * @return	void
535 */
536function print_end_menu_entry_empty($showmode)
537{
538	if ($showmode) {
539		print '</div></li>';
540		print "\n";
541	}
542}
543
544/**
545 * Output menu array
546 *
547 * @return	void
548 */
549function print_end_menu_array_empty()
550{
551	print '</ul>';
552	print '</div>';
553	print "\n";
554}
555