diff --git a/application/controllers/MigrateController.php b/application/controllers/MigrateController.php index b032efff..65643512 100644 --- a/application/controllers/MigrateController.php +++ b/application/controllers/MigrateController.php @@ -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() diff --git a/application/forms/SetAsBackendForm.php b/application/forms/SetAsBackendForm.php index 2c003138..28406335 100644 --- a/application/forms/SetAsBackendForm.php +++ b/application/forms/SetAsBackendForm.php @@ -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() + ); } } diff --git a/library/Icingadb/Hook/IcingadbSupportHook.php b/library/Icingadb/Hook/IcingadbSupportHook.php index 5b84723b..cd43a5ac 100644 --- a/library/Icingadb/Hook/IcingadbSupportHook.php +++ b/library/Icingadb/Hook/IcingadbSupportHook.php @@ -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); } /** diff --git a/public/css/widget/migrate-popup.less b/public/css/widget/migrate-popup.less index 65e9e9bb..f7adb22f 100644 --- a/public/css/widget/migrate-popup.less +++ b/public/css/widget/migrate-popup.less @@ -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; } diff --git a/public/js/migrate.js b/public/js/migrate.js index 101892db..4198e160 100644 --- a/public/js/migrate.js +++ b/public/js/migrate.js @@ -24,22 +24,6 @@ ' \n' + ''; - const BACKEND_FORM = '
\n' + - '
\n' + - '
\n' + - ' \n' + - '
\n' + - ' \n' + - ' \n' + - '
\n' + - ' \n' + - '
\n' + - '
'; - /** * 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('
'); + + 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(); }