summaryrefslogtreecommitdiffstats
path: root/application/controllers/ReportController.php.modal
blob: 915bf2bfceee5fd5120be22d1e7d64681695854c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

namespace Icinga\Module\Reporting\Controllers;

use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Report;
use Icinga\Module\Reporting\Web\Controller;
use Icinga\Module\Reporting\Web\Forms\ReportForm;
use Icinga\Module\Reporting\Web\Forms\ScheduleForm;
use Icinga\Module\Reporting\Web\Forms\SendForm;
use ipl\Web\Widget\ModalToggle;
use ipl\Web\Widget\Modal;
use reportingipl\Web\Url;
use reportingipl\Web\Widget\ActionBar;
use reportingipl\Web\Widget\DropdownLink;

class ReportController extends Controller
{
    use Database;

    /** @var Report */
    protected $report;

    public function init()
    {
        $this->report = Report::fromDb($this->params->getRequired('id'));
    }

    public function indexAction()
    {
        $this->setTitle($this->report->getName());

        $this->addControl($this->assembleActions());

        $this->addContent($this->report->toHtml());
    }

    public function editAction()
    {
        $this->setTitle('Edit Report');

        $values = [
            'name'      => $this->report->getName(),
            'timeframe' => $this->report->getTimeframe()->getId(),
        ];

        $reportlet = $this->report->getReportlets()[0];

        $values['reportlet'] = $reportlet->getClass();

        foreach ($reportlet->getConfig() as $name => $value) {
            $values[$name] = $value;
        }

        $form = new ReportForm();
        $form->setId($this->report->getId());
        $form->populate($values);
        $form->handleRequest(ServerRequest::fromGlobals());

        $this->redirectForm($form, 'reporting/reports');

        $this->addContent($form);
    }

    public function sendAction()
    {
        $this->setTitle('Send Report');

        $form = new SendForm();
        $form
            ->setReport($this->report)
            ->handleRequest(ServerRequest::fromGlobals());

        $this->redirectForm($form, "reporting/report?id={$this->report->getId()}");

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

    public function scheduleAction()
    {
        $this->setTitle('Schedule');

        $form = new ScheduleForm();
        $form
            ->setReport($this->report)
            ->handleRequest(ServerRequest::fromGlobals());

        $this->redirectForm($form, "reporting/report?id={$this->report->getId()}");

        $this->addContent(new Modal($form));

        $this->getResponse()->setHeader('X-Icinga-History', 'no', true);
    }

    protected function assembleActions()
    {
        $reportId = $this->report->getId();

        $download = (new DropdownLink('Download'))
            ->addLink('PDF', Url::fromPath('reporting/report/download?type=pdf', ['id' => $reportId]));

        $send = (new DropdownLink('Send', 'forward'))
            ->addLink('PDF', Url::fromPath('reporting/report/send?type=pdf', ['id' => $reportId]));

        if ($this->report->providesCsv()) {
            $download->addLink('CSV', Url::fromPath('reporting/report/download?type=csv', ['id' => $reportId]));
            $send->addLink('CSV', Url::fromPath('reporting/report/send?type=csv', ['id' => $reportId]));
        }

        if ($this->report->providesJson()) {
            $download->addLink('JSON', Url::fromPath('reporting/report/download?type=json', ['id' => $reportId]));
            $send->addLink('JSON', Url::fromPath('reporting/report/send?type=json', ['id' => $reportId]));
        }

        $actions = new ActionBar();

        $actions
            ->addLink('Modify', Url::fromPath('reporting/report/edit', ['id' => $reportId]), 'edit')
            ->add(new ModalToggle('Schedule', Url::fromPath('reporting/report/schedule', ['id' => $reportId]), 'calendar-empty'))
            ->add($download)
            ->addLink('Send', Url::fromPath('reporting/report/send', ['id' => $reportId]), 'forward');

        return $actions;
    }
}