summaryrefslogtreecommitdiffstats
path: root/application/controllers/TemplateController.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/controllers/TemplateController.php')
-rw-r--r--application/controllers/TemplateController.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/application/controllers/TemplateController.php b/application/controllers/TemplateController.php
new file mode 100644
index 0000000..bb37b3c
--- /dev/null
+++ b/application/controllers/TemplateController.php
@@ -0,0 +1,89 @@
+<?php
+// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Reporting\Controllers;
+
+use DateTime;
+use GuzzleHttp\Psr7\ServerRequest;
+use Icinga\Module\Reporting\Database;
+use Icinga\Module\Reporting\Web\Controller;
+use Icinga\Module\Reporting\Web\Forms\TemplateForm;
+use Icinga\Module\Reporting\Web\Widget\Template;
+use ipl\Sql\Select;
+
+class TemplateController extends Controller
+{
+ use Database;
+
+ public function indexAction()
+ {
+ $this->createTabs()->activate('preview');
+
+ $template = Template::fromDb($this->params->getRequired('id'));
+
+ if ($template === null) {
+ throw new \Exception('Template not found');
+ }
+
+ $template
+ ->setMacros([
+ 'date' => (new DateTime())->format('jS M, Y'),
+ 'time_frame' => 'Time Frame',
+ 'time_frame_absolute' => 'Time Frame (absolute)',
+ 'title' => 'Icinga Report Preview'
+ ])
+ ->setPreview(true);
+
+ $this->addContent($template);
+ }
+
+ public function editAction()
+ {
+ $this->assertPermission('reporting/templates');
+
+ $this->createTabs()->activate('edit');
+
+ $select = (new Select())
+ ->from('template')
+ ->columns(['id', 'settings'])
+ ->where(['id = ?' => $this->params->getRequired('id')]);
+
+ $template = $this->getDb()->select($select)->fetch();
+
+ if ($template === false) {
+ throw new \Exception('Template not found');
+ }
+
+ $template->settings = json_decode($template->settings, true);
+
+ $form = (new TemplateForm())
+ ->setTemplate($template);
+
+ $form->handleRequest(ServerRequest::fromGlobals());
+
+ $this->redirectForm($form, 'reporting/templates');
+
+ $this->addContent($form);
+ }
+
+ protected function createTabs()
+ {
+ $tabs = $this->getTabs();
+
+ if ($this->hasPermission('reporting/templates')) {
+ $tabs->add('edit', [
+ 'title' => $this->translate('Edit template'),
+ 'label' => $this->translate('Edit Template'),
+ 'url' => 'reporting/template/edit?id=' . $this->params->getRequired('id')
+ ]);
+ }
+
+ $tabs->add('preview', [
+ 'title' => $this->translate('Preview template'),
+ 'label' => $this->translate('Preview'),
+ 'url' => 'reporting/template?id=' . $this->params->getRequired('id')
+ ]);
+
+ return $tabs;
+ }
+}