| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\FormElement\ScheduleElement; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use ipl\Html\Attributes; |
| 7 |
use ipl\Html\FormElement\FieldsetElement; |
| 8 |
use ipl\Html\HtmlElement; |
| 9 |
use ipl\Validator\CallbackValidator; |
| 10 |
use ipl\Validator\ValidatorChain; |
| 11 |
use ipl\Web\FormElement\ScheduleElement\Common\FieldsProtector; |
| 12 |
|
| 13 |
class WeeklyFields extends FieldsetElement |
| 14 |
{ |
| 15 |
use FieldsProtector; |
| 16 |
|
| 17 |
/** @var array A list of valid week days */ |
| 18 |
protected $weekdays = []; |
| 19 |
|
| 20 |
/** @var string A valid weekday to be selected by default */ |
| 21 |
protected $default = 'MO'; |
| 22 |
|
| 23 |
public function __construct($name, $attributes = null) |
| 24 |
{ |
| 25 |
$this->weekdays = [ |
| 26 |
'MO' => $this->translate('Mon'), |
| 27 |
'TU' => $this->translate('Tue'), |
| 28 |
'WE' => $this->translate('Wed'), |
| 29 |
'TH' => $this->translate('Thu'), |
| 30 |
'FR' => $this->translate('Fri'), |
| 31 |
'SA' => $this->translate('Sat'), |
| 32 |
'SU' => $this->translate('Sun') |
| 33 |
]; |
| 34 |
|
| 35 |
parent::__construct($name, $attributes); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Set the default weekday to be preselected |
| 40 |
* |
| 41 |
* @param string $default |
| 42 |
* |
| 43 |
* @return $this |
| 44 |
*/ |
| 45 |
public function setDefault(string $default): self |
| 46 |
{ |
| 47 |
$weekday = strlen($default) > 2 ? substr($default, 0, -1) : $default; |
| 48 |
if (! isset($this->weekdays[strtoupper($weekday)])) { |
| 49 |
throw new InvalidArgumentException(sprintf('Invalid weekday provided: %s', $default)); |
| 50 |
} |
| 51 |
|
| 52 |
$this->default = strtoupper($weekday); |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get all the selected weekdays |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function getSelectedWeekDays(): array |
| 63 |
{ |
| 64 |
$selectedDays = []; |
| 65 |
foreach ($this->weekdays as $day => $_) { |
| 66 |
if ($this->getValue($day, 'n') === 'y') { |
| 67 |
$selectedDays[] = $day; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if (empty($selectedDays)) { |
| 72 |
$selectedDays[] = $this->default; |
| 73 |
} |
| 74 |
|
| 75 |
return $selectedDays; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Transform the given weekdays into key=>value array that can be populated |
| 80 |
* |
| 81 |
* @param array $weekdays |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public function loadWeekDays(array $weekdays): array |
| 86 |
{ |
| 87 |
$values = []; |
| 88 |
foreach ($this->weekdays as $weekday => $_) { |
| 89 |
$values[$weekday] = in_array($weekday, $weekdays, true) ? 'y' : 'n'; |
| 90 |
} |
| 91 |
|
| 92 |
return $values; |
| 93 |
} |
| 94 |
|
| 95 |
protected function assemble() |
| 96 |
{ |
| 97 |
$this->getAttributes()->set('id', $this->protectId('weekly-fields')); |
| 98 |
|
| 99 |
$fieldsWrapper = HtmlElement::create('div', ['class' => 'weekly']); |
| 100 |
$listItems = HtmlElement::create('ul', ['class' => ['schedule-element-fields', 'multiple-fields']]); |
| 101 |
|
| 102 |
foreach ($this->weekdays as $day => $value) { |
| 103 |
$checkbox = $this->createElement('checkbox', $day, [ |
| 104 |
'class' => ['autosubmit', 'sr-only'], |
| 105 |
'value' => $day === $this->default, |
| 106 |
'decorators' => [] |
| 107 |
]); |
| 108 |
$this->registerElement($checkbox); |
| 109 |
|
| 110 |
$htmlId = $this->protectId("weekday-$day"); |
| 111 |
$checkbox->getAttributes()->set('id', $htmlId); |
| 112 |
|
| 113 |
$listItem = HtmlElement::create('li'); |
| 114 |
$listItem->addHtml($checkbox, HtmlElement::create('label', ['for' => $htmlId], $value)); |
| 115 |
$listItems->addHtml($listItem); |
| 116 |
} |
| 117 |
|
| 118 |
$fieldsWrapper->addHtml($listItems); |
| 119 |
$this->addHtml($fieldsWrapper); |
| 120 |
} |
| 121 |
|
| 122 |
protected function registerAttributeCallbacks(Attributes $attributes) |
| 123 |
{ |
| 124 |
parent::registerAttributeCallbacks($attributes); |
| 125 |
|
| 126 |
$attributes |
| 127 |
->registerAttributeCallback('default', null, [$this, 'setDefault']) |
| 128 |
->registerAttributeCallback('protector', null, [$this, 'setIdProtector']); |
| 129 |
} |
| 130 |
|
| 131 |
protected function addDefaultValidators(ValidatorChain $chain): void |
| 132 |
{ |
| 133 |
$chain->add( |
| 134 |
new CallbackValidator(function ($_, CallbackValidator $validator): bool { |
| 135 |
$valid = false; |
| 136 |
foreach ($this->weekdays as $weekday => $_) { |
| 137 |
if ($this->getValue($weekday) === 'y') { |
| 138 |
$valid = true; |
| 139 |
|
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (! $valid) { |
| 145 |
$validator->addMessage($this->translate('You must select at least one of these weekdays')); |
| 146 |
} |
| 147 |
|
| 148 |
return $valid; |
| 149 |
}) |
| 150 |
); |
| 151 |
} |
| 152 |
} |