Skip to content

Commit

Permalink
HL API integration
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Jun 28, 2023
1 parent 43db9db commit c1ce2ad
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 1 deletion.
49 changes: 49 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* -------------------------------------------------------------------------
*/

use Glpi\Api\HL\Doc as Doc;

// Plugin hook after *Uninstall*
function plugin_uninstall_after_tag($item) {
$tagitem = new PluginTagTagItem();
Expand Down Expand Up @@ -276,3 +278,50 @@ function plugin_tag_getRuleActions($params = [])

return $actions;
}

function plugin_tag_redefine_api_schemas(array $data): array {
foreach ($data['schemas'] as &$schema) {
if (!isset($schema['x-itemtype'])) {
continue;
}
if (PluginTagTag::canItemtype($schema['x-itemtype'])) {
$schema['properties']['tags'] = [
'type' => Doc\Schema::TYPE_ARRAY,
'description' => 'Tags',
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
'x-join' => [
// This is the join with the desired data
'table' => PluginTagTag::getTable(),
'fkey' => 'plugin_tag_tags_id',
'field' => 'id',
'ref_join' => [
// This is the linking join between the main item and the data needed
'table' => PluginTagTagItem::getTable(),
'fkey' => 'id',
'field' => 'items_id',
'condition' => [
'itemtype' => $schema['x-itemtype']
],
]
],
'properties' => [
'id' => [
'type' => Doc\Schema::TYPE_INTEGER,
'description' => 'ID',
],
'name' => [
'type' => Doc\Schema::TYPE_STRING,
'description' => 'Name',
],
'comment' => [
'type' => Doc\Schema::TYPE_STRING,
'description' => 'Comment',
],
]
]
];
}
}
return $data;
}
141 changes: 141 additions & 0 deletions inc/apicontroller.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/**
* -------------------------------------------------------------------------
* Tag plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Tag.
*
* Tag is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tag is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tag. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2014-2022 by Teclib'.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/tag
* -------------------------------------------------------------------------
*/

use Glpi\Api\HL\Controller\AbstractController;
use Glpi\Api\HL\Doc as Doc;
use Glpi\Api\HL\Route;
use Glpi\Http\Request;
use Glpi\Http\Response;

#[Route(path: '/Tag', priority: 1, tags: ['Tag'])]
final class PluginTagApicontroller extends AbstractController
{
protected static function getRawKnownSchemas(): array
{
return [
'Tag' => [
'type' => Doc\Schema::TYPE_OBJECT,
'x-itemtype' => PluginTagTag::class,
'properties' => [
'id' => [
'type' => Doc\Schema::TYPE_INTEGER,
'format' => Doc\Schema::FORMAT_INTEGER_INT64,
'x-readonly' => true,
],
'name' => ['type' => Doc\Schema::TYPE_STRING],
'comment' => ['type' => Doc\Schema::TYPE_STRING],
]
]
];
}

#[Route(path: '/', methods: ['GET'])]
#[Doc\Route(
description: 'List or search tags'
)]
public function getTags(Request $request): Response
{
return $this->searchBySchema($this->getKnownSchema('Tag'), $request->getParameters());
}

#[Route(path: '/{id}', methods: ['GET'], requirements: ['id' => '\d+'])]
#[Doc\Route(
description: 'Get a tag by ID',
parameters: [
[
'name' => 'id',
'description' => 'The ID of the tag',
'location' => Doc\Parameter::LOCATION_PATH,
'schema' => ['type' => Doc\Schema::TYPE_INTEGER]
]
]
)]
public function getTag(Request $request, int $id): Response
{
return $this->getOneBySchema($this->getKnownSchema('Tag'), $request->getAttributes(), $request->getParameters(), $id);
}

#[Route(path: '/', methods: ['POST'])]
#[Doc\Route(description: 'Create a new tag', parameters: [
[
'name' => '_',
'location' => Doc\Parameter::LOCATION_BODY,
'type' => Doc\Schema::TYPE_OBJECT,
'schema' => 'Tag',
]
])]
public function createTag(Request $request): Response
{
return $this->createBySchema($this->getKnownSchema('Tag'), $request->getParameters(), 'getTag');
}

#[Route(path: '/{id}', methods: ['PATCH'], requirements: ['id' => '\d+'])]
#[Doc\Route(
description: 'Update a tag by ID',
parameters: [
[
'name' => 'id',
'description' => 'The ID of the tag',
'location' => Doc\Parameter::LOCATION_PATH,
'schema' => ['type' => Doc\Schema::TYPE_INTEGER]
],
[
'name' => '_',
'location' => Doc\Parameter::LOCATION_BODY,
'type' => Doc\Schema::TYPE_OBJECT,
'schema' => 'Tag',
]
],
responses: [
['schema' => 'Tag']
]
)]
public function updateTag(Request $request, int $id): Response
{
return $this->updateBySchema($this->getKnownSchema('Tag'), $request->getAttributes(), $request->getParameters(), $id);
}

#[Route(path: '/{id}', methods: ['DELETE'], requirements: ['id' => '\d+'])]
#[Doc\Route(
description: 'Delete a tag by ID',
parameters: [
[
'name' => 'id',
'description' => 'The ID of the tag',
'location' => Doc\Parameter::LOCATION_PATH,
'schema' => ['type' => Doc\Schema::TYPE_INTEGER]
]
]
)]
public function deleteTag(Request $request, int $id): Response
{
return $this->deleteBySchema($this->getKnownSchema('Tag'), $request->getAttributes(), $request->getParameters(), $id);
}
}
7 changes: 6 additions & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// Minimal GLPI version, inclusive
define("PLUGIN_TAG_MIN_GLPI", "10.0.0");
// Maximum GLPI version, exclusive
define("PLUGIN_TAG_MAX_GLPI", "10.0.99");
define("PLUGIN_TAG_MAX_GLPI", "10.1.99");

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

$PLUGIN_HOOKS[Hooks::REDEFINE_API_SCHEMAS]['tag'] = 'plugin_tag_redefine_api_schemas';
$PLUGIN_HOOKS[Hooks::API_CONTROLLERS]['tag'] = [
PluginTagApicontroller::class
];

Plugin::registerClass('PluginTagProfile', ['addtabon' => ['Profile']]);
Plugin::registerClass('PluginTagConfig', ['addtabon' => 'Config']);

Expand Down

0 comments on commit c1ce2ad

Please sign in to comment.