Skip to content

Commit

Permalink
Feat: Enable/disable a tag (#204)
Browse files Browse the repository at this point in the history
* Feat: Enable/disable a tag

* fix header
  • Loading branch information
Rom1-B authored Oct 15, 2024
1 parent e89665b commit a3319c4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 48 deletions.
82 changes: 34 additions & 48 deletions inc/tag.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* -------------------------------------------------------------------------
*/

use Glpi\Application\View\TemplateRenderer;

class PluginTagTag extends CommonDropdown
{
// From CommonDBTM
Expand Down Expand Up @@ -86,36 +88,6 @@ public function showForm($ID, $options = [])
}

$this->initForm($ID, $options);
$this->showFormHeader($options);

echo '<table class="tab_cadre_fixe">';
echo "<tr class='line0 tab_bg_2'>";
echo "<td><label for='name'>" . __('Name') . " <span class='red'>*</span></label></td>";
echo "<td>";
echo '<input type="text" id="name" name="name" value="' . $this->fields['name'] . '" size="40" required>';
echo "</td>";
echo "</tr>";

echo "<tr class='line1 tab_bg_2'>";
echo "<td><label for='comment'>" . __('Description') . "</label></td>";
echo "<td>";
echo "<textarea name='comment' id ='comment' cols='45' rows='3'>" . $this->fields['comment'] . "</textarea>";
echo "</td>";
echo "</tr>";

echo "<tr class='line1 tab_bg_2'>";
echo "<td><label>" . __('HTML color', 'tag') . "</label></td>";
echo "<td>";
Html::showColorField('color', ['value' => $this->fields['color'] ?: '#DDDDDD']);
echo "</td>";
echo "</tr>";

echo "<tr class='line0 tab_bg_2'>";
echo "<td><label>" . _n('Associated item type', 'Associated item types', 2) . "</label></td>";
echo "</td>";
echo "<td>";
// show an hidden input to permist deletion of all values
echo Html::hidden("type_menu");

// retrieve tags elements and existing values
$type_menu_elements = [];
Expand All @@ -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 "</td>";
echo "</tr>";

$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;
}
Expand All @@ -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')) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -381,6 +358,14 @@ public function rawSearchOptions()
'datatype' => 'specific',
];

$tab[] = [
'id' => 8,
'table' => $this->getTable(),
'field' => 'is_active',
'name' => __('Active'),
'datatype' => 'bool',
];

return $tab;
}

Expand Down Expand Up @@ -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' => ''],
Expand Down
51 changes: 51 additions & 0 deletions templates/forms/tag.html.twig
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
# -------------------------------------------------------------------------
# @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 %}

0 comments on commit a3319c4

Please sign in to comment.