PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/menubase.class.php

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