xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision c5b48766684db09f7f8372612300a247ec6929e4)
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  = $request->getQueryParams()['varnames'] ?? [];
87        $vars      = $request->getQueryParams()['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
96        $format      = $request->getQueryParams()['format'] ?? '';
97        $destination = $request->getQueryParams()['destination'] ?? '';
98
99        $user->setPreference('default-report-destination', $destination);
100        $user->setPreference('default-report-format', $format);
101
102        switch ($format) {
103            default:
104            case 'HTML':
105                ob_start();
106                new ReportParserGenerate($xml_filename, new HtmlRenderer(), $variables, $tree, $data_filesystem);
107                $html = ob_get_clean();
108
109                $this->layout = 'layouts/report';
110
111                $response = $this->viewResponse('report-page', [
112                    'content' => $html,
113                    'title'   => I18N::translate('Report'),
114                ]);
115
116                if ($destination === 'download') {
117                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
118                }
119
120                return $response;
121
122            case 'PDF':
123                ob_start();
124                new ReportParserGenerate($xml_filename, new PdfRenderer(), $variables, $tree, $data_filesystem);
125                $pdf = ob_get_clean();
126
127                $headers = ['Content-Type' => 'application/pdf'];
128
129                if ($destination === 'download') {
130                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
131                }
132
133                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
134        }
135    }
136}
137