Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Feb 21, 2024
1 parent 4ba49e7 commit 3775b8c
Show file tree
Hide file tree
Showing 10 changed files with 1,773 additions and 170 deletions.
258 changes: 149 additions & 109 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@

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\Forms\SaveEventRuleForm;
use Icinga\Module\Notifications\Model\Incident;
use Icinga\Module\Notifications\Model\ObjectExtraTag;
use Icinga\Module\Notifications\Model\Rule;
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
use Icinga\Module\Notifications\Widget\EventRuleConfig;
use Icinga\Web\Notification;
use Icinga\Module\Notifications\Web\Form\EventRuleDecorator;
use Icinga\Web\Session;
use ipl\Html\Attributes;
use ipl\Html\Form;
use ipl\Html\FormElement\ButtonElement;
use ipl\Html\FormElement\SubmitButtonElement;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Control\SearchEditor;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
Expand All @@ -32,6 +33,9 @@ class EventRuleController extends CompatController
/** @var Session\SessionNamespace */
private $sessionNamespace;

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

public function init()
{
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
Expand All @@ -42,71 +46,99 @@ public function indexAction(): void
$this->assertPermission('notifications/config/event-rules');

$this->addTitleTab(t('Event Rule'));

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

$ruleId = $this->params->getRequired('id');

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

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

View workflow job for this annotation

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

Cannot cast mixed to int.

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

View workflow job for this annotation

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

Cannot cast mixed to int.

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

View workflow job for this annotation

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

Cannot cast mixed to int.

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

View workflow job for this annotation

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

Cannot cast mixed to int.

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

View workflow job for this annotation

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

Cannot cast mixed to int.

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

View workflow job for this annotation

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

Cannot cast mixed to int.

if ($cache) {
$this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.')));
$eventRuleConfig = new EventRuleConfig(
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId]),
$cache
);
} else {
$eventRuleConfig = new EventRuleConfig(
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId]),
$this->fromDb($ruleId)
);
}

$disableRemoveButton = false;
if (ctype_digit($ruleId)) {
$incidents = Incident::on(Database::get())
->with('rule')
->filter(Filter::equal('rule.id', $ruleId));
$this->controls->addAttributes(['class' => 'event-rule-detail']);
$config = $this->sessionNamespace->get($ruleId);

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::get() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::get() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::get() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::get() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::get() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::get() expects string, int given.

if ($incidents->count() > 0) {
$disableRemoveButton = true;
}
if ($config === null) {
$config = $this->fromDb($ruleId);
$this->sessionNamespace->set($ruleId, $config);

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::set() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::set() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::set() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::set() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::set() expects string, int given.

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

View workflow job for this annotation

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

Parameter #1 $key of method Icinga\Web\Session\SessionNamespace::set() expects string, int given.
}

$saveForm = (new SaveEventRuleForm())
->setShowRemoveButton()
->setShowDismissChangesButton($cache !== null)
->setRemoveButtonDisabled($disableRemoveButton)
->setSubmitButtonDisabled($cache === null)
->setSubmitLabel($this->translate('Save Changes'))
->on(SaveEventRuleForm::ON_SUCCESS, function ($form) use ($ruleId, $eventRuleConfig) {
if ($form->getPressedSubmitElement()->getName() === 'discard_changes') {
$this->sessionNamespace->delete($ruleId);
Notification::success($this->translate('Successfully discarded the pending changes.'));
$this->redirectNow(Links::eventRule($ruleId));
}

if (! $eventRuleConfig->isValid()) {
$eventRuleConfig->addAttributes(['class' => 'invalid']);
return;
// $config = $this->fromDb($ruleId);
//
// if ($filter === '' && isset($config['object_filter'])) {
// $filter = $config['object_filter'];
// }

$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',
'formnovalidate' => true
]
))
->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));

// $eventRuleDecorator = new EventRuleDecorator();
// $eventRuleDecorator->decorate($eventRuleConfigSubmitButton);
// $eventRuleDecorator->decorate($discardChangesButton);

