xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 9fa6ab694d83803eee096c1fcd2492cb9ffa5e5c)
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 League\Flysystem\FilesystemInterface;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36use Psr\Http\Server\RequestHandlerInterface;
37
38use function addcslashes;
39use function assert;
40use function ob_get_clean;
41use function ob_start;
42use function redirect;
43use function response;
44use function route;
45
46use const PHP_VERSION_ID;
47
48/**
49 * Show all available reports.
50 */
51class ReportGenerate implements RequestHandlerInterface
52{
53    use ViewResponseTrait;
54
55    /**
56     * @var ModuleService
57     */
58    private $module_service;
59
60    /**
61     * ReportEngineController constructor.
62     *
63     * @param ModuleService $module_service
64     */
65    public function __construct(ModuleService $module_service)
66    {
67        $this->module_service = $module_service;
68    }
69
70    /**
71     * A list of available reports.
72     *
73     * @param ServerRequestInterface $request
74     *
75     * @return ResponseInterface
76     */
77    public function handle(ServerRequestInterface $request): ResponseInterface
78    {
79        $tree = $request->getAttribute('tree');
80        assert($tree instanceof Tree);
81
82        $user = $request->getAttribute('user');
83        assert($user instanceof UserInterface);
84
85        $data_filesystem = $request->getAttribute('filesystem.data');
86        assert($data_filesystem instanceof FilesystemInterface);
87
88        $report = $request->getAttribute('report');
89        $module = $this->module_service->findByName($report);
90
91        if (!$module instanceof ModuleReportInterface) {
92            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
93        }
94
95        Auth::checkComponentAccess($module, 'report', $tree, $user);
96
97        $varnames  = $request->getQueryParams()['varnames'] ?? [];
98        $vars      = $request->getQueryParams()['vars'] ?? [];
99        $variables = [];
100
101        foreach ($varnames as $name) {
102            $variables[$name]['id'] = $vars[$name] ?? '';
103        }
104
105        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
106
107        $format      = $request->getQueryParams()['format'] ?? '';
108        $destination = $request->getQueryParams()['destination'] ?? '';
109
110        switch ($format) {
111            default:
112            case 'HTML':
113                ob_start();
114                new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree, $data_filesystem);
115                $html = ob_get_clean();
116
117                $this->layout = 'layouts/report';
118
119                $response = $this->viewResponse('report-page', [
120                    'content' => $html,
121                    'title'   => I18N::translate('Report'),
122                ]);
123
124                if ($destination === 'download') {
125                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
126                }
127
128                return $response;
129
130            case 'PDF':
131                if (PHP_VERSION_ID >= 70400) {
132                    $pr    = 'https://github.com/tecnickcom/TCPDF/pull/137';
133                    $error = 'PDF reports do not currently work on PHP >= 7.4';
134                    $error .= '<br>';
135                    $error .= 'Waiting for <a href="' . $pr . '" class="alert-link">' . $pr . '</a>';
136
137                    return $this->viewResponse('errors/unhandled-exception', [
138                        'error' => $error,
139                        'title' => 'TCPDF error',
140                        'tree' => $tree,
141                    ]);
142                }
143
144                ob_start();
145                new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree, $data_filesystem);
146                $pdf = ob_get_clean();
147
148                $headers = ['Content-Type' => 'application/pdf'];
149
150                if ($destination === 'download') {
151                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
152                }
153
154                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
155        }
156    }
157}
158