mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-08 19:42:15 +09:00
Update search operator to process quotation marks and AND or OR operator
This commit is contained in:
parent
86798f7076
commit
d081b75dcc
1 changed files with 40 additions and 7 deletions
|
|
@ -237,23 +237,56 @@ class VariableBase
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'search':
|
case 'search':
|
||||||
$keywords = preg_split('/[\s,]+/', $value, 10, \PREG_SPLIT_NO_EMPTY);
|
$keywords = preg_split('/("[^"]*")|[\s,]+/', $value, 10, \PREG_SPLIT_NO_EMPTY);
|
||||||
$conditions = array();
|
$conditions = array();
|
||||||
|
$operators = array('AND', 'OR', '|');
|
||||||
$placeholders = implode(', ', array_fill(0, count($keywords), '?'));
|
$placeholders = implode(', ', array_fill(0, count($keywords), '?'));
|
||||||
foreach ($keywords as $item)
|
foreach ($keywords as $item)
|
||||||
{
|
{
|
||||||
if (substr($item, 0, 1) === '-')
|
// trim quotation mark
|
||||||
|
if (strlen($item) > 2 && (substr($item, 0, 1) === substr($item, -1)) && substr($item, -1) === '"')
|
||||||
{
|
{
|
||||||
$conditions[] = sprintf('%s NOT LIKE ?', $column);
|
$item = substr($item, 1, -1);
|
||||||
$item = substr($item, 1);
|
}
|
||||||
|
elseif (strlen($item) > 3 && (substr($item, 0, 1) === '-') && (substr($item, 1, 1) === substr($item, -1)) && substr($item, -1) === '"')
|
||||||
|
{
|
||||||
|
$item = '-' . substr($item, 2, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pass blank text
|
||||||
|
if (trim($item) === "")
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// process 'AND' or 'OR' operator
|
||||||
|
if (in_array($item, $operators)
|
||||||
|
{
|
||||||
|
if ($item === '|')
|
||||||
|
{
|
||||||
|
$item = 'OR';
|
||||||
|
}
|
||||||
|
$conditions[] = sprintf('%s', $item);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$conditions[] = sprintf('%s LIKE ?', $column);
|
if (substr($item, 0, 1) === '-')
|
||||||
|
{
|
||||||
|
$conditions[] = sprintf('%s NOT LIKE ?', $column);
|
||||||
|
$item = substr($item, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$conditions[] = sprintf('%s LIKE ?', $column);
|
||||||
|
}
|
||||||
|
$params[] = '%' . str_replace(['\\', '_', '%'], ['\\\\', '\_', '\%'], $item) . '%';
|
||||||
|
// if there is no operator, assume 'AND'
|
||||||
|
$conditions[] = 'AND';
|
||||||
}
|
}
|
||||||
$params[] = '%' . str_replace(['\\', '_', '%'], ['\\\\', '\_', '\%'], $item) . '%';
|
|
||||||
}
|
}
|
||||||
$conditions = implode(' AND ', $conditions);
|
// remove the last point (would be an operator)
|
||||||
|
array_pop($conditions)
|
||||||
|
$conditions = implode(' ', $conditions);
|
||||||
$where = count($keywords) === 1 ? $conditions : "($conditions)";
|
$where = count($keywords) === 1 ? $conditions : "($conditions)";
|
||||||
break;
|
break;
|
||||||
case 'plus':
|
case 'plus':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue