Merge branch 'bugfix/rebuild-form-builder-5525' into bugfix/commands-6593

This commit is contained in:
Eric Lippmann 2014-09-02 15:27:27 +02:00
commit 9bcd861b1c
10 changed files with 361 additions and 478 deletions

View File

@ -9,11 +9,11 @@ use Icinga\Application\Modules\Module;
use Icinga\Web\Widget; use Icinga\Web\Widget;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Config as IcingaConfig; use Icinga\Application\Config as IcingaConfig;
use Icinga\Form\Config\GeneralForm; use Icinga\Form\Config\GeneralConfigForm;
use Icinga\Form\Config\AuthenticationBackendReorderForm; use Icinga\Form\Config\AuthenticationBackendReorderForm;
use Icinga\Form\Config\AuthenticationBackendConfigForm; use Icinga\Form\Config\AuthenticationBackendConfigForm;
use Icinga\Form\Config\ResourceForm; use Icinga\Form\Config\ResourceForm;
use Icinga\Form\Config\ConfirmRemovalForm; use Icinga\Form\ConfirmRemovalForm;
use Icinga\Config\PreservingIniWriter; use Icinga\Config\PreservingIniWriter;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
@ -47,23 +47,12 @@ class ConfigController extends BaseConfigController
*/ */
public function indexAction() public function indexAction()
{ {
$this->view->messageBox = new AlertMessageBox(true); $form = new GeneralConfigForm();
$this->view->tabs->activate('index'); $form->setConfig(IcingaConfig::app());
$form->handleRequest();
$form = new GeneralForm();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
if ($this->writeConfigFile($form->getConfiguration(), 'config')) {
Notification::success($this->translate('New configuration has successfully been stored'));
$this->redirectNow('config');
}
}
} else {
$form->setConfiguration(IcingaConfig::app());
}
$this->view->form = $form; $this->view->form = $form;
$this->view->tabs->activate('index');
} }
/** /**

View File

@ -0,0 +1,120 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\General;
use DateTimeZone;
use Icinga\Web\Form;
use Icinga\Util\Translator;
use Icinga\Data\ResourceFactory;
/**
* Form class to modify the general application configuration
*/
class ApplicationConfigForm extends Form
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_general_application');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array();
$languages = array();
foreach (Translator::getAvailableLocaleCodes() as $language) {
$languages[$language] = $language;
}
$elements[] = $this->createElement(
'select',
'global_language',
array(
'label' => t('Default Language'),
'required' => true,
'multiOptions' => $languages,
'helptext' => t(
'Select the language to use by default. Can be overwritten by a user in his preferences.'
)
)
);
$tzList = array();
foreach (DateTimeZone::listIdentifiers() as $tz) {
$tzList[$tz] = $tz;
}
$elements[] = $this->createElement(
'select',
'global_timezone',
array(
'label' => t('Default Application Timezone'),
'required' => true,
'multiOptions' => $tzList,
'helptext' => t(
'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 .'
),
'value' => date_default_timezone_get()
)
);
$elements[] = $this->createElement(
'text',
'global_modulePath',
array(
'label' => t('Module Path'),
'required' => true,
'helptext' => t(
'Contains the directories that will be searched for available modules, separated by '
. 'colons. Modules that don\'t exist in these directories can still be symlinked in '
. 'the module folder, but won\'t show up in the list of disabled modules.'
),
'value' => realpath(ICINGAWEB_APPDIR . '/../modules')
)
);
$elements[] = $this->createElement(
'select',
'preferences_type',
array(
'required' => true,
'autosubmit' => true,
'label' => t('User Preference Storage Type'),
'multiOptions' => array(
'ini' => t('File System (INI Files)'),
'db' => t('Database'),
'null' => t('Don\'t Store Preferences')
)
)
);
if (isset($formData['preferences_type']) && $formData['preferences_type'] === 'db') {
$backends = array();
foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) {
if ($resource['type'] === 'db') {
$backends[$name] = $name;
}
}
$elements[] = $this->createElement(
'select',
'preferences_resource',
array(
'required' => true,
'multiOptions' => $backends,
'label' => t('Database Connection')
)
);
}
return $elements;
}
}

