Skip to content

Commit b2a08e8

Browse files
committed
event-rules: Enhance navigation flow
1 parent 6468e59 commit b2a08e8

File tree

4 files changed

+67
-106
lines changed

4 files changed

+67
-106
lines changed

application/controllers/EventRuleController.php

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ public function init()
4242

4343
public function indexAction(): void
4444
{
45-
$this->sessionNamespace->delete('-1');
46-
4745
$this->addTitleTab(t('Event Rule'));
4846
$this->controls->addAttributes(['class' => 'event-rule-detail']);
4947

50-
$ruleId = $this->params->getRequired('id');
48+
$ruleId = (int) $this->params->getRequired('id');
5149
$configValues = $this->sessionNamespace->get($ruleId);
5250
$this->controls->addAttributes(['class' => 'event-rule-detail']);
5351

5452
$disableSave = false;
5553
if ($configValues === null) {
56-
$configValues = $this->fromDb((int) $ruleId);
54+
$configValues = $this->fromDb($ruleId);
5755
$disableSave = true;
5856
}
5957

@@ -65,14 +63,15 @@ public function indexAction(): void
6563
$eventRuleConfig
6664
->populate($configValues)
6765
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
68-
$form->addOrUpdateRule((int) $ruleId, $configValues);
66+
$insertId = $form->addOrUpdateRule($ruleId, $configValues);
6967
$this->sessionNamespace->delete($ruleId);
7068
Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name'])));
71-
$this->redirectNow(Links::eventRule((int) $ruleId));
69+
$this->sendExtraUpdates(['#col1']);
70+
$this->redirectNow(Links::eventRule($insertId));
7271
})
7372
->on(EventRuleConfigForm::ON_SENT, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
7473
if ($form->hasBeenRemoved()) {
75-
$form->removeRule((int) $ruleId);
74+
$form->removeRule($ruleId);
7675
$this->sessionNamespace->delete($ruleId);
7776
Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name']));
7877
$this->redirectNow(Links::eventRules());
@@ -84,7 +83,12 @@ public function indexAction(): void
8483
$configValues['name']
8584
)
8685
);
87-
$this->redirectNow(Links::eventRule((int) $ruleId));
86+
87+
if ($ruleId < 0) {
88+
$this->switchToSingleColumnLayout();
89+
} else {
90+
$this->redirectNow(Links::eventRule($ruleId));
91+
}
8892
}
8993
})
9094
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
@@ -122,15 +126,18 @@ public function indexAction(): void
122126
]
123127
);
124128

125-
$deleteButton = new SubmitButtonElement(
126-
'delete',
127-
[
128-
'label' => t('Delete'),
129-
'form' => 'event-rule-config-form',
130-
'class' => 'btn-remove',
131-
'formnovalidate' => true
132-
]
133-
);
129+
$deleteButton = null;
130+
if ($ruleId !== -1) {
131+
$deleteButton = new SubmitButtonElement(
132+
'delete',
133+
[
134+
'label' => t('Delete'),
135+
'form' => 'event-rule-config-form',
136+
'class' => 'btn-remove',
137+
'formnovalidate' => true
138+
]
139+
);
140+
}
134141

135142
$buttonsWrapper->add([$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton]);
136143

@@ -290,14 +297,13 @@ public static function createFilterString(Filter\Rule $filters): ?string
290297

291298
public function editAction(): void
292299
{
293-
/** @var string $ruleId */
294-
$ruleId = $this->params->getRequired('id');
300+
$ruleId = (int) $this->params->getRequired('id');
295301
$config = $this->sessionNamespace->get($ruleId);
296302
if ($config === null) {
297-
if ($ruleId === '-1') {
303+
if ($ruleId === -1) {
298304
$config = ['id' => $ruleId];
299305
} else {
300-
$config = $this->fromDb((int) $ruleId);
306+
$config = $this->fromDb($ruleId);
301307
}
302308
}
303309

@@ -306,23 +312,11 @@ public function editAction(): void
306312
->setAction(Url::fromRequest()->getAbsoluteUrl())
307313
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $config) {
308314
$config['name'] = $form->getValue('name');
309-
if ($ruleId === '-1') {
310-
$redirectUrl = Url::fromPath('notifications/event-rules/add', ['id' => '-1']);
311-
} else {
312-
$redirectUrl = Url::fromPath('notifications/event-rule', ['id' => $ruleId]);
313-
$this->sendExtraUpdates(['#col1']);
314-
}
315-
316315
$this->sessionNamespace->set($ruleId, $config);
317-
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
318-
$this->redirectNow($redirectUrl);
316+
$this->closeModalAndRefreshRemainingViews(Links::eventRule($ruleId));
319317
})->handleRequest($this->getServerRequest());
320318

