Skip to content

Commit f6576f7

Browse files
christoph2497Thomas-Gelf
authored andcommitted
add seperate form for drop and save changes, add warning if session has been modified
1 parent 5ebb069 commit f6576f7

File tree

7 files changed

+126
-49
lines changed

7 files changed

+126
-49
lines changed

application/controllers/ConfigurationController.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace Icinga\Module\Eventtracker\Controllers;
44

5-
use ECSPrefix202509\React\Dns\Model\Message;
65
use Exception;
76
use gipfl\IcingaWeb2\Link;
87
use gipfl\IcingaWeb2\Url;
9-
use gipfl\Json\JsonDecodeException;
108
use gipfl\Json\JsonString;
11-
use gipfl\Web\InlineForm;
129
use gipfl\Web\Widget\Hint;
1310
use gipfl\ZfDbStore\DbStorableInterface;
1411
use gipfl\ZfDbStore\NotFoundError;
@@ -17,14 +14,13 @@
1714
use Icinga\Module\Eventtracker\Db\ConfigStore;
1815
use Icinga\Module\Eventtracker\Engine\Downtime\DowntimeRule;
1916
use Icinga\Module\Eventtracker\Engine\Input\KafkaInput;
20-
use Icinga\Module\Eventtracker\Modifier\Modifier;
21-
use Icinga\Module\Eventtracker\Modifier\ModifierChain;
22-
use Icinga\Module\Eventtracker\Modifier\ModifierRegistry;
2317
use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore;
2418
use Icinga\Module\Eventtracker\Modifier\Settings;
2519
use Icinga\Module\Eventtracker\Web\Dashboard\ConfigurationDashboard;
2620
use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm;
2721
use Icinga\Module\Eventtracker\Web\Form\DowntimeForm;
22+
use Icinga\Module\Eventtracker\Web\Form\DropChannelRuleChangesForm;
23+
use Icinga\Module\Eventtracker\Web\Form\SaveChannelRulesChangesForm;
2824
use Icinga\Module\Eventtracker\Web\Form\SimulateRuleForm;
2925
use Icinga\Module\Eventtracker\Web\Form\UuidObjectForm;
3026
use Icinga\Module\Eventtracker\Web\Table\BaseTable;
@@ -189,6 +185,12 @@ public function channelrulesAction()
189185