View File

@ -0,0 +1,120 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\General;
use Icinga\Application\Icinga;
use Icinga\Web\Form;
use Icinga\Web\Form\Validator\WritablePathValidator;
class LoggingConfigForm extends Form
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_general_logging');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array();
$elements[] = $this->createElement(
'select',
'logging_level',
array(
'required' => true,
'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'),
'multiOptions' => array(
0 => t('None'),
1 => t('Error'),
2 => t('Warning'),
3 => t('Information'),
4 => t('Debug')
)
)
);
$elements[] = $this->createElement(
'select',
'logging_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'),
'multiOptions' => array(
'syslog' => 'Syslog',
'file' => t('File')
)
)
);
if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') {
$elements[] = $this->createElement(
'text',
'logging_application',
array(
'required' => true,
'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => 'icingaweb',
'validators' => array(
array(
'Regex',
false,
array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
)
)
)
)
);
$elements[] = $this->createElement(
'select',
'logging_facility',
array(
'required' => true,
'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'),
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
)
)
);
} elseif ($formData['logging_type'] === 'file') {
$elements[] = $this->createElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
}
return $elements;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{
return realpath(Icinga::app()->getApplicationDir('../var/log/icingaweb.log'));
}
}

View File

@ -0,0 +1,76 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config;
use Icinga\Web\Request;
use Icinga\Web\Notification;
use Icinga\Form\ConfigForm;
use Icinga\Form\Config\General\LoggingConfigForm;
use Icinga\Form\Config\General\ApplicationConfigForm;
/**
* Form class for application-wide and logging specific settings
*/
class GeneralConfigForm extends ConfigForm
{
/**
* Initialize this configuration form
*/
public function init()
{
$this->setName('form_config_general');
$this->setSubmitLabel(t('Save Changes'));
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$appConfigForm = new ApplicationConfigForm();
$loggingConfigForm = new LoggingConfigForm();
return array_merge(
$appConfigForm->createElements($formData),
$loggingConfigForm->createElements($formData)
);
}
/**
* @see Form::onSuccess()
*/
public function onSuccess(Request $request)
{
foreach ($this->getValues() as $sectionAndPropertyName => $value) {
list($section, $property) = explode('_', $sectionAndPropertyName);
if (isset($this->config->{$section})) {
$this->config->{$section}->{$property} = $value;
} else {
$this->config->{$section} = array($property => $value);
}
}
if ($this->save()) {
Notification::success(t('New configuration has successfully been stored'));
} else {
return false;
}
}
/**
* @see Form::onShow()
*/
public function onShow(Request $request)
{
$values = array();
foreach ($this->config as $section => $properties) {
foreach ($properties as $name => $value) {
$values[$section . '_' . $name] = $value;
}
}
$this->populate($values);
}
}

View File

