| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\FormElement\ScheduleElement; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\FormElement\FieldsetElement; |
| 7 |
use ipl\Html\HtmlElement; |
| 8 |
use ipl\Validator\CallbackValidator; |
| 9 |
use ipl\Validator\InArrayValidator; |
| 10 |
use ipl\Validator\ValidatorChain; |
| 11 |
use ipl\Web\FormElement\ScheduleElement\Common\FieldsProtector; |
| 12 |
use ipl\Web\FormElement\ScheduleElement\Common\FieldsUtils; |
| 13 |
|
| 14 |
class MonthlyFields extends FieldsetElement |
| 15 |
{ |
| 16 |
use FieldsUtils; |
| 17 |
use FieldsProtector; |
| 18 |
|
| 19 |
/** @var string Used as radio option to run each selected days/months */ |
| 20 |
public const RUNS_EACH = 'each'; |
| 21 |
|
| 22 |
/** @var string Used as radio option to build complex job schedules */ |
| 23 |
public const RUNS_ONTHE = 'onthe'; |
| 24 |
|
| 25 |
/** @var int Number of days in a week */ |
| 26 |
public const WEEK_DAYS = 7; |
| 27 |
|
| 28 |
/** @var int Day of the month to preselect by default */ |
| 29 |
protected $default = 1; |
| 30 |
|
| 31 |
/** @var int Number of fields to render */ |
| 32 |
protected $availableFields; |
| 33 |
|
| 34 |
protected function init(): void |
| 35 |
{ |
| 36 |
parent::init(); |
| 37 |
$this->initUtils(); |
| 38 |
|
| 39 |
$this->availableFields = (int) date('t'); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Set the available fields/days of the month to be rendered |
| 44 |
* |
| 45 |
* @param int $fields |
| 46 |
* |
| 47 |
* @return $this |
| 48 |
*/ |
| 49 |
public function setAvailableFields(int $fields): self |
| 50 |
{ |
| 51 |
$this->availableFields = $fields; |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Set the default field/day to be selected |
| 58 |
* |
| 59 |
* @param int $default |
| 60 |
* |
| 61 |
* @return $this |
| 62 |
*/ |
| 63 |
public function setDefault(int $default): self |
| 64 |
{ |
| 65 |
$this->default = $default; |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get all the selected weekdays |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function getSelectedDays(): array |
| 76 |
{ |
| 77 |
$selectedDays = []; |
| 78 |
foreach (range(1, $this->availableFields) as $day) { |
| 79 |
if ($this->getValue("day$day", 'n') === 'y') { |
| 80 |
$selectedDays[] = $day; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (empty($selectedDays)) { |
| 85 |
$selectedDays[] = $this->default; |
| 86 |
} |
| 87 |
|
| 88 |
return $selectedDays; |
| 89 |
} |
| 90 |
|
| 91 |
protected function assemble() |
| 92 |
{ |
| 93 |
$this->getAttributes()->set('id', $this->protectId('monthly-fields')); |
| 94 |
|
| 95 |
$runsOn = $this->getPopulatedValue('runsOn', static::RUNS_EACH); |
| 96 |
$this->addElement('radio', 'runsOn', [ |
| 97 |
'required' => true, |
| 98 |
'class' => 'autosubmit', |
| 99 |
'value' => $runsOn, |
| 100 |
'options' => [static::RUNS_EACH => $this->translate('Each')], |
| 101 |
]); |
| 102 |
|
| 103 |
$listItems = HtmlElement::create('ul', ['class' => ['schedule-element-fields', 'multiple-fields']]); |
| 104 |
if ($runsOn === static::RUNS_ONTHE) { |
| 105 |
$listItems->getAttributes()->add('class', 'disabled'); |
| 106 |
} |
| 107 |
|
| 108 |
foreach (range(1, $this->availableFields) as $day) { |
| 109 |
$checkbox = $this->createElement('checkbox', "day$day", [ |
| 110 |
'class' => ['autosubmit', 'sr-only'], |
| 111 |
'value' => $day === $this->default && $runsOn === static::RUNS_EACH, |
| 112 |
'decorators' => [] |
| 113 |
]); |
| 114 |
$this->registerElement($checkbox); |
| 115 |
|
| 116 |
$htmlId = $this->protectId("day$day"); |
| 117 |
$checkbox->getAttributes()->set('id', $htmlId); |
| 118 |
|
| 119 |
$listItem = HtmlElement::create('li'); |
| 120 |
$listItem->addHtml($checkbox, HtmlElement::create('label', ['for' => $htmlId], $day)); |
| 121 |
$listItems->addHtml($listItem); |
| 122 |
} |
| 123 |
|
| 124 |
$monthlyWrapper = HtmlElement::create('div', ['class' => 'monthly']); |
| 125 |
$runsEach = $this->getElement('runsOn'); |
| 126 |
$runsEach->prependWrapper($monthlyWrapper); |
| 127 |
$monthlyWrapper->addHtml($runsEach, $listItems); |
| 128 |
|
| 129 |
$this->addElement('radio', 'runsOn', [ |
| 130 |
'required' => $runsOn !== static::RUNS_EACH, |
| 131 |
'class' => 'autosubmit', |
| 132 |
'options' => [static::RUNS_ONTHE => $this->translate('On the')], |
| 133 |
'validators' => [ |
| 134 |
new InArrayValidator([ |
| 135 |
'strict' => true, |
| 136 |
'haystack' => [static::RUNS_EACH, static::RUNS_ONTHE] |
| 137 |
]) |
| 138 |
] |
| 139 |
]); |
| 140 |
|
| 141 |
$ordinalWrapper = HtmlElement::create('div', ['class' => 'ordinal']); |
| 142 |
$runsOnThe = $this->getElement('runsOn'); |
| 143 |
$runsOnThe->prependWrapper($ordinalWrapper); |
| 144 |
$ordinalWrapper->addHtml($runsOnThe); |
| 145 |
|
| 146 |
$enumerations = $this->createOrdinalElement(); |
| 147 |
$enumerations->getAttributes()->set('disabled', $runsOn === static::RUNS_EACH); |
| 148 |
$this->registerElement($enumerations); |
| 149 |
|
| 150 |
$selectableDays = $this->createOrdinalSelectableDays(); |
| 151 |
$selectableDays->getAttributes()->set('disabled', $runsOn === static::RUNS_EACH); |
| 152 |
$this->registerElement($selectableDays); |
| 153 |
|
| 154 |
$ordinalWrapper->addHtml($enumerations, $selectableDays); |
| 155 |
} |
| 156 |
|
| 157 |
protected function registerAttributeCallbacks(Attributes $attributes) |
| 158 |
{ |
| 159 |
parent::registerAttributeCallbacks($attributes); |
| 160 |
|
| 161 |
$attributes |
| 162 |
->registerAttributeCallback('default', null, [$this, 'setDefault']) |
| 163 |
->registerAttributeCallback('availableFields', null, [$this, 'setAvailableFields']) |
| 164 |
->registerAttributeCallback('protector', null, [$this, 'setIdProtector']); |
| 165 |
} |
| 166 |
|
| 167 |
protected function addDefaultValidators(ValidatorChain $chain): void |
| 168 |
{ |
| 169 |
$chain->add( |
| 170 |
new CallbackValidator(function ($_, CallbackValidator $validator): bool { |
| 171 |
if ($this->getValue('runsOn', static::RUNS_EACH) !== static::RUNS_EACH) { |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
$valid = false; |
| 176 |
foreach (range(1, $this->availableFields) as $day) { |
| 177 |
if ($this->getValue("day$day") === 'y') { |
| 178 |
$valid = true; |
| 179 |
|
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (! $valid) { |
| 185 |
$validator->addMessage($this->translate('You must select at least one of these days')); |
| 186 |
} |
| 187 |
|
| 188 |
return $valid; |
| 189 |
}) |
| 190 |
); |
| 191 |
} |
| 192 |
} |