summaryrefslogtreecommitdiffstats
path: root/application/controllers/CommandTransportController.php
blob: fe005375e251b4b17b8a6b75516c12a3f30f714f (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
152
153
154
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Controllers;

use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Application\Config;
use Icinga\Data\Filter\Filter;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Module\Icingadb\Command\Transport\CommandTransportConfig;
use Icinga\Module\Icingadb\Forms\ApiTransportForm;
use Icinga\Module\Icingadb\Widget\ItemList\CommandTransportList;
use Icinga\Web\Notification;
use ipl\Html\HtmlString;
use ipl\Web\Widget\ButtonLink;

class CommandTransportController extends ConfigController
{
    public function init()
    {
        $this->assertPermission('config/modules');
    }

    public function indexAction()
    {
        $list = new CommandTransportList((new CommandTransportConfig())->select());

        $this->addControl(
            (new ButtonLink(
                t('Create Command Transport'),
                'icingadb/command-transport/add',
                'plus'
            ))->setBaseTarget('_next')
        );

        $this->addContent($list);

        $this->mergeTabs($this->Module()->getConfigTabs());
        $this->getTabs()->disableLegacyExtensions();
        $this->setTitle($this->getTabs()
            ->activate('command-transports')
            ->getActiveTab()
            ->getLabel());
    }

    public function showAction()
    {
        $transportName = $this->params->getRequired('name');

        $transportConfig = (new CommandTransportConfig())
            ->select()
            ->where('name', $transportName)
            ->fetchRow();
        if ($transportConfig === false) {
            $this->httpNotFound(t('Unknown transport'));
        }

        $form = new ApiTransportForm();
        $form->populate((array) $transportConfig);
        $form->on(ApiTransportForm::ON_SUCCESS, function (ApiTransportForm $form) use ($transportName) {
            (new CommandTransportConfig())->update(
                'transport',
                $form->getValues(),
                Filter::where('name', $transportName)
            );

            Notification::success(sprintf(t('Updated command transport "%s" successfully'), $transportName));

            $this->redirectNow('icingadb/command-transport');
        });

        $form->handleRequest(ServerRequest::fromGlobals());

        $this->addContent($form);

        $this->addTitleTab($this->translate('Command Transport: %s'), $transportName);
        $this->getTabs()->disableLegacyExtensions();
    }

    public function addAction()
    {
        $form = new ApiTransportForm();
        $form->on(ApiTransportForm::ON_SUCCESS, function (ApiTransportForm $form) {
            (new CommandTransportConfig())->insert('transport', $form->getValues());

            Notification::success(t('Created command transport successfully'));

            $this->redirectNow('icingadb/command-transport');
        });

        $form->handleRequest(ServerRequest::fromGlobals());

        $this->addContent($form);

        $this->addTitleTab($this->translate('Add Command Transport'));
        $this->getTabs()->disableLegacyExtensions();
    }

    public function removeAction()
    {
        $transportName = $this->params->getRequired('name');

        $form = new ConfirmRemovalForm();
        $form->setOnSuccess(function () use ($transportName) {
            (new CommandTransportConfig())->delete(
                'transport',
                Filter::where('name', $transportName)
            );

            Notification::success(sprintf(t('Removed command transport "%s" successfully'), $transportName));

            $this->redirectNow('icingadb/command-transport');
        });

        $form->handleRequest();

        $this->addContent(HtmlString::create($form->render()));

        $this->setTitle($this->translate('Remove Command Transport: %s'), $transportName);
        $this->getTabs()->disableLegacyExtensions();
    }

    public function sortAction()
    {
        $transportName = $this->params->getRequired('name');
        $newPosition = (int) $this->params->getRequired('pos');

        $config = $this->Config('commandtransports');
        if (! $config->hasSection($transportName)) {
            $this->httpNotFound(t('Unknown transport'));
        }

        if ($newPosition < 0 || $newPosition > $config->count()) {
            $this->httpBadRequest(t('Position out of bounds'));
        }

        $transports = $config->getConfigObject()->toArray();
        $transportNames = array_keys($transports);

        array_splice($transportNames, array_search($transportName, $transportNames, true), 1);
        array_splice($transportNames, $newPosition, 0, [$transportName]);

        $sortedTransports = [];
        foreach ($transportNames as $name) {
            $sortedTransports[$name] = $transports[$name];
        }

        $newConfig = Config::fromArray($sortedTransports);
        $newConfig->saveIni($config->getConfigFile());

        $this->redirectNow('icingadb/command-transport');
    }
}