Skip to content

Commit

Permalink
start removing search sql quirks
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Oct 2, 2024
1 parent 9231fe3 commit 486bc9a
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 314 deletions.
104 changes: 103 additions & 1 deletion src/CommonITILObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
use Glpi\Form\Destination\AnswersSet_FormDestinationItem;
use Glpi\Plugin\Hooks;
use Glpi\RichText\RichText;
use Glpi\Search\AdvancedSearchInterface;
use Glpi\Search\SearchOption;
use Glpi\Team\Team;

/**
Expand All @@ -52,7 +54,7 @@
* @property-read array $groups
* @property-read array $suppliers
**/
abstract class CommonITILObject extends CommonDBTM
abstract class CommonITILObject extends CommonDBTM implements AdvancedSearchInterface
{
use \Glpi\Features\Clonable;
use \Glpi\Features\Timeline;
Expand Down Expand Up @@ -11390,4 +11392,104 @@ public static function getTeamMemberForm(CommonITILObject $item): string
'main_rand' => mt_rand(),
]);
}

public static function getSQLDefaultSelectCriteria(string $itemtype): ?array
{
return null;
}

public static function getSQLSelectCriteria(string $itemtype, SearchOption $opt, bool $meta = false, string $meta_type = ''): ?array
{
return null;
}

public static function getSQLWhereCriteria(string $itemtype, SearchOption $opt, bool $nott, string $searchtype, mixed $val, bool $meta, callable $fn_append_with_search): ?array
{
// Only handle core ITIL objects here
if (!in_array($itemtype, [Ticket::class, Change::class, Problem::class], true)) {
return null;
}

/** @var Ticket|Change|Problem|false $item */
$item = getItemForItemtype($itemtype);
if ($opt['field'] === 'status') {
$to_check = [];
if ($item !== false) {
$to_check = match ($val) {
'process' => $item->getProcessStatusArray(),
'notclosed' => array_diff(array_keys($item::getAllStatusArray()), $item::getClosedStatusArray()),
'old' => array_merge($item::getSolvedStatusArray(), $item::getClosedStatusArray()),
'notold' => $item::getNotSolvedStatusArray(),
'all' => array_keys($item::getAllStatusArray()),
default => array_key_exists($val, $item::getAllStatusArray()) ? [$val] : []
};
}
if (count($to_check) > 0) {
if ($nott) {
return [
$opt->getTableField() => ['NOT IN' => $to_check]
];
}
return [$opt->getTableField() => $to_check];
}
} else if ($opt['table'] === Ticket_Ticket::getTable() && $opt['field'] === 'tickets_id_1') {
$tmp_link = $nott ? 'AND' : 'OR';
$compare = $nott ? '<>' : '=';
$to_add = [];

if ($nott && ($val !== 'NULL' && $val !== 'null')) {
$to_add = [$opt->getTableField() => null];
}

$criteria = [
$tmp_link => [
$opt->getTableField() => [$compare, $val],
"{$opt['table']}.tickets_id_2" => [$compare, $val],
],
Ticket::getTableField('id') => ['<>', $val],
];
if (!empty($to_add)) {
$criteria = [
'OR' => [
$criteria,
$to_add
]
];
}
return $criteria;
} else if (in_array($opt['field'], ['impact', 'urgency', 'priority'], true)) {
if (!is_numeric($val)) {
return [];
}
return match (true) {
$val > 0 => [$opt->getTableField() => [$nott ? '<>' : '=', $val]],
$val < 0 => [$opt->getTableField() => [$nott ? '<' : '>=', $val]],
default => [$opt->getTableField() => [$nott ? '<' : '>=', 0]],
};
} else if (
in_array(
$opt->getTableField(),
['glpi_tickets.global_validation', 'glpi_ticketvalidations.status', 'glpi_changes.global_validation', 'glpi_changevalidations.status'],
true
)
) {
if ($val == 'all') {
return [];
}
$to_check = match ($val) {
'can' => CommonITILValidation::getCanValidationStatusArray(),
'add' => CommonITILValidation::getAllValidationStatusArray(), // Dead case? Handled above
default => []
};
if (count($to_check) === 0) {
$to_check = [$val];
}
return match ($nott) {
true => [$opt->getTableField() => ['NOT IN' => $to_check]],
default => [$opt->getTableField() => $to_check],
};
}

return null;
}
}
27 changes: 25 additions & 2 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@
use Glpi\DBAL\QueryFunction;
use Glpi\Event;
use Glpi\Plugin\Hooks;
use Glpi\Search\AdvancedSearchInterface;
use Glpi\Search\SearchEngine;
use Glpi\Search\SearchOption;