190186
$form = $this->getForm($action); // TODO: w/o form
191187
$modifierRuleStore = new ModifierRuleStore($ns, $uuid, $form);
188+
if ($modifierRuleStore->saveWouldDestroyOtherChanges()) {
189+
$this->content()->add(Hint::warning($this->translate(
190+
'The configuration for this rules has been changed since you started your modifications.'
191+
. ' Be careful, you\'ll override those other changes, in case you save yours.'
192+
)));
193+
}
192194
if ($this->params->get('action') === 'add') {
193195
$ruleForm = new ChannelRuleForm($modifierRuleStore);
194196
$ruleForm->on($ruleForm::ON_SUCCESS, function () use ($ruleForm, $modifierRuleStore) {
@@ -225,7 +227,6 @@ public function channelrulesAction()
225227
$this->content()->add($simulationForm);
226228
}
227229
if ($this->params->get('action') === 'edit') {
228-
var_dump("foobar");
229230
$ruleForm = new ChannelRuleForm($modifierRuleStore);
230231
$ruleForm->editRow(
231232
$this->url()->getParam('row'),
@@ -696,34 +697,27 @@ protected function showRules(
696697

697698
if ($modifierRuleStore->getSessionRules() !== null) {
698699
if ($modifierRuleStore->hasBeenModified()) {
699-
$newForm = new InlineForm();
700-
$newForm->on($newForm::ON_SUCCESS, function () use ($form, $modifierRuleStore) {
701-
$form->populate(['rules' => JsonString::encode($modifierRuleStore->getRules())]);
702-
$form->storeObject();
703-
$modifierRuleStore->deleteSessionRules();
704-
$this->redirectNow($this->url());
705-
});
706-
$newForm->handleRequest($this->getServerRequest());
707-
$newForm->addElement('submit', 'submit', ['label' => $this->translate('Save')]);
700+
$saveForm = new SaveChannelRulesChangesForm($modifierRuleStore);
701+
$saveForm->on($saveForm::ON_SUCCESS, fn () => $this->redirectNow($this->url()));
702+
$saveForm->handleRequest($this->getServerRequest());
703+
704+
$dropForm = new DropChannelRuleChangesForm($modifierRuleStore);
705+
$dropForm->on($dropForm::ON_SUCCESS, fn () => $this->redirectNow($this->url()));
706+
$dropForm->handleRequest($this->getServerRequest());
707+
708708
$message = Hint::warning(
709709
'The order has been modified please safe if you want to preserver the new order'
710710
);
711711

712-
//if ($newForm->hasBeenSubmitted()) {
713-
// $form->populate(['rules' => JsonString::encode($modifierRuleStore->getRules())]);
714-
// $form->storeObject();
715-
// $modifierRuleStore->deleteSessionRules();
716-
// $this->redirectNow($this->url());
717-
//}
718-
$this->content()->add([$message, $newForm]);
712+
$this->content()->add([$message, $saveForm, $dropForm]);
719713
}
720714
}
721715
if ($modifierRuleStore->getRules() === null) {
722716
return;
723717
}
724718
$modifiers = $modifierRuleStore->getRules();
725719
$url = Url::fromPath('eventtracker/configuration/channelrule', [
726-
'uuid' => $uuid->toString()
720+
'uuid' => $uuid->toString(),
727721
]);
728722
try {
729723
if ($sampleObject = $ns->get('simulationObject/'. $uuid->toString())) {

library/Eventtracker/Modifier/ModifierRuleStore.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ class ModifierRuleStore
1111
protected ModifierChain $rules;
1212
protected $uuid;
1313
protected $form;
14-
protected $sessionKey;
14+
protected string $sessionKey;
15+
protected string $sessionKeyStoredChecksum;
1516
public function __construct($ns, $uuid, $form)
1617
{
1718
$this->ns = $ns;
1819
$this->sessionKey = 'channelrules/' . $uuid->toString();
20+
$this->sessionKeyStoredChecksum = $this->sessionKey . '/formerChecksum';
1921
$this->form = $form;
2022
}
2123

@@ -32,11 +34,21 @@ public function hasBeenModified(): bool
3234
return false;
3335
}
3436

37+
public function saveWouldDestroyOtherChanges(): bool
38+
{
39+
if ($checksum = $this->ns->get($this->sessionKeyStoredChecksum)) {
40+
return $checksum !== $this->getStoredRules()->getShortChecksum();
41+
}
42+
43+
return false;
44+
}
45+
3546
public function setModifierRules(ModifierChain $rules)
3647
{
3748
$this->rules = $rules;
3849
$this->ns->set($this->sessionKey, JsonString::encode($rules));
3950
}
51+
4052
public function getSessionRules(): ?ModifierChain
4153
{
4254
$rules = $this->ns->get($this->sessionKey);
@@ -54,6 +66,7 @@ public function getSessionRules(): ?ModifierChain
5466
public function deleteSessionRules()
5567
{
5668
$this->ns->delete($this->sessionKey);
69+
$this->ns->delete($this->sessionKeyStoredChecksum);
5770
}
5871

5972
public function getStoredRules(): ModifierChain
@@ -64,7 +77,19 @@ public function getStoredRules(): ModifierChain
6477
} catch (JsonDecodeException $e) {
6578
$modifier = [];
6679
}
67-
return ModifierChain::fromSerialization($modifier);
80+
$chain = ModifierChain::fromSerialization($modifier);
81+
if (null === $this->ns->get($this->sessionKeyStoredChecksum)) {
82+
$this->ns->set($this->sessionKeyStoredChecksum, $chain->getShortChecksum());
83+
}
84+
85+
return $chain;
86+
}
87+
88+
public function storeRules(): void
89+
{
90+
$this->form->populate(['rules' => JsonString::encode($this->getRules())]);
91+
$this->form->storeObject();
92+
$this->deleteSessionRules();
6893
}
6994

7095
public function getRules(): ModifierChain

library/Eventtracker/Web/Form/ChannelRuleForm.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
namespace Icinga\Module\Eventtracker\Web\Form;
44

5+
use Exception;
56
use gipfl\Translation\TranslationHelper;
67
use gipfl\Web\Form;
7-
use gipfl\Web\InlineForm;
8-
use gipfl\Web\Widget\Hint;
9-
use Icinga\Chart\Inline\Inline;
8+
use gipfl\Web\Form\Decorator\DdDtDecorator;
109
use Icinga\Module\Eventtracker\Modifier\Modifier;
1110
use Icinga\Module\Eventtracker\Modifier\ModifierChain;
1211
use Icinga\Module\Eventtracker\Modifier\ModifierRegistry;
1312
use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore;
1413
use Icinga\Module\Eventtracker\Modifier\Settings;
15-
use Icinga\Web\Form\Decorator\Autosubmit;
16-
use Icinga\Web\Notification;
1714
use ipl\Html\FormElement\SubmitElement;
18-
use function PHPUnit\Framework\isEmpty;
19-
use function PHPUnit\Framework\throwException;
15+
use RuntimeException;
2016

2117
class ChannelRuleForm extends Form
2218
{
@@ -37,7 +33,7 @@ public function __construct(ModifierRuleStore $modifierRuleStore)
3733
public function getModifier(): Modifier
3834
{
3935
if ($this->modifier === null) {
40-
throw new \RuntimeException('Form has no Modifier');
36+
throw new RuntimeException('Form has no Modifier');
4137
}
4238

4339
return $this->modifier;
@@ -51,8 +47,10 @@ public function editRow(int $row, string $compareChecksum)
5147
$this->populate(['modifyProperty' => $modifier[0], 'modifierImplementation' => $modifier[1]->getName()]);
5248
$this->populate((array) $modifier[1]->getSettings()->jsonSerialize());
5349
} else {
54-
throw new \Exception('Checksum doesn\'t not match checksum from url: '
55-
. $compareChecksum . ' != ' . $this->modifierChain->getShortChecksum());
50+
throw new Exception(
51+
'Checksum doesn\'t not match checksum from url: '
52+
. $compareChecksum . ' != ' . $this->modifierChain->getShortChecksum()
53+
);
5654
}
5755
}
5856

@@ -96,7 +94,7 @@ public function onSuccess()
9694
$class = ModifierRegistry::getClassName($this->getValue('modifierImplementation'));
9795
$this->modifier = new $class(Settings::fromSerialization($this->getValues()));
9896

99-
if ($this->row) {
97+
if ($this->row !== null) {
10098
$this->modifierChain->replaceModifier($this->modifier, $this->getPropertyName(), $this->row);
10199
$this->modifierRuleStore->setModifierRules($this->modifierChain);
102100
}
@@ -110,11 +108,9 @@ protected function addCancelButton()
110108
assert($button instanceof SubmitElement);
111109
$this->cancelButton = $button;
112110
$submit = $this->getElement('submit');
113-
assert($submit instanceof SubmitElement);
114111
$decorator = $submit->getWrapper();
115-
assert($decorator instanceof Form\Decorator\DdDtDecorator);
116-
$dd = $decorator->dd();
117-
$dd->add($button);
112+
assert($decorator instanceof DdDtDecorator);
113+
$decorator->dd()->add($button);
118114
$this->registerElement($button);
119115
}
120116

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Icinga\Module\Eventtracker\Web\Form;
4+
5+
use gipfl\Translation\TranslationHelper;
6+
use gipfl\Web\Form\Feature\NextConfirmCancel;
7+
use gipfl\Web\InlineForm;
8+
use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore;
9+
10+
class DropChannelRuleChangesForm extends InlineForm
11+
{
12+
use TranslationHelper;
13+
14+
private ModifierRuleStore $ruleStore;
15+
16+
public function __construct(ModifierRuleStore $ruleStore)
17+
{
18+
$this->ruleStore = $ruleStore;
19+
}
20+
21+
protected function assemble()
22+
{
23+
(new NextConfirmCancel(
24+
NextConfirmCancel::buttonNext($this->translate('Forget my changes'), [
25+
'title' => $this->translate('Click to drop all unsaved changes applied to this channel'),
26+
]),
27+
NextConfirmCancel::buttonConfirm($this->translate('Yes, please drop my changes')),
28+
NextConfirmCancel::buttonCancel($this->translate('Cancel'))
29+
))->addToForm($this);
30+
}
31+
32+
protected function onSuccess()
33+
{
34+
$this->ruleStore->deleteSessionRules();
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Icinga\Module\Eventtracker\Web\Form;
4+
5+
use gipfl\Translation\TranslationHelper;
6+
use gipfl\Web\InlineForm;
7+
use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore;
8+
9+
class SaveChannelRulesChangesForm extends InlineForm
10+
{
11+
use TranslationHelper;
12+
13+
private ModifierRuleStore $ruleStore;
14+
15+
public function __construct(ModifierRuleStore $ruleStore)
16+
{
17+
$this->ruleStore = $ruleStore;
18+
}
19+
20+
protected function assemble()
21+
{
22+
$this->addElement('submit', 'submit', ['label' => $this->translate('Save')]);
23+
}
24+
25+
protected function onSuccess()
26+
{
27+
$this->ruleStore->storeRules();
28+
}
29+
}

library/Eventtracker/Web/Form/SimulateRuleForm.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
use gipfl\Web\Form;
66
use gipfl\Translation\TranslationHelper;
7+
use gipfl\Web\Form\Decorator\DdDtDecorator;
78
use Icinga\Module\Eventtracker\SampleObject;
89
use ipl\Html\FormElement\SubmitElement;
910

1011
class SimulateRuleForm extends Form
1112
{
1213
use TranslationHelper;
1314
protected $ns;
14-
protected $sessionKey;
15+
protected String $sessionKey;
16+
protected ?SubmitElement $cancelButton = null;
1517

1618
public function __construct($ns, $sessionKey)
1719
{
@@ -52,7 +54,7 @@ protected function addCancelButton()
5254
$submit = $this->getElement('submit');
5355
assert($submit instanceof SubmitElement);
5456
$decorator = $submit->getWrapper();
55-
assert($decorator instanceof Form\Decorator\DdDtDecorator);
57+
assert($decorator instanceof DdDtDecorator);
5658
$dd = $decorator->dd();
5759
$dd->add($button);
5860
$this->registerElement($button);

library/Eventtracker/Web/Table/ChannelRulesTable.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,19 @@
88
use gipfl\IcingaWeb2\IconHelper;
99
use gipfl\IcingaWeb2\Link;
1010
use gipfl\IcingaWeb2\Url;
11-
use gipfl\Json\JsonString;
1211
use gipfl\Translation\TranslationHelper;
1312
use gipfl\Web\Form\Feature\NextConfirmCancel;
1413
use gipfl\Web\InlineForm;
1514
use gipfl\Web\Widget\Hint;
1615
use Icinga\Module\Eventtracker\Data\PlainObjectRenderer;
1716
use Icinga\Module\Eventtracker\Modifier\ModifierChain;
18-
use Icinga\Module\Eventtracker\Modifier\ModifierRegistry;
1917
use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore;
2018
use Icinga\Module\Eventtracker\Web\Form\ChannelConfigForm;
2119
use Icinga\Module\Eventtracker\Web\Form\InstanceInlineForm;
22-
use Icinga\Module\Eventtracker\Web\WebActions;
23-
use Icinga\Web\Form\Element\Button;
2420
use ipl\Html\Html;
2521
use ipl\Html\Table;
26-
use ipl\Web\Widget\ActionLink;
27-
use ipl\Web\Widget\ButtonLink;
2822
use Psr\Http\Message\ServerRequestInterface;
23+
use function PHPUnit\Framework\equalTo;
2924

3025
class ChannelRulesTable extends Table
3126
{

0 commit comments

Comments
 (0)