|
| 1 | +<?php |
| 2 | +/* Icinga Web 2 | (c) 2022 Icinga Development Team | GPLv2+ */ |
| 3 | + |
| 4 | +namespace Icinga\Forms\Config\UserGroup; |
| 5 | + |
| 6 | +use ipl\Html\Attributes; |
| 7 | +use ipl\Html\Form; |
| 8 | +use ipl\Html\FormElement\InputElement; |
| 9 | +use ipl\Html\FormElement\SubmitElement; |
| 10 | +use ipl\Html\HtmlElement; |
| 11 | +use ipl\Web\Url; |
| 12 | + |
| 13 | +/** |
| 14 | + * Form for adding one or more group members |
| 15 | + */ |
| 16 | +abstract class SimpleSearchField extends Form |
| 17 | +{ |
| 18 | + protected $defaultAttributes = [ |
| 19 | + 'class' => 'icinga-form icinga-controls search-field', |
| 20 | + 'name' => 'search-field', |
| 21 | + 'role' => 'search-field' |
| 22 | + ]; |
| 23 | + |
| 24 | + /** @var string The term separator */ |
| 25 | + const TERM_SEPARATOR = ','; |
| 26 | + |
| 27 | + /** @var string The search parameter */ |
| 28 | + protected $searchParameter; |
| 29 | + |
| 30 | + /** @var Url The suggestion url */ |
| 31 | + protected $suggestionUrl; |
| 32 | + |
| 33 | + /** @var string Submit label */ |
| 34 | + protected $submitLabel; |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Set the search parameter to use |
| 40 | + * |
| 41 | + * @param string $name |
| 42 | + * |
| 43 | + * @return $this |
| 44 | + */ |
| 45 | + public function setSearchParameter($name) |
| 46 | + { |
| 47 | + $this->searchParameter = $name; |
| 48 | + |
| 49 | + return $this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the search parameter in use |
| 54 | + * |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + public function getSearchParameter() |
| 58 | + { |
| 59 | + return $this->searchParameter ?: 'q'; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Set the suggestion url |
| 64 | + * |
| 65 | + * @param Url $url |
| 66 | + * |
| 67 | + * @return $this |
| 68 | + */ |
| 69 | + public function setSuggestionUrl(Url $url) |
| 70 | + { |
| 71 | + $this->suggestionUrl = $url; |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the suggestion url |
| 78 | + * |
| 79 | + * @return Url |
| 80 | + */ |
| 81 | + public function getSuggestionUrl() |
| 82 | + { |
| 83 | + return $this->suggestionUrl; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Set the submit label |
| 88 | + * |
| 89 | + * @param string $label |
| 90 | + * |
| 91 | + * @return $this |
| 92 | + */ |
| 93 | + public function setSubmitLabel($label) |
| 94 | + { |
| 95 | + $this->submitLabel = $label; |
| 96 | + |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the submit label |
| 102 | + * |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + public function getSubmitLabel() |
| 106 | + { |
| 107 | + return $this->submitLabel ?? 'Submit'; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + public function assemble() |
| 112 | + { |
| 113 | + $filterInput = new InputElement(null, [ |
| 114 | + 'type' => 'text', |
| 115 | + 'placeholder' => 'Type to search', |
| 116 | + 'class' => 'search', |
| 117 | + 'id' => 'search-input', |
| 118 | + 'autocomplete' => 'off', |
| 119 | + 'required' => true, |
| 120 | + 'data-no-auto-submit' => true, |
| 121 | + 'data-enrichment-type' => 'terms', |
| 122 | + 'data-term-separator' => self::TERM_SEPARATOR, |
| 123 | + 'data-term-mode' => 'read-only', |
| 124 | + 'data-term-direction' => 'vertical', |
| 125 | + 'data-data-input' => '#data-input', |
| 126 | + 'data-term-input' => '#term-input', |
| 127 | + 'data-term-container' => '#term-container', |
| 128 | + 'data-term-suggestions' => '#term-suggestions', |
| 129 | + 'data-suggest-url' => $this->getSuggestionUrl() |
| 130 | + ]); |
| 131 | + |
| 132 | + $dataInput = new InputElement('data', ['type' => 'hidden', 'id' => 'data-input']); |
| 133 | + |
| 134 | + $termInput = new InputElement('q', ['type' => 'hidden', 'id' => 'term-input']); |
| 135 | + $this->registerElement($termInput); |
| 136 | + |
| 137 | + $termContainer = new HtmlElement( |
| 138 | + 'div', |
| 139 | + Attributes::create(['id' => 'term-container', 'class' => 'term-container']) |
| 140 | + ); |
| 141 | + |
| 142 | + $termSuggestions = new HtmlElement( |
| 143 | + 'div', |
| 144 | + Attributes::create(['id' => 'term-suggestions', 'class' => 'search-suggestions']) |
| 145 | + ); |
| 146 | + |
| 147 | + $submitButton = new SubmitElement( |
| 148 | + 'submit', |
| 149 | + ['label' => $this->getSubmitLabel(), 'class' => 'btn-primary'] |
| 150 | + ); |
| 151 | + |
| 152 | + $this->registerElement($submitButton); |
| 153 | + |
| 154 | + $this->add([ |
| 155 | + HtmlElement::create( |
| 156 | + 'div', |
| 157 | + ['class' => 'control-group'], |
| 158 | + [$filterInput, $termSuggestions, $dataInput, $termInput, $submitButton] |
| 159 | + ), |
| 160 | + $termContainer |
| 161 | + ]); |
| 162 | + } |
| 163 | +} |
0 commit comments