Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 63 additions & 33 deletions ajax/field_specific_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@
}

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
echo '<td>';
echo __('Allowed values', 'fields') . ' :';
echo '</td>';

echo '<td style="line-height:var(--tblr-body-line-height);">';
if ($field->isNewItem()) {
Dropdown::showFromArray('allowed_values', PluginFieldsToolbox::getGlpiItemtypes(), [
'display_emptychoice' => true,
'multiple' => true
'display_emptychoice' => true,
'multiple' => true
]);
} else {
$allowed_itemtypes = !empty($field->fields['allowed_values'])
Expand All @@ -77,41 +75,75 @@ function ($itemtype) {
)
);
}
echo '</td>';
} else {
// Display correct label
echo Html::scriptBlock(<<<JAVASCRIPT
$('#plugin_fields_default_value_label_{$rand}').show();
$('#plugin_fields_allowed_values_label_{$rand}').hide();
JAVASCRIPT
);
$dropdown_matches = [];
$is_dropdown = $type == 'dropdown' || preg_match('/^dropdown-(?<class>.+)$/', $type, $dropdown_matches) === 1;

// Display "default value(s)" field
echo '<td>';
if ($is_dropdown) {
echo __('Multiple dropdown', 'fields') . ' :';
echo '<br />';
}
echo __('Default value', 'fields') . ' :';
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>';
}
echo '</td>';

echo '<td>';
$dropdown_matches = [];
if ($is_dropdown) {
$multiple = (bool)($_POST['multiple'] ?? $field->fields['multiple']);

// 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()) {
Dropdown::showYesNo(
'multiple',
$multiple,
-1,
[
'rand' => $rand,
]
);
} else {
echo Dropdown::getYesNo($multiple);
}
echo '<br />';

echo '<div style="line-height:var(--tblr-body-line-height);">';
if ($field->isNewItem() && $type == 'dropdown') {
echo '<em class="form-control-plaintext">';
echo __s('Default value will be configurable once field will be created.', 'fields');
echo '</em>';
} else {
$itemtype = $type == 'dropdown'
? PluginFieldsDropdown::getClassname($field->fields['name'])
: $dropdown_matches['class'];
$default_value = $multiple ? json_decode($field->fields['default_value']) : $field->fields['default_value'];
Dropdown::show(
PluginFieldsDropdown::getClassname($field->fields['name']),
$itemtype,
[
'name' => 'default_value',
'value' => $field->fields['default_value'],
'name' => 'default_value' . ($multiple ? '[]' : ''),
'value' => $default_value,
'entity_restrict' => -1,
'multiple' => $multiple,
'rand' => $rand,
]
);
}
echo '</div>';
Ajax::updateItemOnSelectEvent(
"dropdown_multiple$rand",
"plugin_fields_specific_fields_$rand",
"../ajax/field_specific_fields.php",
[
'id' => $id,
'type' => $type,
'multiple' => '__VALUE__',
'rand' => $rand,
]
);
} else {
echo Html::input(
'default_value',
Expand All @@ -120,7 +152,5 @@ function ($itemtype) {
]
);
}
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>';
}
echo '</td>';
}
92 changes: 92 additions & 0 deletions inc/abstractcontainerinstance.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