@ -1,312 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config;
use DateTimeZone;
use Zend_Config;
use Icinga\Web\Form;
use Icinga\Util\Translator;
use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
use Icinga\Web\Form\Validator\WritablePathValidator;
/**
* Configuration form for general, application-wide settings
*/
class GeneralForm extends Form
{
/**
* Initialize this configuration form
*/
public function init()
{
$this->setName('form_config_general');
$this->setSubmitLabel(t('Save Changes'));
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array(
$this->getLanguageSelection(),
$this->getTimezoneSelection(),
$this->getModulePathInput()
);
return array_merge(
$elements,
$this->getPreferencesElements($formData),
$this->getLoggingElements($formData)
);
}
/**
* Populate this form with the given configuration
*
* @param Zend_Config $config The configuration to populate this form with
*
* @return self
*/
public function setConfiguration(Zend_Config $config)
{
$defaults = array();
foreach ($config as $section => $properties) {
foreach ($properties as $name => $value) {
$defaults[$section . '_' . $name] = $value;
}
}
$this->populate($defaults);
return $this;
}
/**
* Return the configured configuration values
*
* @return Zend_Config
*/
public function getConfiguration()
{
$config = array();
$values = $this->getValues();
foreach ($values as $sectionAndPropertyName => $value) {
list($section, $property) = explode('_', $sectionAndPropertyName);
$config[$section][$property] = $value;
}
return new Zend_Config($config);
}
/**
* Return a select field for setting the default language
*
* Possible values are determined by Translator::getAvailableLocaleCodes.
*
* @return Zend_Form_Element
*/
protected function getLanguageSelection()
{
$languages = array();
foreach (Translator::getAvailableLocaleCodes() as $language) {
$languages[$language] = $language;
}
return $this->createElement(
'select',
'global_language',
array(
'label' => t('Default Language'),
'required' => true,
'multiOptions' => $languages,
'helptext' => t(
'Select the language to use by default. Can be overwritten by a user in his preferences.'
)
)
);
}
/**
* Return a select field for setting the default timezone
*
* Possible values are determined by DateTimeZone::listIdentifiers.
*
* @return Zend_Form_Element
*/
protected function getTimezoneSelection()
{
$tzList = array();
foreach (DateTimeZone::listIdentifiers() as $tz) {
$tzList[$tz] = $tz;
}
$this->addElement(
'select',
'global_timezone',
array(
'label' => t('Default Application Timezone'),
'required' => true,
'multiOptions' => $tzList,
'helptext' => t(
'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 .'
),
'value' => date_default_timezone_get()
)
);
}
/**
* Return a input field for setting the module path
*/
protected function getModulePathInput()
{
$this->addElement(
'text',
'global_modulePath',
array(
'label' => t('Module Path'),
'required' => true,
'helptext' => t(
'Contains the directories that will be searched for available modules, separated by '
. 'colons. Modules that don\'t exist in these directories can still be symlinked in '
. 'the module folder, but won\'t show up in the list of disabled modules.'
),
'value' => realpath(ICINGAWEB_APPDIR . '/../modules')
)
);
}
/**
* Return form elements for setting the user preference storage backend
*
* @param array $formData The data sent by the user
*/
protected function getPreferencesElements(array $formData)
{
$elements = array(
$this->createElement(
'select',
'preferences_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('User Preference Storage Type'),
'multiOptions' => array(
'ini' => t('File System (INI Files)'),
'db' => t('Database'),
'null' => t('Don\'t Store Preferences')
)
)
)
);
if (isset($formData['preferences_type']) && $formData['preferences_type'] === 'db') {
$backends = array();
foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) {
if ($resource['type'] === 'db') {
$backends[$name] = $name;
}
}
$elements[] = $this->createElement(
'select',
'preferences_resource',
array(
'required' => true,
'multiOptions' => $backends,
'label' => t('Database Connection')
)
);
}
return $elements;
}
/**
* Return form elements to setup the application's logging
*
* @param array $formData The data sent by the user
*
* @return array
*/
protected function getLoggingElements(array $formData)
{
$elements = array();
$elements[] = $this->createElement(
'select',
'logging_level',
array(
'required' => true,
'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'),
'multiOptions' => array(
0 => t('None'),
1 => t('Error'),
2 => t('Warning'),
3 => t('Information'),
4 => t('Debug')
)
)
);
$elements[] = $this->createElement(
'select',
'logging_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'),
'multiOptions' => array(
'syslog' => 'Syslog',
'file' => t('File')
)
)
);
if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') {
$elements[] = $this->createElement(
'text',
'logging_application',
array(
'required' => true,
'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => 'icingaweb',
'validators' => array(
array(
'Regex',
false,
array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
)
)
)
)
);
$elements[] = $this->createElement(
'select',
'logging_facility',
array(
'required' => true,
'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'),
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
)
)
);
} elseif ($formData['logging_type'] === 'file') {
$elements[] = $this->createElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
}
return $elements;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{
return realpath(Icinga::app()->getApplicationDir('../var/log/icingaweb.log'));
}
}

View File

@ -2,7 +2,7 @@
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config; namespace Icinga\Form;
use Icinga\Web\Form; use Icinga\Web\Form;

