Skip to content

Commit 04f5f98

Browse files
committed
HL API integration
1 parent 14f4664 commit 04f5f98

File tree

2 files changed

+177
-85
lines changed

2 files changed

+177
-85
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
*
@@ -323,3 +325,88 @@ function plugin_datainjection_populate_fields()
323325
}
324326
}
325327
}
328+
329+
function plugin_fields_redefine_api_schemas(array $data): array
330+
{
331+
global $DB;
332+
333+
$fn_fieldTypeToAPIType = static function (string $type): array {
334+
$type = explode('-', $type)[0];
335+
return match ($type) {
336+
'number' => [Schema::TYPE_NUMBER, Schema::FORMAT_NUMBER_FLOAT],
337+
'yesno' => [Schema::TYPE_BOOLEAN, Schema::FORMAT_BOOLEAN_BOOLEAN],
338+
'date' => [Schema::TYPE_STRING, Schema::FORMAT_STRING_DATE],
339+
'datetime' => [Schema::TYPE_STRING, Schema::FORMAT_STRING_DATE_TIME],
340+
default => [Schema::TYPE_STRING, Schema::FORMAT_STRING_STRING],
341+
};
342+
};
343+
344+
foreach ($data['schemas'] as &$schema) {
345+
if (!isset($schema['x-itemtype'])) {
346+
continue;
347+
}
348+
//Note PluginFieldsContainer::findContainer already checks permissions
349+
$container_id = PluginFieldsContainer::findContainer($schema['x-itemtype'], 'dom');
350+
if ($container_id !== null) {
351+
$it = $DB->request([
352+
'SELECT' => [
353+
'glpi_plugin_fields_fields.*',
354+
'glpi_plugin_fields_containers.name AS container_name',
355+
],
356+
'FROM' => 'glpi_plugin_fields_fields',
357+
'LEFT JOIN' => [
358+
'glpi_plugin_fields_containers' => [
359+
'ON' => [
360+
'glpi_plugin_fields_fields' => 'plugin_fields_containers_id',
361+
'glpi_plugin_fields_containers' => 'id'
362+
]
363+
]
364+
],
365+
'WHERE' => [
366+
'plugin_fields_containers_id' => $container_id,
367+
'glpi_plugin_fields_fields.is_active' => 1
368+
]
369+
]);
370+
if (count($it)) {
371+
$custom_fields = [];
372+
foreach ($it as $field) {
373+
$type_format = $fn_fieldTypeToAPIType($field['type']);
374+
$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,
391+
],
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,
398+
]
399+
]
400+
];
401+
}
402+
if (count($custom_fields)) {
403+
$schema['properties']['custom_fields'] = [
404+
'type' => 'object',
405+
'properties' => $custom_fields
406+
];
407+
}
408+
}
409+
}
410+
}
411+
return $data;
412+
}

setup.php

Lines changed: 90 additions & 85 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,102 +86,106 @@ 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] = stripcslashes($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] = stripcslashes($value);
107+
}
103108
}
104109
}
105-
}
106-
107-
if (Plugin::isPluginActive('fusioninventory')) {
108-
$PLUGIN_HOOKS['fusioninventory_inventory']['fields']
109-
= ['PluginFieldsInventory', 'updateInventory'];
110-
}
111110

112-
// complete rule engine
113-
$PLUGIN_HOOKS['use_rules']['fields'] = ['PluginFusioninventoryTaskpostactionRule'];
114-
$PLUGIN_HOOKS['rule_matched']['fields'] = 'plugin_fields_rule_matched';
115-
116-
if (isset($_SESSION['glpiactiveentities'])) {
117-
// add link in plugin page
118-
$PLUGIN_HOOKS['config_page']['fields'] = 'front/container.php';
119-
120-
// add entry to configuration menu
121-
$PLUGIN_HOOKS["menu_toadd"]['fields'] = ['config' => 'PluginFieldsMenu'];
122-
123-
// add tabs to itemtypes
124-
Plugin::registerClass(
125-
'PluginFieldsContainer',
126-
['addtabon' => array_unique(PluginFieldsContainer::getEntries())]
127-
);
128-
129-
//include js and css
130-
$debug = (isset($_SESSION['glpi_use_mode'])
131-
&& $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE ? true : false);
132-
if (!$debug && file_exists(__DIR__ . '/css/fields.min.css')) {
133-
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.min.css';
134-
} else {
135-
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.css';
111+
if (Plugin::isPluginActive('fusioninventory')) {
112+
$PLUGIN_HOOKS['fusioninventory_inventory']['fields']
113+
= ['PluginFieldsInventory', 'updateInventory'];
136114
}
137115

138-
// Add/delete profiles to automaticaly to container
139-
$PLUGIN_HOOKS['item_add']['fields']['Profile'] = ["PluginFieldsProfile", "addNewProfile"];
140-
$PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = ["PluginFieldsProfile", "deleteProfile"];
116+
// complete rule engine
117+
$PLUGIN_HOOKS['use_rules']['fields'] = ['PluginFusioninventoryTaskpostactionRule'];
118+
$PLUGIN_HOOKS['rule_matched']['fields'] = 'plugin_fields_rule_matched';
119+
120+
if (isset($_SESSION['glpiactiveentities'])) {
121+
// add link in plugin page
122+
$PLUGIN_HOOKS['config_page']['fields'] = 'front/container.php';
123+
124+
// add entry to configuration menu
125+
$PLUGIN_HOOKS["menu_toadd"]['fields'] = ['config' => 'PluginFieldsMenu'];
126+
127+
// add tabs to itemtypes
128+
Plugin::registerClass(
129+
'PluginFieldsContainer',
130+
['addtabon' => array_unique(PluginFieldsContainer::getEntries())]
131+
);
132+
133+
//include js and css
134+
$debug = (isset($_SESSION['glpi_use_mode'])
135+
&& $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE ? true : false);
136+
if (!$debug && file_exists(__DIR__ . '/css/fields.min.css')) {
137+
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.min.css';
138+
} else {
139+
$PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.css';
140+
}
141+
142+
// Add/delete profiles to automaticaly to container
143+
$PLUGIN_HOOKS['item_add']['fields']['Profile'] = ["PluginFieldsProfile", "addNewProfile"];
144+
$PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = ["PluginFieldsProfile", "deleteProfile"];
141145

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

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

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

179-
// Display fields in any existing tab
180-
$PLUGIN_HOOKS['post_item_form']['fields'] = [
181-
'PluginFieldsField',
182-
'showForTab'
183-
];
183+
// Display fields in any existing tab
184+
$PLUGIN_HOOKS['post_item_form']['fields'] = [
185+
'PluginFieldsField',
186+
'showForTab'
187+
];
188+
}
184189
}
185190
}
186191

0 commit comments

Comments
 (0)