abstract class PluginFieldsAbstractContainerInstance extends CommonDBTM
{
public static function getSpecificValueToDisplay($field, $values, array $options = [])
{
if (!is_array($values)) {
$values = [$field => $values];
}

$field_id = $options['searchopt']['pfields_fields_id'] ?? null;

$field_specs = new PluginFieldsField();
if ($field_id !== null && $field_specs->getFromDB($field_id)) {
$dropdown_matches = [];
if (
preg_match('/^dropdown-(?<class>.+)$/i', $field_specs->fields['type'], $dropdown_matches) === 1
&& $field_specs->fields['multiple']
) {
$itemtype = $dropdown_matches['class'];
if (!is_a($itemtype, CommonDBTM::class, true)) {
return ''; // Itemtype not exists (maybe a deactivated plugin)
}

if (empty($values[$field])) {
return ''; // Value not defined
}
$values = json_decode($values[$field]);
if (!is_array($values)) {
return ''; // Invalid value
}

$names = [];
foreach ($values as $id) {
$item = new $itemtype();
if ($item->getFromDB($id)) {
$names[] = $item->getName();
}
}

return implode($options['separator'] ?? '<br />', $names);
} elseif (
$field_specs->fields['type'] === 'dropdown'
&& $field_specs->fields['multiple']
) {
$itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']);
if (empty($values[$field])) {
return ''; // Value not defined
}
$values = json_decode($values[$field]);
if (!is_array($values)) {
return ''; // Invalid value
}

return implode(
$options['separator'] ?? '<br />',
Dropdown::getDropdownArrayNames($itemtype::getTable(), $values)
);
}
}

return parent::getSpecificValueToDisplay($field, $values, $options);
}
}
107 changes: 80 additions & 27 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,14 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
return false;
}

// Normalize values
foreach ($data as $key => $value) {
if (is_array($value)) {
// Convert "multiple" values into a JSON string
$data[$key] = json_encode($value);
}
}

$container_obj = new PluginFieldsContainer();
$container_obj->getFromDB($data['plugin_fields_containers_id']);

Expand Down Expand Up @@ -1146,17 +1154,29 @@ public static function constructHistory(
if ($searchoption['linkfield'] == $key) {
$changes[0] = $id_search_option;

//manage dropdown values
if ($searchoption['datatype'] === 'dropdown') {
//manage dropdown values
$changes = [$id_search_option,
"",
Dropdown::getDropdownName($searchoption['table'], $value)
];
}

//manage bool dropdown values
if ($searchoption['datatype'] === 'bool') {
} elseif ($searchoption['datatype'] === 'bool') {
//manage bool values
$changes = [$id_search_option, "", Dropdown::getYesNo($value)];
} elseif ($searchoption['datatype'] === 'specific') {
//manage specific values
$changes = [
$id_search_option,
"",
PluginFieldsAbstractContainerInstance::getSpecificValueToDisplay(
$key,
$value,
[
'searchopt' => $searchoption,
'separator' => ', ',
]
)
];
}
}
}
Expand Down Expand Up @@ -1190,14 +1210,32 @@ public static function constructHistory(
if ($searchoption['linkfield'] == $key) {
$changes[0] = $id_search_option;

//manage dropdown values
if ($searchoption['datatype'] === 'dropdown') {
//manage dropdown values
$changes[1] = Dropdown::getDropdownName($searchoption['table'], $changes[1]);
$changes[2] = Dropdown::getDropdownName($searchoption['table'], $changes[2]);
}
if ($searchoption['datatype'] === 'bool') {
} elseif ($searchoption['datatype'] === 'bool') {
//manage bool values
$changes[1] = Dropdown::getYesNo($changes[1]);
$changes[2] = Dropdown::getYesNo($changes[2]);
} elseif ($searchoption['datatype'] === 'specific') {
//manage specific values
$changes[1] = PluginFieldsAbstractContainerInstance::getSpecificValueToDisplay(
$key,
$changes[1],
[
'searchopt' => $searchoption,
'separator' => ', ',
]
);
$changes[2] = PluginFieldsAbstractContainerInstance::getSpecificValueToDisplay(
$key,
$changes[2],
[
'searchopt' => $searchoption,
'separator' => ', ',
]
);
}
}
}
Expand Down Expand Up @@ -1236,7 +1274,7 @@ public static function validateValues($data, $itemtype, $massiveaction)

