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 Nov 20, 2023
1 parent c663164 commit c277075
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions library/Notifications/Web/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Notifications\Web\Form;

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Model\AvailableChannelType;
use Icinga\Module\Notifications\Model\Channel;
use Icinga\Module\Notifications\Model\Contact;
use Icinga\Module\Notifications\Model\ContactAddress;
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 @@ -227,11 +205,7 @@ public function addOrUpdateContact()

$this->db->beginTransaction();

$addressFromDb = [
'email' => null,
'rocketchat' => null
];

$addressFromDb = [];
if ($this->contactId === null) {
$this->db->insert('contact', $contact);

Expand All @@ -244,17 +218,12 @@ public function addOrUpdateContact()
$addressObjects->filter(Filter::equal('contact_id', $this->contactId));

foreach ($addressObjects as $addressRow) {
if ($addressRow->type === 'email') {
$addressFromDb['email'] = [$addressRow->id, $addressRow->address];
}

if ($addressRow->type === 'rocketchat') {
$addressFromDb['rocketchat'] = [$addressRow->id, $addressRow->address];
}
$addressFromDb[$addressRow->type] = [$addressRow->id, $addressRow->address];
}
}

foreach ($addressFromDb as $type => $value) {
$addr = ! empty($addressFromDb) ? $addressFromDb : $addressFromForm;
foreach ($addr as $type => $value) {
$this->insertOrUpdateAddress($type, $addressFromForm, $addressFromDb);
}

Expand All @@ -281,7 +250,7 @@ public function removeContact()
private function insertOrUpdateAddress(string $type, array $addressFromForm, array $addressFromDb): void
{
if ($addressFromForm[$type] !== null) {
if ($addressFromDb[$type] === null) {
if (! isset($addressFromDb[$type])) {
$address = [
'contact_id' => $this->contactId,
'type' => $type,
Expand All @@ -299,8 +268,38 @@ private function insertOrUpdateAddress(string $type, array $addressFromForm, arr
]
);
}
} elseif ($addressFromDb[$type] !== null) {
} elseif (isset($addressFromDb[$type])) {
$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(
AvailableChannelType::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 c277075

Please sign in to comment.