ExtensibleSetElement: show inherited values

fixes #1310
This commit is contained in:
Thomas Gelf 2020-04-21 11:24:32 +02:00
parent fd80344d79
commit cea6d4b456
2 changed files with 39 additions and 3 deletions

View File

@ -16,6 +16,7 @@ use Icinga\Module\Director\Objects\IcingaTemplateChoice;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Form\Element\ExtensibleSet;
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
use Zend_Form_Element as ZfElement;
use Zend_Form_Element_Select as ZfSelect;
@ -623,6 +624,9 @@ abstract class DirectorObjectForm extends DirectorForm
$multi[null] = $this->translate($this->translate('- inherited -'));
}
$el->setMultiOptions($multi);
} elseif ($el instanceof ExtensibleSet) {
$el->setAttrib('inherited', $inherited);
$el->setAttrib('inheritedFrom', $inheritedFrom);
} else {
if (is_string($inherited) || is_int($inherited)) {
$el->setAttrib('placeholder', $inherited . sprintf($txtInherited, $inheritedFrom));

View File

@ -42,6 +42,10 @@ class ExtensibleSetElement extends BaseHtmlElement
private $hideOptions = [];
private $inherited;
private $inheritedFrom;
protected $defaultAttributes = [
'class' => 'extensible-set'
];
@ -129,11 +133,18 @@ class ExtensibleSetElement extends BaseHtmlElement
$this->disable($attribs['disable']);
unset($attribs['disable']);
}
if (array_key_exists('value', $attribs)) {
$this->setValue($attribs['value']);
unset($attribs['value']);
}
if (array_key_exists('inherited', $attribs)) {
$this->inherited = $attribs['inherited'];
unset($attribs['inherited']);
}
if (array_key_exists('inheritedFrom', $attribs)) {
$this->inheritedFrom = $attribs['inheritedFrom'];
unset($attribs['inheritedFrom']);
}
if (array_key_exists('multiOptions', $attribs)) {
$this->setMultiOptions($attribs['multiOptions']);
@ -228,10 +239,13 @@ class ExtensibleSetElement extends BaseHtmlElement
return;
}
$field = Html::tag('select', ['class' => 'autosubmit']);
$more = $this->inherited === null
? $this->translate('- add more -')
: $this->getInheritedInfo();
$field->add(Html::tag('option', [
'value' => '',
'tabindex' => '-1'
], $this->translate('- add more -')));
], $more));
foreach ($this->multiOptions as $key => $label) {
if ($key === null) {
@ -253,7 +267,9 @@ class ExtensibleSetElement extends BaseHtmlElement
} else {
$field = Html::tag('input', [
'type' => 'text',
'placeholder' => $this->translate('Add a new one...'),
'placeholder' => $this->inherited === null
? $this->translate('Add a new one...')
: $this->getInheritedInfo(),
]);
}
$field->addAttributes([
@ -284,6 +300,22 @@ class ExtensibleSetElement extends BaseHtmlElement
}
}
private function getInheritedInfo()
{
if ($this->inheritedFrom === null) {
return \sprintf(
$this->translate('%s (inherited)'),
\implode(', ', $this->inherited)
);
} else {
return \sprintf(
$this->translate('%s (inherited from %s)'),
\implode(', ', $this->inherited),
$this->inheritedFrom
);
}
}
private function createAddNewButton()
{
return $this->newInlineButtons(