// Apply status overrides
$status_field_name = PluginFieldsStatusOverride::getStatusFieldName($itemtype);
$status_overrides = $data[$status_field_name] !== null
$status_overrides = array_key_exists($status_field_name, $data) && $data[$status_field_name] !== null
? PluginFieldsStatusOverride::getOverridesForItemtypeAndStatus($container->getID(), $itemtype, $data[$status_field_name])
: [];
foreach ($status_overrides as $status_override) {
Expand Down Expand Up @@ -1603,6 +1641,7 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
'glpi_plugin_fields_fields.type',
'glpi_plugin_fields_fields.is_readonly',
'glpi_plugin_fields_fields.allowed_values',
'glpi_plugin_fields_fields.multiple',
'glpi_plugin_fields_containers.id AS container_id',
'glpi_plugin_fields_containers.name AS container_name',
'glpi_plugin_fields_containers.label AS container_label',
Expand Down Expand Up @@ -1673,7 +1712,10 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
$opt[$i]['name'] = $data['container_label'] . " - " . $data['field_label'];
$opt[$i]['linkfield'] = $data['field_name'];
$opt[$i]['joinparams']['jointype'] = "itemtype_item";
$opt[$i]['pfields_type'] = $data['type'];

$opt[$i]['pfields_type'] = $data['type'];
$opt[$i]['pfields_fields_id'] = $data['field_id'];

if ($data['is_readonly']) {
$opt[$i]['massiveaction'] = false;
}
Expand All @@ -1698,31 +1740,42 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)

$dropdown_matches = [];
if ($data['type'] === "dropdown") {
$opt[$i]['table'] = 'glpi_plugin_fields_' . $data['field_name'] . 'dropdowns';
$opt[$i]['field'] = 'completename';
$opt[$i]['linkfield'] = "plugin_fields_" . $data['field_name'] . "dropdowns_id";
$opt[$i]['datatype'] = "dropdown";
$field_name = "plugin_fields_" . $data['field_name'] . "dropdowns_id";

if ($data['multiple']) {
$opt[$i]['table'] = $tablename;
$opt[$i]['field'] = $field_name;
$opt[$i]['datatype'] = 'specific';
} else {
$opt[$i]['table'] = 'glpi_plugin_fields_' . $data['field_name'] . 'dropdowns';
$opt[$i]['field'] = 'completename';
$opt[$i]['linkfield'] = $field_name;
$opt[$i]['datatype'] = "dropdown";

$opt[$i]['forcegroupby'] = true;
$opt[$i]['forcegroupby'] = true;

$opt[$i]['joinparams']['jointype'] = "";
$opt[$i]['joinparams']['beforejoin']['table'] = $tablename;
$opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item";
$opt[$i]['joinparams']['jointype'] = "";
$opt[$i]['joinparams']['beforejoin']['table'] = $tablename;
$opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item";
}
} elseif (
preg_match('/^dropdown-(?<class>.+)$/i', $data['type'], $dropdown_matches)
&& class_exists($dropdown_matches['class'])
) {
$opt[$i]['table'] = CommonDBTM::getTable($dropdown_matches['class']);
$opt[$i]['field'] = 'name';
$opt[$i]['linkfield'] = $data['field_name'];
$opt[$i]['right'] = 'all';
$opt[$i]['datatype'] = "dropdown";
if ($data['multiple']) {
$opt[$i]['datatype'] = 'specific';
} else {
$opt[$i]['table'] = CommonDBTM::getTable($dropdown_matches['class']);
$opt[$i]['field'] = 'name';
$opt[$i]['right'] = 'all';
$opt[$i]['datatype'] = "dropdown";

$opt[$i]['forcegroupby'] = true;
$opt[$i]['forcegroupby'] = true;

$opt[$i]['joinparams']['jointype'] = "";
$opt[$i]['joinparams']['beforejoin']['table'] = $tablename;
$opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item";
$opt[$i]['joinparams']['jointype'] = "";
$opt[$i]['joinparams']['beforejoin']['table'] = $tablename;
$opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item";
}
} elseif ($data['type'] === "glpi_item") {
$itemtype_field = sprintf('itemtype_%s', $data['field_name']);
$items_id_field = sprintf('items_id_%s', $data['field_name']);
Expand Down
Loading