.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/genericobject
* -------------------------------------------------------------------------
*/
class PluginGenericobjectField extends CommonDBTM {
/**
*
* Displat all fields present in DB for an itemtype
* @param $id the itemtype's id
*/
public static function showObjectFieldsForm($id) {
global $DB, $GO_BLACKLIST_FIELDS, $GO_READONLY_FIELDS, $GO_FIELDS, $CFG_GLPI;
$url = Toolbox::getItemTypeFormURL(__CLASS__);
$object_type = new PluginGenericobjectType();
$object_type->getFromDB($id);
$itemtype = $object_type->fields['itemtype'];
$fields_in_db = PluginGenericobjectSingletonObjectField::getInstance($itemtype);
$used_fields = [];
//Reset fields definition only to keep the itemtype ones
$GO_FIELDS = [];
plugin_genericobject_includeCommonFields(true);
PluginGenericobjectType::includeLocales($object_type->fields['name']);
PluginGenericobjectType::includeConstants($object_type->fields['name'], true);
self::addReadOnlyFields($object_type);
foreach ($GO_BLACKLIST_FIELDS as $autofield) {
if (!in_array($autofield, $used_fields)) {
$used_fields[$autofield] = $autofield;
}
}
echo "
";
echo "
";
}
/**
* Method to set fields as read only, when the depend on some features
* that are enabled
* @since 0.85+2.4.0
*/
static function addReadOnlyFields(PluginGenericobjectType $type) {
global $GO_READONLY_FIELDS;
if ($type->canBeReserved()) {
$GO_READONLY_FIELDS[] = 'users_id';
$GO_READONLY_FIELDS[] = 'locations_id';
}
if ($type->canUseGlobalSearch()) {
$GO_READONLY_FIELDS[] = 'serial';
$GO_READONLY_FIELDS[] = 'otherserial';
$GO_READONLY_FIELDS[] = 'locations_id';
$GO_READONLY_FIELDS[] = 'states_id';
$GO_READONLY_FIELDS[] = 'users_id';
$GO_READONLY_FIELDS[] = 'groups_id';
$GO_READONLY_FIELDS[] = 'manufacturers_id';
$GO_READONLY_FIELDS[] = 'users_id_tech';
}
if ($type->canUseItemDevice()) {
$GO_READONLY_FIELDS[] = 'locations_id';
}
}
/**
* Get the name of the field, as defined in a constant file
* The name may be the same, or not depending if it's an isolated dropdown or not
*/
static function getFieldName($field, $itemtype, $options, $remove_prefix = false) {
global $DB;
$field_orig = $field;
$field_table = null;
$input_type = isset($options['input_type'])
? $options['input_type']
: null;
switch ($input_type) {
case 'dropdown':
$dropdown_type = isset($options['dropdown_type'])
? $options['dropdown_type']
: null;
$fk = getForeignKeyFieldForTable(getTableForItemType($itemtype));
if ($dropdown_type == 'isolated') {
if (!$remove_prefix) {
$field = preg_replace("/s_id$/", $field, $fk);
} else {
$fk = preg_replace("/s_id$/", "", $fk);
$field = preg_replace("/".$fk."/", "", $field);
}
}
$field_table = getTableNameForForeignKeyField($field);
//Prepend plugin's table prefix if this dropdown is not already handled by GLPI natively
if (substr($field, 0, strlen('plugin_genericobject')) !== 'plugin_genericobject'
and (
substr($field_table, strlen('glpi_'))
=== substr($field, 0, strlen($field) -strlen('_id'))
)
and !$DB->tableExists($field_table)
) {
if (!$remove_prefix) {
$field = 'plugin_genericobject_' . $field;}
}
break;
}
return $field;
}
/**
*
* Display a dropdown with all available fields for an itemtype
* @since
* @param $name the dropdown name
* @param $itemtype the itemtype
* @param $used an array which contains all fields already added
*
* @return the dropdown random ID
*/
static function dropdownFields($name, $itemtype, $used = []) {
global $GO_FIELDS;
$dropdown_types = [];
foreach ($GO_FIELDS as $field => $values) {
$message = "";
$field_options = [];
$field = self::getFieldName($field, $itemtype, $values, false);
if (!in_array($field, $used)) {
if (!isset($dropdown_types[$field])) {
//Global management :
//meaning that a dropdown can be useful in all types (for example type, model, etc.)
if (isset($values['input_type']) && $values['input_type'] == 'dropdown') {
if (isset($values['entities_id'])) {
$field_options[] = __("Entity")." : ".Dropdown::getYesNo($values['entities_id']);
if ($values['entities_id']) {
if (isset($values['is_recursive'])) {
$field_options[] = __("Child entities")." : ".Dropdown::getYesNo($values['is_recursive']);
}
}
} else {
$field_options[] = __("Entity")." : ".Dropdown::getYesNo(0);
}
if (isset($values['is_tree'])) {
$field_options[] = __("tree structure")." : ".Dropdown::getYesNo($values['is_tree']);
} else {
$field_options[] = __("tree structure")." : ".Dropdown::getYesNo(0);
}
//if (isset($values['isolated']) and $values['isolated']) {
// $field_options[] = __("Isolated") . " : ". Dropdown::getYesNo($values['isolated']);
//} else {
// $field_options[] = __("Isolated") . " : ". Dropdown::getYesNo(0);
//}
}
if (!empty($field_options)) {
$message = "(".trim( implode(", ", $field_options)).")";
}
}
$dropdown_types[$field] = $values['name']." ".$message;
}
}
// Don't show dropdown empty
if (empty($dropdown_types)) {
return '';
}
ksort($dropdown_types);
return Dropdown::showFromArray($name, $dropdown_types, ['display' => false]);
}
/**
*
* Get field's options defined in constant files.
* If this field has not been defined, it means that this field has been defined globally and
* must be dynamically created.
*
* @param $field the current field
* @param $itemtype the itemtype
* @return an array which contains the full field definition
*/
static function getFieldOptions($field, $itemtype = "") {
global $GO_FIELDS;
$options = [];
$cleaned_field = preg_replace("/^plugin_genericobject_/", '', $field);
if (!isset($GO_FIELDS[$cleaned_field]) && !empty($itemtype)) {
// This field has been dynamically defined because it's an isolated dropdown
$tmpfield = self::getFieldName(
$field, $itemtype,
[
'dropdown_type' => 'isolated',
'input_type' => 'dropdown'
],
true
);
$options = $GO_FIELDS[$tmpfield];
$options['realname'] = $tmpfield;
} else if (isset($GO_FIELDS[$cleaned_field])) {
$options = $GO_FIELDS[$cleaned_field];
$options['realname'] = $cleaned_field;
}
return $options;
}
public static function displayFieldDefinition($target, $itemtype, $field, $index, $last = false) {
global $GO_FIELDS, $CFG_GLPI, $GO_BLACKLIST_FIELDS, $GO_READONLY_FIELDS;
$readonly = in_array($field, $GO_READONLY_FIELDS);
$blacklist = in_array($field, $GO_BLACKLIST_FIELDS);
$options = self::getFieldOptions($field, $itemtype);
echo "