Skip to content

Commit c1ce2ad

Browse files
committed
HL API integration
1 parent 43db9db commit c1ce2ad

File tree

3 files changed

+196
-1
lines changed

3 files changed

+196
-1
lines changed

hook.php

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

31+
use Glpi\Api\HL\Doc as Doc;
32+
3133
// Plugin hook after *Uninstall*
3234
function plugin_uninstall_after_tag($item) {
3335
$tagitem = new PluginTagTagItem();
@@ -276,3 +278,50 @@ function plugin_tag_getRuleActions($params = [])
276278

277279
return $actions;
278280
}
281+
282+
function plugin_tag_redefine_api_schemas(array $data): array {
283+
foreach ($data['schemas'] as &$schema) {
284+
if (!isset($schema['x-itemtype'])) {
285+
continue;
286+
}
287+
if (PluginTagTag::canItemtype($schema['x-itemtype'])) {
288+
$schema['properties']['tags'] = [
289+
'type' => Doc\Schema::TYPE_ARRAY,
290+
'description' => 'Tags',
291+
'items' => [
292+
'type' => Doc\Schema::TYPE_OBJECT,
293+
'x-join' => [
294+
// This is the join with the desired data
295+
'table' => PluginTagTag::getTable(),
296+
'fkey' => 'plugin_tag_tags_id',
297+
'field' => 'id',
298+
'ref_join' => [
299+
// This is the linking join between the main item and the data needed
300+
'table' => PluginTagTagItem::getTable(),
301+
'fkey' => 'id',
302+
'field' => 'items_id',
303+
'condition' => [
304+
'itemtype' => $schema['x-itemtype']
305+
],
306+
]
307+
],
308+
'properties' => [
309+
'id' => [
310+
'type' => Doc\Schema::TYPE_INTEGER,
311+
'description' => 'ID',
312+
],
313+
'name' => [
314+
'type' => Doc\Schema::TYPE_STRING,
315+
'description' => 'Name',
316+
],
317+
'comment' => [
318+
'type' => Doc\Schema::TYPE_STRING,
319+
'description' => 'Comment',
320+
],
321+
]
322+
]
323+
];
324+
}
325+
}
326+
return $data;
327+
}

