Skip to content

Commit

Permalink
Handle dropdowns default value
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeto-J authored Nov 18, 2022
1 parent d6652ba commit af9ca03
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 68 deletions.
126 changes: 126 additions & 0 deletions ajax/field_specific_fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Fields is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @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(<<<JAVASCRIPT
$('#plugin_fields_default_value_label_{$rand}').hide();
$('#plugin_fields_allowed_values_label_{$rand}').show();
JAVASCRIPT
);

// Display "allowed values" field
if ($field->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(<<<JAVASCRIPT
$('#plugin_fields_default_value_label_{$rand}').show();
$('#plugin_fields_allowed_values_label_{$rand}').hide();
JAVASCRIPT
);

// Display "default values" field
if (preg_match('/^dropdown-.+/', $type) === 1) {
Dropdown::show(
preg_replace('/^dropdown-/', '', $type),
[
'name' => 'default_value',
'value' => $field->fields['default_value'],
'entity_restrict' => -1,
'rand' => $rand,
]
);
} elseif ($type == 'dropdown') {
if ($field->isNewItem()) {
echo '<em class="form-control-plaintext">';
echo __s('Default value will be configurable once field will be created.', 'fields');
echo '</em>';
} 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 '<i class="pointer fa fa-info" title="' . __s("You can use 'now' for date and datetime field") . '"></i>';
}
}
115 changes: 47 additions & 68 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,17 @@ public function showSummary($container)
echo "<a href='" . Plugin::getWebDir('fields') . "/front/field.form.php?id={$this->getID()}'>{$this->fields['label']}</a>";
echo "</td>";
echo "<td>" . $fields_type[$this->fields['type']] . "</td>";
echo "<td>" . $this->fields['default_value'] . "</td>";
echo "<td>" ;
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 "</td>";
echo "<td align='center'>" . Dropdown::getYesNo($this->fields["mandatory"]) . "</td>";
echo "<td align='center'>";
echo ($this->isActive())
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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(<<<JAVASCRIPT
var plugin_fields_change_field_type_{$rand} = function(selected_val) {
if (selected_val === 'glpi_item') {
$('#plugin_fields_default_value_label_{$rand}').hide();
$('#plugin_fields_default_value_field_{$rand}').find('[name="default_value"]').val(null).trigger('change');
$('#plugin_fields_default_value_field_{$rand}').hide();
$('#plugin_fields_allowed_values_label_{$rand}').show();
$('#plugin_fields_allowed_values_field_{$rand}').show();
} else {
$('#plugin_fields_default_value_label_{$rand}').show();
$('#plugin_fields_default_value_field_{$rand}').show();
$('#plugin_fields_allowed_values_label_{$rand}').hide();
$('#plugin_fields_allowed_values_field_{$rand}').find('[name="allowed_values\[\]"]').val(null).trigger('change');
$('#plugin_fields_allowed_values_field_{$rand}').hide();
}
};
$(
function () {
plugin_fields_change_field_type_{$rand}();
}
);
JAVASCRIPT
);

Dropdown::showFromArray(
'type',
self::getTypes(false),
[
'value' => $this->fields['type'],
'on_change' => 'plugin_fields_change_field_type_' . $rand . '(this.value)',
'value' => $this->fields['type'],
'rand' => $rand,
]
);
}
Expand All @@ -552,48 +535,44 @@ function () {
echo '</div>';
echo "</td>";
echo "<td colspan='3'>";
echo '<div id="plugin_fields_default_value_field_' . $rand . '" ' . $style_default . '>';
echo Html::input(
'default_value',
[
'value' => $this->fields['default_value'],
]
);
if ($this->fields["type"] == "dropdown") {
echo '<a href="' . Plugin::getWebDir('fields') . '/front/commondropdown.php?ddtype=' .
$this->fields['name'] . 'dropdown">
<img src="' . $CFG_GLPI['root_doc'] . '/pics/options_search.png" class="pointer"
alt="' . __('Configure', 'fields') . '" title="' . __('Configure fields values', 'fields') . '">
</a>';
}
if (in_array($this->fields['type'], ['date', 'datetime'])) {
echo "<i class='pointer fa fa-info'
title=\"" . __("You can use 'now' for date and datetime field") . "\"></i>";
}
echo '<div id="plugin_fields_specific_fields_' . $rand . '">';
echo '</div>';
echo '<div id="plugin_fields_allowed_values_field_' . $rand . '" ' . $style_allowed . '>';
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(<<<JAVASCRIPT
$(
function () {
$('#plugin_fields_specific_fields_$rand').load('../ajax/field_specific_fields.php', $load_params);
}
);
JAVASCRIPT
);
} else {
$allowed_itemtypes = !empty($this->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(<<<JAVASCRIPT
$(
function () {
$('#dropdown_type$rand').trigger('change');
}
);
JAVASCRIPT
);
}
echo '</div>';
echo "</td>";
echo "</tr>";

Expand Down

0 comments on commit af9ca03

Please sign in to comment.