@@ -299,7 +299,7 @@ function plugin_fields_rule_matched($params = [])
299
299
300
300
function plugin_fields_giveItem ($ itemtype , $ ID , $ data , $ num )
301
301
{
302
- $ searchopt = & Search::getOptions ($ itemtype );
302
+ $ searchopt = Search::getOptions ($ itemtype );
303
303
$ table = $ searchopt [$ ID ]["table " ];
304
304
305
305
//fix glpi default Search::giveItem who for empty date display "--"
@@ -350,10 +350,14 @@ function plugin_fields_redefine_api_schemas(array $data): array
350
350
};
351
351
};
352
352
353
+ $ new_schemas = [];
354
+
353
355
foreach ($ data ['schemas ' ] as &$ schema ) {
354
356
if (!isset ($ schema ['x-itemtype ' ])) {
355
357
continue ;
356
358
}
359
+ $ itemtype = $ schema ['x-itemtype ' ];
360
+ $ schema_name = $ itemtype . '_CustomFields ' ;
357
361
//Note PluginFieldsContainer::findContainer already checks permissions
358
362
$ container_id = PluginFieldsContainer::findContainer ($ schema ['x-itemtype ' ], 'dom ' );
359
363
if ($ container_id !== null ) {
@@ -377,45 +381,78 @@ function plugin_fields_redefine_api_schemas(array $data): array
377
381
]
378
382
]);
379
383
if (count ($ it )) {
380
- $ custom_fields = [];
381
384
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
+ }
382
391
$ type_format = $ fn_fieldTypeToAPIType ($ field ['type ' ]);
383
392
$ 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
+ ],
400
415
],
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
+ ]
407
426
]
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
+ }
410
445
}
411
- if (count ($ custom_fields ) ) {
446
+ if (isset ( $ new_schemas [ $ schema_name ]) && count ($ new_schemas [ $ schema_name ][ ' properties ' ]) > 0 ) {
412
447
$ 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 ' ],
415
451
];
416
452
}
417
453
}
418
454
}
419
455
}
456
+ $ data ['schemas ' ] = array_merge ($ data ['schemas ' ], $ new_schemas );
420
457
return $ data ;
421
458
}
0 commit comments