menubase.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. <?php
  2. /* Copyright (C) 2007-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
  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. * \file htdocs/core/class/menubase.class.php
  21. * \ingroup core
  22. * \brief File of class to manage dynamic menu entries
  23. */
  24. /**
  25. * Class to manage menu entries
  26. */
  27. class Menubase
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error;
  37. /**
  38. * @var string[] Error codes (or messages)
  39. */
  40. public $errors = array();
  41. /**
  42. * @var int ID
  43. */
  44. public $id;
  45. /**
  46. * @var string Menu handler
  47. */
  48. public $menu_handler;
  49. /**
  50. * @var string Module name if record is added by a module
  51. */
  52. public $module;
  53. /**
  54. * @var string Menu top or left
  55. */
  56. public $type;
  57. /**
  58. * @var string Name family/module for top menu (home, companies, ...)
  59. */
  60. public $mainmenu;
  61. /**
  62. * @var int 0 or Id of mother menu line, or -1 if we use fk_mainmenu and fk_leftmenu
  63. */
  64. public $fk_menu;
  65. /**
  66. * @var string fk_mainmenu
  67. */
  68. public $fk_mainmenu;
  69. /**
  70. * @var string fk_leftmenu
  71. */
  72. public $fk_leftmenu;
  73. /**
  74. * @var int Sort order of entry
  75. */
  76. public $position;
  77. /**
  78. * @var string Relative (or absolute) url to go
  79. */
  80. public $url;
  81. /**
  82. * @var string Target of Url link
  83. */
  84. public $target;
  85. /**
  86. * @var string Key for menu translation
  87. * @deprecated
  88. * @see $title
  89. */
  90. public $titre;
  91. /**
  92. * @var string Key for menu translation
  93. */
  94. public $title;
  95. /**
  96. * @var string Lang file to load for translation
  97. */
  98. public $langs;
  99. /**
  100. * @var string Not used
  101. * @deprecated
  102. */
  103. public $level;
  104. /**
  105. * @var string Name family/module for left menu (setup, info, ...)
  106. */
  107. public $leftmenu;
  108. /**
  109. * @var string Condition to show enabled or disabled
  110. */
  111. public $perms;
  112. /**
  113. * @var string Condition to show or hide
  114. */
  115. public $enabled;
  116. /**
  117. * @var int 0 if menu for all users, 1 for external only, 2 for internal only
  118. */
  119. public $user;
  120. /**
  121. * @var int timestamp
  122. */
  123. public $tms;
  124. /**
  125. * Constructor
  126. *
  127. * @param DoliDB $db Database handler
  128. * @param string $menu_handler Menu handler
  129. */
  130. public function __construct($db, $menu_handler = '')
  131. {
  132. $this->db = $db;
  133. $this->menu_handler = $menu_handler;
  134. return 1;
  135. }
  136. /**
  137. * Create menu entry into database
  138. *
  139. * @param User $user User that create
  140. * @return int <0 if KO, Id of record if OK
  141. */
  142. public function create($user = null)
  143. {
  144. global $conf, $langs;
  145. // Clean parameters
  146. $this->menu_handler = trim($this->menu_handler);
  147. $this->module = trim($this->module);
  148. $this->type = trim($this->type);
  149. $this->mainmenu = trim($this->mainmenu);
  150. $this->leftmenu = trim($this->leftmenu);
  151. $this->fk_menu = (int) $this->fk_menu; // If -1, fk_mainmenu and fk_leftmenu must be defined
  152. $this->fk_mainmenu = trim($this->fk_mainmenu);
  153. $this->fk_leftmenu = trim($this->fk_leftmenu);
  154. $this->position = (int) $this->position;
  155. $this->url = trim($this->url);
  156. $this->target = trim($this->target);
  157. $this->title = trim($this->title);
  158. $this->langs = trim($this->langs);
  159. $this->perms = trim($this->perms);
  160. $this->enabled = trim($this->enabled);
  161. $this->user = (int) $this->user;
  162. if (empty($this->position)) $this->position = 0;
  163. if (!$this->level) $this->level = 0;
  164. // Check parameters
  165. if (empty($this->menu_handler)) return -1;
  166. // For PGSQL, we must first found the max rowid and use it as rowid in insert because postgresql
  167. // may use an already used value because its internal cursor does not increase when we do
  168. // an insert with a forced id.
  169. if (in_array($this->db->type, array('pgsql')))
  170. {
  171. $sql = "SELECT MAX(rowid) as maxrowid FROM ".MAIN_DB_PREFIX."menu";
  172. $resqlrowid = $this->db->query($sql);
  173. if ($resqlrowid) {
  174. $obj = $this->db->fetch_object($resqlrowid);
  175. $maxrowid = $obj->maxrowid;
  176. // Max rowid can be empty if there is no record yet
  177. if (empty($maxrowid)) $maxrowid = 1;
  178. $sql = "SELECT setval('".MAIN_DB_PREFIX."menu_rowid_seq', ".($maxrowid).")";
  179. //print $sql; exit;
  180. $resqlrowidset = $this->db->query($sql);
  181. if (!$resqlrowidset) dol_print_error($this->db);
  182. } else dol_print_error($this->db);
  183. }
  184. // Check that entry does not exists yet on key menu_handler-fk_menu-position-url-entity, to avoid errors with postgresql
  185. $sql = "SELECT count(*)";
  186. $sql .= " FROM ".MAIN_DB_PREFIX."menu";
  187. $sql .= " WHERE menu_handler = '".$this->db->escape($this->menu_handler)."'";
  188. $sql .= " AND fk_menu = ".((int) $this->fk_menu);
  189. $sql .= " AND position = ".((int) $this->position);
  190. $sql .= " AND url = '".$this->db->escape($this->url)."'";
  191. $sql .= " AND entity = ".$conf->entity;
  192. $result = $this->db->query($sql);
  193. if ($result)
  194. {
  195. $row = $this->db->fetch_row($result);
  196. if ($row[0] == 0) // If not found
  197. {
  198. // Insert request
  199. $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu(";
  200. $sql .= "menu_handler,";
  201. $sql .= "entity,";
  202. $sql .= "module,";
  203. $sql .= "type,";
  204. $sql .= "mainmenu,";
  205. $sql .= "leftmenu,";
  206. $sql .= "fk_menu,";
  207. $sql .= "fk_mainmenu,";
  208. $sql .= "fk_leftmenu,";
  209. $sql .= "position,";
  210. $sql .= "url,";
  211. $sql .= "target,";
  212. $sql .= "titre,";
  213. $sql .= "langs,";
  214. $sql .= "perms,";
  215. $sql .= "enabled,";
  216. $sql .= "usertype";
  217. $sql .= ") VALUES (";
  218. $sql .= " '".$this->db->escape($this->menu_handler)."',";
  219. $sql .= " '".$this->db->escape($conf->entity)."',";
  220. $sql .= " '".$this->db->escape($this->module)."',";
  221. $sql .= " '".$this->db->escape($this->type)."',";
  222. $sql .= " ".($this->mainmenu ? "'".$this->db->escape($this->mainmenu)."'" : "''").","; // Can't be null
  223. $sql .= " ".($this->leftmenu ? "'".$this->db->escape($this->leftmenu)."'" : "null").",";
  224. $sql .= " ".((int) $this->fk_menu).",";
  225. $sql .= " ".($this->fk_mainmenu ? "'".$this->db->escape($this->fk_mainmenu)."'" : "null").",";
  226. $sql .= " ".($this->fk_leftmenu ? "'".$this->db->escape($this->fk_leftmenu)."'" : "null").",";
  227. $sql .= " ".((int) $this->position).",";
  228. $sql .= " '".$this->db->escape($this->url)."',";
  229. $sql .= " '".$this->db->escape($this->target)."',";
  230. $sql .= " '".$this->db->escape($this->title)."',";
  231. $sql .= " '".$this->db->escape($this->langs)."',";
  232. $sql .= " '".$this->db->escape($this->perms)."',";
  233. $sql .= " '".$this->db->escape($this->enabled)."',";
  234. $sql .= " '".$this->db->escape($this->user)."'";
  235. $sql .= ")";
  236. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  237. $resql = $this->db->query($sql);
  238. if ($resql)
  239. {
  240. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."menu");
  241. dol_syslog(get_class($this)."::create record added has rowid=".$this->id, LOG_DEBUG);
  242. return $this->id;
  243. } else {
  244. $this->error = "Error ".$this->db->lasterror();
  245. return -1;
  246. }
  247. } else {
  248. dol_syslog(get_class($this)."::create menu entry already exists", LOG_WARNING);
  249. $this->error = 'Error Menu entry already exists';
  250. return 0;
  251. }
  252. } else {
  253. return -1;
  254. }
  255. }
  256. /**
  257. * Update menu entry into database.
  258. *
  259. * @param User $user User that modify
  260. * @param int $notrigger 0=no, 1=yes (no update trigger)
  261. * @return int <0 if KO, >0 if OK
  262. */
  263. public function update($user = null, $notrigger = 0)
  264. {
  265. //global $conf, $langs;
  266. // Clean parameters
  267. $this->rowid = trim($this->rowid);
  268. $this->menu_handler = trim($this->menu_handler);
  269. $this->module = trim($this->module);
  270. $this->type = trim($this->type);
  271. $this->mainmenu = trim($this->mainmenu);
  272. $this->leftmenu = trim($this->leftmenu);
  273. $this->fk_menu = (int) $this->fk_menu;
  274. $this->fk_mainmenu = trim($this->fk_mainmenu);
  275. $this->fk_leftmenu = trim($this->fk_leftmenu);
  276. $this->position = (int) $this->position;
  277. $this->url = trim($this->url);
  278. $this->target = trim($this->target);
  279. $this->title = trim($this->title);
  280. $this->langs = trim($this->langs);
  281. $this->perms = trim($this->perms);
  282. $this->enabled = trim($this->enabled);
  283. $this->user = (int) $this->user;
  284. // Check parameters
  285. // Put here code to add control on parameters values
  286. // Update request
  287. $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
  288. $sql .= " menu_handler='".$this->db->escape($this->menu_handler)."',";
  289. $sql .= " module='".$this->db->escape($this->module)."',";
  290. $sql .= " type='".$this->db->escape($this->type)."',";
  291. $sql .= " mainmenu='".$this->db->escape($this->mainmenu)."',";
  292. $sql .= " leftmenu='".$this->db->escape($this->leftmenu)."',";
  293. $sql .= " fk_menu=".$this->fk_menu.",";
  294. $sql .= " fk_mainmenu=".($this->fk_mainmenu ? "'".$this->db->escape($this->fk_mainmenu)."'" : "null").",";
  295. $sql .= " fk_leftmenu=".($this->fk_leftmenu ? "'".$this->db->escape($this->fk_leftmenu)."'" : "null").",";
  296. $sql .= " position=".($this->position > 0 ? $this->position : 0).",";
  297. $sql .= " url='".$this->db->escape($this->url)."',";
  298. $sql .= " target='".$this->db->escape($this->target)."',";
  299. $sql .= " titre='".$this->db->escape($this->title)."',";
  300. $sql .= " langs='".$this->db->escape($this->langs)."',";
  301. $sql .= " perms='".$this->db->escape($this->perms)."',";
  302. $sql .= " enabled='".$this->db->escape($this->enabled)."',";
  303. $sql .= " usertype='".$this->db->escape($this->user)."'";
  304. $sql .= " WHERE rowid=".$this->id;
  305. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  306. $resql = $this->db->query($sql);
  307. if (!$resql)
  308. {
  309. $this->error = "Error ".$this->db->lasterror();
  310. return -1;
  311. }
  312. return 1;
  313. }
  314. /**
  315. * Load object in memory from database
  316. *
  317. * @param int $id Id object
  318. * @param User $user User that load
  319. * @return int <0 if KO, >0 if OK
  320. */
  321. public function fetch($id, $user = null)
  322. {
  323. //global $langs;
  324. $sql = "SELECT";
  325. $sql .= " t.rowid,";
  326. $sql .= " t.menu_handler,";
  327. $sql .= " t.entity,";
  328. $sql .= " t.module,";
  329. $sql .= " t.type,";
  330. $sql .= " t.mainmenu,";
  331. $sql .= " t.leftmenu,";
  332. $sql .= " t.fk_menu,";
  333. $sql .= " t.fk_mainmenu,";
  334. $sql .= " t.fk_leftmenu,";
  335. $sql .= " t.position,";
  336. $sql .= " t.url,";
  337. $sql .= " t.target,";
  338. $sql .= " t.titre as title,";
  339. $sql .= " t.langs,";
  340. $sql .= " t.perms,";
  341. $sql .= " t.enabled,";
  342. $sql .= " t.usertype as user,";
  343. $sql .= " t.tms";
  344. $sql .= " FROM ".MAIN_DB_PREFIX."menu as t";
  345. $sql .= " WHERE t.rowid = ".$id;
  346. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  347. $resql = $this->db->query($sql);
  348. if ($resql)
  349. {
  350. if ($this->db->num_rows($resql))
  351. {
  352. $obj = $this->db->fetch_object($resql);
  353. $this->id = $obj->rowid;
  354. $this->menu_handler = $obj->menu_handler;
  355. $this->entity = $obj->entity;
  356. $this->module = $obj->module;
  357. $this->type = $obj->type;
  358. $this->mainmenu = $obj->mainmenu;
  359. $this->leftmenu = $obj->leftmenu;
  360. $this->fk_menu = $obj->fk_menu;
  361. $this->fk_mainmenu = $obj->fk_mainmenu;
  362. $this->fk_leftmenu = $obj->fk_leftmenu;
  363. $this->position = $obj->position;
  364. $this->url = $obj->url;
  365. $this->target = $obj->target;
  366. $this->title = $obj->title;
  367. $this->langs = $obj->langs;
  368. $this->perms = $obj->perms;
  369. $this->enabled = str_replace("\"", "'", $obj->enabled);
  370. $this->user = $obj->user;
  371. $this->tms = $this->db->jdate($obj->tms);
  372. }
  373. $this->db->free($resql);
  374. return 1;
  375. } else {
  376. $this->error = "Error ".$this->db->lasterror();
  377. return -1;
  378. }
  379. }
  380. /**
  381. * Delete object in database
  382. *
  383. * @param User $user User that delete
  384. * @return int <0 if KO, >0 if OK
  385. */
  386. public function delete($user)
  387. {
  388. //global $conf, $langs;
  389. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  390. $sql .= " WHERE rowid=".$this->id;
  391. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  392. $resql = $this->db->query($sql);
  393. if (!$resql)
  394. {
  395. $this->error = "Error ".$this->db->lasterror();
  396. return -1;
  397. }
  398. return 1;
  399. }
  400. /**
  401. * Initialise an instance with random values.
  402. * Used to build previews or test instances.
  403. * id must be 0 if object instance is a specimen.
  404. *
  405. * @return void
  406. */
  407. public function initAsSpecimen()
  408. {
  409. $this->id = 0;
  410. $this->menu_handler = 'all';
  411. $this->module = 'specimen';
  412. $this->type = 'top';
  413. $this->mainmenu = '';
  414. $this->fk_menu = '0';
  415. $this->position = '';
  416. $this->url = 'http://dummy';
  417. $this->target = '';
  418. $this->title = 'Specimen menu';
  419. $this->langs = '';
  420. $this->leftmenu = '';
  421. $this->perms = '';
  422. $this->enabled = '';
  423. $this->user = '';
  424. $this->tms = '';
  425. }
  426. /**
  427. * Load tabMenu array with top menu entries found into database.
  428. *
  429. * @param string $mymainmenu Value for mainmenu to filter menu to load (always '')
  430. * @param string $myleftmenu Value for leftmenu to filter menu to load (always '')
  431. * @param int $type_user 0=Menu for backoffice, 1=Menu for front office
  432. * @param string $menu_handler Filter on name of menu_handler used (auguria, eldy...)
  433. * @param array $tabMenu If array with menu entries already loaded, we put this array here (in most cases, it's empty)
  434. * @return array Return array with menu entries for top menu
  435. */
  436. public function menuTopCharger($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
  437. {
  438. global $langs, $user, $conf; // To export to dol_eval function
  439. global $mainmenu, $leftmenu; // To export to dol_eval function
  440. $mainmenu = $mymainmenu; // To export to dol_eval function
  441. $leftmenu = $myleftmenu; // To export to dol_eval function
  442. $newTabMenu = array();
  443. foreach ($tabMenu as $val)
  444. {
  445. if ($val['type'] == 'top') $newTabMenu[] = $val;
  446. }
  447. return $newTabMenu;
  448. }
  449. /**
  450. * Load entries found from database (and stored into $tabMenu) in $this->newmenu array.
  451. * Warning: Entries in $tabMenu must have child after parent
  452. *
  453. * @param Menu $newmenu Menu array to complete (in most cases, it's empty, may be already initialized with some menu manager like eldy)
  454. * @param string $mymainmenu Value for mainmenu to filter menu to load (often $_SESSION["mainmenu"])
  455. * @param string $myleftmenu Value for leftmenu to filter menu to load (always '')
  456. * @param int $type_user 0=Menu for backoffice, 1=Menu for front office
  457. * @param string $menu_handler Filter on name of menu_handler used (auguria, eldy...)
  458. * @param array $tabMenu Array with menu entries already loaded
  459. * @return Menu Menu array for particular mainmenu value or full tabArray
  460. */
  461. public function menuLeftCharger($newmenu, $mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
  462. {
  463. global $langs, $user, $conf; // To export to dol_eval function
  464. global $mainmenu, $leftmenu; // To export to dol_eval function
  465. $mainmenu = $mymainmenu; // To export to dol_eval function
  466. $leftmenu = $myleftmenu; // To export to dol_eval function
  467. // Detect what is top mainmenu id
  468. $menutopid = '';
  469. foreach ($tabMenu as $key => $val)
  470. {
  471. // Define menutopid of mainmenu
  472. if (empty($menutopid) && $val['type'] == 'top' && $val['mainmenu'] == $mainmenu)
  473. {
  474. $menutopid = $val['rowid'];
  475. break;
  476. }
  477. }
  478. // We initialize newmenu with first already found menu entries
  479. $this->newmenu = $newmenu;
  480. // Now complete $this->newmenu->list to add entries found into $tabMenu that are childs of mainmenu=$menutopid, using the fk_menu link that is int (old method)
  481. $this->recur($tabMenu, $menutopid, 1);
  482. // Now complete $this->newmenu->list when fk_menu value is -1 (left menu added by modules with no top menu)
  483. foreach ($tabMenu as $key => $val)
  484. {
  485. //var_dump($tabMenu);
  486. if ($val['fk_menu'] == -1 && $val['fk_mainmenu'] == $mainmenu) // We found a menu entry not linked to parent with good mainmenu
  487. {
  488. //print 'Try to add menu (current is mainmenu='.$mainmenu.' leftmenu='.$leftmenu.') for '.join(',',$val).' fk_mainmenu='.$val['fk_mainmenu'].' fk_leftmenu='.$val['fk_leftmenu'].'<br>';
  489. //var_dump($this->newmenu->liste);exit;
  490. if (empty($val['fk_leftmenu']))
  491. {
  492. $this->newmenu->add($val['url'], $val['titre'], 0, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu'], $val['position']);
  493. //var_dump($this->newmenu->liste);
  494. } else {
  495. // Search first menu with this couple (mainmenu,leftmenu)=(fk_mainmenu,fk_leftmenu)
  496. $searchlastsub = 0; $lastid = 0; $nextid = 0; $found = 0;
  497. foreach ($this->newmenu->liste as $keyparent => $valparent)
  498. {
  499. //var_dump($valparent);
  500. if ($searchlastsub) // If we started to search for last submenu
  501. {
  502. if ($valparent['level'] >= $searchlastsub) $lastid = $keyparent;
  503. if ($valparent['level'] < $searchlastsub)
  504. {
  505. $nextid = $keyparent;
  506. break;
  507. }
  508. }
  509. if ($valparent['mainmenu'] == $val['fk_mainmenu'] && $valparent['leftmenu'] == $val['fk_leftmenu'])
  510. {
  511. //print "We found parent: keyparent='.$keyparent.' - level=".$valparent['level'].' - '.join(',',$valparent).'<br>';
  512. // Now we look to find last subelement of this parent (we add at end)
  513. $searchlastsub = ($valparent['level'] + 1);
  514. $lastid = $keyparent;
  515. $found = 1;
  516. }
  517. }
  518. //print 'We must insert menu entry between entry '.$lastid.' and '.$nextid.'<br>';
  519. if ($found) $this->newmenu->insert($lastid, $val['url'], $val['titre'], $searchlastsub, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu'], $val['position']);
  520. else {
  521. dol_syslog("Error. Modules ".$val['module']." has defined a menu entry with a parent='fk_mainmenu=".$val['fk_leftmenu'].",fk_leftmenu=".$val['fk_leftmenu']."' and position=".$val['position'].'. The parent was not found. May be you forget it into your definition of menu, or may be the parent has a "position" that is after the child (fix field "position" of parent or child in this case).', LOG_WARNING);
  522. //print "Parent menu not found !!<br>";
  523. }
  524. }
  525. }
  526. }
  527. return $this->newmenu;
  528. }
  529. /**
  530. * Load entries found in database into variable $tabMenu. Note that only "database menu entries" are loaded here, hardcoded will not be present into output.
  531. *
  532. * @param string $mymainmenu Value for mainmenu that defined mainmenu
  533. * @param string $myleftmenu Value for left that defined leftmenu
  534. * @param int $type_user Looks for menu entry for 0=Internal users, 1=External users
  535. * @param string $menu_handler Name of menu_handler used ('auguria', 'eldy'...)
  536. * @param array $tabMenu Array to store new entries found (in most cases, it's empty, but may be alreay filled)
  537. * @return int >0 if OK, <0 if KO
  538. */
  539. public function menuLoad($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
  540. {
  541. global $langs, $user, $conf; // To export to dol_eval function
  542. global $mainmenu, $leftmenu; // To export to dol_eval function
  543. $mainmenu = $mymainmenu; // To export to dol_eval function
  544. $leftmenu = $myleftmenu; // To export to dol_eval function
  545. $sql = "SELECT m.rowid, m.type, m.module, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.url, m.titre, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu, m.position";
  546. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  547. $sql .= " WHERE m.entity IN (0,".$conf->entity.")";
  548. $sql .= " AND m.menu_handler IN ('".$menu_handler."','all')";
  549. if ($type_user == 0) $sql .= " AND m.usertype IN (0,2)";
  550. if ($type_user == 1) $sql .= " AND m.usertype IN (1,2)";
  551. $sql .= " ORDER BY m.position, m.rowid";
  552. //print $sql;
  553. //dol_syslog(get_class($this)."::menuLoad mymainmenu=".$mymainmenu." myleftmenu=".$myleftmenu." type_user=".$type_user." menu_handler=".$menu_handler." tabMenu size=".count($tabMenu)."", LOG_DEBUG);
  554. $resql = $this->db->query($sql);
  555. if ($resql)
  556. {
  557. $numa = $this->db->num_rows($resql);
  558. $a = 0;
  559. $b = 0;
  560. while ($a < $numa)
  561. {
  562. //$objm = $this->db->fetch_object($resql);
  563. $menu = $this->db->fetch_array($resql);
  564. // Define $right
  565. $perms = true;
  566. if (isset($menu['perms']))
  567. {
  568. $tmpcond = $menu['perms'];
  569. if ($leftmenu == 'all') $tmpcond = preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/', '1==1', $tmpcond); // Force part of condition to true
  570. $perms = verifCond($tmpcond);
  571. //print "verifCond rowid=".$menu['rowid']." ".$tmpcond.":".$perms."<br>\n";
  572. }
  573. // Define $enabled
  574. $enabled = true;
  575. if (isset($menu['enabled']))
  576. {
  577. $tmpcond = $menu['enabled'];
  578. if ($leftmenu == 'all') $tmpcond = preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/', '1==1', $tmpcond); // Force part of condition to true
  579. $enabled = verifCond($tmpcond);
  580. }
  581. // Define $title
  582. if ($enabled)
  583. {
  584. $title = $langs->trans($menu['titre']); // If $menu['titre'] start with $, a dol_eval is done.
  585. //var_dump($title.'-'.$menu['titre']);
  586. if ($title == $menu['titre']) // Translation not found
  587. {
  588. if (!empty($menu['langs'])) // If there is a dedicated translation file
  589. {
  590. //print 'Load file '.$menu['langs'].'<br>';
  591. $langs->load($menu['langs']);
  592. }
  593. $substitarray = array('__LOGIN__' => $user->login, '__USER_ID__' => $user->id, '__USER_SUPERVISOR_ID__' => $user->fk_user);
  594. $menu['titre'] = make_substitutions($menu['titre'], $substitarray);
  595. if (preg_match("/\//", $menu['titre'])) // To manage translation when title is string1/string2
  596. {
  597. $tab_titre = explode("/", $menu['titre']);
  598. $title = $langs->trans($tab_titre[0])."/".$langs->trans($tab_titre[1]);
  599. } elseif (preg_match('/\|\|/', $menu['titre']))
  600. {
  601. // To manage different translation (Title||AltTitle@ConditionForAltTitle)
  602. $tab_title = explode("||", $menu['titre']);
  603. $alt_title = explode("@", $tab_title[1]);
  604. $title_enabled = verifCond($alt_title[1]);
  605. $title = ($title_enabled ? $langs->trans($alt_title[0]) : $langs->trans($tab_title[0]));
  606. } else {
  607. $title = $langs->trans($menu['titre']);
  608. }
  609. }
  610. //$tmp4=microtime(true);
  611. //print '>>> 3 '.($tmp4 - $tmp3).'<br>';
  612. // We complete tabMenu
  613. $tabMenu[$b]['rowid'] = $menu['rowid'];
  614. $tabMenu[$b]['module'] = $menu['module'];
  615. $tabMenu[$b]['fk_menu'] = $menu['fk_menu'];
  616. $tabMenu[$b]['url'] = $menu['url'];
  617. if (!preg_match("/^(http:\/\/|https:\/\/)/i", $tabMenu[$b]['url']))
  618. {
  619. if (preg_match('/\?/', $tabMenu[$b]['url'])) $tabMenu[$b]['url'] .= '&amp;idmenu='.$menu['rowid'];
  620. else $tabMenu[$b]['url'] .= '?idmenu='.$menu['rowid'];
  621. }
  622. $tabMenu[$b]['titre'] = $title;
  623. $tabMenu[$b]['target'] = $menu['target'];
  624. $tabMenu[$b]['mainmenu'] = $menu['mainmenu'];
  625. $tabMenu[$b]['leftmenu'] = $menu['leftmenu'];
  626. $tabMenu[$b]['perms'] = $perms;
  627. $tabMenu[$b]['langs'] = $menu['langs']; // Note that this should not be used, lang file should be already loaded.
  628. $tabMenu[$b]['enabled'] = $enabled;
  629. $tabMenu[$b]['type'] = $menu['type'];
  630. $tabMenu[$b]['fk_mainmenu'] = $menu['fk_mainmenu'];
  631. $tabMenu[$b]['fk_leftmenu'] = $menu['fk_leftmenu'];
  632. $tabMenu[$b]['position'] = (int) $menu['position'];
  633. $b++;
  634. }
  635. $a++;
  636. }
  637. $this->db->free($resql);
  638. // Currently $tabMenu is sorted on position.
  639. // If a child have a position lower that its parent, we can make a loop to fix this here, but we prefer to show a warning
  640. // into the leftMenuCharger later to avoid useless operations.
  641. return 1;
  642. } else {
  643. dol_print_error($this->db);
  644. return -1;
  645. }
  646. }
  647. /**
  648. * Complete this->newmenu with menu entry found in $tab
  649. *
  650. * @param array $tab Tab array with all menu entries
  651. * @param int $pere Id of parent
  652. * @param int $level Level
  653. * @return void
  654. */
  655. private function recur($tab, $pere, $level)
  656. {
  657. // Loop on tab array
  658. $num = count($tab);
  659. for ($x = 0; $x < $num; $x++)
  660. {
  661. //si un element a pour pere : $pere
  662. if ((($tab[$x]['fk_menu'] >= 0 && $tab[$x]['fk_menu'] == $pere)) && $tab[$x]['enabled'])
  663. {
  664. $this->newmenu->add($tab[$x]['url'], $tab[$x]['titre'], ($level - 1), $tab[$x]['perms'], $tab[$x]['target'], $tab[$x]['mainmenu'], $tab[$x]['leftmenu']);
  665. $this->recur($tab, $tab[$x]['rowid'], ($level + 1));
  666. }
  667. }
  668. }
  669. }