Skip to content

Commit

Permalink
generic development for all models
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed Mar 28, 2022
1 parent 4397ad3 commit 3343d3c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
30 changes: 14 additions & 16 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,8 @@ static function validateValues($data, $itemtype, $massiveaction) {
// Check mandatory fields
if ($field['mandatory'] == 1
&& ($value == ""
|| in_array($field['type'], ['dropdown', 'dropdownuser', 'dropdownoperatingsystems', 'dropdowncomputermodels', 'dropdownnetworkmodels'])
|| in_array($field['type'], ['dropdown', 'dropdownuser', 'dropdownoperatingsystems'])
|| strpos($field['type'], 'models') !== false
&& $value == 0
|| in_array($field['type'], ['date', 'datetime'])
&& $value == 'NULL')) {
Expand Down Expand Up @@ -1607,24 +1608,16 @@ static function getAddSearchOptions($itemtype, $containers_id = false) {
$opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item";
}

if ($data['type'] === "dropdowncomputermodels") {
$opt[$i]['table'] = 'glpi_computermodels';
$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";
}
if(strpos($data['type'], 'models')){
$class = str_replace(['dropdown', 'model'], ['', 'Model'] , $data['type']);
$class = substr($class, 0, -1); //remove last 's'
$class = ucfirst($class); //uppercase for first char

if ($data['type'] === "dropdownnetworkmodels") {
$opt[$i]['table'] = 'glpi_networkequipmentmodels';
$opt[$i]['table'] = CommonDBTM::getTable($class);
$opt[$i]['field'] = 'name';
$opt[$i]['linkfield'] = $data['name'];
$opt[$i]['right'] = 'all';
$opt[$i]['datatype'] = "dropdown";

$opt[$i]['forcegroupby'] = true;

Expand Down Expand Up @@ -1660,7 +1653,12 @@ static function getAddSearchOptions($itemtype, $containers_id = false) {
$opt[$i]['datatype'] = 'weblink';
break;
default:
$opt[$i]['datatype'] = "string";

if (strpos($data['type'], 'model') !== false) {
$opt[$i]['datatype'] = "dropdown";
} else {
$opt[$i]['datatype'] = "string";
}
}

$i++;
Expand Down
26 changes: 23 additions & 3 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,15 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true
$field['itemtype'] = self::getType();
$field['label'] = PluginFieldsLabelTranslation::getLabelFor($field);

//compute model class name for generic field "Model"
$model_class = '';
if (strpos($field['type'], 'models') !== false ){
$model_class = str_replace(['dropdown', 'model'], ['', 'Model'] , $field['type']);
$model_class = substr($model_class, 0, -1); //remove last 's'
$model_class = ucfirst($model_class); //uppercase for first char
}
$field['model_class'] = $model_class;

//get value
$value = null;
if (is_array($found_v)) {
Expand Down Expand Up @@ -807,7 +816,9 @@ function post_getEmpty() {
}

static function getTypes() {
return [
global $CFG_GLPI;

$type = [
'header' => __("Header", "fields"),
'text' => __("Text (single line)", "fields"),
'textarea' => __("Text (multiples lines)", "fields"),
Expand All @@ -819,9 +830,18 @@ static function getTypes() {
'datetime' => __("Date & time", "fields"),
'dropdownuser' => _n("User", "Users", 2),
'dropdownoperatingsystems' => _n("Operating system", "Operating systems", 2),
'dropdowncomputermodels' => _n('Computer model', 'Computer models', 2),
'dropdownnetworkmodels' => _n('Networking equipment model', 'Networking equipment models', 2),
];

foreach ($CFG_GLPI['asset_types'] as $class) {
$itemtype = new $class();
$model_class = $itemtype->getModelClass();
if ($model_class != null) {
$type['dropdown'.strtolower($model_class).'s'] = $model_class::getTypeName(2);
}

}

return $type;
}

function post_addItem() {
Expand Down
12 changes: 10 additions & 2 deletions inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ function migrateCustomfieldTypes($old_type) {
}

static function getSQLType($field_type) {
global $CFG_GLPI;

$types = [
'text' => 'VARCHAR(255) DEFAULT NULL',
'url' => 'TEXT DEFAULT NULL',
Expand All @@ -89,10 +91,16 @@ static function getSQLType($field_type) {
'datetime' => 'VARCHAR(255) DEFAULT NULL',
'dropdownuser' => 'INT NOT NULL DEFAULT 0',
'dropdownoperatingsystems' => 'INT NOT NULL DEFAULT 0',
'dropdowncomputermodels' => 'INT NOT NULL DEFAULT 0',
'dropdownnetworkmodels' => 'INT NOT NULL DEFAULT 0',
];

foreach ($CFG_GLPI['asset_types'] as $class) {
$itemtype = new $class();
$model_class = $itemtype->getModelClass();
if ($model_class != null) {
$types['dropdown'.strtolower($model_class).'s'] = 'INT NOT NULL DEFAULT 0';
}
}

return $types[$field_type];
}
}
10 changes: 3 additions & 7 deletions templates/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
{% set label = field['label'] %}
{% set value = field['value'] %}
{% set readonly = field['is_readonly'] %}
{% set model_class = field['model_class'] %}

{% set field_options = {
'readonly': readonly or not canedit,
Expand Down Expand Up @@ -106,14 +107,9 @@
})) }}
{% endif %}

{% elseif type == 'dropdowncomputermodels' %}
{% elseif 'models' in type %}
{% if not massiveaction %}
{{ macros.dropdownField('ComputerModel', name, value, label, field_options)}}
{% endif %}

{% elseif type == 'dropdownnetworkmodels' %}
{% if not massiveaction %}
{{ macros.dropdownField('NetworkEquipmentModel', name, value, label, field_options) }}
{{ macros.dropdownField(model_class, name, value, label, field_options)}}
{% endif %}
{% endif %}

Expand Down

0 comments on commit 3343d3c

Please sign in to comment.