Skip to content

Commit

Permalink
Open event rule basics in a modal
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Oct 30, 2023
1 parent 8b2967c commit 837d839
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
77 changes: 65 additions & 12 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use ipl\Web\Compat\CompatController;
use ipl\Web\Control\SearchEditor;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;

class EventRuleController extends CompatController
{
Expand Down Expand Up @@ -103,18 +105,16 @@ public function indexAction(): void
$this->redirectNow('__CLOSE__');
})->handleRequest($this->getServerRequest());

$eventRuleForm = (new EventRuleForm())
->populate($cache ?? $this->fromDb($ruleId))
->on(Form::ON_SENT, function ($form) use ($ruleId, $eventRuleConfig, $saveForm) {
$config = $eventRuleConfig->getConfig();
$config['name'] = $form->getValue('name');
$config['is_active'] = $form->getValue('is_active');

$eventRuleConfig->setConfig($config);

$this->sessionNamespace->set($ruleId, $eventRuleConfig->getConfig());
$saveForm->setSubmitButtonDisabled(false);
})->handleRequest($this->getServerRequest());
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $eventRuleConfig->getConfig()['name'] ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => $ruleId
]),
['class' => 'control-button']
))->openInModal()
]);

$eventRuleFormAndSave = Html::tag('div', ['class' => 'event-rule-and-save-forms']);
$eventRuleFormAndSave->add([
Expand Down Expand Up @@ -224,4 +224,57 @@ public function searchEditorAction(): void
$this->getDocument()->add($editor);
$this->setTitle($this->translate('Adjust Filter'));
}

public function editAction()
{
$ruleId = (int) $this->params->getRequired('id');
$cache = $this->sessionNamespace->get($ruleId);

if ($this->params->has('clearCache')) {
$this->sessionNamespace->delete($ruleId);
$cache = [];
}

if (isset($cache) || $ruleId === -1) {
$config = $cache ?? [];
} else {
$config = $this->fromDb($ruleId);
}

$eventRuleForm = (new EventRuleForm())
->populate($config)
->setAction(Url::fromRequest()->getAbsoluteUrl())
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $cache, $config) {
$config['name'] = $form->getValue('name');
$config['is_active'] = $form->getValue('is_active');

if ($cache || $ruleId === -1) {
$this->sessionNamespace->set($ruleId, $config);
} else {
(new SaveEventRuleForm())->editRule($ruleId, $config);
}

if ($ruleId === -1) {
$redirectUrl = Url::fromPath('notifications/event-rules/add', [
'use_cache' => true
]);
} else {
$redirectUrl = Url::fromPath('notifications/event-rule', [
'id' => $ruleId
]);
$this->sendExtraUpdates(['#col1']);
}

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

if ($ruleId === -1) {
$this->setTitle($this->translate('New Event Rule'));
} else {
$this->setTitle($this->translate('Edit Event Rule'));
}

$this->addContent($eventRuleForm);
}
}
26 changes: 14 additions & 12 deletions application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\ButtonLink;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;

class EventRulesController extends CompatController
{
Expand Down Expand Up @@ -86,9 +88,9 @@ public function indexAction(): void
$this->addContent(
(new ButtonLink(
t('New Event Rule'),
'notifications/event-rules/add',
Url::fromPath('notifications/event-rule/edit', ['id' => -1, 'clearCache' => true]),
'plus'
))->setBaseTarget('_next')
))->openInModal()
->addAttributes(['class' => 'new-event-rule'])
);

Expand Down Expand Up @@ -119,16 +121,16 @@ public function addAction(): void

$eventRuleConfig = new EventRuleConfig(Url::fromPath('notifications/event-rules/add-search-editor'), $cache);

$eventRuleForm = (new EventRuleForm())
->populate($cache)
->on(Form::ON_SENT, function ($form) use ($eventRuleConfig) {
$config = $eventRuleConfig->getConfig();
$config['name'] = $form->getValue('name');
$config['is_active'] = $form->getValue('is_active');

$eventRuleConfig->setConfig($config);
$this->sessionNamespace->set(-1, $eventRuleConfig->getConfig());
})->handleRequest($this->getServerRequest());
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $eventRuleConfig->getConfig()['name'] ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => -1
]),
['class' => 'control-button']
))->openInModal()
]);

$saveForm = (new SaveEventRuleForm())
->on(SaveEventRuleForm::ON_SUCCESS, function ($saveForm) use ($eventRuleConfig) {
Expand Down
21 changes: 6 additions & 15 deletions application/forms/EventRuleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,24 @@
namespace Icinga\Module\Notifications\Forms;

use Icinga\Web\Session;
use ipl\Html\Form;
use ipl\I18n\Translation;
use ipl\Web\Common\CsrfCounterMeasure;
use ipl\Web\Common\FormUid;
use ipl\Web\FormDecorator\IcingaFormDecorator;
use ipl\Web\Compat\CompatForm;

class EventRuleForm extends Form
class EventRuleForm extends CompatForm
{
use CsrfCounterMeasure;
use FormUid;
use Translation;

protected $defaultAttributes = [
'class' => ['event-rule-form', 'icinga-form', 'icinga-controls'],
'name' => 'event-rule-form'
];

protected function assemble()
{
$this->setDefaultElementDecorator(new IcingaFormDecorator());

$this->add($this->createCsrfCounterMeasure(Session::getSession()->getId()));
$this->add($this->createUidElement());

$this->addElement(
'text',
'name',
[
'label' => $this->translate('Title'),
'class' => 'autosubmit',
'required' => true
]
);
Expand All @@ -44,9 +32,12 @@ protected function assemble()
'is_active',
[
'label' => $this->translate('Event Rule is active'),
'class' => 'autosubmit',
'value' => 'y'
]
);

$this->addElement('submit', 'btn_submit', [
'label' => $this->translate('Save')
]);
}
}

0 comments on commit 837d839

Please sign in to comment.