From 2d2277e7665ebe233fb449dab7a5a62accda2c35 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 23 Jul 2024 10:40:26 +0200 Subject: [PATCH] TimeframeForm: Fix DateTime handling - `DateTime::createFromFormat()` returns false, if seconds are `00`, which leads to a false positive result at the end. - Check the checkboxes only if start/end is actually relative --- library/Reporting/Web/Forms/TimeframeForm.php | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/library/Reporting/Web/Forms/TimeframeForm.php b/library/Reporting/Web/Forms/TimeframeForm.php index 351cd78..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'); } @@ -102,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, @@ -153,7 +141,7 @@ protected function assemble() if ($relativeEnd === 'n' || $relativeStart === 'n') { if (! $end instanceof DateTime) { - $end = $default; + $end = (new DateTime($end))->format(LocalDateTimeElement::FORMAT); $this->clearPopulatedValue('end'); }