Skip to content

Commit

Permalink
Fix(Field): do not destroy dropdown if used by another container (#868)
Browse files Browse the repository at this point in the history
* Fix(Field): do not destroy dropdown if used by another container
  • Loading branch information
stonebuzz authored Dec 13, 2024
1 parent e48b52e commit 53c66e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 53c66e1

Please sign in to comment.