Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HL API integration #139

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 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,51 @@ 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', // ID field on the main join table
'field' => 'items_id', // items_id field on the linking join table
// Join params becomes tags_ref.items_id=_.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 \Glpi\Api\HL\Search::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 \Glpi\Api\HL\Search::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 \Glpi\Api\HL\Search::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 \Glpi\Api\HL\Search::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 \Glpi\Api\HL\Search::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