FIX : get and delete linked items with association tables methods created in common object

This commit is contained in:
Gauthier PC portable 024 2021-01-11 11:36:19 +01:00
parent 8dfb088fc5
commit e3e4f0590c
3 changed files with 54 additions and 42 deletions

View File

@ -3660,6 +3660,56 @@ abstract class CommonObject
}
}
/**
* Function used to get an array with all items linked to an object id in association table
* @param int $fk_object_where id of object we need to get linked items
* @param string $field_select name of field we need to get a list
* @param string $field_where name of field of object we need to get linked items
* @param string $table_element name of association table
* @return array
*/
static public function getAllItemsLinkedByObjectID($fk_object_where, $field_select, $field_where, $table_element)
{
if(empty($fk_object_where) || empty($field_where) || empty($table_element)) return -1;
global $db;
$sql = 'SELECT '.$field_select.' FROM '.MAIN_DB_PREFIX.$table_element.' WHERE '.$field_where.' = '.$fk_object_where;
$resql = $db->query($sql);
$TRes = array();
if (!empty($resql)) {
while ($res = $db->fetch_object($resql)) {
$TRes[] = $res->{$field_select};
}
}
return $TRes;
}
/**
* Function used to remove all items linked to an object id in association table
* @param int $fk_object_where id of object we need to remove linked items
* @param string $field_where name of field of object we need to delete linked items
* @param string $table_element name of association table
* @return int
*/
static public function deleteAllItemsLinkedByObjectID($fk_object_where, $field_where, $table_element)
{
if(empty($fk_object_where) || empty($field_where) || empty($table_element)) return -1;
global $db;
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$table_element.' WHERE '.$field_where.' = '.$fk_object_where;
$resql = $db->query($sql);
if (empty($resql)) return 0;
return 1;
}
/**
* Set status of an object
*

View File

@ -79,21 +79,9 @@ class WorkstationResource extends CommonObject
*/
static public function getAllResourcesOfWorkstation($fk_workstation)
{
global $db;
$obj = new self($db);
$sql = 'SELECT fk_resource FROM '.MAIN_DB_PREFIX.$obj->table_element.' WHERE fk_workstation = '.$fk_workstation;
$resql = $db->query($sql);
$TRes = array();
if (!empty($resql)) {
while ($res = $db->fetch_object($resql)) {
$TRes[] = $res->fk_resource;
}
}
return $TRes;
return parent::getAllItemsLinkedByObjectID($fk_workstation, 'fk_resource', 'fk_workstation', $obj->table_element);
}
/**
@ -103,15 +91,8 @@ class WorkstationResource extends CommonObject
*/
static public function deleteAllResourcesOfWorkstation($fk_workstation)
{
global $db;
$obj = new self($db);
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$obj->table_element.' WHERE fk_workstation = '.$fk_workstation;
$resql = $db->query($sql);
if (empty($resql)) return 0;
return 1;
return parent::deleteAllItemsLinkedByObjectID($fk_workstation, 'fk_workstation', $obj->table_element);
}
}

View File

@ -79,21 +79,9 @@ class WorkstationUserGroup extends CommonObject
*/
static public function getAllGroupsOfWorkstation($fk_workstation)
{
global $db;
$obj = new self($db);
$sql = 'SELECT fk_usergroup FROM '.MAIN_DB_PREFIX.$obj->table_element.' WHERE fk_workstation = '.$fk_workstation;
$resql = $db->query($sql);
$TRes = array();
if (!empty($resql)) {
while ($res = $db->fetch_object($resql)) {
$TRes[] = $res->fk_usergroup;
}
}
return $TRes;
return parent::getAllItemsLinkedByObjectID($fk_workstation, 'fk_usergroup', 'fk_workstation', $obj->table_element);
}
/**
@ -103,15 +91,8 @@ class WorkstationUserGroup extends CommonObject
*/
static public function deleteAllGroupsOfWorkstation($fk_workstation)
{
global $db;
$obj = new self($db);
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$obj->table_element.' WHERE fk_workstation = '.$fk_workstation;
$resql = $db->query($sql);
if (empty($resql)) return 0;
return 1;
return parent::deleteAllItemsLinkedByObjectID($fk_workstation, 'fk_workstation', $obj->table_element);
}
}