* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Web\Form\Element; use \Exception; use \Zend_Form_Element_Xhtml; use \Icinga\Application\Icinga; use \Icinga\Util\DateTimeFactory; /** * Datetime form element which returns the input as Unix timestamp after the input has been proven valid. Utilizes * DateTimeFactory to ensure time zone awareness * * @see isValid() */ class DateTimePicker extends Zend_Form_Element_Xhtml { /** * View helper to use * @var string */ public $helper = 'formDateTime'; /** * Find whether a variable is a Unix timestamp * * @param mixed $timestamp * @return bool */ public function isUnixTimestamp($timestamp) { return ((string) (int) $timestamp === (string) $timestamp) && ($timestamp <= PHP_INT_MAX) && ($timestamp >= ~PHP_INT_MAX); } /** * Validate filtered date/time strings * * Expects formats that the php date parser understands. Sets element value as Unix timestamp if the input is * considered valid. Utilizes DateTimeFactory to ensure time zone awareness * * @param string $value * @param mixed $context * @return bool * @see \Icinga\Util\DateTimeFactory::create() */ public function isValid($value, $context = null) { if (!parent::isValid($value, $context)) { return false; } if (!is_string($value) && !is_int($value)) { $this->addErrorMessage( _('Invalid type given. Date/time string or Unix timestamp expected') ); return false; } if ($this->isUnixTimestamp($value)) { $dt = DateTimeFactory::create(); $dt->setTimestamp($value); } else { try { $dt = DateTimeFactory::create($value); } catch (Exception $e) { $this->addErrorMessage( _( 'Failed to parse datetime string. See ' . 'http://www.php.net/manual/en/datetime.formats.php for valid formats' ) ); return false; } } $this->setValue($dt->getTimestamp()); return true; } }