diff --git a/library/Notifications/Web/Form/ContactForm.php b/library/Notifications/Web/Form/ContactForm.php index f97b1dd75..75f95a2b3 100644 --- a/library/Notifications/Web/Form/ContactForm.php +++ b/library/Notifications/Web/Form/ContactForm.php @@ -8,6 +8,7 @@ use Icinga\Module\Notifications\Model\Channel; use Icinga\Module\Notifications\Model\Contact; use Icinga\Module\Notifications\Model\ContactAddress; +use Icinga\Module\Notifications\Model\Plugin; use Icinga\Web\Session; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\FormElement\FieldsetElement; @@ -120,37 +121,14 @@ protected function assemble() 'select', 'default_channel_id', [ - 'label' => $this->translate('Default Channel'), - 'required' => true, - 'disable' => [''], - 'options' => $channelOptions + 'label' => $this->translate('Default Channel'), + 'required' => true, + 'disabledOptions' => [''], + 'options' => $channelOptions ] ); - // Fieldset for addresses - $address = (new FieldsetElement( - 'contact_address', - [ - 'label' => $this->translate('Addresses'), - ] - )); - - $this->addElement($address); - - $address->addElement( - 'text', - 'email', - [ - 'label' => $this->translate('Email Address'), - 'validators' => [new EmailAddressValidator()] - ] - )->addElement( - 'text', - 'rocketchat', - [ - 'label' => $this->translate('Rocket.Chat Username'), - ] - ); + $this->addAddressElements(); $this->addElement( 'submit', @@ -303,4 +281,29 @@ private function insertOrUpdateAddress(string $type, array $addressFromForm, arr $this->db->delete('contact_address', ['id = ?' => $addressFromDb[$type][0]]); } } + + /** + * Add address elements for all existing channel plugins + * + * @return void + */ + private function addAddressElements(): void + { + $plugins = $this->db->fetchPairs(Plugin::on($this->db)->columns(['type', 'name'])->assembleSelect()); + if (empty($plugins)) { + return; + } + + $address = new FieldsetElement('contact_address', ['label' => $this->translate('Addresses')]); + $this->addElement($address); + + foreach ($plugins as $type => $label) { + $element = $this->createElement('text', $type, ['label' => $label]); + if ($type === 'email') { + $element->addAttributes(['validators' => [new EmailAddressValidator()]]); + } + + $address->addElement($element); + } + } }