summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/ItemList/MigrationList.php
blob: 43699d3e5995c18aa6f6113a574651978663434c (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
<?php

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

namespace Icinga\Web\Widget\ItemList;

use Generator;
use Icinga\Application\Hook\Common\DbMigrationStep;
use Icinga\Application\Hook\DbMigrationHook;
use Icinga\Application\MigrationManager;
use Icinga\Forms\MigrationForm;
use ipl\I18n\Translation;
use ipl\Web\Common\BaseItemList;
use ipl\Web\Widget\EmptyStateBar;

class MigrationList extends BaseItemList
{
    use Translation;

    protected $baseAttributes = ['class' => 'item-list'];

    /** @var Generator<DbMigrationHook> */
    protected $data;

    /** @var ?MigrationForm */
    protected $migrationForm;

    /** @var bool Whether to render minimal migration list items */
    protected $minimal = true;

    /**
     * Create a new migration list
     *
     * @param Generator<DbMigrationHook>|array<DbMigrationStep|DbMigrationHook> $data
     *
     * @param ?MigrationForm $form
     */
    public function __construct($data, MigrationForm $form = null)
    {
        parent::__construct($data);

        $this->migrationForm = $form;
    }

    /**
     * Set whether to render minimal migration list items
     *
     * @param bool $minimal
     *
     * @return $this
     */
    public function setMinimal(bool $minimal): self
    {
        $this->minimal = $minimal;

        return $this;
    }

    /**
     * Get whether to render minimal migration list items
     *
     * @return bool
     */
    public function isMinimal(): bool
    {
        return $this->minimal;
    }

    protected function getItemClass(): string
    {
        if ($this->isMinimal()) {
            return MigrationListItem::class;
        }

        return MigrationFileListItem::class;
    }

    protected function assemble(): void
    {
        $itemClass = $this->getItemClass();
        if (! $this->isMinimal()) {
            $this->getAttributes()->add('class', 'file-list');
        }

        /** @var DbMigrationHook $data */
        foreach ($this->data as $data) {
            /** @var MigrationFileListItem|MigrationListItem $item */
            $item = new $itemClass($data, $this);
            if ($item instanceof MigrationListItem && $this->migrationForm) {
                $migrateButton = $this->migrationForm->createElement(
                    'submit',
                    sprintf('migrate-%s', $data->getModuleName()),
                    [
                        'required' => false,
                        'label'    => $this->translate('Migrate'),
                        'title'    => sprintf(
                            $this->translatePlural(
                                'Migrate %d pending migration',
                                'Migrate all %d pending migrations',
                                $data->count()
                            ),
                            $data->count()
                        )
                    ]
                );

                $mm = MigrationManager::instance();
                if ($data->isModule() && $mm->hasMigrations(DbMigrationHook::DEFAULT_MODULE)) {
                    $migrateButton->getAttributes()
                        ->set('disabled', true)
                        ->set(
                            'title',
                            $this->translate(
                                'Please apply all the pending migrations of Icinga Web first or use the apply all'
                                . ' button instead.'
                            )
                        );
                }

                $this->migrationForm->registerElement($migrateButton);

                $item->setMigrateButton($migrateButton);
            }

            $this->addHtml($item);
        }

        if ($this->isEmpty()) {
            $this->setTag('div');
            $this->addHtml(new EmptyStateBar(t('No items found.')));
        }
    }
}