xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 <https://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\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Module\ModuleReportInterface;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Report\HtmlRenderer;
29use Fisharebest\Webtrees\Report\PdfRenderer;
30use Fisharebest\Webtrees\Report\ReportParserGenerate;
31use Fisharebest\Webtrees\Services\ModuleService;
32use Fisharebest\Webtrees\Validator;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function addcslashes;
38use function ob_get_clean;
39use function ob_start;
40use function redirect;
41use function response;
42use function route;
43
44/**
45 * Show all available reports.
46 */
47class ReportGenerate implements RequestHandlerInterface
48{
49    use ViewResponseTrait;
50
51    private ModuleService $module_service;
52
53    /**
54     * ReportEngineController constructor.
55     *
56     * @param ModuleService $module_service
57     */
58    public function __construct(ModuleService $module_service)
59    {
60        $this->module_service = $module_service;
61    }
62
63    /**
64     * A list of available reports.
65     *
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $tree = Validator::attributes($request)->tree();
73        $user = Validator::attributes($request)->user();
74
75        $data_filesystem = Registry::filesystem()->data();
76
77        $report = Validator::attributes($request)->string('report');
78        $module = $this->module_service->findByName($report);
79
80        if (!$module instanceof ModuleReportInterface) {
81            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
82        }
83
84        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
85
86        $varnames  = Validator::queryParams($request)->array('varnames');
87        $vars      = Validator::queryParams($request)->array('vars');
88        $variables = [];
89
90        foreach ($varnames as $name) {
91            $variables[$name]['id'] = $vars[$name] ?? '';
92        }
93
94        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
95        $format       = Validator::queryParams($request)->string('format');
96        $destination  = Validator::queryParams($request)->string('destination');
97
98        $user->setPreference('default-report-destination', $destination);
99        $user->setPreference('default-report-format', $format);
100
101        switch ($format) {
102            default:
103            case 'HTML':
104                ob_start();
105                new ReportParserGenerate($xml_filename, new HtmlRenderer(), $variables, $tree, $data_filesystem);
106                $html = ob_get_clean();
107
108                $this->layout = 'layouts/report';
109
110                $response = $this->viewResponse('report-page', [
111                    'content' => $html,
112                    'title'   => I18N::translate('Report'),
113                ]);
114
115                if ($destination === 'download') {
116                    $response = $response->withHeader('content-disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
117                }
118
119                return $response;
120
121            case 'PDF':
122                ob_start();
123                new ReportParserGenerate($xml_filename, new PdfRenderer(), $variables, $tree, $data_filesystem);
124                $pdf = ob_get_clean();
125
126                $headers = ['content-type' => 'application/pdf'];
127
128                if ($destination === 'download') {
129                    $headers['content-disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
130                }
131
132                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
133        }
134    }
135}
136