function prepareInputForAdd($input) { global $DB; //parse name $input['name'] = $this->prepareName($input); //dropdowns : create files if ($input['type'] === "dropdown") { //search if dropdown already exist in this container $found = $this->find("name = '" . $input['name'] . "'\n AND plugin_fields_containers_id = '" . $input['plugin_fields_containers_id'] . "'"); //reject adding for same dropdown on same bloc if (!empty($found)) { Session::AddMessageAfterRedirect(__("You cannot add same field 'dropdown' on same bloc", 'fields', false, ERROR)); return false; } //search if dropdown already exist in other container $found = $this->find("name = '" . $input['name'] . "'"); //for dropdown, if already exist, don't create files if (empty($found)) { PluginFieldsDropdown::create($input); } $oldname = $input['name']; $input['name'] = getForeignKeyFieldForItemType(PluginFieldsDropdown::getClassname($input['name'])); } // Before adding, add the ranking of the new field if (empty($input["ranking"])) { $input["ranking"] = $this->getNextRanking(); } //add field to container table if ($input['type'] !== "header") { $container_obj = new PluginFieldsContainer(); $container_obj->getFromDB($input['plugin_fields_containers_id']); foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) { $classname = "PluginFields" . ucfirst(strtolower($itemtype . preg_replace('/s$/', '', $container_obj->fields['name']))); $classname::addField($input['name'], $input['type']); } } if (isset($oldname)) { $input['name'] = $oldname; } return $input; }
/** * check datas inserted * display a message when not ok * @param array $datas : datas send by form * @return boolean */ static function validateValues($datas) { $valid = true; $empty_errors = array(); $number_errors = array(); $field_obj = new PluginFieldsField(); $fields = $field_obj->find("plugin_fields_containers_id = " . $datas['plugin_fields_containers_id']); foreach ($fields as $fields_id => $field) { if ($field['type'] == "yesno") { continue; } if ($field['type'] == "header") { continue; } $name = $field['name']; if (isset($datas[$name])) { $value = $datas[$name]; } elseif (isset($datas['plugin_fields_' . $name . 'dropdowns_id'])) { $value = $datas['plugin_fields_' . $name . 'dropdowns_id']; } else { $value = ''; } // Check mandatory fields if ($field['mandatory'] == 1 && (empty($value) || in_array($field['type'], array('date', 'datetime')) && $value == 'NULL')) { $empty_errors[] = $field['label']; $valid = false; // Check number fields } elseif ($field['type'] == 'number' && !empty($value) && !is_numeric($value)) { $number_errors[] = $field['label']; $valid = false; } } if (!empty($empty_errors)) { Session::AddMessageAfterRedirect(__("Some mandatory fields are empty", "fields") . " : " . implode(', ', $empty_errors), false, ERROR); } if (!empty($number_errors)) { Session::AddMessageAfterRedirect(__("Some numeric fields contains non numeric values", "fields") . " : " . implode(', ', $number_errors), false, ERROR); } return $valid; }
/** * check data inserted * display a message when not ok * * @param array $data Data send by form * @param string $itemtype Item type * @param boolean $massiveaction ? * * @return boolean */ 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']); foreach ($fields as $fields_id => $field) { if ($field['type'] == "yesno") { continue; } if ($field['type'] == "header") { continue; } $name = $field['name']; if (isset($data[$name])) { $value = $data[$name]; } else { if (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\n `itemtype`='{$itemtype}'\n AND `items_id`='{$data['items_id']}'\n 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 = ''; } } } //translate label $field['itemtype'] = PluginFieldsField::getType(); $field['label'] = PluginFieldsLabelTranslation::getLabelFor($field); // Check mandatory fields if ($field['mandatory'] == 1 && (empty($value) || in_array($field['type'], array('date', 'datetime')) && $value == 'NULL')) { $empty_errors[] = $field['label']; $valid = false; } else { if ($field['type'] == 'number' && !empty($value) && !is_numeric($value)) { // Check number fields $number_errors[] = $field['label']; $valid = false; } else { if ($field['type'] == 'url' && !empty($value)) { if (filter_var($value, FILTER_VALIDATE_URL) === false) { $url_errors[] = $field['label']; $valid = false; } } } } } if (!empty($empty_errors)) { Session::AddMessageAfterRedirect(__("Some mandatory fields are empty", "fields") . " : " . implode(', ', $empty_errors), false, ERROR); } if (!empty($number_errors)) { Session::AddMessageAfterRedirect(__("Some numeric fields contains non numeric values", "fields") . " : " . implode(', ', $number_errors), false, ERROR); } if (!empty($url_errors)) { Session::AddMessageAfterRedirect(__("Some URL fields contains invalid links", "fields") . " : " . implode(', ', $url_errors), false, ERROR); } return $valid; }