menubase.class.php 24 KB

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