From 153e9b4ade74352265e9db48aeaa0eab64e266aa Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 19 Mar 2021 17:11:13 +0100 Subject: [PATCH] SingleValueSearchControl: Add support for groups and meta data --- .../Web/Widget/SingleValueSearchControl.php | 65 ++++++++++++++++--- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/library/Icinga/Web/Widget/SingleValueSearchControl.php b/library/Icinga/Web/Widget/SingleValueSearchControl.php index 6ab537726..8df26a9af 100644 --- a/library/Icinga/Web/Widget/SingleValueSearchControl.php +++ b/library/Icinga/Web/Widget/SingleValueSearchControl.php @@ -7,6 +7,7 @@ use Icinga\Application\Icinga; 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 @@ -28,6 +29,9 @@ class SingleValueSearchControl extends Form /** @var Url */ protected $suggestionUrl; + /** @var array */ + protected $metaDataNames; + /** * Set the search parameter to use * @@ -83,6 +87,20 @@ class SingleValueSearchControl extends Form 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'); @@ -101,6 +119,17 @@ class SingleValueSearchControl extends Form ] ); + if (! empty($this->metaDataNames)) { + $fieldset = new HtmlElement('fieldset'); + foreach ($this->metaDataNames as $name) { + $hiddenElement = $this->createElement('hidden', $this->searchParameter . '-' . $name); + $this->registerElement($hiddenElement); + $fieldset->add($hiddenElement); + } + + $this->getElement($this->searchParameter)->prependWrapper($fieldset); + } + $this->addElement( 'submit', 'btn_sumit', @@ -117,22 +146,40 @@ class SingleValueSearchControl extends Form } /** - * Create a list of search suggestions + * Create a list of search suggestions based on the given groups * - * @param array $values + * @param array $groups * * @return HtmlElement */ - public static function createSuggestions(array $values) + public static function createSuggestions(array $groups) { $ul = new HtmlElement('ul'); + foreach ($groups as $name => $entries) { + if (is_string($name)) { + if ($entries === false) { + $ul->add(new HtmlElement('li', ['class' => 'failure-message'], [ + new HtmlElement('em', null, t('Can\'t search:')), + $name + ])); + continue; + } else { + $ul->add(new HtmlElement('li', ['class' => Suggestions::SUGGESTION_TITLE_CLASS], $name)); + } + } - foreach ($values as $value) { - $ul->add(new HtmlElement('li', null, new InputElement(null, [ - 'value' => $value, - 'type' => 'button', - 'tabindex' => -1 - ]))); + foreach ($entries as $label => $metaData) { + $attributes = [ + 'value' => $label, + 'type' => 'button', + 'tabindex' => -1 + ]; + foreach ($metaData as $key => $value) { + $attributes['data-' . $key] = $value; + } + + $ul->add(new HtmlElement('li', null, new InputElement(null, $attributes))); + } } return $ul;