summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/QuickTable.php
blob: ff3edcc0afd8e259028984918d97f4b5d82f3f15 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Application\Icinga;
use Icinga\Data\Filter\FilterAnd;
use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Data\Filter\FilterNot;
use Icinga\Data\Filter\FilterOr;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Selectable;
use Icinga\Data\Paginatable;
use Icinga\Exception\QueryException;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\PlainObjectRenderer;
use Icinga\Web\Request;
use gipfl\IcingaWeb2\Url;
use Icinga\Web\View;
use Icinga\Web\Widget;
use Icinga\Web\Widget\Paginator;
use ipl\Html\ValidHtml;
use stdClass;
use Zend_Db_Select as ZfDbSelect;

abstract class QuickTable implements Paginatable, ValidHtml
{
    protected $view;

    /** @var Db */
    protected $connection;

    protected $limit;

    protected $offset;

    /** @var Filter */
    protected $filter;

    protected $enforcedFilters = array();

    protected $searchColumns = array();

    protected function getRowClasses($row)
    {
        return array();
    }

    protected function getRowClassesString($row)
    {
        return $this->createClassAttribute($this->getRowClasses($row));
    }

    protected function createClassAttribute($classes)
    {
        $str = $this->createClassesString($classes);
        if (strlen($str) > 0) {
            return ' class="' . $str . '"';
        } else {
            return '';
        }
    }

    private function createClassesString($classes)
    {
        if (is_string($classes)) {
            $classes = array($classes);
        }

        if (empty($classes)) {
            return '';
        } else {
            return implode(' ', $classes);
        }
    }

    protected function getMultiselectProperties()
    {
        /* array(
         *     'url'       => 'director/hosts/edit',
         *     'sourceUrl' => 'director/hosts',
         *     'keys'      => 'name'
         * ) */

        return array();
    }

    protected function renderMultiselectAttributes()
    {
        $props = $this->getMultiselectProperties();

        if (empty($props)) {
            return '';
        }

        $prefix = 'data-icinga-multiselect-';
        $view = $this->view();
        $parts = array();
        $multi = array(
            'url'         => $view->href($props['url']),
            'controllers' => $view->href($props['sourceUrl']),
            'data'        => implode(',', $props['keys']),
        );

        foreach ($multi as $k => $v) {
            $parts[] = $prefix . $k . '="' . $v . '"';
        }

        return ' ' . implode(' ', $parts);
    }

    protected function renderRow($row)
    {
        $htm = "  <tr" . $this->getRowClassesString($row) . ">\n";
        $firstCol = true;

        foreach ($this->getTitles() as $key => $title) {
            // Support missing columns
            if (property_exists($row, $key)) {
                $val = $row->$key;
            } else {
                $val = null;
            }

            $value = null;

            if ($firstCol) {
                if ($val !== null && $url = $this->getActionUrl($row)) {
                    $value = $this->view()->qlink($val, $this->getActionUrl($row));
                }
                $firstCol = false;
            }

            if ($value === null) {
                if ($val === null) {
                    $value = '-';
                } elseif (is_array($val) || $val instanceof stdClass || is_bool($val)) {
                    $value = '<pre>'
                           . $this->view()->escape(PlainObjectRenderer::render($val))
                           . '</pre>';
                } else {
                    $value = $this->view()->escape($val);
                }
            }

            $htm .= '    <td>' . $value . "</td>\n";
        }

        if ($this->hasAdditionalActions()) {
            $htm .= '    <td class="actions">' . $this->renderAdditionalActions($row) . "</td>\n";
        }

        return $htm . "  </tr>\n";
    }

    abstract protected function getTitles();

    protected function getActionUrl($row)
    {
        return false;
    }

    public function setConnection(Selectable $connection)
    {
        $this->connection = $connection;
        return $this;
    }

    /**
     * @return ZfDbSelect
     */
    abstract protected function getBaseQuery();

    public function fetchData()
    {
        $db = $this->db();
        $query = $this->getBaseQuery()->columns($this->getColumns());

        if ($this->hasLimit() || $this->hasOffset()) {
            $query->limit($this->getLimit(), $this->getOffset());
        }

        $this->applyFiltersToQuery($query);

        return $db->fetchAll($query);
    }

