ExtensibleSet: allow to hide elements, for choices

This commit is contained in:
Thomas Gelf 2017-07-05 07:03:20 +02:00
parent 04becc1582
commit ed6a77af5d
2 changed files with 36 additions and 6 deletions

View File

@ -12,7 +12,7 @@ use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\IcingaConfig\StateFilterSet; use Icinga\Module\Director\IcingaConfig\StateFilterSet;
use Icinga\Module\Director\IcingaConfig\TypeFilterSet; use Icinga\Module\Director\IcingaConfig\TypeFilterSet;
use Icinga\Module\Director\Objects\IcingaTemplateChoiceHost; use Icinga\Module\Director\Objects\IcingaTemplateChoice;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util; use Icinga\Module\Director\Util;
use Zend_Form_Element as ZfElement; use Zend_Form_Element as ZfElement;
@ -45,8 +45,6 @@ abstract class DirectorObjectForm extends QuickForm
private $choiceElements = []; private $choiceElements = [];
private $choiceTemplates = [];
protected $preferredObjectType; protected $preferredObjectType;
/** @var IcingaObjectFieldLoader */ /** @var IcingaObjectFieldLoader */
@ -1062,9 +1060,10 @@ abstract class DirectorObjectForm extends QuickForm
return $this; return $this;
} }
protected function addChoiceElement(IcingaTemplateChoiceHost $choice) protected function addChoiceElement(IcingaTemplateChoice $choice)
{ {
$element = $choice->createFormElement($this, $this->object()->imports); $imports = $this->object()->imports;
$element = $choice->createFormElement($this, $imports);
$this->addElement($element); $this->addElement($element);
$this->choiceElements[$element->getName()] = $element; $this->choiceElements[$element->getName()] = $element;
return $this; return $this;
@ -1094,7 +1093,20 @@ abstract class DirectorObjectForm extends QuickForm
return $this; return $this;
} }
$type = $this->object()->getShortTableName(); $db = $this->getDb()->getDbAdapter();
$object = $this->object;
if ($object->supportsChoices()) {
$choiceNames = $db->fetchCol(
$db->select()->from(
$this->object()->getTableName(),
'object_name'
)->where('template_choice_id IS NOT NULL')
);
} else {
$choiceNames = [];
}
$type = $object->getShortTableName();
$this->addElement('extensibleSet', 'imports', array( $this->addElement('extensibleSet', 'imports', array(
'label' => $this->translate('Imports'), 'label' => $this->translate('Imports'),
'description' => $this->translate( 'description' => $this->translate(
@ -1104,6 +1116,7 @@ abstract class DirectorObjectForm extends QuickForm
), ),
'required' => $required, 'required' => $required,
'spellcheck' => 'false', 'spellcheck' => 'false',
'hideOptions' => $choiceNames,
'suggest' => "${type}templates", 'suggest' => "${type}templates",
// 'multiOptions' => $this->optionallyAddFromEnum($enum), // 'multiOptions' => $this->optionallyAddFromEnum($enum),
'sorted' => true, 'sorted' => true,

View File

@ -40,6 +40,8 @@ class ExtensibleSetElement extends BaseElement
private $remainingAttribs; private $remainingAttribs;
private $hideOptions = [];
protected $defaultAttributes = [ protected $defaultAttributes = [
'class' => 'extensible-set' 'class' => 'extensible-set'
]; ];
@ -49,6 +51,12 @@ class ExtensibleSetElement extends BaseElement
$this->name = $this->id = $name; $this->name = $this->id = $name;
} }
public function hideOptions($options)
{
$this->hideOptions = array_merge($this->hideOptions, $options);
return $this;
}
private function setMultiOptions($options) private function setMultiOptions($options)
{ {
$this->multiOptions = $options; $this->multiOptions = $options;
@ -132,6 +140,11 @@ class ExtensibleSetElement extends BaseElement
unset($attribs['multiOptions']); unset($attribs['multiOptions']);
} }
if (array_key_exists('hideOptions', $attribs)) {
$this->hideOptions($attribs['hideOptions']);
unset($attribs['hideOptions']);
}
if (array_key_exists('sorted', $attribs)) { if (array_key_exists('sorted', $attribs)) {
$this->sorted = (bool) $attribs['sorted']; $this->sorted = (bool) $attribs['sorted'];
unset($attribs['sorted']); unset($attribs['sorted']);
@ -270,6 +283,10 @@ class ExtensibleSetElement extends BaseElement
$total = count($this->value); $total = count($this->value);
foreach ($this->value as $val) { foreach ($this->value as $val) {
if (in_array($val, $this->hideOptions)) {
continue;
}
if ($this->multiOptions !== null) { if ($this->multiOptions !== null) {
if ($this->isValidOption($val)) { if ($this->isValidOption($val)) {
$this->multiOptions = $this->removeOption( $this->multiOptions = $this->removeOption(