mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-27 15:54:03 +02:00
Filter handling cleanup and small improvements:
* a pipe (|) is now accepted as OR * improved readability * prepared for splitting logic - most of this could be backend-agnostic * greater / less then is now possible
This commit is contained in:
parent
5467950e79
commit
d44c15eea0
@ -288,8 +288,11 @@ abstract class AbstractQuery extends Query
|
|||||||
$or = array();
|
$or = array();
|
||||||
$and = array();
|
$and = array();
|
||||||
|
|
||||||
if (! is_array($value) && strpos($value, ',') !== false) {
|
if (
|
||||||
$value = preg_split('~,~', $value, -1, PREG_SPLIT_NO_EMPTY);
|
! is_array($value) &&
|
||||||
|
(strpos($value, ',') !== false || strpos($value, '|') !== false)
|
||||||
|
) {
|
||||||
|
$value = preg_split('~[,|]~', $value, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
}
|
}
|
||||||
if (! is_array($value)) {
|
if (! is_array($value)) {
|
||||||
$value = array($value);
|
$value = array($value);
|
||||||
@ -301,38 +304,56 @@ abstract class AbstractQuery extends Query
|
|||||||
// TODO: REALLY??
|
// TODO: REALLY??
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Value starting with minus: negation
|
$not = false;
|
||||||
if ($val[0] === '-') {
|
$force = false;
|
||||||
|
$op = '=';
|
||||||
|
$wildcard = false;
|
||||||
|
|
||||||
|
if ($val[0] === '-' || $val[0] === '!') {
|
||||||
|
// Value starting with minus or !: negation
|
||||||
$val = substr($val, 1);
|
$val = substr($val, 1);
|
||||||
if (strpos($val, '*') === false) {
|
$not = true;
|
||||||
$and[] = $this->db->quoteInto($column . ' != ?', $val);
|
|
||||||
} else {
|
|
||||||
$and[] = $this->db->quoteInto(
|
|
||||||
$column . ' NOT LIKE ?',
|
|
||||||
str_replace('*', '%', $val)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} elseif ($val[0] === '+') { // Value starting with +: enforces AND
|
|
||||||
|
if ($val[0] === '+') {
|
||||||
|
// Value starting with +: enforces AND
|
||||||
// TODO: depends on correct URL handling, not given in all
|
// TODO: depends on correct URL handling, not given in all
|
||||||
// ZF versions
|
// ZF versions.
|
||||||
$val = substr($val, 1);
|
$val = substr($val, 1);
|
||||||
if (strpos($val, '*') === false) {
|
$force = true;
|
||||||
$and[] = $this->db->quoteInto($column . ' = ?', $val);
|
|
||||||
} else {
|
|
||||||
$and[] = $this->db->quoteInto(
|
|
||||||
$column . ' LIKE ?',
|
|
||||||
str_replace('*', '%', $val)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else { // All others ar ORs:
|
if ($val[0] === '<' || $val[0] === '>') {
|
||||||
if (strpos($val, '*') === false) {
|
$op = $val[0];
|
||||||
$or[] = $this->db->quoteInto($column . ' = ?', $val);
|
$val = substr($val, 1);
|
||||||
} else {
|
|
||||||
$or[] = $this->db->quoteInto(
|
|
||||||
$column . ' LIKE ?',
|
|
||||||
str_replace('*', '%', $val)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
if (strpos($val, '*') !== false) {
|
||||||
|
$wildcard = true;
|
||||||
|
$val = str_replace('*', '%', $val);
|
||||||
|
}
|
||||||
|
|
||||||
|
$operator = null;
|
||||||
|
switch ($op) {
|
||||||
|
case '=':
|
||||||
|
if ($not) {
|
||||||
|
$operator = $wildcard ? 'NOT LIKE' : '!=';
|
||||||
|
} else {
|
||||||
|
$operator = $wildcard ? 'LIKE' : '=';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '>':
|
||||||
|
$operator = $not ? '<=' : '>';
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
$operator = $not ? '>=' : '<';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ProgrammingError("'$op' is not a valid operator");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($not || $force) {
|
||||||
|
$and[] = $this->db->quoteInto($column . ' ' . $operator . ' ?', $val);
|
||||||
|
} else {
|
||||||
|
$or[] = $this->db->quoteInto($column . ' ' . $operator . ' ?', $val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user