From da7f6198041e79fb6813e81bf7608d6e521c0e34 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 7 Aug 2013 01:47:32 +0200 Subject: [PATCH] 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 --- application/views/helpers/DateFormat.php | 62 +++++++++++++++++++ application/views/helpers/FormDateTime.php | 3 +- config/config.ini | 4 +- library/Icinga/User.php | 2 +- .../Web/Form/Element/DateTimePicker.php | 4 +- test/php/library/Icinga/UserTest.php | 4 +- 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 application/views/helpers/DateFormat.php diff --git a/application/views/helpers/DateFormat.php b/application/views/helpers/DateFormat.php new file mode 100644 index 000000000..3a77bc791 --- /dev/null +++ b/application/views/helpers/DateFormat.php @@ -0,0 +1,62 @@ +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(); + } +} diff --git a/application/views/helpers/FormDateTime.php b/application/views/helpers/FormDateTime.php index 68b1c4281..5d8618238 100644 --- a/application/views/helpers/FormDateTime.php +++ b/application/views/helpers/FormDateTime.php @@ -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 = ''; } diff --git a/config/config.ini b/config/config.ini index 72ec0985f..d2a539fcc 100755 --- a/config/config.ini +++ b/config/config.ini @@ -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 \ No newline at end of file +;dbname=icingaweb diff --git a/library/Icinga/User.php b/library/Icinga/User.php index 2c250f896..01542481c 100644 --- a/library/Icinga/User.php +++ b/library/Icinga/User.php @@ -324,7 +324,7 @@ class User * * @return string */ - public function getTimezone() + public function getTimeZone() { $tz = $this->preferences->get('timezone'); if ($tz === null) { diff --git a/library/Icinga/Web/Form/Element/DateTimePicker.php b/library/Icinga/Web/Form/Element/DateTimePicker.php index 37f58c350..819c76821 100644 --- a/library/Icinga/Web/Form/Element/DateTimePicker.php +++ b/library/Icinga/Web/Form/Element/DateTimePicker.php @@ -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'; } diff --git a/test/php/library/Icinga/UserTest.php b/test/php/library/Icinga/UserTest.php index dd4293769..fd76a44fe 100644 --- a/test/php/library/Icinga/UserTest.php +++ b/test/php/library/Icinga/UserTest.php @@ -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' ); }