summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/Control/SortControl.php
blob: 65c2c3d7320b33ec3a5404cabe40557db3015130 (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
<?php

namespace ipl\Web\Control;

use GuzzleHttp\Psr7\ServerRequest;
use ipl\Html\Form;
use ipl\Html\FormDecorator\DivDecorator;
use ipl\Html\FormElement\ButtonElement;
use ipl\Html\HtmlElement;
use ipl\Orm\Common\SortUtil;
use ipl\Orm\Query;
use ipl\Stdlib\Str;
use ipl\Web\Common\FormUid;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
use Psr\Http\Message\ServerRequestInterface;

/**
 * Allows to adjust the order of the items to display
 */
class SortControl extends Form
{
    use FormUid;

    /** @var string Default sort param */
    public const DEFAULT_SORT_PARAM = 'sort';

    protected $defaultAttributes = ['class' => 'sort-control'];

    /** @var string Name of the URL parameter which stores the sort column */
    protected $sortParam = self::DEFAULT_SORT_PARAM;

    /**
     * @var Url Request URL
     * @deprecated Access {@see self::getRequest()} instead.
     * @todo Remove once cube calls {@see self::handleRequest()}.
     */
    protected $url;

    /** @var array Possible sort columns as sort string-value pairs */
    private $columns;

    /** @var ?string Default sort string */
    private $default;

    protected $method = 'GET';

    /**
     * Create a new sort control
     *
     * @param array $columns Possible sort columns
     * @param Url $url Request URL
     *
     * @internal Use {@see self::create()} instead.
     */
    private function __construct(array $columns, Url $url)
    {
        $this->setColumns($columns);
        $this->url = $url;
    }

    /**
     * Create a new sort control with the given options
     *
     * @param array<string,string> $options A sort spec to label map
     *
     * @return static
     */
    public static function create(array $options)
    {
        $normalized = [];
        foreach ($options as $spec => $label) {
            $normalized[SortUtil::normalizeSortSpec($spec)] = $label;
        }

        $self = new static($normalized, Url::fromRequest());

        $self->on(self::ON_REQUEST, function (ServerRequestInterface $request) use ($self) {
            if (! $self->hasBeenSent()) {
                // If the form is submitted by POST, handleRequest() won't access the URL, so we have to
                if (($sort = $request->getQueryParams()[$self->getSortParam()] ?? null)) {
                    $self->populate([$self->getSortParam() => $sort]);
                }
            }
        });

        return $self;
    }

    /**
     * Get the possible sort columns
     *
     * @return array Sort string-value pairs
     */
    public function getColumns(): array
    {
        return $this->columns;
    }

    /**
     * Set the possible sort columns
     *
     * @param array $columns Sort string-value pairs
     *
     * @return $this
     */
    public function setColumns(array $columns): self
    {
        // We're working with lowercase keys throughout the sort control
        $this->columns = array_change_key_case($columns, CASE_LOWER);

        return $this;
    }

    /**
     * Get the default sort string
     *
     * @return ?string
     */
    public function getDefault(): ?string
    {
        return $this->default;
    }

    /**
     * Set the default sort string
     *
     * @param string $default
     *
     * @return $this
     */
    public function setDefault(string $default): self
    {
        // We're working with lowercase keys throughout the sort control
        $this->default = strtolower($default);

        return $this;
    }

    /**
     * Get the name of the URL parameter which stores the sort
     *
     * @return string
     */
    public function getSortParam(): string
    {
        return $this->sortParam;
    }

    /**
     * Set the name of the URL parameter which stores the sort
     *
     * @param string $sortParam
     *
     * @return $this
     */
    public function setSortParam(string $sortParam): self
    {
        $this->sortParam = $sortParam;

        return $this;
    }

    /**
     * Get the sort string
     *
     * @return ?string
     */
    public function getSort(): ?string
    {
        if ($this->getRequest() === null) {
            $sort = $this->url->getParam($this->getSortParam(), $this->getDefault());
        } else {
            $sort = $this->getPopulatedValue($this->getSortParam(), $this->getDefault());
        }

        if (! empty($sort)) {
            $columns = $this->getColumns();

            if (! isset($columns[$sort])) {
                // Choose sort string based on the first closest match
                foreach (array_keys($columns) as $key) {
                    if (Str::startsWith($key, $sort)) {
                        $this->populate([$this->getSortParam() => $key]);
                        $sort = $key;

                        break;
                    }
                }
            }
        }

        return $sort;
    }

    /**
     * Sort the given query according to the request
     *
     * @param Query $query
     * @param ?array|string $defaultSort
     *
     * @return $this
     */
    public function apply(Query $query, $defaultSort = null): self
    {
        if ($this->getRequest() === null) {
            // handleRequest() has not been called yet
            // TODO: Remove this once everything using this requires ipl v0.12.0
            $this->handleRequest(ServerRequest::fromGlobals());
        }

        $default = $defaultSort ?? (array) $query->getModel()->getDefaultSort();
        if (! empty($default)) {
            $this->setDefault(SortUtil::normalizeSortSpec($default));
        }

        $sort = $this->getSort();
        if (! empty($sort)) {
            $query->orderBy(SortUtil::createOrderBy($sort));
        }

        return $this;
    }

    protected function assemble()
    {
        $columns = $this->getColumns();
        $sort = $this->getSort();

        if (empty($sort)) {
            reset($columns);
            $sort = key($columns);
        }

        $sort = explode(',', $sort, 2);
        list($column, $direction) = Str::symmetricSplit(array_shift($sort), ' ', 2);

        if (! $direction || strtolower($direction) === 'asc') {
            $toggleIcon = 'sort-alpha-down';
            $toggleDirection = 'desc';
        } else {
            $toggleIcon = 'sort-alpha-down-alt';
            $toggleDirection = 'asc';
        }

        if ($direction !== null) {
            $value = implode(',', array_merge(["{$column} {$direction}"], $sort));
            if (! isset($columns[$value])) {
                foreach ([$column, "{$column} {$toggleDirection}"] as $key) {
                    $key = implode(',', array_merge([$key], $sort));
                    if (isset($columns[$key])) {
                        $columns[$value] = $columns[$key];
                        unset($columns[$key]);

                        break;
                    }
                }
            }
        } else {
            $value = implode(',', array_merge([$column], $sort));
        }

        if (! isset($columns[$value])) {
            $columns[$value] = 'Custom';
        }

        $this->addElement('select', $this->getSortParam(), [
            'class'   => 'autosubmit',
            'label'   => 'Sort By',
            'options' => $columns,
            'value'   => $value
        ]);
        $select = $this->getElement($this->getSortParam());
        (new DivDecorator())->decorate($select);

        // Apply Icinga Web 2 style, for now
        $select->prependWrapper(HtmlElement::create('div', ['class' => 'icinga-controls']));

        $toggleButton = new ButtonElement($this->getSortParam(), [
            'class' => 'control-button spinner',
            'title' => t('Change sort direction'),
            'type'  => 'submit',
            'value' => implode(',', array_merge(["{$column} {$toggleDirection}"], $sort))
        ]);
        $toggleButton->add(new Icon($toggleIcon));

        $this->addHtml($toggleButton);

        if ($this->getMethod() === 'POST' && $this->hasAttribute('name')) {
            $this->addElement($this->createUidElement());
        }
    }
}