|
| 1 | +<?php |
| 2 | +// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 |
| 3 | + |
| 4 | +namespace Icinga\Module\Reporting\Controllers; |
| 5 | + |
| 6 | +use GuzzleHttp\Psr7\ServerRequest; |
| 7 | +use Icinga\Application\Hook; |
| 8 | +use Icinga\Module\Reporting\Database; |
| 9 | +use Icinga\Module\Reporting\Report; |
| 10 | +use Icinga\Module\Reporting\Web\Controller; |
| 11 | +use Icinga\Module\Reporting\Web\Forms\ReportForm; |
| 12 | +use Icinga\Module\Reporting\Web\Forms\ScheduleForm; |
| 13 | +use Icinga\Module\Reporting\Web\Forms\SendForm; |
| 14 | +use Icinga\Web\StyleSheet; |
| 15 | +use ipl\Html\Html; |
| 16 | +use ipl\Html\HtmlString; |
| 17 | +use reportingipl\Web\Url; |
| 18 | +use reportingipl\Web\Widget\ActionBar; |
| 19 | +use reportingipl\Web\Widget\DropdownLink; |
| 20 | + |
| 21 | +class ReportController extends Controller |
| 22 | +{ |
| 23 | + use Database; |
| 24 | + |
| 25 | + /** @var Report */ |
| 26 | + protected $report; |
| 27 | + |
| 28 | + public function init() |
| 29 | + { |
| 30 | + $this->report = Report::fromDb($this->params->getRequired('id')); |
| 31 | + } |
| 32 | + |
| 33 | + public function indexAction() |
| 34 | + { |
| 35 | + $this->setTitle($this->report->getName()); |
| 36 | + |
| 37 | + $this->addControl($this->assembleActions()); |
| 38 | + |
| 39 | + $this->addContent($this->report->toHtml()); |
| 40 | + } |
| 41 | + |
| 42 | + public function editAction() |
| 43 | + { |
| 44 | + $this->setTitle('Edit Report'); |
| 45 | + |
| 46 | + $values = [ |
| 47 | + 'name' => $this->report->getName(), |
| 48 | + // TODO(el): Must cast to string here because ipl/html does not support integer return values for attribute callbacks |
| 49 | + 'timeframe' => (string) $this->report->getTimeframe()->getId(), |
| 50 | + ]; |
| 51 | + |
| 52 | + $reportlet = $this->report->getReportlets()[0]; |
| 53 | + |
| 54 | + $values['reportlet'] = $reportlet->getClass(); |
| 55 | + |
| 56 | + foreach ($reportlet->getConfig() as $name => $value) { |
| 57 | + $values[$name] = $value; |
| 58 | + } |
| 59 | + |
| 60 | + $form = new ReportForm(); |
| 61 | + $form->setId($this->report->getId()); |
| 62 | + $form->populate($values); |
| 63 | + $form->handleRequest(ServerRequest::fromGlobals()); |
| 64 | + |
| 65 | + $this->redirectForm($form, 'reporting/reports'); |
| 66 | + |
| 67 | + $this->addContent($form); |
| 68 | + } |
| 69 | + |
| 70 | + public function sendAction() |
| 71 | + { |
| 72 | + $this->setTitle('Send Report'); |
| 73 | + |
| 74 | + $form = new SendForm(); |
| 75 | + $form |
| 76 | + ->setReport($this->report) |
| 77 | + ->handleRequest(ServerRequest::fromGlobals()); |
| 78 | + |
| 79 | + $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); |
| 80 | + |
| 81 | + $this->addContent($form); |
| 82 | + } |
| 83 | + |
| 84 | + public function scheduleAction() |
| 85 | + { |
| 86 | + $this->setTitle('Schedule'); |
| 87 | + |
| 88 | + $form = new ScheduleForm(); |
| 89 | + $form |
| 90 | + ->setReport($this->report) |
| 91 | + ->handleRequest(ServerRequest::fromGlobals()); |
| 92 | + |
| 93 | + $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); |
| 94 | + |
| 95 | + $this->addContent($form); |
| 96 | + } |
| 97 | + |
| 98 | + public function downloadAction() |
| 99 | + { |
| 100 | + $type = $this->params->getRequired('type'); |
| 101 | + |
| 102 | + $name = sprintf( |
| 103 | + '%s (%s) %s', |
| 104 | + $this->report->getName(), |
| 105 | + $this->report->getTimeframe()->getName(), |
| 106 | + date('Y-m-d H:i') |
| 107 | + ); |
| 108 | + |
| 109 | + switch ($type) { |
| 110 | + case 'pdf': |
| 111 | + $pdfexport = null; |
| 112 | + |
| 113 | + if (Hook::has('Pdfexport')) { |
| 114 | + $pdfexport = Hook::first('Pdfexport'); |
| 115 | + |
| 116 | + if (! $pdfexport->isSupported()) { |
| 117 | + throw new \Exception("Can't export"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (! $pdfexport) { |
| 122 | + throw new \Exception("Can't export"); |
| 123 | + } |
| 124 | + |
| 125 | + $html = Html::tag( |
| 126 | + 'html', |
| 127 | + null, |
| 128 | + [ |
| 129 | + Html::tag( |
| 130 | + 'head', |
| 131 | + null, |
| 132 | + Html::tag( |
| 133 | + 'style', |
| 134 | + null, |
| 135 | + new HtmlString(StyleSheet::forPdf()) |
| 136 | + ) |
| 137 | + ), |
| 138 | + Html::tag( |
| 139 | + 'body', |
| 140 | + null, |
| 141 | + Html::tag( |
| 142 | + 'div', |
| 143 | + ['class' => 'icinga-module module-reporting'], |
| 144 | + new HtmlString($this->report->toHtml()) |
| 145 | + ) |
| 146 | + ) |
| 147 | + ] |
| 148 | + ); |
| 149 | + |
| 150 | + /** @var Hook\PdfexportHook */ |
| 151 | + $pdfexport->streamPdfFromHtml((string) $html, $name); |
| 152 | + exit; |
| 153 | + case 'csv': |
| 154 | + $response = $this->getResponse(); |
| 155 | + $response |
| 156 | + ->setHeader('Content-Type', 'text/csv') |
| 157 | + ->setHeader('Cache-Control', 'no-store') |
| 158 | + ->setHeader( |
| 159 | + 'Content-Disposition', |
| 160 | + 'attachment; filename=' . $name . '.csv' |
| 161 | + ) |
| 162 | + ->appendBody($this->report->toCsv()) |
| 163 | + ->sendResponse(); |
| 164 | + exit; |
| 165 | + case 'json': |
| 166 | + $response = $this->getResponse(); |
| 167 | + $response |
| 168 | + ->setHeader('Content-Type', 'application/json') |
| 169 | + ->setHeader('Cache-Control', 'no-store') |
| 170 | + ->setHeader( |
| 171 | + 'Content-Disposition', |
| 172 | + 'inline; filename=' . $name . '.json' |
| 173 | + ) |
| 174 | + ->appendBody($this->report->toJson()) |
| 175 | + ->sendResponse(); |
| 176 | + exit; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + protected function assembleActions() |
| 181 | + { |
| 182 | + $reportId = $this->report->getId(); |
| 183 | + |
| 184 | + $download = (new DropdownLink('Download')) |
| 185 | + ->addLink('PDF', Url::fromPath('reporting/report/download?type=pdf', ['id' => $reportId])); |
| 186 | + |
| 187 | + if ($this->report->providesData()) { |
| 188 | + $download->addLink('CSV', Url::fromPath('reporting/report/download?type=csv', ['id' => $reportId])); |
| 189 | + $download->addLink('JSON', Url::fromPath('reporting/report/download?type=json', ['id' => $reportId])); |
| 190 | + } |
| 191 | + |
| 192 | + $actions = new ActionBar(); |
| 193 | + |
| 194 | + $actions |
| 195 | + ->addLink('Modify', Url::fromPath('reporting/report/edit', ['id' => $reportId]), 'edit') |
| 196 | + ->addLink('Schedule', Url::fromPath('reporting/report/schedule', ['id' => $reportId]), 'calendar-empty') |
| 197 | + ->add($download) |
| 198 | + ->addLink('Send', Url::fromPath('reporting/report/send', ['id' => $reportId]), 'forward'); |
| 199 | + |
| 200 | + return $actions; |
| 201 | + } |
| 202 | +} |
0 commit comments