summaryrefslogtreecommitdiffstats
path: root/application/controllers/TemplatesController.php
blob: 99a5dcbf371745a02228eab94134e86d26cd14cc (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
<?php

// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting\Controllers;

use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use Icinga\Module\Reporting\Web\Controller;
use Icinga\Module\Reporting\Web\Forms\TemplateForm;
use Icinga\Module\Reporting\Web\ReportsTimeframesAndTemplatesTabs;
use Icinga\Web\Notification;
use ipl\Html\Html;
use ipl\Web\Url;
use ipl\Web\Widget\ButtonLink;
use ipl\Web\Widget\Link;

class TemplatesController extends Controller
{
    use ReportsTimeframesAndTemplatesTabs;

    public function indexAction()
    {
        $this->createTabs()->activate('templates');

        $canManage = $this->hasPermission('reporting/templates');

        if ($canManage) {
            $this->addControl(
                (new ButtonLink(
                    $this->translate('New Template'),
                    Url::fromPath('reporting/templates/new'),
                    'plus'
                ))->openInModal()
            );
        }

        $templates = Model\Template::on(Database::get());

        $sortControl = $this->createSortControl(
            $templates,
            [
                'name'   => $this->translate('Name'),
                'author' => $this->translate('Author'),
                'ctime'  => $this->translate('Created At'),
                'mtime'  => $this->translate('Modified At')
            ]
        );

        $this->addControl($sortControl);

        $tableRows = [];

        /** @var Model\Template $template */
        foreach ($templates as $template) {
            // Preview URL
            $subjectLink = new Link($template->name, Url::fromPath('reporting/template', ['id' => $template->id]));
            $tableRows[] = Html::tag('tr', null, [
                Html::tag('td', null, $subjectLink),
                Html::tag('td', null, $template->author),
                Html::tag('td', null, $template->ctime->format('Y-m-d H:i')),
                Html::tag('td', null, $template->mtime->format('Y-m-d H:i'))
            ]);
        }

        if (! empty($tableRows)) {
            $table = Html::tag(
                'table',
                ['class' => 'common-table table-row-selectable', 'data-base-target' => '_next'],
                [
                    Html::tag(
                        'thead',
                        null,
                        Html::tag(
                            'tr',
                            null,
                            [
                                Html::tag('th', null, 'Name'),
                                Html::tag('th', null, 'Author'),
                                Html::tag('th', null, 'Date Created'),
                                Html::tag('th', null, 'Date Modified')
                            ]
                        )
                    ),
                    Html::tag('tbody', null, $tableRows)
                ]
            );

            $this->addContent($table);
        } else {
            $this->addContent(Html::tag('p', null, 'No templates created yet.'));
        }
    }

    public function newAction()
    {
        $this->assertPermission('reporting/templates');
        $this->addTitleTab($this->translate('New Template'));

        $form = (new TemplateForm())
            ->setAction((string) Url::fromRequest())
            ->on(TemplateForm::ON_SUCCESS, function () {
                Notification::success($this->translate('Created template successfully'));

                $this->closeModalAndRefreshRelatedView(Url::fromPath('reporting/templates'));
            })
            ->handleRequest($this->getServerRequest());

        $this->addContent($form);
    }
}