321-
if ($ruleId === '-1') {
322-
$this->setTitle($this->translate('New Event Rule'));
323-
} else {
324-
$this->setTitle($this->translate('Edit Event Rule'));
325-
}
319+
$this->setTitle($this->translate('Edit Event Rule'));
326320

327321
$this->addContent($eventRuleForm);
328322
}

application/controllers/EventRulesController.php

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66

77
use Icinga\Module\Notifications\Common\Database;
88
use Icinga\Module\Notifications\Common\Links;
9-
use Icinga\Module\Notifications\Forms\EventRuleConfigForm;
9+
use Icinga\Module\Notifications\Forms\EventRuleForm;
1010
use Icinga\Module\Notifications\Model\Rule;
1111
use Icinga\Module\Notifications\View\EventRuleRenderer;
1212
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1313
use Icinga\Module\Notifications\Widget\ItemList\ObjectList;
14-
use Icinga\Web\Notification;
1514
use Icinga\Web\Session;
16-
use ipl\Html\Attributes;
1715
use ipl\Html\Form;
18-
use ipl\Html\FormElement\SubmitButtonElement;
1916
use ipl\Html\Html;
20-
use ipl\Html\HtmlElement;
2117
use ipl\Stdlib\Filter;
2218
use ipl\Web\Compat\CompatController;
2319
use ipl\Web\Compat\SearchControls;
@@ -27,7 +23,6 @@
2723
use ipl\Web\Layout\DetailedItemLayout;
2824
use ipl\Web\Url;
2925
use ipl\Web\Widget\ButtonLink;
30-
use ipl\Web\Widget\Icon;
3126
use ipl\Web\Widget\Link;
3227

3328
class EventRulesController extends CompatController
@@ -49,7 +44,6 @@ public function init()
4944
public function indexAction(): void
5045
{
5146
$eventRules = Rule::on(Database::get());
52-
$this->sessionNamespace->delete('-1');
5347

5448
$limitControl = $this->createLimitControl();
5549
$paginationControl = $this->createPaginationControl($eventRules);
@@ -87,14 +81,24 @@ public function indexAction(): void
8781
$this->addControl($sortControl);
8882
$this->addControl($limitControl);
8983
$this->addControl($searchBar);
90-
$this->addContent(
84+
85+
$addButton =
9186
(new ButtonLink(
92-
t('New Event Rule'),
93-
Url::fromPath('notifications/event-rule/edit', ['id' => -1]),
87+
t('Add Event Rule'),
88+
Url::fromPath('notifications/event-rules/add'),
9489
'plus'
95-
))->openInModal()
96-
->addAttributes(['class' => 'add-new-component'])
97-
);
90+
))->openInModal();
91+
if (isset($this->sessionNamespace->{-1})) {
92+
$this->addContent(Html::tag('div', ['class' => 'add-new-component'], [
93+
$addButton->disable($this->translate(
94+
'You have unsaved changes. Please save or discard them first.'
95+
)),
96+
(new Link($this->translate('Continue Editing'), Links::eventRule(-1)))
97+
->setBaseTarget('_next')
98+
]));
99+
} else {
100+
$this->addContent($addButton->addAttributes(['class' => 'add-new-component']));
101+
}
98102

