Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove event rule session storage #170

Open
wants to merge 14 commits into
base: event-rule-config-enhancement
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 134 additions & 137 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
use Icinga\Module\Notifications\Common\Auth;
sukhwinder33445 marked this conversation as resolved.
Show resolved Hide resolved
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter;
use Icinga\Module\Notifications\Forms\EventRuleConfigForm;
use Icinga\Module\Notifications\Forms\EventRuleForm;
use Icinga\Module\Notifications\Model\Incident;
use Icinga\Module\Notifications\Model\Rule;
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use ipl\Html\Attributes;
use ipl\Html\Form;
use ipl\Html\FormElement\SubmitButtonElement;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Stdlib\Filter;
Expand All @@ -31,124 +29,104 @@ class EventRuleController extends CompatController
{
use Auth;

/** @var Session\SessionNamespace */
private $sessionNamespace;

public function init()
public function init(): void
{
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
$this->assertPermission('notifications/config/event-rule');
}

public function indexAction(): void
{
$this->sessionNamespace->delete('-1');
// Add an empty container and set it as the X-Icinga-Container when sending extra updates
// from the modal for filter or event rule
$this->addContent(new HtmlElement(
'div',
Attributes::create(['class' => 'container', 'id' => 'dummy-event-rule-container'])
));

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

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

$disableSave = false;
if ($configValues === null) {
$configValues = $this->fromDb((int) $ruleId);
$disableSave = true;
$eventRuleConfigValues = $this->fromDb((int) $ruleId);

$filter = $eventRuleConfigValues['object_filter'];

if ($this->getRequest()->isPost()) {
if ($this->getRequest()->has('searchbar')) {
$filter = $this->getRequest()->get('searchbar');
} else {
$filter = null;
}
}

$eventRuleConfig = new EventRuleConfigForm(
$configValues,
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId])
);
$eventRuleConfig = (new EventRuleConfigForm(
$eventRuleConfigValues,
Url::fromPath(
'notifications/event-rule/search-editor',
['id' => $ruleId, 'object_filter' => $filter]
),
$filter
))
->populate($eventRuleConfigValues)
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
$diff = $form->getChanges();
if (empty($diff)) {
return;
}

$eventRuleConfig
->populate($configValues)
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$form->addOrUpdateRule((int) $ruleId, $configValues);
$this->sessionNamespace->delete($ruleId);
Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name'])));
$this->redirectNow(Links::eventRule((int) $ruleId));
})
->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$form->removeRule((int) $ruleId);
$this->sessionNamespace->delete($ruleId);
Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name']));
$this->redirectNow(Links::eventRules());
})
->on(EventRuleConfigForm::ON_DISCARD, function () use ($ruleId, $configValues) {
$this->sessionNamespace->delete($ruleId);
Notification::success(
sprintf(
t('Successfully discarded changes to event rule %s'),
$configValues['name']
)
);
$form->updateRule($ruleId, $diff);
Notification::success(sprintf(
t('Successfully saved event rule %s'),
$eventRuleConfigValues['name']
));

$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::eventRule((int) $ruleId));
})
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$formValues = $form->getValues();
$configValues = array_merge($configValues, $formValues);
$configValues['rule_escalation'] = $formValues['rule_escalation'];
$this->sessionNamespace->set($ruleId, $configValues);
})
->on(
EventRuleConfigForm::ON_DELETE,
function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
$csrf = $form->getElement('CSRFToken');
if ($csrf !== null && $csrf->isValid()) {
$form->removeRule((int) $ruleId);
Notification::success(
sprintf(t('Successfully deleted event rule %s'), $eventRuleConfigValues['name'])
);

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

$cache = $this->sessionNamespace->get($ruleId);
$discardChangesButton = null;
if ($cache !== null) {
$this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.')));
$discardChangesButton = new SubmitButtonElement(
'discard_changes',
[
'label' => t('Discard Changes'),
'form' => 'event-rule-config-form',
'class' => 'btn-discard-changes',
'formnovalidate' => true,
]
);

$disableSave = false;
}

$buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]));
$eventRuleConfigSubmitButton = new SubmitButtonElement(
'save',
[
'label' => t('Save'),
'form' => 'event-rule-config-form',
'disabled' => $disableSave
]
);

$deleteButton = new SubmitButtonElement(
'delete',
[
'label' => t('Delete'),
'form' => 'event-rule-config-form',
'class' => 'btn-remove',
'formnovalidate' => true
]
);
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [
Html::tag('h2', $eventRuleConfigValues['name']),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => $ruleId
]),
['class' => 'control-button']
))->openInModal()
]);

$buttonsWrapper->add([$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton]);
$this->addControl($eventRuleForm);
$this->addControl($eventRuleConfig->createFormSubmitButtons());
$this->addContent($eventRuleConfig);
}

