Skip to content

Commit 900354c

Browse files
authored
Use new decorators in forms (#379)
2 parents a67c699 + eff705e commit 900354c

File tree

4 files changed

+86
-84
lines changed

4 files changed

+86
-84
lines changed

application/forms/ContactGroupForm.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Icinga\Module\Notifications\Model\Contactgroup;
1212
use Icinga\Module\Notifications\Model\Rotation;
1313
use Icinga\Module\Notifications\Model\RotationMember;
14-
use Icinga\Module\Notifications\Model\RuleEscalation;
1514
use Icinga\Module\Notifications\Model\RuleEscalationRecipient;
1615
use Icinga\Web\Session;
1716
use ipl\Html\FormElement\SubmitElement;
@@ -21,6 +20,7 @@
2120
use ipl\Stdlib\Filter;
2221
use ipl\Web\Common\CsrfCounterMeasure;
2322
use ipl\Web\Compat\CompatForm;
23+
use ipl\Web\FormDecorator\IcingaFormDecorator;
2424
use ipl\Web\FormElement\TermInput;
2525
use ipl\Web\FormElement\TermInput\Term;
2626

@@ -29,19 +29,20 @@ class ContactGroupForm extends CompatForm
2929
use CsrfCounterMeasure;
3030

3131
/** @var Connection */
32-
private $db;
32+
private Connection $db;
3333

3434
/** @var ?int Contact group id */
35-
private $contactgroupId;
35+
private ?int $contactgroupId = null;
3636

3737
public function __construct(Connection $db)
3838
{
3939
$this->db = $db;
40+
$this->applyDefaultElementDecorators();
4041
}
4142

4243
protected function assemble(): void
4344
{
44-
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
45+
$this->addCsrfCounterMeasure(Session::getSession()->getId());
4546

4647
$callValidation = function (array $terms) {
4748
$this->validateTerms($terms);
@@ -63,6 +64,11 @@ protected function assemble(): void
6364
->on(TermInput::ON_SAVE, $callValidation)
6465
->on(TermInput::ON_PASTE, $callValidation);
6566

67+
// TODO: TermInput is not compatible with the new decorators yet: https://github.com/Icinga/ipl-web/pull/317
68+
$legacyDecorator = new IcingaFormDecorator();
69+
$termInput->setDefaultElementDecorator($legacyDecorator);
70+
$legacyDecorator->decorate($termInput);
71+
6672
$this->addElement(
6773
'text',
6874
'group_name',

application/forms/EventRuleConfigForm.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
use Icinga\Module\Notifications\Model\Rule;
1212
use Icinga\Module\Notifications\Model\RuleEscalation;
1313
use ipl\Html\Attributes;
14-
use ipl\Html\Form;
1514
use ipl\Html\FormElement\SubmitButtonElement;
1615
use ipl\Html\HtmlElement;
1716
use ipl\Html\ValidHtml;
1817
use ipl\I18n\Translation;
1918
use ipl\Sql\Connection;
2019
use ipl\Web\Common\CsrfCounterMeasure;
20+
use ipl\Web\Compat\CompatForm;
2121
use ipl\Web\Url;
2222
use ipl\Web\Widget\Icon;
2323
use ipl\Web\Widget\Link;
2424

25-
class EventRuleConfigForm extends Form
25+
class EventRuleConfigForm extends CompatForm
2626
{
2727
use CsrfCounterMeasure;
2828
use Translation;
@@ -49,6 +49,9 @@ public function __construct(ConfigProviderInterface $configProvider, Url $search
4949
{
5050
$this->configProvider = $configProvider;
5151
$this->searchEditorUrl = $searchEditorUrl;
52+
53+
$this->addElementLoader('Icinga\\Module\\Notifications\\Forms\\EventRuleConfigElements');
54+
$this->applyDefaultElementDecorators();
5255
}
5356

5457
public function hasBeenSubmitted(): bool
@@ -96,11 +99,11 @@ protected function assemble(): void
9699
new HtmlElement('div', Attributes::create(['class' => 'connector-line']))
97100
);
98101

99-
$escalations = new EventRuleConfigElements\Escalations('escalations', [
100-
'provider' => $this->configProvider,
101-
'required' => true
102+
$this->addElement('escalations', 'escalations', [
103+
'decorators' => [],
104+
'provider' => $this->configProvider,
105+
'required' => true
102106
]);
103-
$this->addElement($escalations);
104107

105108
$this->addElement('hidden', 'id', ['required' => true]);
106109

application/forms/ScheduleForm.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@ class ScheduleForm extends CompatForm
2323
{
2424
use CsrfCounterMeasure;
2525

26-
/** @var string */
27-
protected $submitLabel;
26+
/** @var ?string */
27+
protected ?string $submitLabel;
2828

2929
/** @var bool */
30-
protected $showRemoveButton = false;
30+
protected bool $showRemoveButton = false;
3131

3232
/** @var Connection */
33-
private $db;
33+
private Connection $db;
3434

3535
/** @var ?int */
36-
private $scheduleId;
36+
private ?int $scheduleId = null;
3737

3838
public function __construct(Connection $db)
3939
{
4040
$this->db = $db;
41+
$this->applyDefaultElementDecorators();
4142
}
4243

4344
public function setSubmitLabel(string $label): self
@@ -155,7 +156,7 @@ public function removeSchedule(int $id): void
155156
$this->db->commitTransaction();
156157
}
157158

158-
protected function assemble()
159+
protected function assemble(): void
159160
{
160161
if (! $this->showRemoveButton) {
161162
$this->addHtml(new HtmlElement(
@@ -190,7 +191,7 @@ protected function assemble()
190191
$this->getElement('submit')->prependWrapper((new HtmlDocument())->setHtmlContent($removeBtn));
191192
}
192193

193-
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
194+
$this->addCsrfCounterMeasure(Session::getSession()->getId());
194195
}
195196

196197
/**

library/Notifications/Web/Form/ContactForm.php

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
use Icinga\Web\Session;
1616
use ipl\Html\Attributes;
1717
use ipl\Html\Contract\FormSubmitElement;
18-
use ipl\Html\FormElement\FieldsetElement;
18+
use ipl\Html\FormDecoration\DescriptionDecorator;
19+
use ipl\Html\HtmlDocument;
1920
use ipl\Html\HtmlElement;
2021
use ipl\Html\Text;
2122
use ipl\Sql\Connection;
@@ -25,7 +26,6 @@
2526
use ipl\Validator\StringLengthValidator;
2627
use ipl\Web\Common\CsrfCounterMeasure;
2728
use ipl\Web\Compat\CompatForm;
28-
use ipl\Web\FormElement\SuggestionElement;
2929
use ipl\Web\Url;
3030

3131
class ContactForm extends CompatForm
@@ -44,6 +44,7 @@ class ContactForm extends CompatForm
4444
public function __construct(Connection $db)
4545
{
4646
$this->db = $db;
47+
$this->applyDefaultElementDecorators();
4748

4849
$this->on(self::ON_SENT, function () {
4950
if ($this->hasBeenRemoved()) {
@@ -80,14 +81,8 @@ protected function assemble()
8081
$this->addCsrfCounterMeasure(Session::getSession()->getId());
8182

8283
// Fieldset for contact full name and username
83-
$contact = (new FieldsetElement(
84-
'contact',
85-
[
86-
'label' => $this->translate('Contact'),
87-
]
88-
));
89-
90-
$this->addElement($contact);
84+
$this->addElement('fieldset', 'contact', ['label' => $this->translate('Contact')]);
85+
$contact = $this->getElement('contact');
9186

9287
$contact->addElement(
9388
'text',
@@ -98,49 +93,48 @@ protected function assemble()
9893
]
9994
);
10095

96+
// TODO: remove this once https://github.com/Icinga/ipl-html/issues/178 is fixed
97+
$contact->addElementLoader('ipl\\Web\\FormElement', 'Element');
98+
99+
$contact->addElement(
100+
'suggestion',
101+
'username',
102+
[
103+
'label' => $this->translate('Icinga Web User'),
104+
'description' => $this->translate(
105+
'Use this to associate actions in the UI, such as incident management, with this contact.'
106+
. ' To successfully receive desktop notifications, this is also required.'
107+
),
108+
'suggestionsUrl' => Url::fromPath(
109+
'notifications/contact/suggest-icinga-web-user',
110+
['showCompact' => true, '_disableLayout' => 1]
111+
),
112+
'validators' => [
113+
new StringLengthValidator(['max' => 254]),
114+
new CallbackValidator(function ($value, $validator) {
115+
$contact = Contact::on($this->db)
116+
->filter(Filter::equal('username', $value));
117+
if ($this->contactId) {
118+
$contact->filter(Filter::unequal('id', $this->contactId));
119+
}
120+
121+
if ($contact->first() !== null) {
122+
$validator->addMessage($this->translate(
123+
'A contact with the same username already exists.'
124+
));
125+
126+
return false;
127+
}
128+
129+
return true;
130+
})
131+
]
132+
]
133+
);
101134
$contact
102-
->addElement(
103-
new SuggestionElement(
104-
'username',
105-
Url::fromPath(
106-
'notifications/contact/suggest-icinga-web-user',
107-
['showCompact' => true, '_disableLayout' => 1]
108-
),
109-
[
110-
'label' => $this->translate('Icinga Web User'),
111-
'validators' => [
112-
new StringLengthValidator(['max' => 254]),
113-
new CallbackValidator(function ($value, $validator) {
114-
$contact = Contact::on($this->db)
115-
->filter(Filter::equal('username', $value));
116-
if ($this->contactId) {
117-
$contact->filter(Filter::unequal('id', $this->contactId));
118-
}
119-
120-
if ($contact->first() !== null) {
121-
$validator->addMessage($this->translate(
122-
'A contact with the same username already exists.'
123-
));
124-
125-
return false;
126-
}
127-
128-
return true;
129-
})
130-
]
131-
]
132-
)
133-
)
134-
->addHtml(
135-
new HtmlElement(
136-
'p',
137-
new Attributes(['class' => 'description']),
138-
new Text($this->translate(
139-
'Use this to associate actions in the UI, such as incident management, with this contact.'
140-
. ' To successfully receive desktop notifications, this is also required.'
141-
))
142-
)
143-
);
135+
->getElement('username')
136+
->getDecorators()
137+
->replaceDecorator('Description', DescriptionDecorator::class, ['class' => 'description']);
144138

145139
$channelQuery = Channel::on($this->db)
146140
->columns(['id', 'name', 'type']);
@@ -157,6 +151,10 @@ protected function assemble()
157151
'default_channel_id',
158152
[
159153
'label' => $this->translate('Default Channel'),
154+
'description' => $this->translate(
155+
"Contact will be notified via the default channel, when no specific channel is configured"
156+
. " in an event rule."
157+
),
160158
'required' => true,
161159
'class' => 'autosubmit',
162160
'disabledOptions' => [''],
@@ -166,22 +164,18 @@ protected function assemble()
166164
]
167165
);
168166

167+
$defaultChannel
168+
->getDecorators()
169+
->replaceDecorator('Description', DescriptionDecorator::class, ['class' => 'description']);
170+
$this->decorate($defaultChannel);
171+
169172
$contact->registerElement($defaultChannel);
170173

171174
$this->addAddressElements($channelTypes[$defaultChannel->getValue()] ?? null);
172175

173176
$this->addHtml(new HtmlElement('hr'));
174177

175-
$this->decorate($defaultChannel);
176178
$this->addHtml($defaultChannel);
177-
$this->addHtml(new HtmlElement(
178-
'p',
179-
new Attributes(['class' => 'description']),
180-
new Text($this->translate(
181-
"Contact will be notified via the default channel, when no specific channel is configured"
182-
. " in an event rule."
183-
))
184-
));
185179

186180
$this->addElement(
187181
'submit',
@@ -205,9 +199,7 @@ protected function assemble()
205199
);
206200

207201
$this->registerElement($deleteButton);
208-
$this->getElement('submit')
209-
->getWrapper()
210-
->prepend($deleteButton);
202+
$this->getElement('submit')->prependWrapper((new HtmlDocument())->addHtml($deleteButton));
211203
}
212204
}
213205

@@ -466,15 +458,15 @@ private function addAddressElements(?string $defaultType): void
466458
return;
467459
}
468460

469-
$address = new FieldsetElement('contact_address', ['label' => $this->translate('Channels')]);
461+
$address = $this->createElement('fieldset', 'contact_address', ['label' => $this->translate('Channels')]);
462+
$this->addElement($address);
463+
470464
$address->addHtml(new HtmlElement(
471465
'p',
472466
new Attributes(['class' => 'description']),
473467
new Text($this->translate('Configure the channels available for this contact here.'))
474468
));
475469

476-
$this->addElement($address);
477-
478470
foreach ($plugins as $type => $name) {
479471
$element = $this->createElement('text', $type, [
480472
'label' => $name,

0 commit comments

Comments
 (0)