Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Apr 10, 2024
1 parent 375cdd7 commit dd2c1a5
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 143 deletions.
49 changes: 26 additions & 23 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,40 @@ public function indexAction(): void
$ruleId = $this->params->getRequired('id');
$this->controls->addAttributes(['class' => 'event-rule-detail']);

$disableSave = false;
$configValues = $this->fromDb((int) $ruleId);
/** @var array<string, string> $configFilter */
$configFilter = $this->getRequest()->get('config-filter');
if ($this->getRequest()->has('searchbar')) {
$configValues['object_filter'] = $this->getRequest()->get('searchbar');
} elseif ($configFilter !== null) {
if (isset($configFilter['show-searchbar']) && $configFilter['show-searchbar'] === '0') {
$configValues['object_filter'] = '';
$eventRuleConfigValues = $this->fromDb((int) $ruleId);

if ($this->getRequest()->isPost()) {
if ($this->getRequest()->has('searchbar')) {
$eventRuleConfigValues['object_filter'] = $this->getRequest()->get('searchbar');
} else {
$eventRuleConfigValues['object_filter'] = '';
}
}

$eventRuleConfig = (new EventRuleConfigForm(
$configValues,
Url::fromPath(
'notifications/event-rule/search-editor',
['id' => $ruleId, 'object_filter' => $configValues['object_filter']]
['id' => $ruleId, 'object_filter' => $eventRuleConfigValues['object_filter']]
)
))->populate($configValues);
$eventRuleConfig
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$form->addOrUpdateRule($ruleId, $configValues);
Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name'])));
))
->populate($eventRuleConfigValues)
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
$config = $form->getValues();
$config['object_filter'] = $eventRuleConfigValues['object_filter'];
$form->addOrUpdateRule($ruleId, $config);
Notification::success((sprintf(t('Successfully saved event rule %s'), $eventRuleConfigValues['name'])));
$this->redirectNow(Links::eventRule((int) $ruleId));
})
->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
$form->removeRule((int) $ruleId);
Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name']));
$this->redirectNow('__CLOSE__');
})
->on(
EventRuleConfigForm::ON_DELETE,
function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
$form->removeRule((int) $ruleId);
Notification::success(
sprintf(t('Successfully deleted event rule %s'), $eventRuleConfigValues['name'])
);
$this->redirectNow('__CLOSE__');
}
)
->handleRequest($this->getServerRequest());

$buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]));
Expand All @@ -84,7 +88,6 @@ public function indexAction(): void
[
'label' => t('Save'),
'form' => 'event-rule-config-form',
'disabled' => $disableSave
]
));
$deleteButton = (new SubmitButtonElement(
Expand Down Expand Up @@ -112,7 +115,7 @@ public function indexAction(): void
}

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [
Html::tag('h2', $configValues['name'] ?? ''),
Html::tag('h2', $eventRuleConfigValues['name'] ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
Expand Down
56 changes: 17 additions & 39 deletions application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Notifications\Widget\ItemList\EventRuleList;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use ipl\Html\Attributes;
use ipl\Html\Form;
use ipl\Html\FormElement\SubmitButtonElement;
Expand All @@ -34,20 +33,14 @@ class EventRulesController extends CompatController
/** @var Filter\Rule Filter from query string parameters */
private $filter;

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

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

public function indexAction(): void
{
$eventRules = Rule::on(Database::get());
$this->sessionNamespace->delete('-1');

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($eventRules);
$sortControl = $this->createSortControl(
Expand Down Expand Up @@ -109,25 +102,15 @@ public function addAction(): void
$this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy']));

$this->controls->addAttributes(['class' => 'event-rule-detail']);
/** @var string $ruleId */
$ruleId = $this->params->get('id') ?? '-1';

$params = $this->params->toArray(false);
/** @var array<string, mixed>|null $config */
$config = $this->sessionNamespace->get($ruleId);
/** @var array<string, string> $eventRule */
$eventRule = $this->params->toArray(false);
$ruleId = $eventRule['id'] ?? '-1';

if ($config === null) {
/** @var array<string, mixed> $config */
$config = $params;
}

/** @var array<string, string> $configFilter */
$configFilter = $this->getRequest()->get('config-filter');
if ($this->getRequest()->has('searchbar')) {
$config['object_filter'] = $this->getRequest()->get('searchbar');
} elseif ($configFilter !== null) {
if (isset($configFilter['show-searchbar']) && $configFilter['show-searchbar'] === '0') {
$config['object_filter'] = '';
if ($this->getRequest()->isPost()) {
if ($this->getRequest()->has('searchbar')) {
$eventRule['object_filter'] = $this->getRequest()->get('searchbar');
} else {
$eventRule['object_filter'] = '';
}
}

Expand All @@ -141,34 +124,29 @@ public function addAction(): void
))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));

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

$eventRuleConfig
->on(Form::ON_SENT, function (Form $form) use ($config) {
$config = array_merge($config, $form->getValues());
$this->sessionNamespace->set('-1', $config);
})
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) {
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($eventRule) {
/** @var string $ruleId */
$ruleId = $config['id'];
$ruleId = $eventRule['id'];
/** @var string $ruleName */
$ruleName = $config['name'];
$form->addOrUpdateRule($ruleId, $config);
$this->sessionNamespace->delete($ruleId);
$ruleName = $eventRule['name'];
$eventRuleConfig = array_merge($eventRule, $form->getValues());
$form->addOrUpdateRule($ruleId, $eventRuleConfig);
Notification::success(sprintf(t('Successfully add event rule %s'), $ruleName));
$this->redirectNow('__CLOSE__');
})
->handleRequest($this->getServerRequest());

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $config['name'] ?? ''),
Html::tag('h2', $eventRule['name'] ?? ''),
Html::tag(
'div',
[
Expand Down Expand Up @@ -233,7 +211,7 @@ public function searchEditorAction(): void
$editor->setSuggestionUrl(
Url::fromPath(
"notifications/event-rule/complete",
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId]
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class EscalationCondition extends FieldsetElement
/** @var bool Whether zero conditions allowed */
public $allowZeroConditions;

/** @var int Number of conditions */
public $count = 0;

/**
* Set whether the zero conditions is allowed for the escalation
*
Expand Down Expand Up @@ -110,7 +107,7 @@ protected function assemble(): void
'select',
$opName,
[
'class' => ['class' => 'operator-input', 'autosubmit'],
'class' => ['class' => 'operator-input'],
'options' => array_combine($operators, $operators),
'required' => true
]
Expand All @@ -123,7 +120,7 @@ protected function assemble(): void
'select',
$valName,
[
'class' => ['autosubmit', 'right-operand'],
'class' => ['right-operand'],
'options' => [
'ok' => $this->translate('Ok', 'notification.severity'),
'debug' => $this->translate('Debug', 'notification.severity'),
Expand Down Expand Up @@ -158,7 +155,7 @@ protected function assemble(): void
$valName,
[
'required' => true,
'class' => ['autosubmit', 'right-operand'],
'class' => ['right-operand'],
'validators' => [
new CallbackValidator(function ($value, $validator) {
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
Expand Down
22 changes: 7 additions & 15 deletions application/forms/EventRuleConfigElements/EscalationRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function assemble(): void
'select',
'column_' . $i,
[
'class' => ['autosubmit', 'left-operand'],
'class' => ['left-operand', 'autosubmit'],
'options' => [
'' => sprintf(' - %s - ', $this->translate('Please choose'))
] + $this->fetchOptions(),
Expand All @@ -73,6 +73,9 @@ protected function assemble(): void
);

$this->registerElement($col);
/** @var string $recipientVal */
$recipientVal = $this->getValue('column_' . $i) ?? '';
$recipient = explode('_', $recipientVal);

$options = ['' => sprintf(' - %s - ', $this->translate('Please choose'))];
$options += Channel::fetchChannelNames(Database::get());
Expand All @@ -82,7 +85,7 @@ protected function assemble(): void
'select',
'val_' . $i,
[
'class' => ['autosubmit', 'right-operand'],
'class' => ['right-operand'],
'options' => $options,
'disabledOptions' => [''],
'value' => $this->getPopulatedValue('val_' . $i)
Expand All @@ -97,21 +100,10 @@ protected function assemble(): void
$options[''] = $this->translate('Default User Channel');

$val->setOptions($options);

$val->setDisabledOptions([]);

if ($this->getPopulatedValue('val_' . $i, '') === '') {
$val->addAttributes(['class' => 'default-channel']);
}
} else {
$val->addAttributes(['required' => true]);
}
} else {
/** @var BaseFormElement $val */
$val = $this->createElement('text', 'val_' . $i, [
'class' => 'right-operand',
'placeholder' => $this->translate('Please make a decision'),
'disabled' => true,
'value' => $this->getPopulatedValue('val_' . $i)
]);
}

$this->registerElement($val);
Expand Down
Loading

0 comments on commit dd2c1a5

Please sign in to comment.