summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/FormDecorator/IcingaFormDecorator.php
blob: f03893103873b1e4111dc8e59930332971b9825b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

namespace ipl\Web\FormDecorator;

use Icinga\Web\Window;
use ipl\Html\Attributes;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Html\FormDecorator\DivDecorator;
use ipl\Html\FormElement\CheckboxElement;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\Web\Widget\Icon;

class IcingaFormDecorator extends DivDecorator
{
    const SUBMIT_ELEMENT_CLASS = 'form-controls';
    const INPUT_ELEMENT_CLASS = 'control-group';
    const ERROR_CLASS = 'errors';

    protected function assembleElement()
    {
        if ($this->formElement instanceof FormSubmitElement) {
            $this->formElement->getAttributes()->add('class', 'btn-primary');
        }

        $element = parent::assembleElement();

        if ($element instanceof CheckboxElement) {
            return $this->createCheckbox($element);
        }

        return $element;
    }

    protected function createCheckbox(CheckboxElement $checkbox)
    {
        if (! $checkbox->getAttributes()->has('id')) {
            $checkbox->setAttribute(
                'id',
                $checkbox->getName() . '_' . Window::getInstance()->getContainerId()
            );
        }

        $checkbox->getAttributes()->add('class', 'sr-only');

        $classes = ['toggle-switch'];
        if ($checkbox->getAttributes()->get('disabled')->getValue()) {
            $classes[] = 'disabled';
        }

        $document = new HtmlDocument();
        $document->addHtml(
            $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']))
            )
        );

        $checkbox->prependWrapper($document);

        return $checkbox;
    }

    protected function assembleLabel()
    {
        $label = parent::assembleLabel();
        if (! $this->formElement instanceof FieldsetElement) {
            if ($label !== null) {
                $label->addWrapper(new HtmlElement('div', Attributes::create(['class' => 'control-label-group'])));
            } elseif (! $this->formElement instanceof FormSubmitElement) {
                $label = new HtmlElement(
                    'div',
                    Attributes::create(['class' => 'control-label-group']),
                    HtmlString::create('&nbsp')
                );
            }
        }

        return $label;
    }

    protected function assembleDescription()
    {
        if ($this->formElement instanceof FieldsetElement) {
            return parent::assembleDescription();
        }

        if (($description = $this->formElement->getDescription()) !== null) {
            $iconAttributes = [
                'class' => 'control-info',
                'role'  => 'image',
                'title' => $description
            ];

            $describedBy = null;
            if ($this->formElement->getAttributes()->has('id')) {
                $iconAttributes['aria-hidden'] = 'true';

                $descriptionId = 'desc_' . $this->formElement->getAttributes()->get('id')->getValue();
                $describedBy = new HtmlElement('span', Attributes::create([
                    'id'    => $descriptionId,
                    'class' => 'sr-only'
                ]), Text::create($description));

                $this->formElement->getAttributes()->set('aria-describedby', $descriptionId);
            }

            return [
                new Icon('info-circle', $iconAttributes),
                $describedBy
            ];
        }
    }
}