Skip to content

Commit

Permalink
Introduce MigrationList controller & widget
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jun 26, 2023
1 parent 265d2bd commit 6b9292a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
45 changes: 45 additions & 0 deletions application/controllers/MigrationListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/* Icinga Web 2 | (c) 2023 Icinga GmbH | GPLv2+ */

namespace Icinga\Controllers;

use Icinga\Application\Hook\MigrationHook;
use Icinga\Web\View\MigrationList;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Web\Compat\CompatController;

class MigrationListController extends CompatController
{
public function indexAction()
{
$migrations = MigrationHook::getPendingMigrations()->select();
$this->setupSortControl(
[
'name' => $this->translate('Name'),
'state' => $this->translate('State')
],
$migrations,
['name' => 'DESC']
);
$this->setupLimitControl();
$this->setupPaginationControl($migrations);
$this->setupFilterControl($migrations, [
'name' => $this->translate('Name'),
'state' => $this->translate('State'),
], ['name']);

$this->addControl(HtmlString::create((string) $this->view->paginator));
$this->addControl(HtmlElement::create('div', ['class' => 'sort-controls-container'], [
HtmlString::create((string) $this->view->limiter),
HtmlString::create((string) $this->view->sortBox)
]));
$this->addControl(HtmlString::create((string) $this->view->filterEditor));

$this->addTitleTab($this->translate('Migrations'));
$this->getTabs()->disableLegacyExtensions();
$this->setAutorefreshInterval(10);
$this->addContent(new MigrationList($migrations));
}
}
72 changes: 72 additions & 0 deletions library/Icinga/Web/View/MigrationList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Icinga\Web\View;

use Icinga\Authentication\Auth;
use ipl\Html\FormattedString;
use ipl\Html\HtmlElement;
use ipl\Html\Table;
use ipl\Html\Text;
use ipl\Web\Common\BaseTarget;
use ipl\Web\Widget\Link;
use Traversable;

class MigrationList extends Table
{
use BaseTarget;

protected $defaultAttributes = ['class' => ['app-health', 'common-table', 'table-row-selectable']];

/** @var Traversable */
protected $migrations = [];

public function __construct(Traversable $migrations)
{
$this->migrations = $migrations;
}

protected function assemble()
{
if (empty($this->migrations)) {
$this->addHtml(HtmlElement::create('pre', null, Text::create(t('There are no pending migrations'))));

return;
}

$hasPermission = Auth::getInstance()->hasPermission('application/migrations');
foreach ($this->migrations as $migration) {
$attributes = [
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
];

$name = HtmlElement::create('span', null, $migration->name);
$this->addHtml(
Table::tr([
Table::th(HtmlElement::create('span', ['class' => ['ball', 'ball-size-xl', $migration->state]])),
Table::td([
new HtmlElement(
'header',
null,
FormattedString::create(
t(
'%s has %s %s %s migrations',
'<name> has <count-migrations> <migration-type> <state-text> migrations'
),
$migration->url && $hasPermission
? new Link($name, $migration->url, $attributes)
: $name,
HtmlElement::create('span', null, $migration->pending ?: ""),
HtmlElement::create('strong', null, $migration->stateText),
$migration->db
? HtmlElement::create('span', null, 'DB')
: HtmlElement::create('span', null, 'PHP-Code')
)
),
HtmlElement::create('section', null, $migration->description)
])
], $attributes)
);
}
}
}

0 comments on commit 6b9292a

Please sign in to comment.