Unverified Commit 2c4f092d authored by Adrien Clairembault's avatar Adrien Clairembault Committed by GitHub
Browse files

Add 'Myself' search criteria (#9170)

* Add 'Myself' search criteria

* Support dashbord filters

* Add unit tests

* Fix typo in comment

* Fix comment typo 2
parent 118334c5
......@@ -77,6 +77,15 @@ if (isset($_POST['searchtype'])) {
$searchopt['toadd'] = ['mygroups' => __('My groups')];
break;
case "glpi_users.name" :
$searchopt['toadd'] = [
[
'id' => 'myself',
'text' => __('Myself'),
]
];
break;
case "glpi_changes.status" :
case "glpi_changes.impact" :
case "glpi_changes.urgency" :
......
......@@ -153,7 +153,15 @@ JAVASCRIPT;
}
static function user_tech(string $value = ""): string {
return self::dropdown($value, 'user_tech', User::class, ['right' => 'own_ticket']);
return self::dropdown($value, 'user_tech', User::class, [
'right' => 'own_ticket',
'toadd' => [
[
'id' => 'myself',
'text' => __('Myself'),
]
]
]);
}
static function dropdown(
......
......@@ -1522,12 +1522,19 @@ class Provider {
}
}
if (isset($apply_filters['user_tech'])
&& (int) $apply_filters['user_tech'] > 0) {
if (isset($apply_filters['user_tech'])) {
if ((int) $apply_filters['user_tech'] > 0) {
$users_id = (int) $apply_filters['user_tech'];
} else if ($apply_filters['user_tech'] == 'myself') {
$users_id = $_SESSION['glpiID'];
} else {
// Invalid value, should never happen
$users_id = -1;
}
if ($DB->fieldExists($table, 'users_id_tech')) {
$where += [
"$table.users_id_tech" => (int) $apply_filters['user_tech']
"$table.users_id_tech" => $users_id,
];
} else if (in_array($table, [
Ticket::getTable(),
......@@ -1550,7 +1557,7 @@ class Provider {
];
$where += [
"ul.type" => \CommonITILActor::ASSIGN,
"ul.users_id" => (int) $apply_filters['user_tech']
"ul.users_id" => $users_id,
];
}
}
......
......@@ -1429,7 +1429,7 @@ final class DbUtils {
/**
* Get name of the user with ID=$ID (optional with link to user.form.php)
*
* @param integer $ID ID of the user.
* @param integer|string $ID ID of the user.
* @param integer $link 1 = Show link to user.form.php 2 = return array with comments and link
* (default =0)
*
......@@ -1445,7 +1445,15 @@ final class DbUtils {
"comment" => ""];
}
if ($ID) {
if ($ID == 'myself') {
$name = __('Myself');
if (isset($user['name'])) {
$user['name'] = $name;
} else {
$user = $name;
}
} else if ($ID) {
$iterator = $DB->request(
'glpi_users', [
'WHERE' => ['id' => $ID]
......
......@@ -3724,6 +3724,10 @@ class Dropdown {
}
}
foreach ($post['toadd'] ?? [] as $toadd) {
$results[] = $toadd;
}
if (count($users)) {
foreach ($users as $ID => $output) {
$title = sprintf(__('%1$s - %2$s'), $output, $logins[$ID]);
......
......@@ -3055,6 +3055,13 @@ JAVASCRIPT;
case "glpi_users.name" :
$options2['right'] = (isset($searchopt['right']) ? $searchopt['right'] : 'all');
$options2['inactive_deleted'] = 1;
$searchopt['toadd'] = [
[
'id' => 'myself',
'text' => __('Myself'),
]
];
break;
}
......@@ -4261,6 +4268,16 @@ JAVASCRIPT;
// case "glpi_users_validation.name" :
case "glpi_users.name" :
if ($val == 'myself') {
switch ($searchtype) {
case 'equals' :
return " $link (`$table`.`id` = " . $DB->quoteValue($_SESSION['glpiID']) . ") ";
case 'notequals' :
return " $link (`$table`.`id` <> " . $DB->quoteValue($_SESSION['glpiID']) . ") ";
}
}
if ($itemtype == 'User') { // glpi_users case / not link table
if (in_array($searchtype, ['equals', 'notequals'])) {
$search_str = "`$table`.`id`" . $SEARCH;
......
......@@ -3989,7 +3989,6 @@ JAVASCRIPT;
*/
static function dropdown($options = []) {
global $CFG_GLPI;
// Default values
$p = [
'name' => 'users_id',
......@@ -4014,6 +4013,7 @@ JAVASCRIPT;
'url' => $CFG_GLPI['root_doc'] . "/ajax/getDropdownUsers.php",
'inactive_deleted' => 0,
'with_no_right' => 0,
'toadd' => [],
];
if (is_array($options) && count($options)) {
......@@ -4028,7 +4028,7 @@ JAVASCRIPT;
}
// Check default value for dropdown : need to be a numeric
if ((strlen($p['value']) == 0) || !is_numeric($p['value'])) {
if ((strlen($p['value']) == 0) || (!is_numeric($p['value']) && $p['value'] != 'myself')) {
$p['value'] = 0;
}
......@@ -4046,8 +4046,10 @@ JAVASCRIPT;
$view_users = self::canView();
if (!empty($p['value']) && ($p['value'] > 0)) {
$default = $user["name"];
if ($p['value'] == 'myself') {
$default = __("Myself");
} else if (!empty($p['value']) && ($p['value'] > 0)) {
$default = $user["name"];
} else {
if ($p['all']) {
$default = __('All');
......@@ -4082,6 +4084,7 @@ JAVASCRIPT;
'with_no_right' => $p['with_no_right'],
'entity_restrict' => ($entity_restrict = (is_array($p['entity']) ? json_encode(array_values($p['entity'])) : $p['entity'])),
'specific_tags' => $p['specific_tags'],
'toadd' => $p['toadd'],
'_idor_token' => Session::getNewIDORToken(__CLASS__, [
'right' => $p['right'],
'entity_restrict' => $entity_restrict,
......
......@@ -33,8 +33,10 @@
namespace tests\units;
use CommonDBTM;
use CommonITILActor;
use DBConnection;
use DbTestCase;
use Ticket;
/* Test for inc/search.class.php */
......@@ -1539,6 +1541,125 @@ class Search extends DbTestCase {
// Check results
$this->array($names)->isEqualTo($expected);
}
protected function testMyselfSearchCriteriaProvider(): array {
$TU_USER_users_id = getItemByTypeName('User', TU_USER, true);
$tech_users_id = getItemByTypeName('User', 'tech', true);
$root_entity = getItemByTypeName('Entity', '_test_root_entity', true);
// Create test data
$to_create = [
[
'name' => 'testMyselfSearchCriteriaProvider 1',
'observer' => $TU_USER_users_id,
],
[
'name' => 'testMyselfSearchCriteriaProvider 2',
'observer' => $TU_USER_users_id,
],
[
'name' => 'testMyselfSearchCriteriaProvider 3',
'observer' => $TU_USER_users_id,
],
[
'name' => 'testMyselfSearchCriteriaProvider 4',
'observer' => $tech_users_id,
],
];
foreach ($to_create as $params) {
$ticket = new Ticket();
$tickets_id = $ticket->add([
'name' => $params['name'],
'content' => 'testMyselfSearchCriteriaProvider',
'_users_id_observer' => $params['observer'],
'entities_id' => $root_entity,
]);
$this->integer($tickets_id)->isGreaterThan(0);
$actors = $ticket->getITILActors();
$this->integer($actors[$params['observer']][0])->isEqualTo(CommonITILActor::OBSERVER);
}
return [
// Case 1: Search for tickets where 'TU_USER' is an observer
[
'criteria' => [
[
'link' => 'AND',
'field' => 66, // Observer search option
'searchtype' => 'equals',
'value' => $TU_USER_users_id,
]
],
'expected' => [
'testMyselfSearchCriteriaProvider 1',
'testMyselfSearchCriteriaProvider 2',
'testMyselfSearchCriteriaProvider 3',
]
],
// Case 2: Search for tickets where 'tech' is an observer
[
'criteria' => [
[
'link' => 'AND',
'field' => 66, // Observer search option
'searchtype' => 'equals',
'value' => $tech_users_id,
]
],
'expected' => [
'testMyselfSearchCriteriaProvider 4',
]
],
// Case 3: Search for tickets where the current user (TU_USER) is an observer
[
'criteria' => [
[
'link' => 'AND',
'field' => 66, // Observer search option
'searchtype' => 'equals',
'value' => 'myself',
]
],
'expected' => [
'testMyselfSearchCriteriaProvider 1',
'testMyselfSearchCriteriaProvider 2',
'testMyselfSearchCriteriaProvider 3',
]
],
];
}
/**
* Functionnal test for the 'myself' search criteria.
* We use the output type "Search::NAMES_OUTPUT" during the test as it make
* it easy to parse the results.
*
* @dataProvider testMyselfSearchCriteriaProvider
*/
public function testMyselfSearchCriteria(array $criteria, array $expected) {
$this->login();
// Run search and capture results
ob_start();
\Search::showList('Ticket', [
'display_type' => \Search::NAMES_OUTPUT,
'export_all' => 1,
'criteria' => $criteria,
'item_type' => 'Ticket',
'is_deleted' => 0,
'as_map' => 0,
]);
$names = ob_get_contents();
ob_end_clean();
// Convert results to array and remove last row (always empty for NAMES_OUTPUT)
$names = explode("\n", $names);
array_pop($names);
// Check results
$this->array($names)->isEqualTo($expected);
}
}
class DupSearchOpt extends \CommonDBTM {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment