summaryrefslogtreecommitdiffstats
path: root/application/controllers/TemplateController.php
blob: bb37b3c27342a388a7adfa8bfbb0aa07d97f3a50 (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
<?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;
    }
}