Skip to content

Commit 53c66e1

Browse files
authored
Fix(Field): do not destroy dropdown if used by another container (#868)
* Fix(Field): do not destroy dropdown if used by another container
1 parent e48b52e commit 53c66e1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [UNRELAESE]
99

10+
### Fixed
11+
- Do not destroy the dropdown table/class if it is being used by another container.
12+
1013
## [1.21.16] - 2024-11-12
1114

1215
### Fixed

inc/field.class.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ private function cleanDisplayPreferences($itemtype, $so_id)
343343
// phpcs:ignore PSR1.Methods.CamelCapsMethodName
344344
public function pre_deleteItem()
345345
{
346+
/**
347+
* @var \DBmysql $DB
348+
*/
349+
global $DB;
350+
346351
//retrieve search option ID to clean DiplayPreferences
347352
$container_obj = new PluginFieldsContainer();
348353
$container_obj->getFromDB($this->fields['plugin_fields_containers_id']);
@@ -389,7 +394,25 @@ public function pre_deleteItem()
389394
]);
390395

391396
if ($this->fields['type'] === 'dropdown') {
392-
return PluginFieldsDropdown::destroy($this->fields['name']);
397+
//load all container (except current one) and check if another use this fields
398+
$container_obj = new PluginFieldsContainer();
399+
$all_container = $container_obj->find(['id' => ['!=', $this->fields['plugin_fields_containers_id']]]);
400+
401+
$use_by_another = false;
402+
foreach ($all_container as $container_fields) {
403+
foreach (json_decode($container_fields['itemtypes']) as $itemtype) {
404+
$dropdown_classname = PluginFieldsDropdown::getClassname($this->fields['name']);
405+
$classname = PluginFieldsContainer::getClassname($itemtype, $container_fields['name']);
406+
$dropdown_fk = getForeignKeyFieldForItemType($dropdown_classname);
407+
if ($DB->fieldExists(getTableForItemType($classname), $dropdown_fk)) {
408+
$use_by_another = true;
409+
}
410+
}
411+
}
412+
413+
if (!$use_by_another) {
414+
return PluginFieldsDropdown::destroy($this->fields['name']);
415+
}
393416
}
394417

395418
return true;

0 commit comments

Comments
 (0)