$eventRuleConfig = (new EventRuleConfigForm(
$config['object_filter'] ?? '',

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

View workflow job for this annotation

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

Ignored error pattern #^Cannot access offset 'object_filter' on mixed\.$# in path /home/runner/work/icinga-notifications-web/icinga-notifications-web/application/controllers/EventRuleController.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 101 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 class Icinga\Module\Notifications\Forms\EventRuleConfigForm constructor expects string, mixed given.

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

View workflow job for this annotation

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

Ignored error pattern #^Cannot access offset 'object_filter' on mixed\.$# in path /home/runner/work/icinga-notifications-web/icinga-notifications-web/application/controllers/EventRuleController.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 101 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 class Icinga\Module\Notifications\Forms\EventRuleConfigForm constructor expects string, mixed given.

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

View workflow job for this annotation

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

Ignored error pattern #^Cannot access offset 'object_filter' on mixed\.$# in path /home/runner/work/icinga-notifications-web/icinga-notifications-web/application/controllers/EventRuleController.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 101 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 class Icinga\Module\Notifications\Forms\EventRuleConfigForm constructor expects string, mixed given.

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

View workflow job for this annotation

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

Ignored error pattern #^Cannot access offset 'object_filter' on mixed\.$# in path /home/runner/work/icinga-notifications-web/icinga-notifications-web/application/controllers/EventRuleController.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 101 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 class Icinga\Module\Notifications\Forms\EventRuleConfigForm constructor expects string, mixed given.

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

View workflow job for this annotation

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

Ignored error pattern #^Cannot access offset 'object_filter' on mixed\.$# in path /home/runner/work/icinga-notifications-web/icinga-notifications-web/application/controllers/EventRuleController.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 101 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 class Icinga\Module\Notifications\Forms\EventRuleConfigForm constructor expects string, mixed given.

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

View workflow job for this annotation

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

Ignored error pattern #^Cannot access offset 'object_filter' on mixed\.$# in path /home/runner/work/icinga-notifications-web/icinga-notifications-web/application/controllers/EventRuleController.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 101 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 class Icinga\Module\Notifications\Forms\EventRuleConfigForm constructor expects string, mixed given.
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId])
))
->registerElement($eventRuleConfigSubmitButton)
->registerElement($discardChangesButton)
->registerElement($deleteButton);

$eventRuleConfig->populate($config);

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

View workflow job for this annotation

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

Parameter #1 $values of method Icinga\Module\Notifications\Forms\EventRuleConfigForm::populate() expects iterable<string, mixed>, mixed given.

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

View workflow job for this annotation

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

Parameter #1 $values of method Icinga\Module\Notifications\Forms\EventRuleConfigForm::populate() expects iterable<string, mixed>, mixed given.

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

View workflow job for this annotation

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

Parameter #1 $values of method Icinga\Module\Notifications\Forms\EventRuleConfigForm::populate() expects iterable<string, mixed>, mixed given.

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

View workflow job for this annotation

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

Parameter #1 $values of method Icinga\Module\Notifications\Forms\EventRuleConfigForm::populate() expects iterable<string, mixed>, mixed given.

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

View workflow job for this annotation

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

Parameter #1 $values of method Icinga\Module\Notifications\Forms\EventRuleConfigForm::populate() expects iterable<string, mixed>, mixed given.

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

View workflow job for this annotation

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

Parameter #1 $values of method Icinga\Module\Notifications\Forms\EventRuleConfigForm::populate() expects iterable<string, mixed>, mixed given.
$eventRuleConfig
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($deleteButton, $eventRuleConfigSubmitButton, $config) {

Check failure on line 110 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 $deleteButton.

Check failure on line 110 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 $deleteButton.

Check failure on line 110 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 $deleteButton.

Check failure on line 110 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 $deleteButton.

Check failure on line 110 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 $deleteButton.

Check failure on line 110 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 $deleteButton.
$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);
});

$form->editRule($ruleId, $this->sessionNamespace->get($ruleId));
$this->sessionNamespace->delete($ruleId);

