Merge branch 'feature/date-and-time-preview-4609'

resolves #4609
fixes #4552
This commit is contained in:
Marius Hein 2013-08-29 15:11:06 +02:00
commit 12b3f5f732
6 changed files with 160 additions and 26 deletions

View File

@ -32,6 +32,7 @@ use \DateTimeZone;
use \Zend_Config;
use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select;
use \Zend_View_Helper_DateFormat;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Web\Form;
@ -42,7 +43,6 @@ use \Icinga\Web\Form\Decorator\ConditionalHidden;
/**
* Configuration form for general, application-wide settings
*
*/
class GeneralForm extends Form
{
@ -66,12 +66,19 @@ class GeneralForm extends Form
*
* @var null
*/
private $resources = null;
private $resources;
/**
* The view helper to format date/time strings
*
* @var Zend_View_Helper_DateFormat
*/
private $dateHelper;
/**
* Set the configuration to be used for this form
*
* @param IcingaConfig $cfg
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
@ -81,7 +88,7 @@ class GeneralForm extends Form
/**
* Set a specific configuration directory to use for configuration specific default paths
*
* @param string $dir
* @param string $dir
*/
public function setConfigDir($dir)
{
@ -100,11 +107,34 @@ class GeneralForm extends Form
return $this->configDir === null ? IcingaConfig::$configDir : $this->configDir;
}
/**
* Return the view helper to format date/time strings
*
* @return Zend_View_Helper_DateFormat
*/
public function getDateFormatter()
{
if ($this->dateHelper === null) {
return $this->getView()->dateFormat();
}
return $this->dateHelper;
}
/**
* Set the view helper that is used to format date/time strings (used for testing)
*
* @param Zend_View_Helper_DateFormat $dateHelper
*/
public function setDateFormatter(Zend_View_Helper_DateFormat $dateHelper)
{
$this->dateHelper = $dateHelper;
}
/**
* Set an alternative array of resources that should be used instead of the DBFactory resource set
* (used for testing)
*
* @param array $resources The resources to use for populating the db selection field
* @param array $resources The resources to use for populating the db selection field
*/
public function setResources(array $resources)
{
@ -128,7 +158,7 @@ class GeneralForm extends Form
/**
* Add the checkbox for using the development environment to this form
*
* @param Zend_Config $cfg The "global" section of the config.ini
* @param Zend_Config $cfg The "global" section of the config.ini
*/
private function addDevelopmentCheckbox(Zend_Config $cfg)
{
@ -153,7 +183,7 @@ class GeneralForm extends Form
*
* Possible values are determined by DateTimeZone::listIdentifiers
*
* @param Zend_Config $cfg The "global" section of the config.ini
* @param Zend_Config $cfg The "global" section of the config.ini
*/
private function addTimezoneSelection(Zend_Config $cfg)
{
@ -180,7 +210,7 @@ class GeneralForm extends Form
/**
* Add configuration settings for module paths
*
* @param Zend_Config $cfg The "global" section of the config.ini
* @param Zend_Config $cfg The "global" section of the config.ini
*/
private function addModuleSettings(Zend_Config $cfg)
{
@ -202,32 +232,42 @@ class GeneralForm extends Form
/**
* Add text fields for the date and time format used in the application
*
* @param Zend_Config $cfg The "global" section of the config.ini
* @param Zend_Config $cfg The "global" section of the config.ini
*/
private function addDateFormatSettings(Zend_Config $cfg)
{
$phpUrl = '<a href="http://php.net/manual/en/function.date.php" target="_new">'
. 'the official PHP documentation</a>';
$dateFormatValue = $this->getRequest()->getParam('date_format', '');
if (empty($dateFormatValue)) {
$dateFormatValue = $cfg->get('dateFormat', 'd/m/Y');
}
$txtDefaultDateFormat = new Zend_Form_Element_Text(
array(
'name' => 'date_format',
'label' => 'Date Format',
'helptext' => 'Display dates according to this format. See ' . $phpUrl . ' for possible values',
'helptext' => 'Display dates according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getDateFormatter()->format(time(), $dateFormatValue),
'required' => true,
'value' => $cfg->get('dateFormat', 'd/m/Y')
'value' => $dateFormatValue
)
);
$this->addElement($txtDefaultDateFormat);
$txtDefaultDateFormat->addValidator(new DateFormatValidator());
$timeFormatValue = $this->getRequest()->getParam('time_format', '');
if (empty($timeFormatValue)) {
$timeFormatValue = $cfg->get('timeFormat', 'g:i A');
}
$txtDefaultTimeFormat = new Zend_Form_Element_Text(
array(
'name' => 'time_format',
'label' => 'Time Format',
'required' => true,
'helptext' => 'Display times according to this format. See ' . $phpUrl . ' for possible values',
'value' => $cfg->get('timeFormat', 'g:i A')
'helptext' => 'Display times according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getDateFormatter()->format(time(), $timeFormatValue),
'value' => $timeFormatValue
)
);
$txtDefaultTimeFormat->addValidator(new TimeFormatValidator());
@ -237,7 +277,7 @@ class GeneralForm extends Form
/**
* Add form elements for setting the user preference storage backend
*
* @param Zend_Config $cfg The Zend_config object of preference section
* @param Zend_Config $cfg The Zend_config object of preference section
*/
public function addUserPreferencesDialog(Zend_Config $cfg)
{

View File

@ -32,6 +32,7 @@ use \DateTimeZone;
use \Zend_Config;
use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select;
use \Zend_View_Helper_DateFormat;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
use \Icinga\User\Preferences;
@ -58,6 +59,13 @@ class GeneralForm extends Form
*/
private $preferences;
/**
* The view helper to format date/time strings
*
* @var Zend_View_Helper_DateFormat
*/
private $dateHelper;
/**
* Set the configuration to be used for this form when no preferences are set yet
*
@ -91,6 +99,29 @@ class GeneralForm extends Form
return $this->getRequest()->getUser()->getPreferences();
}
/**
* Return the view helper to format date/time strings
*
* @return Zend_View_Helper_DateFormat
*/
public function getDateFormatter()
{
if ($this->dateHelper === null) {
return $this->getView()->dateFormat();
}
return $this->dateHelper;
}
/**
* Set the view helper that is used to format date/time strings (used for testing)
*
* @param Zend_View_Helper_DateFormat $dateHelper
*/
public function setDateFormatter(Zend_View_Helper_DateFormat $dateHelper)
{
$this->dateHelper = $dateHelper;
}
/**
* Add a select field for setting the user's timezone.
*
@ -161,13 +192,18 @@ class GeneralForm extends Form
'required' => true
)
);
$dateFormatValue = $this->getRequest()->getParam('date_format', '');
if (empty($dateFormatValue)) {
$dateFormatValue = $prefs->get('app.dateFormat', $cfg->get('dateFormat', 'd/m/Y'));
}
$txtDefaultDateFormat = new Zend_Form_Element_Text(
array(
'name' => 'date_format',
'label' => 'Preferred Date Format',
'helptext' => 'Display dates according to this format. See ' . $phpUrl . ' for possible values',
'helptext' => 'Display dates according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getDateFormatter()->format(time(), $dateFormatValue),
'required' => !$useGlobalDateFormat,
'value' => $prefs->get('app.dateFormat', $cfg->get('dateFormat', 'd/m/Y'))
'value' => $dateFormatValue
)
);
@ -186,13 +222,18 @@ class GeneralForm extends Form
'required' => !$useGlobalTimeFormat
)
);
$timeFormatValue = $this->getRequest()->getParam('time_format', '');
if (empty($timeFormatValue)) {
$timeFormatValue = $prefs->get('app.timeFormat', $cfg->get('timeFormat', 'g:i A'));
}
$txtDefaultTimeFormat = new Zend_Form_Element_Text(
array(
'name' => 'time_format',
'label' => 'Preferred Time Format',
'required' => !$useGlobalTimeFormat,
'helptext' => 'Display times according to this format. See ' . $phpUrl . ' for possible values',
'value' => $prefs->get('app.timeFormat', $cfg->get('timeFormat', 'g:i A'))
'helptext' => 'Display times according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getDateFormatter()->format(time(), $timeFormatValue),
'value' => $timeFormatValue
)
);
$txtDefaultTimeFormat->addValidator(new TimeFormatValidator());

View File

@ -8,6 +8,7 @@ use \DateTime;
use \Icinga\Application\Icinga;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Util\DateTimeFactory;
use \Zend_Controller_Request_Http;
/**
* Helper to format date and time. Utilizes DateTimeFactory to ensure time zone awareness
@ -16,9 +17,9 @@ use \Icinga\Util\DateTimeFactory;
*/
class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
{
/**
* Current request
*
* @var Zend_Controller_Request_Abstract
*/
private $request;
@ -26,11 +27,17 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
/**
* Constructor
*
* Retrieve request
* Retrieves the request from the application's front controller if not given.
*
* @param Zend_Controller_Request_Abstract $request The request to use
*/
public function __construct()
public function __construct(Zend_Controller_Request_Abstract $request = null)
{
$this->request = Icinga::app()->getFrontController()->getRequest();
if ($request === null) {
$this->request = Icinga::app()->getFrontController()->getRequest();
} else {
$this->request = $request;
}
}
/**
@ -50,7 +57,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
* @param string $format
* @return string
*/
private function format($timestamp, $format)
public function format($timestamp, $format)
{
$dt = DateTimeFactory::create();
$dt->setTimestamp($timestamp);

View File

@ -28,13 +28,13 @@
namespace Icinga\Application;
use \DateTimeZone;
use \Exception;
use \Zend_Layout;
use \Zend_Paginator;
use \Zend_View_Helper_PaginationControl;
use \Zend_Controller_Action_HelperBroker;
use \Zend_Controller_Router_Route;
use \Zend_Controller_Action_Helper_ViewRenderer;
use \Zend_Controller_Front;
use \Icinga\Application\Logger;
use \Icinga\Authentication\Manager as AuthenticationManager;
@ -46,6 +46,7 @@ use \Icinga\Web\Request;
use \Icinga\Web\View;
use \Icinga\User\Preferences\StoreFactory;
use \Icinga\User\Preferences\SessionStore;
use \Icinga\Util\DateTimeFactory;
/**
* Use this if you want to make use of Icinga functionality in other web projects
@ -102,9 +103,9 @@ class Web extends ApplicationBootstrap
{
return $this->setupConfig()
->setupErrorHandling()
->setupTimezone()
->setupResourceFactories()
->setupUser()
->setupTimezone()
->setupRequest()
->setupZendMvc()
->setupTranslation()
@ -400,4 +401,25 @@ class Web extends ApplicationBootstrap
return $this;
}
/**
* Setup user timezone if set and valid, otherwise global default timezone
*
* @return self
* @see ApplicationBootstrap::setupTimezone
*/
protected function setupTimezone()
{
$userTimeZone = $this->user === null ? null : $this->user->getPreferences()->get('app.timezone');
try {
$tz = new DateTimeZone($userTimeZone);
} catch (Exception $e) {
return parent::setupTimezone();
}
date_default_timezone_set($userTimeZone);
DateTimeFactory::setConfig(array('timezone' => $tz));
return $this;
}
}

View File

@ -37,14 +37,21 @@ use Icinga\Test\BaseTestCase;
require_once 'Zend/Form.php';
require_once 'Zend/Config.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/View/Helper/Abstract.php';
require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Config/GeneralForm.php';
require_once BaseTestCase::$appDir . '/views/helpers/DateFormat.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd
use \DateTimeZone;
use \Icinga\Web\Form;
use \DOMDocument;
use \Zend_Config;
use \Zend_View;
use \Zend_View_Helper_DateFormat;
use \Icinga\Util\DateTimeFactory;
class GeneralFormTest extends BaseTestCase
{
@ -71,8 +78,10 @@ class GeneralFormTest extends BaseTestCase
*/
public function testCorrectFieldPopulation()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Config\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setConfiguration(
new Zend_Config(
array(
@ -146,8 +155,10 @@ class GeneralFormTest extends BaseTestCase
public function testCorrectConditionalIniFieldRendering()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Config\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setConfiguration(
new Zend_Config(
array(
@ -182,8 +193,10 @@ class GeneralFormTest extends BaseTestCase
public function testCorrectConditionalDbFieldRendering()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Config\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setConfiguration(
new Zend_Config(
array(

View File

@ -38,15 +38,22 @@ use Icinga\Test\BaseTestCase;
require_once 'Zend/Config.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Form/Element/Select.php';
require_once 'Zend/View/Helper/Abstract.php';
require_once BaseTestCase::$libDir . '/User/Preferences.php';
require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Preference/GeneralForm.php';
require_once BaseTestCase::$libDir . '/User/Preferences/ChangeSet.php';
require_once BaseTestCase::$appDir . '/views/helpers/DateFormat.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd
use \DateTimeZone;
use \Icinga\Web\Form;
use \Zend_Config;
use Icinga\User\Preferences;
use \Icinga\User\Preferences;
use \Zend_View_Helper_DateFormat;
use \Icinga\Util\DateTimeFactory;
/**
* Test for general form, mainly testing enable/disable behaviour
@ -60,8 +67,10 @@ class GeneralFormTest extends BaseTestCase
*/
public function testDisableFormIfUsingDefault()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Preference\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setRequest($this->getRequest());
$form->setConfiguration(
new Zend_Config(
@ -89,8 +98,10 @@ class GeneralFormTest extends BaseTestCase
*/
public function testEnableFormIfUsingPreference()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Preference\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setRequest($this->getRequest());
$form->setConfiguration(
new Zend_Config(