Merge branch 'bugfix/rebuild-form-builder-5525' into bugfix/autologin-backend-form-6434
This commit is contained in:
commit
02c3223a82
|
@ -3,18 +3,14 @@
|
|||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
use \Exception;
|
||||
|
||||
use \Zend_Config;
|
||||
use Icinga\Config\PreservingIniWriter;
|
||||
use Icinga\Web\Controller\ModuleActionController;
|
||||
use Icinga\Web\Notification;
|
||||
use Icinga\Web\Url;
|
||||
|
||||
use Icinga\Module\Monitoring\Form\Config\BackendForm;
|
||||
use Icinga\Module\Monitoring\Form\Config\ConfirmRemovalForm;
|
||||
use Icinga\Module\Monitoring\Form\Config\Backend\EditBackendForm;
|
||||
use Icinga\Module\Monitoring\Form\Config\Backend\CreateBackendForm;
|
||||
use Icinga\Module\Monitoring\Form\Config\Instance\EditInstanceForm;
|
||||
use Icinga\Module\Monitoring\Form\Config\Instance\CreateInstanceForm;
|
||||
|
||||
use Icinga\Exception\NotReadableError;
|
||||
|
||||
/**
|
||||
|
@ -22,7 +18,6 @@ use Icinga\Exception\NotReadableError;
|
|||
*/
|
||||
class Monitoring_ConfigController extends ModuleActionController
|
||||
{
|
||||
|
||||
/**
|
||||
* Display a list of available backends and instances
|
||||
*/
|
||||
|
@ -48,33 +43,44 @@ class Monitoring_ConfigController extends ModuleActionController
|
|||
*/
|
||||
public function editbackendAction()
|
||||
{
|
||||
// Fetch the backend to be edited
|
||||
$backend = $this->getParam('backend');
|
||||
if (!$this->isExistingBackend($backend)) {
|
||||
$this->view->error = 'Unknown backend ' . $backend;
|
||||
return;
|
||||
$backendsConfig = $this->Config('backends')->toArray();
|
||||
if (false === array_key_exists($backend, $backendsConfig)) {
|
||||
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
|
||||
Notification::error(sprintf($this->translate('Cannot edit "%s". Backend not found.'), $backend));
|
||||
$this->redirectNow('monitoring/config');
|
||||
}
|
||||
$backendForm = new EditBackendForm();
|
||||
$backendForm->setRequest($this->getRequest());
|
||||
$backendForm->setBackendConfiguration($this->Config('backends')->get($backend));
|
||||
|
||||
if ($backendForm->isSubmittedAndValid()) {
|
||||
$newConfig = $backendForm->getConfig();
|
||||
$config = $this->Config('backends');
|
||||
$config->$backend = $newConfig;
|
||||
if ($this->writeConfiguration($config, 'backends')) {
|
||||
Notification::success('Backend ' . $backend . ' Modified.');
|
||||
$this->redirectNow('monitoring/config');
|
||||
} else {
|
||||
$this->render('show-configuration');
|
||||
return;
|
||||
$form = new BackendForm();
|
||||
$request = $this->getRequest();
|
||||
if ($request->isPost()) {
|
||||
if ($form->isValid($request->getPost())) {
|
||||
list($newName, $config) = $form->getBackendConfig();
|
||||
|
||||
if ($newName !== $backend) {
|
||||
// Backend name has changed
|
||||
unset($backendsConfig[$backend]); // We can safely use unset as all values are part of the form
|
||||
}
|
||||
|
||||
$backendsConfig[$newName] = $config;
|
||||
if ($this->writeConfiguration($backendsConfig, 'backends')) {
|
||||
Notification::success(sprintf($this->translate('Backend "%s" successfully modified.'), $backend));
|
||||
$this->redirectNow('monitoring/config');
|
||||
} else {
|
||||
$this->render('show-configuration');
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$form->setBackendConfig($backend, $backendsConfig[$backend]);
|
||||
}
|
||||
$this->view->name = $backend;
|
||||
$this->view->form = $backendForm;
|
||||
|
||||
$this->view->form = $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a form to create a new backends
|
||||
* Display a form to create a new backend
|
||||
*/
|
||||
public function createbackendAction()
|
||||
{
|
||||
|
@ -128,7 +134,7 @@ class Monitoring_ConfigController extends ModuleActionController
|
|||
}
|
||||
|
||||
/**
|
||||
* Display a form to remove the instance identified by the 'instance' parameter
|
||||
* Display a confirmation form to remove the instance identified by the 'instance' parameter
|
||||
*/
|
||||
public function removeinstanceAction()
|
||||
{
|
||||
|
@ -214,10 +220,18 @@ class Monitoring_ConfigController extends ModuleActionController
|
|||
}
|
||||
|
||||
/**
|
||||
* Display a form to remove the instance identified by the 'instance' parameter
|
||||
* Write configuration to an ini file
|
||||
*
|
||||
* @param Zend_Config $config The configuration to write
|
||||
* @param string $file The config file to write to
|
||||
*
|
||||
* @return bool Whether the configuration was written or not
|
||||
*/
|
||||
private function writeConfiguration($config, $file)
|
||||
protected function writeConfiguration($config, $file)
|
||||
{
|
||||
if (is_array($config)) {
|
||||
$config = new Zend_Config($config);
|
||||
}
|
||||
$target = $this->Config($file)->getConfigFile();
|
||||
$writer = new PreservingIniWriter(array('filename' => $target, 'config' => $config));
|
||||
|
||||
|
@ -234,26 +248,26 @@ class Monitoring_ConfigController extends ModuleActionController
|
|||
}
|
||||
|
||||
/**
|
||||
* Return true if the backend exists in the current configuration
|
||||
* Return whether the given backend exists in the current configuration
|
||||
*
|
||||
* @param string $backend The name of the backend to check for existence
|
||||
* @param string $backend The name of the backend to check
|
||||
*
|
||||
* @return bool True if the backend name exists, otherwise false
|
||||
* @return bool Whether the backend exists or not
|
||||
*/
|
||||
private function isExistingBackend($backend)
|
||||
protected function isExistingBackend($backend)
|
||||
{
|
||||
$backendCfg = $this->Config('backends');
|
||||
return $backend && $backendCfg->get($backend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the instance exists in the current configuration
|
||||
* Return whether the given instance exists in the current configuration
|
||||
*
|
||||
* @param string $instance The name of the instance to check for existence
|
||||
* @param string $instance The name of the instance to check
|
||||
*
|
||||
* @return bool True if the instance name exists, otherwise false
|
||||
* @return bool Whether the instance exists or not
|
||||
*/
|
||||
private function isExistingInstance($instance)
|
||||
protected function isExistingInstance($instance)
|
||||
{
|
||||
$instanceCfg = $this->Config('instances');
|
||||
return $instanceCfg && $instanceCfg->get($instance);
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Module\Monitoring\Form\Config\Backend;
|
||||
|
||||
use \Zend_Config;
|
||||
|
||||
/**
|
||||
* Extended EditBackendForm for creating new Backends
|
||||
*
|
||||
* @see EditBackendForm
|
||||
*/
|
||||
class CreateBackendForm extends EditBackendForm
|
||||
{
|
||||
/**
|
||||
* Create this form
|
||||
*
|
||||
* @see EditBackendForm::create()
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$this->setBackendConfiguration(new Zend_Config(array('type' => 'ido')));
|
||||
$this->addElement(
|
||||
'text',
|
||||
'backend_name',
|
||||
array(
|
||||
'label' => 'Backend Name',
|
||||
'required' => true,
|
||||
'helptext' => 'This will be the identifier of this backend'
|
||||
)
|
||||
);
|
||||
parent::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the backend that is to be created
|
||||
*
|
||||
* @return string The name of the backend as entered in the form
|
||||
*/
|
||||
public function getBackendName()
|
||||
{
|
||||
return $this->getValue('backend_name');
|
||||
}
|
||||
}
|
|
@ -1,148 +0,0 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Module\Monitoring\Form\Config\Backend;
|
||||
|
||||
use Zend_Config;
|
||||
use Icinga\Web\Form;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Data\ResourceFactory;
|
||||
|
||||
/**
|
||||
* Form for modifying a monitoring backend
|
||||
*/
|
||||
class EditBackendForm extends Form
|
||||
{
|
||||
/**
|
||||
* Database resources to use instead of the one's from ResourceFactory (used for testing)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $resources;
|
||||
|
||||
/**
|
||||
* The Backend configuration to use for populating the form
|
||||
*
|
||||
* @var Zend_Config
|
||||
*/
|
||||
protected $backend;
|
||||
|
||||
/**
|
||||
* Set the configuration to be used for initial population of the form
|
||||
*
|
||||
* @param Zend_Form $config
|
||||
*/
|
||||
public function setBackendConfiguration($config)
|
||||
{
|
||||
$this->backend = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom array of resources to be used in this form instead of the ones from ResourceFactory
|
||||
* (used for testing)
|
||||
*/
|
||||
public function setResources($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()
|
||||
{
|
||||
if ($this->resources === null) {
|
||||
return ResourceFactory::getResourceConfigs()->toArray();
|
||||
} else {
|
||||
return $this->resources;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of all resources of the given type ready to be used as content for a select input
|
||||
*
|
||||
* @param string $type The type of resources to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getResourcesByType($type)
|
||||
{
|
||||
$backends = array();
|
||||
foreach ($this->getResources() as $name => $resource) {
|
||||
if ($resource['type'] === $type) {
|
||||
$backends[$name] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
return $backends;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create this form
|
||||
*
|
||||
* @see Icinga\Web\Form::create()
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$backendType = $this->getRequest()->getParam('backend_type', $this->backend->type);
|
||||
|
||||
$this->addElement(
|
||||
'select',
|
||||
'backend_type',
|
||||
array(
|
||||
'label' => 'Backend Type',
|
||||
'value' => $this->backend->type,
|
||||
'required' => true,
|
||||
'helptext' => 'The data source used for retrieving monitoring information',
|
||||
'multiOptions' => array(
|
||||
'ido' => 'IDO Backend',
|
||||
'statusdat' => 'Status.dat',
|
||||
'livestatus' => 'Livestatus'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->addElement(
|
||||
'select',
|
||||
'backend_resource',
|
||||
array(
|
||||
'label' => 'Resource',
|
||||
'value' => $this->backend->resource,
|
||||
'required' => true,
|
||||
'multiOptions' => $this->getResourcesByType($backendType === 'ido' ? 'db' : $backendType),
|
||||
'helptext' => 'The resource to use'
|
||||
)
|
||||
);
|
||||
$this->addElement(
|
||||
'checkbox',
|
||||
'backend_disable',
|
||||
array(
|
||||
'label' => 'Disable This Backend',
|
||||
'required' => true,
|
||||
'value' => $this->backend->disabled
|
||||
)
|
||||
);
|
||||
|
||||
$this->enableAutoSubmit(array('backend_type'));
|
||||
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a configuration containing the backend settings entered in this form
|
||||
*
|
||||
* @return Zend_Config The updated configuration for this backend
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
$values = $this->getValues();
|
||||
return new Zend_Config(
|
||||
array(
|
||||
'type' => $values['backend_type'],
|
||||
'disabled' => $values['backend_disable'],
|
||||
'resource' => $values['backend_resource']
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Module\Monitoring\Form\Config;
|
||||
|
||||
use Icinga\Web\Form;
|
||||
use Icinga\Data\ResourceFactory;
|
||||
|
||||
/**
|
||||
* Form for modifying a monitoring backend
|
||||
*/
|
||||
class BackendForm extends Form
|
||||
{
|
||||
/**
|
||||
* @see Form::createElements()
|
||||
*/
|
||||
public function createElements(array $formData)
|
||||
{
|
||||
return array(
|
||||
$this->createElement(
|
||||
'text',
|
||||
'name',
|
||||
array(
|
||||
'required' => true,
|
||||
'label' => t('Backend Name'),
|
||||
'helptext' => t('The identifier of this backend')
|
||||
)
|
||||
),
|
||||
$this->createElement(
|
||||
'select',
|
||||
'type',
|
||||
array(
|
||||
'required' => true,
|
||||
'class' => 'autosubmit',
|
||||
'label' => t('Backend Type'),
|
||||
'helptext' => t('The data source used for retrieving monitoring information'),
|
||||
'multiOptions' => array(
|
||||
'ido' => 'IDO Backend',
|
||||
'statusdat' => 'Status.dat',
|
||||
'livestatus' => 'Livestatus'
|
||||
)
|
||||
)
|
||||
),
|
||||
$this->createElement(
|
||||
'select',
|
||||
'resource',
|
||||
array(
|
||||
'required' => true,
|
||||
'label' => t('Resource'),
|
||||
'helptext' => t('The resource to use'),
|
||||
'multiOptions' => $this->getResourcesByType(
|
||||
false === isset($formData['type']) || $formData['type'] === 'ido' ? 'db' : $formData['type']
|
||||
)
|
||||
)
|
||||
),
|
||||
$this->createElement(
|
||||
'checkbox',
|
||||
'disabled',
|
||||
array(
|
||||
'required' => true,
|
||||
'label' => t('Disable This Backend')
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Form::addSubmitButton()
|
||||
*/
|
||||
public function addSubmitButton()
|
||||
{
|
||||
$this->addElement(
|
||||
'submit',
|
||||
'btn_submit',
|
||||
array(
|
||||
'label' => t('Save Changes')
|
||||
)
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the backend configuration values and its name
|
||||
*
|
||||
* The first value is the name and the second one the values as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBackendConfig()
|
||||
{
|
||||
$values = $this->getValues();
|
||||
$name = $values['name'];
|
||||
|
||||
if ($values['disabled'] == '0') {
|
||||
unset($values['disabled']);
|
||||
}
|
||||
|
||||
unset($values['name']);
|
||||
unset($values['btn_submit']);
|
||||
unset($values[$this->getTokenElementName()]);
|
||||
return array($name, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the form with the given configuration values
|
||||
*
|
||||
* @param string $name The name of the backend
|
||||
* @param array $config The configuration values
|
||||
*/
|
||||
public function setBackendConfig($name, array $config)
|
||||
{
|
||||
$config['name'] = $name;
|
||||
$this->populate($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of all resources of the given type ready to be used as content for a select input
|
||||
*
|
||||
* @param string $type The type of resources to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getResourcesByType($type)
|
||||
{
|
||||
$backends = array();
|
||||
foreach (array_keys(ResourceFactory::getResourceConfigs($type)->toArray()) as $name) {
|
||||
$backends[$name] = $name;
|
||||
}
|
||||
|
||||
return $backends;
|
||||
}
|
||||
}
|
|
@ -1,13 +1,2 @@
|
|||
<?php if ($this->name): ?>
|
||||
<h4><i> <i class="icinga-icon-edit"></i> Edit Backend "<?= $this->escape($this->name) ?>"</h4>
|
||||
<?php else: ?>
|
||||
<h4> <i class="icinga-icon-create"></i> Create New Backend</h4>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($this->error): ?>
|
||||
<div class="alert alert-danger">
|
||||
<?= $this->escape($this->error); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?= $this->form; ?>
|
||||
<h4><?= $this->translate('Edit Existing Backend'); ?></h4>
|
||||
<?= $form; ?>
|
Loading…
Reference in New Issue