Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
- Resolve save for config filter
- Remove unnecessary auto-submits
- Add extra-updates for config filter of new event rule
  • Loading branch information
raviks789 committed Apr 5, 2024
1 parent bfb1542 commit 5c1bcef
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 88 deletions.
152 changes: 72 additions & 80 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Icinga\Module\Notifications\Common\Auth;
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\Util\Environment;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use ipl\Html\Attributes;
Expand All @@ -31,36 +33,23 @@ class EventRuleController extends CompatController
{
use Auth;

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

/** @var ?string Event rule config filter */
protected $filter;

public function init()
{
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
}

public function indexAction(): void
{
$this->assertPermission('notifications/config/event-rule');
$this->sessionNamespace->delete('-1');
$this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy']));

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

/** @var string $ruleId */
$ruleId = $this->params->getRequired('id');
/** @var array<string, mixed>|null $configValues */
$configValues = $this->sessionNamespace->get($ruleId);
$this->controls->addAttributes(['class' => 'event-rule-detail']);

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

$eventRuleConfig = (new EventRuleConfigForm(
$configValues,
Expand All @@ -72,51 +61,16 @@ public function indexAction(): void
$eventRuleConfig
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$form->addOrUpdateRule($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('__CLOSE__');
})
->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']
)
);
$this->redirectNow(Links::eventRule((int) $ruleId));
})
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$configValues = array_merge($configValues, $form->getValues());
$configValues['rule_escalation'] = $form->getValues()['rule_escalation'];
$this->sessionNamespace->set($ruleId, $configValues);
})
->handleRequest($this->getServerRequest());

/** @var array<string, mixed> $cache */
$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',
Expand All @@ -137,7 +91,7 @@ public function indexAction(): void
));

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

if ($ruleId > 0) {
Expand All @@ -150,7 +104,7 @@ public function indexAction(): void
}
}

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [
Html::tag('h2', $configValues['name'] ?? ''),
(new Link(
new Icon('edit'),
Expand All @@ -166,6 +120,40 @@ public function indexAction(): void
$this->addContent($eventRuleConfig);
}

public function ruleAction(): void
{
$ruleId = $this->params->getRequired('id');
$query = Rule::on(Database::get())
->withoutColumns('timeperiod_id')
->filter(Filter::equal('id', $ruleId));

Check failure on line 128 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 128 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 128 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 128 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 128 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 128 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

$rule = $query->first();

$this->getDocument()->add([
Html::tag('h2', $rule->name ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => $ruleId
]),
['class' => 'control-button']
))->openInModal()
]);
}

public function configFilterAction(): void
{
$ruleId = $this->params->getRequired('id');
$objectFilter = $this->params->getRequired('object_filter');
$this->getDocument()->add((new EventRuleConfigFilter('config-filter'))
->setObjectFilter($objectFilter)

Check failure on line 149 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #1 $filter of method Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter::setObjectFilter() expects string, mixed given.

Check failure on line 149 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #1 $filter of method Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter::setObjectFilter() expects string, mixed given.

Check failure on line 149 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #1 $filter of method Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter::setObjectFilter() expects string, mixed given.

Check failure on line 149 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #1 $filter of method Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter::setObjectFilter() expects string, mixed given.

Check failure on line 149 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #1 $filter of method Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter::setObjectFilter() expects string, mixed given.

Check failure on line 149 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #1 $filter of method Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter::setObjectFilter() expects string, mixed given.
->setSearchEditorUrl(
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId])
)
->ensureAssembled()
->getContent());
}