/**
* Entity class
*/
class Entity extends CommonTreeDropdown
class Entity extends CommonTreeDropdown implements AdvancedSearchInterface
{
use Glpi\Features\Clonable;
use MapGeolocation;
Expand Down Expand Up @@ -3079,7 +3082,7 @@ public static function getEntitySelectorTree(): array

$adapt_tree = static function (&$entities) use (&$adapt_tree, $base_path) {
foreach ($entities as $entities_id => &$entity) {
$entity['key'] = $entities_id;
$entity['key'] = $entities_id;

$title = "<a href='$base_path?active_entity={$entities_id}'>" . htmlspecialchars($entity['name']) . "</a>";
$entity['title'] = $title;
Expand Down Expand Up @@ -3125,4 +3128,24 @@ public static function getEntitySelectorTree(): array

return $entitiestree;
}

public static function getSQLDefaultSelectCriteria(string $itemtype): ?array
{
global $DB;
$itemtable = SearchEngine::getOrigTableName($itemtype);
return [
"{$itemtable}.id AS entities_id",
new QueryExpression($DB::quoteValue('1') . ' AS ' . $DB::quoteName('is_recursive')),
];
}

public static function getSQLSelectCriteria(string $itemtype, SearchOption $opt, bool $meta = false, string $meta_type = ''): ?array
{
return null;
}

public static function getSQLWhereCriteria(string $itemtype, SearchOption $opt, bool $nott, string $searchtype, mixed $val, bool $meta, callable $fn_append_with_search): ?array
{
return null;
}
}
21 changes: 20 additions & 1 deletion src/FieldUnicity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
*/

use Glpi\Application\View\TemplateRenderer;
use Glpi\Search\AdvancedSearchInterface;
use Glpi\Search\SearchOption;

/**
* FieldUnicity Class
**/
class FieldUnicity extends CommonDropdown
class FieldUnicity extends CommonDropdown implements AdvancedSearchInterface
{
// From CommonDBTM
public $dohistory = true;
Expand Down Expand Up @@ -589,4 +591,21 @@ public static function getIcon()
{
return "ti ti-fingerprint";
}

public static function getSQLDefaultSelectCriteria(string $itemtype): ?array
{
return [
'glpi_fieldunicities.itemtype AS ITEMTYPE'
];
}

public static function getSQLSelectCriteria(string $itemtype, SearchOption $opt, bool $meta = false, string $meta_type = ''): ?array
{
return null;
}

public static function getSQLWhereCriteria(string $itemtype, SearchOption $opt, bool $nott, string $searchtype, mixed $val, bool $meta, callable $fn_append_with_search): ?array
{
return null;
}
}
71 changes: 71 additions & 0 deletions src/Glpi/Search/AdvancedSearchInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2023 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @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/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Search;

use Glpi\DBAL\QueryFunction;

interface AdvancedSearchInterface
{
/**
* @param class-string<\CommonDBTM> $itemtype
* @return array|null
*/
public static function getSQLDefaultSelectCriteria(string $itemtype): ?array;

/**
* @param class-string<\CommonDBTM> $itemtype
* @param SearchOption $opt
* @param bool $meta
* @param string $meta_type
* @return array|null
*/
public static function getSQLSelectCriteria(string $itemtype, SearchOption $opt, bool $meta = false, string $meta_type = ''): ?array;

/**
* This method is called on the class that the search option belongs to (based on the table).
* It can return an array of criteria to handle the search option in a non-standard way, or return null to indicate it wasn't handled at all.
* @param class-string<\CommonDBTM> $itemtype The main itemtype being searched on
* @param SearchOption $opt The search option being handled
* @param bool $nott Whether the search option is negated
* @param string $searchtype The search type (e.g. 'contains')
* @param mixed $val The value to search for
* @param bool $meta Whether the search option is for a meta field
* @param callable $fn_append_with_search A helper function to append a criterion to a criteria array in a standardized way
* @phpstan-param callable(array &$criteria, string|QueryFunction $value): void $fn_append_with_search
* @return array|null
*/
public static function getSQLWhereCriteria(string $itemtype, SearchOption $opt, bool $nott, string $searchtype, mixed $val, bool $meta, callable $fn_append_with_search): ?array;
}
Loading

0 comments on commit 486bc9a

Please sign in to comment.