Skip to content

Commit

Permalink
fix: multiple dropdown fields emptied when solution added (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rom1-B authored Jun 11, 2024
1 parent 108a1e5 commit dd603e7
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ public function prepareInputForAdd($input)
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
foreach (json_decode($founditemtypes) as $founditemtype) {
if (in_array($founditemtype, $input['itemtypes'])) {
Session::AddMessageAfterRedirect(__("You cannot add several blocks with type 'Insertion in the form' on same object", "fields"), false, ERROR);
return false;
Session::AddMessageAfterRedirect(__("You cannot add several blocks with type 'Insertion in the form' on same object", "fields"), false, ERROR);
return false;
}
}
}
Expand All @@ -572,8 +572,8 @@ public function prepareInputForAdd($input)
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
foreach (json_decode($founditemtypes) as $founditemtype) {
if (in_array($founditemtype, $input['itemtypes'])) {
Session::AddMessageAfterRedirect(__("You cannot add several blocks with type 'Insertion in the form of a specific tab' on same object tab", "fields"), false, ERROR);
return false;
Session::AddMessageAfterRedirect(__("You cannot add several blocks with type 'Insertion in the form of a specific tab' on same object tab", "fields"), false, ERROR);
return false;
}
}
}
Expand Down Expand Up @@ -725,7 +725,7 @@ public function pre_deleteItem()

//delete table
if (class_exists($classname)) {
$classname::uninstall();
$classname::uninstall();
} else {
//class does not exists; try to remove any existing table
$tablename = getTableForItemType($classname);
Expand Down Expand Up @@ -964,7 +964,7 @@ public static function showFormSubtype($params, $display = false)
foreach ($tabs as &$value) {
$results = [];
if (preg_match_all('#<sup.*>(.+)</sup>#', $value, $results)) {
$value = str_replace($results[0][0], "", $value);
$value = str_replace($results[0][0], "", $value);
}
}

Expand Down Expand Up @@ -1485,9 +1485,9 @@ public static function validateValues($data, $itemtype, $massiveaction)
}
} elseif (isset($data[$name])) {
$value = $data[$name];
} else if (isset($data['plugin_fields_' . $name . 'dropdowns_id'])) {
} elseif (isset($data['plugin_fields_' . $name . 'dropdowns_id'])) {
$value = $data['plugin_fields_' . $name . 'dropdowns_id'];
} else if ($field['mandatory'] == 1 && isset($data['items_id'])) {
} elseif ($field['mandatory'] == 1 && isset($data['items_id'])) {
$tablename = getTableForItemType(self::getClassname($itemtype, $container->fields['name']));

$iterator = $DB->request([
Expand All @@ -1502,7 +1502,7 @@ public static function validateValues($data, $itemtype, $massiveaction)
$db_result = $iterator->current();
if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) {
$value = $db_result['plugin_fields_' . $name . 'dropdowns_id'];
} else if (isset($db_result[$name])) {
} elseif (isset($db_result[$name])) {
$value = $db_result[$name];
}
} else {
Expand All @@ -1528,11 +1528,11 @@ public static function validateValues($data, $itemtype, $massiveaction)
) {
$empty_errors[] = $field['label'];
$valid = false;
} else if ($field['type'] == 'number' && !empty($value) && !is_numeric($value)) {
} elseif ($field['type'] == 'number' && !empty($value) && !is_numeric($value)) {
// Check number fields
$number_errors[] = $field['label'];
$valid = false;
} else if ($field['type'] == 'url' && !empty($value)) {
} elseif ($field['type'] == 'url' && !empty($value)) {
if (filter_var($value, FILTER_VALIDATE_URL) === false) {
$url_errors[] = $field['label'];
$valid = false;
Expand Down Expand Up @@ -1741,7 +1741,7 @@ public static function preItem(CommonDBTM $item)
*/
private static function populateData($c_id, CommonDBTM $item)
{
//find fields associated to found container
//find fields associated to found container
$field_obj = new PluginFieldsField();
$fields = $field_obj->find(
[
Expand All @@ -1751,10 +1751,10 @@ private static function populateData($c_id, CommonDBTM $item)
"ranking"
);

//prepare data to update
//prepare data to update
$data = ['plugin_fields_containers_id' => $c_id];
if (!$item->isNewItem()) {
//no ID yet while creating
//no ID yet while creating
$data['items_id'] = $item->getID();
}

Expand Down Expand Up @@ -1785,15 +1785,15 @@ private static function populateData($c_id, CommonDBTM $item)
}

if (isset($item->input[$field['name']])) {
//standard field
//standard field
$input = $field['name'];
} else {
//dropdown field
//dropdown field
$input = "plugin_fields_" . $field['name'] . "dropdowns_id";
}
if (isset($item->input[$input])) {
$has_fields = true;
// Before is_number check, help user to have a number correct, during a massive action of a number field
// Before is_number check, help user to have a number correct, during a massive action of a number field
if ($field['type'] == 'number') {
$item->input[$input] = str_replace(",", ".", $item->input[$input]);
}
Expand All @@ -1813,16 +1813,21 @@ private static function populateData($c_id, CommonDBTM $item)
// ex my_dom[]
//in these conditions, the input is never sent by the browser
if ($field['multiple']) {
//handle multi dropodwn field
//handle multi dropdown field
if ($field['type'] == 'dropdown') {
$multiple_key = sprintf('plugin_fields_%sdropdowns_id', $field['name']);
$multiple_key_defined = '_' . $multiple_key . '_defined';
//values are defined by user
if (isset($item->input[$multiple_key])) {
$data[$multiple_key] = $item->input[$multiple_key];
} else { //multi dropdown is empty or has been emptied
$has_fields = true;
} elseif (
isset($item->input[$multiple_key_defined])
&& $item->input[$multiple_key_defined]
) { //multi dropdown is empty or has been emptied
$data[$multiple_key] = [];
$has_fields = true;
}
$has_fields = true;
}

//managed multi GLPI item dropdown field
Expand Down

0 comments on commit dd603e7

Please sign in to comment.