summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/ItemList/MigrationFileListItem.php
blob: 007a730e009abaa595d528944be66a5754a37eb9 (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
<?php

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

namespace Icinga\Web\Widget\ItemList;

use Icinga\Application\Hook\Common\DbMigrationStep;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
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\Widget\EmptyState;
use ipl\Web\Widget\Icon;

class MigrationFileListItem extends BaseListItem
{
    use Translation;

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

    protected function assembleVisual(BaseHtmlElement $visual): void
    {
        if ($this->item->getLastState()) {
            $visual->getAttributes()->add('class', 'upgrade-failed');
            $visual->addHtml(new Icon('circle-xmark'));
        }
    }

    protected function assembleTitle(BaseHtmlElement $title): void
    {
        $scriptPath = $this->item->getScriptPath();
        /** @var string $parentDirs */
        $parentDirs = substr($scriptPath, (int) strpos($scriptPath, 'schema'));
        $parentDirs = substr($parentDirs, 0, strrpos($parentDirs, '/') + 1);

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

        if ($this->item->getLastState()) {
            $title->addHtml(
                new HtmlElement(
                    'span',
                    Attributes::create(['class' => 'upgrade-failed']),
                    Text::create($this->translate('Upgrade failed'))
                )
            );
        }
    }

    protected function assembleHeader(BaseHtmlElement $header): void
    {
        $header->addHtml($this->createTitle());
    }

    protected function assembleCaption(BaseHtmlElement $caption): void
    {
        if ($this->item->getDescription()) {
            $caption->addHtml(Text::create($this->item->getDescription()));
        } else {
            $caption->addHtml(new EmptyState(Text::create($this->translate('No description provided.'))));
        }
    }

    protected function assembleFooter(BaseHtmlElement $footer): void
    {
        if ($this->item->getLastState()) {
            $footer->addHtml(
                new HtmlElement(
                    'section',
                    Attributes::create(['class' => 'caption']),
                    new HtmlElement('pre', null, new HtmlString(Html::escape($this->item->getLastState())))
                )
            );
        }
    }

    protected function assembleMain(BaseHtmlElement $main): void
    {
        $main->addHtml($this->createHeader(), $this->createCaption());
    }
}