diff --git a/application/forms/IcingaTemplateChoiceForm.php b/application/forms/IcingaTemplateChoiceForm.php index 2359fc15..31fe6102 100644 --- a/application/forms/IcingaTemplateChoiceForm.php +++ b/application/forms/IcingaTemplateChoiceForm.php @@ -91,6 +91,15 @@ class IcingaTemplateChoiceForm extends DirectorObjectForm '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(); } diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index 23adb4e1..cfc99280 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -1180,9 +1180,21 @@ abstract class DirectorObjectForm extends DirectorForm $connection = $this->getDb(); $choiceType = 'TemplateChoice' . ucfirst($type); + $table = "icinga_$type"; $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) { - $this->addChoiceElement($choice); + $required = $choice->get('required_template_id'); + if ($required === null || in_array($required, $importedIds, false)) { + $this->addChoiceElement($choice); + } } return $this;