Framework: Implement DateFormat view helper

Support date, time and datetime formatting based on format strings
set either by the user or via config.ini. The view helper
FormDateTime uses the new helper already

refs #4440
refs #4424
This commit is contained in:
Eric Lippmann 2013-08-07 01:47:32 +02:00
parent 7c732ef682
commit da7f619804
6 changed files with 71 additions and 8 deletions

View File

@ -0,0 +1,62 @@
<?php
use \DateTime;
use \DateTimeZone;
use Icinga\Application\Icinga;
use Icinga\Application\Config as IcingaConfig;
class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
{
public function dateFormat()
{
return $this;
}
public function formatDate($timestamp)
{
$dt = new DateTime($timestamp, $this->getTimeZone());
return $dt->format($this->getDateFormat());
}
public function timeFormat($timestamp)
{
$dt = new DateTime($timestamp, $this->getTimeZone());
return $dt->format($this->getTimeFormat());
}
public function formatDateTime($timestamp)
{
$dt = new DateTime($timestamp, $this->getTimeZone());
return $dt->format($this->getDateTimeFormat());
}
private function getRequest()
{
// TODO(el/WIP): Set via constructor
return Icinga::app()->getFrontController()->getRequest();
}
private function getTimeZone()
{
return new DateTimeZone($this->getRequest()->getUser()->getTimeZone());
}
public function getDateFormat()
{
return $this->getRequest()->getUser()->getPreferences()->get(
'dateFormat', IcingaConfig::app()->global->get('dateFormat', 'Y-m-d')
);
}
public function getTimeFormat()
{
return $this->getRequest()->getUser()->getPreferences()->get(
'timeFormat', IcingaConfig::app()->global->get('timeFormat', 'H:i:s')
);
}
public function getDateTimeFormat()
{
return $this->getDateFormat() . ' ' . $this->getTimeFormat();
}
}

View File

@ -55,8 +55,7 @@ class Zend_View_Helper_FormDateTime extends Zend_View_Helper_FormElement
// Do we have a value?
if (isset($value) && !empty($value)) {
$dt = new DateTime($value);
$value = ' value="' . $dt->format('Y-m-d H:i:s') . '"';
$value = ' value="' . $this->view->dateFormat()->formatDateTime($value) . '"';
} else {
$value = '';
}

View File

@ -4,6 +4,8 @@ timezone = "Europe/Berlin"
indexModule = monitoring
indexController = dashboard
moduleFolder = "/etc/icinga2-web/enabledModules"
dateFormat = "d/m/Y"
timeFormat = "g:i A"
[logging]
; General log
@ -29,4 +31,4 @@ type=ini
;dbhost=127.0.0.1
;dbpassword=icingaweb
;dbuser=icingaweb
;dbname=icingaweb
;dbname=icingaweb

View File

@ -324,7 +324,7 @@ class User
*
* @return string
*/
public function getTimezone()
public function getTimeZone()
{
$tz = $this->preferences->get('timezone');
if ($tz === null) {

View File

@ -36,8 +36,8 @@ use Zend_Form_Element_Xhtml;
class DateTimePicker extends Zend_Form_Element_Xhtml
{
/**
* Default form view helper to use for rendering
* View helper to use
* @var string
*/
public $helper = "formDateTime";
public $helper = 'formDateTime';
}

View File

@ -49,7 +49,7 @@ class UserTest extends \PHPUnit_Framework_TestCase
$user = new IcingaUser('unittest');
$prefs = new UserPreferences(array());
$user->setPreferences($prefs);
$this->assertEquals($user->getTimezone(), $defaultTz,
$this->assertEquals($user->getTimeZone(), $defaultTz,
'User\'s timezone does not match the default timezone'
);
}
@ -64,7 +64,7 @@ class UserTest extends \PHPUnit_Framework_TestCase
'timezone' => $explicitTz
));
$user->setPreferences($prefs);
$this->assertEquals($user->getTimezone(), $explicitTz,
$this->assertEquals($user->getTimeZone(), $explicitTz,
'User\'s timezone does not match the timezone set by himself'
);
}