Skip to content

Commit b4e5b27

Browse files
committed
Remove event rule session storage
1 parent ce523f6 commit b4e5b27

File tree

7 files changed

+235
-239
lines changed

7 files changed

+235
-239
lines changed

application/controllers/EventRuleController.php

Lines changed: 110 additions & 100 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,99 +31,63 @@ 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
/** @var string $ruleId */
5446
$ruleId = $this->params->getRequired('id');
55-
/** @var array<string, mixed>|null $configValues */
56-
$configValues = $this->sessionNamespace->get($ruleId);
5747
$this->controls->addAttributes(['class' => 'event-rule-detail']);
5848

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

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

102-
/** @var array<string, mixed> $cache */
103-
$cache = $this->sessionNamespace->get($ruleId);
104-
$discardChangesButton = null;
105-
if ($cache !== null) {
106-
$this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.')));
107-
$discardChangesButton = (new SubmitButtonElement(
108-
'discard_changes',
109-
[
110-
'label' => t('Discard Changes'),
111-
'form' => 'event-rule-config-form',
112-
'class' => 'btn-discard-changes',
113-
'formnovalidate' => true,
114-
]
115-
));
116-
$disableSave = false;
117-
}
118-
119-
12085
$buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]));
12186
$eventRuleConfigSubmitButton = (new SubmitButtonElement(
12287
'save',
12388
[
12489
'label' => t('Save'),
12590
'form' => 'event-rule-config-form',
126-
'disabled' => $disableSave
12791
]
12892
));
12993
$deleteButton = (new SubmitButtonElement(
@@ -137,7 +101,7 @@ public function indexAction(): void
137101
));
138102

139103
$buttonsWrapper->add(
140-
[$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton]
104+
[$eventRuleConfigSubmitButton, $deleteButton]
141105
);
142106

143107
if ($ruleId > 0) {
@@ -150,8 +114,8 @@ public function indexAction(): void
150114
}
151115
}
152116

153-
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
154-
Html::tag('h2', $configValues['name'] ?? ''),
117+
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [
118+
Html::tag('h2', $eventRuleConfigValues['name'] ?? ''),
155119
(new Link(
156120
new Icon('edit'),
157121
Url::fromPath('notifications/event-rule/edit', [
@@ -166,6 +130,49 @@ public function indexAction(): void
166130
$this->addContent($eventRuleConfig);
167131
}
168132

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

249249
/** @var string $objectFilter */
250-
$objectFilter = $eventRule['object_filter'] ?? '';
250+
$objectFilter = $this->params->shift('object_filter', '');
251251
$editor->setQueryString($objectFilter);
252252
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
253253
$editor->setSuggestionUrl(
254254
Url::fromPath(
255255
"notifications/event-rule/complete",
256-
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
256+
['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId]
257257
)
258258
);
259259

260-
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
260+
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId) {
261261
$filter = self::createFilterString($form->getFilter());
262-
$eventRule['object_filter'] = $filter;
263-
$this->sessionNamespace->set($ruleId, $eventRule);
264-
$this->getResponse()
265-
->setHeader('X-Icinga-Container', '_self')
266-
->redirectAndExit(
267-
Url::fromPath(
268-
'notifications/event-rule',
269-
['id' => $ruleId]
262+
$this->sendExtraUpdates(
263+
[
264+
'#config-filter' => Url::fromPath(
265+
'notifications/event-rule/config-filter',
266+
[
267+
'id' => $ruleId,
268+
'object_filter' => $filter
269+
]
270270
)
271-
);
271+
]
272+
);
273+
$this->getResponse()
274+
->setHeader('X-Icinga-Container', 'dummy')
275+
->redirectAndExit('__CLOSE__');
272276
});
273277

274278
$editor->handleRequest($this->getServerRequest());
@@ -303,14 +307,11 @@ public function editAction(): void
303307
{
304308
/** @var string $ruleId */
305309
$ruleId = $this->params->getRequired('id');
306-
/** @var array<string, mixed>|null $config */
307-
$config = $this->sessionNamespace->get($ruleId);
308-
if ($config === null) {
309-
if ($ruleId === '-1') {
310-
$config = ['id' => $ruleId];
311-
} else {
312-
$config = $this->fromDb((int) $ruleId);
313-
}
310+
if ($ruleId === '-1') {
311+
$config = ['id' => $ruleId];
312+
} else {
313+
/** @var array<string, mixed> $config */
314+
$config = $this->fromDb((int) $ruleId);
314315
}
315316

316317
$eventRuleForm = (new EventRuleForm())
@@ -332,14 +333,23 @@ public function editAction(): void
332333

333334
if ($ruleId === '-1') {
334335
$redirectUrl = Url::fromPath('notifications/event-rules/add', $params);
336+
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
337+
$this->redirectNow($redirectUrl);
335338
} else {
336-
$redirectUrl = Url::fromPath('notifications/event-rule', $params);
337-
$this->sendExtraUpdates(['#col1']);
339+
Database::get()->update('rule', [
340+
'name' => $config['name'],
341+
'is_active' => $config['is_active'] ?? 'n'
342+
], ['id = ?' => $ruleId]);
343+
$this->sendExtraUpdates([
344+
'#event-rule-form' => Url::fromPath(
345+
'notifications/event-rule/rule',
346+
['id' => $ruleId]
347+
)->getAbsoluteUrl()
348+
]);
349+
350+
$this->getResponse()->setHeader('X-Icinga-Container', 'dummy')
351+
->redirectAndExit('__CLOSE__');
338352
}
339-
340-
$this->sessionNamespace->set($ruleId, $config);
341-
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
342-
$this->redirectNow($redirectUrl);
343353
})->handleRequest($this->getServerRequest());
344354

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

0 commit comments

Comments
 (0)