187 global $conf, $langs;
190 if (!isset($this->enabled)) {
191 $this->enabled =
'1';
193 $this->entity = (isset($this->entity) && (int) $this->entity >= 0 ? (
int) $this->entity : $conf->entity);
194 $this->menu_handler = trim((
string) $this->menu_handler);
195 $this->module = trim((
string) $this->module);
196 $this->
type = trim((
string) $this->
type);
197 $this->mainmenu = trim((
string) $this->mainmenu);
198 $this->leftmenu = trim((
string) $this->leftmenu);
199 $this->fk_menu = (int) $this->fk_menu;
200 $this->fk_mainmenu = trim((
string) $this->fk_mainmenu);
201 $this->fk_leftmenu = trim((
string) $this->fk_leftmenu);
203 $this->url = trim((
string) $this->url);
204 $this->target = trim((
string) $this->target);
205 $this->title = trim((
string) $this->title);
206 $this->langs = trim((
string) $this->langs);
207 $this->perms = trim((
string) $this->perms);
208 $this->enabled = trim((
string) $this->enabled);
218 if (empty($this->menu_handler)) {
225 if (in_array($this->db->type, array(
'pgsql'))) {
226 $sql =
"SELECT MAX(rowid) as maxrowid FROM ".$this->db->prefix().
"menu";
227 $resqlrowid = $this->db->query($sql);
229 $obj = $this->db->fetch_object($resqlrowid);
230 $maxrowid = $obj->maxrowid;
233 if (empty($maxrowid)) {
237 $sql =
"SELECT setval('".$this->db->prefix().
"menu_rowid_seq', ".($maxrowid).
")";
239 $resqlrowidset = $this->db->query($sql);
240 if (!$resqlrowidset) {
249 $sql =
"SELECT count(*)";
250 $sql .=
" FROM ".$this->db->prefix().
"menu";
251 $sql .=
" WHERE menu_handler = '".$this->db->escape($this->menu_handler).
"'";
252 $sql .=
" AND fk_menu = ".((int) $this->fk_menu);
253 $sql .=
" AND position = ".((int) $this->
position);
254 $sql .=
" AND url = '".$this->db->escape($this->url).
"'";
255 $sql .=
" AND entity IN (0, ".$conf->entity.
")";
257 $result = $this->db->query($sql);
259 $row = $this->db->fetch_row($result);
263 $sql =
"INSERT INTO ".$this->db->prefix().
"menu(";
264 $sql .=
"menu_handler,";
271 $sql .=
"fk_mainmenu,";
272 $sql .=
"fk_leftmenu,";
282 $sql .=
") VALUES (";
283 $sql .=
" '".$this->db->escape($this->menu_handler).
"',";
284 $sql .=
" '".$this->db->escape($this->entity).
"',";
285 $sql .=
" '".$this->db->escape($this->module).
"',";
286 $sql .=
" '".$this->db->escape($this->type).
"',";
287 $sql .=
" ".($this->mainmenu ?
"'".$this->db->escape($this->mainmenu).
"'" :
"''").
",";
288 $sql .=
" ".($this->leftmenu ?
"'".$this->db->escape($this->leftmenu).
"'" :
"null").
",";
289 $sql .=
" ".((int) $this->fk_menu).
",";
290 $sql .=
" ".($this->fk_mainmenu ?
"'".$this->db->escape($this->fk_mainmenu).
"'" :
"null").
",";
291 $sql .=
" ".($this->fk_leftmenu ?
"'".$this->db->escape($this->fk_leftmenu).
"'" :
"null").
",";
292 $sql .=
" ".((int) $this->
position).
",";
293 $sql .=
" '".$this->db->escape($this->url).
"',";
294 $sql .=
" '".$this->db->escape($this->target).
"',";
295 $sql .=
" '".$this->db->escape($this->title).
"',";
296 $sql .=
" '".$this->db->escape($this->prefix).
"',";
297 $sql .=
" '".$this->db->escape($this->langs).
"',";
298 $sql .=
" '".$this->db->escape($this->perms).
"',";
299 $sql .=
" '".$this->db->escape($this->enabled).
"',";
300 $sql .=
" '".$this->db->escape($this->
user).
"'";
303 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
304 $resql = $this->db->query($sql);
306 $this->
id = $this->db->last_insert_id($this->db->prefix().
"menu");
307 dol_syslog(get_class($this).
"::create record added has rowid=".((
int) $this->
id), LOG_DEBUG);
311 $this->error =
"Error ".$this->db->lasterror();
315 dol_syslog(get_class($this).
"::create menu entry already exists", LOG_WARNING);
316 $this->error =
'Error Menu entry ('.$this->menu_handler.
','.$this->
position.
','.$this->url.
') already exists';
331 public function update($user =
null, $notrigger = 0)
337 $this->menu_handler = trim($this->menu_handler);
338 $this->module = trim($this->module);
340 $this->mainmenu = trim($this->mainmenu);
341 $this->leftmenu = trim($this->leftmenu);
342 $this->fk_menu = (int) $this->fk_menu;
343 $this->fk_mainmenu = trim($this->fk_mainmenu);
344 $this->fk_leftmenu = trim($this->fk_leftmenu);
346 $this->url = trim($this->url);
347 $this->target = trim($this->target);
348 $this->title = trim($this->title);
349 $this->prefix = trim($this->prefix);
350 $this->langs = trim($this->langs);
351 $this->perms = trim($this->perms);
352 $this->enabled = trim($this->enabled);
359 $sql =
"UPDATE ".$this->db->prefix().
"menu SET";
360 $sql .=
" menu_handler='".$this->db->escape($this->menu_handler).
"',";
361 $sql .=
" module='".$this->db->escape($this->module).
"',";
362 $sql .=
" type='".$this->db->escape($this->
type).
"',";
363 $sql .=
" mainmenu='".$this->db->escape($this->mainmenu).
"',";
364 $sql .=
" leftmenu='".$this->db->escape($this->leftmenu).
"',";
365 $sql .=
" fk_menu=".((int) $this->fk_menu).
",";
366 $sql .=
" fk_mainmenu=".($this->fk_mainmenu ?
"'".$this->db->escape($this->fk_mainmenu).
"'" :
"null").
",";
367 $sql .=
" fk_leftmenu=".($this->fk_leftmenu ?
"'".$this->db->escape($this->fk_leftmenu).
"'" :
"null").
",";
368 $sql .=
" position=".($this->position > 0 ? ((int) $this->
position) : 0).
",";
369 $sql .=
" url='".$this->db->escape($this->url).
"',";
370 $sql .=
" target='".$this->db->escape($this->target).
"',";
371 $sql .=
" titre='".$this->db->escape($this->title).
"',";
372 $sql .=
" prefix='".$this->db->escape($this->prefix).
"',";
373 $sql .=
" langs='".$this->db->escape($this->langs).
"',";
374 $sql .=
" perms='".$this->db->escape($this->perms).
"',";
375 $sql .=
" enabled='".$this->db->escape($this->enabled).
"',";
376 $sql .=
" usertype='".$this->db->escape($this->
user).
"'";
377 $sql .=
" WHERE rowid=".((int) $this->
id);
379 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
380 $resql = $this->db->query($sql);
382 $this->error =
"Error ".$this->db->lasterror();
397 public function fetch($id, $user =
null)
403 $sql .=
" t.menu_handler,";
404 $sql .=
" t.entity,";
405 $sql .=
" t.module,";
407 $sql .=
" t.mainmenu,";
408 $sql .=
" t.leftmenu,";
409 $sql .=
" t.fk_menu,";
410 $sql .=
" t.fk_mainmenu,";
411 $sql .=
" t.fk_leftmenu,";
412 $sql .=
" t.position,";
414 $sql .=
" t.target,";
415 $sql .=
" t.titre as title,";
416 $sql .=
" t.prefix,";
419 $sql .=
" t.enabled,";
420 $sql .=
" t.usertype as user,";
422 $sql .=
" FROM ".$this->db->prefix().
"menu as t";
423 $sql .=
" WHERE t.rowid = ".((int) $id);
425 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
426 $resql = $this->db->query($sql);
428 if ($this->db->num_rows($resql)) {
429 $obj = $this->db->fetch_object($resql);
431 $this->
id = $obj->rowid;
433 $this->menu_handler = $obj->menu_handler;
434 $this->entity = $obj->entity;
435 $this->module = $obj->module;
436 $this->
type = $obj->type;
437 $this->mainmenu = $obj->mainmenu;
438 $this->leftmenu = $obj->leftmenu;
439 $this->fk_menu = $obj->fk_menu;
440 $this->fk_mainmenu = $obj->fk_mainmenu;
441 $this->fk_leftmenu = $obj->fk_leftmenu;
443 $this->url = $obj->url;
444 $this->target = $obj->target;
445 $this->title = $obj->title;
446 $this->prefix = $obj->prefix;
447 $this->langs = $obj->langs;
448 $this->perms = $obj->perms;
449 $this->enabled = str_replace(
"\"",
"'", $obj->enabled);
450 $this->
user = $obj->user;
451 $this->tms = $this->db->jdate($obj->tms);
453 $this->db->free($resql);
457 $this->error =
"Error ".$this->db->lasterror();
526 public function menuTopCharger($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
528 global $langs, $user, $conf;
529 global $mainmenu, $leftmenu;
531 $mainmenu = $mymainmenu;
532 $leftmenu = $myleftmenu;
534 $newTabMenu = array();
535 foreach ($tabMenu as $val) {
536 if ($val[
'type'] ==
'top') {
537 $newTabMenu[] = $val;
556 public function menuLeftCharger($newmenu, $mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
558 global $langs, $user, $conf;
559 global $mainmenu, $leftmenu;
561 $mainmenu = $mymainmenu;
562 $leftmenu = $myleftmenu;
566 foreach ($tabMenu as $key => $val) {
568 if (empty($menutopid) && $val[
'type'] ==
'top' && $val[
'mainmenu'] == $mainmenu) {
569 $menutopid = $val[
'rowid'];
575 $this->newmenu = $newmenu;
578 $this->
recur($tabMenu, $menutopid, 1);
581 foreach ($tabMenu as $key => $val) {
582 if ($val[
'fk_menu'] == -1 && $val[
'fk_mainmenu'] == $mainmenu) {
586 if (empty($val[
'fk_leftmenu'])) {
587 $this->newmenu->add($val[
'url'], $val[
'titre'], 0, $val[
'perms'], $val[
'target'], $val[
'mainmenu'], $val[
'leftmenu'], $val[
'position'],
'',
'',
'', $val[
'prefix']);
595 foreach ($this->newmenu->liste as $keyparent => $valparent) {
597 if ($searchlastsub) {
598 if ($valparent[
'level'] >= $searchlastsub) {
599 $lastid = $keyparent;
601 if ($valparent[
'level'] < $searchlastsub) {
602 $nextid = $keyparent;
606 if ($valparent[
'mainmenu'] == $val[
'fk_mainmenu'] && $valparent[
'leftmenu'] == $val[
'fk_leftmenu']) {
609 $searchlastsub = ($valparent[
'level'] + 1);
610 $lastid = $keyparent;
616 $this->newmenu->insert($lastid, $val[
'url'], $val[
'titre'], $searchlastsub, $val[
'perms'], $val[
'target'], $val[
'mainmenu'], $val[
'leftmenu'], $val[
'position'],
'',
'',
'', $val[
'prefix']);
618 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);
625 return $this->newmenu;
639 public function menuLoad($mymainmenu, $myleftmenu, $type_user, $menu_handler, &$tabMenu)
641 global $langs, $user, $conf;
642 global $mainmenu, $leftmenu;
644 $mainmenu = $mymainmenu;
645 $leftmenu = $myleftmenu;
647 $sql =
"SELECT m.rowid, m.type, m.module, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.url, m.titre, m.prefix, m.langs, m.perms, m.enabled, m.target, m.mainmenu, m.leftmenu, m.position";
648 $sql .=
" FROM ".$this->db->prefix().
"menu as m";
649 $sql .=
" WHERE m.entity IN (0,".$conf->entity.
")";
650 $sql .=
" AND m.menu_handler IN ('".$this->db->escape($menu_handler).
"','all')";
651 if ($type_user == 0) {
652 $sql .=
" AND m.usertype IN (0,2)";
654 if ($type_user == 1) {
655 $sql .=
" AND m.usertype IN (1,2)";
657 $sql .=
" ORDER BY m.position, m.rowid";
661 $resql = $this->db->query($sql);
663 $numa = $this->db->num_rows($resql);
669 $menu = $this->db->fetch_array($resql);
673 if (isset($menu[
'perms'])) {
674 $tmpcond = $menu[
'perms'];
675 if ($leftmenu ==
'all') {
676 $tmpcond = preg_replace(
'/\$leftmenu\s*==\s*["\'a-zA-Z_]+/',
'1==1', $tmpcond);
684 if (isset($menu[
'enabled'])) {
685 $tmpcond = $menu[
'enabled'];
686 if ($leftmenu ==
'all') {
687 $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));