2
0
forked from Wavyzz/dolibarr

Merge backport

This commit is contained in:
ldestailleur
2025-03-04 20:50:54 +01:00
parent 7d8fd29f63
commit 86f836b652
2 changed files with 27 additions and 4 deletions

View File

@@ -11710,13 +11710,20 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$value = preg_replace('/\s*\|\s*/', '|', $value);
$crits = explode(' ', $value);
// Split criteria on ' '.
// For mode 3, the split is done later on the , only and not on the ' '.
if ($mode != -3 && $mode != 3) {
$crits = explode(' ', $value);
} else {
$crits = array($value);
}
$res = '';
if (!is_array($fields)) {
$fields = array($fields);
}
$i1 = 0; // count the nb of and criteria added (all fields / criteria)
$i1 = 0; // count the nb of "and" criteria added (all fields / criteria)
foreach ($crits as $crit) { // Loop on each AND criteria
$crit = trim($crit);
$i2 = 0; // count the nb of valid criteria added for this this first criteria
@@ -11771,7 +11778,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0)
$listofcodes .= "'".$db->escape($val)."'";
}
}
$newres .= ($i2 > 0 ? ' OR ' : '').$field." ".($mode == -3 ? 'NOT ' : '')."IN (".$db->sanitize($listofcodes, 1).")";
$newres .= ($i2 > 0 ? ' OR ' : '').$field." ".($mode == -3 ? 'NOT ' : '')."IN (".$db->sanitize($listofcodes, 1, 0, 1).")";
$i2++; // a criteria for 1 more field was added to string
}
if ($mode == -3) {

View File

@@ -1894,7 +1894,6 @@ class FunctionsLibTest extends CommonClassTest
return true;
}
/**
* testRoundUpToNextMultiple
*
@@ -1918,4 +1917,21 @@ class FunctionsLibTest extends CommonClassTest
$this->assertEquals(roundUpToNextMultiple(40.5, 6), 42);
$this->assertEquals(roundUpToNextMultiple(44.5, 6), 48);
}
/**
* testNaturalSearch
*
* @return void;
*/
public function testNaturalSearch()
{
$s = natural_search("t.field", "abc def");
$this->assertEquals($s, " AND (t.field LIKE '%abc%' AND t.field LIKE '%def%')");
$s = natural_search("t.field", "'abc def' ghi");
$this->assertEquals($s, " AND (t.field LIKE '%abc def%' AND t.field LIKE '%ghi%')");
$s = natural_search("t.field", "abc def,ghi", 3);
$this->assertEquals($s, " AND (t.field IN ('abc def','ghi'))");
}
}