PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/asterix14/dolibarr
PHP | 621 lines | 529 code | 27 blank | 65 comment | 5 complexity | bbd99f6e6bf0916ab49890743f7618ec MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2007-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009-2010 Regis Houssin <regis@dolibarr.fr>
  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 2 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 Menubase
  25. * \brief Class to manage menu entries
  26. */
  27. class Menubase
  28. {
  29. var $db; // To store db handler
  30. var $error; // To return error code (or message)
  31. var $errors=array(); // To return several error codes (or messages)
  32. var $id;
  33. var $menu_handler;
  34. var $module;
  35. var $type;
  36. var $mainmenu;
  37. var $fk_menu;
  38. var $fk_mainmenu;
  39. var $fk_leftmenu;
  40. var $position;
  41. var $url;
  42. var $target;
  43. var $titre;
  44. var $langs;
  45. var $level;
  46. var $leftmenu; //<! 0=Left menu in pre.inc.php files must not be overwrite by database menu, 1=Must be
  47. var $perms;
  48. var $enabled;
  49. var $user;
  50. var $tms;
  51. /**
  52. * Constructor
  53. *
  54. * @param DoliDB $DB Database handler
  55. * @param string $menu_handler Menu handler
  56. * @param string $type Type
  57. */
  58. function Menubase($DB,$menu_handler='',$type='')
  59. {
  60. $this->db = $DB;
  61. $this->menu_handler = $menu_handler;
  62. $this->type = $type;
  63. return 1;
  64. }
  65. /**
  66. * Create menu entry into database
  67. *
  68. * @param User $user User that create
  69. * @return int <0 if KO, Id of record if OK
  70. */
  71. function create($user=0)
  72. {
  73. global $conf, $langs;
  74. // Clean parameters
  75. $this->menu_handler=trim($this->menu_handler);
  76. $this->module=trim($this->module);
  77. $this->type=trim($this->type);
  78. $this->mainmenu=trim($this->mainmenu);
  79. $this->leftmenu=trim($this->leftmenu);
  80. $this->fk_menu=trim($this->fk_menu);
  81. $this->fk_mainmenu=trim($this->fk_mainmenu);
  82. $this->fk_leftmenu=trim($this->fk_leftmenu);
  83. $this->position=trim($this->position);
  84. $this->url=trim($this->url);
  85. $this->target=trim($this->target);
  86. $this->titre=trim($this->titre);
  87. $this->langs=trim($this->langs);
  88. $this->perms=trim($this->perms);
  89. $this->enabled=trim($this->enabled);
  90. $this->user=trim($this->user);
  91. if (! $this->level) $this->level=0;
  92. // Check parameters
  93. // Put here code to add control on parameters values
  94. // FIXME
  95. // Get the max rowid in llx_menu and use it as rowid in insert because postgresql
  96. // may use an already used value because its internal cursor does not increase when we do
  97. // an insert with a forced id.
  98. // Two solution: Disable menu handler using database when database is postgresql or update counter when
  99. // enabling such menus.
  100. // Insert request
  101. $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu(";
  102. $sql.= "menu_handler,";
  103. $sql.= "entity,";
  104. $sql.= "module,";
  105. $sql.= "type,";
  106. $sql.= "mainmenu,";
  107. $sql.= "leftmenu,";
  108. $sql.= "fk_menu,";
  109. $sql.= "fk_mainmenu,";
  110. $sql.= "fk_leftmenu,";
  111. $sql.= "position,";
  112. $sql.= "url,";
  113. $sql.= "target,";
  114. $sql.= "titre,";
  115. $sql.= "langs,";
  116. $sql.= "perms,";
  117. $sql.= "enabled,";
  118. $sql.= "usertype";
  119. $sql.= ") VALUES (";
  120. $sql.= " '".$this->menu_handler."',";
  121. $sql.= " '".$conf->entity."',";
  122. $sql.= " '".$this->module."',";
  123. $sql.= " '".$this->type."',";
  124. $sql.= " '".$this->mainmenu."',";
  125. $sql.= " '".$this->leftmenu."',";
  126. $sql.= " '".$this->fk_menu."',";
  127. $sql.= " ".($this->fk_mainmenu?"'".$this->fk_mainmenu."'":"null").",";
  128. $sql.= " ".($this->fk_leftmenu?"'".$this->fk_leftmenu."'":"null").",";
  129. $sql.= " '".$this->position."',";
  130. $sql.= " '".$this->url."',";
  131. $sql.= " '".$this->target."',";
  132. $sql.= " '".$this->titre."',";
  133. $sql.= " '".$this->langs."',";
  134. $sql.= " '".$this->perms."',";
  135. $sql.= " '".$this->enabled."',";
  136. $sql.= " '".$this->user."'";
  137. $sql.= ")";
  138. dol_syslog("Menubase::create sql=".$sql, LOG_DEBUG);
  139. $resql=$this->db->query($sql);
  140. if ($resql)
  141. {
  142. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."menu");
  143. dol_syslog("Menubase::create record added has rowid=".$this->id, LOG_DEBUG);
  144. return $this->id;
  145. }
  146. else
  147. {
  148. $this->error="Error ".$this->db->lasterror();
  149. dol_syslog("Menubase::create ".$this->error, LOG_ERR);
  150. return -1;
  151. }
  152. }
  153. /**
  154. * Update menu entry into database.
  155. *
  156. * @param User $user User that modify
  157. * @param int $notrigger 0=no, 1=yes (no update trigger)
  158. * @return int <0 if KO, >0 if OK
  159. */
  160. function update($user=0, $notrigger=0)
  161. {
  162. global $conf, $langs;
  163. // Clean parameters
  164. $this->rowid=trim($this->rowid);
  165. $this->menu_handler=trim($this->menu_handler);
  166. $this->module=trim($this->module);
  167. $this->type=trim($this->type);
  168. $this->mainmenu=trim($this->mainmenu);
  169. $this->leftmenu=trim($this->leftmenu);
  170. $this->fk_menu=trim($this->fk_menu);
  171. $this->fk_mainmenu=trim($this->fk_mainmenu);
  172. $this->fk_leftmenu=trim($this->fk_leftmenu);
  173. $this->position=trim($this->position);
  174. $this->url=trim($this->url);
  175. $this->target=trim($this->target);
  176. $this->titre=trim($this->titre);
  177. $this->langs=trim($this->langs);
  178. $this->perms=trim($this->perms);
  179. $this->enabled=trim($this->enabled);
  180. $this->user=trim($this->user);
  181. // Check parameters
  182. // Put here code to add control on parameters values
  183. // Update request
  184. $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
  185. $sql.= " menu_handler='".$this->db->escape($this->menu_handler)."',";
  186. $sql.= " module='".$this->db->escape($this->module)."',";
  187. $sql.= " type='".$this->type."',";
  188. $sql.= " mainmenu='".$this->db->escape($this->mainmenu)."',";
  189. $sql.= " leftmenu='".$this->db->escape($this->leftmenu)."',";
  190. $sql.= " fk_menu='".$this->fk_menu."',";
  191. $sql.= " fk_mainmenu=".($this->fk_mainmenu?"'".$this->fk_mainmenu."'":"null").",";
  192. $sql.= " fk_leftmenu=".($this->fk_leftmenu?"'".$this->fk_leftmenu."'":"null").",";
  193. $sql.= " position='".$this->position."',";
  194. $sql.= " url='".$this->db->escape($this->url)."',";
  195. $sql.= " target='".$this->db->escape($this->target)."',";
  196. $sql.= " titre='".$this->db->escape($this->titre)."',";
  197. $sql.= " langs='".$this->db->escape($this->langs)."',";
  198. $sql.= " perms='".$this->db->escape($this->perms)."',";
  199. $sql.= " enabled='".$this->db->escape($this->enabled)."',";
  200. $sql.= " usertype='".$this->user."'";
  201. $sql.= " WHERE rowid=".$this->id;
  202. dol_syslog("Menubase::update sql=".$sql, LOG_DEBUG);
  203. $resql = $this->db->query($sql);
  204. if (! $resql)
  205. {
  206. $this->error="Error ".$this->db->lasterror();
  207. dol_syslog("Menubase::update ".$this->error, LOG_ERR);
  208. return -1;
  209. }
  210. return 1;
  211. }
  212. /*
  213. * \brief Load object in memory from database
  214. * \param id id object
  215. * \param user User that load
  216. * \return int <0 if KO, >0 if OK
  217. */
  218. function fetch($id, $user=0)
  219. {
  220. global $langs;
  221. $sql = "SELECT";
  222. $sql.= " t.rowid,";
  223. $sql.= " t.menu_handler,";
  224. $sql.= " t.entity,";
  225. $sql.= " t.module,";
  226. $sql.= " t.type,";
  227. $sql.= " t.mainmenu,";
  228. $sql.= " t.leftmenu,";
  229. $sql.= " t.fk_menu,";
  230. $sql.= " t.position,";
  231. $sql.= " t.url,";
  232. $sql.= " t.target,";
  233. $sql.= " t.titre,";
  234. $sql.= " t.langs,";
  235. $sql.= " t.perms,";
  236. $sql.= " t.enabled,";
  237. $sql.= " t.usertype as user,";
  238. $sql.= " t.tms";
  239. $sql.= " FROM ".MAIN_DB_PREFIX."menu as t";
  240. $sql.= " WHERE t.rowid = ".$id;
  241. dol_syslog("Menubase::fetch sql=".$sql, LOG_DEBUG);
  242. $resql=$this->db->query($sql);
  243. if ($resql)
  244. {
  245. if ($this->db->num_rows($resql))
  246. {
  247. $obj = $this->db->fetch_object($resql);
  248. $this->id = $obj->rowid;
  249. $this->menu_handler = $obj->menu_handler;
  250. $this->entity = $obj->entity;
  251. $this->module = $obj->module;
  252. $this->type = $obj->type;
  253. $this->mainmenu = $obj->mainmenu;
  254. $this->leftmenu = $obj->leftmenu;
  255. $this->fk_menu = $obj->fk_menu;
  256. $this->position = $obj->position;
  257. $this->url = $obj->url;
  258. $this->target = $obj->target;
  259. $this->titre = $obj->titre;
  260. $this->langs = $obj->langs;
  261. $this->perms = $obj->perms;
  262. $this->enabled = str_replace("\"","'",$obj->enabled);
  263. $this->user = $obj->user;
  264. $this->tms = $this->db->jdate($obj->tms);
  265. }
  266. $this->db->free($resql);
  267. return 1;
  268. }
  269. else
  270. {
  271. $this->error="Error ".$this->db->lasterror();
  272. dol_syslog("Menubase::fetch ".$this->error, LOG_ERR);
  273. return -1;
  274. }
  275. }
  276. /**
  277. * Delete object in database
  278. *
  279. * @param User $user User that delete
  280. * @return int <0 if KO, >0 if OK
  281. */
  282. function delete($user)
  283. {
  284. global $conf, $langs;
  285. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  286. $sql.= " WHERE rowid=".$this->id;
  287. dol_syslog("Menubase::delete sql=".$sql);
  288. $resql = $this->db->query($sql);
  289. if (! $resql)
  290. {
  291. $this->error="Error ".$this->db->lasterror();
  292. dol_syslog("Menubase::delete ".$this->error, LOG_ERR);
  293. return -1;
  294. }
  295. return 1;
  296. }
  297. /**
  298. * Initialise an instance with random values.
  299. * Used to build previews or test instances.
  300. * id must be 0 if object instance is a specimen.
  301. *
  302. * @return void
  303. */
  304. function initAsSpecimen()
  305. {
  306. $this->id=0;
  307. $this->menu_handler='all';
  308. $this->module='specimen';
  309. $this->type='top';
  310. $this->mainmenu='';
  311. $this->fk_menu='0';
  312. $this->position='';
  313. $this->url='http://dummy';
  314. $this->target='';
  315. $this->titre='Specimen menu';
  316. $this->langs='';
  317. $this->level='';
  318. $this->leftmenu='';
  319. $this->perms='';
  320. $this->enabled='';
  321. $this->user='';
  322. $this->tms='';
  323. }
  324. /**
  325. * Complete this->newmenu with menu entry found in $tab
  326. *
  327. * @param array $tab Tab array
  328. * @param int $pere Id of parent
  329. * @param int $rang Rang
  330. * @param string $myleftmenu Value for left that defined leftmenu
  331. * @return void
  332. */
  333. function recur($tab, $pere, $rang, $myleftmenu)
  334. {
  335. global $leftmenu; // To be exported in dol_eval function
  336. //print "xx".$pere;
  337. $leftmenu = $myleftmenu;
  338. //ballayage du tableau
  339. $num = count($tab);
  340. for ($x = 0; $x < $num; $x++)
  341. {
  342. //si un element a pour pere : $pere
  343. if ($tab[$x][1] == $pere)
  344. {
  345. if ($tab[$x][7])
  346. {
  347. $leftmenuConstraint = true;
  348. if ($tab[$x][6])
  349. {
  350. $leftmenuConstraint = verifCond($tab[$x][6]);
  351. }
  352. if ($leftmenuConstraint)
  353. {
  354. //print 'name='.$tab[$x][3].' pere='.$pere." ".$tab[$x][6];
  355. $this->newmenu->add((! preg_match("/^(http:\/\/|https:\/\/)/i",$tab[$x][2])) ? $tab[$x][2] : $tab[$x][2], $tab[$x][3], $rang -1, $tab[$x][4], $tab[$x][5], $tab[$x][8]);
  356. $this->recur($tab, $tab[$x][0], $rang +1, $lelfmenu);
  357. }
  358. }
  359. }
  360. }
  361. }
  362. /**
  363. * Load tabMenu array
  364. *
  365. * @param string $mainmenu Value for mainmenu that defined top menu
  366. * @param string $myleftmenu Left menu name
  367. * @param int $type_user 0=Internal,1=External,2=All
  368. * @param string $menu_handler Name of menu_handler used (auguria, eldy...)
  369. * @param array &$tabMenu If array with menu entries already loaded, we put this array here (in most cases, it's empty)
  370. * @return array Return array with menu entries for top menu
  371. */
  372. function menuTopCharger($mainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu=null)
  373. {
  374. global $langs, $user, $conf;
  375. global $leftmenu,$rights; // To export to dol_eval function
  376. $leftmenu=$myleftmenu; // To export to dol_eval function
  377. // Load datas into tabMenu
  378. if (count($tabMenu) == 0)
  379. {
  380. $this->menuLoad($leftmenu, $type_user, $menu_handler, $tabMenu);
  381. }
  382. $newTabMenu=array();
  383. $i=0;
  384. if (is_array($tabMenu))
  385. {
  386. foreach($tabMenu as $val)
  387. {
  388. if ($val[9]=='top')
  389. {
  390. $newTabMenu[$i]['rowid']=$val[0];
  391. $newTabMenu[$i]['fk_menu']=$val[1];
  392. $newTabMenu[$i]['url']=$val[2];
  393. $newTabMenu[$i]['titre']=$val[3];
  394. $newTabMenu[$i]['right']=$val[4];
  395. $newTabMenu[$i]['atarget']=$val[5];
  396. $newTabMenu[$i]['leftmenu']=$val[6];
  397. $newTabMenu[$i]['enabled']=$val[7];
  398. $newTabMenu[$i]['mainmenu']=$val[8];
  399. $newTabMenu[$i]['type']=$val[9];
  400. $newTabMenu[$i]['lang']=$val[10];
  401. $i++;
  402. }
  403. }
  404. }
  405. return $newTabMenu;
  406. }
  407. /**
  408. * Load entries found in database in a menu array.
  409. *
  410. * @param array $newmenu Menu array to complete
  411. * @param string $mainmenu Value for mainmenu that defined top menu of left menu
  412. * @param string $myleftmenu Value that defined leftmenu
  413. * @param int $type_user 0=Internal,1=External,2=All
  414. * @param string $menu_handler Name of menu_handler used (auguria, eldy...)
  415. * @param array &$tabMenu If array with menu entries already loaded, we put this array here (in most cases, it's empty)
  416. * @return array Menu array for particular mainmenu value or full tabArray
  417. */
  418. function menuLeftCharger($newmenu, $mainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu=null)
  419. {
  420. global $langs, $user, $conf; // To export to dol_eval function
  421. global $leftmenu,$rights; // To export to dol_eval function
  422. $leftmenu=$myleftmenu; // To export to dol_eval function
  423. $this->newmenu = $newmenu;
  424. // Load datas into tabMenu
  425. if (count($tabMenu) == 0)
  426. {
  427. $this->menuLoad($leftmenu, $type_user, $menu_handler, $tabMenu);
  428. }
  429. //var_dump($tabMenu);
  430. // Define menutopid
  431. $menutopid='';
  432. if (is_array($tabMenu))
  433. {
  434. foreach($tabMenu as $val)
  435. {
  436. if ($val[9] == 'top' && $val[8] == $mainmenu)
  437. {
  438. $menutopid=$val[0];
  439. break;
  440. }
  441. }
  442. }
  443. // Now edit this->newmenu->list to add entries found into tabMenu that are in childs of mainmenu claimed
  444. $this->recur($tabMenu, $menutopid, 1, $leftmenu);
  445. return $this->newmenu;
  446. }
  447. /**
  448. * Load entries found in database in a menu array.
  449. *
  450. * @param string $myleftmenu Value for left that defined leftmenu
  451. * @param int $type_user 0=Internal,1=External,2=All
  452. * @param string $menu_handler Name of menu_handler used (auguria, eldy...)
  453. * @param array &$tabMenu If array with menu entries already load, we put this array here (in most cases, it's empty)
  454. * @return int >0 if OK, <0 if KO
  455. */
  456. function menuLoad($myleftmenu, $type_user, $menu_handler, &$tabMenu=array())
  457. {
  458. global $langs, $user, $conf; // To export to dol_eval function
  459. global $leftmenu, $rights; // To export to dol_eval function
  460. $menutopid=0;
  461. $leftmenu=$myleftmenu; // To export to dol_eval function
  462. $sql = "SELECT m.rowid, m.type, m.fk_menu, m.url, m.titre, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu";
  463. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  464. $sql.= " WHERE m.entity = ".$conf->entity;
  465. $sql.= " AND m.menu_handler in('".$menu_handler."','all')";
  466. if ($type_user == 0) $sql.= " AND m.usertype in (0,2)";
  467. if ($type_user == 1) $sql.= " AND m.usertype in (1,2)";
  468. // If type_user == 2, no test required
  469. $sql.= " ORDER BY m.position, m.rowid";
  470. dol_syslog("Menubase::menuLeftCharger sql=".$sql);
  471. $resql = $this->db->query($sql);
  472. if ($resql)
  473. {
  474. $numa = $this->db->num_rows($resql);
  475. $a = 0;
  476. $b = 0;
  477. $oldrowid=0;
  478. while ($a < $numa)
  479. {
  480. //$objm = $this->db->fetch_object($resql);
  481. $menu = $this->db->fetch_array($resql);
  482. // Define $chaine
  483. $chaine="";
  484. $title = $langs->trans($menu['titre']);
  485. if ($title == $menu['titre']) // Translation not found
  486. {
  487. if (! empty($menu['langs'])) // If there is a dedicated translation file
  488. {
  489. $langs->load($menu['langs']);
  490. }
  491. if (preg_match("/\//",$menu['titre'])) // To manage translation when title is string1/string2
  492. {
  493. $tab_titre = explode("/",$menu['titre']);
  494. $chaine = $langs->trans($tab_titre[0])."/".$langs->trans($tab_titre[1]);
  495. }
  496. else
  497. {
  498. $chaine = $langs->trans($menu['titre']);
  499. }
  500. }
  501. else
  502. {
  503. $chaine = $title;
  504. }
  505. // Define $right
  506. $perms = true;
  507. if ($menu['perms'])
  508. {
  509. $perms = verifCond($menu['perms']);
  510. //print "verifCond rowid=".$menu['rowid']." ".$menu['right'].":".$perms."<br>\n";
  511. }
  512. // Define $enabled
  513. $enabled = true;
  514. if ($menu['enabled'])
  515. {
  516. $enabled = verifCond($menu['enabled']);
  517. if ($conf->use_javascript_ajax && $conf->global->MAIN_MENU_USE_JQUERY_ACCORDION && preg_match('/^\$leftmenu/',$menu['enabled'])) $enabled=1;
  518. //print "verifCond chaine=".$chaine." rowid=".$menu['rowid']." ".$menu['enabled'].":".$enabled."<br>\n";
  519. }
  520. // 0=rowid, 1=fk_menu, 2=url, 3=text, 4=perms, 5=target, 8=mainmenu
  521. $tabMenu[$b][0] = $menu['rowid'];
  522. $tabMenu[$b][1] = $menu['fk_menu'];
  523. $tabMenu[$b][2] = $menu['url'];
  524. if (! preg_match("/^(http:\/\/|https:\/\/)/i",$tabMenu[$b][2]))
  525. {
  526. if (preg_match('/\?/',$tabMenu[$b][2])) $tabMenu[$b][2].='&amp;idmenu='.$menu['rowid'];
  527. else $tabMenu[$b][2].='?idmenu='.$menu['rowid'];
  528. }
  529. $tabMenu[$b][3] = $chaine;
  530. $tabMenu[$b][5] = $menu['target'];
  531. $tabMenu[$b][6] = $menu['leftmenu'];
  532. if (! isset($tabMenu[$b][4])) $tabMenu[$b][4] = $perms;
  533. else $tabMenu[$b][4] = ($tabMenu[$b][4] && $perms);
  534. if (! isset($tabMenu[$b][7])) $tabMenu[$b][7] = $enabled;
  535. else $tabMenu[$b][7] = ($tabMenu[$b][7] && $enabled);
  536. $tabMenu[$b][8] = $menu['mainmenu'];
  537. $tabMenu[$b][9] = $menu['type'];
  538. $tabMenu[$b][10] = $menu['langs'];
  539. $b++;
  540. $a++;
  541. }
  542. $this->db->free($resql);
  543. return 1;
  544. }
  545. else
  546. {
  547. dol_print_error($this->db);
  548. return -1;
  549. }
  550. }
  551. }
  552. ?>