Apply the relative range start (if any) to the absolute range selector

refs #33
This commit is contained in:
Alexander A. Klimov 2017-09-06 18:32:57 +02:00
parent fe9eeba818
commit 9970fa8cb9
3 changed files with 81 additions and 13 deletions

View file

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

View file

@ -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;
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Icinga\Module\Graphite\Forms\TimeRangePicker;
use Icinga\Web\UrlParams;
trait TimeRangePickerTrait
{
/**
* Extract the relative time range (if any) from the given URL parameters
*
* @param UrlParams $params
*
* @return bool|int|null
*/
protected function getRelativeSeconds(UrlParams $params)
{
$seconds = $params->get('graph_range');
if ($seconds === null) {
return null;
}
return preg_match('/^(?:0|[1-9]\d*)$/', $seconds) ? (int) $seconds : false;
}
}