Skip to content

Commit 5970d3c

Browse files
committed
finalize schema changes (hopfully)
1 parent 41a8af3 commit 5970d3c

File tree

1 file changed

+66
-29
lines changed

1 file changed

+66
-29
lines changed

hook.php

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ function plugin_fields_rule_matched($params = [])
299299

300300
function plugin_fields_giveItem($itemtype, $ID, $data, $num)
301301
{
302-
$searchopt = &Search::getOptions($itemtype);
302+
$searchopt = Search::getOptions($itemtype);
303303
$table = $searchopt[$ID]["table"];
304304

305305
//fix glpi default Search::giveItem who for empty date display "--"
@@ -350,10 +350,14 @@ function plugin_fields_redefine_api_schemas(array $data): array
350350
};
351351
};
352352

353+
$new_schemas = [];
354+
353355
foreach ($data['schemas'] as &$schema) {
354356
if (!isset($schema['x-itemtype'])) {
355357
continue;
356358
}
359+
$itemtype = $schema['x-itemtype'];
360+
$schema_name = $itemtype . '_CustomFields';
357361
//Note PluginFieldsContainer::findContainer already checks permissions
358362
$container_id = PluginFieldsContainer::findContainer($schema['x-itemtype'], 'dom');
359363
if ($container_id !== null) {
@@ -377,45 +381,78 @@ function plugin_fields_redefine_api_schemas(array $data): array
377381
]
378382
]);
379383
if (count($it)) {
380-
$custom_fields = [];
381384
foreach ($it as $field) {
385+
if (!isset($new_schemas[$schema_name])) {
386+
$new_schemas[$schema_name] = [
387+
'type' => Schema::TYPE_OBJECT,
388+
'properties' => []
389+
];
390+
}
382391
$type_format = $fn_fieldTypeToAPIType($field['type']);
383392
$table = strtolower("glpi_plugin_fields_{$schema['x-itemtype']}{$field['container_name']}s");
384-
$custom_fields[$field['name']] = [
385-
'type' => Schema::TYPE_OBJECT,
386-
'x-join' => [
387-
// This is the table with the desired values
388-
'table' => $table,
389-
'fkey' => 'id',
390-
'field' => 'items_id',
391-
'condition' => [
392-
'itemtype' => $schema['x-itemtype'],
393-
]
394-
],
395-
'properties' => [
396-
'id' => [
397-
'type' => Schema::TYPE_INTEGER,
398-
'format' => Schema::FORMAT_INTEGER_INT64,
399-
'x-readonly' => true,
393+
$sql_field = $field['name'];
394+
if (str_starts_with($field['type'], 'dropdown')) {
395+
if (str_starts_with($field['type'], 'dropdown-')) {
396+
$dropdown_type = explode('-', $field['type'], 2)[1];
397+
} else {
398+
$dropdown_type = 'PluginFields' . ucfirst($field['name']) . 'Dropdown';
399+
}
400+
$is_tree = is_subclass_of($dropdown_type, CommonTreeDropdown::class);
401+
$new_schemas[$schema_name]['properties'][$field['name']] = [
402+
'type' => Schema::TYPE_OBJECT,
403+
'x-join' => [
404+
'table' => $dropdown_type::getTable(), // This is the table with the desired values
405+
'field' => 'id',
406+
'fkey' => $dropdown_type::getForeignKeyField(),
407+
'ref_join' => [
408+
'table' => $table,
409+
'fkey' => 'id',
410+
'field' => 'items_id',
411+
'condition' => [
412+
'itemtype' => $schema['x-itemtype'],
413+
]
414+
],
400415
],
401-
'value' => [
402-
'x-field' => $field['name'],
403-
'type' => $type_format[0],
404-
'format' => $type_format[1],
405-
// No support to change these fields for now.
406-
'x-readonly' => true,
416+
'properties' => [
417+
'id' => [
418+
'type' => Schema::TYPE_INTEGER,
419+
'format' => Schema::FORMAT_INTEGER_INT64,
420+
'x-readonly' => true,
421+
],
422+
'value' => [
423+
'type' => Schema::TYPE_STRING,
424+
'x-field' => $is_tree ? 'completename' : 'name',
425+
]
407426
]
408-
]
409-
];
427+
];
428+
} else {
429+
$new_schemas[$schema_name]['properties'][$field['name']] = [
430+
'type' => $type_format[0],
431+
'format' => $type_format[1],
432+
'x-join' => [
433+
// This is the table with the desired values
434+
'table' => $table,
435+
'fkey' => 'id',
436+
'field' => 'items_id',
437+
'condition' => [
438+
'itemtype' => $schema['x-itemtype'],
439+
]
440+
],
441+
'x-field' => $sql_field,
442+
'x-readonly' => true
443+
];
444+
}
410445
}
411-
if (count($custom_fields)) {
446+
if (isset($new_schemas[$schema_name]) && count($new_schemas[$schema_name]['properties']) > 0) {
412447
$schema['properties']['custom_fields'] = [
413-
'type' => 'object',
414-
'properties' => $custom_fields
448+
'type' => Schema::TYPE_OBJECT,
449+
'x-full-schema' => $schema_name,
450+
'properties' => $new_schemas[$schema_name]['properties'],
415451
];
416452
}
417453
}
418454
}
419455
}
456+
$data['schemas'] = array_merge($data['schemas'], $new_schemas);
420457
return $data;
421458
}

0 commit comments

Comments
 (0)