1<?php
2/* Copyright (C) 2007      Patrick Raguin       <patrick.raguin@gmail.com>
3 * Copyright (C) 2009      Regis Houssin        <regis.houssin@inodbox.com>
4 * Copyright (C) 2008-2013 Laurent Destailleur  <eldy@users.sourceforge.net>
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
20/**
21 *	\file       htdocs/core/menus/standard/auguria_menu.php
22 *	\brief      Menu auguria manager
23 */
24
25
26/**
27 *	Class to manage menu Auguria
28 */
29class MenuManager
30{
31	/**
32	 * @var DoliDB Database handler.
33	 */
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 = "auguria";
39
40	public $menu_array;
41	public $menu_array_after;
42
43	public $tabMenu;
44
45
46	/**
47	 *  Constructor
48	 *
49	 *  @param	DoliDB		$db     	Database handler
50	 *  @param	int			$type_user	Type of user
51	 */
52	public function __construct($db, $type_user)
53	{
54		$this->type_user = $type_user;
55		$this->db = $db;
56	}
57
58
59   	/**
60   	 * Load this->tabMenu
61   	 *
62   	 * @param	string	$forcemainmenu		To force mainmenu to load
63   	 * @param	string	$forceleftmenu		To force leftmenu to load
64   	 * @return	void
65   	 */
66   	public function loadMenu($forcemainmenu = '', $forceleftmenu = '')
67   	{
68		global $conf, $user, $langs;
69
70   		// On sauve en session le menu principal choisi
71		if (GETPOSTISSET("mainmenu")) $_SESSION["mainmenu"] = GETPOST("mainmenu", 'aZ09');
72		if (GETPOSTISSET("idmenu"))   $_SESSION["idmenu"] = GETPOST("idmenu", 'int');
73
74		// Read mainmenu and leftmenu that define which menu to show
75		if (GETPOSTISSET("mainmenu"))
76		{
77			// On sauve en session le menu principal choisi
78			$mainmenu = GETPOST("mainmenu", 'aZ09');
79			$_SESSION["mainmenu"] = $mainmenu;
80			$_SESSION["leftmenuopened"] = "";
81		} else {
82			// On va le chercher en session si non defini par le lien
83			$mainmenu = isset($_SESSION["mainmenu"]) ? $_SESSION["mainmenu"] : '';
84		}
85		if (!empty($forcemainmenu)) $mainmenu = $forcemainmenu;
86
87		if (GETPOSTISSET("leftmenu"))
88		{
89			// On sauve en session le menu principal choisi
90			$leftmenu = GETPOST("leftmenu", 'aZ09');
91			$_SESSION["leftmenu"] = $leftmenu;
92
93			if ($_SESSION["leftmenuopened"] == $leftmenu)	// To collapse
94			{
95				//$leftmenu="";
96				$_SESSION["leftmenuopened"] = "";
97			} else {
98				$_SESSION["leftmenuopened"] = $leftmenu;
99			}
100		} else {
101			// On va le chercher en session si non defini par le lien
102			$leftmenu = isset($_SESSION["leftmenu"]) ? $_SESSION["leftmenu"] : '';
103		}
104		if (!empty($forceleftmenu)) $leftmenu = $forceleftmenu;
105
106		require_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
107		$tabMenu = array();
108		$menuArbo = new Menubase($this->db, 'auguria');
109		$menuArbo->menuLoad($mainmenu, $leftmenu, $this->type_user, 'auguria', $tabMenu);
110		$this->tabMenu = $tabMenu;
111		//var_dump($tabMenu);
112
113		//if ($forcemainmenu == 'all') { var_dump($this->tabMenu); exit; }
114   	}
115
116
117	/**
118	 *  Show menu.
119	 *  Menu defined in sql tables were stored into $this->tabMenu BEFORE this is called.
120	 *
121	 *	@param	string	$mode		    'top', 'topnb', 'left', 'jmobile' (used to get full xml ul/li menu)
122	 *  @param	array	$moredata		An array with more data to output
123	 *  @return int                     0 or nb of top menu entries if $mode = 'topnb'
124	 */
125	public function showmenu($mode, $moredata = null)
126	{
127		global $conf, $langs, $user;
128
129		require_once DOL_DOCUMENT_ROOT.'/core/menus/standard/auguria.lib.php';
130
131		if ($this->type_user == 1)
132		{
133			$conf->global->MAIN_SEARCHFORM_SOCIETE_DISABLED = 1;
134			$conf->global->MAIN_SEARCHFORM_CONTACT_DISABLED = 1;
135		}
136
137		require_once DOL_DOCUMENT_ROOT.'/core/class/menu.class.php';
138		$this->menu = new Menu();
139
140		if (empty($conf->global->MAIN_MENU_INVERT))
141		{
142			if ($mode == 'top')  print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
143			if ($mode == 'left') print_left_auguria_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata);
144		} else {
145			$conf->global->MAIN_SHOW_LOGO = 0;
146			if ($mode == 'top')  print_left_auguria_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0);
147			if ($mode == 'left') print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
148		}
149
150		if ($mode == 'topnb')
151		{
152			print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode);
153			return $this->menu->getNbOfVisibleMenuEntries();
154		}
155
156		if ($mode == 'jmobile')     // Used to get menu in xml ul/li
157		{
158			print_auguria_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode);
159
160			// $this->menu->liste is top menu
161			//var_dump($this->menu->liste);exit;
162			$lastlevel = array();
163			print '<!-- Generate menu list from menu handler '.$this->name.' -->'."\n";
164			foreach ($this->menu->liste as $key => $val)		// $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
165			{
166				print '<ul class="ulmenu" data-inset="true">';
167				print '<li class="lilevel0">';
168				if ($val['enabled'] == 1)
169				{
170					$substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
171					$substitarray['__USERID__'] = $user->id; // For backward compatibility
172					$val['url'] = make_substitutions($val['url'], $substitarray);
173
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') print '<span class="fa fa-home fa-fw paddingright" aria-hidden="true"></span>';
181
182					print $val['titre'];
183					print '</a>'."\n";
184
185					// Search submenu fot this mainmenu entry
186					$tmpmainmenu = $val['mainmenu'];
187					$tmpleftmenu = 'all';
188					$submenu = new Menu();
189					print_left_auguria_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $submenu, 1, $tmpmainmenu, $tmpleftmenu);
190					$nexturl = dol_buildpath($submenu->liste[0]['url'], 1);
191
192					$canonrelurl = preg_replace('/\?.*$/', '', $relurl);
193					$canonnexturl = preg_replace('/\?.*$/', '', $nexturl);
194					//var_dump($canonrelurl);
195					//var_dump($canonnexturl);
196					print '<ul>'."\n";
197					if (($canonrelurl != $canonnexturl && !in_array($val['mainmenu'], array('tools')))
198						|| (strpos($canonrelurl, '/product/index.php') !== false || strpos($canonrelurl, '/compta/bank/list.php') !== false))
199					{
200						// We add sub entry
201						print str_pad('', 1).'<li class="lilevel1 ui-btn-icon-right ui-btn">'; // ui-btn to highlight on clic
202						print '<a href="'.$relurl.'">';
203						if ($langs->trans(ucfirst($val['mainmenu'])."Dashboard") == ucfirst($val['mainmenu'])."Dashboard")  // No translation
204						{
205							if (in_array($val['mainmenu'], array('cashdesk', 'externalsite', 'website', 'collab'))) print $langs->trans("Access");
206							else print $langs->trans("Dashboard");
207						} else print $langs->trans(ucfirst($val['mainmenu'])."Dashboard");
208						print '</a>';
209						print '</li>'."\n";
210					}
211
212					if ($val['level'] == 0)
213					{
214						if ($val['enabled'])
215						{
216							$lastlevel[0] = 'enabled';
217						} elseif ($showmenu)                 // Not enabled but visible (so greyed)
218						{
219							$lastlevel[0] = 'greyed';
220						} else {
221							$lastlevel[0] = 'hidden';
222						}
223					}
224
225					$lastlevel2 = array();
226					foreach ($submenu->liste as $key2 => $val2)		// $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
227					{
228						$showmenu = true;
229						if (!empty($conf->global->MAIN_MENU_HIDE_UNAUTHORIZED) && empty($val2['enabled'])) $showmenu = false;
230
231						// If at least one parent is not enabled, we do not show any menu of all children
232						if ($val2['level'] > 0)
233						{
234							$levelcursor = $val2['level'] - 1;
235							while ($levelcursor >= 0)
236							{
237								if ($lastlevel2[$levelcursor] != 'enabled') $showmenu = false;
238								$levelcursor--;
239							}
240						}
241
242						if ($showmenu)		// Visible (option to hide when not allowed is off or allowed)
243	   					{
244	   						$substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
245	   						$substitarray['__USERID__'] = $user->id; // For backward compatibility
246	   						$val2['url'] = make_substitutions($val2['url'], $substitarray); // Make also substitution of __(XXX)__ and __[XXX]__
247
248	   						if (!preg_match("/^(http:\/\/|https:\/\/)/i", $val2['url']))
249	   						{
250	   							$relurl2 = dol_buildpath($val2['url'], 1);
251	   						} else {
252	   							$relurl2 = $val2['url'];
253	   						}
254							$canonurl2 = preg_replace('/\?.*$/', '', $val2['url']);
255							//var_dump($val2['url'].' - '.$canonurl2.' - '.$val2['level']);
256							if (in_array($canonurl2, array('/admin/index.php', '/admin/tools/index.php', '/core/tools.php'))) $relurl2 = '';
257
258							$disabled = '';
259							if (!$val2['enabled'])
260							{
261								$disabled = " vsmenudisabled";
262							}
263
264							print str_pad('', $val2['level'] + 1);
265							print '<li class="lilevel'.($val2['level'] + 1);
266							if ($val2['level'] == 0) print ' ui-btn-icon-right ui-btn'; // ui-btn to highlight on clic
267							print $disabled.'">'; // ui-btn to highlight on clic
268							if ($relurl2)
269							{
270								if ($val2['enabled'])	// Allowed
271								{
272									print '<a href="'.$relurl2.'"';
273									//print ' data-ajax="false"';
274									print '>';
275									$lastlevel2[$val2['level']] = 'enabled';
276								} else // Not allowed but visible (greyed)
277								{
278									print '<a href="#" class="vsmenudisabled">';
279									$lastlevel2[$val2['level']] = 'greyed';
280								}
281							} else {
282								if ($val2['enabled'])	// Allowed
283								{
284									$lastlevel2[$val2['level']] = 'enabled';
285								} else {
286									$lastlevel2[$val2['level']] = 'greyed';
287								}
288							}
289							print $val2['titre'];
290							if ($relurl2)
291							{
292								if ($val2['enabled'])	// Allowed
293									print '</a>';
294								else print '</a>';
295							}
296							print '</li>'."\n";
297	   					}
298					}
299					//var_dump($submenu);
300					print '</ul>';
301				}
302				if ($val['enabled'] == 2)
303				{
304					print '<font class="vsmenudisabled">'.$val['titre'].'</font>';
305				}
306				print '</li>';
307				print '</ul>'."\n";
308			}
309		}
310
311		unset($this->menu);
312
313		//print 'xx'.$mode;
314		return 0;
315	}
316}
317