Skip to content

Commit abf448f

Browse files
RotationConfigForm: Improve rotation time input (#293)
resolves #287
2 parents ddef0d3 + dc4290a commit abf448f

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

application/forms/RotationConfigForm.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,13 @@ protected function assemblePartialDayOptions(FieldsetElement $options): DateTime
722722
7 => $this->translate('Sunday')
723723
]
724724
]);
725+
726+
$timeOptions = $this->getTimeOptions();
725727
$options->addElement('select', 'from', [
726728
'class' => 'autosubmit',
727729
'required' => true,
728730
'value' => '00:00',
729-
'options' => $this->getTimeOptions(),
731+
'options' => $timeOptions,
730732
'label' => $this->translate('From')
731733
]);
732734
$from = $options->getElement('from');
@@ -740,9 +742,18 @@ protected function assemblePartialDayOptions(FieldsetElement $options): DateTime
740742
'validators' => [new GreaterThanValidator()]
741743
]);
742744

745+
$selectedFromTime = $from->getValue();
746+
foreach ($timeOptions as $key => $value) {
747+
$timeOptions[$key] = sprintf('%s (%s)', $value, $this->translate('Next Day'));
748+
749+
if ($selectedFromTime === $key) {
750+
break;
751+
}
752+
}
753+
743754
$to = $options->createElement('select', 'to', [
744755
'required' => true,
745-
'options' => $this->getTimeOptions()
756+
'options' => $timeOptions
746757
]);
747758
$options->registerElement($to);
748759

@@ -818,27 +829,18 @@ protected function assembleMultiDayOptions(FieldsetElement $options): DateTime
818829
]);
819830
$from = $options->getElement('from_day');
820831

832+
$selectedFromDay = (int) $from->getValue();
833+
834+
for ($i = 1; $i <= $selectedFromDay; $i++) {
835+
$toDays[$i] = sprintf('%s (%s)', $toDays[$i], $this->translate('Next week'));
836+
}
837+
821838
$options->addElement('select', 'to_day', [
822839
'class' => 'autosubmit',
823840
'required' => true,
824841
'options' => $toDays,
825842
'value' => 7,
826-
'label' => $this->translate('To', 'notifications.rotation'),
827-
'validators' => [
828-
new CallbackValidator(function ($value, $validator) use ($options) {
829-
if ($value !== $options->getValue('from_day')) {
830-
return true;
831-
}
832-
833-
if ($options->getValue('from_at') < $options->getValue('to_at')) {
834-
$validator->addMessage($this->translate('Shifts cannot last longer than 7 days'));
835-
836-
return false;
837-
}
838-
839-
return true;
840-
})
841-
]
843+
'label' => $this->translate('To', 'notifications.rotation')
842844
]);
843845
$to = $options->getElement('to_day');
844846

@@ -850,17 +852,24 @@ protected function assembleMultiDayOptions(FieldsetElement $options): DateTime
850852
'label' => $this->translate('Handoff every')
851853
]);
852854

855+
$timeOptions = $this->getTimeOptions();
853856
$fromAt = $options->createElement('select', 'from_at', [
854857
'class' => 'autosubmit',
855858
'required' => true,
856-
'options' => $this->getTimeOptions()
859+
'options' => $timeOptions
857860
]);
858861
$options->registerElement($fromAt);
859862

863+
if ($selectedFromDay === (int) $to->getValue()) {
864+
$selectedFromAt = $fromAt->getValue();
865+
$keyIndex = array_search($selectedFromAt, array_keys($timeOptions));
866+
$timeOptions = array_slice($timeOptions, 0, $keyIndex + 1, true);
867+
}
868+
860869
$toAt = $options->createElement('select', 'to_at', [
861870
'class' => 'autosubmit',
862871
'required' => true,
863-
'options' => $this->getTimeOptions()
872+
'options' => $timeOptions
864873
]);
865874
$options->registerElement($toAt);
866875

0 commit comments

Comments
 (0)