From af9ca0320c81b454ca08a06746935b5d86f237fb Mon Sep 17 00:00:00 2001 From: Aeto-J <109135726+Aeto-J@users.noreply.github.com> Date: Fri, 18 Nov 2022 11:47:46 +0100 Subject: [PATCH] Handle dropdowns default value --- ajax/field_specific_fields.php | 126 +++++++++++++++++++++++++++++++++ inc/field.class.php | 115 ++++++++++++------------------ 2 files changed, 173 insertions(+), 68 deletions(-) create mode 100644 ajax/field_specific_fields.php diff --git a/ajax/field_specific_fields.php b/ajax/field_specific_fields.php new file mode 100644 index 00000000..60b50ff2 --- /dev/null +++ b/ajax/field_specific_fields.php @@ -0,0 +1,126 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2022 by Fields plugin team. + * @copyright 2015-2022 Teclib' and contributors. + * @copyright 2003-2014 by the INDEPNET Development Team. + * @licence https://www.gnu.org/licenses/gpl-3.0.html + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +include('../../../inc/includes.php'); +header("Content-Type: text/html; charset=UTF-8"); +Html::header_nocache(); +Session::checkLoginUser(); + +$id = $_POST['id']; +$type = $_POST['type']; +$rand = $_POST['rand']; + +$field = new PluginFieldsField(); +if ($id > 0) { + $field->getFromDB($id); +} else { + $field->getEmpty(); +} + +if ($type === 'glpi_item') { + // Display correct label + echo Html::scriptBlock(<<isNewItem()) { + Dropdown::showFromArray('allowed_values', PluginFieldsToolbox::getGlpiItemtypes(), [ + 'display_emptychoice' => true, + 'multiple' => true + ]); + } else { + $allowed_itemtypes = !empty($field->fields['allowed_values']) + ? json_decode($field->fields['allowed_values']) + : []; + echo implode( + ', ', + array_map( + function ($itemtype) { + return is_a($itemtype, CommonDBTM::class, true) + ? $itemtype::getTypeName(Session::getPluralNumber()) + : $itemtype; + }, + $allowed_itemtypes + ) + ); + } +} else { + // Display correct label + echo Html::scriptBlock(<< 'default_value', + 'value' => $field->fields['default_value'], + 'entity_restrict' => -1, + 'rand' => $rand, + ] + ); + } elseif ($type == 'dropdown') { + if ($field->isNewItem()) { + echo ''; + echo __s('Default value will be configurable once field will be created.', 'fields'); + echo ''; + } else { + Dropdown::show( + PluginFieldsDropdown::getClassname($field->fields['name']), + [ + 'name' => 'default_value', + 'value' => $field->fields['default_value'], + 'entity_restrict' => -1, + 'rand' => $rand, + ] + ); + } + } else { + echo Html::input( + 'default_value', + [ + 'value' => $field->fields['default_value'], + ] + ); + } + if (in_array($type, ['date', 'datetime'])) { + echo ''; + } +} diff --git a/inc/field.class.php b/inc/field.class.php index 82e85540..4b387377 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -426,7 +426,17 @@ public function showSummary($container) echo "getID()}'>{$this->fields['label']}"; echo ""; echo "" . $fields_type[$this->fields['type']] . ""; - echo "" . $this->fields['default_value'] . ""; + echo "" ; + if (preg_match('/^dropdown-.+/', $this->fields['type'])) { + $table = getTableForItemType(preg_replace('/^dropdown-/', '', $this->fields['type'])); + echo Dropdown::getDropdownName($table, $this->fields["default_value"]); + } elseif ($this->fields['type'] === 'dropdown') { + $table = getTableForItemType(PluginFieldsDropdown::getClassname($this->fields['name'])); + echo Dropdown::getDropdownName($table, $this->fields["default_value"]); + } else { + echo $this->fields['default_value']; + } + echo ""; echo "" . Dropdown::getYesNo($this->fields["mandatory"]) . ""; echo ""; echo ($this->isActive()) @@ -457,8 +467,6 @@ public function showSummary($container) public function showForm($ID, $options = []) { - global $CFG_GLPI; - $rand = mt_rand(); if (isset($options['parent_id']) && !empty($options['parent_id'])) { @@ -503,37 +511,12 @@ public function showForm($ID, $options = []) if ($edit) { echo self::getTypes(true)[$this->fields['type']]; } else { - // if glpi_item selected display dropdown and hide input default_value - echo Html::scriptBlock(<< $this->fields['type'], - 'on_change' => 'plugin_fields_change_field_type_' . $rand . '(this.value)', + 'value' => $this->fields['type'], + 'rand' => $rand, ] ); } @@ -552,48 +535,44 @@ function () { echo ''; echo ""; echo ""; - echo '
'; - echo Html::input( - 'default_value', - [ - 'value' => $this->fields['default_value'], - ] - ); - if ($this->fields["type"] == "dropdown") { - echo ' - ' . __('Configure', 'fields') . ' - '; - } - if (in_array($this->fields['type'], ['date', 'datetime'])) { - echo ""; - } + echo '
'; echo '
'; - echo '
'; - if (!$edit) { - Dropdown::showFromArray('allowed_values', PluginFieldsToolbox::getGlpiItemtypes(), [ - 'display_emptychoice' => true, - 'multiple' => true - ]); + if ($edit) { + $load_params = json_encode( + [ + 'id' => $ID, + 'type' => $this->fields['type'], + 'rand' => $rand, + ] + ); + echo Html::scriptBlock(<<fields['allowed_values']) - ? json_decode($this->fields['allowed_values']) - : []; - echo implode( - ', ', - array_map( - function ($itemtype) { - return is_a($itemtype, CommonDBTM::class, true) - ? $itemtype::getTypeName(Session::getPluralNumber()) - : $itemtype; - }, - $allowed_itemtypes - ) + Ajax::updateItemOnSelectEvent( + "dropdown_type$rand", + "plugin_fields_specific_fields_$rand", + "../ajax/field_specific_fields.php", + [ + 'id' => $ID, + 'type' => '__VALUE__', + 'rand' => $rand, + ] + ); + echo Html::scriptBlock(<<'; echo ""; echo "";