Skip to content

Commit 41a8af3

Browse files
committed
HL API integration
1 parent bfb97f8 commit 41a8af3

File tree

2 files changed

+176
-84
lines changed

2 files changed

+176
-84
lines changed

hook.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* -------------------------------------------------------------------------
2929
*/
3030

31+
use Glpi\Api\HL\Doc\Schema;
32+
3133
/**
3234
* Plugin install process
3335
*
@@ -332,3 +334,88 @@ function plugin_datainjection_populate_fields()
332334
}
333335
}
334336
}
337+
338+
function plugin_fields_redefine_api_schemas(array $data): array
339+
{
340+
global $DB;
341+
342+
$fn_fieldTypeToAPIType = static function (string $type): array {
343+
$type = explode('-', $type)[0];
344+
return match ($type) {
345+
'number' => [Schema::TYPE_NUMBER, Schema::FORMAT_NUMBER_FLOAT],
346+
'yesno' => [Schema::TYPE_BOOLEAN, Schema::FORMAT_BOOLEAN_BOOLEAN],
347+
'date' => [Schema::TYPE_STRING, Schema::FORMAT_STRING_DATE],
348+
'datetime' => [Schema::TYPE_STRING, Schema::FORMAT_STRING_DATE_TIME],
349+
default => [Schema::TYPE_STRING, Schema::FORMAT_STRING_STRING],
350+
};
351+
};
352+
353+
foreach ($data['schemas'] as &$schema) {
354+
if (!isset($schema['x-itemtype'])) {
355+
continue;
356+
}
357+
//Note PluginFieldsContainer::findContainer already checks permissions
358+
$container_id = PluginFieldsContainer::findContainer($schema['x-itemtype'], 'dom');
359+
if ($container_id !== null) {
360+
$it = $DB->request([
361+
'SELECT' => [
362+
'glpi_plugin_fields_fields.*',
363+
'glpi_plugin_fields_containers.name AS container_name',
364+
],
365+
'FROM' => 'glpi_plugin_fields_fields',
366+
'LEFT JOIN' => [
367+
'glpi_plugin_fields_containers' => [
368+
'ON' => [
369+
'glpi_plugin_fields_fields' => 'plugin_fields_containers_id',
370+
'glpi_plugin_fields_containers' => 'id'
371+
]
372+
]
373+
],
374+
'WHERE' => [
375+
'plugin_fields_containers_id' => $container_id,
376+
'glpi_plugin_fields_fields.is_active' => 1
377+
]
378+
]);
379+
if (count($it)) {
380+
$custom_fields = [];
381+
foreach ($it as $field) {
382+
$type_format = $fn_fieldTypeToAPIType($field['type']);
383+
$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,
400+
],
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,
407+
]
408+
]
409+
];
410+
}
411+
if (count($custom_fields)) {
412+
$schema['properties']['custom_fields'] = [
413+
'type' => 'object',
414+
'properties' => $custom_fields
415+
];
416+
}
417+
}
418+
}
419+
}
420+
return $data;
421+
}

setup.php

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// Minimal GLPI version, inclusive
3434
define("PLUGIN_FIELDS_MIN_GLPI", "10.0.0");
3535
// Maximum GLPI version, exclusive
36-
define("PLUGIN_FIELDS_MAX_GLPI", "10.0.99");
36+
define("PLUGIN_FIELDS_MAX_GLPI", "10.1.99");
3737

3838
if (!defined("PLUGINFIELDS_DIR")) {
3939
define("PLUGINFIELDS_DIR", Plugin::getPhpDir("fields"));
@@ -63,6 +63,7 @@
6363
mkdir(PLUGINFIELDS_FRONT_PATH);
6464
}
6565

66+
use Glpi\Plugin\Hooks;
6667
use Symfony\Component\Yaml\Yaml;
6768

6869
/**
@@ -85,105 +86,109 @@ function plugin_init_fields()
8586
$pluginfields_autoloader = new PluginFieldsAutoloader([PLUGINFIELDS_CLASS_PATH]);
8687
$pluginfields_autoloader->register();
8788

88-
if (Session::getLoginUserID() && Plugin::isPluginActive('fields')) {
89-
// Init hook about itemtype(s) for plugin fields
90-
if (!isset($PLUGIN_HOOKS['plugin_fields'])) {
91-
$PLUGIN_HOOKS['plugin_fields'] = [];
92-
}
89+
if (Plugin::isPluginActive('fields')) {
90+
// This API integration cannot be done inside a login check since the plugin is initialized before the Router handles authentication
91+
$PLUGIN_HOOKS[Hooks::REDEFINE_API_SCHEMAS]['fields'] = 'plugin_fields_redefine_api_schemas';
92+
if (Session::getLoginUserID()) {
93+
// Init hook about itemtype(s) for plugin fields
94+
if (!isset($PLUGIN_HOOKS['plugin_fields'])) {
95+
$PLUGIN_HOOKS['plugin_fields'] = [];
96+
}
9397

94-
// When a Category is changed during ticket creation
95-
if (
96-
isset($_POST) && !empty($_POST)
97-
&& isset($_POST['_plugin_fields_type'])
98-
&& $_SERVER['REQUEST_URI'] ?? '' == Ticket::getFormURL()
99-
) {
100-
foreach ($_POST as $key => $value) {
101-
if (!is_array($value)) {
102-
$_SESSION['plugin']['fields']['values_sent'][$key] = $value;
98+
// When a Category is changed during ticket creation
99+
if (
100+
isset($_POST) && !empty($_POST)
101+
&& isset($_POST['_plugin_fields_type'])
102+
&& $_SERVER['REQUEST_URI'] ?? '' == Ticket::getFormURL()
103+
) {
104+
foreach ($_POST as $key => $value) {
105+
if (!is_array($value)) {
106+
$_SESSION['plugin']['fields']['values_sent'][$key] = $value;
107+
}
103108
}
104109
}
105-
}
106110

107-
if (Plugin::isPluginActive('fusioninventory')) {
108-
$PLUGIN_HOOKS['fusioninventory_inventory']['fields']
109-
= ['PluginFieldsInventory', 'updateInventory'];
110-
}
111+
if (Plugin::isPluginActive('fusioninventory')) {
112+
$PLUGIN_HOOKS['fusioninventory_inventory']['fields']
113+
= ['PluginFieldsInventory', 'updateInventory'];
114+
}
111115

112-
// complete rule engine
113-
$PLUGIN_HOOKS['use_rules']['fields'] = ['PluginFusioninventoryTaskpostactionRule'];
114-
$PLUGIN_HOOKS['rule_matched']['fields'] = 'plugin_fields_rule_matched';
116+
// complete rule engine
117+
$PLUGIN_HOOKS['use_rules']['fields'] = ['PluginFusioninventoryTaskpostactionRule'];
118+
$PLUGIN_HOOKS['rule_matched']['fields'] = 'plugin_fields_rule_matched';
115119

116-
if (isset($_SESSION['glpiactiveentities'])) {
117-
// add link in plugin page
118-
$PLUGIN_HOOKS['config_page']['fields'] = 'front/container.php';
120+
if (isset($_SESSION['glpiactiveentities'])) {
121+
// add link in plugin page
122+
$PLUGIN_HOOKS['config_page']['fields'] = 'front/container.php';
119123

120-
// add entry to configuration menu
121-
$PLUGIN_HOOKS["menu_toadd"]['fields'] = ['config' => 'PluginFieldsMenu'];
124+
// add entry to configuration menu
125+
$PLUGIN_HOOKS["menu_toadd"]['fields'] = ['config' => 'PluginFieldsMenu'];
122126

123-
// add tabs to itemtypes
124-
$itemtypes = array_unique(PluginFieldsContainer::getEntries());
125-
if (count($itemtypes) > 0) {
126-
Plugin::registerClass(
127-
'PluginFieldsContainer',
128-
['addtabon' => $itemtypes]
129-
);
130-
}
127+
// add tabs to itemtypes
128+
$itemtypes = array_unique(PluginFieldsContainer::getEntries());
129+
if (count($itemtypes) > 0) {
130+
Plugin::registerClass(
131+
'PluginFieldsContainer',
132+
['addtabon' => $itemtypes]
133+
);
134+
}
131135

132-
//include js and css
133-
$debug = (isset($_SESSION['glpi_use_mode'])
134-
&& $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE ? true : false);
135-
if (!$debug && file_exists(__DIR__ . '/css/fields.min.css')) {
136-
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.min.css';
137-
} else {
138-
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.css';
139-
}
136+
//include js and css
137+
$debug = (isset($_SESSION['glpi_use_mode'])
138+
&& $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE ? true : false);
139+
if (!$debug && file_exists(__DIR__ . '/css/fields.min.css')) {
140+
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.min.css';
141+
} else {
142+
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.css';
143+
}
140144

141-
// Add/delete profiles to automaticaly to container
142-
$PLUGIN_HOOKS['item_add']['fields']['Profile'] = ["PluginFieldsProfile", "addNewProfile"];
143-
$PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = ["PluginFieldsProfile", "deleteProfile"];
145+
// Add/delete profiles to automaticaly to container
146+
$PLUGIN_HOOKS['item_add']['fields']['Profile'] = ["PluginFieldsProfile", "addNewProfile"];
147+
$PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = ["PluginFieldsProfile", "deleteProfile"];
144148

145-
//load drag and drop javascript library on Package Interface
146-
$PLUGIN_HOOKS['add_javascript']['fields'][] = "lib/redips-drag-min.js";
147-
if (!$debug && file_exists(__DIR__ . '/js/drag-field-row.min.js')) {
148-
$PLUGIN_HOOKS['add_javascript']['fields'][] = 'js/drag-field-row.min.js';
149-
} else {
150-
$PLUGIN_HOOKS['add_javascript']['fields'][] = 'js/drag-field-row.js';
149+
//load drag and drop javascript library on Package Interface
150+
$PLUGIN_HOOKS['add_javascript']['fields'][] = "lib/redips-drag-min.js";
151+
if (!$debug && file_exists(__DIR__ . '/js/drag-field-row.min.js')) {
152+
$PLUGIN_HOOKS['add_javascript']['fields'][] = 'js/drag-field-row.min.js';
153+
} else {
154+
$PLUGIN_HOOKS['add_javascript']['fields'][] = 'js/drag-field-row.js';
155+
}
151156
}
152-
}
153157

154-
// Add Fields to Datainjection
155-
if (Plugin::isPluginActive('datainjection')) {
156-
$PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] = "plugin_datainjection_populate_fields";
157-
}
158+
// Add Fields to Datainjection
159+
if (Plugin::isPluginActive('datainjection')) {
160+
$PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] = "plugin_datainjection_populate_fields";
161+
}
158162

159-
//Retrieve dom container
160-
$itemtypes = PluginFieldsContainer::getUsedItemtypes();
161-
if ($itemtypes !== false) {
162-
foreach ($itemtypes as $itemtype) {
163-
$PLUGIN_HOOKS['pre_item_update']['fields'][$itemtype] = [
164-
"PluginFieldsContainer",
165-
"preItemUpdate"
166-
];
167-
$PLUGIN_HOOKS['pre_item_add']['fields'][$itemtype] = [
168-
"PluginFieldsContainer",
169-
"preItem"
170-
];
171-
$PLUGIN_HOOKS['item_add']['fields'][$itemtype] = [
172-
"PluginFieldsContainer",
173-
"postItemAdd"
174-
];
175-
$PLUGIN_HOOKS['pre_item_purge'] ['fields'][$itemtype] = [
176-
"PluginFieldsContainer",
177-
"preItemPurge"
178-
];
163+
//Retrieve dom container
164+
$itemtypes = PluginFieldsContainer::getUsedItemtypes();
165+
if ($itemtypes !== false) {
166+
foreach ($itemtypes as $itemtype) {
167+
$PLUGIN_HOOKS['pre_item_update']['fields'][$itemtype] = [
168+
"PluginFieldsContainer",
169+
"preItemUpdate"
170+
];
171+
$PLUGIN_HOOKS['pre_item_add']['fields'][$itemtype] = [
172+
"PluginFieldsContainer",
173+
"preItem"
174+
];
175+
$PLUGIN_HOOKS['item_add']['fields'][$itemtype] = [
176+
"PluginFieldsContainer",
177+
"postItemAdd"
178+
];
179+
$PLUGIN_HOOKS['pre_item_purge'] ['fields'][$itemtype] = [
180+
"PluginFieldsContainer",
181+
"preItemPurge"
182+
];
183+
}
179184
}
180-
}
181185

182-
// Display fields in any existing tab
183-
$PLUGIN_HOOKS['post_item_form']['fields'] = [
184-
'PluginFieldsField',
185-
'showForTab'
186-
];
186+
// Display fields in any existing tab
187+
$PLUGIN_HOOKS['post_item_form']['fields'] = [
188+
'PluginFieldsField',
189+
'showForTab'
190+
];
191+
}
187192
}
188193
}
189194

0 commit comments

Comments
 (0)