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