xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 00e28091d5dbdf816443155193c9fd33a0b3dd34)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Module\ModuleReportInterface;
28use Fisharebest\Webtrees\Report\ReportHtml;
29use Fisharebest\Webtrees\Report\ReportParserGenerate;
30use Fisharebest\Webtrees\Report\ReportPdf;
31use Fisharebest\Webtrees\Services\ModuleService;
32use Fisharebest\Webtrees\Tree;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function addcslashes;
38use function assert;
39use function ob_get_clean;
40use function ob_start;
41use function redirect;
42use function response;
43use function route;
44
45use const PHP_VERSION_ID;
46
47/**
48 * Show all available reports.
49 */
50class ReportGenerate implements RequestHandlerInterface
51{
52    use ViewResponseTrait;
53
54    /**
55     * @var ModuleService
56     */
57    private $module_service;
58
59    /**
60     * ReportEngineController constructor.
61     *
62     * @param ModuleService $module_service
63     */
64    public function __construct(ModuleService $module_service)
65    {
66        $this->module_service = $module_service;
67    }
68
69    /**
70     * A list of available reports.
71     *
72     * @param ServerRequestInterface $request
73     *
74     * @return ResponseInterface
75     */
76    public function handle(ServerRequestInterface $request): ResponseInterface
77    {
78        $tree = $request->getAttribute('tree');
79        assert($tree instanceof Tree);
80
81        $user = $request->getAttribute('user');
82        assert($user instanceof UserInterface);
83
84        $report = $request->getAttribute('report');
85        $module = $this->module_service->findByName($report);
86
87        if (!$module instanceof ModuleReportInterface) {
88            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
89        }
90
91        Auth::checkComponentAccess($module, 'report', $tree, $user);
92
93        $varnames  = $request->getQueryParams()['varnames'] ?? [];
94        $vars      = $request->getQueryParams()['vars'] ?? [];
95        $variables = [];
96
97        foreach ($varnames as $name) {
98            $variables[$name]['id'] = $vars[$name] ?? '';
99        }
100
101        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
102
103        $format      = $request->getQueryParams()['format'] ?? '';
104        $destination = $request->getQueryParams()['destination'] ?? '';
105
106        switch ($format) {
107            default:
108            case 'HTML':
109                ob_start();
110                new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree);
111                $html = ob_get_clean();
112
113                $this->layout = 'layouts/report';
114
115                $response = $this->viewResponse('report-page', [
116                    'content' => $html,
117                    'title'   => I18N::translate('Report'),
118                ]);
119
120                if ($destination === 'download') {
121                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
122                }
123
124                return $response;
125
126            case 'PDF':
127                if (PHP_VERSION_ID >= 70400) {
128                    $pr    = 'https://github.com/tecnickcom/TCPDF/pull/137';
129                    $error = 'PDF reports do not currently work on PHP >= 7.4';
130                    $error .= '<br>';
131                    $error .= 'Waiting for <a href="' . $pr . '" class="alert-link">' . $pr . '</a>';
132
133                    return $this->viewResponse('errors/unhandled-exception', [
134                        'error' => $error,
135                        'title' => 'TCPDF error',
136                        'tree' => $tree,
137                    ]);
138                }
139
140                ob_start();
141                new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree);
142                $pdf = ob_get_clean();
143
144                $headers = ['Content-Type' => 'application/pdf'];
145
146                if ($destination === 'download') {
147                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
148                }
149
150                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
151        }
152    }
153}
154