From 980de7e23e03c1e2fd1cbb8d726aea631ec8d4bc Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 15 Sep 2023 16:56:22 +0200 Subject: [PATCH] migrations/index: Let the migrate all button submit the migration form --- .../controllers/MigrationsController.php | 24 +++++++++++-------- application/forms/MigrationForm.php | 10 ++++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/application/controllers/MigrationsController.php b/application/controllers/MigrationsController.php index 1260a870b0d..5229f066c21 100644 --- a/application/controllers/MigrationsController.php +++ b/application/controllers/MigrationsController.php @@ -14,6 +14,7 @@ use Icinga\Web\Widget\ItemList\MigrationList; use Icinga\Web\Widget\Tabextension\OutputFormat; use ipl\Html\Attributes; +use ipl\Html\FormElement\SubmitButtonElement; use ipl\Html\HtmlElement; use ipl\Html\Text; use ipl\Web\Compat\CompatController; @@ -53,22 +54,25 @@ public function indexAction(): void } $migrateListForm = new MigrationForm(); + $migrateListForm->setAttribute('id', $this->getRequest()->protectId('migration-form')); $migrateListForm->setRenderDatabaseUserChange(! $mm->validateDatabasePrivileges()); - $migrateGlobalForm = new MigrationForm(); - $migrateGlobalForm->getAttributes()->set('name', sprintf('migrate-%s', DbMigrationHook::ALL_MIGRATIONS)); - if ($canApply && $mm->hasPendingMigrations()) { - $migrateGlobalForm->addElement('submit', sprintf('migrate-%s', DbMigrationHook::ALL_MIGRATIONS), [ - 'required' => true, - 'label' => $this->translate('Migrate All'), - 'title' => $this->translate('Migrate all pending migrations') + $migrateAllButton = new SubmitButtonElement(sprintf('migrate-%s', DbMigrationHook::ALL_MIGRATIONS), [ + 'form' => $migrateListForm->getAttribute('id')->getValue(), + 'label' => $this->translate('Migrate All'), + 'title' => $this->translate('Migrate all pending migrations') ]); - $this->controls->getAttributes()->add('class', 'default-layout'); - $this->handleMigrateRequest($migrateGlobalForm); + // Is the first button, so will be cloned and that the visible + // button is outside the form doesn't matter for Web's JS + $migrateListForm->registerElement($migrateAllButton); - $this->addControl($migrateGlobalForm); + // Make sure it looks familiar, even if not inside a form + $migrateAllButton->setWrapper(new HtmlElement('div', Attributes::create(['class' => 'icinga-controls']))); + + $this->controls->getAttributes()->add('class', 'default-layout'); + $this->addControl($migrateAllButton); } $this->handleFormatRequest($mm->toArray()); diff --git a/application/forms/MigrationForm.php b/application/forms/MigrationForm.php index 120c84684b9..c5d517f03b7 100644 --- a/application/forms/MigrationForm.php +++ b/application/forms/MigrationForm.php @@ -6,7 +6,6 @@ use Icinga\Application\MigrationManager; use ipl\Html\Attributes; -use ipl\Html\Form; use ipl\Html\FormElement\CheckboxElement; use ipl\Html\FormElement\FieldsetElement; use ipl\Html\HtmlElement; @@ -15,10 +14,11 @@ use ipl\Validator\CallbackValidator; use ipl\Web\Common\CsrfCounterMeasure; use ipl\Web\Common\FormUid; +use ipl\Web\Compat\CompatForm; use ipl\Web\FormDecorator\IcingaFormDecorator; use PDOException; -class MigrationForm extends Form +class MigrationForm extends CompatForm { use CsrfCounterMeasure; use FormUid; @@ -50,6 +50,12 @@ public function setRenderDatabaseUserChange(bool $value = true): self return $this; } + public function hasDefaultElementDecorator() + { + // The base implementation registers a decorator we don't want here + return false; + } + protected function assemble(): void { $this->addHtml($this->createUidElement());