Skip to content

Commit

Permalink
Better check on mandatory fields; fixes #78
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Nov 28, 2016
1 parent a2150cb commit d93a3c6
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,9 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem
function updateFieldsValues($data, $massiveaction = false, $itemtype) {
global $DB;

if (self::validateValues($data, $massiveaction) === false) return false;
if (self::validateValues($data, $itemtype, $massiveaction) === false) {
return false;
}

$container_obj = new PluginFieldsContainer;
$container_obj->getFromDB($data['plugin_fields_containers_id']);
Expand Down Expand Up @@ -857,14 +859,23 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data,
/**
* check data inserted
* display a message when not ok
* @param array $data data send by form
*
* @param array $data Data send by form
* @param string $itemtype Item type
* @param boolean $massiveaction ?
*
* @return boolean
*/
static function validateValues($data, $massiveaction) {
static function validateValues($data, $itemtype, $massiveaction) {
global $DB;

$valid = true;
$empty_errors = array();
$number_errors = array();

$container = new self();
$container->getFromDB($data['plugin_fields_containers_id']);

$field_obj = new PluginFieldsField();
$fields = $field_obj->find("plugin_fields_containers_id = ".
$data['plugin_fields_containers_id']);
Expand All @@ -878,6 +889,24 @@ static function validateValues($data, $massiveaction) {
$value = $data[$name];
} elseif(isset($data['plugin_fields_' . $name . 'dropdowns_id'])) {
$value = $data['plugin_fields_' . $name . 'dropdowns_id'];
} else if ($field['mandatory'] == 1) {
$tablename = "glpi_plugin_fields_" . strtolower(
$itemtype . getPlural(preg_replace('/s$/', '', $container->fields['name']))
);

$query = "SELECT * FROM `$tablename` WHERE
`itemtype`='$itemtype'
AND `items_id`='{$data['items_id']}'
AND `plugin_fields_containers_id`='{$data['plugin_fields_containers_id']}'";

$db_result = [];
if ($result = $DB->query($query)) {
$db_result = $DB->fetch_assoc($result);
if (isset($db_result[$name])) {
$value = $db_result[$name];
}
}

} else {
if ($massiveaction) continue;
$value = '';
Expand Down

0 comments on commit d93a3c6

Please sign in to comment.