Skip to content

Commit c677b3c

Browse files
authored
Incremental config updates (#187)
requires Icinga/icinga-notifications#191
2 parents dd29ea3 + 013a57d commit c677b3c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1714
-702
lines changed

application/controllers/ChannelController.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,30 @@
66

77
use Icinga\Module\Notifications\Common\Database;
88
use Icinga\Module\Notifications\Forms\ChannelForm;
9-
use Icinga\Module\Notifications\Model\Channel;
109
use Icinga\Web\Notification;
11-
use ipl\Sql\Connection;
12-
use ipl\Stdlib\Filter;
1310
use ipl\Web\Compat\CompatController;
1411

1512
class ChannelController extends CompatController
1613
{
17-
/** @var Connection */
18-
private $db;
19-
20-
public function init()
14+
public function init(): void
2115
{
2216
$this->assertPermission('config/modules');
23-
24-
$this->db = Database::get();
2517
}
2618

27-
public function indexAction()
19+
public function indexAction(): void
2820
{
29-
$channel = Channel::on($this->db);
3021
$channelId = $this->params->getRequired('id');
31-
32-
$channel->filter(Filter::equal('id', $channelId));
33-
34-
$channel = $channel->first();
35-
36-
$this->addTitleTab(sprintf(t('Channel: %s'), $channel->name));
37-
38-
$form = (new ChannelForm($this->db, $channelId))
39-
->populate($channel)
22+
$form = (new ChannelForm(Database::get()))
23+
->loadChannel($channelId)
4024
->on(ChannelForm::ON_SUCCESS, function (ChannelForm $form) {
4125
if ($form->getPressedSubmitElement()->getName() === 'delete') {
26+
$form->removeChannel();
4227
Notification::success(sprintf(
4328
t('Deleted channel "%s" successfully'),
4429
$form->getValue('name')
4530
));
4631
} else {
32+
$form->editChannel();
4733
Notification::success(sprintf(
4834
t('Channel "%s" has successfully been saved'),
4935
$form->getValue('name')
@@ -53,6 +39,8 @@ public function indexAction()
5339
$this->redirectNow('__CLOSE__');
5440
})->handleRequest($this->getServerRequest());
5541

42+
$this->addTitleTab(sprintf(t('Channel: %s'), $form->getChannelName()));
43+
5644
$this->addContent($form);
5745
}
5846
}

application/controllers/ChannelsController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public function indexAction()
5252
$sortControl = $this->createSortControl(
5353
$channels,
5454
[
55-
'name' => t('Name'),
56-
'type' => t('Type')
55+
'name' => t('Name'),
56+
'type' => t('Type'),
57+
'changed_at' => t('Changed At')
5758
]
5859
);
5960

@@ -104,6 +105,7 @@ public function addAction()
104105
$this->addTitleTab(t('Add Channel'));
105106
$form = (new ChannelForm($this->db))
106107
->on(ChannelForm::ON_SUCCESS, function (ChannelForm $form) {
108+
$form->addChannel();
107109
Notification::success(
108110
sprintf(
109111
t('New channel %s has successfully been added'),

application/controllers/ContactController.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,37 @@
1515

1616
class ContactController extends CompatController
1717
{
18-
/** @var Connection */
19-
private $db;
20-
21-
public function init()
18+
public function init(): void
2219
{
2320
$this->assertPermission('notifications/config/contacts');
24-
25-
$this->db = Database::get();
2621
}
2722

28-
public function indexAction()
23+
public function indexAction(): void
2924
{
30-
$contact = Contact::on($this->db);
3125
$contactId = $this->params->getRequired('id');
3226

33-
$contact->filter(Filter::equal('id', $contactId));
34-
35-
$contact = $contact->first();
36-
37-
$this->addTitleTab(sprintf(t('Contact: %s'), $contact->full_name));
38-
39-
$form = (new ContactForm($this->db, $contactId))
40-
->populate($contact)
27+
$form = (new ContactForm(Database::get()))
28+
->loadContact($contactId)
4129
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
42-
$form->addOrUpdateContact();
43-
/** @var FieldsetElement $contactElement */
44-
$contactElement = $form->getElement('contact');
30+
$form->editContact();
4531
Notification::success(sprintf(
4632
t('Contact "%s" has successfully been saved'),
47-
$contactElement->getValue('full_name')
33+
$form->getContactName()
4834
));
4935

5036
$this->redirectNow('__CLOSE__');
5137
})->on(ContactForm::ON_REMOVE, function (ContactForm $form) {
5238
$form->removeContact();
53-
/** @var FieldsetElement $contactElement */
54-
$contactElement = $form->getElement('contact');
5539
Notification::success(sprintf(
5640
t('Deleted contact "%s" successfully'),
57-
$contactElement->getValue('full_name')
41+
$form->getContactName()
5842
));
5943

6044
$this->redirectNow('__CLOSE__');
6145
})->handleRequest($this->getServerRequest());
6246

47+
$this->addTitleTab(sprintf(t('Contact: %s'), $form->getContactName()));
48+
6349
$this->addContent($form);
6450
}
6551
}

application/controllers/ContactGroupController.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Icinga\Module\Notifications\Common\Database;
88
use Icinga\Module\Notifications\Common\Links;
99
use Icinga\Module\Notifications\Forms\ContactGroupForm;
10+
use Icinga\Module\Notifications\Model\Contact;
1011
use Icinga\Module\Notifications\Model\Contactgroup;
12+
use Icinga\Module\Notifications\Model\ContactgroupMember;
1113
use Icinga\Module\Notifications\Widget\ItemList\ContactList;
1214
use Icinga\Web\Notification;
1315
use ipl\Html\Attributes;
@@ -44,7 +46,13 @@ public function indexAction(): void
4446

4547
$this->addControl(new HtmlElement('div', new Attributes(['class' => 'header']), Text::create($group->name)));
4648

47-
$this->addControl($this->createPaginationControl($group->contact));
49+
$contacts = Contact::on(Database::get())
50+
->filter(Filter::all(
51+
Filter::equal('contactgroup_member.contactgroup_id', $groupId),
52+
Filter::equal('contactgroup_member.deleted', 'n')
53+
));
54+
55+
$this->addControl($this->createPaginationControl($contacts));
4856
$this->addControl($this->createLimitControl());
4957

5058
$this->addContent(
@@ -56,7 +64,7 @@ public function indexAction(): void
5664
))->openInModal()
5765
);
5866

59-
$this->addContent(new ContactList($group->contact));
67+
$this->addContent(new ContactList($contacts));
6068

6169
$this->addTitleTab(t('Contact Group'));
6270
$this->setTitle(sprintf(t('Contact Group: %s'), $group->name));
@@ -90,12 +98,11 @@ public function editAction(): void
9098
}
9199
})
92100
->on(Form::ON_SUCCESS, function (ContactGroupForm $form) use ($groupId) {
93-
if ($form->editGroup()) {
94-
Notification::success(sprintf(
95-
t('Successfully updated contact group %s'),
96-
$form->getValue('group_name')
97-
));
98-
}
101+
$form->editGroup();
102+
Notification::success(sprintf(
103+
t('Successfully updated contact group %s'),
104+
$form->getValue('group_name')
105+
));
99106

100107
$this->closeModalAndRefreshRemainingViews(Links::contactGroup($groupId));
101108
})

application/controllers/ContactGroupsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function indexAction(): void
4646
$sortControl = $this->createSortControl(
4747
$groups,
4848
[
49-
'name' => t('Group Name'),
49+
'name' => t('Group Name'),
50+
'changed_at' => t('Changed At')
5051
]
5152
);
5253

application/controllers/ContactsController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function indexAction()
5050
$sortControl = $this->createSortControl(
5151
$contacts,
5252
[
53-
'full_name' => t('Full Name'),
53+
'full_name' => t('Full Name'),
54+
'changed_at' => t('Changed At')
5455
]
5556
);
5657

@@ -101,15 +102,15 @@ public function indexAction()
101102
$this->getTabs()->activate('contacts');
102103
}
103104

