Skip to content

Commit

Permalink
ContactForm: Add address elements based on existing plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Oct 25, 2023
1 parent 1204a23 commit f44f842
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions library/Notifications/Web/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit f44f842

Please sign in to comment.