From a3319c43654c662240ca8de381a7390843488bfc Mon Sep 17 00:00:00 2001
From: "Romain B." <8530352+Rom1-B@users.noreply.github.com>
Date: Tue, 15 Oct 2024 08:16:47 +0200
Subject: [PATCH] Feat: Enable/disable a tag (#204)
* Feat: Enable/disable a tag
* fix header
---
inc/tag.class.php | 82 +++++++++++++++--------------------
templates/forms/tag.html.twig | 51 ++++++++++++++++++++++
2 files changed, 85 insertions(+), 48 deletions(-)
create mode 100644 templates/forms/tag.html.twig
diff --git a/inc/tag.class.php b/inc/tag.class.php
index a72a71e..7735a25 100644
--- a/inc/tag.class.php
+++ b/inc/tag.class.php
@@ -28,6 +28,8 @@
* -------------------------------------------------------------------------
*/
+use Glpi\Application\View\TemplateRenderer;
+
class PluginTagTag extends CommonDropdown
{
// From CommonDBTM
@@ -86,36 +88,6 @@ public function showForm($ID, $options = [])
}
$this->initForm($ID, $options);
- $this->showFormHeader($options);
-
- echo '
';
- echo "";
- echo " | ";
- echo "";
- echo '';
- echo " | ";
- echo "
";
-
- echo "";
- echo " | ";
- echo "";
- echo "";
- echo " | ";
- echo "
";
-
- echo "";
- echo " | ";
- echo "";
- Html::showColorField('color', ['value' => $this->fields['color'] ?: '#DDDDDD']);
- echo " | ";
- echo "
";
-
- echo "";
- echo " | ";
- echo "";
- echo "";
- // show an hidden input to permist deletion of all values
- echo Html::hidden("type_menu");
// retrieve tags elements and existing values
$type_menu_elements = [];
@@ -126,20 +98,12 @@ public function showForm($ID, $options = [])
}
$type_menu_values = !empty($this->fields['type_menu']) ? json_decode($this->fields['type_menu']) : [];
- // show the multiple dropdown
- Dropdown::showFromArray(
- "type_menu",
- $type_menu_elements,
- [
- 'values' => $type_menu_values,
- 'multiple' => 'multiples'
- ]
- );
-
- echo " | ";
- echo "
";
-
- $this->showFormButtons($options);
+ TemplateRenderer::getInstance()->display('@tag/forms/tag.html.twig', [
+ 'item' => $this,
+ 'params' => $options,
+ 'type_menu_elements' => $type_menu_elements,
+ 'type_menu_values' => $type_menu_values,
+ ]);
return true;
}
@@ -164,12 +128,16 @@ public static function install(Migration $migration)
`id` int {$default_key_sign} NOT NULL auto_increment,
`entities_id` int {$default_key_sign} NOT NULL DEFAULT '0',
`is_recursive` tinyint NOT NULL DEFAULT '1',
+ `is_active` tinyint NOT NULL DEFAULT '1',
`name` varchar(255) NOT NULL DEFAULT '',
`comment` text,
`color` varchar(50) NOT NULL DEFAULT '',
`type_menu` text,
PRIMARY KEY (`id`),
- KEY `name` (`name`)
+ KEY `name` (`name`),
+ KEY `entities_id` (`entities_id`),
+ KEY `is_recursive` (`is_recursive`),
+ KEY `is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;
SQL;
if (method_exists($DB, 'doQueryOrDie')) {
@@ -178,10 +146,19 @@ public static function install(Migration $migration)
/** @phpstan-ignore-next-line */
$DB->queryOrDie($query);
}
- }
+ } else {
+ if (!$DB->fieldExists($table, 'type_menu')) {
+ $migration->addField($table, 'type_menu', "text");
+ }
+
+ if (!$DB->fieldExists($table, 'is_active')) {
+ $migration->addField($table, 'is_active', "tinyint NOT NULL DEFAULT '1'");
+ }
- if (!$DB->fieldExists($table, 'type_menu')) {
- $migration->addField($table, 'type_menu', "text");
+ $migration->addKey($table, 'name');
+ $migration->addKey($table, 'entities_id');
+ $migration->addKey($table, 'is_recursive');
+ $migration->addKey($table, 'is_active');
$migration->migrationOneTable($table);
}
@@ -381,6 +358,14 @@ public function rawSearchOptions()
'datatype' => 'specific',
];
+ $tab[] = [
+ 'id' => 8,
+ 'table' => $this->getTable(),
+ 'field' => 'is_active',
+ 'name' => __('Active'),
+ 'datatype' => 'bool',
+ ];
+
return $tab;
}
@@ -655,6 +640,7 @@ public static function showTagDropdown($params = [])
// Restrict tags finding by itemtype and entity
$where = [
+ 'is_active' => 1,
'OR' => [
['type_menu' => null],
['type_menu' => ''],
diff --git a/templates/forms/tag.html.twig b/templates/forms/tag.html.twig
new file mode 100644
index 0000000..8aaaf33
--- /dev/null
+++ b/templates/forms/tag.html.twig
@@ -0,0 +1,51 @@
+{#
+ # -------------------------------------------------------------------------
+ # 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 .
+ # -------------------------------------------------------------------------
+ # @copyright Copyright (C) 2014-2023 by Teclib'.
+ # @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
+ # @link https://github.com/pluginsGLPI/tag
+ # -------------------------------------------------------------------------
+ #}
+
+{% extends "generic_show_form.html.twig" %}
+{% import 'components/form/fields_macros.html.twig' as fields %}
+{% set field_options = field_options ?? {} %}
+
+{% block more_fields %}
+ {{ fields.colorField(
+ 'color',
+ item.fields['color'] ?? '#DDDDDD',
+ __('Background color'),
+ field_options
+ ) }}
+
+ {{ fields.dropdownArrayField(
+ 'type_menu',
+ '',
+ type_menu_elements,
+ _n('Associated item type', 'Associated item types', get_plural_number()),
+ field_options|merge({
+ 'values': type_menu_values,
+ 'multiple': true
+ })
+ ) }}
+{% endblock %}