summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/ItemList/MigrationListItem.php
blob: 284ce4c84b5606489ce8a54d8b8b31adabc3be43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

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

namespace Icinga\Web\Widget\ItemList;

use Icinga\Application\Hook\Common\DbMigrationStep;
use Icinga\Application\Hook\DbMigrationHook;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Contract\FormElement;
use ipl\Html\FormattedString;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Web\Common\BaseListItem;
use ipl\Web\Url;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
use LogicException;

class MigrationListItem extends BaseListItem
{
    use Translation;

    /** @var ?FormElement */
    protected $migrateButton;

    /** @var DbMigrationHook Just for type hint */
    protected $item;

    /**
     * Set a migration form of this list item
     *
     * @param FormElement $migrateButton
     *
     * @return $this
     */
    public function setMigrateButton(FormElement $migrateButton): self
    {
        $this->migrateButton = $migrateButton;

        return $this;
    }

    protected function assembleTitle(BaseHtmlElement $title): void
    {
        $title->addHtml(
            FormattedString::create(
                t('%s ', '<name>'),
                HtmlElement::create('span', ['class' => 'subject'], $this->item->getName())
            )
        );
    }

    protected function assembleHeader(BaseHtmlElement $header): void
    {
        if ($this->migrateButton === null) {
            throw new LogicException('Please set the migrate submit button beforehand');
        }

        $header->addHtml($this->createTitle());
        $header->addHtml($this->migrateButton);
    }

    protected function assembleCaption(BaseHtmlElement $caption): void
    {
        $migrations = $this->item->getMigrations();
        /** @var DbMigrationStep $migration */
        $migration = array_shift($migrations);
        if ($migration->getLastState()) {
            if ($migration->getDescription()) {
                $caption->addHtml(Text::create($migration->getDescription()));
            } else {
                $caption->addHtml(new EmptyState(Text::create($this->translate('No description provided.'))));
            }

            $scriptPath = $migration->getScriptPath();
            /** @var string $parentDirs */
            $parentDirs = substr($scriptPath, (int) strpos($scriptPath, 'schema'));
            $parentDirs = substr($parentDirs, 0, strrpos($parentDirs, '/') + 1);

            $title = new HtmlElement('div', Attributes::create(['class' => 'title']));
            $title->addHtml(
                new HtmlElement('span', null, Text::create($parentDirs)),
                new HtmlElement(
                    'span',
                    Attributes::create(['class' => 'version']),
                    Text::create($migration->getVersion() . '.sql')
                ),
                new HtmlElement(
                    'span',
                    Attributes::create(['class' => 'upgrade-failed']),
                    Text::create($this->translate('Upgrade failed'))
                )
            );

            $error = new HtmlElement('div', Attributes::create([
                'class'               => 'collapsible',
                'data-visible-height' => '58',
            ]));
            $error->addHtml(new HtmlElement('pre', null, new HtmlString(Html::escape($migration->getLastState()))));

            $errorSection = new HtmlElement('div', Attributes::create(['class' => 'errors-section',]));
            $errorSection->addHtml(
                new HtmlElement('header', null, new Icon('circle-xmark', ['class' => 'status-icon']), $title),
                $caption,
                $error
            );

            $caption->prependWrapper($errorSection);
        }
    }

    protected function assembleFooter(BaseHtmlElement $footer): void
    {
        $footer->addHtml((new MigrationList($this->item->getLatestMigrations(3)))->setMinimal(false));
        if ($this->item->count() > 3) {
            $footer->addHtml(
                new Link(
                    sprintf($this->translate('Show all %d migrations'), $this->item->count()),
                    Url::fromPath(
                        'migrations/migration',
                        [DbMigrationHook::MIGRATION_PARAM => $this->item->getModuleName()]
                    ),
                    [
                        'data-base-target' => '_next',
                        'class' => 'show-more'
                    ]
                )
            );
        }
    }

    protected function assembleMain(BaseHtmlElement $main): void
    {
        $main->addHtml($this->createHeader());
        $caption = $this->createCaption();
        if (! $caption->isEmpty()) {
            $main->addHtml($caption);
        }

        $footer = $this->createFooter();
        if ($footer) {
            $main->addHtml($footer);
        }
    }
}