Skip to content

Commit f4f539b

Browse files
committed
Remove event rule session storage
1 parent d35dd96 commit f4f539b

File tree

7 files changed

+235
-229
lines changed

7 files changed

+235
-229
lines changed

application/controllers/EventRuleController.php

Lines changed: 111 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use Icinga\Module\Notifications\Common\Auth;
88
use Icinga\Module\Notifications\Common\Database;
99
use Icinga\Module\Notifications\Common\Links;
10+
use Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter;
1011
use Icinga\Module\Notifications\Forms\EventRuleConfigForm;
1112
use Icinga\Module\Notifications\Forms\EventRuleForm;
1213
use Icinga\Module\Notifications\Model\Incident;
1314
use Icinga\Module\Notifications\Model\Rule;
1415
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
1516
use Icinga\Web\Notification;
16-
use Icinga\Web\Session;
1717
use ipl\Html\Attributes;
1818
use ipl\Html\Form;
1919
use ipl\Html\FormElement\SubmitButtonElement;
@@ -31,95 +31,62 @@ class EventRuleController extends CompatController
3131
{
3232
use Auth;
3333

34-
/** @var Session\SessionNamespace */
35-
private $sessionNamespace;
36-
3734
/** @var ?string Event rule config filter */
3835
protected $filter;
3936

40-
public function init()
41-
{
42-
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
43-
}
44-
4537
public function indexAction(): void
4638
{
4739
$this->assertPermission('notifications/config/event-rule');
48-
$this->sessionNamespace->delete('-1');
40+
$this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy']));
4941

5042
$this->addTitleTab(t('Event Rule'));
5143
$this->controls->addAttributes(['class' => 'event-rule-detail']);
5244

5345
$ruleId = $this->params->getRequired('id');
54-
$configValues = $this->sessionNamespace->get($ruleId);
5546
$this->controls->addAttributes(['class' => 'event-rule-detail']);
5647

57-
$disableSave = false;
58-
if ($configValues === null) {
59-
$configValues = $this->fromDb((int) $ruleId);
60-
$disableSave = true;
48+
$eventRuleConfigValues = $this->fromDb((int) $ruleId);
49+
50+
if ($this->getRequest()->isPost()) {
51+
if ($this->getRequest()->has('searchbar')) {
52+
$eventRuleConfigValues['object_filter'] = $this->getRequest()->get('searchbar');
53+
} else {
54+
$eventRuleConfigValues['object_filter'] = '';
55+
}
6156
}
6257

6358
$eventRuleConfig = (new EventRuleConfigForm(
64-
$configValues,
6559
Url::fromPath(
6660
'notifications/event-rule/search-editor',
67-
['id' => $ruleId]
61+
['id' => $ruleId, 'object_filter' => $eventRuleConfigValues['object_filter']]
6862
)
69-
))->populate($configValues);
70-
$eventRuleConfig
71-
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
72-
$form->addOrUpdateRule($ruleId, $configValues);
73-
$this->sessionNamespace->delete($ruleId);
74-
Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name'])));
63+
))
64+
->populate($eventRuleConfigValues)
65+
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
66+
$config = $form->getValues();
67+
$config['object_filter'] = $eventRuleConfigValues['object_filter'];
68+
$form->addOrUpdateRule($ruleId, $config);
69+
Notification::success((sprintf(t('Successfully saved event rule %s'), $eventRuleConfigValues['name'])));
7570
$this->redirectNow(Links::eventRule((int) $ruleId));
7671
})
77-
->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
78-
$form->removeRule((int) $ruleId);
79-
$this->sessionNamespace->delete($ruleId);
80-
Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name']));
81-
$this->redirectNow('__CLOSE__');
82-
})
83-
->on(EventRuleConfigForm::ON_DISCARD, function () use ($ruleId, $configValues) {
84-
$this->sessionNamespace->delete($ruleId);
85-
Notification::success(
86-
sprintf(
87-
t('Successfully discarded changes to event rule %s'),
88-
$configValues['name']
89-
)
90-
);
91-
$this->redirectNow(Links::eventRule((int) $ruleId));
92-
})
93-
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
94-
$configValues = array_merge($configValues, $form->getValues());
95-
$configValues['rule_escalation'] = $form->getValues()['rule_escalation'];
96-
$this->sessionNamespace->set($ruleId, $configValues);
97-
})
72+
->on(
73+
EventRuleConfigForm::ON_DELETE,
74+
function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
75+
$form->removeRule((int) $ruleId);
76+
Notification::success(
77+
sprintf(t('Successfully deleted event rule %s'), $eventRuleConfigValues['name'])
78+
);
79+
$this->redirectNow('__CLOSE__');
80+
}
81+
)
9882
->handleRequest($this->getServerRequest());
9983

