2013-08-14 10:53:25 +02:00
|
|
|
<?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\Config;
|
|
|
|
|
|
|
|
use \Icinga\Application\Config as IcingaConfig;
|
2013-08-14 14:52:47 +02:00
|
|
|
use \Icinga\Application\Icinga;
|
2013-08-15 14:27:53 +02:00
|
|
|
use \Icinga\Application\DbAdapterFactory;
|
2013-08-14 10:53:25 +02:00
|
|
|
use \Icinga\Web\Form;
|
2013-08-19 18:25:20 +02:00
|
|
|
use \Icinga\Web\Form\Validator\WritablePathValidator;
|
|
|
|
use \Icinga\Web\Form\Validator\TimeFormatValidator;
|
|
|
|
use \Icinga\Web\Form\Validator\DateFormatValidator;
|
2013-08-14 14:52:47 +02:00
|
|
|
use \Icinga\Web\Form\Decorator\ConditionalHidden;
|
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
use \DateTimeZone;
|
|
|
|
use \Zend_Config;
|
2013-08-14 14:52:47 +02:00
|
|
|
use \Zend_Form_Element_Text;
|
2013-08-15 14:27:53 +02:00
|
|
|
use \Zend_Form_Element_Select;
|
2013-08-14 10:53:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration form for general, application-wide settings
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class GeneralForm extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The configuration to use for populating this form
|
|
|
|
*
|
|
|
|
* @var IcingaConfig
|
|
|
|
*/
|
|
|
|
private $config = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base directory of the icingaweb configuration
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $configDir = null;
|
|
|
|
|
2013-08-15 14:27:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The resources to use instead of the factory provided ones (use for testing)
|
|
|
|
*
|
|
|
|
* @var null
|
|
|
|
*/
|
|
|
|
private $resources = null;
|
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
/**
|
|
|
|
* Set the configuration to be used for this form
|
|
|
|
*
|
|
|
|
* @param IcingaConfig $cfg
|
|
|
|
*/
|
2013-08-15 10:56:17 +02:00
|
|
|
public function setConfiguration($cfg)
|
2013-08-14 10:53:25 +02:00
|
|
|
{
|
|
|
|
$this->config = $cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a specific configuration directory to use for configuration specific default paths
|
|
|
|
*
|
|
|
|
* @param string $dir
|
|
|
|
*/
|
|
|
|
public function setConfigDir($dir)
|
|
|
|
{
|
|
|
|
$this->configDir = $dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the config path set for this form or the application wide config path if none is set
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @see IcingaConfig::configDir
|
|
|
|
*/
|
|
|
|
public function getConfigDir()
|
|
|
|
{
|
|
|
|
return $this->configDir === null ? IcingaConfig::$configDir : $this->configDir;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:27:53 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function setResources(array $resources)
|
|
|
|
{
|
|
|
|
$this->resources = $resources;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return content of the resources.ini or previously set resources for displaying in the database selection field
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getResources()
|
|
|
|
{
|
2013-08-16 16:24:12 +02:00
|
|
|
if ($this->resources === null) {
|
2013-08-15 14:27:53 +02:00
|
|
|
return DbAdapterFactory::getResources();
|
|
|
|
} else {
|
|
|
|
return $this->resources;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
/**
|
|
|
|
* Add the checkbox for using the development environment to this form
|
|
|
|
*
|
|
|
|
* @param Zend_Config $cfg The "global" section of the config.ini
|
|
|
|
*/
|
|
|
|
private function addDevelopmentCheckbox(Zend_Config $cfg)
|
|
|
|
{
|
|
|
|
$env = $cfg->get('environment', 'development');
|
|
|
|
$this->addElement(
|
|
|
|
'checkbox',
|
|
|
|
'environment',
|
|
|
|
array(
|
|
|
|
'label' => 'Development mode',
|
|
|
|
'required' => true,
|
2013-08-20 17:06:44 +02:00
|
|
|
'helptext' => 'Set true to show more detailed errors '
|
2013-08-19 18:25:20 +02:00
|
|
|
. 'and disable certain optimizations '
|
|
|
|
. 'in order to make debugging easier.',
|
2013-08-14 10:53:25 +02:00
|
|
|
'tooltip' => 'More verbose output',
|
|
|
|
'value' => $env === 'development'
|
|
|
|
)
|
|
|
|
);
|
2013-08-19 18:25:20 +02:00
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a select field for setting the default timezone.
|
|
|
|
*
|
|
|
|
* Possible values are determined by DateTimeZone::listIdentifiers
|
|
|
|
*
|
|
|
|
* @param Zend_Config $cfg The "global" section of the config.ini
|
|
|
|
*/
|
|
|
|
private function addTimezoneSelection(Zend_Config $cfg)
|
|
|
|
{
|
|
|
|
$tzList = array();
|
2013-08-16 16:24:12 +02:00
|
|
|
foreach (DateTimeZone::listIdentifiers() as $tz) {
|
2013-08-14 10:53:25 +02:00
|
|
|
$tzList[$tz] = $tz;
|
|
|
|
}
|
2013-08-19 18:25:20 +02:00
|
|
|
$helptext = 'Select the timezone to be used as the default. User\'s can set their own timezone if'
|
|
|
|
. ' they like to, but this is the timezone to be used as the default setting .';
|
2013-08-14 10:53:25 +02:00
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'timezone',
|
|
|
|
array(
|
|
|
|
'label' => 'Default application timezone',
|
|
|
|
'required' => true,
|
|
|
|
'multiOptions' => $tzList,
|
2013-08-19 18:25:20 +02:00
|
|
|
'helptext' => $helptext,
|
2013-08-14 10:53:25 +02:00
|
|
|
'value' => $cfg->get('timezone', date_default_timezone_get())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add configuration settings for module paths
|
|
|
|
*
|
|
|
|
* @param Zend_Config $cfg The "global" section of the config.ini
|
|
|
|
*/
|
|
|
|
private function addModuleSettings(Zend_Config $cfg)
|
|
|
|
{
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'module_folder',
|
|
|
|
array(
|
|
|
|
'label' => 'Module folder',
|
|
|
|
'required' => true,
|
2013-08-19 18:25:20 +02:00
|
|
|
'helptext' => 'Use this folder to activate modules (must be writable by your webserver)',
|
2013-08-16 16:24:12 +02:00
|
|
|
'value' => $cfg->get('moduleFolder', $this->getConfigDir() . '/config/enabledModules')
|
|
|
|
)
|
|
|
|
);
|
2013-08-14 10:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add text fields for the date and time format used in the application
|
|
|
|
*
|
|
|
|
* @param Zend_Config $cfg The "global" section of the config.ini
|
|
|
|
*/
|
|
|
|
private function addDateFormatSettings(Zend_Config $cfg)
|
|
|
|
{
|
2013-08-16 16:24:12 +02:00
|
|
|
$phpUrl = '<a href="http://php.net/manual/en/function.date.php" target="_new">'
|
|
|
|
. 'the official PHP documentation</a>';
|
2013-08-14 10:53:25 +02:00
|
|
|
|
2013-08-19 18:25:20 +02:00
|
|
|
$txtDefaultDateFormat = new Zend_Form_Element_Text(
|
2013-08-14 10:53:25 +02:00
|
|
|
array(
|
2013-08-19 18:25:20 +02:00
|
|
|
'name' => 'date_format',
|
2013-08-14 10:53:25 +02:00
|
|
|
'label' => 'Date format',
|
2013-08-19 18:25:20 +02:00
|
|
|
'helptext' => 'Display dates according to this format. See ' . $phpUrl . ' for possible values',
|
2013-08-14 10:53:25 +02:00
|
|
|
'required' => true,
|
2013-08-19 18:25:20 +02:00
|
|
|
'value' => $cfg->get('dateFormat', 'd/m/Y')
|
2013-08-14 10:53:25 +02:00
|
|
|
)
|
|
|
|
);
|
2013-08-19 18:25:20 +02:00
|
|
|
$this->addElement($txtDefaultDateFormat);
|
|
|
|
$txtDefaultDateFormat->addValidator(new DateFormatValidator());
|
2013-08-14 10:53:25 +02:00
|
|
|
|
2013-08-19 18:25:20 +02:00
|
|
|
$txtDefaultTimeFormat = new Zend_Form_Element_Text(
|
2013-08-14 10:53:25 +02:00
|
|
|
array(
|
2013-08-19 18:25:20 +02:00
|
|
|
'name' => 'time_format',
|
2013-08-14 10:53:25 +02:00
|
|
|
'label' => 'Time format',
|
|
|
|
'required' => true,
|
2013-08-19 18:25:20 +02:00
|
|
|
'helptext' => 'Display times according to this format. See ' . $phpUrl . ' for possible values',
|
|
|
|
'value' => $cfg->get('timeFormat', 'g:i A')
|
2013-08-16 16:24:12 +02:00
|
|
|
)
|
|
|
|
);
|
2013-08-19 18:25:20 +02:00
|
|
|
$txtDefaultTimeFormat->addValidator(new TimeFormatValidator());
|
|
|
|
$this->addElement($txtDefaultTimeFormat);
|
2013-08-14 10:53:25 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 16:24:12 +02:00
|
|
|
/**
|
|
|
|
* Add form elements for setting the user preference storage backend
|
|
|
|
*
|
|
|
|
* @param Zend_Config $cfg The Zend_config object of preference section
|
|
|
|
*/
|
2013-08-14 14:52:47 +02:00
|
|
|
public function addUserPreferencesDialog(Zend_Config $cfg)
|
|
|
|
{
|
|
|
|
$backend = $cfg->get('type', 'ini');
|
|
|
|
if ($this->getRequest()->get('preferences_type', null) !== null) {
|
|
|
|
$backend = $this->getRequest()->get('preferences_type');
|
|
|
|
}
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'preferences_type',
|
|
|
|
array(
|
|
|
|
'label' => 'User preference storage type',
|
|
|
|
'required' => true,
|
|
|
|
'value' => $backend,
|
|
|
|
'multiOptions' => array(
|
|
|
|
'ini' => 'File system (ini files)',
|
|
|
|
'db' => 'Database'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$txtPreferencesIniPath = new Zend_Form_Element_Text(
|
|
|
|
array(
|
|
|
|
'name' => 'preferences_ini_path',
|
|
|
|
'label' => 'Path to store user preference files',
|
|
|
|
'required' => $backend === 'ini',
|
|
|
|
'condition' => $backend === 'ini',
|
|
|
|
'value' => $cfg->get('configPath')
|
|
|
|
)
|
|
|
|
);
|
2013-08-20 17:06:44 +02:00
|
|
|
|
2013-08-15 14:27:53 +02:00
|
|
|
$backends = array();
|
2013-08-16 16:24:12 +02:00
|
|
|
foreach ($this->getResources() as $name => $resource) {
|
2013-08-15 14:27:53 +02:00
|
|
|
if ($resource['type'] !== 'db') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$backends[$name] = $name;
|
|
|
|
}
|
2013-08-14 14:52:47 +02:00
|
|
|
|
2013-08-15 14:27:53 +02:00
|
|
|
$txtPreferencesDbResource = new Zend_Form_Element_Select(
|
2013-08-14 14:52:47 +02:00
|
|
|
array(
|
2013-08-15 14:27:53 +02:00
|
|
|
'name' => 'preferences_db_resource',
|
|
|
|
'label' => 'Database connection',
|
|
|
|
'required' => $backend === 'db',
|
|
|
|
'condition' => $backend === 'db',
|
|
|
|
'value' => $cfg->get('resource'),
|
|
|
|
'multiOptions' => $backends
|
2013-08-14 14:52:47 +02:00
|
|
|
)
|
|
|
|
);
|
2013-08-20 17:06:44 +02:00
|
|
|
$validator = new WritablePathValidator();
|
|
|
|
$validator->setRequireExistence();
|
|
|
|
$txtPreferencesIniPath->addValidator($validator);
|
2013-08-14 14:52:47 +02:00
|
|
|
$this->addElement($txtPreferencesIniPath);
|
|
|
|
$this->addElement($txtPreferencesDbResource);
|
|
|
|
|
|
|
|
$txtPreferencesIniPath->addDecorator(new ConditionalHidden());
|
|
|
|
$txtPreferencesDbResource->addDecorator(new ConditionalHidden());
|
2013-08-16 16:24:12 +02:00
|
|
|
$this->enableAutoSubmit(
|
|
|
|
array(
|
|
|
|
'preferences_type'
|
|
|
|
)
|
|
|
|
);
|
2013-08-14 14:52:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
/**
|
|
|
|
* Create the general form, using the provided configuration
|
|
|
|
*
|
|
|
|
* @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());
|
|
|
|
}
|
2013-08-14 14:52:47 +02:00
|
|
|
$preferences = $this->config->preferences;
|
2013-08-20 17:06:44 +02:00
|
|
|
|
2013-08-14 14:52:47 +02:00
|
|
|
if ($preferences === null) {
|
|
|
|
$preferences = new Zend_Config(array());
|
|
|
|
}
|
2013-08-14 10:53:25 +02:00
|
|
|
|
|
|
|
$this->addDevelopmentCheckbox($global);
|
|
|
|
$this->addTimezoneSelection($global);
|
|
|
|
$this->addModuleSettings($global);
|
|
|
|
$this->addDateFormatSettings($global);
|
2013-08-14 14:52:47 +02:00
|
|
|
$this->addUserPreferencesDialog($preferences);
|
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
$this->setSubmitLabel('Save changes');
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:24:12 +02:00
|
|
|
/**
|
|
|
|
* Return an Zend_Config object containing the configuration set in this form
|
|
|
|
*
|
|
|
|
* @return Zend_Config
|
|
|
|
*/
|
2013-08-14 10:53:25 +02:00
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
if ($this->config === null) {
|
|
|
|
$this->config = new Zend_Config(array());
|
|
|
|
}
|
2013-08-14 14:52:47 +02:00
|
|
|
if ($this->config->global === null) {
|
2013-08-14 10:53:25 +02:00
|
|
|
$this->config->global = new Zend_Config(array());
|
|
|
|
}
|
2013-08-14 14:52:47 +02:00
|
|
|
if ($this->config->preferences === null) {
|
|
|
|
$this->config->preferences = new Zend_Config(array());
|
|
|
|
}
|
2013-08-14 10:53:25 +02:00
|
|
|
|
|
|
|
$values = $this->getValues();
|
|
|
|
$cfg = clone $this->config;
|
|
|
|
$cfg->global->environment = ($values['environment'] == 1) ? 'development' : 'production';
|
|
|
|
$cfg->global->timezone = $values['timezone'];
|
|
|
|
$cfg->global->moduleFolder = $values['module_folder'];
|
|
|
|
$cfg->global->dateFormat = $values['date_format'];
|
|
|
|
$cfg->global->timeFormat = $values['time_format'];
|
|
|
|
|
2013-08-14 14:52:47 +02:00
|
|
|
$cfg->preferences->type = $values['preferences_type'];
|
|
|
|
if ($cfg->preferences->type === 'ini') {
|
|
|
|
$cfg->preferences->configPath = $values['preferences_ini_path'];
|
|
|
|
} elseif ($cfg->preferences->type === 'db') {
|
|
|
|
$cfg->preferences->resource = $values['preferences_db_resource'];
|
|
|
|
}
|
|
|
|
|
2013-08-14 10:53:25 +02:00
|
|
|
return $cfg;
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|