/** * We can add several single CommonDBChild to a given Item. In such case, we display a "+" * button and the fields already entered. * This method display the fields * * @since version 0.84 * * @todo study if we cannot use these methods for the user emails * @see showAddChildButtonForItemForm() * * @param $item CommonDBTM object the item on which to add the current CommenDBChild * @param $field_name the name of the HTML field inside Item's form * @param $canedit (default NULL) NULL to use default behaviour * * @return nothing (display only) **/ static function showChildsForItemForm(CommonDBTM $item, $field_name, $canedit = NULL) { global $DB, $CFG_GLPI; $items_id = $item->getID(); if (is_null($canedit)) { if ($item->isNewItem()) { if (!$item->canCreate()) { return false; } $canedit = $item->canUpdate(); } else { if (!$item->can($items_id, 'r')) { return false; } $canedit = $item->can($items_id, "w"); } } $lower_name = strtolower(get_called_class()); $div_id = "add_" . $lower_name . "_to_" . $item->getType() . "_" . $items_id; // To be sure not to load bad datas from this table if ($items_id == 0) { $items_id = -99; } $query = "SELECT *\n FROM `" . static::getTable() . "`\n WHERE `" . static::$items_id . "` = '" . $item->getID() . "'"; if (preg_match('/^itemtype/', static::$itemtype)) { $query .= " AND `itemtype` = '" . $item->getType() . "'"; } $current_item = new static(); if ($current_item->maybeDeleted()) { $query .= " AND `is_deleted` = '0'"; } $count = 0; foreach ($DB->request($query) as $data) { $current_item->fields = $data; if ($count) { echo '<br>'; } $count++; $current_item->showChildForItemForm($canedit, $field_name . "[" . $current_item->getID() . "]"); } if ($canedit) { echo "<div id='{$div_id}'>"; // No Child display field if ($count == 0) { $current_item->getEmpty(); $current_item->showChildForItemForm($canedit, $field_name . "[-100]"); } echo "</div>"; } }
/** * Populates fields data from item * * @param integer $c_id Container ID * @param CommonDBTM $item Item instance * * @return array|false */ private static function populateData($c_id, CommonDBTM $item) { //find fields associated to found container $field_obj = new PluginFieldsField(); $fields = $field_obj->find("plugin_fields_containers_id = {$c_id} AND type != 'header'", "ranking"); //prepare data to update $data = ['plugin_fields_containers_id' => $c_id]; if ($item->isNewItem()) { //no ID yet while creating $data['items_id'] = $item->getID(); } $has_fields = false; foreach ($fields as $field) { if (isset($item->input[$field['name']])) { //standard field $input = $field['name']; } else { //dropdown field $input = "plugin_fields_" . $field['name'] . "dropdowns_id"; } if (isset($item->input[$input])) { $has_fields = true; // Before is_number check, help user to have a number correct, during a massive action of a number field if ($field['type'] == 'number') { $item->input[$input] = str_replace(",", ".", $item->input[$input]); } $data[$input] = $item->input[$input]; } } if ($has_fields === true) { return $data; } else { return false; } }