From 6fa38973ebc2f0f60bee7ef72490b1d2cf7869f5 Mon Sep 17 00:00:00 2001 From: MyuTsu Date: Wed, 11 Dec 2024 10:39:31 +0100 Subject: [PATCH 1/4] Fix: API issue preventing multi-field updates --- inc/container.class.php | 113 +++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 01957572..cbb27a84 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1635,19 +1635,19 @@ public static function preItemUpdate(CommonDBTM $item) { self::preItem($item); if (array_key_exists('_plugin_fields_data', $item->input)) { - $data = $item->input['_plugin_fields_data']; - //update data - $container = new self(); - if ( - count($data) == 0 - || $container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction'])) - ) { - $item->input['date_mod'] = $_SESSION['glpi_currenttime']; - - return true; + foreach ($item->input['_plugin_fields_data'] as $containerClass) { + $data = $containerClass; + //update data + $container = new self(); + if ( + count($data) == 0 + || $container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction'])) + ) { + $item->input['date_mod'] = $_SESSION['glpi_currenttime']; + } else { + return $item->input = []; + } } - - return $item->input = []; } return true; @@ -1663,9 +1663,14 @@ public static function preItemUpdate(CommonDBTM $item) */ public static function preItem(CommonDBTM $item) { + /** @var DBmysql $DB */ + global $DB; + + $c_id = []; + $loc_c = new PluginFieldsContainer(); //find container (if not exist, do nothing) if (isset($_REQUEST['c_id'])) { - $c_id = $_REQUEST['c_id']; + $c_id = [$_REQUEST['c_id']]; } else { $type = 'dom'; if (isset($_REQUEST['_plugin_fields_type'])) { @@ -1675,57 +1680,67 @@ public static function preItem(CommonDBTM $item) if ($type == 'domtab') { $subtype = $_REQUEST['_plugin_fields_subtype']; } - if (false === ($c_id = self::findContainer(get_Class($item), $type, $subtype))) { - // tries for 'tab' - if (false === ($c_id = self::findContainer(get_Class($item)))) { - return false; + foreach ($item->input as $key => $value) { + $container_find = false; + $container_id = self::findContainer(get_class($item), $type, $subtype); + if ($container_id === false) { + $container_id = self::findContainer(get_class($item)); + if ($container_id === false) { + return false; + } + $container_find = true; + } + $loc_c->getFromDB($container_id); + if (!$DB->fieldExists(static::getTable(), $key)) { + if (!$container_find) { + $container_id = self::findContainer(get_class($item)); + if ($container_id === false) { + return false; + } + $container_find = true; + } + } + if (!in_array($container_id, $c_id, true)) { + $c_id[] = $container_id; } } } - $loc_c = new PluginFieldsContainer(); - $loc_c->getFromDB($c_id); - // check rights on $c_id + foreach ($c_id as $container_id) { + $loc_c->getFromDB($container_id); - if (isset($_SESSION['glpiactiveprofile']['id']) && $_SESSION['glpiactiveprofile']['id'] != null && $c_id > 0) { - $right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $c_id); - if (($right > READ) === false) { + if (isset($_SESSION['glpiactiveprofile']['id']) && $_SESSION['glpiactiveprofile']['id'] != null && $container_id > 0) { + $right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $container_id); + if (($right > READ) === false) { + return; + } + } else { return; } - } else { - return; - } + // need to check if container is usable on this object entity + $entities = [$loc_c->fields['entities_id']]; + if ($loc_c->fields['is_recursive']) { + $entities = getSonsOf(getTableForItemType('Entity'), $loc_c->fields['entities_id']); + } - // need to check if container is usable on this object entity - $entities = [$loc_c->fields['entities_id']]; - if ($loc_c->fields['is_recursive']) { - $entities = getSonsOf(getTableForItemType('Entity'), $loc_c->fields['entities_id']); - } - - //workaround: when a ticket is created from readdonly profile, - //it is not initialized; see https://github.com/glpi-project/glpi/issues/1438 - if (!isset($item->fields) || count($item->fields) == 0) { - $item->fields = $item->input; - } - - if ($item->isEntityAssign() && !in_array($item->getEntityID(), $entities)) { - return false; - } + if ($item->isEntityAssign() && !in_array($item->getEntityID(), $entities)) { + return false; + } - if (false !== ($data = self::populateData($c_id, $item))) { - if (self::validateValues($data, $item::getType(), isset($_REQUEST['massiveaction'])) === false) { - $item->input = []; + $populate_data = self::populateData($container_id, $item); + if (false !== $populate_data) { + if (self::validateValues($populate_data, $item::getType(), isset($_REQUEST['massiveaction'])) === false) { + $item->input = []; - return []; + return []; + } + $item->input['_plugin_fields_data'][] = $populate_data; } - $item->input['_plugin_fields_data'] = $data; - - return $data; } - return; + return $item->input['_plugin_fields_data']; } /** From cce4fc07fbb9e09983adf2fdb2f87a4796b10f9b Mon Sep 17 00:00:00 2001 From: MyuTsu Date: Wed, 11 Dec 2024 11:15:12 +0100 Subject: [PATCH 2/4] refactor preItem --- inc/container.class.php | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index cbb27a84..53e7995b 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1681,24 +1681,13 @@ public static function preItem(CommonDBTM $item) $subtype = $_REQUEST['_plugin_fields_subtype']; } foreach ($item->input as $key => $value) { - $container_find = false; $container_id = self::findContainer(get_class($item), $type, $subtype); - if ($container_id === false) { + $container_is_find = $loc_c->getFromDB($container_id); + if (!$DB->fieldExists(static::getTable(), $key) || !$container_is_find) { $container_id = self::findContainer(get_class($item)); if ($container_id === false) { return false; } - $container_find = true; - } - $loc_c->getFromDB($container_id); - if (!$DB->fieldExists(static::getTable(), $key)) { - if (!$container_find) { - $container_id = self::findContainer(get_class($item)); - if ($container_id === false) { - return false; - } - $container_find = true; - } } if (!in_array($container_id, $c_id, true)) { $c_id[] = $container_id; From 76afc667143f13642c408a17ac0bbae0a3f64682 Mon Sep 17 00:00:00 2001 From: MyuTsu Date: Wed, 11 Dec 2024 13:50:37 +0100 Subject: [PATCH 3/4] refactor --- inc/container.class.php | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 53e7995b..ca508c01 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1635,19 +1635,17 @@ public static function preItemUpdate(CommonDBTM $item) { self::preItem($item); if (array_key_exists('_plugin_fields_data', $item->input)) { - foreach ($item->input['_plugin_fields_data'] as $containerClass) { - $data = $containerClass; - //update data + foreach ($item->input['_plugin_fields_data'] as $container_class) { + //update container_class $container = new self(); if ( - count($data) == 0 - || $container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction'])) + count($container_class) !== 0 + && !$container->updateFieldsValues($container_class, $item->getType(), isset($_REQUEST['massiveaction'])) ) { - $item->input['date_mod'] = $_SESSION['glpi_currenttime']; - } else { return $item->input = []; } } + $item->input['date_mod'] = $_SESSION['glpi_currenttime']; } return true; @@ -1666,11 +1664,11 @@ public static function preItem(CommonDBTM $item) /** @var DBmysql $DB */ global $DB; - $c_id = []; - $loc_c = new PluginFieldsContainer(); + $container_ids = []; + $field_container = new PluginFieldsContainer(); //find container (if not exist, do nothing) if (isset($_REQUEST['c_id'])) { - $c_id = [$_REQUEST['c_id']]; + $container_ids = [$_REQUEST['c_id']]; } else { $type = 'dom'; if (isset($_REQUEST['_plugin_fields_type'])) { @@ -1681,23 +1679,21 @@ public static function preItem(CommonDBTM $item) $subtype = $_REQUEST['_plugin_fields_subtype']; } foreach ($item->input as $key => $value) { - $container_id = self::findContainer(get_class($item), $type, $subtype); - $container_is_find = $loc_c->getFromDB($container_id); - if (!$DB->fieldExists(static::getTable(), $key) || !$container_is_find) { - $container_id = self::findContainer(get_class($item)); - if ($container_id === false) { + if (!$DB->fieldExists(static::getTable(), $key) || false === ($container_id = self::findContainer(get_Class($item), $type, $subtype))) { + // tries for 'tab' + if (false === ($container_id = self::findContainer(get_Class($item)))) { return false; } } - if (!in_array($container_id, $c_id, true)) { - $c_id[] = $container_id; + if (!in_array($container_id, $container_ids, true)) { + $container_ids[] = $container_id; } } } // check rights on $c_id - foreach ($c_id as $container_id) { - $loc_c->getFromDB($container_id); + foreach ($container_ids as $container_id) { + $field_container->getFromDB($container_id); if (isset($_SESSION['glpiactiveprofile']['id']) && $_SESSION['glpiactiveprofile']['id'] != null && $container_id > 0) { $right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $container_id); @@ -1709,9 +1705,9 @@ public static function preItem(CommonDBTM $item) } // need to check if container is usable on this object entity - $entities = [$loc_c->fields['entities_id']]; - if ($loc_c->fields['is_recursive']) { - $entities = getSonsOf(getTableForItemType('Entity'), $loc_c->fields['entities_id']); + $entities = [$field_container->fields['entities_id']]; + if ($field_container->fields['is_recursive']) { + $entities = getSonsOf(getTableForItemType('Entity'), $field_container->fields['entities_id']); } if ($item->isEntityAssign() && !in_array($item->getEntityID(), $entities)) { From 031530ac3611526fe77ccd8867beec8e327b6b8c Mon Sep 17 00:00:00 2001 From: MyuTsu Date: Wed, 11 Dec 2024 15:00:18 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecb1b205..f3d9ab6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed -- Fix `container` to prevent calls from `API` returning full container data +- Fixed a problem with fields being updated if there are several containers on the object. ## [1.21.15] - 2024-10-09