Implement user preferences form

refs #4525
This commit is contained in:
Jannis Moßhammer 2013-08-19 20:00:55 +02:00 committed by Eric Lippmann
parent 71603ccd70
commit 49d92d0c33
3 changed files with 267 additions and 5 deletions

View File

@ -29,13 +29,25 @@
use \Icinga\Web\Controller\BasePreferenceController;
use \Icinga\Web\Widget\Tab;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Web\Url;
use \Icinga\Form\Preference\GeneralForm;
/**
* Application wide preference controller for user preferences
*/
class PreferenceController extends BasePreferenceController
{
/**
* This controller modifies the session
*
* @var bool
*
* @see \Icinga\Web\Controller\ActionController::$modifiesSession
*/
protected $modifiesSession = true;
/**
* Create tabs for this preference controller
*
@ -48,9 +60,8 @@ class PreferenceController extends BasePreferenceController
return array(
'preference' => new Tab(
array(
'name' => 'preferences',
'iconCls' => 'user',
'title' => 'Preferences',
'name' => 'general',
'title' => 'General settings',
'url' => Url::fromPath('/preference')
)
)
@ -58,11 +69,33 @@ class PreferenceController extends BasePreferenceController
}
/**
* @TODO: Implement User preferences (feature #5425)
* General settings for date and time
*/
public function indexAction()
{
$form = new GeneralForm();
$form->setConfiguration(IcingaConfig::app());
$form->setRequest($this->_request);
if ($form->isSubmittedAndValid()) {
$preferences = $form->getPreferences();
$userPreferences = $this->getRequest()->getUser()->getPreferences();
$userPreferences->startTransaction();
foreach ($preferences as $key=>$value) {
if ($value == "") {
$userPreferences->remove($key);
} else {
$userPreferences->set($key, $value);
}
}
try {
$userPreferences->commit();
$this->view->success = true;
} catch (Exception $e) {
$this->view->exceptionMessage = $e->getMessage();
}
}
$this->view->form = $form;
}
}
// @codingStandardsIgnoreEnd

View File

@ -0,0 +1,217 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Preference;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Web\Form;
use \Icinga\Web\Form\Validator\TimeFormatValidator;
use \Icinga\Web\Form\Validator\DateFormatValidator;
use \DateTimeZone;
use \Zend_Config;
use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select;
/**
* General user preferences
*
*/
class GeneralForm extends Form
{
/**
* The configuration to use for populating this form
*
* @var IcingaConfig
*/
private $config = null;
/**
* Set the configuration to be used for this form when no preferences are set yet
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/**
* Add a select field for setting the user's timezone.
*
* Possible values are determined by DateTimeZone::listIdentifiers
* Also, a 'use default format' checkbox is added in order to allow a user to discard his overwritten setting
*
* @param Zend_Config $cfg The "global" section of the config.ini to be used as default valuse
*/
private function addTimezoneSelection(Zend_Config $cfg)
{
$tzList = array();
foreach (DateTimeZone::listIdentifiers() as $tz) {
$tzList[$tz] = $tz;
}
$helptext = 'Use the following timezone for dates and times';
$prefs = $this->getRequest()->getUser()->getPreferences();
$useGlobalTimezone = $this->getRequest()->getParam('default_timezone', !$prefs->has('app.timezone'));
$selectTimezone = new Zend_Form_Element_Select(
array(
'name' => 'timezone',
'label' => 'Your current timezone',
'required' => !$useGlobalTimezone,
'multiOptions' => $tzList,
'helptext' => $helptext,
'value' => $prefs->get('app.timezone', $cfg->get('timezone', date_default_timezone_get()))
)
);
$this->addElement(
'checkbox',
'default_timezone',
array(
'label' => 'Use default timezone',
'value' => !$prefs->has('app.timezone'),
'required' => true
)
);
if ($useGlobalTimezone) {
$selectTimezone->setAttrib('disabled', 1);
}
$this->addElement($selectTimezone);
$this->enableAutoSubmit(array('default_timezone'));
}
/**
* Add text fields for the date and time format used for this user
*
* Also, a 'use default format' checkbox is added in order to allow a user to discard his overwritten setting
*
* @param Zend_Config $cfg The "global" section of the config.ini to be used as default values
*/
private function addDateFormatSettings(Zend_Config $cfg)
{
$prefs = $this->getRequest()->getUser()->getPreferences();
$useGlobalDateFormat = $this->getRequest()->getParam('default_date_format', !$prefs->has('app.dateFormat'));
$useGlobalTimeFormat = $this->getRequest()->getParam('default_time_format', !$prefs->has('app.timeFormat'));
$phpUrl = '<a href="http://php.net/manual/en/function.date.php" target="_new">'
. 'the official PHP documentation</a>';
$this->addElement(
'checkbox',
'default_date_format',
array(
'label' => 'Use default date format',
'value' => !$prefs->has('app.dateFormat'),
'required' => true
)
);
$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',
'required' => !$useGlobalDateFormat,
'value' => $prefs->get('app.dateFormat', $cfg->get('dateFormat', 'd/m/Y'))
)
);
$this->addElement($txtDefaultDateFormat);
$txtDefaultDateFormat->addValidator(new DateFormatValidator());
if ($useGlobalDateFormat) {
$txtDefaultDateFormat->setAttrib('disabled', '1');
}
$this->addElement(
'checkbox',
'default_time_format',
array(
'label' => 'Use default time format',
'value' => !$prefs->has('app.timeFormat'),
'required' => !$useGlobalTimeFormat
)
);
$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'))
)
);
$txtDefaultTimeFormat->addValidator(new TimeFormatValidator());
$this->addElement($txtDefaultTimeFormat);
if ($useGlobalTimeFormat) {
$txtDefaultTimeFormat->setAttrib('disabled', '1');
}
$this->enableAutoSubmit(array('default_time_format', 'default_date_format'));
}
/**
* Create the general form, using the global configuration as fallback values for preferences
*
* @see Form::create()
*/
public function create()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$global = $this->config->global;
if ($global === null) {
$global = new Zend_Config(array());
}
$this->addTimezoneSelection($global);
$this->addDateFormatSettings($global);
$this->setSubmitLabel('Save changes');
}
/**
* Return an array containing the preferences set in this form
*
* @return array
*/
public function getPreferences()
{
$values = $this->getValues();
return array(
'app.timezone' => $values['timezone'],
'app.dateFormat' => $values['date_format'],
'app.timeFormat' => $values['time_format']
);
}
}

View File

@ -1,3 +1,15 @@
<?= $this->tabs->render($this); ?>
<h3>Preferences </h3>
<?php if (isset($this->success)) : ?>
<div class="alert alert-success">
<h4>Preferences updated sucessfully</h4>
</div>
<?php endif; ?>
<?php if (isset($this->exceptionMessage)): ?>
<div class="alert alert-danger">
<h4>Could not update preferences due to an internal exception (<?= $this->exceptionMessage ?>)</h4>
</div>
<?php endif; ?>
<?= $this->form ?>