2013-07-16 15:39:47 +02:00
|
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-07-16 15:39:47 +02:00
|
|
|
|
|
|
|
|
|
namespace Icinga\Web\Form\Element;
|
|
|
|
|
|
2014-09-03 14:40:58 +02:00
|
|
|
|
use DateTime;
|
2014-10-06 10:20:26 +02:00
|
|
|
|
use Icinga\Web\Form\FormElement;
|
2014-09-03 14:40:58 +02:00
|
|
|
|
use Icinga\Web\Form\Validator\DateTimeValidator;
|
2013-07-16 15:39:47 +02:00
|
|
|
|
|
2013-08-06 19:05:16 +02:00
|
|
|
|
/**
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* A date-and-time input control
|
2013-08-06 19:05:16 +02:00
|
|
|
|
*/
|
2014-10-06 10:20:26 +02:00
|
|
|
|
class DateTimePicker extends FormElement
|
2013-07-16 15:39:47 +02:00
|
|
|
|
{
|
2014-09-03 14:40:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* Form view helper to use for rendering
|
2013-10-20 15:30:49 +02:00
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-09-03 14:40:58 +02:00
|
|
|
|
public $helper = 'formDateTime';
|
2013-10-20 15:30:49 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2014-09-03 14:40:58 +02:00
|
|
|
|
protected $local = true;
|
2013-10-20 15:30:49 +02:00
|
|
|
|
|
2013-07-16 15:39:47 +02:00
|
|
|
|
/**
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* The expected lower bound for the element’s value
|
|
|
|
|
*
|
|
|
|
|
* @var DateTime|null
|
2013-07-16 15:39:47 +02:00
|
|
|
|
*/
|
2014-09-03 14:40:58 +02:00
|
|
|
|
protected $min;
|
2013-08-07 17:14:58 +02:00
|
|
|
|
|
2013-08-30 17:42:39 +02:00
|
|
|
|
/**
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* The expected upper bound for the element’s
|
|
|
|
|
*
|
|
|
|
|
* @var DateTime|null
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*/
|
2014-09-03 14:40:58 +02:00
|
|
|
|
protected $max;
|
2013-08-30 17:42:39 +02:00
|
|
|
|
|
2013-08-07 17:14:58 +02:00
|
|
|
|
/**
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* (non-PHPDoc)
|
2014-10-06 10:20:26 +02:00
|
|
|
|
* @see Zend_Form_Element::init() For the method documentation.
|
2013-08-27 16:00:45 +02:00
|
|
|
|
*/
|
2014-09-03 14:40:58 +02:00
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->addValidator(
|
|
|
|
|
new DateTimeValidator($this->local), true // true for breaking the validator chain on failure
|
|
|
|
|
);
|
|
|
|
|
if ($this->min !== null) {
|
|
|
|
|
$this->addValidator('GreaterThan', true, array('min' => $this->min));
|
|
|
|
|
}
|
|
|
|
|
if ($this->max !== null) {
|
|
|
|
|
$this->addValidator('LessThan', true, array('max' => $this->max));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setLocal($local)
|
|
|
|
|
{
|
|
|
|
|
$this->local = (bool) $local;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getLocal()
|
|
|
|
|
{
|
|
|
|
|
return $this->local;
|
|
|
|
|
}
|
2013-08-27 16:00:45 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* Set the expected lower bound for the element’s value
|
|
|
|
|
*
|
|
|
|
|
* @param DateTime $min
|
2013-08-07 17:14:58 +02:00
|
|
|
|
*
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* @return $this
|
2013-08-07 17:14:58 +02:00
|
|
|
|
*/
|
2014-09-03 14:40:58 +02:00
|
|
|
|
public function setMin(DateTime $min)
|
2013-08-07 17:14:58 +02:00
|
|
|
|
{
|
2014-09-03 14:40:58 +02:00
|
|
|
|
$this->min = $min;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the expected lower bound for the element’s value
|
|
|
|
|
*
|
|
|
|
|
* @return DateTime|null
|
|
|
|
|
*/
|
|
|
|
|
public function getMin()
|
|
|
|
|
{
|
|
|
|
|
return $this->min;
|
|
|
|
|
}
|
2013-10-20 15:30:49 +02:00
|
|
|
|
|
2014-09-03 14:40:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* Set the expected upper bound for the element’s value
|
|
|
|
|
*
|
|
|
|
|
* @param DateTime $max
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function setMax(DateTime $max)
|
|
|
|
|
{
|
|
|
|
|
$this->max = $max;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2013-10-20 15:30:49 +02:00
|
|
|
|
|
2014-09-03 14:40:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* Get the expected upper bound for the element’s value
|
|
|
|
|
*
|
|
|
|
|
* @return DateTime|null
|
|
|
|
|
*/
|
|
|
|
|
public function getMax()
|
|
|
|
|
{
|
|
|
|
|
return $this->max;
|
2013-08-07 17:14:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* Is the date and time valid?
|
2013-08-07 17:14:58 +02:00
|
|
|
|
*
|
2014-09-03 14:40:58 +02:00
|
|
|
|
* @param string|DateTime $value
|
|
|
|
|
* @param mixed $context
|
2013-08-07 17:14:58 +02:00
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isValid($value, $context = null)
|
|
|
|
|
{
|
2014-09-03 14:40:58 +02:00
|
|
|
|
if (! parent::isValid($value, $context)) {
|
2013-08-07 17:14:58 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-02-17 12:50:11 +01:00
|
|
|
|
|
2014-09-03 14:40:58 +02:00
|
|
|
|
if (! $value instanceof DateTime) {
|
|
|
|
|
$format = $this->local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339;
|
2015-02-17 12:50:11 +01:00
|
|
|
|
$dateTime = DateTime::createFromFormat($format, $value);
|
|
|
|
|
if ($dateTime === false) {
|
|
|
|
|
$dateTime = DateTime::createFromFormat(substr($format, 0, strrpos($format, ':')), $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->setValue($dateTime);
|
2013-08-07 17:14:58 +02:00
|
|
|
|
}
|
2015-02-17 12:50:11 +01:00
|
|
|
|
|
2013-08-07 17:14:58 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-07-17 14:08:07 +02:00
|
|
|
|
}
|