IcingaTemplateChoiceForm: allow to require...

...a specific template/import

fixes #1178
This commit is contained in:
Thomas Gelf 2021-01-15 11:20:33 +01:00
parent 6687524d2f
commit 121dd774c4
2 changed files with 22 additions and 1 deletions

View File

@ -91,6 +91,15 @@ class IcingaTemplateChoiceForm extends DirectorObjectForm
'value' => 1, 'value' => 1,
)); ));
$this->addElement('select', 'required_template', [
'label' => $this->translate('Associated Template'),
'description' => $this->translate(
'Choose Choice Associated Template'
),
'required' => true,
'multiOptions' => $this->fetchUnboundTemplates(),
]);
$this->setButtons(); $this->setButtons();
} }

View File

@ -1180,9 +1180,21 @@ abstract class DirectorObjectForm extends DirectorForm
$connection = $this->getDb(); $connection = $this->getDb();
$choiceType = 'TemplateChoice' . ucfirst($type); $choiceType = 'TemplateChoice' . ucfirst($type);
$table = "icinga_$type";
$choices = IcingaObject::loadAllByType($choiceType, $connection); $choices = IcingaObject::loadAllByType($choiceType, $connection);
$chosenTemplates = $this->getSentOrObjectValue('imports');
$db = $connection->getDbAdapter();
$importedIds = $db->fetchCol(
$db->select()->from($table, 'id')
->where('object_name in (?)', (array)$chosenTemplates)
->where('object_type = ?', 'template')
);
foreach ($choices as $choice) { foreach ($choices as $choice) {
$this->addChoiceElement($choice); $required = $choice->get('required_template_id');
if ($required === null || in_array($required, $importedIds, false)) {
$this->addChoiceElement($choice);
}
} }
return $this; return $this;