From 2ad54c21002f112866828b95ee25efdce202f8fb Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Mon, 20 Apr 2009 14:12:37 +0000 Subject: [PATCH] New: possibility to declare constraint menu in module --- dev/skeletons/modMyModule.class.php | 3 +- htdocs/admin/menus/edit.php | 43 +++---- htdocs/core/menubase.class.php | 108 ++++++++++++++++++ .../modules/DolibarrModules.class.php | 12 ++ htdocs/includes/modules/modAgenda.class.php | 13 ++- htdocs/includes/modules/modCashDesk.class.php | 3 +- htdocs/includes/modules/modECM.class.php | 3 +- htdocs/includes/modules/modPhenix.class.php | 13 ++- 8 files changed, 167 insertions(+), 31 deletions(-) diff --git a/dev/skeletons/modMyModule.class.php b/dev/skeletons/modMyModule.class.php index 29bfb516dd4..2438b6aaa21 100644 --- a/dev/skeletons/modMyModule.class.php +++ b/dev/skeletons/modMyModule.class.php @@ -146,7 +146,8 @@ class modMyModule extends DolibarrModules // 'position'=>100, // 'perms'=>'1', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules // 'target'=>'', - // 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both + // 'user'=>2, // 0=Menu for internal users, 1=external users, 2=both + // 'constraint'=>'$conf->mymodule->enabled'); // Add constraint // $r++; // // Example to declare a Left Menu entry: diff --git a/htdocs/admin/menus/edit.php b/htdocs/admin/menus/edit.php index d1938afa857..0a2b3d783c1 100644 --- a/htdocs/admin/menus/edit.php +++ b/htdocs/admin/menus/edit.php @@ -182,44 +182,35 @@ if (isset($_GET["action"]) && $_GET["action"] == 'add') if (isset($_GET["action"]) && $_GET["action"] == 'add_const') { - - if($_POST['type'] == 'prede') + $langs->load("errors"); + $menu = new Menubase($db); + $result=$menu->addConstraint($_POST['menuId'], $_POST['constraint'], $_POST['type']); + if ($result > 0) { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu_const(fk_menu, fk_constraint) VALUES(".$_POST['menuId'].",".$_POST['constraint'].")"; + $mesg='
'.$langs->trans("RecordModifiedSuccessfully").'
'; } else { - - $sql = "SELECT max(rowid) as maxId FROM ".MAIN_DB_PREFIX."menu_constraint"; - $result = $db->query($sql); - $objc = $db->fetch_object($result); - $constraint = ($objc->maxId) + 1; - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu_constraint(rowid,action) VALUES(".$constraint.",'".$_POST['constraint']."')"; - $db->query($sql); - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu_const(fk_menu, fk_constraint) VALUES(".$_POST['menuId'].",".$constraint.")"; + $mesg='
'.$menu->error.'
'; } - $db->query($sql); - header("location:edit.php?action=edit&menuId=".$_POST['menuId']); exit; } if (isset($_GET["action"]) && $_GET["action"] == 'del_const') { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu_const WHERE fk_menu = ".$_GET['menuId']." AND fk_constraint = ".$_GET['constId']; - $db->query($sql); - - $sql = "SELECT count(rowid) as countId FROM ".MAIN_DB_PREFIX."menu_const WHERE fk_constraint = ".$_GET['constId']; - $result = $db->query($sql); - $objc = $db->fetch_object($result); - if($objc->countId == 0) + $langs->load("errors"); + $menu = new Menubase($db); + $result=$menu->delConstraint($_GET['menuId'], $_GET['constId']); + if ($result > 0) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu_constraint WHERE rowid = ".$_GET['constId']; - $db->query($sql); - } + $mesg='
'.$langs->trans("RecordModifiedSuccessfully").'
'; + } + else + { + $mesg='
'.$menu->error.'
'; + } header("location:edit.php?action=edit&menuId=".$_GET['menuId']); exit; @@ -529,7 +520,7 @@ elseif (isset($_GET["action"]) && $_GET["action"] == 'edit') // Ajout de contraintes predefinis print '
'; print ''; - print ''; + print ''; $var=!$var; print ''; diff --git a/htdocs/core/menubase.class.php b/htdocs/core/menubase.class.php index ef7fdfa8693..acf708ac724 100644 --- a/htdocs/core/menubase.class.php +++ b/htdocs/core/menubase.class.php @@ -660,6 +660,114 @@ class Menubase return $tabMenu; } + + /** + * \brief Add constraint menu + * \param menuId Menu id + * \param constraint Value or id for constraint + * \param type type of constraint + * \return int <0 if KO, >0 if OK + */ + function addConstraint($menuId, $constraint, $type='') + { + if($type == 'predefined') + { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu_const(fk_menu, fk_constraint) VALUES(".$menuId.",".$constraint.")"; + } + else + { + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."menu_constraint"; + $sql.= " WHERE action = '".$constraint."'"; + $resSql = $this->db->query($sql); + if (! $resSql) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::addConstraint::4 ".$this->error, LOG_ERR); + return -4; + } + $num = $this->db->num_rows($resSql); + if ($num) + { + $obj = $this->db->fetch_object($resSql); + $constraintId = $obj->rowid; + } + else + { + $sql = "SELECT max(rowid) as maxId FROM ".MAIN_DB_PREFIX."menu_constraint"; + $resultId = $this->db->query($sql); + if (! $resultId) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::addConstraint::3 ".$this->error, LOG_ERR); + return -3; + } + + $objc = $this->db->fetch_object($resultId); + $constraintId = ($objc->maxId) + 1; + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu_constraint(rowid,action) VALUES(".$constraintId.",'".$constraint."')"; + $result = $this->db->query($sql); + if (! $result) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::addConstraint::2 sql = ".$sql." ".$this->error, LOG_ERR); + return -2; + } + } + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."menu_const(fk_menu, fk_constraint) VALUES(".$menuId.",".$constraintId.")"; + } + + $result = $this->db->query($sql); + if (! $result) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::addConstraint::1 sql = ".$sql." ".$this->error, LOG_ERR); + return -1; + } + return 1; + } + + /** + * \brief Delete constraint menu + * \param menuId Menu id + * \param constId Constraint id + * \return int <0 if KO, >0 if OK + */ + function delConstraint($menuId, $constId) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu_const WHERE fk_menu = ".$menuId." AND fk_constraint = ".$constId; + $result = $this->db->query($sql); + if (! $result) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::delConstraint::3 ".$this->error, LOG_ERR); + return -3; + } + + $sql = "SELECT count(rowid) as countId FROM ".MAIN_DB_PREFIX."menu_const WHERE fk_constraint = ".$constId; + $result = $this->db->query($sql); + if (! $result) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::delConstraint::2 ".$this->error, LOG_ERR); + return -2; + } + + $objc = $this->db->fetch_object($result); + if($objc->countId == 0) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu_constraint WHERE rowid = ".$constId; + $result = $this->db->query($sql); + if (! $result) + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog("Menubase::delConstraint::1 ".$this->error, LOG_ERR); + return -1; + } + } + return 1; + } } diff --git a/htdocs/includes/modules/DolibarrModules.class.php b/htdocs/includes/modules/DolibarrModules.class.php index 3e0b63d64fe..44d40b5fa18 100644 --- a/htdocs/includes/modules/DolibarrModules.class.php +++ b/htdocs/includes/modules/DolibarrModules.class.php @@ -873,11 +873,23 @@ class DolibarrModules $menu->perms=$this->menu[$key]['perms']; $menu->target=$this->menu[$key]['target']; $menu->user=$this->menu[$key]['user']; + $menu->constraint=$this->menu[$key]['constraint']; if (! $err) { $result=$menu->create($user); if ($result > 0) { + if ($menu->constraint) + { + $resultConstraint=$menu->addConstraint($result,$menu->constraint); + if ($resultConstraint < 0) + { + $this->error=$menu->error; + dol_syslog('DolibarrModules::addConstraint result='.$resultConstraint." ".$this->error, LOG_ERR); + $err++; + break; + } + } $this->menu[$key]['rowid']=$result; } else diff --git a/htdocs/includes/modules/modAgenda.class.php b/htdocs/includes/modules/modAgenda.class.php index cb676c1100b..4bd2460118a 100644 --- a/htdocs/includes/modules/modAgenda.class.php +++ b/htdocs/includes/modules/modAgenda.class.php @@ -140,7 +140,18 @@ class modAgenda extends DolibarrModules //------ $r=0; - $this->menu[$r]=array('fk_menu'=>0,'type'=>'top','titre'=>'Agenda','mainmenu'=>'agenda','leftmenu'=>'0','url'=>'/comm/action/index.php','langs'=>'commercial','position'=>100,'perms'=>'$user->rights->agenda->myactions->read','target'=>'','user'=>0); + $this->menu[$r]=array('fk_menu'=>0, + 'type'=>'top', + 'titre'=>'Agenda', + 'mainmenu'=>'agenda', + 'leftmenu'=>'0', + 'url'=>'/comm/action/index.php', + 'langs'=>'commercial', + 'position'=>100, + 'perms'=>'$user->rights->agenda->myactions->read', + 'target'=>'', + 'user'=>0, + 'constraint'=>'$conf->agenda->enabled'); $r++; // Exports diff --git a/htdocs/includes/modules/modCashDesk.class.php b/htdocs/includes/modules/modCashDesk.class.php index 9bb247c762c..936bd5e0f21 100644 --- a/htdocs/includes/modules/modCashDesk.class.php +++ b/htdocs/includes/modules/modCashDesk.class.php @@ -103,7 +103,8 @@ class modCashDesk extends DolibarrModules 'position'=>100, 'perms'=>1, // Use 'perms'=>'1' if you want your menu with no permission rules 'target'=>'', - 'user'=>0); // 0=Menu for internal users, 1=external users, 2=both + 'user'=>0, // 0=Menu for internal users, 1=external users, 2=both + 'constraint'=>'$conf->cashdesk->enabled'); //Constraint $r++; // This is to declare a Left Menu entry: diff --git a/htdocs/includes/modules/modECM.class.php b/htdocs/includes/modules/modECM.class.php index 8301dfd5fe4..dadb820ff37 100644 --- a/htdocs/includes/modules/modECM.class.php +++ b/htdocs/includes/modules/modECM.class.php @@ -132,7 +132,8 @@ class modECM extends DolibarrModules 'position'=>100, 'perms'=>'$user->rights->ecm->download || $user->rights->ecm->upload || $user->rights->ecm->setup', 'target'=>'', - 'user'=>0); + 'user'=>0, + 'constraint'=>'$conf->ecm->enabled'); $r++; // Left menu linked to top menu diff --git a/htdocs/includes/modules/modPhenix.class.php b/htdocs/includes/modules/modPhenix.class.php index 32136bb1488..4272e36cf85 100644 --- a/htdocs/includes/modules/modPhenix.class.php +++ b/htdocs/includes/modules/modPhenix.class.php @@ -98,7 +98,18 @@ class modPhenix extends DolibarrModules //------ $r=0; - $this->menu[$r]=array('fk_menu'=>0,'type'=>'top','titre'=>'Calendar','mainmenu'=>'phenix','leftmenu'=>'1','url'=>'/phenix/phenix.php','langs'=>'other','position'=>100,'perms'=>'','target'=>'','user'=>0); + $this->menu[$r]=array('fk_menu'=>0, + 'type'=>'top', + 'titre'=>'Calendar', + 'mainmenu'=>'phenix', + 'leftmenu'=>'1', + 'url'=>'/phenix/phenix.php', + 'langs'=>'other', + 'position'=>100, + 'perms'=>'', + 'target'=>'', + 'user'=>0, + 'constraint'=>'$conf->phenix->enabled'); $r++; }