-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Resolve save for config filter - Remove unnecessary auto-submits - Add extra-updates for config filter of new event rule
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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', | ||
|
@@ -137,7 +91,7 @@ public function indexAction(): void | |
)); | ||
|
||
$buttonsWrapper->add( | ||
[$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton] | ||
[$eventRuleConfigSubmitButton, $deleteButton] | ||
); | ||
|
||
if ($ruleId > 0) { | ||
|
@@ -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'), | ||
|
@@ -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
|
||
|
||
$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
|
||
->setSearchEditorUrl( | ||
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId]) | ||
) | ||
->ensureAssembled() | ||
->getContent()); | ||
} | ||
|
||
/** | ||
* Create config from db | ||
* | ||
|
@@ -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
|
||
|
||
$editor = new SearchEditor(); | ||
|
||
|
@@ -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
|
||
$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()); | ||
|
@@ -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
|
||
$config = ['id' => $ruleId]; | ||
} else { | ||
$config = $this->fromDb((int) $ruleId); | ||
} | ||
|
||
$eventRuleForm = (new EventRuleForm()) | ||
|
@@ -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') { | ||
|