Skip to content

Commit

Permalink
Reference channels by their ID
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Oct 16, 2023
1 parent 723d360 commit 66ff4d8
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 38 deletions.
14 changes: 6 additions & 8 deletions application/forms/EscalationRecipientForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Notifications\Forms;

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Model\Channel;
use Icinga\Module\Notifications\Model\Contact;
use Icinga\Module\Notifications\Model\Contactgroup;
use Icinga\Module\Notifications\Model\Schedule;
Expand Down Expand Up @@ -69,11 +70,8 @@ protected function assembleElements(): void

$this->registerElement($col);

$options = [
'' => sprintf(' - %s - ', $this->translate('Please choose')),
'email' => 'E-Mail',
'rocketchat' => 'Rocket.Chat'
];
$options = ['' => sprintf(' - %s - ', $this->translate('Please choose'))];
$options += Channel::fetchChannelTypes(Database::get());

$val = $this->createElement(
'select',
Expand Down Expand Up @@ -134,7 +132,7 @@ public function getValues()
}

$value = [];
$value['channel_type'] = $this->getValue('value' . $count);
$value['channel_id'] = $this->getValue('value' . $count);
$value['id'] = $this->getValue('id' . $count);

$columnName = $this->getValue('column' . $count);
Expand Down Expand Up @@ -165,9 +163,9 @@ public function populate($values)

$count = $key + 1;
$selectedOption = str_replace('id', $elementValue, $elementName, $replaced);
if ($replaced) {
if ($replaced && $elementName !== 'channel_id') {
$values['column' . $count] = $selectedOption;
} elseif ($elementName === 'channel_type') {
} elseif ($elementName === 'channel_id') {
$values['value' . $count] = $elementValue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions application/forms/SaveEventRuleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function addRule(array $config): int
foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) {
$data = [
'rule_escalation_id' => $escalationId,
'channel_type' => $recipientConfig['channel_type']
'channel_id' => $recipientConfig['channel_id']
];

switch (true) {
Expand Down Expand Up @@ -343,7 +343,7 @@ function (array $element) use ($recipientId) {
foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) {
$data = [
'rule_escalation_id' => $escalationId,
'channel_type' => $recipientConfig['channel_type']
'channel_id' => $recipientConfig['channel_id']
];

switch (true) {
Expand Down
39 changes: 39 additions & 0 deletions library/Notifications/Model/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Icinga\Module\Notifications\Model;

use ipl\Orm\Model;
use ipl\Orm\Relations;
use ipl\Sql\Connection;

class Channel extends Model
{
Expand Down Expand Up @@ -45,4 +47,41 @@ public function getDefaultSort()
{
return ['name'];
}

public function createRelations(Relations $relations)
{
$relations->hasMany('incident_history', IncidentHistory::class)->setJoinType('LEFT');
$relations->hasMany('rule_escalation_recipient', RuleEscalationRecipient::class)->setJoinType('LEFT');
$relations->hasMany('contact', Contact::class)
->setJoinType('LEFT')
->setForeignKey('default_channel_id');
}

/**
* Fetch and map all the configured channel types to a key => value array
*
* @param Connection $conn
*
* @return array<int, string> All the channel types mapped as id => type
*/
public static function fetchChannelTypes(Connection $conn): array
{
$channels = [];
foreach (Channel::on($conn) as $channel) {
switch ($channel->type) {
case 'rocketchat':
$name = 'Rocket.Chat';
break;
case 'email':
$name = t('E-Mail');
break;
default:
$name = $channel->type;
}

$channels[$channel->id] = $name;
}

return $channels;
}
}
5 changes: 4 additions & 1 deletion library/Notifications/Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getColumns(): array
'full_name',
'username',
'color',
'default_channel'
'default_channel_id'
];
}

Expand Down Expand Up @@ -57,6 +57,9 @@ public function getDefaultSort()

public function createRelations(Relations $relations)
{
$relations->belongsTo('channel', Channel::class)
->setCandidateKey('default_channel_id');

$relations->belongsToMany('incident', Incident::class)
->through('incident_contact')
->setJoinType('LEFT');
Expand Down
5 changes: 3 additions & 2 deletions library/Notifications/Model/IncidentHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function getColumns()
'contact_id',
'schedule_id',
'contactgroup_id',
'channel_id',
'caused_by_incident_history_id',
'channel_type',
'new_severity',
'old_severity',
'new_recipient_role',
Expand All @@ -57,7 +57,7 @@ public function getColumnDefinitions()
'schedule_id' => t('Schedule Id'),
'contactgroup_id' => t('Contact Group Id'),
'caused_by_incident_history_id' => t('Caused By Incident History Id'),
'channel_type' => t('Channel Type'),
'channel_id' => t('Channel ID'),
'new_recipient_role' => t('New Recipient Role'),
'old_recipient_role' => t('Old Recipient Role'),
'message' => t('Message')
Expand All @@ -84,5 +84,6 @@ public function createRelations(Relations $relations)
$relations->belongsTo('schedule', Schedule::class)->setJoinType('LEFT');
$relations->belongsTo('rule', Rule::class)->setJoinType('LEFT');
$relations->belongsTo('rule_escalation', RuleEscalation::class)->setJoinType('LEFT');
$relations->belongsTo('channel', Channel::class)->setJoinType('LEFT');
}
}
18 changes: 7 additions & 11 deletions library/Notifications/Model/RuleEscalationRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,21 @@ public function getColumns()
'contact_id',
'contactgroup_id',
'schedule_id',
'channel_type'
'channel_id'
];
}

