Skip to content

Commit

Permalink
migrations/index: Let the migrate all button submit the migration form
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Sep 15, 2023
1 parent 60cd647 commit 980de7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
24 changes: 14 additions & 10 deletions application/controllers/MigrationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 8 additions & 2 deletions application/forms/MigrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 980de7e

Please sign in to comment.