104-
public function addAction()
105+
public function addAction(): void
105106
{
106107
$this->addTitleTab(t('Add Contact'));
107108

108109
$form = (new ContactForm($this->db))
109110
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
110-
$form->addOrUpdateContact();
111+
$form->addContact();
111112
Notification::success(t('New contact has successfully been added'));
112-
$this->redirectNow(Url::fromPath('notifications/contacts'));
113+
$this->redirectNow(Links::contacts());
113114
})->handleRequest($this->getServerRequest());
114115

115116
$this->addContent($form);

application/controllers/EventRuleController.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Icinga\Module\Notifications\Forms\EventRuleForm;
1111
use Icinga\Module\Notifications\Forms\SaveEventRuleForm;
1212
use Icinga\Module\Notifications\Model\Incident;
13-
use Icinga\Module\Notifications\Model\ObjectExtraTag;
1413
use Icinga\Module\Notifications\Model\Rule;
1514
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
1615
use Icinga\Module\Notifications\Widget\EventRuleConfig;
@@ -62,21 +61,9 @@ public function indexAction(): void
6261
);
6362
}
6463

65-
$disableRemoveButton = false;
66-
if (ctype_digit($ruleId)) {
67-
$incidents = Incident::on(Database::get())
68-
->with('rule')
69-
->filter(Filter::equal('rule.id', $ruleId));
70-
71-
if ($incidents->count() > 0) {
72-
$disableRemoveButton = true;
73-
}
74-
}
75-
7664
$saveForm = (new SaveEventRuleForm())
7765
->setShowRemoveButton()
7866
->setShowDismissChangesButton($cache !== null)
79-
->setRemoveButtonDisabled($disableRemoveButton)
8067
->setSubmitButtonDisabled($cache === null)
8168
->setSubmitLabel($this->translate('Save Changes'))
8269
->on(SaveEventRuleForm::ON_SUCCESS, function ($form) use ($ruleId, $eventRuleConfig) {
@@ -151,7 +138,7 @@ public function indexAction(): void
151138
public function fromDb(int $ruleId): array
152139
{
153140
$query = Rule::on(Database::get())
154-
->withoutColumns('timeperiod_id')
141+
->columns(['id', 'name', 'object_filter'])
155142
->filter(Filter::equal('id', $ruleId));
156143

157144
$rule = $query->first();
@@ -161,12 +148,20 @@ public function fromDb(int $ruleId): array
161148

162149
$config = iterator_to_array($rule);
163150

164-
foreach ($rule->rule_escalation as $re) {
151+
$ruleEscalations = $rule
152+
->rule_escalation
153+
->withoutColumns(['changed_at', 'deleted']);
154+
155+
foreach ($ruleEscalations as $re) {
165156
foreach ($re as $k => $v) {
166157
$config[$re->getTableName()][$re->position][$k] = $v;
167158
}
168159

169-
foreach ($re->rule_escalation_recipient as $recipient) {
160+
$escalationRecipients = $re
161+
->rule_escalation_recipient
162+
->withoutColumns(['changed_at', 'deleted']);
163+
164+
foreach ($escalationRecipients as $recipient) {
170165
$config[$re->getTableName()][$re->position]['recipient'][] = iterator_to_array($recipient);
171166
}
172167
}
@@ -248,7 +243,6 @@ public function editAction(): void
248243
->setAction(Url::fromRequest()->getAbsoluteUrl())
249244
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $cache, $config) {
250245
$config['name'] = $form->getValue('name');
251-
$config['is_active'] = $form->getValue('is_active');
252246

253247
if ($cache || $ruleId === '-1') {
254248
$this->sessionNamespace->set($ruleId, $config);

application/controllers/EventRulesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function indexAction(): void
5151
$sortControl = $this->createSortControl(
5252
$eventRules,
5353
[
54-
'name' => t('Name'),
54+
'name' => t('Name'),
55+
'changed_at' => t('Changed At')
5556
]
5657
);
5758

application/controllers/ScheduleController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function settingsAction(): void
6868
$this->setTitle($this->translate('Edit Schedule'));
6969
$scheduleId = (int) $this->params->getRequired('id');
7070

71-
$form = new ScheduleForm();
71+
$form = new ScheduleForm(Database::get());
7272
$form->setShowRemoveButton();
7373
$form->loadSchedule($scheduleId);
7474
$form->setSubmitLabel($this->translate('Save Changes'));
@@ -95,7 +95,7 @@ public function settingsAction(): void
9595
public function addAction(): void
9696
{
9797
$this->setTitle($this->translate('New Schedule'));
98-
$form = (new ScheduleForm())
98+
$form = (new ScheduleForm(Database::get()))
9999
->setAction($this->getRequest()->getUrl()->getAbsoluteUrl())
100100
->on(Form::ON_SUCCESS, function (ScheduleForm $form) {
101101
$scheduleId = $form->addSchedule();

application/controllers/SchedulesController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public function indexAction(): void
3232
$limitControl = $this->createLimitControl();
3333
$sortControl = $this->createSortControl(
3434
$schedules,
35-
['schedule.name' => t('Name')]
35+
[
36+
'schedule.name' => t('Name'),
37+
'changed_at' => t('Changed At')
38+
]
3639
);
3740

3841
$paginationControl = $this->createPaginationControl($schedules);

0 commit comments

Comments
 (0)