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-07 17:14:58 +02:00
|
|
|
use \Exception;
|
2013-08-12 13:56:14 +02:00
|
|
|
use \Zend_Form_Element_Xhtml;
|
|
|
|
use \Icinga\Application\Icinga;
|
|
|
|
use \Icinga\Util\DateTimeFactory;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds 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
|
|
|
|
*
|
2013-08-12 13:56:14 +02:00
|
|
|
* 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
|
2013-08-07 17:14:58 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @param mixed $context
|
|
|
|
* @return bool
|
2013-08-12 13:56:14 +02:00
|
|
|
* @see \Icinga\Util\DateTimeFactory::create()
|
2013-08-07 17:14:58 +02:00
|
|
|
*/
|
|
|
|
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)) {
|
|
|
|
// Using the Unix timestamp format to construct a new DateTime
|
|
|
|
$value = '@' . $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-08-12 13:56:14 +02:00
|
|
|
$dt = DateTimeFactory::create($value);
|
2013-08-07 17:14:58 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|