eldy_menu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/menus/standard/eldy_menu.php
  20. * \brief Menu eldy manager
  21. */
  22. /**
  23. * Class to manage menu Eldy
  24. */
  25. class MenuManager
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. public $type_user; // Put 0 for internal users, 1 for external users
  32. public $atarget = ""; // To store default target to use onto links
  33. public $name = "eldy";
  34. public $menu_array;
  35. public $menu_array_after;
  36. public $tabMenu;
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. * @param int $type_user Type of user
  42. */
  43. public function __construct($db, $type_user)
  44. {
  45. $this->type_user = $type_user;
  46. $this->db = $db;
  47. }
  48. /**
  49. * Load this->tabMenu
  50. *
  51. * @param string $forcemainmenu To force mainmenu to load
  52. * @param string $forceleftmenu To force leftmenu to load
  53. * @return void
  54. */
  55. public function loadMenu($forcemainmenu = '', $forceleftmenu = '')
  56. {
  57. global $conf, $user, $langs;
  58. // We save into session the main menu selected
  59. if (GETPOSTISSET("mainmenu")) {
  60. $_SESSION["mainmenu"] = GETPOST("mainmenu", 'aZ09');
  61. }
  62. if (GETPOSTISSET("idmenu")) {
  63. $_SESSION["idmenu"] = GETPOST("idmenu", 'int');
  64. }
  65. // Read now mainmenu and leftmenu that define which menu to show
  66. if (GETPOSTISSET("mainmenu")) {
  67. // On sauve en session le menu principal choisi
  68. $mainmenu = GETPOST("mainmenu", 'aZ09');
  69. $_SESSION["mainmenu"] = $mainmenu;
  70. $_SESSION["leftmenuopened"] = "";
  71. } else {
  72. // On va le chercher en session si non defini par le lien
  73. $mainmenu = isset($_SESSION["mainmenu"]) ? $_SESSION["mainmenu"] : '';
  74. }
  75. if (!empty($forcemainmenu)) {
  76. $mainmenu = $forcemainmenu;
  77. }
  78. if (GETPOSTISSET("leftmenu")) {
  79. // On sauve en session le menu principal choisi
  80. $leftmenu = GETPOST("leftmenu", 'aZ09');
  81. $_SESSION["leftmenu"] = $leftmenu;
  82. if ($_SESSION["leftmenuopened"] == $leftmenu) { // To collapse
  83. //$leftmenu="";
  84. $_SESSION["leftmenuopened"] = "";
  85. } else {
  86. $_SESSION["leftmenuopened"] = $leftmenu;
  87. }
  88. } else {
  89. // On va le chercher en session si non defini par le lien
  90. $leftmenu = isset($_SESSION["leftmenu"]) ? $_SESSION["leftmenu"] : '';
  91. }
  92. if (!empty($forceleftmenu)) {
  93. $leftmenu = $forceleftmenu;
  94. }
  95. require_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
  96. $tabMenu = array();
  97. $menuArbo = new Menubase($this->db, 'eldy');
  98. $menuArbo->menuLoad($mainmenu, $leftmenu, $this->type_user, 'eldy', $tabMenu);
  99. $this->tabMenu = $tabMenu;
  100. //var_dump($tabMenu);
  101. //if ($forcemainmenu == 'all') { var_dump($this->tabMenu); exit; }
  102. }
  103. /**
  104. * Show menu.
  105. * Menu defined in sql tables were stored into $this->tabMenu BEFORE this is called.
  106. *
  107. * @param string $mode 'top', 'topnb', 'left', 'jmobile' (used to get full xml ul/li menu)
  108. * @param array $moredata An array with more data to output
  109. * @return int 0 or nb of top menu entries if $mode = 'topnb'
  110. */
  111. public function showmenu($mode, $moredata = null)
  112. {
  113. global $conf, $langs, $user;
  114. //var_dump($this->tabMenu);
  115. require_once DOL_DOCUMENT_ROOT.'/core/menus/standard/eldy.lib.php';
  116. if ($this->type_user == 1) {
  117. $conf->global->MAIN_SEARCHFORM_SOCIETE_DISABLED = 1;
  118. $conf->global->MAIN_SEARCHFORM_CONTACT_DISABLED = 1;
  119. }
  120. require_once DOL_DOCUMENT_ROOT.'/core/class/menu.class.php';
  121. $this->menu = new Menu();
  122. if (empty($conf->global->MAIN_MENU_INVERT)) {
  123. if ($mode == 'top') {
  124. print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
  125. }
  126. if ($mode == 'left') {
  127. print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata, $this->type_user);
  128. }
  129. } else {
  130. $conf->global->MAIN_SHOW_LOGO = 0;
  131. if ($mode == 'top') {
  132. print_left_eldy_menu($this->db, $this->menu_array, $this->menu_array_after, $this->tabMenu, $this->menu, 0, '', '', $moredata, $this->type_user);
  133. }
  134. if ($mode == 'left') {
  135. print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 0, $mode);
  136. }
  137. }
  138. if ($mode == 'topnb') {
  139. print_eldy_menu($this->db, $this->atarget, $this->type_user, $this->tabMenu, $this->menu, 1, $mode); // no output
  140. return $this->menu->getNbOfVisibleMenuEntries();
  141. }
  142. if ($mode == 'jmobile') { // Used to get menu in xml ul/li
  143. 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
  144. // $this->menu->liste is top menu
  145. //var_dump($this->menu->liste);exit;
  146. $lastlevel = array();
  147. print '<!-- Generate menu list from menu handler '.$this->name.' -->'."\n";
  148. foreach ($this->menu->liste as $key => $val) { // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
  149. print '<ul class="ulmenu" data-inset="true">';
  150. print '<li class="lilevel0">';
  151. if ($val['enabled'] == 1) {
  152. $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
  153. $substitarray['__USERID__'] = $user->id; // For backward compatibility
  154. $val['url'] = make_substitutions($val['url'], $substitarray);
  155. $relurl = dol_buildpath($val['url'], 1);
  156. $canonurl = preg_replace('/\?.*$/', '', $val['url']);
  157. print '<a class="alilevel0" href="#">';
  158. // Add font-awesome
  159. if ($val['level'] == 0 && !empty($val['prefix'])) {
  160. print $val['prefix'];
  161. }
  162. print $val['titre'];
  163. print '</a>'."\n";
  164. // Search submenu fot this mainmenu entry
  165. $tmpmainmenu = $val['mainmenu'];
  166. $tmpleftmenu = 'all';
  167. $submenu = new Menu();
  168. 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)
  169. // Note: $submenu contains menu entry with substitution not yet done
  170. //if ($tmpmainmenu.'-'.$tmpleftmenu == 'home-all') {
  171. //var_dump($submenu); exit;
  172. //}
  173. //if ($tmpmainmenu=='accountancy') {
  174. //var_dump($submenu->liste); exit;
  175. //}
  176. $nexturl = dol_buildpath(empty($submenu->liste[0]['url']) ? '' : $submenu->liste[0]['url'], 1);
  177. $canonrelurl = preg_replace('/\?.*$/', '', $relurl);
  178. $canonnexturl = preg_replace('/\?.*$/', '', $nexturl);
  179. //var_dump($canonrelurl);
  180. //var_dump($canonnexturl);
  181. print '<ul>'."\n";
  182. if (($canonrelurl != $canonnexturl && !in_array($val['mainmenu'], array('tools')))
  183. || (strpos($canonrelurl, '/product/index.php') !== false || strpos($canonrelurl, '/compta/bank/list.php') !== false)) {
  184. // We add sub entry
  185. print str_pad('', 1).'<li class="lilevel1 ui-btn-icon-right ui-btn">'; // ui-btn to highlight on clic
  186. print '<a href="'.$relurl.'">';
  187. if ($val['level'] == 0) {
  188. print '<span class="fa fa-home fa-fw paddingright pictofixedwidth" aria-hidden="true"></span>';
  189. }
  190. if ($langs->trans(ucfirst($val['mainmenu'])."Dashboard") == ucfirst($val['mainmenu'])."Dashboard") { // No translation
  191. if (in_array($val['mainmenu'], array('cashdesk', 'externalsite', 'website', 'collab', 'takepos'))) {
  192. print $langs->trans("Access");
  193. } else {
  194. print $langs->trans("Dashboard");
  195. }
  196. } else {
  197. print $langs->trans(ucfirst($val['mainmenu'])."Dashboard");
  198. }
  199. print '</a>';
  200. print '</li>'."\n";
  201. }
  202. /*
  203. if ($val['level'] == 0) {
  204. if ($val['enabled']) {
  205. $lastlevel[0] = 'enabled';
  206. } elseif ($showmenu) {
  207. // Not enabled but visible (so greyed)
  208. $lastlevel[0] = 'greyed';
  209. } else {
  210. $lastlevel[0] = 'hidden';
  211. }
  212. }
  213. */
  214. $lastlevel2 = array();
  215. foreach ($submenu->liste as $key2 => $val2) { // $val['url','titre','level','enabled'=0|1|2,'target','mainmenu','leftmenu'
  216. $showmenu = true;
  217. if (!empty($conf->global->MAIN_MENU_HIDE_UNAUTHORIZED) && empty($val2['enabled'])) {
  218. $showmenu = false;
  219. }
  220. // If at least one parent is not enabled, we do not show any menu of all children
  221. if ($val2['level'] > 0) {
  222. $levelcursor = $val2['level'] - 1;
  223. while ($levelcursor >= 0) {
  224. if ($lastlevel2[$levelcursor] != 'enabled') {
  225. $showmenu = false;
  226. }
  227. $levelcursor--;
  228. }
  229. }
  230. if ($showmenu) { // Visible (option to hide when not allowed is off or allowed)
  231. $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
  232. $substitarray['__USERID__'] = $user->id; // For backward compatibility
  233. $val2['url'] = make_substitutions($val2['url'], $substitarray); // Make also substitution of __(XXX)__ and __[XXX]__
  234. if (!preg_match("/^(http:\/\/|https:\/\/)/i", $val2['url'])) {
  235. $relurl2 = dol_buildpath($val2['url'], 1);
  236. } else {
  237. $relurl2 = $val2['url'];
  238. }
  239. $canonurl2 = preg_replace('/\?.*$/', '', $val2['url']);
  240. //var_dump($val2['url'].' - '.$canonurl2.' - '.$val2['level']);
  241. if (in_array($canonurl2, array('/admin/index.php', '/admin/tools/index.php', '/core/tools.php'))) {
  242. $relurl2 = '';
  243. }
  244. $disabled = '';
  245. if (!$val2['enabled']) {
  246. $disabled = " vsmenudisabled";
  247. }
  248. print str_pad('', $val2['level'] + 1);
  249. print '<li class="lilevel'.($val2['level'] + 1);
  250. if ($val2['level'] == 0) {
  251. print ' ui-btn-icon-right ui-btn'; // ui-btn to highlight on clic
  252. }
  253. print $disabled.'">'; // ui-btn to highlight on clic
  254. if ($relurl2) {
  255. if ($val2['enabled']) { // Allowed
  256. print '<a href="'.$relurl2.'"';
  257. //print ' data-ajax="false"';
  258. print '>';
  259. $lastlevel2[$val2['level']] = 'enabled';
  260. } else // Not allowed but visible (greyed)
  261. {
  262. print '<a href="#" class="vsmenudisabled">';
  263. $lastlevel2[$val2['level']] = 'greyed';
  264. }
  265. } else {
  266. if ($val2['enabled']) { // Allowed
  267. $lastlevel2[$val2['level']] = 'enabled';
  268. } else {
  269. $lastlevel2[$val2['level']] = 'greyed';
  270. }
  271. }
  272. // Add font-awesome
  273. if ($val2['level'] == 0 && !empty($val2['prefix'])) {
  274. print $val2['prefix'];
  275. }
  276. print $val2['titre'];
  277. if ($relurl2) {
  278. if ($val2['enabled']) { // Allowed
  279. print '</a>';
  280. } else {
  281. print '</a>';
  282. }
  283. }
  284. print '</li>'."\n";
  285. }
  286. }
  287. //var_dump($submenu);
  288. print '</ul>';
  289. }
  290. if ($val['enabled'] == 2) {
  291. print '<span class="spanlilevel0 vsmenudisabled">';
  292. // Add font-awesome
  293. if ($val['level'] == 0 && !empty($val['prefix'])) {
  294. print $val['prefix'];
  295. }
  296. print $val['titre'];
  297. print '</span>';
  298. }
  299. print '</li>';
  300. print '</ul>'."\n";
  301. }
  302. }
  303. unset($this->menu);
  304. //print 'xx'.$mode;
  305. return 0;
  306. }
  307. }