186 global $conf, $langs;
189 if (!isset($this->enabled)) {
190 $this->enabled =
'1';
192 $this->entity = (isset($this->entity) && (int) $this->entity >= 0 ? (
int) $this->entity : $conf->entity);
193 $this->menu_handler = trim((
string) $this->menu_handler);
194 $this->module = trim((
string) $this->module);
195 $this->
type = trim((
string) $this->
type);
196 $this->mainmenu = trim((
string) $this->mainmenu);
197 $this->leftmenu = trim((
string) $this->leftmenu);
198 $this->fk_menu = (int) $this->fk_menu;
199 $this->fk_mainmenu = trim((
string) $this->fk_mainmenu);
200 $this->fk_leftmenu = trim((
string) $this->fk_leftmenu);
202 $this->url = trim((
string) $this->url);
203 $this->target = trim((
string) $this->target);
204 $this->title = trim((
string) $this->title);
205 $this->langs = trim((
string) $this->langs);
206 $this->perms = trim((
string) $this->perms);
207 $this->enabled = trim((
string) $this->enabled);
217 if (empty($this->menu_handler)) {
224 if (in_array($this->db->type, array(
'pgsql'))) {
225 $sql =
"SELECT MAX(rowid) as maxrowid FROM ".$this->db->prefix().
"menu";
226 $resqlrowid = $this->db->query($sql);
228 $obj = $this->db->fetch_object($resqlrowid);
229 $maxrowid = $obj->maxrowid;
232 if (empty($maxrowid)) {
236 $sql =
"SELECT setval('".$this->db->prefix().
"menu_rowid_seq', ".($maxrowid).
")";
238 $resqlrowidset = $this->db->query($sql);
239 if (!$resqlrowidset) {
248 $sql =
"SELECT count(*)";
249 $sql .=
" FROM ".$this->db->prefix().
"menu";
250 $sql .=
" WHERE menu_handler = '".$this->db->escape($this->menu_handler).
"'";
251 $sql .=
" AND fk_menu = ".((int) $this->fk_menu);
252 $sql .=
" AND position = ".((int) $this->
position);
253 $sql .=
" AND url = '".$this->db->escape($this->url).
"'";
254 $sql .=
" AND entity IN (0, ".$conf->entity.
")";
256 $result = $this->db->query($sql);
258 $row = $this->db->fetch_row($result);
262 $sql =
"INSERT INTO ".$this->db->prefix().
"menu(";
263 $sql .=
"menu_handler,";
270 $sql .=
"fk_mainmenu,";
271 $sql .=
"fk_leftmenu,";
281 $sql .=
") VALUES (";
282 $sql .=
" '".$this->db->escape($this->menu_handler).
"',";
283 $sql .=
" '".$this->db->escape($this->entity).
"',";
284 $sql .=
" '".$this->db->escape($this->module).
"',";
285 $sql .=
" '".$this->db->escape($this->type).
"',";
286 $sql .=
" ".($this->mainmenu ?
"'".$this->db->escape($this->mainmenu).
"'" :
"''").
",";
287 $sql .=
" ".($this->leftmenu ?
"'".$this->db->escape($this->leftmenu).
"'" :
"null").
",";
288 $sql .=
" ".((int) $this->fk_menu).
",";
289 $sql .=
" ".($this->fk_mainmenu ?
"'".$this->db->escape($this->fk_mainmenu).
"'" :
"null").
",";
290 $sql .=
" ".($this->fk_leftmenu ?
"'".$this->db->escape($this->fk_leftmenu).
"'" :
"null").
",";
291 $sql .=
" ".((int) $this->
position).
",";
292 $sql .=
" '".$this->db->escape($this->url).
"',";
293 $sql .=
" '".$this->db->escape($this->target).
"',";
294 $sql .=
" '".$this->db->escape($this->title).
"',";
295 $sql .=
" '".$this->db->escape($this->prefix).
"',";
296 $sql .=
" '".$this->db->escape($this->langs).
"',";
297 $sql .=
" '".$this->db->escape($this->perms).
"',";
298 $sql .=
" '".$this->db->escape($this->enabled).
"',";
299 $sql .=
" '".$this->db->escape($this->
user).
"'";
302 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
303 $resql = $this->db->query($sql);
305 $this->
id = $this->db->last_insert_id($this->db->prefix().
"menu");
306 dol_syslog(get_class($this).
"::create record added has rowid=".((
int) $this->
id), LOG_DEBUG);
310 $this->error =
"Error ".$this->db->lasterror();
314 dol_syslog(get_class($this).
"::create menu entry already exists", LOG_WARNING);
315 $this->error =
'Error Menu entry ('.$this->menu_handler.
','.$this->
position.
','.$this->url.
') already exists';
330 public function update($user =
null, $notrigger = 0)
335 $this->menu_handler = trim($this->menu_handler);
336 $this->module = trim($this->module);
338 $this->mainmenu = trim($this->mainmenu);
339 $this->leftmenu = trim($this->leftmenu);
340 $this->fk_menu = (int) $this->fk_menu;
341 $this->fk_mainmenu = trim($this->fk_mainmenu);
342 $this->fk_leftmenu = trim($this->fk_leftmenu);
344 $this->url = trim($this->url);
345 $this->target = trim($this->target);
346 $this->title = trim($this->title);
347 $this->prefix = trim($this->prefix);
348 $this->langs = trim($this->langs);
349 $this->perms = trim($this->perms);
350 $this->enabled = trim($this->enabled);
357 $sql =
"UPDATE ".$this->db->prefix().
"menu SET";
358 $sql .=
" menu_handler='".$this->db->escape($this->menu_handler).
"',";
359 $sql .=
" module='".$this->db->escape($this->module).
"',";
360 $sql .=
" type='".$this->db->escape($this->
type).
"',";
361 $sql .=
" mainmenu='".$this->db->escape($this->mainmenu).
"',";
362 $sql .=
" leftmenu='".$this->db->escape($this->leftmenu).
"',";
363 $sql .=
" fk_menu=".((int) $this->fk_menu).
",";
364 $sql .=
" fk_mainmenu=".($this->fk_mainmenu ?
"'".$this->db->escape($this->fk_mainmenu).
"'" :
"null").
",";
365 $sql .=
" fk_leftmenu=".($this->fk_leftmenu ?
"'".$this->db->escape($this->fk_leftmenu).
"'" :
"null").
",";
366 $sql .=
" position=".($this->position > 0 ? ((int) $this->
position) : 0).
",";
367 $sql .=
" url='".$this->db->escape($this->url).
"',";
368 $sql .=
" target='".$this->db->escape($this->target).
"',";
369 $sql .=
" titre='".$this->db->escape($this->title).
"',";
370 $sql .=
" prefix='".$this->db->escape($this->prefix).
"',";
371 $sql .=
" langs='".$this->db->escape($this->langs).
"',";
372 $sql .=
" perms='".$this->db->escape($this->perms).
"',";
373 $sql .=
" enabled='".$this->db->escape($this->enabled).
"',";
374 $sql .=
" usertype='".$this->db->escape($this->
user).
"'";
375 $sql .=
" WHERE rowid=".((int) $this->
id);
377 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
378 $resql = $this->db->query($sql);
380 $this->error =
"Error ".$this->db->lasterror();
395 public function fetch($id, $user =
null)
401 $sql .=
" t.menu_handler,";
402 $sql .=
" t.entity,";
403 $sql .=
" t.module,";
405 $sql .=
" t.mainmenu,";
406 $sql .=
" t.leftmenu,";
407 $sql .=
" t.fk_menu,";
408 $sql .=
" t.fk_mainmenu,";
409 $sql .=
" t.fk_leftmenu,";
410 $sql .=
" t.position,";
412 $sql .=
" t.target,";
413 $sql .=
" t.titre as title,";
414 $sql .=
" t.prefix,";
417 $sql .=
" t.enabled,";
418 $sql .=
" t.usertype as user,";
420 $sql .=
" FROM ".$this->db->prefix().
"menu as t";
421 $sql .=
" WHERE t.rowid = ".((int) $id);
423 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
424 $resql = $this->db->query($sql);
426 if ($this->db->num_rows($resql)) {
427 $obj = $this->db->fetch_object($resql);
429 $this->
id = $obj->rowid;
431 $this->menu_handler = $obj->menu_handler;
432 $this->entity = $obj->entity;
433 $this->module = $obj->module;
434 $this->
type = $obj->type;
435 $this->mainmenu = $obj->mainmenu;
436 $this->leftmenu = $obj->leftmenu;
437 $this->fk_menu = $obj->fk_menu;
438 $this->fk_mainmenu = $obj->fk_mainmenu;
439 $this->fk_leftmenu = $obj->fk_leftmenu;
441 $this->url = $obj->url;
442 $this->target = $obj->target;
443 $this->title = $obj->title;
444 $this->prefix = $obj->prefix;
445 $this->langs = $obj->langs;
446 $this->perms = $obj->perms;
447 $this->enabled = str_replace(
"\"",
"'", $obj->enabled);
448 $this->
user = $obj->user;
449 $this->tms = $this->db->jdate($obj->tms);
451 $this->db->free($resql);
455 $this->error =
"Error ".$this->db->lasterror();
524 public function menuTopCharger($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
526 global $langs, $user, $conf;
527 global $mainmenu, $leftmenu;
529 $mainmenu = $mymainmenu;
530 $leftmenu = $myleftmenu;
532 $newTabMenu = array();
533 foreach ($tabMenu as $val) {
534 if ($val[
'type'] ==
'top') {
535 $newTabMenu[] = $val;
554 public function menuLeftCharger($newmenu, $mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
556 global $langs, $user, $conf;
557 global $mainmenu, $leftmenu;
559 $mainmenu = $mymainmenu;
560 $leftmenu = $myleftmenu;
564 foreach ($tabMenu as $key => $val) {
566 if (empty($menutopid) && $val[
'type'] ==
'top' && $val[
'mainmenu'] == $mainmenu) {
567 $menutopid = $val[
'rowid'];
573 $this->newmenu = $newmenu;
576 $this->
recur($tabMenu, $menutopid, 1);
579 foreach ($tabMenu as $key => $val) {
580 if ($val[
'fk_menu'] == -1 && $val[
'fk_mainmenu'] == $mainmenu) {
584 if (empty($val[
'fk_leftmenu'])) {
585 $this->newmenu->add($val[
'url'], $val[
'titre'], 0, $val[
'perms'], $val[
'target'], $val[
'mainmenu'], $val[
'leftmenu'], $val[
'position'],
'',
'',
'', $val[
'prefix']);
593 foreach ($this->newmenu->liste as $keyparent => $valparent) {
595 if ($searchlastsub) {
596 if ($valparent[
'level'] >= $searchlastsub) {
597 $lastid = $keyparent;
599 if ($valparent[
'level'] < $searchlastsub) {
600 $nextid = $keyparent;
604 if ($valparent[
'mainmenu'] == $val[
'fk_mainmenu'] && $valparent[
'leftmenu'] == $val[
'fk_leftmenu']) {
607 $searchlastsub = ($valparent[
'level'] + 1);
608 $lastid = $keyparent;
614 $this->newmenu->insert($lastid, $val[
'url'], $val[
'titre'], $searchlastsub, $val[
'perms'], $val[
'target'], $val[
'mainmenu'], $val[
'leftmenu'], $val[
'position'],
'',
'',
'', $val[
'prefix']);
616 dol_syslog(
"Error. Modules ".$val[
'module'].
" has defined a menu entry with a parent='fk_mainmenu=".$val[
'fk_leftmenu'].
",fk_leftmenu=".$val[
'fk_leftmenu'].
"' and position=".$val[
'position'].
'. The parent was not found. May be you forget it into your definition of menu, or may be the parent has a "position" that is after the child (fix field "position" of parent or child in this case).', LOG_WARNING);
623 return $this->newmenu;
637 public function menuLoad($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
639 global $langs, $user, $conf;
640 global $mainmenu, $leftmenu;
642 $mainmenu = $mymainmenu;
643 $leftmenu = $myleftmenu;
645 $sql =
"SELECT m.rowid, m.type, m.module, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.url, m.titre,";
646 $sql .=
" m.prefix, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu, m.position";
647 $sql .=
" FROM ".$this->db->prefix().
"menu as m";
648 $sql .=
" WHERE m.entity IN (0,".$conf->entity.
")";
649 $sql .=
" AND m.menu_handler IN ('".$this->db->escape($menu_handler).
"','all')";
650 if ($type_user == 0) {
651 $sql .=
" AND m.usertype IN (0,2)";
653 if ($type_user == 1) {
654 $sql .=
" AND m.usertype IN (1,2)";
656 $sql .=
" ORDER BY m.type DESC, m.position, m.rowid";
660 $resql = $this->db->query($sql);
662 $numa = $this->db->num_rows($resql);
668 $menu = $this->db->fetch_array($resql);
672 if (isset($menu[
'perms'])) {
673 $tmpcond = $menu[
'perms'];
674 if ($leftmenu ==
'all') {
675 $tmpcond = preg_replace(
'/\$leftmenu\s*==\s*["\'a-zA-Z_]+/',
'1==1', $tmpcond);
683 if (isset($menu[
'enabled'])) {
684 $tmpcond = $menu[
'enabled'];
685 if ($leftmenu ==
'all') {
686 $tmpcond = preg_replace(
'/\$leftmenu\s*==\s*["\'a-zA-Z_]+/',
'1==1', $tmpcond);
694 $title = $langs->trans($menu[
'titre']);
696 if ($title == $menu[
'titre']) {
697 if (!empty($menu[
'langs'])) {
699 $langs->load($menu[
'langs']);
702 $substitarray = array(
'__LOGIN__' => $user->login,
'__USER_ID__' => $user->id,
'__USER_SUPERVISOR_ID__' => $user->fk_user);
705 if (preg_match(
"/\//", $menu[
'titre'])) {
706 $tab_titre = explode(
"/", $menu[
'titre']);
707 $title = $langs->trans($tab_titre[0]).
"/".$langs->trans($tab_titre[1]);
708 } elseif (preg_match(
'/\|\|/', $menu[
'titre'])) {
710 $tab_title = explode(
"||", $menu[
'titre']);
711 $alt_title = explode(
"@", $tab_title[1]);
712 $title_enabled =
verifCond($alt_title[1]);
713 $title = ($title_enabled ? $langs->trans($alt_title[0]) : $langs->trans($tab_title[0]));
715 $title = $langs->trans($menu[
'titre']);
722 $tabMenu[$b][
'rowid'] = $menu[
'rowid'];
723 $tabMenu[$b][
'module'] = $menu[
'module'];
724 $tabMenu[$b][
'fk_menu'] = $menu[
'fk_menu'];
725 $tabMenu[$b][
'url'] = $menu[
'url'];
726 if (!preg_match(
"/^(http:\/\/|https:\/\/)/i", $tabMenu[$b][
'url'])) {
727 if (preg_match(
'/\?/', $tabMenu[$b][
'url'])) {
728 $tabMenu[$b][
'url'] .=
'&idmenu='.$menu[
'rowid'];
730 $tabMenu[$b][
'url'] .=
'?idmenu='.$menu[
'rowid'];
733 $tabMenu[$b][
'titre'] = $title;
734 $tabMenu[$b][
'prefix'] = $menu[
'prefix'];
735 $tabMenu[$b][
'target'] = $menu[
'target'];
736 $tabMenu[$b][
'mainmenu'] = $menu[
'mainmenu'];
737 $tabMenu[$b][
'leftmenu'] = $menu[
'leftmenu'];
738 $tabMenu[$b][
'perms'] = $perms;
739 $tabMenu[$b][
'langs'] = $menu[
'langs'];
740 $tabMenu[$b][
'enabled'] = $enabled;
741 $tabMenu[$b][
'type'] = $menu[
'type'];
742 $tabMenu[$b][
'fk_mainmenu'] = $menu[
'fk_mainmenu'];
743 $tabMenu[$b][
'fk_leftmenu'] = $menu[
'fk_leftmenu'];
744 $tabMenu[$b][
'position'] = (int) $menu[
'position'];
751 $this->db->free($resql);
772 private function recur($tab, $pere, $level)
776 for ($x = 0; $x < $num; $x++) {
778 if ((($tab[$x][
'fk_menu'] >= 0 && $tab[$x][
'fk_menu'] == $pere)) && $tab[$x][
'enabled']) {
779 $this->newmenu->add($tab[$x][
'url'], $tab[$x][
'titre'], ($level - 1), $tab[$x][
'perms'], $tab[$x][
'target'], $tab[$x][
'mainmenu'], $tab[$x][
'leftmenu'], 0,
'',
'',
'', $tab[$x][
'prefix']);
780 $this->
recur($tab, $tab[$x][
'rowid'], ($level + 1));