Notification::success($this->translate('Successfully updated rule.'));
$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::eventRule($ruleId));
})->on(SaveEventRuleForm::ON_REMOVE, function ($form) use ($ruleId) {
$form->removeRule($ruleId);
$this->sessionNamespace->delete($ruleId);

Notification::success($this->translate('Successfully removed rule.'));
$this->redirectNow('__CLOSE__');
})->handleRequest($this->getServerRequest());
$eventRuleConfig->handleRequest($this->getServerRequest());

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $eventRuleConfig->getConfig()['name'] ?? ''),
Html::tag('h2', $config['name'] ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
Expand All @@ -115,30 +147,11 @@ public function indexAction(): void
['class' => 'control-button']
))->openInModal()
]);
$this->addControl($eventRuleForm);
$this->addControl($eventRuleConfigSubmitButton);
$this->addControl($discardChangesButton);
$this->addControl($deleteButton);

$eventRuleFormAndSave = Html::tag('div', ['class' => 'event-rule-and-save-forms']);
$eventRuleFormAndSave->add([
$eventRuleForm,
$saveForm
]);

$eventRuleConfig
->on(EventRuleConfig::ON_CHANGE, function ($eventRuleConfig) use ($ruleId, $saveForm) {
$this->sessionNamespace->set($ruleId, $eventRuleConfig->getConfig());
$saveForm->setSubmitButtonDisabled(false);
$this->redirectNow(Links::eventRule($ruleId));
});

foreach ($eventRuleConfig->getForms() as $form) {
$form->handleRequest($this->getServerRequest());

if (! $form->hasBeenSent()) {
// Force validation of populated values in case we display an unsaved rule
$form->validatePartial();
}
}

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

Expand Down Expand Up @@ -167,7 +180,7 @@ public function fromDb(int $ruleId): array
}

foreach ($re->rule_escalation_recipient as $recipient) {
$config[$re->getTableName()][$re->position]['recipient'][] = iterator_to_array($recipient);
$config[$re->getTableName()][$re->position]['recipients'][] = iterator_to_array($recipient);
}
}

Expand All @@ -188,7 +201,6 @@ public function completeAction(): void
$this->getDocument()->add($suggestions);
}


/**
* searchEditorAction for Object Extra Tags
*
Expand All @@ -200,13 +212,24 @@ public function searchEditorAction(): void
{
$ruleId = $this->params->shiftRequired('id');

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

$editor = EventRuleConfig::createSearchEditor()
->setQueryString($eventRule['object_filter'] ?? '');
if ($eventRule === null) {
$eventRule = $this->fromDb($ruleId);
}

$editor = new SearchEditor();

$editor->setQueryString($eventRule['object_filter'] ?? '');
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
$editor->setSuggestionUrl(Url::fromPath(
"notifications/event-rule/complete",
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
));

$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
$eventRule['object_filter'] = EventRuleConfig::createFilterString($form->getFilter());
$filter = self::createFilterString($form->getFilter());
$eventRule['object_filter'] = $filter;

$this->sessionNamespace->set($ruleId, $eventRule);
$this->getResponse()
Expand All @@ -225,45 +248,62 @@ public function searchEditorAction(): void
$this->setTitle($this->translate('Adjust Filter'));
}

public static function createFilterString($filters): string
{
foreach ($filters as $filter) {
if ($filter instanceof Filter\Chain) {
self::createFilterString($filter);
} elseif (empty($filter->getValue())) {
$filter->setValue(true);
}
}

if ($filters instanceof Filter\Condition && empty($filters->getValue())) {
$filters->setValue(true);
}

$filterStr = QueryString::render($filters);

return ! empty($filterStr) ? $filterStr : '';
}

public function editAction(): void
{
/** @var string $ruleId */
$ruleId = $this->params->getRequired('id');
/** @var ?array<string, mixed> $cache */
$cache = $this->sessionNamespace->get($ruleId);

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

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

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

if ($cache || $ruleId === '-1') {
$this->sessionNamespace->set($ruleId, $config);
$db = Database::get();
$params = [];
if ($ruleId === '-1') {
$params = $config;
} else {
(new SaveEventRuleForm())->editRule((int) $ruleId, $config);
$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;
}

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

Expand Down
Loading

0 comments on commit 3775b8c

Please sign in to comment.