Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeframeForm: Fix relative datetime handling #244

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 45 additions & 38 deletions library/Reporting/Web/Forms/TimeframeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,10 @@
'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',
Expand All @@ -65,14 +59,15 @@

if ($relativeStart === 'n') {
if (! $start instanceof DateTime) {
$start = $default;
$start = (new DateTime($start))->format(LocalDateTimeElement::FORMAT);

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.
$this->clearPopulatedValue('start');
}

$this->addElement(
new LocalDateTimeElement('start', [
'required' => true,
'value' => $start,
'class' => 'autosubmit',
'label' => $this->translate('Start'),
'description' => $this->translate('Specifies the start time of this timeframe')
])
Expand Down Expand Up @@ -101,16 +96,10 @@
]);
}

$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,
Expand All @@ -120,18 +109,50 @@
]);
}

$endDateValidator = new CallbackValidator(function ($value, CallbackValidator $validator) {
if ($value === null) {
return true;
}
Comment on lines +113 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this is not necessary, as the field for end time is required.


$start = $this->getValue('start');

if (! $start instanceof DateTime) {
$start = new DateTime($start);

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 120 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.
}

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);

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.
$this->clearPopulatedValue('end');
}

$this->addElement(
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 {
Expand All @@ -140,21 +161,7 @@
'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]
]);
}

Expand Down
Loading