xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 7f7be5ebddc34c2c0789769121d48fa5de6606c5)
11cfe16bdSGreg Roach<?php
21cfe16bdSGreg Roach
31cfe16bdSGreg Roach/**
41cfe16bdSGreg Roach * webtrees: online genealogy
51cfe16bdSGreg Roach * Copyright (C) 2019 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
151cfe16bdSGreg Roach * along with this program. If not, see <http://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;
28b6f35a76SGreg Roachuse Fisharebest\Webtrees\Report\HtmlRenderer;
291cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserGenerate;
30b6f35a76SGreg Roachuse Fisharebest\Webtrees\Report\PdfRenderer;
311cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
321cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree;
33a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface;
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
531cfe16bdSGreg Roach    /**
541cfe16bdSGreg Roach     * @var ModuleService
551cfe16bdSGreg Roach     */
561cfe16bdSGreg Roach    private $module_service;
571cfe16bdSGreg Roach
581cfe16bdSGreg Roach    /**
591cfe16bdSGreg Roach     * ReportEngineController constructor.
601cfe16bdSGreg Roach     *
611cfe16bdSGreg Roach     * @param ModuleService $module_service
621cfe16bdSGreg Roach     */
631cfe16bdSGreg Roach    public function __construct(ModuleService $module_service)
641cfe16bdSGreg Roach    {
651cfe16bdSGreg Roach        $this->module_service = $module_service;
661cfe16bdSGreg Roach    }
671cfe16bdSGreg Roach
681cfe16bdSGreg Roach    /**
691cfe16bdSGreg Roach     * A list of available reports.
701cfe16bdSGreg Roach     *
711cfe16bdSGreg Roach     * @param ServerRequestInterface $request
721cfe16bdSGreg Roach     *
731cfe16bdSGreg Roach     * @return ResponseInterface
741cfe16bdSGreg Roach     */
751cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
761cfe16bdSGreg Roach    {
771cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
7875964c75SGreg Roach        assert($tree instanceof Tree);
791cfe16bdSGreg Roach
801cfe16bdSGreg Roach        $user = $request->getAttribute('user');
8175964c75SGreg Roach        assert($user instanceof UserInterface);
821cfe16bdSGreg Roach
83a04bb9a2SGreg Roach        $data_filesystem = $request->getAttribute('filesystem.data');
84a04bb9a2SGreg Roach        assert($data_filesystem instanceof FilesystemInterface);
85a04bb9a2SGreg Roach
861cfe16bdSGreg Roach        $report = $request->getAttribute('report');
871cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
881cfe16bdSGreg Roach
891cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
901cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
911cfe16bdSGreg Roach        }
921cfe16bdSGreg Roach
931cfe16bdSGreg Roach        Auth::checkComponentAccess($module, 'report', $tree, $user);
941cfe16bdSGreg Roach
951cfe16bdSGreg Roach        $varnames  = $request->getQueryParams()['varnames'] ?? [];
961cfe16bdSGreg Roach        $vars      = $request->getQueryParams()['vars'] ?? [];
971cfe16bdSGreg Roach        $variables = [];
981cfe16bdSGreg Roach
991cfe16bdSGreg Roach        foreach ($varnames as $name) {
1001cfe16bdSGreg Roach            $variables[$name]['id'] = $vars[$name] ?? '';
1011cfe16bdSGreg Roach        }
1021cfe16bdSGreg Roach
1031cfe16bdSGreg Roach        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
1041cfe16bdSGreg Roach
1051cfe16bdSGreg Roach        $format      = $request->getQueryParams()['format'] ?? '';
1061cfe16bdSGreg Roach        $destination = $request->getQueryParams()['destination'] ?? '';
1071cfe16bdSGreg Roach
108*7f7be5ebSGreg Roach        $user->setPreference('default-report-destination', $destination);
109*7f7be5ebSGreg Roach        $user->setPreference('default-report-format', $format);
110*7f7be5ebSGreg Roach
1111cfe16bdSGreg Roach        switch ($format) {
1121cfe16bdSGreg Roach            default:
1131cfe16bdSGreg Roach            case 'HTML':
1141cfe16bdSGreg Roach                ob_start();
115b6f35a76SGreg Roach                new ReportParserGenerate($xml_filename, new HtmlRenderer(), $variables, $tree, $data_filesystem);
1161cfe16bdSGreg Roach                $html = ob_get_clean();
1171cfe16bdSGreg Roach
1181cfe16bdSGreg Roach                $this->layout = 'layouts/report';
1191cfe16bdSGreg Roach
1201cfe16bdSGreg Roach                $response = $this->viewResponse('report-page', [
1211cfe16bdSGreg Roach                    'content' => $html,
1221cfe16bdSGreg Roach                    'title'   => I18N::translate('Report'),
1231cfe16bdSGreg Roach                ]);
1241cfe16bdSGreg Roach
1251cfe16bdSGreg Roach                if ($destination === 'download') {
1261cfe16bdSGreg Roach                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
1271cfe16bdSGreg Roach                }
1281cfe16bdSGreg Roach
1291cfe16bdSGreg Roach                return $response;
1301cfe16bdSGreg Roach
1311cfe16bdSGreg Roach            case 'PDF':
1321cfe16bdSGreg Roach                ob_start();
133b6f35a76SGreg Roach                new ReportParserGenerate($xml_filename, new PdfRenderer(), $variables, $tree, $data_filesystem);
1341cfe16bdSGreg Roach                $pdf = ob_get_clean();
1351cfe16bdSGreg Roach
1361cfe16bdSGreg Roach                $headers = ['Content-Type' => 'application/pdf'];
1371cfe16bdSGreg Roach
1381cfe16bdSGreg Roach                if ($destination === 'download') {
1391cfe16bdSGreg Roach                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
1401cfe16bdSGreg Roach                }
1411cfe16bdSGreg Roach
1421cfe16bdSGreg Roach                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
1431cfe16bdSGreg Roach        }
1441cfe16bdSGreg Roach    }
1451cfe16bdSGreg Roach}
146