New class UserGroup works with common functions now

This commit is contained in:
Maxime Kohlhaas 2020-04-13 19:03:48 +02:00
parent 2e004e93ea
commit b8a0b4b6e4
2 changed files with 37 additions and 189 deletions

View File

@ -110,6 +110,21 @@ class UserGroup extends CommonObject
'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'ModelPDF', 'enabled'=>1, 'visible'=>0, 'position'=>100),
);
/**
* @var int Field with ID of parent key if this field has a parent
*/
public $fk_element = 'fk_usergroup';
/**
* @var array List of child tables. To test if we can delete object.
*/
protected $childtables=array();
/**
* @var array List of child tables. To know object to delete on cascade.
*/
protected $childtablesoncascade = array('usergroup_rights','usergroup_user');
/**
* Constructor de la classe
@ -135,47 +150,23 @@ class UserGroup extends CommonObject
{
global $conf;
$sql = "SELECT g.rowid, g.entity, g.nom as name, g.note, g.datec, g.tms as datem";
$sql .= " FROM ".MAIN_DB_PREFIX."usergroup as g";
if ($groupname)
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
if (!empty($groupname))
{
$sql .= " WHERE g.nom = '".$this->db->escape($groupname)."'";
$result = $this->fetchCommon(0, '', ' AND nom = \''.$this->db->escape($groupname).'\'');
}
else
{
$sql .= " WHERE g.rowid = ".$id;
$result = $this->fetchCommon($id);
}
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
if($result)
{
if ($this->db->num_rows($result))
if ($load_members)
{
$obj = $this->db->fetch_object($result);
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
$this->entity = $obj->entity;
$this->name = $obj->name;
$this->nom = $obj->name; // Deprecated
$this->note = $obj->note;
$this->datec = $obj->datec;
$this->datem = $obj->datem;
if ($load_members)
$this->members = $this->listUsersForGroup();
// Retreive all extrafield
// fetch optionals attributes and labels
$this->fetch_optionals();
// Sav current LDAP Current DN
//$this->ldap_dn = $this->_load_ldap_dn($this->_load_ldap_info(),0);
$this->members = $this->listUsersForGroup();
}
$this->db->free($result);
return 1;
}
else
@ -647,50 +638,7 @@ class UserGroup extends CommonObject
*/
public function delete(User $user)
{
global $conf, $langs;
$error = 0;
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights";
$sql .= " WHERE fk_usergroup = ".$this->id;
$this->db->query($sql);
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
$sql .= " WHERE fk_usergroup = ".$this->id;
$this->db->query($sql);
// Remove extrafields
if ((!$error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
{
$result = $this->deleteExtraFields();
if ($result < 0)
{
$error++;
dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
}
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup";
$sql .= " WHERE rowid = ".$this->id;
$result = $this->db->query($sql);
if ($result)
{
// Call trigger
$result = $this->call_trigger('GROUP_DELETE', $user);
if ($result < 0) { $error++; $this->db->rollback(); return -1; }
// End call triggers
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
dol_print_error($this->db);
return -1;
}
return $this->deleteCommon($user);
}
/**
@ -701,67 +649,15 @@ class UserGroup extends CommonObject
*/
public function create($notrigger = 0)
{
global $user, $conf, $langs, $hookmanager;
global $user, $conf;
$error = 0;
$now = dol_now();
$this->datec = dol_now();
if (!isset($this->entity)) $this->entity = $conf->entity; // If not defined, we use default value
$entity = $this->entity;
if (!empty($conf->multicompany->enabled) && $conf->entity == 1) $entity = $this->entity;
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup (";
$sql .= "datec";
$sql .= ", nom";
$sql .= ", entity";
$sql .= ") VALUES (";
$sql .= "'".$this->db->idate($now)."'";
$sql .= ",'".$this->db->escape($this->nom)."'";
$sql .= ",".$this->db->escape($entity);
$sql .= ")";
dol_syslog(get_class($this)."::create", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."usergroup");
if ($this->update(1) < 0) return -2;
$action = 'create';
// Actions on extra fields (by external module or standard code)
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result = $this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (!$error && !$notrigger)
{
// Call trigger
$result = $this->call_trigger('GROUP_CREATE', $user);
if ($result < 0) { $error++; $this->db->rollback(); return -1; }
// End call triggers
}
if ($error > 0) { $error++; $this->db->rollback(); return -1; }
else $this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
$this->error = $this->db->lasterror();
return -1;
}
return $this->createCommon($user, $notrigger);
}
/**
@ -772,9 +668,7 @@ class UserGroup extends CommonObject
*/
public function update($notrigger = 0)
{
global $user, $conf, $langs, $hookmanager;
$error = 0;
global $user, $conf;
$entity = $conf->entity;
if (!empty($conf->multicompany->enabled) && $conf->entity == 1)
@ -782,55 +676,7 @@ class UserGroup extends CommonObject
$entity = $this->entity;
}
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."usergroup SET ";
$sql .= " nom = '".$this->db->escape($this->name)."'";
$sql .= ", entity = ".$this->db->escape($entity);
$sql .= ", note = '".$this->db->escape($this->note)."'";
$sql .= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$action = 'update';
// Actions on extra fields (by external module or standard code)
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result = $this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (!$error && !$notrigger)
{
// Call trigger
$result = $this->call_trigger('GROUP_MODIFY', $user);
if ($result < 0) { $error++; }
// End call triggers
}
if (!$error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -$error;
}
}
else
{
$this->db->rollback();
dol_print_error($this->db);
return -1;
}
return $this->updateCommon($user, $notrigger);
}

View File

@ -68,16 +68,14 @@ if (!empty($conf->multicompany->enabled) && $conf->entity > 1 && $conf->global->
}
$object = new Usergroup($db);
if ($id > 0)
{
$object->fetch($id);
$object->getrights();
}
$extrafields = new ExtraFields($db);
// fetch optionals attributes and labels
$extrafields->fetch_name_optionals_label($object->table_element);
// Load object
include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
$object->getrights();
// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
$hookmanager->initHooks(array('groupcard', 'globalcard'));
@ -325,6 +323,8 @@ else
{
if ($id)
{
$res = $object->fetch_optionals();
$head = group_prepare_head($object);
$title = $langs->trans("Group");
@ -375,6 +375,8 @@ else
print "</td></tr>\n";
}
unset($object->fields['nom']); // Name already displayed in banner
// Common attributes
$keyforbreak = '';
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';