/**
* Create config from db
*
Expand Down Expand Up @@ -237,12 +225,7 @@ 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);
}
$eventRule = $this->fromDb($ruleId);

Check failure on line 228 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #1 $ruleId of method Icinga\Module\Notifications\Controllers\EventRuleController::fromDb() expects int, string given.

Check failure on line 228 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #1 $ruleId of method Icinga\Module\Notifications\Controllers\EventRuleController::fromDb() expects int, string given.

Check failure on line 228 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #1 $ruleId of method Icinga\Module\Notifications\Controllers\EventRuleController::fromDb() expects int, string given.

Check failure on line 228 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #1 $ruleId of method Icinga\Module\Notifications\Controllers\EventRuleController::fromDb() expects int, string given.

Check failure on line 228 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #1 $ruleId of method Icinga\Module\Notifications\Controllers\EventRuleController::fromDb() expects int, string given.

Check failure on line 228 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #1 $ruleId of method Icinga\Module\Notifications\Controllers\EventRuleController::fromDb() expects int, string given.

$editor = new SearchEditor();

Expand All @@ -259,16 +242,20 @@ public function searchEditorAction(): void

$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {

Check failure on line 243 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Anonymous function has an unused use $eventRule.

Check failure on line 243 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Anonymous function has an unused use $eventRule.

Check failure on line 243 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Anonymous function has an unused use $eventRule.

Check failure on line 243 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Anonymous function has an unused use $eventRule.

Check failure on line 243 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Anonymous function has an unused use $eventRule.

Check failure on line 243 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Anonymous function has an unused use $eventRule.
$filter = self::createFilterString($form->getFilter());
$eventRule['object_filter'] = $filter;
$this->sessionNamespace->set($ruleId, $eventRule);
$this->getResponse()
->setHeader('X-Icinga-Container', '_self')
->redirectAndExit(
Url::fromPath(
'notifications/event-rule',
['id' => $ruleId]
$this->sendExtraUpdates(
[
'#config-filter' => Url::fromPath(
'notifications/event-rule/config-filter',
[
'id' => $ruleId,
'object_filter' => $filter
]
)
);
]
);
$this->getResponse()
->setHeader('X-Icinga-Container', 'dummy')
->redirectAndExit('__CLOSE__');
});

$editor->handleRequest($this->getServerRequest());
Expand Down Expand Up @@ -304,13 +291,10 @@ public function editAction(): void
/** @var string $ruleId */
$ruleId = $this->params->getRequired('id');
/** @var array<string, mixed>|null $config */
$config = $this->sessionNamespace->get($ruleId);
if ($config === null) {
if ($ruleId === '-1') {
$config = ['id' => $ruleId];
} else {
$config = $this->fromDb((int) $ruleId);
}
if ($ruleId === '-1') {

Check failure on line 294 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Variable $config in PHPDoc tag @var does not exist.

Check failure on line 294 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Variable $config in PHPDoc tag @var does not exist.

Check failure on line 294 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Variable $config in PHPDoc tag @var does not exist.

Check failure on line 294 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Variable $config in PHPDoc tag @var does not exist.

Check failure on line 294 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Variable $config in PHPDoc tag @var does not exist.

Check failure on line 294 in application/controllers/EventRuleController.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Variable $config in PHPDoc tag @var does not exist.
$config = ['id' => $ruleId];
} else {
$config = $this->fromDb((int) $ruleId);
}

$eventRuleForm = (new EventRuleForm())
Expand All @@ -332,14 +316,22 @@ public function editAction(): void

if ($ruleId === '-1') {
$redirectUrl = Url::fromPath('notifications/event-rules/add', $params);
$this->redirectNow($redirectUrl);
} else {
$redirectUrl = Url::fromPath('notifications/event-rule', $params);
$this->sendExtraUpdates(['#col1']);
Database::get()->update('rule', [
'name' => $config['name'],
'is_active' => $config['is_active'] ?? 'n'
], ['id = ?' => $ruleId]);
$this->sendExtraUpdates([
'#event-rule-form' => Url::fromPath(
'notifications/event-rule/rule',
['id' => $ruleId]
)->getAbsoluteUrl()
]);

$this->getResponse()->setHeader('X-Icinga-Container', 'dummy')
->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
21 changes: 14 additions & 7 deletions application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,20 @@ public function addAction(): void

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $config['name'] ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => -1
]),
['class' => 'control-button']
))->openInModal()
Html::tag(
'div',
[
'class' => 'not-allowed',
'title' => $this->translate('Cannot edit event rule until it is saved to database')
],
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => -1
]),
['class' => ['control-button', 'disabled']]
))->openInModal()
)
]);

$this->addControl($eventRuleForm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EventRuleConfigFilter extends FieldsetElement
/** @var ?string Event rule's object filter */
protected $objectFilter;

protected $defaultAttributes = ['class' => 'config-filter'];
protected $defaultAttributes = ['class' => 'config-filter', 'id' => 'config-filter'];

protected function assemble(): void
{
Expand Down
8 changes: 8 additions & 0 deletions public/css/detail/event-rule-detail.less
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
width: 25em;
}
}

.control-button.disabled {
pointer-events: none;
}

.not-allowed {
cursor: not-allowed;
}
}

.save-config {
Expand Down

0 comments on commit 5c1bcef

Please sign in to comment.