Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(display): check data after reload from self-service #543

Merged
62 changes: 62 additions & 0 deletions ajax/container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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
* -------------------------------------------------------------------------
*/

include("../../../inc/includes.php");

use Glpi\Http\Response;

if (isset($_GET['action']) && $_GET['action'] === 'get_fields_html') {
$containers_id = $_GET['id'];
$itemtype = $_GET['itemtype'];
$items_id = (int)$_GET['items_id'];
$type = $_GET['type'];
$subtype = $_GET['subtype'];
$input = $_GET['input'];

$item = new $itemtype();
if ($items_id > 0 && !$item->getFromDB($items_id)) {
Response::sendError(404, 'Not Found');
}
$item->input = $input;

$display_condition = new PluginFieldsContainerDisplayCondition();
if ($display_condition->computeDisplayContainer($item, $containers_id)) {
PluginFieldsField::showDomContainer(
$containers_id,
$item,
$type,
$subtype
);
} else {
echo "";
}
} else {
Response::sendError(404, 'Not Found');
}
18 changes: 16 additions & 2 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
foreach ($found_c as $data) {
$dataitemtypes = json_decode($data['itemtypes']);
if (in_array(get_class($item), $dataitemtypes) != false) {
return PluginFieldsField::showForTabContainer($data['id'], $item->fields['id'], get_class($item));
return PluginFieldsField::showForTabContainer($data['id'], $item);
}
}
}
Expand Down Expand Up @@ -1234,6 +1234,17 @@ public static function validateValues($data, $itemtype, $massiveaction)
'plugin_fields_containers_id' => $data['plugin_fields_containers_id']
]);

// Apply status overrides
$status_overrides = $data['status'] !== null
? PluginFieldsStatusOverride::getOverridesForItemtypeAndStatus($container->getID(), $itemtype, $data['status'])
: [];
foreach ($status_overrides as $status_override) {
if (isset($fields[$status_override['plugin_fields_fields_id']])) {
$fields[$status_override['plugin_fields_fields_id']]['is_readonly'] = $status_override['is_readonly'];
$fields[$status_override['plugin_fields_fields_id']]['mandatory'] = $status_override['mandatory'];
}
}

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

$query = "SELECT * FROM `$tablename` WHERE
Expand Down Expand Up @@ -1520,6 +1531,9 @@ private static function populateData($c_id, CommonDBTM $item)
$data['items_id'] = $item->getID();
}

// Add status so it can be used with status overrides
$data['status'] = $item->fields['status'] ?? null;

$has_fields = false;
foreach ($fields as $field) {
if ($field['type'] == 'glpi_item') {
Expand Down
12 changes: 7 additions & 5 deletions inc/containerdisplaycondition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,36 +392,38 @@ public function checkCondition($item)
$condition = $this->fields['condition'];
$searchOption = Search::getOptions(get_class($item))[$this->fields['search_option']];

$fields = array_merge($item->fields, $item->input);

switch ($condition) {
case self::SHOW_CONDITION_EQ:
// '='
if ($value == $item->fields[$searchOption['linkfield']]) {
if ($value == $fields[$searchOption['linkfield']]) {
return false;
}
break;
case self::SHOW_CONDITION_NE:
// '≠'
if ($value != $item->fields[$searchOption['linkfield']]) {
if ($value != $fields[$searchOption['linkfield']]) {
return false;
}
break;
case self::SHOW_CONDITION_LT:
// '<';
if ($item->fields[$searchOption['linkfield']] > $value) {
if ($fields[$searchOption['linkfield']] > $value) {
return false;
}
break;
case self::SHOW_CONDITION_GT:
//'>';
if ($item->fields[$searchOption['linkfield']] > $value) {
if ($fields[$searchOption['linkfield']] > $value) {
return false;
}
break;
case self::SHOW_CONDITION_REGEX:
//'regex';
if (self::checkRegex($value)) {
$value = Sanitizer::unsanitize($value);
if (preg_match_all($value . "i", $item->fields[$searchOption['linkfield']]) > 0) {
if (preg_match_all($value . "i", $fields[$searchOption['linkfield']]) > 0) {
return false;
}
}
Expand Down
Loading