Skip to content

Fix - Default tech in Tasks Templates #19427

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

Merged
merged 10 commits into from
Apr 23, 2025
Merged
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
4 changes: 2 additions & 2 deletions ajax/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
unset($template->fields['groups_id_tech']);
}

if ($template->fields['users_id_tech'] == 0) {
unset($template->fields['users_id_tech']);
if ($template->fields['users_id_tech'] == -1) {
$template->fields['users_id_tech'] = Session::getLoginUserID();
}

// Return json response with the template fields
Expand Down
47 changes: 47 additions & 0 deletions install/migrations/update_10.0.x_to_11.0.0/tasktemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

/**
* @var \DBmysql $DB
* @var \Migration $migration
*/

$migration->addField(
'glpi_tasktemplates',
'use_current_user',
'bool',
[
'after' => 'users_id_tech',
]
);
1 change: 1 addition & 0 deletions install/mysql/glpi-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7170,6 +7170,7 @@ CREATE TABLE `glpi_tasktemplates` (
`state` int NOT NULL DEFAULT '0',
`is_private` tinyint NOT NULL DEFAULT '0',
`users_id_tech` int unsigned NOT NULL DEFAULT '0',
`use_current_user` tinyint NOT NULL DEFAULT '0',
`groups_id_tech` int unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `name` (`name`),
Expand Down
123 changes: 123 additions & 0 deletions phpunit/functional/TaskTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,127 @@ protected function getInstance(): \AbstractITILChildTemplate
{
return new \TaskTemplate();
}

public function testCurrentUserFeature()
{
$this->login();
$template = new \TaskTemplate();

$template_id = $this->createItem(
'TaskTemplate',
[
'name' => 'Test Current User Template',
'content' => 'Test content',
'users_id_tech' => -1
]
)->getID();

$this->assertTrue($template->getFromDB($template_id));

$this->assertEquals(1, $template->fields['use_current_user']);
$this->assertEquals(-1, $template->fields['users_id_tech']); // Value should be transformed in post_getFromDB

$specific_user_id = getItemByTypeName('User', 'tech', true);
$this->updateItem(
'TaskTemplate',
$template_id,
[
'users_id_tech' => $specific_user_id
]
);

$this->assertTrue($template->getFromDB($template_id));

$this->assertEquals(0, $template->fields['use_current_user']);
$this->assertEquals($specific_user_id, (int)$template->fields['users_id_tech']);

// Test updating back to current user
$this->updateItem(
'TaskTemplate',
$template_id,
[
'users_id_tech' => -1
]
);

$this->assertTrue($template->getFromDB($template_id));

$this->assertEquals(1, $template->fields['use_current_user']);
$this->assertEquals(-1, $template->fields['users_id_tech']); // Value should be transformed in post_getFromDB

$this->updateItem(
'TaskTemplate',
$template_id,
[
'users_id_tech' => 0
]
);

$this->assertTrue($template->getFromDB($template_id));

$this->assertEquals(0, $template->fields['use_current_user']);
$this->assertEquals(0, (int)$template->fields['users_id_tech']);
}

public function testSpecificValueToDisplay()
{
$this->login();

$values = [
'users_id_tech' => -1,
'use_current_user' => 1
];
$result = \TaskTemplate::getSpecificValueToDisplay('users_id_tech', $values);
$this->assertEquals(__('Current logged-in user'), $result);

$specific_user_id = getItemByTypeName('User', 'tech', true);
$values = [
'users_id_tech' => $specific_user_id,
'use_current_user' => 0
];
$result = \TaskTemplate::getSpecificValueToDisplay('users_id_tech', $values);
$this->assertStringContainsString('tech', $result);
}

public function testSearchAbility()
{
$this->login();

$this->createItem(
'TaskTemplate',
[
'name' => 'Search Template Current User',
'content' => 'Content 1',
'users_id_tech' => -1
]
)->getID();

$specific_user_id = getItemByTypeName('User', 'tech', true);
$this->createItem(
'TaskTemplate',
[
'name' => 'Search Template Specific User',
'content' => 'Content 2',
'users_id_tech' => $specific_user_id
]
)->getID();

$this->createItem(
'TaskTemplate',
[
'name' => 'Search Template No User',
'content' => 'Content 3',
'users_id_tech' => 0
]
)->getID();

$condition = \TaskTemplate::addWhere('AND', 0, \TaskTemplate::class, 7, 'equals', -1);
$this->assertEquals(' AND (`glpi_tasktemplates`.`use_current_user` = 1)', $condition);

$condition = \TaskTemplate::addWhere('AND', 0, \TaskTemplate::class, 7, 'equals', $specific_user_id);
$this->assertEquals(" AND (`glpi_tasktemplates`.`use_current_user` = 0 AND `glpi_tasktemplates`.`users_id_tech` = $specific_user_id)", $condition);

$condition = \TaskTemplate::addWhere('AND', 0, \TaskTemplate::class, 7, 'equals', 0);
$this->assertEquals(' AND (`glpi_tasktemplates`.`use_current_user` = 0 AND `glpi_tasktemplates`.`users_id_tech` = 0)', $condition);
}
}
132 changes: 126 additions & 6 deletions src/TaskTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class TaskTemplate extends AbstractITILChildTemplate

public static $rightname = 'tasktemplate';


public function post_getFromDB()
{
if (isset($this->fields['use_current_user']) && $this->fields['use_current_user']) {
$this->fields['users_id_tech'] = -1;
}
}

public static function getTypeName($nb = 0)
{
Expand Down Expand Up @@ -133,12 +138,15 @@ public function rawSearchOptions()

$tab[] = [
'id' => '7',
'table' => 'glpi_users',
'field' => 'name',
'linkfield' => 'users_id_tech',
'table' => $this->getTable(),
'field' => 'users_id_tech',
'name' => __('By'),
'datatype' => 'dropdown',
'right' => 'own_ticket'
'searchtype' => [
'0' => 'equals',
'1' => 'notequals',
],
'datatype' => 'specific',
'additionalfields' => ['use_current_user']
];

$tab[] = [
Expand Down Expand Up @@ -182,6 +190,12 @@ public static function getSpecificValueToDisplay($field, $values, array $options
switch ($field) {
case 'state':
return Planning::getState($values[$field]);
case 'users_id_tech':
if (isset($values['use_current_user']) && $values['use_current_user'] == 1) {
return __('Current logged-in user');
}

return getUserLink($values[$field]);
}
return parent::getSpecificValueToDisplay($field, $values, $options);
}
Expand All @@ -198,6 +212,21 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
switch ($field) {
case 'state':
return Planning::dropdownState($name, $values[$field], false);
case 'users_id_tech':
return User::dropdown([
'name' => $name,
'aria_label' => __('By'),
'right' => 'own_ticket',
'value' => $values[$field],
'width' => '100%',
'display' => false,
'toadd' => [
[
'id' => -1,
'text' => __('Current logged-in user')
]
]
]);
}
return parent::getSpecificValueToSelect($field, $name, $values, $options);
}
Expand All @@ -214,10 +243,17 @@ public function displaySpecificTypeField($ID, $field = [], array $options = [])
case 'users_id_tech':
User::dropdown([
'name' => "users_id_tech",
'aria_label' => __('By'),
'right' => "own_ticket",
'value' => $this->fields["users_id_tech"],
'entity' => $this->fields["entities_id"],
'width' => '100%',
'toadd' => [
[
'id' => -1,
'text' => __('Current logged-in user')
]
]
]);
break;
case 'groups_id_tech':
Expand Down Expand Up @@ -250,6 +286,40 @@ public function displaySpecificTypeField($ID, $field = [], array $options = [])
}
}

