SetAsBackendForm: Make it a ipl\Html\Form

This commit is contained in:
Johannes Meyer 2022-02-24 16:22:37 +01:00
parent 598d26f0f9
commit fb6eb88ddf
5 changed files with 48 additions and 60 deletions

View file

@ -5,6 +5,7 @@
namespace Icinga\Module\Icingadb\Controllers;
use Exception;
use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Exception\IcingaException;
use Icinga\Module\Icingadb\Compat\UrlMigrator;
use Icinga\Module\Icingadb\Forms\SetAsBackendForm;
@ -68,23 +69,18 @@ class MigrateController extends Controller
{
$this->assertHttpMethod('get');
$this->getResponse()
->setBody(IcingadbSupportHook::isIcingaDbSetAsPreferredBackend())
->sendResponse();
exit;
$form = new SetAsBackendForm();
$form->setAction(Url::fromPath('icingadb/migrate/checkbox-submit')->getAbsoluteUrl());
$this->getDocument()->addHtml($form);
}
public function checkboxSubmitAction()
{
$this->assertHttpMethod('post');
$this->addPart(HtmlString::create('"bogus"'), 'Behavior:Migrate');
$form = (new SetAsBackendForm())
->setOnSuccess(function () use (&$form) {
$this->addPart(HtmlString::create('"bogus"'), 'Behavior:Migrate');
$form->save($form->getElement('backend')->isChecked());
return false;
});
$form->handleRequest();
(new SetAsBackendForm())->handleRequest(ServerRequest::fromGlobals());
}
public function backendSupportAction()

View file

@ -4,20 +4,31 @@
namespace Icinga\Module\Icingadb\Forms;
class SetAsBackendForm extends SetAsBackendConfigForm
use Icinga\Module\Icingadb\Hook\IcingadbSupportHook;
use Icinga\Web\Session;
use ipl\Web\Compat\CompatForm;
class SetAsBackendForm extends CompatForm
{
public function init()
protected $defaultAttributes = [
'id' => 'setAsBackendForm',
'class' => 'icinga-controls'
];
protected function assemble()
{
$this->setName('IcingaModuleIcingadbFormsSetAsBackendForm');
$this->setTokenDisabled();
// If you change name here, please change in migration.js also.
$this->addElement('checkbox', 'backend', [
'class' => 'autosubmit',
'label' => t('Use Icinga DB As Backend'),
'value' => IcingadbSupportHook::isIcingaDbSetAsPreferredBackend()
]);
}
public function createElements(array $formData)
public function onSuccess()
{
parent::createElements([]);
$this->removeElement('btn_submit');
$this->removeElement('btn_submit_session');
Session::getSession()->getNamespace('icingadb')->set(
IcingadbSupportHook::PREFERENCE_NAME,
$this->getElement('backend')->isChecked()
);
}
}

View file

@ -5,15 +5,15 @@
namespace Icinga\Module\Icingadb\Hook;
use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use Icinga\Web\Session;
abstract class IcingadbSupportHook
{
use HookUtils;
/** @var string key name of preference */
const PREFERENCE_NAME = 'icingadb_as_backend';
const PREFERENCE_NAME = 'icingadb.as_backend';
/**
* Return whether your module supports IcingaDB or not
@ -32,14 +32,9 @@ abstract class IcingadbSupportHook
*/
final public static function isIcingaDbSetAsPreferredBackend(): bool
{
if ($user = Auth::getInstance()->getUser()) {
$webPreferences = $user->getPreferences()->get('icingaweb');
if (! empty($webPreferences) && array_key_exists(static::PREFERENCE_NAME, $webPreferences)) {
return (bool) $webPreferences[static::PREFERENCE_NAME];
}
}
return false;
return (bool) Session::getSession()
->getNamespace('icingadb')
->get(self::PREFERENCE_NAME, false);
}
/**

View file

@ -146,11 +146,11 @@
margin-top: 0.5em;
width: 100%;
.wrapper {
.control-group {
display: flex;
align-items: center;
.checkbox-label {
.control-label-group {
margin-right: .5em;
}

View file

@ -24,22 +24,6 @@
' <button type="button" value="0"><i class="icon-"></i></button>\n' +
'</li>';
const BACKEND_FORM = '<form id="setAsBackendForm" name="IcingaModuleIcingadbFormsSetAsBackendForm" ' +
'class="icinga-form icinga-controls" method="post" action="icingadb/migrate/checkbox-submit" ' +
'data-base-target="migrate-popup-backend-submit-blackhole">\n' +
' <div class="wrapper">\n' +
' <div class="checkbox-label">\n' +
' <span><label for="setAsBackendForm-checkbox">Use Icinga DB As Backend</label></span>\n' +
' </div>\n' +
' <input type="checkbox" name="backend" value="1" id="setAsBackendForm-checkbox" class="autosubmit sr-only">\n' +
' <label for="setAsBackendForm-checkbox" aria-hidden="true" class="toggle-switch">\n' +
' <span class="toggle-slider"></span>\n' +
' </label>\n' +
' </div>\n' +
' <input type="hidden" name="formUID" value="IcingaModuleIcingadbFormsSetAsBackendForm" id="IcingaModuleIcingadbFormsSetAsBackendForm">\n' +
' <div id="migrate-popup-backend-submit-blackhole"></div>\n' +
'</form>';
/**
* Icinga DB Migration behavior.
*
@ -370,29 +354,31 @@
});
if (Object.keys(supportedModules).length) {
let $form = this.Popup().find('.suggestion-area > #setAsBackendForm');
if (! $form.length) {
$form = $(BACKEND_FORM);
this.Popup().find('.suggestion-area > ul').after($form);
}
this.backendSupportRelated = { ...this.backendSupportRelated, ...supportedModules };
let req = $.ajax({
context : this,
type : 'get',
url : this.icinga.config.baseUrl + '/icingadb/migrate/checkbox-state'
url : this.icinga.config.baseUrl + '/icingadb/migrate/checkbox-state?showCompact'
});
req.done(this.setCheckboxState);
}
};
Migrate.prototype.setCheckboxState = function(isChecked, textStatus, req) {
var $form = this.Popup().find('.suggestion-area > #setAsBackendForm');
var $checkbox = $form.find('input#setAsBackendForm-checkbox');
Migrate.prototype.setCheckboxState = function(html, textStatus, req) {
let $form = this.Popup().find('.suggestion-area > #setAsBackendForm');
if (! $form.length) {
$form = $(html);
$form.attr('data-base-target', 'migrate-popup-backend-submit-blackhole');
$form.append('<div id="migrate-popup-backend-submit-blackhole"></div>');
this.Popup().find('.suggestion-area > ul').after($form);
} else {
let $newForm = $(html);
$form.find('[name=backend]').prop('checked', $newForm.find('[name=backend]').is(':checked'));
}
$checkbox.prop('checked', isChecked);
this.showPopup();
}