@@ -290,7 +290,7 @@ function plugin_fields_rule_matched($params = [])
290290
291291function plugin_fields_giveItem ($ itemtype , $ ID , $ data , $ num )
292292{
293- $ searchopt = & Search::getOptions ($ itemtype );
293+ $ searchopt = Search::getOptions ($ itemtype );
294294 $ table = $ searchopt [$ ID ]["table " ];
295295
296296 //fix glpi default Search::giveItem who for empty date display "--"
@@ -341,10 +341,14 @@ function plugin_fields_redefine_api_schemas(array $data): array
341341 };
342342 };
343343
344+ $ new_schemas = [];
345+
344346 foreach ($ data ['schemas ' ] as &$ schema ) {
345347 if (!isset ($ schema ['x-itemtype ' ])) {
346348 continue ;
347349 }
350+ $ itemtype = $ schema ['x-itemtype ' ];
351+ $ schema_name = $ itemtype . '_CustomFields ' ;
348352 //Note PluginFieldsContainer::findContainer already checks permissions
349353 $ container_id = PluginFieldsContainer::findContainer ($ schema ['x-itemtype ' ], 'dom ' );
350354 if ($ container_id !== null ) {
@@ -368,45 +372,78 @@ function plugin_fields_redefine_api_schemas(array $data): array
368372 ]
369373 ]);
370374 if (count ($ it )) {
371- $ custom_fields = [];
372375 foreach ($ it as $ field ) {
376+ if (!isset ($ new_schemas [$ schema_name ])) {
377+ $ new_schemas [$ schema_name ] = [
378+ 'type ' => Schema::TYPE_OBJECT ,
379+ 'properties ' => []
380+ ];
381+ }
373382 $ type_format = $ fn_fieldTypeToAPIType ($ field ['type ' ]);
374383 $ table = strtolower ("glpi_plugin_fields_ {$ schema ['x-itemtype ' ]}{$ field ['container_name ' ]}s " );
375- $ custom_fields [$ field ['name ' ]] = [
376- 'type ' => Schema::TYPE_OBJECT ,
377- 'x-join ' => [
378- // This is the table with the desired values
379- 'table ' => $ table ,
380- 'fkey ' => 'id ' ,
381- 'field ' => 'items_id ' ,
382- 'condition ' => [
383- 'itemtype ' => $ schema ['x-itemtype ' ],
384- ]
385- ],
386- 'properties ' => [
387- 'id ' => [
388- 'type ' => Schema::TYPE_INTEGER ,
389- 'format ' => Schema::FORMAT_INTEGER_INT64 ,
390- 'x-readonly ' => true ,
384+ $ sql_field = $ field ['name ' ];
385+ if (str_starts_with ($ field ['type ' ], 'dropdown ' )) {
386+ if (str_starts_with ($ field ['type ' ], 'dropdown- ' )) {
387+ $ dropdown_type = explode ('- ' , $ field ['type ' ], 2 )[1 ];
388+ } else {
389+ $ dropdown_type = 'PluginFields ' . ucfirst ($ field ['name ' ]) . 'Dropdown ' ;
390+ }
391+ $ is_tree = is_subclass_of ($ dropdown_type , CommonTreeDropdown::class);
392+ $ new_schemas [$ schema_name ]['properties ' ][$ field ['name ' ]] = [
393+ 'type ' => Schema::TYPE_OBJECT ,
394+ 'x-join ' => [
395+ 'table ' => $ dropdown_type ::getTable (), // This is the table with the desired values
396+ 'field ' => 'id ' ,
397+ 'fkey ' => $ dropdown_type ::getForeignKeyField (),
398+ 'ref_join ' => [
399+ 'table ' => $ table ,
400+ 'fkey ' => 'id ' ,
401+ 'field ' => 'items_id ' ,
402+ 'condition ' => [
403+ 'itemtype ' => $ schema ['x-itemtype ' ],
404+ ]
405+ ],
391406 ],
392- 'value ' => [
393- 'x-field ' => $ field ['name ' ],
394- 'type ' => $ type_format [0 ],
395- 'format ' => $ type_format [1 ],
396- // No support to change these fields for now.
397- 'x-readonly ' => true ,
407+ 'properties ' => [
408+ 'id ' => [
409+ 'type ' => Schema::TYPE_INTEGER ,
410+ 'format ' => Schema::FORMAT_INTEGER_INT64 ,
411+ 'x-readonly ' => true ,
412+ ],
413+ 'value ' => [
414+ 'type ' => Schema::TYPE_STRING ,
415+ 'x-field ' => $ is_tree ? 'completename ' : 'name ' ,
416+ ]
398417 ]
399- ]
400- ];
418+ ];
419+ } else {
420+ $ new_schemas [$ schema_name ]['properties ' ][$ field ['name ' ]] = [
421+ 'type ' => $ type_format [0 ],
422+ 'format ' => $ type_format [1 ],
423+ 'x-join ' => [
424+ // This is the table with the desired values
425+ 'table ' => $ table ,
426+ 'fkey ' => 'id ' ,
427+ 'field ' => 'items_id ' ,
428+ 'condition ' => [
429+ 'itemtype ' => $ schema ['x-itemtype ' ],
430+ ]
431+ ],
432+ 'x-field ' => $ sql_field ,
433+ 'x-readonly ' => true
434+ ];
435+ }
401436 }
402- if (count ($ custom_fields ) ) {
437+ if (isset ( $ new_schemas [ $ schema_name ]) && count ($ new_schemas [ $ schema_name ][ ' properties ' ]) > 0 ) {
403438 $ schema ['properties ' ]['custom_fields ' ] = [
404- 'type ' => 'object ' ,
405- 'properties ' => $ custom_fields
439+ 'type ' => Schema::TYPE_OBJECT ,
440+ 'x-full-schema ' => $ schema_name ,
441+ 'properties ' => $ new_schemas [$ schema_name ]['properties ' ],
406442 ];
407443 }
408444 }
409445 }
410446 }
447+ $ data ['schemas ' ] = array_merge ($ data ['schemas ' ], $ new_schemas );
411448 return $ data ;
412449}
0 commit comments