summaryrefslogtreecommitdiffstats
path: root/application/controllers/CommandTransportController.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:36:40 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:36:40 +0000
commita0901c4b7f2db488cb4fb3be2dd921a0308f4659 (patch)
treefafb393cf330a60df129ff10d0059eb7b14052a7 /application/controllers/CommandTransportController.php
parentInitial commit. (diff)
downloadicingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.tar.xz
icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.zip
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'application/controllers/CommandTransportController.php')
-rw-r--r--application/controllers/CommandTransportController.php155
1 files changed, 155 insertions, 0 deletions
diff --git a/application/controllers/CommandTransportController.php b/application/controllers/CommandTransportController.php
new file mode 100644
index 0000000..b0b4c32
--- /dev/null
+++ b/application/controllers/CommandTransportController.php
@@ -0,0 +1,155 @@
+<?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->setAttrib('style', 'text-align:center;');
+ $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');
+ }
+}