if ($ruleId > 0) {
$incidentCount = Incident::on(Database::get())
->with('rule')
->filter(Filter::equal('rule.id', $ruleId))
->count();
public function ruleAction(): void
{
/** @var int $ruleId */
$ruleId = $this->params->getRequired('id');
$query = Rule::on(Database::get())
->withoutColumns('timeperiod_id')
->filter(Filter::equal('id', $ruleId));

if ($incidentCount) {
$deleteButton->addAttributes([
'disabled' => true,
'title' => t('There are active incidents for this event rule and hence cannot be removed')
]);
}
}
$rule = $query->first();

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $configValues['name']),
$this->getDocument()->add([
Html::tag('h2', $rule->name),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
Expand All @@ -157,10 +135,25 @@ public function indexAction(): void
['class' => 'control-button']
))->openInModal()
]);
}

$this->addControl($eventRuleForm);
$this->addControl($buttonsWrapper);
$this->addContent($eventRuleConfig);
public function configFilterAction(): void
{
$ruleId = $this->params->getRequired('id');
$objectFilter = $this->params->get('object_filter');
$eventRuleFilterFieldset = new EventRuleConfigFilter(
Url::fromPath(
'notifications/event-rule/search-editor',
['id' => $ruleId, 'object_filter' => $objectFilter]
),
$objectFilter
);

if (! $objectFilter) {
$eventRuleFilterFieldset->getAttributes()->add('class', 'empty-filter');
}

$this->getDocument()->add($eventRuleFilterFieldset);
}

/**
Expand All @@ -182,6 +175,7 @@ public function fromDb(int $ruleId): array
}

$config = iterator_to_array($rule);
$config['object_filter'] = $config['object_filter'] ?? null;

foreach ($rule->rule_escalation as $re) {
foreach ($re as $k => $v) {
Expand Down Expand Up @@ -231,33 +225,28 @@ public function searchEditorAction(): void
{
/** @var string $ruleId */
$ruleId = $this->params->shiftRequired('id');

$eventRule = $this->sessionNamespace->get($ruleId);

if ($eventRule === null) {
$eventRule = $this->fromDb((int) $ruleId);
}

$editor = new SearchEditor();

$objectFilter = $eventRule['object_filter'] ?? '';
/** @var string $objectFilter */
$objectFilter = $this->params->shift('object_filter', '');
$editor->setQueryString($objectFilter)
->setAction(Url::fromRequest()->getAbsoluteUrl())
->setSuggestionUrl(
Links::ruleFilterSuggestionUrl($ruleId)->addParams(['_disableLayout' => true, 'showCompact' => true])
);

$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
$eventRule['object_filter'] = self::createFilterString($form->getFilter());
$this->sessionNamespace->set($ruleId, $eventRule);
$this->getResponse()
->setHeader('X-Icinga-Container', '_self')
->redirectAndExit(
Url::fromPath(
'notifications/event-rule',
['id' => $ruleId]
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId) {
$this->sendExtraUpdates(
[
'#filter-wrapper' => Url::fromPath(
'notifications/event-rule/config-filter',
['id' => $ruleId, 'object_filter' => self::createFilterString($form->getFilter())]
)
);
]
);
$this->getResponse()
->setHeader('X-Icinga-Container', 'dummy-event-rule-container')
->redirectAndExit('__CLOSE__');
});

$editor->handleRequest($this->getServerRequest());
Expand Down Expand Up @@ -293,31 +282,39 @@ public function editAction(): void
{
/** @var string $ruleId */
$ruleId = $this->params->getRequired('id');
$config = $this->sessionNamespace->get($ruleId);
if ($config === null) {
if ($ruleId === '-1') {
$config = ['id' => $ruleId];
} else {
$config = $this->fromDb((int) $ruleId);
}
$db = Database::get();
if ($ruleId === '-1') {
$config = ['id' => $ruleId];
} else {
// Casting to array is required as Connection::fetchOne actually returns stdClass and not array
$config = (array) $db->fetchOne(
Rule::on($db)->withoutColumns('timeperiod_id')->filter(Filter::equal('id', $ruleId))->assembleSelect()
);
}

$eventRuleForm = (new EventRuleForm())
->populate($config)
->setAction(Url::fromRequest()->getAbsoluteUrl())
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $config) {
$config['name'] = $form->getValue('name');
$config['is_active'] = $form->getValue('is_active');
->on(Form::ON_SUCCESS, function (EventRuleForm $form) use ($ruleId, $db) {
if ($ruleId === '-1') {
$redirectUrl = Url::fromPath('notifications/event-rules/add', ['id' => '-1']);
} else {
$redirectUrl = Url::fromPath('notifications/event-rule', ['id' => $ruleId]);
$db->insert('rule', $form->getValues());
$id = $db->lastInsertId();

$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::eventRule($id));
} else {
$db->update('rule', $form->getValues(), ['id = ?' => $ruleId]);
$this->sendExtraUpdates([
'#event-rule-form' => Url::fromPath(
'notifications/event-rule/rule', ['id' => $ruleId]
)->getAbsoluteUrl(),
'#col1'
]);

$this->getResponse()->setHeader('X-Icinga-Container', 'dummy-event-rule-container')
->redirectAndExit('__CLOSE__');
}

$this->sessionNamespace->set($ruleId, $config);
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
$this->redirectNow($redirectUrl);
})->handleRequest($this->getServerRequest());

if ($ruleId === '-1') {
Expand Down
Loading