Skip to content

Commit 8499e9f

Browse files
Lainowstonebuzz
andauthored
Fix number search option (#926)
* Fix number search option * update changelog * Fix search option * Fix comment * Fix comment * Fix comment * Update hook.php Co-authored-by: Stanislas <[email protected]> * Update CHANGELOG.md Co-authored-by: Stanislas <[email protected]> * Add suggestions * Fix php cs * Fix AND NOT & OR NOT for equals * Add cast * Update hook.php Co-authored-by: Stanislas <[email protected]> * Fix decimal cast * Fix Out of range sql warning --------- Co-authored-by: Stanislas <[email protected]>
1 parent a355e61 commit 8499e9f

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Fixed
1111

12+
- Fix `numeric` field search
1213
- Fix containers migration while adding `is_recursive` field
1314
- Fix container update from other context (like plugins)
1415
- Fix "not equals" search operator for dropdown `multiple`

hook.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,45 @@ function plugin_datainjection_populate_fields()
338338

339339
function plugin_fields_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype)
340340
{
341-
$searchopt = &Search::getOptions($itemtype);
342-
$table = $searchopt[$ID]['table'];
343-
$field = $searchopt[$ID]['field'];
341+
/** @var \DBmysql $DB */
342+
global $DB;
343+
344+
$searchopt = &Search::getOptions($itemtype);
345+
$table = $searchopt[$ID]['table'];
346+
$field = $searchopt[$ID]['field'];
347+
$pfields_type = $searchopt[$ID]['pfields_type'] ?? '';
344348

345349
$field_field = new PluginFieldsField();
346350

351+
if (
352+
$field_field->getFromDBByCrit(
353+
[
354+
'name' => $field,
355+
'type' => 'number',
356+
],
357+
)
358+
&& $pfields_type == 'number'
359+
) {
360+
// if 'number' field with name is found with searchtype 'equals' or 'notequals'
361+
// update WHERE clause with `$table_$field.$field` because without `$table_$field.id` is used
362+
if ($searchtype == 'equals' || $searchtype == 'notequals') {
363+
$operator = ($searchtype == 'equals') ? '=' : '!=';
364+
if ($nott) {
365+
$link = $link . ' NOT ';
366+
}
367+
return $link . 'CAST(' . $DB->quoteName("$table" . '_' . "$field") . '.' . $DB->quoteName($field) . ' AS DECIMAL(10,7))' . $operator . ' ' . $DB->quoteValue($val) ;
368+
} else {
369+
// if 'number' field with name is found with <= or >= or < or > search
370+
// update WHERE clause with the correct operator
371+
$val = html_entity_decode($val);
372+
if (preg_match('/(<=|>=|>|<)/', $val, $matches)) {
373+
$operator = $matches[1];
374+
$val = trim(str_replace($operator, '', $val));
375+
return $link . $DB->quoteName("$table" . '_' . "$field") . '.' . $DB->quoteName($field) . $operator . ' ' . $DB->quoteValue($val);
376+
}
377+
}
378+
}
379+
347380
// if 'multiple' field with name is found -> 'Dropdown-XXXX' case
348381
// update WHERE clause with LIKE statement
349382
if (

inc/container.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1967,7 +1967,9 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
19671967
$opt[$i]['datatype'] = 'text';
19681968
break;
19691969
case 'number':
1970-
$opt[$i]['datatype'] = 'decimal';
1970+
// change datatype to string to get `is` / `is not` operator
1971+
$opt[$i]['datatype'] = 'string';
1972+
$opt[$i]['searchtype'] = ['contains', 'notcontains', 'equals', 'notequals'];
19711973
break;
19721974
case 'date':
19731975
case 'datetime':

0 commit comments

Comments
 (0)