Skip to content

Commit 47e9108

Browse files
committed
EventRuleForm: Save new event rule to the database on submission
1 parent 070f2f9 commit 47e9108

File tree

2 files changed

+51
-130
lines changed

2 files changed

+51
-130
lines changed

application/controllers/EventRuleController.php

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function indexAction(): void
7474
return;
7575
}
7676

77-
$form->addOrUpdateRule($ruleId, $diff);
77+
$form->updateRule($ruleId, $diff);
7878
Notification::success(sprintf(
7979
t('Successfully saved event rule %s'),
8080
$eventRuleConfigValues['name']
@@ -272,68 +272,6 @@ public static function createFilterString(Filter\Rule $filters): ?string
272272
return $filterStr !== '' ? rawurldecode($filterStr) : null;
273273
}
274274

275-
public function addAction(): void
276-
{
277-
$this->addTitleTab(t('Add Event Rule'));
278-
$this->getTabs()->setRefreshUrl(Url::fromRequest());
279-
280-
// Add an empty container and set it as the X-Icinga-Container when sending extra updates
281-
// from the modal for filter or event rule
282-
$this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy-event-rule-container']));
283-
284-
$this->controls->addAttributes(['class' => 'event-rule-detail']);
285-
$ruleId = $this->params->get('id');
286-
287-
if ($this->getRequest()->isPost()) {
288-
if ($this->getRequest()->has('searchbar')) {
289-
$eventRule['object_filter'] = $this->getRequest()->get('searchbar');
290-
} else {
291-
$eventRule['object_filter'] = null;
292-
}
293-
}
294-
295-
$eventRuleConfig = new EventRuleConfigForm(
296-
$eventRule,
297-
Url::fromPath(
298-
'notifications/event-rule/search-editor',
299-
['id' => $ruleId, 'object_filter' => $eventRule['object_filter']]
300-
)
301-
);
302-
303-
$eventRuleConfig
304-
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($eventRule) {
305-
$eventRuleConfig = array_merge($eventRule, $form->getValues());
306-
$ruleId = $form->addOrUpdateRule((int) $eventRule['id'], $eventRuleConfig);
307-
Notification::success(sprintf(t('Successfully add event rule %s'), $eventRule['name']));
308-
309-
$this->sendExtraUpdates(['#col1']);
310-
$this->redirectNow(Links::eventRule($ruleId));
311-
})
312-
->handleRequest($this->getServerRequest());
313-
314-
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
315-
Html::tag('h2', $eventRule['name'] ?? ''),
316-
Html::tag(
317-
'div',
318-
[
319-
'class' => 'not-allowed',
320-
'title' => $this->translate('Cannot edit event rule until it is saved to database')
321-
],
322-
(new Link(
323-
new Icon('edit'),
324-
Url::fromPath('notifications/event-rule/edit', [
325-
'id' => -1
326-
]),
327-
['class' => ['control-button', 'disabled']]
328-
))->openInModal()
329-
)
330-
]);
331-
332-
$this->addControl($eventRuleForm);
333-
$this->addControl($eventRuleConfig->createFormSubmitButtons());
334-
$this->addContent($eventRuleConfig);
335-
}
336-
337275
public function editAction(): void
338276
{
339277
/** @var string $ruleId */
@@ -351,18 +289,24 @@ public function editAction(): void
351289
$eventRuleForm = (new EventRuleForm())
352290
->populate($config)
353291
->setAction(Url::fromRequest()->getAbsoluteUrl())
354-
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $config, $db) {
355-
$config['name'] = $form->getValue('name');
356-
$config['is_active'] = $form->getValue('is_active');
357-
292+
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $db) {
358293
if ($ruleId === '-1') {
359-
$redirectUrl = Url::fromPath('notifications/event-rules/add', ['id' => '-1']);
294+
$db->insert('rule', [
295+
'name' => $form->getValue('name'),
296+
'timeperiod_id' => null,
297+
'object_filter' => null,
298+
'is_active' => $form->getValue('is_active')
299+
]);
300+
301+
$id = $db->lastInsertId();
302+
360303
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
361-
$this->redirectNow($redirectUrl);
304+
$this->sendExtraUpdates(['#col1']);
305+
$this->redirectNow(Links::eventRule($id));
362306
} else {
363307
$db->update('rule', [
364-
'name' => $config['name'],
365-
'is_active' => $config['is_active'] ?? 'n'
308+
'name' => $form->getValue('name'),
309+
'is_active' => $form->getValue('is_active')
366310
], ['id = ?' => $ruleId]);
367311

368312
$this->sendExtraUpdates([

application/forms/EventRuleConfigForm.php

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -99,53 +99,43 @@ public function createFormSubmitButtons(): ValidHtml
9999
$buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]));
100100
$ruleId = $this->config['id'];
101101

102-
if ($ruleId === '-1') {
103-
return (new SubmitButtonElement(
104-
'save',
105-
[
106-
'label' => $this->translate('Add Event Rule'),
107-
'form' => 'event-rule-config-form',
108-
]
109-
))->setWrapper($buttonsWrapper);
110-
} else {
111-
$saveButton = new SubmitButtonElement(
112-
'save',
113-
[
114-
'label' => $this->translate('Save'),
115-
'form' => 'event-rule-config-form',
116-
]
117-
);
102+
$saveButton = new SubmitButtonElement(
103+
'save',
104+
[
105+
'label' => $this->translate('Save'),
106+
'form' => 'event-rule-config-form',
107+
]
108+
);
118109

119-
$deleteButton = new SubmitButtonElement(
120-
'delete',
121-
[
122-
'label' => $this->translate('Delete'),
123-
'form' => 'event-rule-config-form',
124-
'class' => 'btn-remove',
125-
'formnovalidate' => true
126-
]
127-
);
110+
$deleteButton = new SubmitButtonElement(
111+
'delete',
112+
[
113+
'label' => $this->translate('Delete'),
114+
'form' => 'event-rule-config-form',
115+
'class' => 'btn-remove',
116+
'formnovalidate' => true
117+
]
118+
);
128119

129-
$buttonsWrapper->addHtml($saveButton, $deleteButton);
130-
131-
if ((int) $ruleId > 0) {
132-
$incidentCount = Incident::on(Database::get())
133-
->with('rule')
134-
->filter(Filter::equal('rule.id', $ruleId))
135-
->count();
136-
137-
if ($incidentCount) {
138-
$deleteButton->addAttributes([
139-
'disabled' => true,
140-
'title' => $this->translate(
141-
'There are active incidents for this event rule and hence cannot be removed'
142-
)
143-
]);
144-
}
145-
}
120+
$buttonsWrapper->addHtml($saveButton, $deleteButton);
121+
122+
if ((int) $ruleId > 0) {
123+
$incidentCount = Incident::on(Database::get())
124+
->with('rule')
125+
->filter(Filter::equal('rule.id', $ruleId))
126+
->count();
146127

147-
return $buttonsWrapper;
128+
if ($incidentCount) {
129+
$deleteButton->addAttributes([
130+
'disabled' => true,
131+
'title' => $this->translate(
132+
'There are active incidents for this event rule and hence cannot be removed'
133+
)
134+
]);
135+
}
148136
}
137+
138+
return $buttonsWrapper;
149139
}
150140

151141
protected function assemble(): void
@@ -469,24 +459,11 @@ protected function createRemoveButton(string $prefix): SubmitButtonElement
469459
*
470460
* @return int
471461
*/
472-
public function addOrUpdateRule(int $id, array $config): int
462+
public function updateRule(int $id, array $config): int
473463
{
474464
$db = Database::get();
475465
$db->beginTransaction();
476-
if ($id < 0) {
477-
$db->insert('rule', [
478-
'name' => $config['name'],
479-
'timeperiod_id' => $config['timeperiod_id'] ?? null,
480-
'object_filter' => $config['object_filter'] ?? null,
481-
'is_active' => $config['is_active'] ?? 'n'
482-
]);
483-
484-
$id = $db->lastInsertId();
485-
} else {
486-
$db->update('rule', [
487-
'object_filter' => $config['object_filter'] ?? null,
488-
], ['id = ?' => $id]);
489-
}
466+
$db->update('rule', ['object_filter' => $config['object_filter'] ?? null], ['id = ?' => $id]);
490467

491468
if (! isset($config['rule_escalation'])) {
492469
$db->commitTransaction();
@@ -542,7 +519,7 @@ public function addOrUpdateRule(int $id, array $config): int
542519

543520
$db->commitTransaction();
544521

545-
return (int) $id;
522+
return $id;
546523
}
547524

548525
/**

0 commit comments

Comments
 (0)