diff --git a/application/forms/RotationConfigForm.php b/application/forms/RotationConfigForm.php index db38f31cb..b6cf9b8f7 100644 --- a/application/forms/RotationConfigForm.php +++ b/application/forms/RotationConfigForm.php @@ -919,11 +919,28 @@ protected function assembleMultiDayOptions(FieldsetElement $options): DateTime 'options' => $timeOptions ]); $options->registerElement($fromAt); + $selectedFromAt = $fromAt->getValue(); + + // Small optimization only, out-of-range options are only required under certain conditions + $removeOutOfRangeToAtOptions = function () use ($selectedFromAt, $timeOptions) { + return array_slice( + $timeOptions, + 0, + array_search($selectedFromAt, array_keys($timeOptions), true) + 1, + true + ); + }; - if ($selectedFromDay === (int) $to->getValue()) { - $selectedFromAt = $fromAt->getValue(); - $keyIndex = array_search($selectedFromAt, array_keys($timeOptions)); - $timeOptions = array_slice($timeOptions, 0, $keyIndex + 1, true); + $timeOptionsFirstKey = array_key_first($timeOptions); + $selectedToDay = (int) $to->getValue(); + $endOfDay = 'endOfDay'; + if ($selectedFromDay === $selectedToDay) { + $timeOptions = $removeOutOfRangeToAtOptions(); + } else { + $timeOptions[$endOfDay] = sprintf( + $this->translate('%s (End of day)'), + $timeOptions[$timeOptionsFirstKey] + ); } $toAt = $options->createElement('select', 'to_at', [ @@ -933,6 +950,17 @@ protected function assembleMultiDayOptions(FieldsetElement $options): DateTime ]); $options->registerElement($toAt); + if ($toAt->getValue() === $endOfDay) { + $selectedToDay = $selectedToDay === 7 ? 1 : $selectedToDay + 1; + + if ($selectedFromDay === $selectedToDay) { + $toAt->setOptions($removeOutOfRangeToAtOptions()); + } + + $to->setValue($selectedToDay); + $toAt->setValue($timeOptionsFirstKey); + } + $from->prependWrapper( (new HtmlDocument())->addHtml( $from,