summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/ItemTable/StateItemTable.php
blob: f392322a64034c2ddf03bb97f70fe1834290f3b6 (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
<?php

/* Icinga DB Web | (c) 2022 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Widget\ItemTable;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Form;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Orm\Common\SortUtil;
use ipl\Orm\Query;
use ipl\Web\Control\SortControl;
use ipl\Web\Widget\EmptyStateBar;
use ipl\Web\Widget\Icon;

/** @todo Figure out what this might (should) have in common with the new BaseItemTable implementation */
abstract class StateItemTable extends BaseHtmlElement
{
    protected $baseAttributes = [
        'class' => 'state-item-table'
    ];

    /** @var array<string, string> The columns to render */
    protected $columns;

    /** @var iterable The datasource */
    protected $data;

    /** @var string The sort rules */
    protected $sort;

    protected $tag = 'table';

    /**
     * Create a new item table
     *
     * @param iterable $data Datasource of the table
     * @param array<string, string> $columns The columns to render, keys are labels
     */
    public function __construct(iterable $data, array $columns)
    {
        $this->data = $data;
        $this->columns = array_flip($columns);

        $this->addAttributes($this->baseAttributes);

        $this->init();
    }

    /**
     * Initialize the item table
     *
     * If you want to adjust the item table after construction, override this method.
     */
    protected function init()
    {
    }

    /**
     * Get the columns being rendered
     *
     * @return array<string, string>
     */
    public function getColumns(): array
    {
        return $this->columns;
    }

    /**
     * Set sort rules (as returned by {@see SortControl::getSort()})
     *
     * @param ?string $sort
     *
     * @return $this
     */
    public function setSort(?string $sort): self
    {
        $this->sort = $sort;

        return $this;
    }

    abstract protected function getItemClass(): string;

    abstract protected function getVisualColumn(): string;

    protected function getVisualLabel()
    {
        return new Icon('heartbeat', ['title' => t('Severity')]);
    }

    protected function assembleColumnHeader(BaseHtmlElement $header, string $name, $label): void
    {
        $sortRules = [];
        if ($this->sort !== null) {
            $sortRules = SortUtil::createOrderBy($this->sort);
        }

        $active = false;
        $sortDirection = null;
        foreach ($sortRules as $rule) {
            if ($rule[0] === $name) {
                $sortDirection = $rule[1];
                $active = true;
                break;
            }
        }

        if ($sortDirection === 'desc') {
            $value = "$name asc";
        } else {
            $value = "$name desc";
        }

        $icon = 'sort';
        if ($active) {
            $icon = $sortDirection === 'desc' ? 'sort-up' : 'sort-down';
        }

        $form = new Form();
        $form->setAttribute('method', 'GET');

        $button = $form->createElement('button', 'sort', [
            'value' => $value,
            'type'  => 'submit',
            'title' => is_string($label) ? $label : null,
            'class' => $active ? 'active' : null
        ]);
        $button->addHtml(
            Html::tag(
                'span',
                null,
                // With &nbsp; to have the height sized the same as the others
                $label ?? HtmlString::create('&nbsp;')
            ),
            new Icon($icon)
        );
        $form->addElement($button);

        $header->add($form);

        switch (true) {
            case substr($name, -7) === '.output':
            case substr($name, -12) === '.long_output':
                $header->getAttributes()->add('class', 'has-plugin-output');
                break;
            case substr($name, -22) === '.icon_image.icon_image':
                $header->getAttributes()->add('class', 'has-icon-images');
                break;
            case substr($name, -17) === '.performance_data':
            case substr($name, -28) === '.normalized_performance_data':
                $header->getAttributes()->add('class', 'has-performance-data');
                break;
        }
    }

    protected function assemble()
    {
        $itemClass = $this->getItemClass();

        $headerRow = new HtmlElement('tr');

        $visualCell = new HtmlElement('th', Attributes::create(['class' => 'has-visual']));
        $this->assembleColumnHeader($visualCell, $this->getVisualColumn(), $this->getVisualLabel());
        $headerRow->addHtml($visualCell);

        foreach ($this->columns as $name => $label) {
            $headerCell = new HtmlElement('th');
            $this->assembleColumnHeader($headerCell, $name, is_int($label) ? $name : $label);
            $headerRow->addHtml($headerCell);
        }

        $this->addHtml(new HtmlElement('thead', null, $headerRow));

        $body = new HtmlElement('tbody', Attributes::create(['data-base-target' => '_next']));
        foreach ($this->data as $item) {
            $body->addHtml(new $itemClass($item, $this));
        }

        if ($body->isEmpty()) {
            $body->addHtml(new HtmlElement(
                'tr',
                null,
                new HtmlElement(
                    'td',
                    Attributes::create(['colspan' => count($this->columns)]),
                    new EmptyStateBar(t('No items found.'))
                )
            ));
        }

        $this->addHtml($body);
    }

    /**
     * Enrich the given list of column names with appropriate labels
     *
     * @param Query $query
     * @param array $columns
     *
     * @return array
     */
    public static function applyColumnMetaData(Query $query, array $columns): array
    {
        $newColumns = [];
        foreach ($columns as $columnPath) {
            $label = $query->getResolver()->getColumnDefinition($columnPath)->getLabel();
            $newColumns[$label ?? $columnPath] = $columnPath;
        }

        return $newColumns;
    }
}