99103
$this->addContent(
100104
(new ObjectList($eventRules, new EventRuleRenderer()))
@@ -111,62 +115,19 @@ public function indexAction(): void
111115

112116
public function addAction(): void
113117
{
114-
$this->addTitleTab(t('Add Event Rule'));
115-
$this->getTabs()->setRefreshUrl(Url::fromPath('notifications/event-rules/add', ['id' => '-1']));
116-
117-
$this->controls->addAttributes(['class' => 'event-rule-detail']);
118-
$ruleId = $this->params->get('id');
119-
$config = $this->sessionNamespace->get($ruleId);
120-
$config['object_filter'] = $config['object_filter'] ?? null;
121-
122-
$eventRuleConfigSubmitButton = (new SubmitButtonElement(
123-
'save',
124-
[
125-
'label' => t('Add Event Rule'),
126-
'form' => 'event-rule-config-form'
127-
]
128-
))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
129-
130-
$eventRuleConfig = new EventRuleConfigForm(
131-
$config,
132-
Url::fromPath(
133-
'notifications/event-rules/search-editor',
134-
['id' => $ruleId]
135-
)
136-
);
137-
138-
$eventRuleConfig
139-
->populate($config)
140-
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) {
141-
$ruleId = (int) $config['id'];
142-
$ruleName = $config['name'];
143-
$insertId = $form->addOrUpdateRule($ruleId, $config);
144-
$this->sessionNamespace->delete($ruleId);
145-
Notification::success(sprintf(t('Successfully add event rule %s'), $ruleName));
146-
$this->redirectNow(Links::eventRule($insertId));
147-
})
148-
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($config) {
149-
$formValues = $form->getValues();
150-
$config = array_merge($config, $formValues);
151-
$config['rule_escalation'] = $formValues['rule_escalation'];
152-
$this->sessionNamespace->set('-1', $config);
153-
})
154-
->handleRequest($this->getServerRequest());
155-
156-
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
157-
Html::tag('h2', $config['name'] ?? ''),
158-
(new Link(
159-
new Icon('edit'),
160-
Url::fromPath('notifications/event-rule/edit', [
161-
'id' => -1
162-
]),
163-
['class' => 'control-button']
164-
))->openInModal()
165-
]);
166-
167-
$this->addControl($eventRuleForm);
168-
$this->addControl($eventRuleConfigSubmitButton);
169-
$this->addContent($eventRuleConfig);
118+
$this->setTitle($this->translate('Add Event Rule'));
119+
120+
$eventRuleForm = (new EventRuleForm())
121+
->populate(['id' => -1])
122+
->setAction(Url::fromRequest()->getAbsoluteUrl())
123+
->on(Form::ON_SUCCESS, function ($form) {
124+
$this->sessionNamespace->set(-1, ['id' => -1, 'name' => $form->getValue('name')]);
125+
$this->sendExtraUpdates(['#col1']);
126+
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
127+
$this->redirectNow(Links::eventRule(-1));
128+
})->handleRequest($this->getServerRequest());
129+
130+
$this->addContent($eventRuleForm);
170131
}
171132

172133
public function completeAction(): void

application/forms/EventRuleConfigForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected function assemble(): void
143143
$this->getElement('zero-condition-escalation')->setValue($defaultEscalationPrefix);
144144
}
145145

146-
$configFilter = new EventRuleConfigFilter($this->searchEditorUrl, $this->config['object_filter']);
146+
$configFilter = new EventRuleConfigFilter($this->searchEditorUrl, $this->config['object_filter'] ?? null);
147147
$this->registerElement($configFilter);
148148

149149
$addEscalationButton = $this->createElement(
@@ -429,7 +429,7 @@ public function addOrUpdateRule(int $id, array $config): int
429429

430430
$db->commitTransaction();
431431

432-
return (int) $id;
432+
return $id;
433433
}
434434

435435
/**

public/css/common.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080

8181
.add-new-component {
8282
margin: 0 0 1em 1em;
83+
84+
&:is(div) {
85+
display: flex;
86+
gap: .5em;
87+
align-items: baseline;
88+
}
8389
}
8490

8591
.item-layout.rule footer {

0 commit comments

Comments
 (0)