menubase.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. public $db; // To store db handler
  29. public $error; // To return error code (or message)
  30. public $errors=array(); // To return several error codes (or messages)
  31. public $id;
  32. public $menu_handler;
  33. public $module;
  34. public $type;
  35. public $mainmenu;
  36. public $fk_menu;
  37. public $fk_mainmenu;
  38. public $fk_leftmenu;
  39. public $position;
  40. public $url;
  41. public $target;
  42. public $titre;
  43. public $langs;
  44. public $level;
  45. public $leftmenu; //<! Not used
  46. public $perms;
  47. public $enabled;
  48. public $user;
  49. public $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->db->escape($this->type)."',";
  199. $sql.= " mainmenu='".$this->db->escape($this->mainmenu)."',";
  200. $sql.= " leftmenu='".$this->db->escape($this->leftmenu)."',";
  201. $sql.= " fk_menu='".$this->db->escape($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 > 0 ? $this->position : 0).",";
  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->db->escape($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.fk_mainmenu,";
  242. $sql.= " t.fk_leftmenu,";
  243. $sql.= " t.position,";
  244. $sql.= " t.url,";
  245. $sql.= " t.target,";
  246. $sql.= " t.titre,";
  247. $sql.= " t.langs,";
  248. $sql.= " t.perms,";
  249. $sql.= " t.enabled,";
  250. $sql.= " t.usertype as user,";
  251. $sql.= " t.tms";
  252. $sql.= " FROM ".MAIN_DB_PREFIX."menu as t";
  253. $sql.= " WHERE t.rowid = ".$id;
  254. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  255. $resql=$this->db->query($sql);
  256. if ($resql)
  257. {
  258. if ($this->db->num_rows($resql))
  259. {
  260. $obj = $this->db->fetch_object($resql);
  261. $this->id = $obj->rowid;
  262. $this->menu_handler = $obj->menu_handler;
  263. $this->entity = $obj->entity;
  264. $this->module = $obj->module;
  265. $this->type = $obj->type;
  266. $this->mainmenu = $obj->mainmenu;
  267. $this->leftmenu = $obj->leftmenu;
  268. $this->fk_menu = $obj->fk_menu;
  269. $this->fk_mainmenu = $obj->fk_mainmenu;
  270. $this->fk_leftmenu = $obj->fk_leftmenu;
  271. $this->position = $obj->position;
  272. $this->url = $obj->url;
  273. $this->target = $obj->target;
  274. $this->titre = $obj->titre;
  275. $this->langs = $obj->langs;
  276. $this->perms = $obj->perms;
  277. $this->enabled = str_replace("\"","'",$obj->enabled);
  278. $this->user = $obj->user;
  279. $this->tms = $this->db->jdate($obj->tms);
  280. }
  281. $this->db->free($resql);
  282. return 1;
  283. }
  284. else
  285. {
  286. $this->error="Error ".$this->db->lasterror();
  287. return -1;
  288. }
  289. }
  290. /**
  291. * Delete object in database
  292. *
  293. * @param User $user User that delete
  294. * @return int <0 if KO, >0 if OK
  295. */
  296. function delete($user)
  297. {
  298. global $conf, $langs;
  299. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  300. $sql.= " WHERE rowid=".$this->id;
  301. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  302. $resql = $this->db->query($sql);
  303. if (! $resql)
  304. {
  305. $this->error="Error ".$this->db->lasterror();
  306. return -1;
  307. }
  308. return 1;
  309. }
  310. /**
  311. * Initialise an instance with random values.
  312. * Used to build previews or test instances.
  313. * id must be 0 if object instance is a specimen.
  314. *
  315. * @return void
  316. */
  317. function initAsSpecimen()
  318. {
  319. $this->id=0;
  320. $this->menu_handler='all';
  321. $this->module='specimen';
  322. $this->type='top';
  323. $this->mainmenu='';
  324. $this->fk_menu='0';
  325. $this->position='';
  326. $this->url='http://dummy';
  327. $this->target='';
  328. $this->titre='Specimen menu';
  329. $this->langs='';
  330. $this->level='';
  331. $this->leftmenu='';
  332. $this->perms='';
  333. $this->enabled='';
  334. $this->user='';
  335. $this->tms='';
  336. }
  337. /**
  338. * Load tabMenu array with top menu entries found into database.
  339. *
  340. * @param string $mymainmenu Value for mainmenu to filter menu to load (always '')
  341. * @param string $myleftmenu Value for leftmenu to filter menu to load (always '')
  342. * @param int $type_user 0=Menu for backoffice, 1=Menu for front office
  343. * @param string $menu_handler Filter on name of menu_handler used (auguria, eldy...)
  344. * @param array $tabMenu If array with menu entries already loaded, we put this array here (in most cases, it's empty)
  345. * @return array Return array with menu entries for top menu
  346. */
  347. function menuTopCharger($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
  348. {
  349. global $langs, $user, $conf; // To export to dol_eval function
  350. global $mainmenu,$leftmenu; // To export to dol_eval function
  351. $mainmenu=$mymainmenu; // To export to dol_eval function
  352. $leftmenu=$myleftmenu; // To export to dol_eval function
  353. $newTabMenu=array();
  354. foreach($tabMenu as $val)
  355. {
  356. if ($val['type']=='top') $newTabMenu[]=$val;
  357. }
  358. return $newTabMenu;
  359. }
  360. /**
  361. * Load entries found from database in this->newmenu array.
  362. *
  363. * @param Menu $newmenu Menu array to complete (in most cases, it's empty, may be already initialized with some menu manager like eldy)
  364. * @param string $mymainmenu Value for mainmenu to filter menu to load (often $_SESSION["mainmenu"])
  365. * @param string $myleftmenu Value for leftmenu to filter menu to load (always '')
  366. * @param int $type_user 0=Menu for backoffice, 1=Menu for front office
  367. * @param string $menu_handler Filter on name of menu_handler used (auguria, eldy...)
  368. * @param array $tabMenu Array with menu entries already loaded
  369. * @return Menu Menu array for particular mainmenu value or full tabArray
  370. */
  371. function menuLeftCharger($newmenu, $mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
  372. {
  373. global $langs, $user, $conf; // To export to dol_eval function
  374. global $mainmenu,$leftmenu; // To export to dol_eval function
  375. $mainmenu=$mymainmenu; // To export to dol_eval function
  376. $leftmenu=$myleftmenu; // To export to dol_eval function
  377. // Detect what is top mainmenu id
  378. $menutopid='';
  379. foreach($tabMenu as $key => $val)
  380. {
  381. // Define menutopid of mainmenu
  382. if (empty($menutopid) && $val['type'] == 'top' && $val['mainmenu'] == $mainmenu)
  383. {
  384. $menutopid=$val['rowid'];
  385. break;
  386. }
  387. }
  388. // We initialize newmenu with first already found menu entries
  389. $this->newmenu = $newmenu;
  390. // Now edit this->newmenu->list to add entries found into tabMenu that are childs of mainmenu claimed, using the fk_menu link (old method)
  391. $this->recur($tabMenu, $menutopid, 1);
  392. // Now update this->newmenu->list when fk_menu value is -1 (left menu added by modules with no top menu)
  393. foreach($tabMenu as $key => $val)
  394. {
  395. //var_dump($tabMenu);
  396. if ($val['fk_menu'] == -1 && $val['fk_mainmenu'] == $mainmenu) // We found a menu entry not linked to parent with good mainmenu
  397. {
  398. //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>';
  399. //var_dump($this->newmenu->liste);exit;
  400. if (empty($val['fk_leftmenu']))
  401. {
  402. $this->newmenu->add($val['url'], $val['titre'], 0, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu'], $val['position']);
  403. //var_dump($this->newmenu->liste);
  404. }
  405. else
  406. {
  407. // Search first menu with this couple (mainmenu,leftmenu)=(fk_mainmenu,fk_leftmenu)
  408. $searchlastsub=0;$lastid=0;$nextid=0;$found=0;
  409. foreach($this->newmenu->liste as $keyparent => $valparent)
  410. {
  411. //var_dump($valparent);
  412. if ($searchlastsub) // If we started to search for last submenu
  413. {
  414. if ($valparent['level'] >= $searchlastsub) $lastid=$keyparent;
  415. if ($valparent['level'] < $searchlastsub)
  416. {
  417. $nextid=$keyparent;
  418. break;
  419. }
  420. }
  421. if ($valparent['mainmenu'] == $val['fk_mainmenu'] && $valparent['leftmenu'] == $val['fk_leftmenu'])
  422. {
  423. //print "We found parent: keyparent='.$keyparent.' - level=".$valparent['level'].' - '.join(',',$valparent).'<br>';
  424. // Now we look to find last subelement of this parent (we add at end)
  425. $searchlastsub=($valparent['level']+1);
  426. $lastid=$keyparent;
  427. $found=1;
  428. }
  429. }
  430. //print 'We must insert menu entry between entry '.$lastid.' and '.$nextid.'<br>';
  431. if ($found) $this->newmenu->insert($lastid, $val['url'], $val['titre'], $searchlastsub, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu'], $val['position']);
  432. }
  433. }
  434. }
  435. return $this->newmenu;
  436. }
  437. /**
  438. * Load entries found in database into variable $tabMenu. Note that only "database menu entries" are loaded here, hardcoded will not be present into output.
  439. *
  440. * @param string $mymainmenu Value for mainmenu that defined mainmenu
  441. * @param string $myleftmenu Value for left that defined leftmenu
  442. * @param int $type_user Looks for menu entry for 0=Internal users, 1=External users
  443. * @param string $menu_handler Name of menu_handler used ('auguria', 'eldy'...)
  444. * @param array $tabMenu Array to store new entries found (in most cases, it's empty, but may be alreay filled)
  445. * @return int >0 if OK, <0 if KO
  446. */
  447. function menuLoad($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
  448. {
  449. global $langs, $user, $conf; // To export to dol_eval function
  450. global $mainmenu, $leftmenu; // To export to dol_eval function
  451. $menutopid=0;
  452. $mainmenu=$mymainmenu; // To export to dol_eval function
  453. $leftmenu=$myleftmenu; // To export to dol_eval function
  454. $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";
  455. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  456. $sql.= " WHERE m.entity IN (0,".(! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode)?"1,":"").$conf->entity.")";
  457. $sql.= " AND m.menu_handler IN ('".$menu_handler."','all')";
  458. if ($type_user == 0) $sql.= " AND m.usertype IN (0,2)";
  459. if ($type_user == 1) $sql.= " AND m.usertype IN (1,2)";
  460. $sql.= " ORDER BY m.position, m.rowid";
  461. //print $sql;
  462. //$tmp1=microtime(true);
  463. //print '>>> 1 0<br>';
  464. dol_syslog(get_class($this)."::menuLoad mymainmenu=".$mymainmenu." myleftmenu=".$myleftmenu." type_user=".$type_user." menu_handler=".$menu_handler." tabMenu size=".count($tabMenu)."", LOG_DEBUG);
  465. $resql = $this->db->query($sql);
  466. if ($resql)
  467. {
  468. $numa = $this->db->num_rows($resql);
  469. $a = 0;
  470. $b = 0;
  471. $oldrowid=0;
  472. while ($a < $numa)
  473. {
  474. //$objm = $this->db->fetch_object($resql);
  475. $menu = $this->db->fetch_array($resql);
  476. // Define $right
  477. $perms = true;
  478. if ($menu['perms'])
  479. {
  480. $tmpcond=$menu['perms'];
  481. if ($leftmenu == 'all') $tmpcond=preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/','1==1',$tmpcond); // Force part of condition to true
  482. $perms = verifCond($tmpcond);
  483. //print "verifCond rowid=".$menu['rowid']." ".$tmpcond.":".$perms."<br>\n";
  484. }
  485. // Define $enabled
  486. $enabled = true;
  487. if ($menu['enabled'])
  488. {
  489. $tmpcond=$menu['enabled'];
  490. if ($leftmenu == 'all') $tmpcond=preg_replace('/\$leftmenu\s*==\s*["\'a-zA-Z_]+/','1==1',$tmpcond); // Force part of condition to true
  491. $enabled = verifCond($tmpcond);
  492. }
  493. // Define $title
  494. if ($enabled)
  495. {
  496. $title = $langs->trans($menu['titre']);
  497. if ($title == $menu['titre']) // Translation not found
  498. {
  499. if (! empty($menu['langs'])) // If there is a dedicated translation file
  500. {
  501. //print 'Load file '.$menu['langs'].'<br>';
  502. $langs->load($menu['langs']);
  503. }
  504. if (preg_match("/\//",$menu['titre'])) // To manage translation when title is string1/string2
  505. {
  506. $tab_titre = explode("/",$menu['titre']);
  507. $title = $langs->trans($tab_titre[0])."/".$langs->trans($tab_titre[1]);
  508. }
  509. else if (preg_match('/\|\|/',$menu['titre'])) // To manage different translation (Title||AltTitle@ConditionForAltTitle)
  510. {
  511. $tab_title = explode("||",$menu['titre']);
  512. $alt_title = explode("@",$tab_title[1]);
  513. $title_enabled = verifCond($alt_title[1]);
  514. $title = ($title_enabled ? $langs->trans($alt_title[0]) : $langs->trans($tab_title[0]));
  515. }
  516. else
  517. {
  518. $title = $langs->trans($menu['titre']);
  519. }
  520. }
  521. //$tmp4=microtime(true);
  522. //print '>>> 3 '.($tmp4 - $tmp3).'<br>';
  523. // We complete tabMenu
  524. $tabMenu[$b]['rowid'] = $menu['rowid'];
  525. $tabMenu[$b]['module'] = $menu['module'];
  526. $tabMenu[$b]['fk_menu'] = $menu['fk_menu'];
  527. $tabMenu[$b]['url'] = $menu['url'];
  528. if (! preg_match("/^(http:\/\/|https:\/\/)/i",$tabMenu[$b]['url']))
  529. {
  530. if (preg_match('/\?/',$tabMenu[$b]['url'])) $tabMenu[$b]['url'].='&amp;idmenu='.$menu['rowid'];
  531. else $tabMenu[$b]['url'].='?idmenu='.$menu['rowid'];
  532. }
  533. $tabMenu[$b]['titre'] = $title;
  534. $tabMenu[$b]['target'] = $menu['target'];
  535. $tabMenu[$b]['mainmenu'] = $menu['mainmenu'];
  536. $tabMenu[$b]['leftmenu'] = $menu['leftmenu'];
  537. $tabMenu[$b]['perms'] = $perms;
  538. $tabMenu[$b]['enabled'] = $enabled;
  539. $tabMenu[$b]['type'] = $menu['type'];
  540. //$tabMenu[$b]['langs'] = $menu['langs'];
  541. $tabMenu[$b]['fk_mainmenu'] = $menu['fk_mainmenu'];
  542. $tabMenu[$b]['fk_leftmenu'] = $menu['fk_leftmenu'];
  543. $tabMenu[$b]['position'] = $menu['position'];
  544. $b++;
  545. }
  546. $a++;
  547. }
  548. $this->db->free($resql);
  549. return 1;
  550. }
  551. else
  552. {
  553. dol_print_error($this->db);
  554. return -1;
  555. }
  556. }
  557. /**
  558. * Complete this->newmenu with menu entry found in $tab
  559. *
  560. * @param array $tab Tab array
  561. * @param int $pere Id of parent
  562. * @param int $level Level
  563. * @return void
  564. */
  565. private function recur($tab, $pere, $level)
  566. {
  567. // Loop on tab array
  568. $num = count($tab);
  569. for ($x = 0; $x < $num; $x++)
  570. {
  571. //si un element a pour pere : $pere
  572. if ( (($tab[$x]['fk_menu'] >= 0 && $tab[$x]['fk_menu'] == $pere)) && $tab[$x]['enabled'])
  573. {
  574. $this->newmenu->add($tab[$x]['url'], $tab[$x]['titre'], ($level-1), $tab[$x]['perms'], $tab[$x]['target'], $tab[$x]['mainmenu'], $tab[$x]['leftmenu']);
  575. $this->recur($tab, $tab[$x]['rowid'], ($level+1));
  576. }
  577. }
  578. }
  579. }