SingleValueSearchControl: Add support for groups and meta data

This commit is contained in:
Johannes Meyer 2021-03-19 17:11:13 +01:00
parent 05fdd98ba8
commit 153e9b4ade

View File

@ -7,6 +7,7 @@ use Icinga\Application\Icinga;
use ipl\Html\Form; use ipl\Html\Form;
use ipl\Html\FormElement\InputElement; use ipl\Html\FormElement\InputElement;
use ipl\Html\HtmlElement; use ipl\Html\HtmlElement;
use ipl\Web\Control\SearchBar\Suggestions;
use ipl\Web\Url; use ipl\Web\Url;
class SingleValueSearchControl extends Form class SingleValueSearchControl extends Form
@ -28,6 +29,9 @@ class SingleValueSearchControl extends Form
/** @var Url */ /** @var Url */
protected $suggestionUrl; protected $suggestionUrl;
/** @var array */
protected $metaDataNames;
/** /**
* Set the search parameter to use * Set the search parameter to use
* *
@ -83,6 +87,20 @@ class SingleValueSearchControl extends Form
return $this; 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() protected function assemble()
{ {
$suggestionsId = Icinga::app()->getRequest()->protectId('single-value-suggestions'); $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( $this->addElement(
'submit', 'submit',
'btn_sumit', '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 * @return HtmlElement
*/ */
public static function createSuggestions(array $values) public static function createSuggestions(array $groups)
{ {
$ul = new HtmlElement('ul'); $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) { foreach ($entries as $label => $metaData) {
$ul->add(new HtmlElement('li', null, new InputElement(null, [ $attributes = [
'value' => $value, 'value' => $label,
'type' => 'button', 'type' => 'button',
'tabindex' => -1 'tabindex' => -1
]))); ];
foreach ($metaData as $key => $value) {
$attributes['data-' . $key] = $value;
}
$ul->add(new HtmlElement('li', null, new InputElement(null, $attributes)));
}
} }
return $ul; return $ul;