2013-07-16 15:39:47 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 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-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(
|
2017-01-27 14:48:59 +01:00
|
|
|
new DateTimeValidator($this->local),
|
|
|
|
true // true for breaking the validator chain on failure
|
2014-09-03 14:40:58 +02:00
|
|
|
);
|
2013-08-07 17:14:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 17:19:57 +02:00
|
|
|
/**
|
|
|
|
* Get the expected date and time format of any user input
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFormat()
|
|
|
|
{
|
|
|
|
return $this->local ? 'Y-m-d\TH:i:s' : DateTime::RFC3339;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2017-02-13 18:20:22 +01:00
|
|
|
if (is_scalar($value) && $value !== '' && ! preg_match('/\D/', $value)) {
|
|
|
|
$dateTime = new DateTime();
|
|
|
|
$value = $dateTime->setTimestamp($value)->format($this->getFormat());
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-04-18 17:19:57 +02:00
|
|
|
$format = $this->getFormat();
|
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
|
|
|
}
|