function dropdown_getTypeName($class, $nb = 0) { global $GO_FIELDS; $fk = getForeignKeyFieldForTable(getTableForItemType($class)); $instance = new $class(); $options = PluginGenericobjectField::getFieldOptions($fk, $instance->linked_itemtype); $dropdown_type = isset($options['dropdown_type']) ? $options['dropdown_type'] : null; $label = $options['name']; if (!is_null($dropdown_type) and $dropdown_type === 'isolated') { $linked_itemtype_object = new $instance->linked_itemtype(); $label .= " (" . __($linked_itemtype_object::getTypeName(), 'genericobject') . ")"; } if ($label != '') { return $label; } else { return $class; } }
@license GPLv2+ http://www.gnu.org/licenses/gpl.txt @link https://forge.indepnet.net/projects/genericobject @link http://www.glpi-project.org/ @since 2009 ---------------------------------------------------------------------- */ include "../../../inc/includes.php"; if (isset($_POST["delete"])) { $type = new PluginGenericobjectType(); $type->getFromDB($_POST["id"]); $itemtype = $type->fields['itemtype']; PluginGenericobjectType::registerOneType($itemtype); foreach ($_POST["fields"] as $field => $value) { if ($type->can($_POST["id"], PURGE) && $value == 1 && PluginGenericobjectField::checkNecessaryFieldsDelete($itemtype, $field)) { PluginGenericobjectField::deleteField(getTableForItemType($itemtype), $field); Session::addMessageAfterRedirect(__("Field(s) deleted successfully", "genericobject"), true, INFO); } } } elseif (isset($_POST["add_field"])) { $type = new PluginGenericobjectType(); if ($_POST["new_field"] && $type->can($_POST["id"], UPDATE)) { $itemtype = $type->fields['itemtype']; PluginGenericobjectType::registerOneType($itemtype); PluginGenericobjectField::addNewField(getTableForItemType($itemtype), $_POST["new_field"]); Session::addMessageAfterRedirect(__("Field added successfully", "genericobject")); } } elseif (isset($_POST['action'])) { //Move field PluginGenericobjectField::changeFieldOrder($_POST); } Html::back();
/** * * Create or overwrite files for an itemtype * @since 2.2.0 * @param $itemtype the itemtype to check * @param $name type's short name * @param $overwrite force to overwrite existing files * @return nothing */ static function checkClassAndFilesForOneItemType($itemtype, $name, $overwrite = false) { global $DB; $table = getTableForItemType($itemtype); //If class doesn't exist but table exists, create class if (TableExists($table) && ($overwrite || !class_exists($itemtype))) { self::addNewObject($name, $itemtype, array('add_table' => false, 'create_default_profile' => false, 'add_injection_file' => false, 'add_language_file' => false)); } foreach ($DB->list_fields($table) as $field => $options) { if (preg_match("/s_id\$/", $field)) { $dropdowntable = getTableNameForForeignKeyField($field); $dropdownclass = getItemTypeForTable($dropdowntable); if (TableExists($dropdowntable) && !class_exists($dropdownclass)) { $name = str_replace("glpi_plugin_genericobject_", "", $dropdowntable); $name = getSingular($name); $params = PluginGenericobjectField::getOptionsWithGlobal($field, $dropdownclass); $params['linked_itemtype'] = $itemtype; self::addNewDropdown($name, 'PluginGenericobject' . ucfirst($name), $params); } } } }
/** * Get all dropdown fields associated with an itemtype * @param itemtype the itemtype * @return an array or fields that represents the dropdown tables */ static function getDropdownForItemtype($itemtype) { global $DB; $associated_tables = array(); if (class_exists($itemtype)) { $source_table = getTableForItemType($itemtype); foreach (PluginGenericobjectSingletonObjectField::getInstance($itemtype) as $field => $value) { $table = getTableNameForForeignKeyField($field); $options = PluginGenericobjectField::getFieldOptions($field, $itemtype); if (isset($options['input_type']) and $options['input_type'] === 'dropdown' and preg_match('/^glpi_plugin_genericobject/', $table)) { $associated_tables[] = $table; } } } return $associated_tables; }
function getObjectSearchOptions($with_linkfield = false) { global $DB, $GO_FIELDS, $GO_BLACKLIST_FIELDS; $datainjection_blacklisted = array('id', 'date_mod', 'entities_id'); $index_exceptions = array('entities_id' => 80, 'is_recursive' => 86, 'date_mod' => 19, 'comment' => 16, 'notepad' => 90, 'name' => 1, 'id' => 2); $index = 3; $options = array(); $table = getTableForItemType(get_called_class()); foreach (PluginGenericobjectSingletonObjectField::getInstance(get_called_class()) as $field => $values) { $searchoption = PluginGenericobjectField::getOptionsWithGlobal($field, $this->objecttype->fields['itemtype']); //Some fields have fixed index values... $currentindex = $index; if (isset($index_exceptions[$field])) { $currentindex = $index_exceptions[$field]; } elseif (in_array($currentindex, $index_exceptions)) { //If this index is reserved, jump to next $currentindex++; } if (in_array($field, array('is_deleted'))) { continue; } $item = new $this->objecttype->fields['itemtype'](); //Table definition $tmp = getTableNameForForeignKeyField($field); if ($with_linkfield) { $options[$currentindex]['linkfield'] = $field; } if ($tmp != '') { $itemtype = getItemTypeForTable($tmp); $tmpobj = new $itemtype(); //Set table $options[$currentindex]['table'] = $tmp; //Set field if ($tmpobj instanceof CommonTreeDropdown) { $options[$currentindex]['field'] = 'completename'; } else { $options[$currentindex]['field'] = 'name'; } } else { $options[$currentindex]['table'] = $table; $options[$currentindex]['field'] = $field; } $options[$currentindex]['name'] = $searchoption['name']; //Massive action or not if (isset($searchoption['massiveaction'])) { $options[$currentindex]['massiveaction'] = $searchoption['massiveaction']; } //Datainjection option if (!in_array($field, $datainjection_blacklisted)) { $options[$currentindex]['injectable'] = 1; } else { $options[$currentindex]['injectable'] = 0; } //Field type switch ($values['Type']) { default: case "varchar(255)": if ($field == 'name') { $options[$currentindex]['datatype'] = 'itemlink'; $options[$currentindex]['itemlink_type'] = get_called_class(); $options[$currentindex]['massiveaction'] = false; } else { if (isset($searchoption['datatype']) && $searchoption['datatype'] == 'weblink') { $options[$currentindex]['datatype'] = 'weblink'; } else { $options[$currentindex]['datatype'] = 'string'; } } if ($item->canUsePluginDataInjection()) { //Datainjection specific $options[$currentindex]['checktype'] = 'text'; $options[$currentindex]['displaytype'] = 'text'; } break; case "tinyint(1)": $options[$currentindex]['datatype'] = 'bool'; if ($item->canUsePluginDataInjection()) { //Datainjection specific $options[$currentindex]['displaytype'] = 'bool'; } break; case "text": case "longtext": $options[$currentindex]['datatype'] = 'text'; if ($item->canUsePluginDataInjection()) { //Datainjection specific $options[$currentindex]['displaytype'] = 'multiline_text'; } break; case "int(11)": $options[$currentindex]['datatype'] = 'integer'; if ($item->canUsePluginDataInjection()) { if ($tmp != '') { $options[$currentindex]['displaytype'] = 'dropdown'; $options[$currentindex]['checktype'] = 'text'; } else { //Datainjection specific $options[$currentindex]['displaytype'] = 'dropdown_integer'; $options[$currentindex]['checktype'] = 'integer'; } } break; case "float": case "decimal": $options[$currentindex]['datatype'] = $values['Type']; if ($item->canUsePluginDataInjection()) { //Datainjection specific $options[$currentindex]['display'] = 'text'; $options[$currentindex]['checktype'] = $values['Type']; } break; case "date": $options[$currentindex]['datatype'] = 'date'; if ($item->canUsePluginDataInjection()) { //Datainjection specific $options[$currentindex]['displaytype'] = 'date'; $options[$currentindex]['checktype'] = 'date'; } break; case "datetime": $options[$currentindex]['datatype'] = 'datetime'; if ($item->canUsePluginDataInjection()) { //Datainjection specific $options[$currentindex]['displaytype'] = 'date'; $options[$currentindex]['checktype'] = 'date'; } break; } $index++; } return $options; }