View File

@ -28,7 +28,7 @@ class Form extends Zend_Form
/** /**
* The callback to call instead of Form::onSuccess() * The callback to call instead of Form::onSuccess()
* *
* @var Closure * @var Callback
*/ */
protected $onSuccess; protected $onSuccess;
@ -79,8 +79,8 @@ class Form extends Zend_Form
/** /**
* Create a new form * Create a new form
* *
* Accepts an additional option `onSuccess' which is a * Accepts an additional option `onSuccess' which is a callback that is called instead of this
* callback that is called instead of this form's method. * form's method. It is called using the following signature: (Request $request, Form $form).
* *
* @see Zend_Form::__construct() * @see Zend_Form::__construct()
* *
@ -99,7 +99,15 @@ class Form extends Zend_Form
if ($this->onSuccess !== null && false === is_callable($this->onSuccess)) { if ($this->onSuccess !== null && false === is_callable($this->onSuccess)) {
throw new LogicException('The option `onSuccess\' is not callable'); throw new LogicException('The option `onSuccess\' is not callable');
} }
if (! isset($options['elementDecorators'])) {
$options['elementDecorators'] = array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'span', 'class' => 'description')),
'Label',
array('HtmlTag', array('tag' => 'div'))
);
}
parent::__construct($options); parent::__construct($options);
} }
@ -333,8 +341,12 @@ class Form extends Zend_Form
'submit', 'submit',
'btn_submit', 'btn_submit',
array( array(
'ignore' => true, 'ignore' => true,
'label' => $this->submitLabel 'label' => $this->submitLabel,
'decorators' => array(
'ViewHelper',
array('HtmlTag', array('tag' => 'div'))
)
) )
); );
} }
@ -357,36 +369,23 @@ class Form extends Zend_Form
*/ */
public function createElement($type, $name, $options = null) public function createElement($type, $name, $options = null)
{ {
$el = parent::createElement($type, $name, $options); if (is_array($options) && ! isset($options['disableLoadDefaultDecorators'])) {
$options['disableLoadDefaultDecorators'] = true;
if ($el) { }
if (strpos(strtolower(get_class($el)), 'hidden') !== false) { $el = parent::createElement($type, $name, $options);
$el->setDecorators(array('ViewHelper')); if ($el && $el->getAttrib('autosubmit')) {
} else { $el->addDecorator(new NoScriptApply()); // Non-JS environments
$el->removeDecorator('HtmlTag'); $class = $el->getAttrib('class');
$el->removeDecorator('Label'); if (is_array($class)) {
$el->removeDecorator('DtDdWrapper'); $class[] = 'autosubmit';
} elseif ($class === null) {
if ($el->getAttrib('autosubmit')) { $class = 'autosubmit';
// Need to add this decorator first or it interferes with the other's two HTML otherwise } else {
$el->addDecorator(new NoScriptApply()); // Non-JS environments $class .= ' autosubmit';
$class = $el->getAttrib('class'); }
if (is_array($class)) { $el->setAttrib('class', $class); // JS environments
$class[] = 'autosubmit'; unset($el->autosubmit);
} elseif ($class === null) {
$class = 'autosubmit';
} else {
$class .= ' autosubmit';
}
$el->setAttrib('class', $class); // JS environments
unset($el->autosubmit);
}
$el->addDecorator(new ElementWrapper());
$el->addDecorator(new HelpText());
}
} }
return $el; return $el;
} }
@ -457,7 +456,7 @@ class Form extends Zend_Form
$this->populate($formData); // Necessary to get isSubmitted() to work $this->populate($formData); // Necessary to get isSubmitted() to work
if (! $this->getSubmitLabel() || $this->isSubmitted()) { if (! $this->getSubmitLabel() || $this->isSubmitted()) {
if ($this->isValid($formData) if ($this->isValid($formData)
&& (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $request)) && (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $request, $this))
|| ($this->onSuccess === null && false !== $this->onSuccess($request)))) { || ($this->onSuccess === null && false !== $this->onSuccess($request)))) {
$this->getResponse()->redirectAndExit($this->getRedirectUrl()); $this->getResponse()->redirectAndExit($this->getRedirectUrl());
} }
@ -556,7 +555,6 @@ class Form extends Zend_Form
if ($this->loadDefaultDecoratorsIsDisabled()) { if ($this->loadDefaultDecoratorsIsDisabled()) {
return $this; return $this;
} }
$decorators = $this->getDecorators(); $decorators = $this->getDecorators();
if (empty($decorators)) { if (empty($decorators)) {
if ($this->viewScript) { if ($this->viewScript) {
@ -565,12 +563,11 @@ class Form extends Zend_Form
'form' => $this 'form' => $this
)); ));
} else { } else {
$this->addDecorator('FormElements') $this
//->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')) ->addDecorator('FormElements')
->addDecorator('Form'); ->addDecorator('Form');
} }
} }
return $this; return $this;
} }

