xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
11cfe16bdSGreg Roach<?php
21cfe16bdSGreg Roach
31cfe16bdSGreg Roach/**
41cfe16bdSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
61cfe16bdSGreg Roach * This program is free software: you can redistribute it and/or modify
71cfe16bdSGreg Roach * it under the terms of the GNU General Public License as published by
81cfe16bdSGreg Roach * the Free Software Foundation, either version 3 of the License, or
91cfe16bdSGreg Roach * (at your option) any later version.
101cfe16bdSGreg Roach * This program is distributed in the hope that it will be useful,
111cfe16bdSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121cfe16bdSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131cfe16bdSGreg Roach * GNU General Public License for more details.
141cfe16bdSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
161cfe16bdSGreg Roach */
171cfe16bdSGreg Roach
181cfe16bdSGreg Roachdeclare(strict_types=1);
191cfe16bdSGreg Roach
201cfe16bdSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
211cfe16bdSGreg Roach
221cfe16bdSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
231cfe16bdSGreg Roachuse Fisharebest\Webtrees\Auth;
241cfe16bdSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
251cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
261cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N;
271cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
29b6f35a76SGreg Roachuse Fisharebest\Webtrees\Report\HtmlRenderer;
30b6f35a76SGreg Roachuse Fisharebest\Webtrees\Report\PdfRenderer;
3155134edcSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserGenerate;
321cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
331cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree;
341cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface;
351cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
361cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
371cfe16bdSGreg Roach
381cfe16bdSGreg Roachuse function addcslashes;
391cfe16bdSGreg Roachuse function assert;
401cfe16bdSGreg Roachuse function ob_get_clean;
411cfe16bdSGreg Roachuse function ob_start;
421cfe16bdSGreg Roachuse function redirect;
431cfe16bdSGreg Roachuse function response;
441cfe16bdSGreg Roachuse function route;
451cfe16bdSGreg Roach
461cfe16bdSGreg Roach/**
471cfe16bdSGreg Roach * Show all available reports.
481cfe16bdSGreg Roach */
491cfe16bdSGreg Roachclass ReportGenerate implements RequestHandlerInterface
501cfe16bdSGreg Roach{
511cfe16bdSGreg Roach    use ViewResponseTrait;
521cfe16bdSGreg Roach
53*43f2f523SGreg Roach    private ModuleService $module_service;
541cfe16bdSGreg Roach
551cfe16bdSGreg Roach    /**
561cfe16bdSGreg Roach     * ReportEngineController constructor.
571cfe16bdSGreg Roach     *
581cfe16bdSGreg Roach     * @param ModuleService $module_service
591cfe16bdSGreg Roach     */
601cfe16bdSGreg Roach    public function __construct(ModuleService $module_service)
611cfe16bdSGreg Roach    {
621cfe16bdSGreg Roach        $this->module_service = $module_service;
631cfe16bdSGreg Roach    }
641cfe16bdSGreg Roach
651cfe16bdSGreg Roach    /**
661cfe16bdSGreg Roach     * A list of available reports.
671cfe16bdSGreg Roach     *
681cfe16bdSGreg Roach     * @param ServerRequestInterface $request
691cfe16bdSGreg Roach     *
701cfe16bdSGreg Roach     * @return ResponseInterface
711cfe16bdSGreg Roach     */
721cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
731cfe16bdSGreg Roach    {
741cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
7575964c75SGreg Roach        assert($tree instanceof Tree);
761cfe16bdSGreg Roach
771cfe16bdSGreg Roach        $user = $request->getAttribute('user');
7875964c75SGreg Roach        assert($user instanceof UserInterface);
791cfe16bdSGreg Roach
806b9cb339SGreg Roach        $data_filesystem = Registry::filesystem()->data();
81a04bb9a2SGreg Roach
821cfe16bdSGreg Roach        $report = $request->getAttribute('report');
831cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
841cfe16bdSGreg Roach
851cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
861cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
871cfe16bdSGreg Roach        }
881cfe16bdSGreg Roach
89ef483801SGreg Roach        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
901cfe16bdSGreg Roach
911cfe16bdSGreg Roach        $varnames  = $request->getQueryParams()['varnames'] ?? [];
921cfe16bdSGreg Roach        $vars      = $request->getQueryParams()['vars'] ?? [];
931cfe16bdSGreg Roach        $variables = [];
941cfe16bdSGreg Roach
951cfe16bdSGreg Roach        foreach ($varnames as $name) {
961cfe16bdSGreg Roach            $variables[$name]['id'] = $vars[$name] ?? '';
971cfe16bdSGreg Roach        }
981cfe16bdSGreg Roach
991cfe16bdSGreg Roach        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
1001cfe16bdSGreg Roach
1011cfe16bdSGreg Roach        $format      = $request->getQueryParams()['format'] ?? '';
1021cfe16bdSGreg Roach        $destination = $request->getQueryParams()['destination'] ?? '';
1031cfe16bdSGreg Roach
1047f7be5ebSGreg Roach        $user->setPreference('default-report-destination', $destination);
1057f7be5ebSGreg Roach        $user->setPreference('default-report-format', $format);
1067f7be5ebSGreg Roach
1071cfe16bdSGreg Roach        switch ($format) {
1081cfe16bdSGreg Roach            default:
1091cfe16bdSGreg Roach            case 'HTML':
1101cfe16bdSGreg Roach                ob_start();
111b6f35a76SGreg Roach                new ReportParserGenerate($xml_filename, new HtmlRenderer(), $variables, $tree, $data_filesystem);
1121cfe16bdSGreg Roach                $html = ob_get_clean();
1131cfe16bdSGreg Roach
1141cfe16bdSGreg Roach                $this->layout = 'layouts/report';
1151cfe16bdSGreg Roach
1161cfe16bdSGreg Roach                $response = $this->viewResponse('report-page', [
1171cfe16bdSGreg Roach                    'content' => $html,
1181cfe16bdSGreg Roach                    'title'   => I18N::translate('Report'),
1191cfe16bdSGreg Roach                ]);
1201cfe16bdSGreg Roach
1211cfe16bdSGreg Roach                if ($destination === 'download') {
1221cfe16bdSGreg Roach                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
1231cfe16bdSGreg Roach                }
1241cfe16bdSGreg Roach
1251cfe16bdSGreg Roach                return $response;
1261cfe16bdSGreg Roach
1271cfe16bdSGreg Roach            case 'PDF':
1281cfe16bdSGreg Roach                ob_start();
129b6f35a76SGreg Roach                new ReportParserGenerate($xml_filename, new PdfRenderer(), $variables, $tree, $data_filesystem);
1301cfe16bdSGreg Roach                $pdf = ob_get_clean();
1311cfe16bdSGreg Roach
1321cfe16bdSGreg Roach                $headers = ['Content-Type' => 'application/pdf'];
1331cfe16bdSGreg Roach
1341cfe16bdSGreg Roach                if ($destination === 'download') {
1351cfe16bdSGreg Roach                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
1361cfe16bdSGreg Roach                }
1371cfe16bdSGreg Roach
1381cfe16bdSGreg Roach                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
1391cfe16bdSGreg Roach        }
1401cfe16bdSGreg Roach    }
1411cfe16bdSGreg Roach}
142