From 53c66e1e6a97dfe85161db67268e81325b0a81d1 Mon Sep 17 00:00:00 2001 From: Stanislas Date: Fri, 13 Dec 2024 15:58:35 +0100 Subject: [PATCH] Fix(Field): do not destroy dropdown if used by another container (#868) * Fix(Field): do not destroy dropdown if used by another container --- CHANGELOG.md | 3 +++ inc/field.class.php | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fdb30c4..ecb1b205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [UNRELAESE] +### Fixed +- Do not destroy the dropdown table/class if it is being used by another container. + ## [1.21.16] - 2024-11-12 ### Fixed diff --git a/inc/field.class.php b/inc/field.class.php index 8e32845d..c0b82967 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -343,6 +343,11 @@ private function cleanDisplayPreferences($itemtype, $so_id) // phpcs:ignore PSR1.Methods.CamelCapsMethodName public function pre_deleteItem() { + /** + * @var \DBmysql $DB + */ + global $DB; + //retrieve search option ID to clean DiplayPreferences $container_obj = new PluginFieldsContainer(); $container_obj->getFromDB($this->fields['plugin_fields_containers_id']); @@ -389,7 +394,25 @@ public function pre_deleteItem() ]); if ($this->fields['type'] === 'dropdown') { - return PluginFieldsDropdown::destroy($this->fields['name']); + //load all container (except current one) and check if another use this fields + $container_obj = new PluginFieldsContainer(); + $all_container = $container_obj->find(['id' => ['!=', $this->fields['plugin_fields_containers_id']]]); + + $use_by_another = false; + foreach ($all_container as $container_fields) { + foreach (json_decode($container_fields['itemtypes']) as $itemtype) { + $dropdown_classname = PluginFieldsDropdown::getClassname($this->fields['name']); + $classname = PluginFieldsContainer::getClassname($itemtype, $container_fields['name']); + $dropdown_fk = getForeignKeyFieldForItemType($dropdown_classname); + if ($DB->fieldExists(getTableForItemType($classname), $dropdown_fk)) { + $use_by_another = true; + } + } + } + + if (!$use_by_another) { + return PluginFieldsDropdown::destroy($this->fields['name']); + } } return true;