static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true, $show_table = true, $massiveaction = false) { if (empty($fields)) { return false; } //get object associated with this fields $tmp = $fields; $first_field = array_shift($tmp); $container_obj = new PluginFieldsContainer(); $container_obj->getFromDB($first_field['plugin_fields_containers_id']); $classname = "PluginFields" . $itemtype . preg_replace('/s$/', '', $container_obj->fields['name']); $obj = new $classname(); //find row for this object with the items_id $found_values = $obj->find("plugin_fields_containers_id = " . $first_field['plugin_fields_containers_id'] . " AND items_id = " . $items_id); $found_v = array_shift($found_values); // find profiles (to check if current profile can edit fields) $fprofile = new PluginFieldsProfile(); $found_p = $fprofile->find("`profiles_id` = '" . $_SESSION['glpiactiveprofile']['id'] . "'\n AND `plugin_fields_containers_id` = '" . $first_field['plugin_fields_containers_id'] . "'"); $first_found_p = array_shift($found_p); // test status for "CommonITILObject" objects if (is_subclass_of($itemtype, "CommonITILObject")) { $items_obj = new $itemtype(); if ($items_id > 0) { $items_obj->getFromDB($items_id); } else { $items_obj->getEmpty(); } if (in_array($items_obj->fields['status'], $items_obj->getClosedStatusArray()) || in_array($items_obj->fields['status'], $items_obj->getSolvedStatusArray()) || $first_found_p['right'] != CREATE) { $canedit = false; } } //show all fields $html = ""; $odd = 0; foreach ($fields as $field) { if ($field['type'] === 'header') { $html .= "<tr class='tab_bg_2'>"; $html .= "<th colspan='4'>" . $field['label'] . "</td>"; $html .= "</tr>"; $odd = 0; } else { //get value $value = ""; if (is_array($found_v)) { if ($field['type'] == "dropdown") { $value = $found_v["plugin_fields_" . $field['name'] . "dropdowns_id"]; } else { $value = $found_v[$field['name']]; } } if (!$field['is_readonly']) { if ($field['type'] == "dropdown") { if (isset($_SESSION['plugin']['fields']['values_sent']["plugin_fields_" . $field['name'] . "dropdowns_id"])) { $value = $_SESSION['plugin']['fields']['values_sent']["plugin_fields_" . $field['name'] . "dropdowns_id"]; } } else { if (isset($_SESSION['plugin']['fields']['values_sent'][$field['name']])) { $value = $_SESSION['plugin']['fields']['values_sent'][$field['name']]; } } } //get default value if (empty($value) && !empty($field['default_value'])) { $value = $field['default_value']; } //show field if ($show_table) { if ($odd % 2 == 0) { $html .= "<tr class='tab_bg_2'>"; } $required = $field['mandatory'] == 1 ? "<span class='red'>*</span>" : ''; $field['itemtype'] = self::getType(); $txt_label = PluginFieldsLabelTranslation::getLabelFor($field); $label = " <label for='{$field['name']}'>{$txt_label} {$required}</label>"; if (stristr($container_obj->fields['itemtypes'], 'Ticket') !== false && $container_obj->fields['type'] == 'dom' && strpos($_SERVER['HTTP_REFERER'], ".injector.php") === false && strpos($_SERVER['HTTP_REFERER'], ".public.php") === false) { $html .= "<th width='13%'>{$label}</th>"; } else { $html .= "<td>{$label}</td>"; } $html .= "<td>"; } $readonly = $field['is_readonly']; switch ($field['type']) { case 'number': case 'text': $value = Html::cleanInputText($value); if ($canedit && !$readonly) { $html .= "<input type='text' name='" . $field['name'] . "' value=\"{$value}\" />"; } else { $html .= $value; } break; case 'url': $value = Html::cleanInputText($value); if ($canedit && !$readonly) { $html .= "<input type='text' name='" . $field['name'] . "' value=\"{$value}\" />"; if ($value != '') { $html .= "<a target=\"_blank\" href=\"{$value}\">" . __('show', 'fields') . "</a>"; } } else { $html .= "<a target=\"_blank\" href=\"{$value}\">{$value}</a>"; } break; case 'textarea': if ($canedit && !$readonly) { $html .= "<textarea cols='45' rows='4' name='" . $field['name'] . "'>" . "{$value}</textarea>"; } else { $html .= nl2br($value); } break; case 'dropdown': if ($canedit && !$readonly) { //find entity on current object $obj = new $itemtype(); $obj->getFromDB($items_id); ob_start(); if (strpos($field['name'], "dropdowns_id") !== false) { $dropdown_itemtype = getItemTypeForTable(getTableNameForForeignKeyField($field['name'])); } else { $dropdown_itemtype = PluginFieldsDropdown::getClassname($field['name']); } Dropdown::show($dropdown_itemtype, array('value' => $value, 'entity' => $obj->getEntityID())); $html .= ob_get_contents(); ob_end_clean(); } else { $dropdown_table = "glpi_plugin_fields_" . $field['name'] . "dropdowns"; $html .= Dropdown::getDropdownName($dropdown_table, $value); } break; case 'yesno': if ($canedit && !$readonly) { ob_start(); Dropdown::showYesNo($field['name'], $value); $html .= ob_get_contents(); ob_end_clean(); } else { $html .= Dropdown::getYesNo($value); } break; case 'date': if ($canedit && !$readonly) { ob_start(); Html::showDateFormItem($field['name'], $value); $html .= ob_get_contents(); ob_end_clean(); } else { $html .= Html::convDate($value); } break; case 'datetime': if ($canedit && !$readonly) { ob_start(); Html::showDateTimeFormItem($field['name'], $value); $html .= ob_get_contents(); ob_end_clean(); } else { $html .= Html::convDateTime($value); } break; case 'dropdownuser': if ($massiveaction) { continue; } if ($canedit && !$readonly) { ob_start(); User::dropdown(array('name' => $field['name'], 'value' => $value, 'entity' => -1, 'right' => 'all', 'condition' => 'is_active=1 && is_deleted=0')); $html .= ob_get_contents(); ob_end_clean(); } else { $showuserlink = 0; if (Session::haveRight('user', 'r')) { $showuserlink = 1; } $html .= getUserName($value, $showuserlink); } } if ($show_table) { $html .= "</td>"; if ($odd % 2 == 1) { $html .= "</tr>"; } $odd++; } } } if ($show_table && $odd % 2 == 1) { $html .= "</tr>"; } unset($_SESSION['plugin']['fields']['values_sent']); return $html; }
function plugin_fields_getDropdown() { $dropdowns = array(); $field_obj = new PluginFieldsField(); $fields = $field_obj->find("`type` = 'dropdown'"); foreach ($fields as $field) { $field['itemtype'] = PluginFieldsField::getType(); $label = PluginFieldsLabelTranslation::getLabelFor($field); $dropdowns["PluginFields" . ucfirst($field['name']) . "Dropdown"] = $label; } asort($dropdowns); return $dropdowns; }
static function getAddSearchOptions($itemtype, $containers_id = false) { global $DB; $opt = array(); $i = 76665; $query = "SELECT fields.name, fields.label, fields.type, fields.is_readonly,\n containers.name as container_name, containers.label as container_label,\n containers.itemtypes, containers.id as container_id, fields.id as field_id\n FROM glpi_plugin_fields_containers containers\n INNER JOIN glpi_plugin_fields_fields fields\n ON containers.id = fields.plugin_fields_containers_id\n AND containers.is_active = 1\n WHERE containers.itemtypes LIKE '%{$itemtype}%'\n AND fields.type != 'header'\n ORDER BY fields.id ASC"; $res = $DB->query($query); while ($data = $DB->fetch_assoc($res)) { if ($containers_id !== false) { // Filter by container (don't filter by SQL for have $i value with few containers for a itemtype) if ($data['container_id'] != $containers_id) { $i++; continue; } } $tablename = "glpi_plugin_fields_" . strtolower($itemtype . getPlural(preg_replace('/s$/', '', $data['container_name']))); //get translations $container = ['itemtype' => PluginFieldsContainer::getType(), 'id' => $data['container_id'], 'label' => $data['container_label']]; $data['container_label'] = PluginFieldsLabelTranslation::getLabelFor($container); $field = ['itemtype' => PluginFieldsField::getType(), 'id' => $data['field_id'], 'label' => $data['label']]; $data['label'] = PluginFieldsLabelTranslation::getLabelFor($field); $opt[$i]['table'] = $tablename; $opt[$i]['field'] = $data['name']; $opt[$i]['name'] = $data['container_label'] . " - " . $data['label']; $opt[$i]['linkfield'] = $data['name']; $opt[$i]['joinparams']['jointype'] = "itemtype_item"; $opt[$i]['pfields_type'] = $data['type']; if ($data['is_readonly']) { $opt[$i]['massiveaction'] = false; } if ($data['type'] === "dropdown") { $opt[$i]['table'] = 'glpi_plugin_fields_' . $data['name'] . 'dropdowns'; $opt[$i]['field'] = 'completename'; $opt[$i]['linkfield'] = "plugin_fields_" . $data['name'] . "dropdowns_id"; $opt[$i]['forcegroupby'] = true; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; } if ($data['type'] === "dropdownuser") { $opt[$i]['table'] = 'glpi_users'; $opt[$i]['field'] = 'name'; $opt[$i]['linkfield'] = $data['name']; $opt[$i]['right'] = 'all'; $opt[$i]['forcegroupby'] = true; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; } switch ($data['type']) { case 'dropdown': case 'dropdownuser': $opt[$i]['datatype'] = "dropdown"; break; case 'yesno': $opt[$i]['datatype'] = "bool"; break; case 'textarea': $opt[$i]['datatype'] = "text"; break; case 'number': $opt[$i]['datatype'] = "number"; break; case 'date': case 'datetime': $opt[$i]['datatype'] = $data['type']; break; default: $opt[$i]['datatype'] = "string"; } $i++; } return $opt; }