View File

@ -1,70 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
/**
* Decorator that wraps form elements with their labels to allow easier styling
*
* Labels are drawn for all elements, except hidden, button and submit elements. If you want a
* placeholder for these elements, set the 'addLabelPlaceholder' property. This can be useful in
* cases where you want to put inputs with and inputs without labels on the same line and don't
* want buttons to 'jump'
*/
class ElementWrapper extends Zend_Form_Decorator_Abstract
{
/**
* An array of elements that won't get a <label> dom added per default
*
* @var array
*/
protected static $noLabel = array(
'Zend_Form_Element_Hidden',
'Zend_Form_Element_Button',
'Zend_Form_Element_Submit'
);
/**
* Return the DOM for the element label
*
* @param String $elementName The name of the element
*
* @return String The DOM for the form element's label
*/
public function getLabel($elementName)
{
$label = $this->getElement()->getLabel();
if (! $label) {
$label = '&nbsp;';
}
if (in_array($this->getElement()->getType(), self::$noLabel)
&& false === $this->getElement()->getAttrib('addLabelPlaceholder', false)) {
$label = '';
} else {
if (in_array($this->getElement()->getType(), self::$noLabel)) {
$label = '&nbsp;';
}
$label = '<label for="' . $elementName . '">' . $label . '</label>';
}
return $label;
}
/**
* Render this element
*
* @param String $content The content of the form element
*
* @return String The decorated form element
*/
public function render($content)
{
$elementName = $this->getElement()->getName();
$label = $this->getLabel($elementName);
return '<div class="form-element" id="' . $elementName . '-element">' . $label . $content . '</div>';
}
}

View File

@ -1,37 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
/**
* Decorator that automatically adds a helptext to an input element
* when the 'helptext' attribute is set
*/
class HelpText extends Zend_Form_Decorator_Abstract
{
/**
* Add a helptext to an input field
*
* @param string $content The help text
*
* @return string The generated tag
*/
public function render($content = '')
{
$attributes = $this->getElement()->getAttribs();
$visible = true;
if (isset($attributes['condition'])) {
$visible = $attributes['condition'] == '1';
}
if (isset($attributes['helptext']) && $visible) {
$content = $content
. '<p class="help-block">'
. $attributes['helptext']
. '</p>';
}
return $content;
}
}

View File

@ -5,7 +5,7 @@
use Icinga\Config\PreservingIniWriter; use Icinga\Config\PreservingIniWriter;
use Icinga\Web\Controller\ModuleActionController; use Icinga\Web\Controller\ModuleActionController;
use Icinga\Web\Notification; use Icinga\Web\Notification;
use Icinga\Form\Config\ConfirmRemovalForm; use Icinga\Form\ConfirmRemovalForm;
use Icinga\Module\Monitoring\Form\Config\BackendForm; use Icinga\Module\Monitoring\Form\Config\BackendForm;
use Icinga\Module\Monitoring\Form\Config\InstanceForm; use Icinga\Module\Monitoring\Form\Config\InstanceForm;
use Icinga\Module\Monitoring\Form\Config\SecurityForm; use Icinga\Module\Monitoring\Form\Config\SecurityForm;