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