config/module(en|dis)able: Utilize a form instead of a link

This commit is contained in:
Johannes Meyer 2018-10-08 15:50:02 +02:00
parent b3e0b5d587
commit 7ba0466048
4 changed files with 101 additions and 27 deletions

View File

@ -12,6 +12,7 @@ use Icinga\Application\Modules\Module;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotFoundError;
use Icinga\Forms\ActionForm;
use Icinga\Forms\Config\GeneralConfigForm;
use Icinga\Forms\Config\ResourceConfigForm;
use Icinga\Forms\Config\UserBackendConfigForm;
@ -122,7 +123,20 @@ class ConfigController extends Controller
$module = new Module($app, $name, $manager->getModuleDir($name));
}
$toggleForm = new ActionForm();
$toggleForm->setDefaults(['identifier' => $name]);
if (! $this->view->moduleData->enabled) {
$toggleForm->setAction(Url::fromPath('config/moduleenable'));
$toggleForm->setDescription(sprintf($this->translate('Enable the %s module'), $name));
} elseif ($this->view->moduleData->loaded) {
$toggleForm->setAction(Url::fromPath('config/moduledisable'));
$toggleForm->setDescription(sprintf($this->translate('Disable the %s module'), $name));
} else {
$toggleForm = null;
}
$this->view->module = $module;
$this->view->toggleForm = $toggleForm;
$this->view->tabs = $module->getConfigTabs()->activate('info');
$this->view->moduleGitCommitId = Version::getGitHead($module->getBaseDir());
} else {
@ -137,15 +151,20 @@ class ConfigController extends Controller
public function moduleenableAction()
{
$this->assertPermission('config/modules');
$module = $this->getParam('name');
$manager = Icinga::app()->getModuleManager();
try {
$manager->enableModule($module);
$form = new ActionForm();
$form->setOnSuccess(function (ActionForm $form) {
$module = $form->getValue('identifier');
Icinga::app()->getModuleManager()->enableModule($module);
Notification::success(sprintf($this->translate('Module "%s" enabled'), $module));
$this->rerenderLayout()->reloadCss()->redirectNow('config/modules');
});
try {
$form->handleRequest();
} catch (Exception $e) {
$this->view->exceptionMessage = $e->getMessage();
$this->view->moduleName = $module;
$this->view->moduleName = $form->getValue('name');
$this->view->action = 'enable';
$this->render('module-configuration-error');
}
@ -157,15 +176,20 @@ class ConfigController extends Controller
public function moduledisableAction()
{
$this->assertPermission('config/modules');
$module = $this->getParam('name');
$manager = Icinga::app()->getModuleManager();
try {
$manager->disableModule($module);
$form = new ActionForm();
$form->setOnSuccess(function (ActionForm $form) {
$module = $form->getValue('identifier');
Icinga::app()->getModuleManager()->disableModule($module);
Notification::success(sprintf($this->translate('Module "%s" disabled'), $module));
$this->rerenderLayout()->reloadCss()->redirectNow('config/modules');
});
try {
$form->handleRequest();
} catch (Exception $e) {
$this->view->exceptionMessage = $e->getMessage();
$this->view->moduleName = $module;
$this->view->moduleName = $form->getValue('name');
$this->view->action = 'disable';
$this->render('module-configuration-error');
}

View File

@ -0,0 +1,61 @@
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms;
use Icinga\Web\Form;
class ActionForm extends Form
{
/**
* The icon shown on the button
*
* @var string
*/
protected $icon = 'arrows-cw';
/**
* Set the icon to show on the button
*
* @param string $name
*
* @return $this
*/
public function setIcon($name)
{
$this->icon = (string) $name;
return $this;
}
public function init()
{
$this->setAttrib('class', 'inline');
$this->setUidDisabled(true);
$this->setDecorators(['FormElements', 'Form']);
}
public function createElements(array $formData)
{
$this->addElement(
'hidden',
'identifier',
[
'required' => true,
'decorators' => ['ViewHelper']
]
);
$this->addElement(
'button',
'btn_submit',
[
'escape' => false,
'type' => 'submit',
'class' => 'link-button spinner',
'value' => 'btn_submit',
'decorators' => ['ViewHelper'],
'label' => $this->getView()->icon($this->icon),
'title' => $this->getDescription()
]
);
}
}

View File

@ -18,22 +18,11 @@
</tr>
<tr>
<th><?= $this->translate('State') ?></th>
<td><?= $state ?><?php if ($state === 'enabled'): ?>
<?= $this->qlink(
$this->translate('disable'),
'config/moduledisable',
array('name' => $module->getName()),
array('title' => sprintf($this->translate('Disable the %s module'), $module->getName()), 'class' => 'action-link')
); ?>
<?php endif ?>
<?php if ($state === 'disabled'): ?>
<?= $this->qlink(
$this->translate('enable'),
'config/moduleenable',
array('name' => $module->getName()),
array('title' => sprintf($this->translate('Enable the %s module'), $module->getName()), 'class' => 'action-link')
); ?>
<?php endif ?>
<td>
<?= $state ?>
<?php if (isset($this->toggleForm)): ?>
<?= $this->toggleForm ?>
<?php endif ?>
</td>
<tr>
<th><?= $this->escape($this->translate('Version')) ?></th>

View File

@ -27,7 +27,7 @@
echo $this->qlink(
$module->name,
'config/module/',
'config/module',
array('name' => $module->name),
array(
'class' => 'rowaction',