public function getColumnDefinitions()
{
return [
'rule_escalation_id' => t('Rule Escalation ID'),
'contact_id' => t('Contact ID'),
'contactgroup_id' => t('Contactgroup ID'),
'schedule_id' => t('Schedule ID'),
'channel_type' => t('Channel Type')
'rule_escalation_id' => t('Rule Escalation ID'),
'contact_id' => t('Contact ID'),
'contactgroup_id' => t('Contactgroup ID'),
'schedule_id' => t('Schedule ID'),
'channel_id' => t('Channel ID')
];
}

public function getSearchColumns()
{
return ['channel_type'];
}

public function getDefaultSort()
{
return ['rule_escalation_id'];
Expand All @@ -57,6 +52,7 @@ public function createRelations(Relations $relations)
$relations->belongsTo('contact', Contact::class);
$relations->belongsTo('schedule', Schedule::class);
$relations->belongsTo('contactgroup', Contactgroup::class);
$relations->belongsTo('channel', Channel::class);
}

/**
Expand Down
21 changes: 11 additions & 10 deletions library/Notifications/Web/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Icinga\Module\Notifications\Web\Form;

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Model\Channel;
use Icinga\Module\Notifications\Model\Contact;
use Icinga\Module\Notifications\Model\ContactAddress;
use Icinga\Web\Session;
Expand Down Expand Up @@ -77,6 +79,9 @@ protected function assemble()

$this->addElement($contact);

$channelOptions = ['' => sprintf(' - %s - ', $this->translate('Please choose'))];
$channelOptions += Channel::fetchChannelTypes(Database::get());

$contact->addElement(
'text',
'full_name',
Expand Down Expand Up @@ -113,16 +118,12 @@ protected function assemble()
]
)->addElement(
'select',
'default_channel',
'default_channel_id',
[
'label' => $this->translate('Default Channel'),
'required' => true,
'disable' => [''],
'options' => [
'' => sprintf(' - %s - ', $this->translate('Please choose')),
'email' => $this->translate('Email'),
'rocketchat' => 'Rocket.Chat'
]
'options' => $channelOptions
]
);

Expand Down Expand Up @@ -185,10 +186,10 @@ public function populate($values)
$formValues = [];
if (! isset($formValues['contact'])) {
$formValues['contact'] = [
'full_name' => $values->full_name,
'username' => $values->username,
'color' => $values->color,
'default_channel' => $values->default_channel
'full_name' => $values->full_name,
'username' => $values->username,
'color' => $values->color,
'default_channel_id' => $values->default_channel_id
];
}

Expand Down
3 changes: 2 additions & 1 deletion library/Notifications/Widget/Detail/IncidentDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ protected function createHistory()
'rule',
'rule_escalation',
'contactgroup',
'schedule'
'schedule',
'channel'
])
)
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,21 @@ protected function buildMessage(): string
$message = sprintf(
t('Contact %s notified via %s as member of contact group %s'),
$this->item->contact->full_name,
$this->item->channel_type,
$this->item->channel->type,
$this->item->contactgroup->name
);
} elseif ($this->item->schedule_id) {
$message = sprintf(
t('Contact %s notified via %s as member of schedule %s'),
$this->item->contact->full_name,
$this->item->channel_type,
$this->item->channel->type,
$this->item->schedule->name
);
} else {
$message = sprintf(
t('Contact %s notified via %s'),
$this->item->contact->full_name,
$this->item->channel_type
$this->item->channel->type
);
}
break;
Expand Down

0 comments on commit 66ff4d8

Please sign in to comment.