FIX Search when criteria start with !

This commit is contained in:
Laurent Destailleur 2023-04-08 12:11:00 +02:00
parent 7155e5c3a4
commit 45fb3d9bbe
2 changed files with 19 additions and 21 deletions

View File

@ -9765,10 +9765,10 @@ function dol_getmypid()
* like "keyword1 keyword2" = We want record field like keyword1 AND field like keyword2
* or like "keyword1|keyword2" = We want record field like keyword1 OR field like keyword2
* If param $mode is 1, can contains an operator <, > or = like "<10" or ">=100.5 < -1000"
* If param $mode is 2, can contains a list of int id separated by comma like "1,3,4"
* If param $mode is 3, can contains a list of string separated by comma like "a,b,c"
* @param integer $mode 0=value is list of keyword strings, 1=value is a numeric test (Example ">5.5 <10"), 2=value is a list of ID separated with comma (Example '1,3,4')
* 3=value is list of string separated with comma (Example 'text 1,text 2'), 4=value is a list of ID separated with comma (Example '2,7') to be used to search into a multiselect string '1,2,3,4'
* If param $mode is 2 or -2, can contains a list of int id separated by comma like "1,3,4"
* If param $mode is 3 or -3, can contains a list of string separated by comma like "a,b,c".
* @param integer $mode 0=value is list of keyword strings, 1=value is a numeric test (Example ">5.5 <10"), 2=value is a list of ID separated with comma (Example '1,3,4'), -2 is for exclude list,
* 3=value is list of string separated with comma (Example 'text 1,text 2'), -3 if for exclude list, 4=value is a list of ID separated with comma (Example '2,7') to be used to search into a multiselect string '1,2,3,4'
* @param integer $nofirstand 1=Do not output the first 'AND'
* @return string $res The statement to append to the SQL query
* @see dolSqlDateFilter()
@ -9783,7 +9783,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$value = preg_replace('/\*/', '%', $value); // Replace * with %
}
if ($mode == 1) {
$value = preg_replace('/([!<>=]+)\s+([0-9'.preg_quote($langs->trans("DecimalSeparator"), '/').'\-])/', '\1\2', $value); // Clean string '< 10' into '<10' so we can the explode on space to get all tests to do
$value = preg_replace('/([!<>=]+)\s+([0-9'.preg_quote($langs->trans("DecimalSeparator"), '/').'\-])/', '\1\2', $value); // Clean string '< 10' into '<10' so we can then explode on space to get all tests to do
}
$value = preg_replace('/\s*\|\s*/', '|', $value);
@ -9794,16 +9794,15 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$fields = array($fields);
}
$j = 0;
foreach ($crits as $crit) {
$i1 = 0; // count the nb of and criteria added (all fields / criterias)
foreach ($crits as $crit) { // Loop on each AND criteria
$crit = trim($crit);
$i = 0;
$i2 = 0;
$i2 = 0; // count the nb of valid criteria added for this this first criteria
$newres = '';
foreach ($fields as $field) {
if ($mode == 1) {
$tmpcrits = explode('|', $crit);
$i3 = 0; // count the nb of valid criteria added for this field
$i3 = 0; // count the nb of valid criteria added for this current field
foreach ($tmpcrits as $tmpcrit) {
if ($tmpcrit !== '0' && empty($tmpcrit)) {
continue;
@ -9830,7 +9829,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$i3++; // a criteria was added to string
}
}
$i2++;
$i2++; // a criteria for 1 more field was added to string
} elseif ($mode == 2 || $mode == -2) {
$crit = preg_replace('/[^0-9,]/', '', $crit); // ID are always integer
$newres .= ($i2 > 0 ? ' OR ' : '').$field." ".($mode == -2 ? 'NOT ' : '');
@ -9838,7 +9837,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
if ($mode == -2) {
$newres .= ' OR '.$field.' IS NULL';
}
$i2++; // a criteria was added to string
$i2++; // a criteria for 1 more field was added to string
} elseif ($mode == 3 || $mode == -3) {
$tmparray = explode(',', $crit);
if (count($tmparray)) {
@ -9851,7 +9850,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
}
}
$newres .= ($i2 > 0 ? ' OR ' : '').$field." ".($mode == -3 ? 'NOT ' : '')."IN (".$db->sanitize($listofcodes, 1).")";
$i2++; // a criteria was added to string
$i2++; // a criteria for 1 more field was added to string
}
if ($mode == -3) {
$newres .= ' OR '.$field.' IS NULL';
@ -9868,20 +9867,20 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$newres .= ' OR '.$field." LIKE '%,".$db->escape($val)."'";
$newres .= ' OR '.$field." LIKE '%,".$db->escape($val).",%'";
$newres .= ')';
$i2++;
$i2++; // a criteria for 1 more field was added to string (we can add several citeria for the same field as it is a multiselect search criteria)
}
}
}
} else { // $mode=0
$tmpcrits = explode('|', $crit);
$i3 = 0; // count the nb of valid criteria added for this field
foreach ($tmpcrits as $tmpcrit) {
$i3 = 0; // count the nb of valid criteria added for the current couple criteria/field
foreach ($tmpcrits as $tmpcrit) { // loop on each OR criteria
if ($tmpcrit !== '0' && empty($tmpcrit)) {
continue;
}
$tmpcrit = trim($tmpcrit);
if ($tmpcrit == '^$') { // If we search empty, we must combined different fields with AND
if ($tmpcrit == '^$' || strpos($crit, '!') === 0) { // If we search empty, we must combined different OR fields with AND
$newres .= (($i2 > 0 || $i3 > 0) ? ' AND ' : '');
} else {
$newres .= (($i2 > 0 || $i3 > 0) ? ' OR ' : '');
@ -9927,15 +9926,15 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$i3++;
}
$i2++; // a criteria was added to string
$i2++; // a criteria for 1 more field was added to string
}
$i++;
}
if ($newres) {
$res = $res.($res ? ' AND ' : '').($i2 > 1 ? '(' : '').$newres.($i2 > 1 ? ')' : '');
}
$j++;
$i1++;
}
$res = ($nofirstand ? "" : " AND ")."(".$res.")";

View File

@ -501,7 +501,6 @@ if ($search_opp_percent) {
$sql .= dolSqlDateFilter('p.dateo', $search_sday, $search_smonth, $search_syear);
$sql .= dolSqlDateFilter('p.datee', $search_eday, $search_emonth, $search_eyear);
if ($search_date_start_start) {
$sql .= " AND p.dateo >= '".$db->idate($search_date_start_start)."'";
}