From d93a3c6bea823458cd10d40e0a23ef780c53ea24 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 28 Nov 2016 15:54:58 +0100 Subject: [PATCH] Better check on mandatory fields; fixes #78 --- inc/container.class.php | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 0e3451f7..43b5d07c 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -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']); @@ -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']); @@ -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 = '';