menubase.class.php 25 KB

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