2013-08-07 01:47:32 +02:00
|
|
|
<?php
|
2013-08-07 10:53:44 +02:00
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-08-07 01:47:32 +02:00
|
|
|
|
|
|
|
use \DateTime;
|
2013-08-12 14:18:58 +02:00
|
|
|
use \Icinga\Application\Icinga;
|
|
|
|
use \Icinga\Application\Config as IcingaConfig;
|
|
|
|
use \Icinga\Util\DateTimeFactory;
|
2013-08-28 15:55:21 +02:00
|
|
|
use \Zend_Controller_Request_Http;
|
2013-08-30 17:42:39 +02:00
|
|
|
use \Icinga\Web\Form\Validator\DateTimeValidator;
|
2013-08-07 01:47:32 +02:00
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-12 14:18:58 +02:00
|
|
|
* Helper to format date and time. Utilizes DateTimeFactory to ensure time zone awareness
|
|
|
|
*
|
|
|
|
* @see \Icinga\Util\DateTimeFactory::create()
|
2013-08-07 10:53:44 +02:00
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|
|
|
{
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
|
|
|
* Current request
|
2013-08-28 15:55:21 +02:00
|
|
|
*
|
2013-08-07 10:53:44 +02:00
|
|
|
* @var Zend_Controller_Request_Abstract
|
|
|
|
*/
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2013-08-28 15:55:21 +02:00
|
|
|
* Retrieves the request from the application's front controller if not given.
|
|
|
|
*
|
|
|
|
* @param Zend_Controller_Request_Abstract $request The request to use
|
2013-08-07 10:53:44 +02:00
|
|
|
*/
|
2013-08-28 15:55:21 +02:00
|
|
|
public function __construct(Zend_Controller_Request_Abstract $request = null)
|
2013-08-07 10:53:44 +02:00
|
|
|
{
|
2013-08-28 15:55:21 +02:00
|
|
|
if ($request === null) {
|
|
|
|
$this->request = Icinga::app()->getFrontController()->getRequest();
|
|
|
|
} else {
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
2013-08-07 10:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper entry point
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function dateFormat()
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-07 17:13:47 +02:00
|
|
|
* Return date formatted according to given format respecting the user's timezone
|
|
|
|
*
|
|
|
|
* @param int $timestamp
|
|
|
|
* @param string $format
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-08-28 10:50:10 +02:00
|
|
|
public function format($timestamp, $format)
|
2013-08-07 17:13:47 +02:00
|
|
|
{
|
2013-08-12 14:18:58 +02:00
|
|
|
$dt = DateTimeFactory::create();
|
2013-08-30 17:42:39 +02:00
|
|
|
if (DateTimeValidator::isUnixTimestamp($timestamp)) {
|
|
|
|
$dt->setTimestamp($timestamp);
|
|
|
|
} else {
|
|
|
|
return $timestamp;
|
|
|
|
}
|
2013-08-07 17:13:47 +02:00
|
|
|
return $dt->format($format);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format date according to user's format
|
2013-08-07 10:53:44 +02:00
|
|
|
*
|
|
|
|
* @param int $timestamp A unix timestamp
|
|
|
|
* @return string The formatted date string
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function formatDate($timestamp)
|
|
|
|
{
|
2013-08-07 17:13:47 +02:00
|
|
|
return $this->format($timestamp, $this->getDateFormat());
|
2013-08-07 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-07 17:13:47 +02:00
|
|
|
* Format time according to user's format
|
2013-08-07 10:53:44 +02:00
|
|
|
*
|
|
|
|
* @param int $timestamp A unix timestamp
|
|
|
|
* @return string The formatted time string
|
|
|
|
*/
|
|
|
|
public function formatTime($timestamp)
|
2013-08-07 01:47:32 +02:00
|
|
|
{
|
2013-08-07 17:13:47 +02:00
|
|
|
return $this->format($timestamp, $this->getTimeFormat());
|
2013-08-07 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-07 17:13:47 +02:00
|
|
|
* Format datetime according to user's format
|
2013-08-07 10:53:44 +02:00
|
|
|
*
|
|
|
|
* @param int $timestamp A unix timestamp
|
|
|
|
* @return string The formatted datetime string
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function formatDateTime($timestamp)
|
|
|
|
{
|
2013-08-07 17:13:47 +02:00
|
|
|
return $this->format($timestamp, $this->getDateTimeFormat());
|
2013-08-07 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-07 17:13:47 +02:00
|
|
|
* Retrieve the user's date format string
|
2013-08-07 10:53:44 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function getDateFormat()
|
|
|
|
{
|
2013-08-07 10:53:44 +02:00
|
|
|
return $this->request->getUser()->getPreferences()->get(
|
2013-08-07 01:47:32 +02:00
|
|
|
'dateFormat', IcingaConfig::app()->global->get('dateFormat', 'Y-m-d')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-07 17:13:47 +02:00
|
|
|
* Retrieve the user's time format string
|
2013-08-07 10:53:44 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function getTimeFormat()
|
|
|
|
{
|
2013-08-07 10:53:44 +02:00
|
|
|
return $this->request->getUser()->getPreferences()->get(
|
2013-08-07 01:47:32 +02:00
|
|
|
'timeFormat', IcingaConfig::app()->global->get('timeFormat', 'H:i:s')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-07 10:53:44 +02:00
|
|
|
/**
|
2013-08-07 17:13:47 +02:00
|
|
|
* Retrieve the user's datetime format string
|
2013-08-07 10:53:44 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function getDateTimeFormat()
|
|
|
|
{
|
|
|
|
return $this->getDateFormat() . ' ' . $this->getTimeFormat();
|
|
|
|
}
|
|
|
|
}
|
2013-08-07 10:53:44 +02:00
|
|
|
|
|
|
|
// @codingStandardsIgnoreStop
|