Skip to content

Commit

Permalink
WIP2
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Feb 27, 2024
1 parent 3775b8c commit e152bf2
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 327 deletions.
149 changes: 72 additions & 77 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use Icinga\Module\Notifications\Common\Auth;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
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\Module\Notifications\Web\Form\EventRuleDecorator;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use ipl\Html\Attributes;
use ipl\Html\Form;
Expand Down Expand Up @@ -52,90 +55,92 @@ public function indexAction(): void

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

$discardChangesButton = null;
if ($config === null) {
$config = $this->fromDb($ruleId);
$this->sessionNamespace->set($ruleId, $config);
}

// $config = $this->fromDb($ruleId);
//
// if ($filter === '' && isset($config['object_filter'])) {
// $filter = $config['object_filter'];
// }
$eventRuleConfig = (new EventRuleConfigForm(
$config,
Url::fromPath(
'notifications/event-rule/search-editor',
['id' => $config['id']]
)
))->populate($config);
$eventRuleConfig
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) {
$form->insertOrAddRule($config['id'], $config);
$this->sessionNamespace->delete($config['id']);
Notification::success((sprintf(t('Successfully saved event rule %s'), $config['name'])));

$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::eventRule($config['id']));
})
->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($config) {
$ruleId = $config['id'];
$form->removeRule($ruleId);
$this->sessionNamespace->delete($ruleId);
Notification::success(sprintf(t('Successfully deleted event rule %s'), $config['name']));
$this->redirectNow('__CLOSE__');
})
->on(EventRuleConfigForm::ON_DISCARD, function () use ($config) {
$ruleId = $config['id'];
$this->sessionNamespace->delete($ruleId);
$this->redirectNow(Links::eventRule($ruleId));
})
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($config) {
$formValues = $form->getValues();
$config = array_merge($config, $form->getValues());
$this->sessionNamespace->set($config['id'], $config);
})
->handleRequest($this->getServerRequest());

$cache = $this->sessionNamespace->get($ruleId);
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,
]
));
}


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

$discardChangesButton = (new SubmitButtonElement(
'discard_changes',
[
'label' => t('Discard Changes'),
'form' => 'event-rule-config-form',
'formnovalidate' => true,
]
))
->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));

));
$deleteButton = (new SubmitButtonElement(
'delete',
[
'label' => t('Delete'),
'form' => 'event-rule-config-form',
'class' => 'btn-remove',
'label' => t('Delete'),
'form' => 'event-rule-config-form',
'class' => 'btn-remove',
'formnovalidate' => true
]
))
->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
));

// $eventRuleDecorator = new EventRuleDecorator();
// $eventRuleDecorator->decorate($eventRuleConfigSubmitButton);
// $eventRuleDecorator->decorate($discardChangesButton);
$buttonsWrapper->add(
[$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton]
);

$eventRuleConfig = (new EventRuleConfigForm(
$config['object_filter'] ?? '',
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId])
))
->registerElement($eventRuleConfigSubmitButton)
->registerElement($discardChangesButton)
->registerElement($deleteButton);

$eventRuleConfig->populate($config);
$eventRuleConfig
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($deleteButton, $eventRuleConfigSubmitButton, $config) {
$pressedButton = $form->getPressedSubmitElement();
if ($pressedButton) {
var_dump($pressedButton->getName());die;
if ($pressedButton->getName() === 'delete') {
$this->sessionNamespace->delete($config['id']);
} else {
$db = Database::get();
$ruleId = (int) $config['id'];
if ($ruleId !== -1) {
// var_dump($config);die;
$db->update('rule', [
'name' => $config['name'],
'timeperiod_id' => $config['timeperiod_id'] ?? null,
'object_filter' => $config['object_filter'] ?? null,
'is_active' => $config['is_active'] ?? 'n'
], ['id = ?' => $ruleId]);

$form->insertOrAddRule($ruleId, $config);
}
}
}
})
->on(Form::ON_SENT, function (Form $form) use ($config, $ruleId) {
$config = array_merge($config, $form->getValues());
$this->sessionNamespace->set($ruleId, $config);
});
if ($ruleId > 0) {
$incidents = Incident::on(Database::get())
->with('rule')
->filter(Filter::equal('rule.id', $ruleId));

$eventRuleConfig->handleRequest($this->getServerRequest());
if ($incidents->count() > 0) {
$deleteButton->addAttributes(['disabled' => true]);
}
}

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $config['name'] ?? ''),
Expand All @@ -148,10 +153,8 @@ public function indexAction(): void
))->openInModal()
]);
$this->addControl($eventRuleForm);
$this->addControl($eventRuleConfigSubmitButton);
$this->addControl($discardChangesButton);
$this->addControl($deleteButton);

$this->addControl($buttonsWrapper);
$this->addContent($eventRuleConfig);
}

Expand Down Expand Up @@ -284,19 +287,10 @@ public function editAction(): void
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $config) {
$config['name'] = $form->getValue('name');
$config['is_active'] = $form->getValue('is_active');

$db = Database::get();
$params = [];
if ($ruleId === '-1') {
$params = $config;
} else {
$db->update('rule', [
'name' => $config['name'],
'timeperiod_id' => $config['timeperiod_id'] ?? null,
'object_filter' => $config['object_filter'] ?? null,
'is_active' => $config['is_active'] ?? 'n'
], ['id = ?' => $ruleId]);

$params['id'] = $ruleId;
}

Expand All @@ -307,6 +301,7 @@ public function editAction(): void
$this->sendExtraUpdates(['#col1']);
}

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

0 comments on commit e152bf2

Please sign in to comment.