From e3e4f0590c768673302165039107ee3c93ec19d3 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Mon, 11 Jan 2021 11:36:19 +0100 Subject: [PATCH] FIX : get and delete linked items with association tables methods created in common object --- htdocs/core/class/commonobject.class.php | 50 +++++++++++++++++++ .../class/workstationresource.class.php | 23 +-------- .../class/workstationusergroup.class.php | 23 +-------- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index c8994d32a1d..57f2364e42f 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -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 * diff --git a/htdocs/workstation/class/workstationresource.class.php b/htdocs/workstation/class/workstationresource.class.php index cfb6706058d..21398460eca 100644 --- a/htdocs/workstation/class/workstationresource.class.php +++ b/htdocs/workstation/class/workstationresource.class.php @@ -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); } } diff --git a/htdocs/workstation/class/workstationusergroup.class.php b/htdocs/workstation/class/workstationusergroup.class.php index 6b6bcb5cb4c..b45d24c7c60 100644 --- a/htdocs/workstation/class/workstationusergroup.class.php +++ b/htdocs/workstation/class/workstationusergroup.class.php @@ -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); } }