From 4ada86876033fa171e2896d7e3d3c5645d8062db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:46:47 +0200 Subject: Adding upstream version 0.10.0. Signed-off-by: Daniel Baumann --- application/controllers/ReportController.php | 208 +++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 application/controllers/ReportController.php (limited to 'application/controllers/ReportController.php') diff --git a/application/controllers/ReportController.php b/application/controllers/ReportController.php new file mode 100644 index 0000000..090c759 --- /dev/null +++ b/application/controllers/ReportController.php @@ -0,0 +1,208 @@ +report = Report::fromDb($this->params->getRequired('id')); + } + + public function indexAction() + { + $this->addTitleTab($this->report->getName()); + + $this->addControl($this->assembleActions()); + + Environment::raiseExecutionTime(); + Environment::raiseMemoryLimit(); + + try { + $this->addContent($this->report->toHtml()); + } catch (\Exception $e) { + $this->addContent(Error::show($e)); + } + } + + public function editAction() + { + $this->assertPermission('reporting/reports'); + $this->addTitleTab('Edit Report'); + + $values = [ + 'name' => $this->report->getName(), + // TODO(el): Must cast to string here because ipl/html does not + // support integer return values for attribute callbacks + 'timeframe' => (string) $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->addTitleTab('Send Report'); + + Environment::raiseExecutionTime(); + Environment::raiseMemoryLimit(); + + $form = new SendForm(); + $form + ->setReport($this->report) + ->handleRequest(ServerRequest::fromGlobals()); + + $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); + + $this->addContent($form); + } + + public function scheduleAction() + { + $this->assertPermission('reporting/schedules'); + $this->addTitleTab('Schedule'); + + $form = new ScheduleForm(); + $form + ->setReport($this->report) + ->handleRequest(ServerRequest::fromGlobals()); + + $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); + + $this->addContent($form); + } + + public function downloadAction() + { + $type = $this->params->getRequired('type'); + + Environment::raiseExecutionTime(); + Environment::raiseMemoryLimit(); + + $name = sprintf( + '%s (%s) %s', + $this->report->getName(), + $this->report->getTimeframe()->getName(), + date('Y-m-d H:i') + ); + + switch ($type) { + case 'pdf': + /** @var Hook\PdfexportHook */ + Pdfexport::first()->streamPdfFromHtml($this->report->toPdf(), $name); + exit; + case 'csv': + $response = $this->getResponse(); + $response + ->setHeader('Content-Type', 'text/csv') + ->setHeader('Cache-Control', 'no-store') + ->setHeader( + 'Content-Disposition', + 'attachment; filename=' . $name . '.csv' + ) + ->appendBody($this->report->toCsv()) + ->sendResponse(); + exit; + case 'json': + $response = $this->getResponse(); + $response + ->setHeader('Content-Type', 'application/json') + ->setHeader('Cache-Control', 'no-store') + ->setHeader( + 'Content-Disposition', + 'inline; filename=' . $name . '.json' + ) + ->appendBody($this->report->toJson()) + ->sendResponse(); + exit; + } + } + + protected function assembleActions() + { + $reportId = $this->report->getId(); + + $download = (new CompatDropdown('Download')) + ->addLink( + 'PDF', + Url::fromPath('reporting/report/download?type=pdf', ['id' => $reportId]), + null, + ['target' => '_blank'] + ); + + if ($this->report->providesData()) { + $download->addLink( + 'CSV', + Url::fromPath('reporting/report/download?type=csv', ['id' => $reportId]), + null, + ['target' => '_blank'] + ); + $download->addLink( + 'JSON', + Url::fromPath('reporting/report/download?type=json', ['id' => $reportId]), + null, + ['target' => '_blank'] + ); + } + + $actions = new ActionBar(); + + if ($this->hasPermission('reporting/reports')) { + $actions->addLink( + 'Modify', + Url::fromPath('reporting/report/edit', ['id' => $reportId]), + 'edit' + ); + } + + if ($this->hasPermission('reporting/schedules')) { + $actions->addLink( + 'Schedule', + Url::fromPath('reporting/report/schedule', ['id' => $reportId]), + 'calendar-empty' + ); + } + + $actions + ->add($download) + ->addLink('Send', Url::fromPath('reporting/report/send', ['id' => $reportId]), 'forward'); + + return $actions; + } +} -- cgit v1.2.3