diff --git a/library/Reporting/Web/Forms/TimeframeForm.php b/library/Reporting/Web/Forms/TimeframeForm.php index fe84152..2194a0b 100644 --- a/library/Reporting/Web/Forms/TimeframeForm.php +++ b/library/Reporting/Web/Forms/TimeframeForm.php @@ -46,16 +46,10 @@ protected function assemble() 'description' => $this->translate('A unique name of this timeframe') ]); - $default = new DateTime('00:00:00'); - $start = $this->getPopulatedValue('start', $default); - if (! $start instanceof DateTime) { - $datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $start); - if ($datetime) { - $start = $datetime; - } - } - - $relativeStart = $this->getPopulatedValue('relative-start', $start instanceof DateTime ? 'n' : 'y'); + $start = $this->getPopulatedValue('start', new DateTime('00:00:00')); + $canBeConverted = $start instanceof DateTime + || DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $start) !== false; + $relativeStart = $this->getPopulatedValue('relative-start', $canBeConverted ? 'n' : 'y'); $this->addElement('checkbox', 'relative-start', [ 'required' => false, 'class' => 'autosubmit', @@ -65,7 +59,7 @@ protected function assemble() if ($relativeStart === 'n') { if (! $start instanceof DateTime) { - $start = $default; + $start = (new DateTime($start))->format(LocalDateTimeElement::FORMAT); $this->clearPopulatedValue('start'); } @@ -73,6 +67,7 @@ protected function assemble() new LocalDateTimeElement('start', [ 'required' => true, 'value' => $start, + 'class' => 'autosubmit', 'label' => $this->translate('Start'), 'description' => $this->translate('Specifies the start time of this timeframe') ]) @@ -101,16 +96,10 @@ protected function assemble() ]); } - $default = new DateTime('23:59:59'); - $end = $this->getPopulatedValue('end', $default); - if (! $end instanceof DateTime) { - $datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $end); - if ($datetime) { - $end = $datetime; - } - } - - $relativeEnd = $this->getPopulatedValue('relative-end', $end instanceof DateTime ? 'n' : 'y'); + $end = $this->getPopulatedValue('end', new DateTime('23:59:59')); + $canBeConverted = $end instanceof DateTime + || DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $end) !== false; + $relativeEnd = $this->getPopulatedValue('relative-end', $canBeConverted ? 'n' : 'y'); if ($relativeStart === 'y') { $this->addElement('checkbox', 'relative-end', [ 'required' => false, @@ -120,9 +109,39 @@ protected function assemble() ]); } + $endDateValidator = new CallbackValidator(function ($value, CallbackValidator $validator) { + if ($value === null) { + return true; + } + + $start = $this->getValue('start'); + + if (! $start instanceof DateTime) { + $start = new DateTime($start); + } + + if (! $value instanceof DateTime) { + try { + $value = new DateTime($value); + } catch (Exception $_) { + $validator->addMessage($this->translate('Invalid textual date time')); + + return false; + } + } + + if ($value <= $start) { + $validator->addMessage($this->translate('End time must be greater than start time')); + + return false; + } + + return true; + }); + if ($relativeEnd === 'n' || $relativeStart === 'n') { if (! $end instanceof DateTime) { - $end = $default; + $end = (new DateTime($end))->format(LocalDateTimeElement::FORMAT); $this->clearPopulatedValue('end'); } @@ -130,8 +149,10 @@ protected function assemble() new LocalDateTimeElement('end', [ 'required' => true, 'value' => $end, + 'class' => 'autosubmit', 'label' => $this->translate('End'), - 'description' => $this->translate('Specifies the end time of this timeframe') + 'description' => $this->translate('Specifies the end time of this timeframe'), + 'validators' => [$endDateValidator] ]) ); } else { @@ -140,21 +161,7 @@ protected function assemble() 'label' => $this->translate('End'), 'placeholder' => $this->translate('Last day of this month'), 'description' => $this->translate('Specifies the end time of this timeframe'), - 'validators' => [ - new CallbackValidator(function ($value, CallbackValidator $validator) { - if ($value !== null) { - try { - new DateTime($value); - } catch (Exception $_) { - $validator->addMessage($this->translate('Invalid textual date time')); - - return false; - } - } - - return true; - }) - ] + 'validators' => [$endDateValidator] ]); }