    protected function applyFiltersToQuery(ZfDbSelect $query)
    {
        $filter = null;
        $enforced = $this->enforcedFilters;
        if ($this->filter && ! $this->filter->isEmpty()) {
            $filter = $this->filter;
        } elseif (! empty($enforced)) {
            $filter = array_shift($enforced);
        }
        if ($filter) {
            foreach ($enforced as $f) {
                $filter = $filter->andFilter($f);
            }
            $query->where($this->renderFilter($filter));
        }

        return $query;
    }

    public function getPaginator()
    {
        $paginator = new Paginator();
        $paginator->setQuery($this);

        return $paginator;
    }

    #[\ReturnTypeWillChange]
    public function count()
    {
        $db = $this->db();
        $query = clone($this->getBaseQuery());
        $query->reset('order')->columns(array('COUNT(*)'));
        $this->applyFiltersToQuery($query);

        return $db->fetchOne($query);
    }

    public function limit($count = null, $offset = null)
    {
        $this->limit = $count;
        $this->offset = $offset;

        return $this;
    }

    public function hasLimit()
    {
        return $this->limit !== null;
    }

    public function getLimit()
    {
        return $this->limit;
    }

    public function hasOffset()
    {
        return $this->offset !== null;
    }

    public function getOffset()
    {
        return $this->offset;
    }

    public function hasAdditionalActions()
    {
        return method_exists($this, 'renderAdditionalActions');
    }

    /** @return Db */
    protected function connection()
    {
        // TODO: Fail if missing? Require connection in constructor?
        return $this->connection;
    }

    protected function db()
    {
        return $this->connection()->getDbAdapter();
    }

    protected function renderTitles($row)
    {
        $view = $this->view();
        $htm = "<thead>\n  <tr>\n";

        foreach ($row as $title) {
            $htm .= '    <th>' . $view->escape($title) . "</th>\n";
        }

        if ($this->hasAdditionalActions()) {
            $htm .= '    <th class="actions">' . $view->translate('Actions') . "</th>\n";
        }

        return $htm . "  </tr>\n</thead>\n";
    }

    protected function url($url, $params)
    {
        return Url::fromPath($url, $params);
    }

    protected function listTableClasses()
    {
        $classes = array('simple', 'common-table', 'table-row-selectable');
        $multi = $this->getMultiselectProperties();
        if (! empty($multi)) {
            $classes[] = 'multiselect';
        }

        return $classes;
    }

    public function render()
    {
        $data = $this->fetchData();

        $htm = '<table'
             . $this->createClassAttribute($this->listTableClasses())
             . $this->renderMultiselectAttributes()
             . '>' . "\n"
             . $this->renderTitles($this->getTitles())
             . $this->beginTableBody();
        foreach ($data as $row) {
            $htm .= $this->renderRow($row);
        }
        return $htm . $this->endTableBody() . $this->endTable();
    }

    protected function beginTableBody()
    {
        return "<tbody>\n";
    }

    protected function endTableBody()
    {
        return "</tbody>\n";
    }

    protected function endTable()
    {
        return "</table>\n";
    }

    /**
     * @return View
     */
    protected function view()
    {
        if ($this->view === null) {
            $this->view = Icinga::app()->getViewRenderer()->view;
        }
        return $this->view;
    }


    public function setView($view)
    {
        $this->view = $view;
    }

    public function __toString()
    {
        return $this->render();
    }

    protected function getSearchColumns()
    {
        return $this->searchColumns;
    }

    abstract public function getColumns();

    public function getFilterColumns()
    {
        $keys = array_keys($this->getColumns());
        return array_combine($keys, $keys);
    }

    public function setFilter($filter)
    {
        $this->filter = $filter;
        return $this;
    }

    public function enforceFilter($filter, $expression = null)
    {
        if (! $filter instanceof Filter) {
            $filter = Filter::where($filter, $expression);
        }
        $this->enforcedFilters[] = $filter;
        return $this;
    }

    public function getFilterEditor(Request $request)
    {
        $filterEditor = Widget::create('filterEditor')
            ->setColumns(array_keys($this->getColumns()))
            ->setSearchColumns($this->getSearchColumns())
            ->preserveParams('limit', 'sort', 'dir', 'view', 'backend', '_dev')
            ->ignoreParams('page')
            ->handleRequest($request);

        $filter = $filterEditor->getFilter();
        $this->setFilter($filter);

        return $filterEditor;
    }