public function prepareInputForAdd($input)
{
$input = parent::prepareInputForAdd($input);

$input = $this->prepareInput($input);

return $input;
}

public function prepareInputForUpdate($input)
{
$input = parent::prepareInputForUpdate($input);

$input = $this->prepareInput($input);

return $input;
}

private function prepareInput($input)
{
if ($input === false) {
return false;
}

if (isset($input['users_id_tech']) && (int) $input['users_id_tech'] == -1) {
$input['use_current_user'] = 1;
$input['users_id_tech'] = 0;
} else if (isset($input['users_id_tech'])) {
$input['use_current_user'] = 0;
}

return $input;
}

public static function getIcon()
{
return "fas fa-layer-group";
Expand All @@ -259,4 +329,54 @@ public function getCloneRelations(): array
{
return [];
}

public static function addWhere($link, $nott, $itemtype, $ID, $searchtype, $val)
{
if ($itemtype !== self::class) {
return false;
}

$searchopt = Search::getOptions($itemtype);
if (!isset($searchopt[$ID]['field'])) {
return false;
}

$field = $searchopt[$ID]['field'];
$table = self::getTable();

if ($field === 'users_id_tech') {
$positive_condition = ($nott == 0 && $searchtype == 'equals') || ($nott == 1 && $searchtype == 'notequals') || ($nott == 0 && $searchtype == 'empty');
$table_use_current_user = "`$table`.`use_current_user`";
$table_users_id_tech = "`$table`.`users_id_tech`";
$int_val = (int) $val;

if ($val == -1) {
if ($positive_condition) {
return " $link ($table_use_current_user = 1)";
} else {
return " $link ($table_use_current_user = 0)";
}
} else if ($val == 0) {
if ($positive_condition) {
return " $link ($table_use_current_user = 0 AND $table_users_id_tech = 0)";
} else {
return " $link ($table_use_current_user = 1 OR $table_users_id_tech != 0)";
}
} else if ($val == 'null' && $searchtype == 'empty') {
if ($positive_condition) {
return " $link ($table_use_current_user = 0 AND $table_users_id_tech = 0)";
} else {
return " $link ($table_use_current_user = 1 OR $table_users_id_tech != 0)";
}
} else {
if ($positive_condition) {
return " $link ($table_use_current_user = 0 AND $table_users_id_tech = $int_val)";
} else {
return " $link ($table_use_current_user = 1 OR $table_users_id_tech != $int_val)";
}
}
}

return false;
}
}
2 changes: 2 additions & 0 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4156,6 +4156,8 @@ public static function dropdown($options = [])

if ($p['value'] === 'myself') {
$default = __("Myself");
} else if ((int) $p['value'] === -1) {
$default = __('Current logged-in user');
} else if (!empty($p['value']) && ($p['value'] > 0)) {
$default = $user_name;
} else {
Expand Down