100-
$cache = $this->sessionNamespace->get($ruleId);
101-
$discardChangesButton = null;
102-
if ($cache !== null) {
103-
$this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.')));
104-
$discardChangesButton = (new SubmitButtonElement(
105-
'discard_changes',
106-
[
107-
'label' => t('Discard Changes'),
108-
'form' => 'event-rule-config-form',
109-
'class' => 'btn-discard-changes',
110-
'formnovalidate' => true,
111-
]
112-
));
113-
$disableSave = false;
114-
}
115-
11684
$buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]));
11785
$eventRuleConfigSubmitButton = (new SubmitButtonElement(
11886
'save',
11987
[
12088
'label' => t('Save'),
12189
'form' => 'event-rule-config-form',
122-
'disabled' => $disableSave
12390
]
12491
));
12592
$deleteButton = (new SubmitButtonElement(
@@ -133,7 +100,7 @@ public function indexAction(): void
133100
));
134101

135102
$buttonsWrapper->add(
136-
[$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton]
103+
[$eventRuleConfigSubmitButton, $deleteButton]
137104
);
138105

139106
if ($ruleId > 0) {
@@ -146,8 +113,8 @@ public function indexAction(): void
146113
}
147114
}
148115

149-
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
150-
Html::tag('h2', $configValues['name'] ?? ''),
116+
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [
117+
Html::tag('h2', $eventRuleConfigValues['name'] ?? ''),
151118
(new Link(
152119
new Icon('edit'),
153120
Url::fromPath('notifications/event-rule/edit', [
@@ -162,6 +129,49 @@ public function indexAction(): void
162129
$this->addContent($eventRuleConfig);
163130
}
164131

132+
public function ruleAction(): void
133+
{
134+
/** @var int $ruleId */
135+
$ruleId = $this->params->getRequired('id');
136+
$query = Rule::on(Database::get())
137+
->withoutColumns('timeperiod_id')
138+
->filter(Filter::equal('id', $ruleId));
139+
140+
$rule = $query->first();
141+
142+
$this->getDocument()->add([
143+
Html::tag('h2', $rule->name ?? ''),
144+
(new Link(
145+
new Icon('edit'),
146+
Url::fromPath('notifications/event-rule/edit', [
147+
'id' => $ruleId
148+
]),
149+
['class' => 'control-button']
150+
))->openInModal()
151+
]);
152+
}
153+
154+
public function configFilterAction(): void
155+
{
156+
$ruleId = $this->params->getRequired('id');
157+
/** @var string $objectFilter */
158+
$objectFilter = $this->params->get('object_filter', '');
159+
$eventRuleFilterFieldset = (new EventRuleConfigFilter('config-filter'))
160+
->setObjectFilter($objectFilter)
161+
->setSearchEditorUrl(
162+
Url::fromPath(
163+
'notifications/event-rule/search-editor',
164+
['id' => $ruleId, 'object_filter' => $objectFilter]
165+
)
166+
);
167+
168+
if ($objectFilter === '') {
169+
$eventRuleFilterFieldset->getAttributes()->add('class', 'empty-filter');
170+
}
171+
172+
$this->getDocument()->add($eventRuleFilterFieldset);
173+
}
174+
165175
/**
166176
* Create config from db
167177
*
@@ -232,37 +242,35 @@ public function searchEditorAction(): void
232242
{
233243
/** @var string $ruleId */
234244
$ruleId = $this->params->shiftRequired('id');
235-
236-
$eventRule = $this->sessionNamespace->get($ruleId);
237-
238-
if ($eventRule === null) {
239-
$eventRule = $this->fromDb((int) $ruleId);
240-
}
241-
242245
$editor = new SearchEditor();
243246

244-
$objectFilter = $eventRule['object_filter'] ?? '';
247+
/** @var string $objectFilter */
248+
$objectFilter = $this->params->shift('object_filter', '');
245249
$editor->setQueryString($objectFilter);
246250
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
247251
$editor->setSuggestionUrl(
248252
Url::fromPath(
249253
"notifications/event-rule/complete",
250-
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
254+
['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId]
251255
)
252256
);
253257

254-
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
258+
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId) {
255259
$filter = self::createFilterString($form->getFilter());
256-
$eventRule['object_filter'] = $filter;
257-
$this->sessionNamespace->set($ruleId, $eventRule);
258-
$this->getResponse()
259-
->setHeader('X-Icinga-Container', '_self')
260-
->redirectAndExit(
261-
Url::fromPath(
262-
'notifications/event-rule',
263-
['id' => $ruleId]
260+
$this->sendExtraUpdates(
261+
[
262+
'#config-filter' => Url::fromPath(
263+
'notifications/event-rule/config-filter',
264+
[
265+
'id' => $ruleId,
266+
'object_filter' => $filter
267+
]
264268
)
265-
);
269+
]
270+
);
271+
$this->getResponse()
272+
->setHeader('X-Icinga-Container', 'dummy')
273+
->redirectAndExit('__CLOSE__');
266274
});
267275

268276
$editor->handleRequest($this->getServerRequest());
@@ -297,13 +305,11 @@ public function editAction(): void
297305
{
298306
/** @var string $ruleId */
299307
$ruleId = $this->params->getRequired('id');
300-
$config = $this->sessionNamespace->get($ruleId);
301-
if ($config === null) {
302-
if ($ruleId === '-1') {
303-
$config = ['id' => $ruleId];
304-
} else {
305-
$config = $this->fromDb((int) $ruleId);
306-
}
308+
if ($ruleId === '-1') {
309+
$config = ['id' => $ruleId];
310+
} else {
311+
/** @var array<string, mixed> $config */
312+
$config = $this->fromDb((int) $ruleId);
307313
}
308314

309315
$eventRuleForm = (new EventRuleForm())
@@ -325,14 +331,23 @@ public function editAction(): void
325331

326332
if ($ruleId === '-1') {
327333
$redirectUrl = Url::fromPath('notifications/event-rules/add', $params);
334+
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
335+
$this->redirectNow($redirectUrl);
328336
} else {
329-
$redirectUrl = Url::fromPath('notifications/event-rule', $params);
330-
$this->sendExtraUpdates(['#col1']);
337+
Database::get()->update('rule', [
338+
'name' => $config['name'],
339+
'is_active' => $config['is_active'] ?? 'n'
340+
], ['id = ?' => $ruleId]);
341+
$this->sendExtraUpdates([
342+
'#event-rule-form' => Url::fromPath(
343+
'notifications/event-rule/rule',
344+
['id' => $ruleId]
345+
)->getAbsoluteUrl()
346+
]);
347+
348+
$this->getResponse()->setHeader('X-Icinga-Container', 'dummy')
349+
->redirectAndExit('__CLOSE__');
331350
}
332-
333-
$this->sessionNamespace->set($ruleId, $config);
334-
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
335-
$this->redirectNow($redirectUrl);
336351
})->handleRequest($this->getServerRequest());
337352

338353
if ($ruleId === '-1') {

0 commit comments

Comments
 (0)