summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Data/Filter/FilterChain.php
blob: 0f1e07198cb4054cfa5baa2e104f34595f38dd6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Data\Filter;

use Icinga\Exception\ProgrammingError;
use Icinga\Exception\QueryException;

/**
 * FilterChain
 *
 * A FilterChain contains a list ...
 */
abstract class FilterChain extends Filter
{
    protected $filters = array();

    protected $operatorName;

    protected $operatorSymbol;

    protected $allowedColumns;

    /**
     * Set the filters
     *
     * @param   array   $filters
     *
     * @return  $this
     */
    public function setFilters(array $filters)
    {
        $this->filters = $filters;

        $this->refreshChildIds();

        return $this;
    }

    public function hasId($id)
    {
        foreach ($this->filters() as $filter) {
            if ($filter->hasId($id)) {
                return true;
            }
        }
        return parent::hasId($id);
    }

    public function getById($id)
    {
        foreach ($this->filters() as $filter) {
            if ($filter->hasId($id)) {
                return $filter->getById($id);
            }
        }
        return parent::getById($id);
    }

    public function removeId($id)
    {
        if ($id === $this->getId()) {
            $this->filters = array();
            return $this;
        }
        $remove = null;
        foreach ($this->filters as $key => $filter) {
            if ($filter->getId() === $id) {
                $remove = $key;
            } elseif ($filter instanceof FilterChain) {
                $filter->removeId($id);
            }
        }
        if ($remove !== null) {
            unset($this->filters[$remove]);
            $this->filters = array_values($this->filters);
        }
        $this->refreshChildIds();
        return $this;
    }

    public function replaceById($id, $filter)
    {
        $found = false;
        foreach ($this->filters as $k => $child) {
            if ($child->getId() == $id) {
                $this->filters[$k] = $filter;
                $found = true;
                break;
            }
            if ($child->hasId($id)) {
                $child->replaceById($id, $filter);
                $found = true;
                break;
            }
        }
        if (! $found) {
            throw new ProgrammingError('You tried to replace an unexistant child filter');
        }
        $this->refreshChildIds();
        return $this;
    }

    protected function refreshChildIds()
    {
        $i = 0;
        $id = $this->getId();
        foreach ($this->filters as $filter) {
            $i++;
            $filter->setId($id . '-' . $i);
        }
        return $this;
    }

    public function setId($id)
    {
        return parent::setId($id)->refreshChildIds();
    }

    public function getOperatorName()
    {
        return $this->operatorName;
    }

    public function setOperatorName($name)
    {
        if ($name !== $this->operatorName) {
            return Filter::chain($name, $this->filters);
        }
        return $this;
    }

    public function getOperatorSymbol()
    {
        return $this->operatorSymbol;
    }

    public function setAllowedFilterColumns(array $columns)
    {
        $this->allowedColumns = $columns;
        return $this;
    }

    /**
     * List and return all column names referenced in this filter
     *
     * @param   array   $columns    The columns listed so far
     *
     * @return  array
     */
    public function listFilteredColumns(array $columns = array())
    {
        foreach ($this->filters as $filter) {
            if ($filter instanceof FilterExpression) {
                $column= $filter->getColumn();
                if (! in_array($column, $columns, true)) {
                    $columns[] = $column;
                }
            } else {
                $columns = $filter->listFilteredColumns($columns);
            }
        }

        return $columns;
    }

    public function toQueryString()
    {
        $parts = array();
        if (empty($this->filters)) {
            return '';
        }
        foreach ($this->filters() as $filter) {
            if (! $filter->isEmpty()) {
                $parts[] = $filter->toQueryString();
            }
        }

        // TODO: getLevel??
        if (strpos($this->getId(), '-')) {
            return '(' . implode($this->getOperatorSymbol(), $parts) . ')';
        } else {
            return implode($this->getOperatorSymbol(), $parts);
        }
    }

    /**
     * Get simple string representation
     *
     * Useful for debugging only
     *
     * @return string
     */
    public function __toString()
    {
        if (empty($this->filters)) {
            return '';
        }
        $parts = array();
        foreach ($this->filters as $filter) {
            if ($filter instanceof FilterChain) {
                $parts[] = '(' . $filter . ')';
            } else {
                $parts[] = (string) $filter;
            }
        }
        $op = ' '  . $this->getOperatorSymbol() . ' ';
        return implode($op, $parts);
    }

    public function __construct($filters = array())
    {
        foreach ($filters as $filter) {
            $this->addFilter($filter);
        }
    }

    public function isExpression()
    {
        return false;
    }

    public function isChain()
    {
        return true;
    }

    public function isEmpty()
    {
        return empty($this->filters);
    }

    public function addFilter(Filter $filter)
    {
        if (! empty($this->allowedColumns)) {
            $this->validateFilterColumns($filter);
        }

        $this->filters[] = $filter;
        $filter->setId($this->getId() . '-' . $this->count());
        return $this;
    }

    protected function validateFilterColumns(Filter $filter)
    {
        if ($filter->isExpression()) {
            $valid = false;
            foreach ($this->allowedColumns as $column) {
                if (is_callable($column)) {
                    if (call_user_func($column, $filter->getColumn())) {
                        $valid = true;
                        break;
                    }
                } elseif ($filter->getColumn() === $column) {
                    $valid = true;
                    break;
                }
            }

            if (! $valid) {
                throw new QueryException('Invalid filter column provided: %s', $filter->getColumn());
            }
        } else {
            foreach ($filter->filters() as $subFilter) {
                $this->validateFilterColumns($subFilter);
            }
        }
    }

    public function &filters()
    {
        return $this->filters;
    }

    public function count()
    {
        return count($this->filters);
    }

    public function __clone()
    {
        foreach ($this->filters as & $filter) {
            $filter = clone $filter;
        }
    }
}