2013-07-16 15:39:47 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Web\Form\Element;
|
|
|
|
|
2013-08-12 13:56:14 +02:00
|
|
|
use \Zend_Form_Element_Xhtml;
|
|
|
|
use \Icinga\Util\DateTimeFactory;
|
2013-08-27 16:00:45 +02:00
|
|
|
use \Icinga\Exception\ProgrammingError;
|
2013-07-16 15:39:47 +02:00
|
|
|
|
2013-08-06 19:05:16 +02:00
|
|
|
/**
|
2013-08-12 13:56:14 +02:00
|
|
|
* 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()
|
2013-08-06 19:05:16 +02:00
|
|
|
*/
|
2013-08-06 16:08:15 +02:00
|
|
|
class DateTimePicker extends Zend_Form_Element_Xhtml
|
2013-07-16 15:39:47 +02:00
|
|
|
{
|
|
|
|
/**
|
2013-08-07 01:47:32 +02:00
|
|
|
* View helper to use
|
2013-07-16 15:39:47 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public $helper = 'formDateTime';
|
2013-08-07 17:14:58 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-27 16:00:45 +02:00
|
|
|
* Valid formats to check user input against
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $patterns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a variable is a Unix timestamp
|
2013-08-07 17:14:58 +02:00
|
|
|
*
|
|
|
|
* @param mixed $timestamp
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isUnixTimestamp($timestamp)
|
|
|
|
{
|
2013-08-27 16:00:45 +02:00
|
|
|
return (is_int($timestamp) || ctype_digit($timestamp))
|
2013-08-07 17:14:58 +02:00
|
|
|
&& ($timestamp <= PHP_INT_MAX)
|
|
|
|
&& ($timestamp >= ~PHP_INT_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate filtered date/time strings
|
|
|
|
*
|
2013-08-27 16:00:45 +02:00
|
|
|
* Expects one or more valid formats being set in $this->patterns. Sets element value as Unix timestamp
|
|
|
|
* if the input is considered valid. Utilizes DateTimeFactory to ensure time zone awareness.
|
2013-08-07 17:14:58 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @param mixed $context
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid($value, $context = null)
|
|
|
|
{
|
2013-08-12 13:56:14 +02:00
|
|
|
if (!parent::isValid($value, $context)) {
|
2013-08-07 17:14:58 +02:00
|
|
|
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)) {
|
2013-08-12 14:18:58 +02:00
|
|
|
$dt = DateTimeFactory::create();
|
|
|
|
$dt->setTimestamp($value);
|
|
|
|
} else {
|
2013-08-27 16:00:45 +02:00
|
|
|
if (!isset($this->patterns)) {
|
|
|
|
throw new ProgrammingError('Cannot parse datetime string without any pattern');
|
|
|
|
}
|
|
|
|
|
|
|
|
$match_found = false;
|
|
|
|
foreach ($this->patterns as $pattern) {
|
|
|
|
$dt = DateTimeFactory::parse($value, $pattern);
|
|
|
|
if ($dt !== false && $dt->format($pattern) === $value) {
|
|
|
|
$match_found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$match_found) {
|
2013-08-12 14:18:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-07 17:14:58 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 13:56:14 +02:00
|
|
|
$this->setValue($dt->getTimestamp());
|
|
|
|
|
2013-08-07 17:14:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-17 14:08:07 +02:00
|
|
|
}
|