Debian Codegraph @

vendor/ipl/web/src/FormElement/ScheduleElement/AnnuallyFields.php

raw
1 <?php
2
3 namespace ipl\Web\FormElement\ScheduleElement;
4
5 use InvalidArgumentException;
6 use ipl\Html\Attributes;
7 use ipl\Html\FormattedString;
8 use ipl\Html\FormElement\FieldsetElement;
9 use ipl\Html\HtmlElement;
10 use ipl\Web\FormElement\ScheduleElement\Common\FieldsProtector;
11 use ipl\Web\FormElement\ScheduleElement\Common\FieldsUtils;
12 use ipl\Web\Widget\Icon;
13
14 class AnnuallyFields extends FieldsetElement
15 {
16 use FieldsUtils;
17 use FieldsProtector;
18
19 /** @var array A list of valid months */
20 protected $months = [];
21
22 /** @var string A month to preselect by default */
23 protected $default = 'JAN';
24
25 public function __construct($name, $attributes = null)
26 {
27 $this->months = [
28 'JAN' => $this->translate('Jan'),
29 'FEB' => $this->translate('Feb'),
30 'MAR' => $this->translate('Mar'),
31 'APR' => $this->translate('Apr'),
32 'MAY' => $this->translate('May'),
33 'JUN' => $this->translate('Jun'),
34 'JUL' => $this->translate('Jul'),
35 'AUG' => $this->translate('Aug'),
36 'SEP' => $this->translate('Sep'),
37 'OCT' => $this->translate('Oct'),
38 'NOV' => $this->translate('Nov'),
39 'DEC' => $this->translate('Dec')
40 ];
41
42 parent::__construct($name, $attributes);
43 }
44
45 protected function init(): void
46 {
47 parent::init();
48 $this->initUtils();
49
50 $this->addDefaultPluginLoader('element', 'ipl\\Web\\FormElement\\ScheduleElement', '');
51 }
52
53 /**
54 * Set the default month to be activated
55 *
56 * @param string $default
57 *
58 * @return $this
59 */
60 public function setDefault(string $default): self
61 {
62 if (! isset($this->months[strtoupper($this->default)])) {
63 throw new InvalidArgumentException(sprintf('Invalid month provided: %s', $default));
64 }
65
66 $this->default = strtoupper($default);
67
68 return $this;
69 }
70
71 protected function assemble()
72 {
73 $this->getAttributes()->set('id', $this->protectId('annually-fields'));
74
75 $fieldsSelector = $this->createElement('fieldsRadio', 'month', [
76 'class' => ['autosubmit', 'sr-only'],
77 'value' => $this->default,
78 'options' => $this->months,
79 'protector' => function ($value) {
80 return $this->protectId($value);
81 }
82 ]);
83 $this->registerElement($fieldsSelector);
84 $this->decorate($fieldsSelector);
85
86 $runsOnThe = $this->getPopulatedValue('runsOnThe', 'n');
87 $checkbox = $this->createElement('checkbox', 'runsOnThe', [
88 'class' => 'autosubmit',
89 'value' => $runsOnThe
90 ]);
91 $this->registerElement($checkbox);
92 $this->decorate($checkbox);
93
94 $checkboxControls = HtmlElement::create('div', ['class' => 'toggle-slider-controls']);
95 $checkbox->prependWrapper($checkboxControls);
96 $checkboxControls->addHtml($checkbox, HtmlElement::create('span', null, $this->translate('On the')));
97
98 $notes = HtmlElement::create('div', ['class' => 'note']);
99 $notes->addHtml(
100 FormattedString::create(
101 $this->translate('Use %s / %s keys to choose a month by keyboard.'),
102 new Icon('arrow-left'),
103 new Icon('arrow-right')
104 )
105 );
106 $annuallyWrapper = HtmlElement::create('div', ['class' => 'annually']);
107 $fieldsSelector->prependWrapper($annuallyWrapper);
108 $annuallyWrapper->addHtml($fieldsSelector, $notes);
109
110 $this->addHtml($fieldsSelector, $checkbox);
111
112 $enumerations = $this->createOrdinalElement();
113 $enumerations->getAttributes()->set('disabled', $runsOnThe === 'n');
114 $this->registerElement($enumerations);
115 $this->decorate($enumerations);
116
117 $selectableDays = $this->createOrdinalSelectableDays();
118 $selectableDays->getAttributes()->set('disabled', $runsOnThe === 'n');
119 $this->registerElement($selectableDays);
120
121 $ordinalWrapper = HtmlElement::create('div', ['class' => ['ordinal', 'annually']]);
122 $enumerations->prependWrapper($ordinalWrapper);
123 $ordinalWrapper->addHtml($enumerations, $selectableDays);
124
125 $this->addHtml($enumerations);
126 }
127
128 protected function registerAttributeCallbacks(Attributes $attributes)
129 {
130 parent::registerAttributeCallbacks($attributes);
131
132 $attributes
133 ->registerAttributeCallback('default', null, [$this, 'setDefault'])
134 ->registerAttributeCallback('protector', null, [$this, 'setIdProtector']);
135 }
136 }