inc/apicontroller.class.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Tag plugin for GLPI
6+
* -------------------------------------------------------------------------
7+
*
8+
* LICENSE
9+
*
10+
* This file is part of Tag.
11+
*
12+
* Tag is free software; you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation; either version 2 of the License, or
15+
* (at your option) any later version.
16+
*
17+
* Tag is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with Tag. If not, see <http://www.gnu.org/licenses/>.
24+
* -------------------------------------------------------------------------
25+
* @copyright Copyright (C) 2014-2022 by Teclib'.
26+
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
27+
* @link https://github.com/pluginsGLPI/tag
28+
* -------------------------------------------------------------------------
29+
*/
30+
31+
use Glpi\Api\HL\Controller\AbstractController;
32+
use Glpi\Api\HL\Doc as Doc;
33+
use Glpi\Api\HL\Route;
34+
use Glpi\Http\Request;
35+
use Glpi\Http\Response;
36+
37+
#[Route(path: '/Tag', priority: 1, tags: ['Tag'])]
38+
final class PluginTagApicontroller extends AbstractController
39+
{
40+
protected static function getRawKnownSchemas(): array
41+
{
42+
return [
43+
'Tag' => [
44+
'type' => Doc\Schema::TYPE_OBJECT,
45+
'x-itemtype' => PluginTagTag::class,
46+
'properties' => [
47+
'id' => [
48+
'type' => Doc\Schema::TYPE_INTEGER,
49+
'format' => Doc\Schema::FORMAT_INTEGER_INT64,
50+
'x-readonly' => true,
51+
],
52+
'name' => ['type' => Doc\Schema::TYPE_STRING],
53+
'comment' => ['type' => Doc\Schema::TYPE_STRING],
54+
]
55+
]
56+
];
57+
}
58+
59+
#[Route(path: '/', methods: ['GET'])]
60+
#[Doc\Route(
61+
description: 'List or search tags'
62+
)]
63+
public function getTags(Request $request): Response
64+
{
65+
return $this->searchBySchema($this->getKnownSchema('Tag'), $request->getParameters());
66+
}
67+
68+
#[Route(path: '/{id}', methods: ['GET'], requirements: ['id' => '\d+'])]
69+
#[Doc\Route(
70+
description: 'Get a tag by ID',
71+
parameters: [
72+
[
73+
'name' => 'id',
74+
'description' => 'The ID of the tag',
75+
'location' => Doc\Parameter::LOCATION_PATH,
76+
'schema' => ['type' => Doc\Schema::TYPE_INTEGER]
77+
]
78+
]
79+
)]
80+
public function getTag(Request $request, int $id): Response
81+
{
82+
return $this->getOneBySchema($this->getKnownSchema('Tag'), $request->getAttributes(), $request->getParameters(), $id);
83+
}
84+
85+
#[Route(path: '/', methods: ['POST'])]
86+
#[Doc\Route(description: 'Create a new tag', parameters: [
87+
[
88+
'name' => '_',
89+
'location' => Doc\Parameter::LOCATION_BODY,
90+
'type' => Doc\Schema::TYPE_OBJECT,
91+
'schema' => 'Tag',
92+
]
93+
])]
94+
public function createTag(Request $request): Response
95+
{
96+
return $this->createBySchema($this->getKnownSchema('Tag'), $request->getParameters(), 'getTag');
97+
}
98+
99+
#[Route(path: '/{id}', methods: ['PATCH'], requirements: ['id' => '\d+'])]
100+
#[Doc\Route(
101+
description: 'Update a tag by ID',
102+
parameters: [
103+
[
104+
'name' => 'id',
105+
'description' => 'The ID of the tag',
106+
'location' => Doc\Parameter::LOCATION_PATH,
107+
'schema' => ['type' => Doc\Schema::TYPE_INTEGER]
108+
],
109+
[
110+
'name' => '_',
111+
'location' => Doc\Parameter::LOCATION_BODY,
112+
'type' => Doc\Schema::TYPE_OBJECT,
113+
'schema' => 'Tag',
114+
]
115+
],
116+
responses: [
117+
['schema' => 'Tag']
118+
]
119+
)]
120+
public function updateTag(Request $request, int $id): Response
121+
{
122+
return $this->updateBySchema($this->getKnownSchema('Tag'), $request->getAttributes(), $request->getParameters(), $id);
123+
}
124+
125+
#[Route(path: '/{id}', methods: ['DELETE'], requirements: ['id' => '\d+'])]
126+
#[Doc\Route(
127+
description: 'Delete a tag by ID',
128+
parameters: [
129+
[
130+
'name' => 'id',
131+
'description' => 'The ID of the tag',
132+
'location' => Doc\Parameter::LOCATION_PATH,
133+
'schema' => ['type' => Doc\Schema::TYPE_INTEGER]
134+
]
135+
]
136+
)]
137+
public function deleteTag(Request $request, int $id): Response
138+
{
139+
return $this->deleteBySchema($this->getKnownSchema('Tag'), $request->getAttributes(), $request->getParameters(), $id);
140+
}
141+
}

setup.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// Minimal GLPI version, inclusive
3636
define("PLUGIN_TAG_MIN_GLPI", "10.0.0");
3737
// Maximum GLPI version, exclusive
38-
define("PLUGIN_TAG_MAX_GLPI", "10.0.99");
38+
define("PLUGIN_TAG_MAX_GLPI", "10.1.99");
3939

4040
/**
4141
* Init hooks of the plugin.
@@ -135,6 +135,11 @@ function plugin_init_tag() {
135135
$PLUGIN_HOOKS['add_javascript']['tag'][] = 'js/entity.js';
136136
}
137137

138+
$PLUGIN_HOOKS[Hooks::REDEFINE_API_SCHEMAS]['tag'] = 'plugin_tag_redefine_api_schemas';
139+
$PLUGIN_HOOKS[Hooks::API_CONTROLLERS]['tag'] = [
140+
PluginTagApicontroller::class
141+
];
142+
138143
Plugin::registerClass('PluginTagProfile', ['addtabon' => ['Profile']]);
139144
Plugin::registerClass('PluginTagConfig', ['addtabon' => 'Config']);
140145

0 commit comments

Comments
 (0)