menubase.class.php 25 KB

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