IcingaTemplateChoice: take over code generating...

...the choice form element
This commit is contained in:
Thomas Gelf 2017-06-23 00:17:02 +02:00
parent d7422fa55c
commit 0b89c222eb
2 changed files with 56 additions and 38 deletions

View File

@ -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) {

View File

@ -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;
}