diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:46:47 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:46:47 +0000 |
commit | 4ada86876033fa171e2896d7e3d3c5645d8062db (patch) | |
tree | f0d1fee61877df200ccfb1c0af58a39cd551fb46 /library/Reporting/Web/Forms/Decorator/CompatDecorator.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-reporting-upstream.tar.xz icingaweb2-module-reporting-upstream.zip |
Adding upstream version 0.10.0.upstream/0.10.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | library/Reporting/Web/Forms/Decorator/CompatDecorator.php | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/library/Reporting/Web/Forms/Decorator/CompatDecorator.php b/library/Reporting/Web/Forms/Decorator/CompatDecorator.php new file mode 100644 index 0000000..b2eb536 --- /dev/null +++ b/library/Reporting/Web/Forms/Decorator/CompatDecorator.php @@ -0,0 +1,63 @@ +<?php +// Icinga Reporting | (c) 2021 Icinga GmbH | GPLv2 + +namespace Icinga\Module\Reporting\Web\Forms\Decorator; + +use Icinga\Application\Version; +use ipl\Html\Attributes; +use ipl\Html\FormElement\CheckboxElement; +use ipl\Html\HtmlElement; + +class CompatDecorator extends \ipl\Web\Compat\CompatDecorator +{ + protected function createCheckboxCompat(CheckboxElement $checkbox) + { + if (! $checkbox->getAttributes()->has('id')) { + $checkbox->setAttribute('id', base64_encode(random_bytes(8))); + } + + $checkbox->getAttributes()->add('class', 'sr-only'); + + $classes = ['toggle-switch']; + if ($checkbox->getAttributes()->get('disabled')->getValue()) { + $classes[] = 'disabled'; + } + + return [ + $checkbox, + new HtmlElement('label', Attributes::create([ + 'class' => $classes, + 'aria-hidden' => 'true', + 'for' => $checkbox->getAttributes()->get('id')->getValue() + ]), new HtmlElement('span', Attributes::create(['class' => 'toggle-slider']))) + ]; + } + + protected function assembleElementCompat() + { + if ($this->formElement instanceof CheckboxElement) { + return $this->createCheckboxCompat($this->formElement); + } + + return $this->formElement; + } + + protected function assemble() + { + if (version_compare(Version::VERSION, '2.9.0', '>=')) { + parent::assemble(); + return; + } + + if ($this->formElement->hasBeenValidated() && ! $this->formElement->isValid()) { + $this->getAttributes()->add('class', 'has-error'); + } + + $this->add(array_filter([ + $this->assembleLabel(), + $this->assembleElementCompat(), + $this->assembleDescription(), + $this->assembleErrors() + ])); + } +} |