    protected function mapFilterColumn($col)
    {
        $cols = $this->getColumns();
        return $cols[$col];
    }

    protected function renderFilter(Filter $filter, $level = 0)
    {
        $str = '';
        if ($filter instanceof FilterChain) {
            if ($filter instanceof FilterAnd) {
                $op = ' AND ';
            } elseif ($filter instanceof FilterOr) {
                $op = ' OR ';
            } elseif ($filter instanceof FilterNot) {
                $op = ' AND ';
                $str .= ' NOT ';
            } else {
                throw new QueryException(
                    'Cannot render filter: %s',
                    $filter
                );
            }
            $parts = array();
            if (! $filter->isEmpty()) {
                foreach ($filter->filters() as $f) {
                    $filterPart = $this->renderFilter($f, $level + 1);
                    if ($filterPart !== '') {
                        $parts[] = $filterPart;
                    }
                }
                if (! empty($parts)) {
                    if ($level > 0) {
                        $str .= ' (' . implode($op, $parts) . ') ';
                    } else {
                        $str .= implode($op, $parts);
                    }
                }
            }
        } else {
            /** @var FilterExpression $filter */
            $str .= $this->whereToSql(
                $this->mapFilterColumn($filter->getColumn()),
                $filter->getSign(),
                $filter->getExpression()
            );
        }

        return $str;
    }

    protected function escapeForSql($value)
    {
        // bindParam? bindValue?
        if (is_array($value)) {
            $ret = array();
            foreach ($value as $val) {
                $ret[] = $this->escapeForSql($val);
            }
            return implode(', ', $ret);
        } else {
            //if (preg_match('/^\d+$/', $value)) {
            //    return $value;
            //} else {
            return $this->db()->quote($value);
            //}
        }
    }

    protected function escapeWildcards($value)
    {
        return preg_replace('/\*/', '%', $value);
    }

    protected function valueToTimestamp($value)
    {
        // We consider integers as valid timestamps. Does not work for URL params
        if (! is_string($value) || ctype_digit($value)) {
            return $value;
        }
        $value = strtotime($value);
        if (! $value) {
            /*
            NOTE: It's too late to throw exceptions, we might finish in __toString
            throw new QueryException(sprintf(
                '"%s" is not a valid time expression',
                $value
            ));
            */
        }
        return $value;
    }

    protected function timestampForSql($value)
    {
        // TODO: do this db-aware
        return $this->escapeForSql(date('Y-m-d H:i:s', $value));
    }

    /**
     * Check for timestamp fields
     *
     * TODO: This is not here to do automagic timestamp stuff. One may
     *       override this function for custom voodoo, IdoQuery right now
     *       does. IMO we need to split whereToSql functionality, however
     *       I'd prefer to wait with this unless we understood how other
     *       backends will work. We probably should also rename this
     *       function to isTimestampColumn().
     *
     * @param  string $field Field Field name to checked
     * @return bool          Whether this field expects timestamps
     */
    public function isTimestamp($field)
    {
        return false;
    }

    public function whereToSql($col, $sign, $expression)
    {
        if ($this->isTimestamp($col)) {
            $expression = $this->valueToTimestamp($expression);
        }

        if (is_array($expression) && $sign === '=') {
            // TODO: Should we support this? Doesn't work for blub*
            return $col . ' IN (' . $this->escapeForSql($expression) . ')';
        } elseif ($sign === '=' && strpos($expression, '*') !== false) {
            if ($expression === '*') {
                // We'll ignore such filters as it prevents index usage and because "*" means anything, anything means
                // all whereas all means that whether we use a filter to match anything or no filter at all makes no
                // difference, except for performance reasons...
                return '';
            }

            return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
        } elseif ($sign === '!=' && strpos($expression, '*') !== false) {
            if ($expression === '*') {
                // We'll ignore such filters as it prevents index usage and because "*" means nothing, so whether we're
                // using a real column with a valid comparison here or just an expression which cannot be evaluated to
                // true makes no difference, except for performance reasons...
                return $this->escapeForSql(0);
            }

            return $col . ' NOT LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
        } else {
            return $col . ' ' . $sign . ' ' . $this->escapeForSql($expression);
        }
    }
}