From 9970fa8cb9bfd85045e1084d09fe105041b97751 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 6 Sep 2017 18:32:57 +0200 Subject: [PATCH] Apply the relative range start (if any) to the absolute range selector refs #33 --- .../forms/TimeRangePicker/CommonForm.php | 11 +--- .../forms/TimeRangePicker/CustomForm.php | 58 +++++++++++++++++-- .../TimeRangePicker/TimeRangePickerTrait.php | 25 ++++++++ 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 application/forms/TimeRangePicker/TimeRangePickerTrait.php diff --git a/application/forms/TimeRangePicker/CommonForm.php b/application/forms/TimeRangePicker/CommonForm.php index a2df048..536a60d 100644 --- a/application/forms/TimeRangePicker/CommonForm.php +++ b/application/forms/TimeRangePicker/CommonForm.php @@ -8,10 +8,7 @@ use Zend_Form_Element_Select; class CommonForm extends Form { - /** - * @var string - */ - protected $seconds = '/^(?:0|[1-9]\d*)$/'; + use TimeRangePickerTrait; /** * The selectable units with themselves in seconds @@ -149,12 +146,10 @@ class CommonForm extends Form protected function urlToForm() { $params = $this->getRedirectUrl()->getParams(); - $seconds = $params->get('graph_range'); + $seconds = $this->getRelativeSeconds($params); if ($seconds !== null) { - if (preg_match($this->seconds, $seconds)) { - $seconds = (int) $seconds; - + if ($seconds !== false) { foreach ($this->rangeFactors as $unit => $factor) { /** @var Zend_Form_Element_Select $element */ $element = $this->getElement($unit); diff --git a/application/forms/TimeRangePicker/CustomForm.php b/application/forms/TimeRangePicker/CustomForm.php index 92c26eb..918290e 100644 --- a/application/forms/TimeRangePicker/CustomForm.php +++ b/application/forms/TimeRangePicker/CustomForm.php @@ -11,6 +11,8 @@ use Icinga\Web\Form; class CustomForm extends Form { + use TimeRangePickerTrait; + /** * @var string */ @@ -28,6 +30,13 @@ class CustomForm extends Form */ protected $timeZone; + /** + * Right now + * + * @var DateTime + */ + protected $now; + public function init() { $this->setName('form_timerangepickercustom_graphite'); @@ -75,7 +84,7 @@ class CustomForm extends Form $this->setSubmitLabel($this->translate('Update')); - $this->urlToForm('start'); + $this->urlToForm('start', $this->getRelativeTimestamp()); $this->urlToForm('end'); } @@ -126,12 +135,13 @@ class CustomForm extends Form /** * Set this form's elements' default values based on the redirect URL's parameters * - * @param string $part Either 'start' or 'end' + * @param string $part Either 'start' or 'end' + * @param int $defaultTimestamp Fallback */ - protected function urlToForm($part) + protected function urlToForm($part, $defaultTimestamp = null) { $params = $this->getRedirectUrl()->getParams(); - $timestamp = $params->get("graph_$part"); + $timestamp = $params->get("graph_$part", $defaultTimestamp); if ($timestamp !== null) { if (preg_match($this->timestamp, $timestamp)) { @@ -150,6 +160,17 @@ class CustomForm extends Form } } + /** + * Get the relative range start (if any) set by {@link CommonForm} + * + * @return int|null + */ + protected function getRelativeTimestamp() + { + $seconds = $this->getRelativeSeconds($this->getRedirectUrl()->getParams()); + return is_int($seconds) ? $this->getNow()->getTimestamp() - $seconds : null; + } + /** * Change the redirect URL's parameters based on this form's elements' values * @@ -170,7 +191,7 @@ class CustomForm extends Form } else { $dateTime = DateTime::createFromFormat( $this->dateTimeFormat, - ($date === '' ? (new DateTime())->setTimezone($this->getTimeZone())->format('Y-m-d') : $date) + ($date === '' ? $this->getNow()->format('Y-m-d') : $date) . 'T' . ($time === '' ? $defaultTime : $time), $this->getTimeZone() ); @@ -217,4 +238,31 @@ class CustomForm extends Form $this->timeZone = $timeZone; return $this; } + + /** + * Get {@link now} + * + * @return DateTime + */ + public function getNow() + { + if ($this->now === null) { + $this->now = (new DateTime())->setTimezone($this->getTimeZone()); + } + + return $this->now; + } + + /** + * Set {@link now} + * + * @param DateTime $now + * + * @return $this + */ + public function setNow($now) + { + $this->now = $now; + return $this; + } } diff --git a/application/forms/TimeRangePicker/TimeRangePickerTrait.php b/application/forms/TimeRangePicker/TimeRangePickerTrait.php new file mode 100644 index 0000000..f5f903b --- /dev/null +++ b/application/forms/TimeRangePicker/TimeRangePickerTrait.php @@ -0,0 +1,25 @@ +get('graph_range'); + if ($seconds === null) { + return null; + } + + return preg_match('/^(?:0|[1-9]\d*)$/', $seconds) ? (int) $seconds : false; + } +}