From 438f7aa9f506a2d61c07ec944ec0aaa19058a943 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 24 Jul 2023 15:55:52 +0200 Subject: [PATCH] Don't render apply all button when there is no result set --- library/Icinga/Web/Widget/Migrations.php | 44 +++++++++--------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/library/Icinga/Web/Widget/Migrations.php b/library/Icinga/Web/Widget/Migrations.php index e276aa458a..43d2352605 100644 --- a/library/Icinga/Web/Widget/Migrations.php +++ b/library/Icinga/Web/Widget/Migrations.php @@ -5,6 +5,8 @@ namespace Icinga\Web\Widget; use Generator; +use Icinga\Data\Filter\Filter; +use Icinga\Data\SimpleQuery; use Icinga\Forms\MigrationForm; use Icinga\Web\Widget\ItemList\MigrationList; use ipl\Html\BaseHtmlElement; @@ -18,19 +20,25 @@ class Migrations extends BaseHtmlElement protected $defaultAttributes = ['class' => 'migrations']; - /** @var Generator */ + /** @var Traversable */ protected $moduleMigrations; - /** @var Generator */ + /** @var Traversable */ protected $systemMigrations; /** @var callable */ protected $formCallback; - public function __construct(Traversable $migrations, callable $formCallback) + public function __construct(SimpleQuery $migrations, callable $formCallback) { - $this->moduleMigrations = $this->yieldModuleMigrations($migrations); - $this->systemMigrations = $this->yieldSystemMigrations($migrations); + $modulesQuery = clone $migrations; + $modulesQuery->addFilter(Filter::where('isModule', true)); + + $systemQuery = clone $migrations; + $systemQuery->addFilter(Filter::where('isModule', false)); + + $this->moduleMigrations = $modulesQuery; + $this->systemMigrations = $systemQuery; $this->formCallback = $formCallback; } @@ -44,10 +52,10 @@ protected function assemble() call_user_func_array($this->formCallback, [$migrationForm]); $header = HtmlElement::create('div', ['class' => 'modules-header']); - $header->addHtml( - HtmlElement::create('h2', ['class' => 'migration-list-header'], Text::create(t('Modules'))), - $migrationForm - ); + $header->addHtml(HtmlElement::create('h2', ['class' => 'migration-list-header'], Text::create(t('Modules')))); + if ($this->moduleMigrations->hasResult()) { + $header->addHtml($migrationForm); + } $moduleListControl = HtmlElement::create('div', ['class' => 'migration-list-control']); $moduleListControl->addHtml($header, new MigrationList($this->moduleMigrations, $this->formCallback)); @@ -55,22 +63,4 @@ protected function assemble() $this->addHtml($frameWorkListControl); $this->addHtml($moduleListControl); } - - protected function yieldSystemMigrations(Traversable $migrations): Generator - { - foreach ($migrations as $migration) { - if (! $migration->isModule) { - yield $migration; - } - } - } - - protected function yieldModuleMigrations(Traversable $migrations): Generator - { - foreach ($migrations as $migration) { - if ($migration->isModule) { - yield $migration; - } - } - } }