Framework: Add doctrings to DateFormat view helper

refs #4424
This commit is contained in:
Eric Lippmann 2013-08-07 10:53:44 +02:00
parent da7f619804
commit 5c98acd36c
2 changed files with 76 additions and 13 deletions

View File

@ -1,62 +1,125 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
use \DateTime; use \DateTime;
use \DateTimeZone; use \DateTimeZone;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Config as IcingaConfig; use Icinga\Application\Config as IcingaConfig;
/**
* Helper to format date and time
*/
class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
{ {
/**
* Current request
* @var Zend_Controller_Request_Abstract
*/
private $request;
/**
* Constructor
*
* Retrieve current request
*/
public function __construct()
{
$this->request = Icinga::app()->getFrontController()->getRequest();
}
/**
* Helper entry point
*
* @return self
*/
public function dateFormat() public function dateFormat()
{ {
return $this; return $this;
} }
/**
* Format date according to current user's format
*
* @param int $timestamp A unix timestamp
* @return string The formatted date string
*/
public function formatDate($timestamp) public function formatDate($timestamp)
{ {
$dt = new DateTime($timestamp, $this->getTimeZone()); $dt = new DateTime($timestamp, $this->getTimeZone());
return $dt->format($this->getDateFormat()); return $dt->format($this->getDateFormat());
} }
public function timeFormat($timestamp) /**
* Format time according to current user's format
*
* @param int $timestamp A unix timestamp
* @return string The formatted time string
*/
public function formatTime($timestamp)
{ {
$dt = new DateTime($timestamp, $this->getTimeZone()); $dt = new DateTime($timestamp, $this->getTimeZone());
return $dt->format($this->getTimeFormat()); return $dt->format($this->getTimeFormat());
} }
/**
* Format datetime according to current user's format
*
* @param int $timestamp A unix timestamp
* @return string The formatted datetime string
*/
public function formatDateTime($timestamp) public function formatDateTime($timestamp)
{ {
$dt = new DateTime($timestamp, $this->getTimeZone()); $dt = new DateTime($timestamp, $this->getTimeZone());
return $dt->format($this->getDateTimeFormat()); return $dt->format($this->getDateTimeFormat());
} }
private function getRequest() /**
{ * Retrieve the current user's timezone
// TODO(el/WIP): Set via constructor *
return Icinga::app()->getFrontController()->getRequest(); * @return DateTimeZone
} */
private function getTimeZone() private function getTimeZone()
{ {
return new DateTimeZone($this->getRequest()->getUser()->getTimeZone()); return new DateTimeZone($this->request->getUser()->getTimeZone());
} }
/**
* Retrieve the current user's date format string
*
* @return string
*/
public function getDateFormat() public function getDateFormat()
{ {
return $this->getRequest()->getUser()->getPreferences()->get( return $this->request->getUser()->getPreferences()->get(
'dateFormat', IcingaConfig::app()->global->get('dateFormat', 'Y-m-d') 'dateFormat', IcingaConfig::app()->global->get('dateFormat', 'Y-m-d')
); );
} }
/**
* Retrieve the current user's time format string
*
* @return string
*/
public function getTimeFormat() public function getTimeFormat()
{ {
return $this->getRequest()->getUser()->getPreferences()->get( return $this->request->getUser()->getPreferences()->get(
'timeFormat', IcingaConfig::app()->global->get('timeFormat', 'H:i:s') 'timeFormat', IcingaConfig::app()->global->get('timeFormat', 'H:i:s')
); );
} }
/**
* Retrieve the current user's datetime format string
*
* @return string
*/
public function getDateTimeFormat() public function getDateTimeFormat()
{ {
return $this->getDateFormat() . ' ' . $this->getTimeFormat(); return $this->getDateFormat() . ' ' . $this->getTimeFormat();
} }
} }
// @codingStandardsIgnoreStop

View File

@ -36,9 +36,9 @@ class Zend_View_Helper_FormDateTime extends Zend_View_Helper_FormElement
/** /**
* Generate a 'datetime' element * Generate a 'datetime' element
* *
* @param string $name The element name * @param string $name The element name
* @param int $value The default timestamp * @param int $value The default timestamp
* @param array $attribs Attributes for the element tag * @param array $attribs Attributes for the element tag
* *
* @return string The element XHTML * @return string The element XHTML
*/ */