From 0b89c222ebba7b99d8912cdbd8fe03c241df72bb Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 23 Jun 2017 00:17:02 +0200 Subject: [PATCH] IcingaTemplateChoice: take over code generating... ...the choice form element --- .../Director/Objects/IcingaTemplateChoice.php | 53 +++++++++++++++++++ .../Director/Web/Form/DirectorObjectForm.php | 41 ++------------ 2 files changed, 56 insertions(+), 38 deletions(-) diff --git a/library/Director/Objects/IcingaTemplateChoice.php b/library/Director/Objects/IcingaTemplateChoice.php index dba70383..08c1501a 100644 --- a/library/Director/Objects/IcingaTemplateChoice.php +++ b/library/Director/Objects/IcingaTemplateChoice.php @@ -2,6 +2,10 @@ namespace Icinga\Module\Director\Objects; +use Icinga\Module\Director\Web\Form\QuickForm; +use ipl\Translation\TranslationHelper; +use Zend_Form_Element as ZfElement; + class IcingaTemplateChoice extends IcingaObject { private $objectTable; @@ -23,6 +27,55 @@ class IcingaTemplateChoice extends IcingaObject return substr($this->table, 0, -16); } + public function createFormElement(QuickForm $form, $imports = [], $namePrefix = 'choice') + { + $db = $this->getDb(); + $query = $db->select()->from($this->getObjectTableName(), [ + 'value' => 'object_name', + 'label' => 'object_name' + ])->where('template_choice_id = ?', $this->get('id')); + + $required = $this->isRequired() && !$this->isTemplate(); + $type = $this->allowsMultipleChoices() ? 'multiselect' : 'select'; + + $choices = $db->fetchPairs($query); + + $chosen = []; + foreach ($imports as $import) { + if (array_key_exists($import, $choices)) { + $chosen[] = $import; + } + } + + $attributes = [ + 'label' => $this->getObjectName(), + 'description' => $this->get('description'), + 'required' => $required, + 'ignore' => true, + 'value' => $chosen, + 'multiOptions' => $form->optionalEnum($choices), + 'class' => 'autosubmit' + ]; + + // unused + if ($type === 'extensibleSet') { + $attributes['sorted'] = true; + } + + $key = $namePrefix . $this->get('id'); + return $form->createElement($type, $key, $attributes); + } + + public function isRequired() + { + return (int) $this->min_required > 0; + } + + public function allowsMultipleChoices() + { + return (int) $this->max_allowed > 1; + } + public function getChoices() { if ($this->choices === null) { diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index 3ba281e2..e1bb846e 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -1054,44 +1054,9 @@ abstract class DirectorObjectForm extends QuickForm protected function addChoiceElement(IcingaTemplateChoiceHost $choice) { - $db = $this->getDb()->getDbAdapter(); - $query = $db->select()->from($choice->getObjectTableName(), [ - 'value' => 'object_name', - 'label' => 'object_name' - ])->where('template_choice_id = ?', $choice->get('id')); - - $min = (int) $choice->get('min_required'); - $max = (int) $choice->get('max_allowed'); - - $required = $min > 0 && !$this->isTemplate(); - $type = $max === 1 ? 'select' : 'multiselect'; - - $choices = $db->fetchPairs($query); - $chosen = []; - foreach ($this->object()->imports as $import) { - if (array_key_exists($import, $choices)) { - $chosen[] = $import; - } - } - $key = 'choice' . $choice->get('id'); - $attributes = [ - 'label' => $choice->getObjectName(), - 'description' => $choice->get('description'), - 'required' => $required, - 'ignore' => true, - 'value' => $chosen, //$this->getSentValue($key), - 'multiOptions' => $this->optionalEnum($choices), - 'class' => 'autosubmit' - ]; - - // unused - if ($type === 'extensibleSet') { - $attributes['sorted'] = true; - } - - $this->addElement($type, $key, $attributes); - $this->choiceElements[$key] = $this->getElement($key); - + $element = $choice->createFormElement($this, $this->object()->imports); + $this->addElement($element); + $this->choiceElements[$element->getName()] = $element; return $this; }