Skip to content

Commit e870efa

Browse files
committed
Update config form to allow for an arbitrary number of backends
No longer requires callouts
1 parent f613520 commit e870efa

4 files changed

Lines changed: 254 additions & 167 deletions

File tree

application/controllers/ConfigController.php

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66
namespace Icinga\Module\Pdfexport\Controllers;
77

88
use Icinga\Application\Config;
9+
use Icinga\Application\Logger;
910
use Icinga\Module\Pdfexport\Forms\BackendConfigForm;
11+
use Icinga\Web\Form\ConfigForm;
12+
use Icinga\Web\Notification;
13+
use ipl\Html\Attributes;
14+
use ipl\Html\Form;
1015
use ipl\Html\HtmlString;
16+
use ipl\Html\Table;
1117
use ipl\Web\Compat\CompatController;
1218
use Icinga\Web\Widget\Tabs;
19+
use ipl\Web\Widget\ButtonLink;
20+
use ipl\Web\Widget\Icon;
21+
use ipl\Web\Widget\Link;
1322

1423
class ConfigController extends CompatController
1524
{
@@ -20,17 +29,96 @@ public function init()
2029
parent::init();
2130
}
2231

23-
public function backendAction()
32+
public function backendsAction(): void
2433
{
34+
$button = new ButtonLink(
35+
$this->translate('Create a New Backend'),
36+
'pdfexport/config/createbackend',
37+
'plus',
38+
['title' => $this->translate('Create a New Backend')],
39+
);
40+
$button->setBaseTarget('_next');
41+
$this->addContent($button);
42+
43+
$table = new Table();
44+
$table->setAttributes(Attributes::create([
45+
'class' => 'table-row-selectable common-table',
46+
'data-base-target' => '_next',
47+
]));
48+
$table->add(Table::tr([
49+
Table::th($this->translate('Backend')),
50+
Table::th($this->translate('Priority')),
51+
]));
52+
53+
$config = Config::module('pdfexport');
54+
55+
$sections = [];
56+
foreach ($config as $name => $data) {
57+
$sections[] = [$name, $data, (int) $data->get('priority')];
58+
}
59+
60+
usort($sections, function ($a, $b) {
61+
return $a[2] <=> $b[2];
62+
});
63+
64+
foreach ($sections as [$name, $data]) {
65+
$table->add(Table::tr([
66+
Table::td([
67+
new Icon('print'),
68+
new Link($name, 'pdfexport/config/backend?backend=' . $name),
69+
]),
70+
Table::td($data->get('priority')),
71+
], [
72+
'class' => 'clickable',
73+
]));
74+
}
75+
76+
$this->mergeTabs($this->Module()->getConfigTabs()->activate('backends'));
77+
$this->addContent($table);
78+
}
79+
80+
public function backendAction(): void
81+
{
82+
$name = $this->params->shiftRequired('backend');
83+
$this->addTitleTab($this->translate(sprintf('Edit %s', $name)));
84+
2585
$form = new BackendConfigForm();
2686
$form->setConfig(Config::module('pdfexport'));
87+
$form->setSection($name);
88+
89+
$form->on(Form::ON_SUBMIT, function () use ($form) {
90+
Notification::success($this->translate('Updated print backend'));
91+
$this->redirectNow('__CLOSE__');
92+
});
93+
94+
$form->on(ConfigForm::ON_DELETE, function () use ($form) {
95+
Notification::success($this->translate('Print backend deleted'));
96+
$this->redirectNow('__CLOSE__');
97+
});
2798

2899
$form->handleRequest($this->getServerRequest());
29100

30-
$this->mergeTabs($this->Module()->getConfigTabs()->activate('backend'));
31101
$this->addContent(HtmlString::create($form->render()));
32102
}
33103

104+
public function createbackendAction(): void
105+
{
106+
$this->addTitleTab($this->translate(sprintf('Create Print Backend')));
107+
108+
$form = new BackendConfigForm();
109+
$form->setConfig(Config::module('pdfexport'));
110+
$form->setIsCreateForm(true);
111+
112+
$form->on(Form::ON_SUBMIT, function () {
113+
Notification::success($this->translate('Created new print backend'));
114+
$this->redirectNow('__CLOSE__');
115+
});
116+
117+
$form->handleRequest($this->getServerRequest());
118+
119+
$this->addContent($form);
120+
}
121+
34122
protected function mergeTabs(Tabs $tabs): void
35123
{
36124
foreach ($tabs->getTabs() as $tab) {

0 commit comments

Comments
 (0)