Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix item creation failing when a Yes/No field has a non boolean default value
- Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value
- Fix GenericObject migration error message to name the required 3.0.x version
- Fix sort on number fields: values were sorted as text (`1000`, `12345`, `1500`, `999`)

## [1.25.1] - 2026-10-28

Expand Down
17 changes: 17 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ function plugin_fields_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype)
};
}

function plugin_fields_addOrderBy($itemtype, $ID, $order, $key)
{
/** @var DBmysql $DB */
global $DB;

$searchopt = Search::getOptions($itemtype)[$ID] ?? [];
if (($searchopt['pfields_type'] ?? null) !== 'number') {
return null;
}

// Number values are stored in a VARCHAR column: sort them as DOUBLE (`+ 0`), whose range covers any stored value unlike DECIMAL.
// Empty values become NULL, sorted first in ascending order like core does.
$column = $DB->quoteName($searchopt['table'] . '_' . $searchopt['field']) . '.' . $DB->quoteName($searchopt['field']);

return sprintf("NULLIF(%s, '') + 0 %s", $column, $order);
}

function plugin_item_transfer_fields(array $options): void
{
$itemtype = $options['type'] ?? null;
Expand Down
90 changes: 72 additions & 18 deletions tests/Units/HookAddWhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,77 @@ public function testNumberFieldSearch(string $searchtype, string $value, bool $n
* @return list<string> Sorted names (without prefix) of the found items
*/
private function searchNumberField(string $searchtype, string $value, bool $not): array
{
$found = $this->searchNumberFieldItems(static fn(int $so_id): array => [
'criteria' => [
['link' => $not ? 'AND NOT' : 'AND', 'field' => $so_id, 'searchtype' => $searchtype, 'value' => $value],
],
]);
sort($found);

return $found;
}

public static function numberSortProvider(): iterable
{
// Items without value are NULL, sorted first in ascending order
$asc = [null, null, -5.0, 0.0, 12.5, 999.0, 1000.0, 1000.0, 1500.0, 12345.0, 1e40];
yield 'ASC' => ['ASC', $asc];
yield 'DESC' => ['DESC', array_reverse($asc)];
}

/**
* Number values are stored as VARCHAR and used to be sorted as strings (1000, 12345, 1500, 999).
*/
#[DataProvider('numberSortProvider')]
public function testNumberFieldSort(string $order, array $expected): void
{
$found = $this->searchNumberFieldItems(static fn(int $so_id): array => [
'sort' => [$so_id],
'order' => [$order],
]);

// Compare values, not names: the order of equal values (1000 and 1000.0, empty values) is not defined
$sorted = array_map(
static fn(string $name): ?float => in_array($name, ['cleared', 'unsaved'], true) ? null : (float) $name,
$found,
);
$this->assertSame($expected, $sorted);
}

/**
* Search computers among items storing a predefined set of values in a number field.
*
* @param callable(int): array $params Search parameters to add, given the search option id of the number field
*
* @return list<string> Names (without prefix) of the found items, in result order
*/
private function searchNumberFieldItems(callable $params): array
{
[$so_id, $prefix] = $this->createNumberFieldItems();
$params = $params($so_id);
$data = Search::getDatas(Computer::class, [
'is_deleted' => 0,
'start' => 0,
'list_limit' => 100,
'criteria' => [
['field' => 1, 'searchtype' => 'contains', 'value' => '^' . $prefix],
...($params['criteria'] ?? []),
],
] + $params, [1]);

return array_map(
static fn(array $row): string => substr((string) $row['raw']['ITEM_Computer_1'], strlen($prefix)),
$data['data']['rows'],
);
}

/**
* Create a number field and computers storing a predefined set of values in it.
*
* @return array{int, string} Search option id of the field, prefix of the created computers names
*/
private function createNumberFieldItems(): array
{
$this->login();

Expand Down Expand Up @@ -220,23 +291,6 @@ private function searchNumberField(string $searchtype, string $value, bool $not)
// Item for which the container was never saved: no row, NULL through the LEFT JOIN
$this->createItem(Computer::class, ['name' => $prefix . 'unsaved', 'entities_id' => $entities_id]);

$so_id = PluginFieldsField::SEARCH_OPTION_STARTING_INDEX + $field->getID();
$data = Search::getDatas(Computer::class, [
'is_deleted' => 0,
'start' => 0,
'list_limit' => 100,
'criteria' => [
['field' => 1, 'searchtype' => 'contains', 'value' => '^' . $prefix],
['link' => $not ? 'AND NOT' : 'AND', 'field' => $so_id, 'searchtype' => $searchtype, 'value' => $value],
],
], [1]);

$found = array_map(
static fn(array $row): string => substr((string) $row['raw']['ITEM_Computer_1'], strlen($prefix)),
$data['data']['rows'],
);
sort($found);

return $found;
return [PluginFieldsField::SEARCH_OPTION_STARTING_INDEX + $field->getID(), $prefix];
}
}