summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/FormElement/ScheduleElement/AnnuallyFields.php
blob: 857711a64c7d69bfad0d99cd441e881a24800d80 (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
124
125
126
127
128
129
130
131
132
133
<?php

namespace ipl\Web\FormElement\ScheduleElement;

use InvalidArgumentException;
use ipl\Html\Attributes;
use ipl\Html\FormattedString;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\HtmlElement;
use ipl\Web\FormElement\ScheduleElement\Common\FieldsProtector;
use ipl\Web\FormElement\ScheduleElement\Common\FieldsUtils;
use ipl\Web\Widget\Icon;

class AnnuallyFields extends FieldsetElement
{
    use FieldsUtils;
    use FieldsProtector;

    /** @var array A list of valid months */
    protected $months = [];

    /** @var string A month to preselect by default */
    protected $default = 'JAN';

    public function __construct($name, $attributes = null)
    {
        $this->months = [
            'JAN' => $this->translate('Jan'),
            'FEB' => $this->translate('Feb'),
            'MAR' => $this->translate('Mar'),
            'APR' => $this->translate('Apr'),
            'MAY' => $this->translate('May'),
            'JUN' => $this->translate('Jun'),
            'JUL' => $this->translate('Jul'),
            'AUG' => $this->translate('Aug'),
            'SEP' => $this->translate('Sep'),
            'OCT' => $this->translate('Oct'),
            'NOV' => $this->translate('Nov'),
            'DEC' => $this->translate('Dec')
        ];

        parent::__construct($name, $attributes);
    }

    protected function init(): void
    {
        parent::init();
        $this->initUtils();
    }

    /**
     * Set the default month to be activated
     *
     * @param string $default
     *
     * @return $this
     */
    public function setDefault(string $default): self
    {
        if (! isset($this->months[strtoupper($this->default)])) {
            throw new InvalidArgumentException(sprintf('Invalid month provided: %s', $default));
        }

        $this->default = strtoupper($default);

        return $this;
    }

    protected function assemble()
    {
        $this->getAttributes()->set('id', $this->protectId('annually-fields'));

        $fieldsSelector = new FieldsRadio('month', [
            'class'     => ['autosubmit', 'sr-only'],
            'value'     => $this->default,
            'options'   => $this->months,
            'protector' => function ($value) {
                return $this->protectId($value);
            }
        ]);
        $this->registerElement($fieldsSelector);

        $runsOnThe = $this->getPopulatedValue('runsOnThe', 'n');
        $this->addElement('checkbox', 'runsOnThe', [
            'class' => 'autosubmit',
            'value' => $runsOnThe
        ]);

        $checkboxControls = HtmlElement::create('div', ['class' => 'toggle-slider-controls']);
        $checkbox = $this->getElement('runsOnThe');
        $checkbox->prependWrapper($checkboxControls);
        $checkboxControls->addHtml($checkbox, HtmlElement::create('span', null, $this->translate('On the')));

        $annuallyWrapper = HtmlElement::create('div', ['class' => 'annually']);
        $checkboxControls->prependWrapper($annuallyWrapper);
        $annuallyWrapper->addHtml($fieldsSelector);

        $notes = HtmlElement::create('div', ['class' => 'note']);
        $notes->addHtml(
            FormattedString::create(
                $this->translate('Use %s / %s keys to choose a month by keyboard.'),
                new Icon('arrow-left'),
                new Icon('arrow-right')
            )
        );
        $annuallyWrapper->addHtml($notes);

        $enumerations = $this->createOrdinalElement();
        $enumerations->getAttributes()->set('disabled', $runsOnThe === 'n');
        $this->registerElement($enumerations);

        $selectableDays = $this->createOrdinalSelectableDays();
        $selectableDays->getAttributes()->set('disabled', $runsOnThe === 'n');
        $this->registerElement($selectableDays);

        $ordinalWrapper = HtmlElement::create('div', ['class' => ['ordinal', 'annually']]);
        $this
            ->decorate($enumerations)
            ->addHtml($enumerations);

        $enumerations->prependWrapper($ordinalWrapper);
        $ordinalWrapper->addHtml($enumerations, $selectableDays);
    }

    protected function registerAttributeCallbacks(Attributes $attributes)
    {
        parent::registerAttributeCallbacks($attributes);

        $attributes
            ->registerAttributeCallback('default', null, [$this, 'setDefault'])
            ->registerAttributeCallback('protector', null, [$this, 'setIdProtector']);
    }
}