Skip to content

Commit

Permalink
AssignFilter: add 'contains' operator
Browse files Browse the repository at this point in the history
fixes #13133
  • Loading branch information
Thomas-Gelf committed Dec 13, 2016
1 parent 58a64ce commit 7c6a100
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
37 changes: 28 additions & 9 deletions application/views/helpers/FormDataFilter.php
Expand Up @@ -160,7 +160,12 @@ protected function element(FilterExpression $filter = null)
$type = 'host';
$filter = clone($filter);

$filter->setExpression(json_decode($filter->getExpression()));
if ($this->columnIsJson($filter)) {
$filter->setExpression(json_decode($filter->getColumn()));
} else {
$filter->setExpression(json_decode($filter->getExpression()));
}

$dummy = IcingaObject::createByType($type);
$col = $filter->getColumn();
if ($dummy->hasProperty($col)) {
Expand Down Expand Up @@ -204,6 +209,12 @@ protected function boolean(Filter $filter = null)
return $el;
}

protected function columnIsJson(FilterExpression $filter)
{
$col = $filter->getColumn();
return strlen($col) && $col[0] === '"';
}

protected function text(Filter $filter = null)
{
$value = $filter === null ? '' : $filter->getExpression();
Expand Down Expand Up @@ -275,7 +286,7 @@ protected function selectOperator(Filter $filter = null)
);
}

protected function selectSign(Filter $filter = null)
protected function selectSign(FilterExpression $filter = null)
{
$signs = array(
'=' => '=',
Expand All @@ -285,19 +296,24 @@ protected function selectSign(Filter $filter = null)
'>=' => '>=',
'<=' => '<=',
'in' => 'in',
'contains' => 'contains',
// 'true' => 'is true (or set)',
);

if ($filter === null) {
$sign = null;
} else {
$expression = json_decode($filter->getExpression());
if ($expression === true) {
$sign = 'true';
} elseif (is_array($expression)) {
$sign = 'in';
if ($this->columnIsJson($filter)) {
$sign = 'contains';
} else {
$sign = $filter->getSign();
$expression = json_decode($filter->getExpression());
if ($expression === true) {
$sign = 'true';
} elseif (is_array($expression)) {
$sign = 'in';
} else {
$sign = $filter->getSign();
}
}
}

Expand All @@ -320,9 +336,12 @@ public function setColumns(array $columns = null)
return $this;
}

protected function selectColumn(Filter $filter = null)
protected function selectColumn(FilterExpression $filter = null)
{
$active = $filter === null ? null : $filter->getColumn();
if ($filter && $this->columnIsJson($filter)) {
$active = $filter->getExpression();
}

if (! $this->hasColumnList()) {
return $this->view->formText(
Expand Down
9 changes: 8 additions & 1 deletion library/Director/IcingaConfig/AssignRenderer.php
Expand Up @@ -60,7 +60,14 @@ protected function renderFilter(Filter $filter)

protected function renderEquals($column, $expression)
{
if (substr($column, -7) === '.groups') {
if ($column[0] === '"') {
// "me"=vars.users -> "me" in vars.users
return sprintf(
'%s in %s',
$column,
$expression
);
} else if (substr($column, -7) === '.groups') {
return sprintf(
'%s in %s',
$expression,
Expand Down
8 changes: 8 additions & 0 deletions library/Director/Web/Form/Element/DataFilter.php
Expand Up @@ -275,6 +275,14 @@ protected function entryToFilterExpression($entry)
'=',
json_encode($value)
);
} elseif ($entry['sign'] === 'contains') {
$value = array_key_exists('value', $entry) ? $entry['value'] : null;

return Filter::expression(
json_encode($value),
'=',
$entry['column']
);
} else {
$value = array_key_exists('value', $entry) ? $entry['value'] : null;

Expand Down

0 comments on commit 7c6a100

Please sign in to comment.