184 if (!isset($this->enabled)) {
185 $this->enabled =
'1';
187 $this->entity = (isset($this->entity) && (int) $this->entity >= 0 ? (
int) $this->entity : $conf->entity);
188 $this->menu_handler = trim((
string) $this->menu_handler);
189 $this->module = trim((
string) $this->module);
190 $this->
type = trim((
string) $this->
type);
191 $this->mainmenu = trim((
string) $this->mainmenu);
192 $this->leftmenu = trim((
string) $this->leftmenu);
193 $this->fk_menu = (int) $this->fk_menu;
194 $this->fk_mainmenu = trim((
string) $this->fk_mainmenu);
195 $this->fk_leftmenu = trim((
string) $this->fk_leftmenu);
197 $this->url = trim((
string) $this->url);
198 $this->target = trim((
string) $this->target);
199 $this->title = trim((
string) $this->title);
200 $this->langs = trim((
string) $this->langs);
201 $this->perms = trim((
string) $this->perms);
202 $this->enabled = trim((
string) $this->enabled);
209 if (empty($this->menu_handler)) {
216 if (in_array($this->db->type, array(
'pgsql'))) {
217 $sql =
"SELECT MAX(rowid) as maxrowid FROM ".$this->db->prefix().
"menu";
218 $resqlrowid = $this->db->query($sql);
220 $obj = $this->db->fetch_object($resqlrowid);
221 $maxrowid = $obj->maxrowid;
224 if (empty($maxrowid)) {
228 $sql =
"SELECT setval('".$this->db->prefix().
"menu_rowid_seq', ".($maxrowid).
")";
230 $resqlrowidset = $this->db->query($sql);
231 if (!$resqlrowidset) {
240 $sql =
"SELECT count(*)";
241 $sql .=
" FROM ".$this->db->prefix().
"menu";
242 $sql .=
" WHERE menu_handler = '".$this->db->escape($this->menu_handler).
"'";
243 $sql .=
" AND fk_menu = ".((int) $this->fk_menu);
244 $sql .=
" AND position = ".((int) $this->
position);
245 $sql .=
" AND url = '".$this->db->escape($this->url).
"'";
246 $sql .=
" AND entity IN (0, ".$conf->entity.
")";
248 $result = $this->db->query($sql);
250 $row = $this->db->fetch_row($result);
254 $sql =
"INSERT INTO ".$this->db->prefix().
"menu(";
255 $sql .=
"menu_handler,";
262 $sql .=
"fk_mainmenu,";
263 $sql .=
"fk_leftmenu,";
273 $sql .=
") VALUES (";
274 $sql .=
" '".$this->db->escape($this->menu_handler).
"',";
275 $sql .=
" '".$this->db->escape($this->entity).
"',";
276 $sql .=
" '".$this->db->escape($this->module).
"',";
277 $sql .=
" '".$this->db->escape($this->type).
"',";
278 $sql .=
" ".($this->mainmenu ?
"'".$this->db->escape($this->mainmenu).
"'" :
"''").
",";
279 $sql .=
" ".($this->leftmenu ?
"'".$this->db->escape($this->leftmenu).
"'" :
"null").
",";
280 $sql .=
" ".((int) $this->fk_menu).
",";
281 $sql .=
" ".($this->fk_mainmenu ?
"'".$this->db->escape($this->fk_mainmenu).
"'" :
"null").
",";
282 $sql .=
" ".($this->fk_leftmenu ?
"'".$this->db->escape($this->fk_leftmenu).
"'" :
"null").
",";
283 $sql .=
" ".((int) $this->
position).
",";
284 $sql .=
" '".$this->db->escape($this->url).
"',";
285 $sql .=
" '".$this->db->escape($this->target).
"',";
286 $sql .=
" '".$this->db->escape($this->title).
"',";
287 $sql .=
" '".$this->db->escape($this->prefix).
"',";
288 $sql .=
" '".$this->db->escape($this->langs).
"',";
289 $sql .=
" '".$this->db->escape($this->perms).
"',";
290 $sql .=
" '".$this->db->escape($this->enabled).
"',";
291 $sql .=
" '".$this->db->escape($this->
user).
"'";
294 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
295 $resql = $this->db->query($sql);
297 $this->
id = $this->db->last_insert_id($this->db->prefix().
"menu");
298 dol_syslog(get_class($this).
"::create record added has rowid=".((
int) $this->
id), LOG_DEBUG);
302 $this->error =
"Error ".$this->db->lasterror();
306 dol_syslog(get_class($this).
"::create menu entry already exists", LOG_WARNING);
307 $this->error =
'Error Menu entry ('.$this->menu_handler.
','.$this->
position.
','.$this->url.
') already exists';
322 public function update($user =
null, $notrigger = 0)
327 $this->menu_handler = trim($this->menu_handler);
328 $this->module = trim($this->module);
330 $this->mainmenu = trim($this->mainmenu);
331 $this->leftmenu = trim($this->leftmenu);
332 $this->fk_menu = (int) $this->fk_menu;
333 $this->fk_mainmenu = trim($this->fk_mainmenu);
334 $this->fk_leftmenu = trim($this->fk_leftmenu);
336 $this->url = trim($this->url);
337 $this->target = trim($this->target);
338 $this->title = trim($this->title);
339 $this->prefix = trim($this->prefix);
340 $this->langs = trim($this->langs);
341 $this->perms = trim($this->perms);
342 $this->enabled = trim($this->enabled);
349 $sql =
"UPDATE ".$this->db->prefix().
"menu SET";
350 $sql .=
" menu_handler='".$this->db->escape($this->menu_handler).
"',";
351 $sql .=
" module='".$this->db->escape($this->module).
"',";
352 $sql .=
" type='".$this->db->escape($this->
type).
"',";
353 $sql .=
" mainmenu='".$this->db->escape($this->mainmenu).
"',";
354 $sql .=
" leftmenu='".$this->db->escape($this->leftmenu).
"',";
355 $sql .=
" fk_menu=".((int) $this->fk_menu).
",";
356 $sql .=
" fk_mainmenu=".($this->fk_mainmenu ?
"'".$this->db->escape($this->fk_mainmenu).
"'" :
"null").
",";
357 $sql .=
" fk_leftmenu=".($this->fk_leftmenu ?
"'".$this->db->escape($this->fk_leftmenu).
"'" :
"null").
",";
358 $sql .=
" position=".($this->position > 0 ? ((int) $this->
position) : 0).
",";
359 $sql .=
" url='".$this->db->escape($this->url).
"',";
360 $sql .=
" target='".$this->db->escape($this->target).
"',";
361 $sql .=
" titre='".$this->db->escape($this->title).
"',";
362 $sql .=
" prefix='".$this->db->escape($this->prefix).
"',";
363 $sql .=
" langs='".$this->db->escape($this->langs).
"',";
364 $sql .=
" perms='".$this->db->escape($this->perms).
"',";
365 $sql .=
" enabled='".$this->db->escape($this->enabled).
"',";
366 $sql .=
" usertype='".$this->db->escape($this->
user).
"'";
367 $sql .=
" WHERE rowid=".((int) $this->
id);
369 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
370 $resql = $this->db->query($sql);
372 $this->error =
"Error ".$this->db->lasterror();
387 public function fetch($id, $user =
null)
393 $sql .=
" t.menu_handler,";
394 $sql .=
" t.entity,";
395 $sql .=
" t.module,";
397 $sql .=
" t.mainmenu,";
398 $sql .=
" t.leftmenu,";
399 $sql .=
" t.fk_menu,";
400 $sql .=
" t.fk_mainmenu,";
401 $sql .=
" t.fk_leftmenu,";
402 $sql .=
" t.position,";
404 $sql .=
" t.target,";
405 $sql .=
" t.titre as title,";
406 $sql .=
" t.prefix,";
409 $sql .=
" t.enabled,";
410 $sql .=
" t.usertype as user,";
412 $sql .=
" FROM ".$this->db->prefix().
"menu as t";
413 $sql .=
" WHERE t.rowid = ".((int) $id);
415 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
416 $resql = $this->db->query($sql);
418 if ($this->db->num_rows($resql)) {
419 $obj = $this->db->fetch_object($resql);
421 $this->
id = $obj->rowid;
423 $this->menu_handler = $obj->menu_handler;
424 $this->entity = $obj->entity;
425 $this->module = $obj->module;
426 $this->
type = $obj->type;
427 $this->mainmenu = $obj->mainmenu;
428 $this->leftmenu = $obj->leftmenu;
429 $this->fk_menu = $obj->fk_menu;
430 $this->fk_mainmenu = $obj->fk_mainmenu;
431 $this->fk_leftmenu = $obj->fk_leftmenu;
433 $this->url = $obj->url;
434 $this->target = $obj->target;
435 $this->title = $obj->title;
436 $this->prefix = $obj->prefix;
437 $this->langs = $obj->langs;
438 $this->perms = str_replace(
"\"",
"'", $obj->perms);
439 $this->enabled = str_replace(
"\"",
"'", $obj->enabled);
440 $this->
user = $obj->user;
441 $this->tms = $this->db->jdate($obj->tms);
443 $this->db->free($resql);
447 $this->error =
"Error ".$this->db->lasterror();
518 public function menuTopCharger($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
520 global $langs, $user, $conf;
521 global $mainmenu, $leftmenu;
523 $mainmenu = $mymainmenu;
524 $leftmenu = $myleftmenu;
526 $newTabMenu = array();
527 foreach ($tabMenu as $val) {
528 if ($val[
'type'] ==
'top') {
529 $newTabMenu[] = $val;
548 public function menuLeftCharger($newmenu, $mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
550 global $langs, $user, $conf;
551 global $mainmenu, $leftmenu;
553 $mainmenu = $mymainmenu;
554 $leftmenu = $myleftmenu;
558 foreach ($tabMenu as $key => $val) {
560 if (empty($menutopid) && $val[
'type'] ==
'top' && $val[
'mainmenu'] == $mainmenu) {
561 $menutopid = $val[
'rowid'];
567 $this->newmenu = $newmenu;
570 $this->
recur($tabMenu, $menutopid, 1);
573 foreach ($tabMenu as $key => $val) {
574 if ($val[
'fk_menu'] == -1 && $val[
'fk_mainmenu'] == $mainmenu) {
577 if (empty($val[
'fk_leftmenu'])) {
578 $this->newmenu->add($val[
'url'], $val[
'titre'], 0, $val[
'perms'], $val[
'target'], $val[
'mainmenu'], $val[
'leftmenu'], $val[
'position'],
'',
'',
'', $val[
'prefix']);
586 foreach ($this->newmenu->liste as $keyparent => $valparent) {
588 if ($searchlastsub) {
589 if ($valparent[
'level'] >= $searchlastsub) {
590 $lastid = $keyparent;
592 if ($valparent[
'level'] < $searchlastsub) {
593 $nextid = $keyparent;
597 if ($valparent[
'mainmenu'] == $val[
'fk_mainmenu'] && $valparent[
'leftmenu'] == $val[
'fk_leftmenu']) {
600 $searchlastsub = ($valparent[
'level'] + 1);
601 $lastid = $keyparent;
607 $this->newmenu->insert($lastid, $val[
'url'], $val[
'titre'], $searchlastsub, $val[
'perms'], $val[
'target'], $val[
'mainmenu'], $val[
'leftmenu'], $val[
'position'],
'',
'',
'', $val[
'prefix']);
609 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);
616 return $this->newmenu;
630 public function menuLoad($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
632 global $langs, $user, $conf;
633 global $mainmenu, $leftmenu;
635 $mainmenu = $mymainmenu;
636 $leftmenu = $myleftmenu;
638 $sql =
"SELECT m.rowid, m.type, m.module, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.url, m.titre,";
639 $sql .=
" m.prefix, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu, m.position";
640 $sql .=
" FROM ".$this->db->prefix().
"menu as m";
641 $sql .=
" WHERE m.entity IN (0,".$conf->entity.
")";
642 $sql .=
" AND m.menu_handler IN ('".$this->db->escape($menu_handler).
"','all')";
643 if ($type_user == 0) {
644 $sql .=
" AND m.usertype IN (0,2)";
646 if ($type_user == 1) {
647 $sql .=
" AND m.usertype IN (1,2)";
649 $sql .=
" ORDER BY m.type DESC, m.position, m.rowid";
653 $resql = $this->db->query($sql);
655 $numa = $this->db->num_rows($resql);
661 $menu = $this->db->fetch_array($resql);
665 if (isset($menu[
'perms'])) {
666 $tmpcond = $menu[
'perms'];
667 if ($leftmenu ==
'all') {
668 $tmpcond = preg_replace(
'/\$leftmenu\s*==\s*["\'a-zA-Z_]+/',
'1==1', $tmpcond);
676 if (isset($menu[
'enabled'])) {
677 $tmpcond = $menu[
'enabled'];
678 if ($leftmenu ==
'all') {
679 $tmpcond = preg_replace(
'/\$leftmenu\s*==\s*["\'a-zA-Z_]+/',
'1==1', $tmpcond);
686 if ($enabled && isset($menu)) {
687 $title = $langs->trans($menu[
'titre']);
689 if ($title == $menu[
'titre']) {
690 if (!empty($menu[
'langs'])) {
692 $langs->load($menu[
'langs']);
695 $substitarray = array(
'__LOGIN__' => $user->login,
'__USER_ID__' => $user->id,
'__USER_SUPERVISOR_ID__' => $user->fk_user);
698 if (preg_match(
"/\//", $menu[
'titre'])) {
699 $tab_titre = explode(
"/", $menu[
'titre']);
700 $title = $langs->trans($tab_titre[0]).
"/".$langs->trans($tab_titre[1]);
701 } elseif (preg_match(
'/\|\|/', $menu[
'titre'])) {
703 $tab_title = explode(
"||", $menu[
'titre']);
704 $alt_title = explode(
"@", $tab_title[1]);
705 $title_enabled =
verifCond($alt_title[1]);
706 $title = ($title_enabled ? $langs->trans($alt_title[0]) : $langs->trans($tab_title[0]));
708 $title = $langs->trans($menu[
'titre']);
713 $tabMenu[$b][
'rowid'] = $menu[
'rowid'];
714 $tabMenu[$b][
'module'] = $menu[
'module'];
715 $tabMenu[$b][
'fk_menu'] = $menu[
'fk_menu'];
716 $tabMenu[$b][
'url'] = $menu[
'url'];
717 if (!preg_match(
"/^(http:\/\/|https:\/\/)/i", $tabMenu[$b][
'url'])) {
718 if (preg_match(
'/\?/', $tabMenu[$b][
'url'])) {
719 $tabMenu[$b][
'url'] .=
'&idmenu='.$menu[
'rowid'];
721 $tabMenu[$b][
'url'] .=
'?idmenu='.$menu[
'rowid'];
724 $tabMenu[$b][
'titre'] = $title;
725 $tabMenu[$b][
'prefix'] = $menu[
'prefix'];
726 $tabMenu[$b][
'target'] = $menu[
'target'];
727 $tabMenu[$b][
'mainmenu'] = $menu[
'mainmenu'];
728 $tabMenu[$b][
'leftmenu'] = $menu[
'leftmenu'];
729 $tabMenu[$b][
'perms'] = $perms;
730 $tabMenu[$b][
'langs'] = $menu[
'langs'];
731 $tabMenu[$b][
'enabled'] = $enabled;
732 $tabMenu[$b][
'type'] = $menu[
'type'];
733 $tabMenu[$b][
'fk_mainmenu'] = $menu[
'fk_mainmenu'];
734 $tabMenu[$b][
'fk_leftmenu'] = $menu[
'fk_leftmenu'];
735 $tabMenu[$b][
'position'] = (int) $menu[
'position'];
742 $this->db->free($resql);
763 private function recur($tab, $pere, $level)
767 for ($x = 0; $x < $num; $x++) {
769 if ((($tab[$x][
'fk_menu'] >= 0 && $tab[$x][
'fk_menu'] == $pere)) && $tab[$x][
'enabled']) {
770 $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']);
771 $this->
recur($tab, $tab[$x][
'rowid'], ($level + 1));