menubase.class.php 27 KB

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