summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/SingleValueSearchControl.php
blob: 470518caa1831c45b8bfa386e237351ca302c9fc (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
<?php
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */

namespace Icinga\Web\Widget;

use Icinga\Application\Icinga;
use ipl\Html\Attributes;
use ipl\Html\Form;
use ipl\Html\FormElement\InputElement;
use ipl\Html\HtmlElement;
use ipl\Web\Control\SearchBar\Suggestions;
use ipl\Web\Url;

class SingleValueSearchControl extends Form
{
    /** @var string */
    const DEFAULT_SEARCH_PARAMETER = 'q';

    protected $defaultAttributes = ['class' => 'icinga-controls inline'];

    /** @var string */
    protected $searchParameter = self::DEFAULT_SEARCH_PARAMETER;

    /** @var string */
    protected $inputLabel;

    /** @var string */
    protected $submitLabel;

    /** @var Url */
    protected $suggestionUrl;

    /** @var array */
    protected $metaDataNames;

    /**
     * Set the search parameter to use
     *
     * @param   string $name
     * @return  $this
     */
    public function setSearchParameter($name)
    {
        $this->searchParameter = $name;

        return $this;
    }

    /**
     * Set the input's label
     *
     * @param string $label
     *
     * @return $this
     */
    public function setInputLabel($label)
    {
        $this->inputLabel = $label;

        return $this;
    }

    /**
     * Set the submit button's label
     *
     * @param string $label
     *
     * @return $this
     */
    public function setSubmitLabel($label)
    {
        $this->submitLabel = $label;

        return $this;
    }

    /**
     * Set the suggestion url
     *
     * @param   Url $url
     *
     * @return  $this
     */
    public function setSuggestionUrl(Url $url)
    {
        $this->suggestionUrl = $url;

        return $this;
    }

    /**
     * Set names for which hidden meta data elements should be created
     *
     * @param string ...$names
     *
     * @return $this
     */
    public function setMetaDataNames(...$names)
    {
        $this->metaDataNames = $names;

        return $this;
    }

    protected function assemble()
    {
        $suggestionsId = Icinga::app()->getRequest()->protectId('single-value-suggestions');

        $this->addElement(
            'text',
            $this->searchParameter,
            [
                'required'              => true,
                'minlength'             => 1,
                'autocomplete'          => 'off',
                'class'                 => 'search',
                'data-enrichment-type'  => 'completion',
                'data-term-suggestions' => '#' . $suggestionsId,
                'data-suggest-url'      => $this->suggestionUrl,
                'placeholder'           => $this->inputLabel
            ]
        );

        if (! empty($this->metaDataNames)) {
            $fieldset = new HtmlElement('fieldset');
            foreach ($this->metaDataNames as $name) {
                $hiddenElement = $this->createElement('hidden', $this->searchParameter . '-' . $name);
                $this->registerElement($hiddenElement);
                $fieldset->addHtml($hiddenElement);
            }

            $this->getElement($this->searchParameter)->prependWrapper($fieldset);
        }

        $this->addElement(
            'submit',
            'btn_sumit',
            [
                'label' => $this->submitLabel,
                'class' => 'btn-primary'
            ]
        );

        $this->add(HtmlElement::create('div', [
            'id'    => $suggestionsId,
            'class' => 'search-suggestions'
        ]));
    }

    /**
     * Create a list of search suggestions based on the given groups
     *
     * @param array $groups
     *
     * @return HtmlElement
     */
    public static function createSuggestions(array $groups)
    {
        $ul = new HtmlElement('ul');
        foreach ($groups as list($name, $entries)) {
            if ($name) {
                if ($entries === false) {
                    $ul->addHtml(HtmlElement::create('li', ['class' => 'failure-message'], [
                        HtmlElement::create('em', null, t('Can\'t search:')),
                        $name
                    ]));
                    continue;
                } elseif (empty($entries)) {
                    $ul->addHtml(HtmlElement::create('li', ['class' => 'failure-message'], [
                        HtmlElement::create('em', null, t('No results:')),
                        $name
                    ]));
                    continue;
                } else {
                    $ul->addHtml(
                        HtmlElement::create('li', ['class' => Suggestions::SUGGESTION_TITLE_CLASS], $name)
                    );
                }
            }

            $index = 0;
            foreach ($entries as list($label, $metaData)) {
                $attributes = [
                    'value'     => $label,
                    'type'      => 'button',
                    'tabindex'  => -1
                ];
                foreach ($metaData as $key => $value) {
                    $attributes['data-' . $key] = $value;
                }

                $liAtrs = ['class' => $index === 0 ? 'default' : null];
                $ul->addHtml(new HtmlElement('li', Attributes::create($liAtrs), new InputElement(null, $attributes)));
                $index++;
            }
        }

        return $ul;
    }
}