diff --git a/CHANGELOG.md b/CHANGELOG.md index 429de175..eab06bb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [UNRELEASED] + +### Fixed + +- Fix `search option` for `multiple` dropdown + ## [1.21.23] - 2025-08-26 -- FIx undefined array key `multiple_dropdown_action` during import +### Fixed + +- Fix undefined array key `multiple_dropdown_action` during import - Fix incompatibility of `multiple` dropdowns with `massiveaction` - Fix default value properly applied in multiple dropdown search options - Fix `search option` for default values in `multiple` dropdown @@ -15,11 +23,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - The field name was empty in the GLPI logs ### Added + - Add `replace` and `add` options in massive action for the multiple dropdowns fields ## [1.21.22] - 2025-05-28 ### Fixed + - Fix condition check logic for dropdown field values - Fix validation for mandatory multiple dropdown - Fix `twig` error about undefined `dropdown_options` diff --git a/hook.php b/hook.php index a7c5699a..e1683f12 100644 --- a/hook.php +++ b/hook.php @@ -400,9 +400,9 @@ function plugin_fields_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype) $tablefield = "$table" . '_' . "$field"; switch ($searchtype) { case 'equals': - return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'notequals' : 'equals', $itemtype, $field_field); + return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'notequals' : 'equals', $field_field); case 'notequals': - return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'equals' : 'notequals', $itemtype, $field_field); + return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'equals' : 'notequals', $field_field); } } else { // if 'multiple' field with cleaned name is found -> 'dropdown' case @@ -420,9 +420,9 @@ function plugin_fields_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype) ) { switch ($searchtype) { case 'equals': - return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'notequals' : 'equals', $itemtype, $field_field); + return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'notequals' : 'equals', $field_field); case 'notequals': - return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'equals' : 'notequals', $itemtype, $field_field); + return PluginFieldsDropdown::multipleDropdownAddWhere($link, $tablefield, $field, $val, $nott ? 'equals' : 'notequals', $field_field); } } else { return false; diff --git a/inc/dropdown.class.php b/inc/dropdown.class.php index 926b670a..02d092d4 100644 --- a/inc/dropdown.class.php +++ b/inc/dropdown.class.php @@ -255,32 +255,26 @@ public static function getClassname($system_name) return 'PluginFields' . ucfirst($system_name) . 'Dropdown'; } - public static function multipleDropdownAddWhere($link, $tablefield, $field, $val, $searchtype, $itemtype, $field_field) + public static function multipleDropdownAddWhere($link, $tablefield, $field, $val, $searchtype, $field_field = []) { /** @var \DBmysql $DB */ global $DB; - $default_value_request = ""; - - if ($field_field->fields['default_value'] === '["' . $val . '"]') { - switch ($searchtype) { - case 'equals': - $default_value_request = ' OR (' . $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName('itemtype') . ' IS NULL' . - ' AND ' . $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName('items_id') . ' IS NULL)'; - break; - case 'notequals': - $default_value_request = ' AND (' . $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName('itemtype') . '=' . $DB->quoteValue($itemtype) . - ' OR ' . $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName('items_id') . '=' . $itemtype::getTable() . '.id)'; - break; - } - } + // Determines the default value + $default_value = $field_field->fields['default_value'] ?? ''; + $default_value = ($default_value == '[]') ? '' : $DB->quoteValue($default_value); - switch ($searchtype) { - case 'equals': - return $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName($field) . ' LIKE ' . $DB->quoteValue("%\"$val\"%") . $default_value_request; - case 'notequals': - return $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName($field) . ' NOT LIKE ' . $DB->quoteValue("%\"$val\"%") . - ' OR ' . $link . $DB->quoteName($tablefield) . '.' . $DB->quoteName($field) . ' IS NULL' . $default_value_request; - } + // Constructs the SQL query base + $quotedField = $DB->quoteName($tablefield) . '.' . $DB->quoteName($field); + $sqlBase = $link . ' COALESCE(' . $quotedField . ', ' . $default_value . ')'; + + // Handles “equals” and “notequals” cases in a unified manner + $operator = ($searchtype === 'equals') ? '' : 'NOT '; + $condition = ($val == '0') + ? $operator . 'IN("", "[]")' + : $operator . 'LIKE ' . $DB->quoteValue("%\"$val\"%"); + + return $sqlBase . ' ' . $condition; } + }