| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 95.65% covered (success) | 95.65% | 22 / 23 | 50.00% covered (danger) | 50.00% | 1 / 2 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 1
ReportsController | 95.65% covered (success) | 95.65% | 22 / 23 | 50.00% covered (danger) | 50.00% | 1 / 2 | 5 | 0.00% covered (danger) | 0.00% | 0 / 1
index | 100.00% covered (success) | 100.00% | 2 / 2 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |
export | 95.24% covered (success) | 95.24% | 20 / 21 | 0.00% covered (danger) | 0.00% | 0 / 1 | 4 |

1 | <?php
2 |
3 | namespace App\Controllers;
4 |
5 | use CodeIgniter\Exceptions\PageNotFoundException;
6 | use CodeIgniter\HTTP\ResponseInterface;
7 | use Dompdf\Dompdf;
8 | use Dompdf\Options;
9 |
10 | class ReportsController extends BaseController
11 | {
12 | public function index(): string
13 | {
14 | $data = service('reports')->getIndexData($this->request->getGet(), auth()->user());
15 |
16 | return view('reports/index', $data);
17 | }
18 |
19 | public function export(string $format): ResponseInterface
20 | {
21 | if (! in_array($format, ['csv', 'pdf'], true)) {
22 | throw PageNotFoundException::forPageNotFound();
23 | }
24 |
25 | $export = service('reports')->getExportData($this->request->getGet(), auth()->user());
26 |
27 | if ($format === 'csv') {
28 | return $this->response
29 | ->setHeader('Content-Type', 'text/csv; charset=UTF-8')
30 | ->setHeader('Content-Disposition', 'attachment; filename="' . $export['file_basename'] . '.csv"')
31 | ->setBody(service('reports')->buildCsv($export));
32 | }
33 |
34 | $options = new Options([
35 | 'defaultFont' => 'DejaVu Sans',
36 | 'isRemoteEnabled' => false,
37 | 'isHtml5ParserEnabled' => true,
38 | ]);
39 | $dompdf = new Dompdf($options);
40 | $dompdf->setPaper('A4', count($export['report']['columns'] ?? []) > 4 ? 'landscape' : 'portrait');
41 | $dompdf->loadHtml(view('reports/pdf', $export), 'UTF-8');
42 | $dompdf->render();
43 |
44 | return $this->response
45 | ->setHeader('Content-Type', 'application/pdf')
46 | ->setHeader('Content-Disposition', 'attachment; filename="' . $export['file_basename'] . '.pdf"')
47 | ->setBody($dompdf->output());
48 | }
49 | }