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