xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 75964c75fd01f8e12e2a313cc89fc2222413a03f)
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;
281cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportHtml;
291cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserGenerate;
301cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportPdf;
311cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
321cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree;
331cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface;
341cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
351cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
361cfe16bdSGreg Roach
371cfe16bdSGreg Roachuse function addcslashes;
381cfe16bdSGreg Roachuse function assert;
391cfe16bdSGreg Roachuse function ob_get_clean;
401cfe16bdSGreg Roachuse function ob_start;
411cfe16bdSGreg Roachuse function redirect;
421cfe16bdSGreg Roachuse function response;
431cfe16bdSGreg Roachuse function route;
441cfe16bdSGreg Roach
451cfe16bdSGreg Roach/**
461cfe16bdSGreg Roach * Show all available reports.
471cfe16bdSGreg Roach */
481cfe16bdSGreg Roachclass ReportGenerate implements RequestHandlerInterface
491cfe16bdSGreg Roach{
501cfe16bdSGreg Roach    use ViewResponseTrait;
511cfe16bdSGreg Roach
521cfe16bdSGreg Roach    /**
531cfe16bdSGreg Roach     * @var ModuleService
541cfe16bdSGreg Roach     */
551cfe16bdSGreg Roach    private $module_service;
561cfe16bdSGreg Roach
571cfe16bdSGreg Roach    /**
581cfe16bdSGreg Roach     * ReportEngineController constructor.
591cfe16bdSGreg Roach     *
601cfe16bdSGreg Roach     * @param ModuleService $module_service
611cfe16bdSGreg Roach     */
621cfe16bdSGreg Roach    public function __construct(ModuleService $module_service)
631cfe16bdSGreg Roach    {
641cfe16bdSGreg Roach        $this->module_service = $module_service;
651cfe16bdSGreg Roach    }
661cfe16bdSGreg Roach
671cfe16bdSGreg Roach    /**
681cfe16bdSGreg Roach     * A list of available reports.
691cfe16bdSGreg Roach     *
701cfe16bdSGreg Roach     * @param ServerRequestInterface $request
711cfe16bdSGreg Roach     *
721cfe16bdSGreg Roach     * @return ResponseInterface
731cfe16bdSGreg Roach     */
741cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
751cfe16bdSGreg Roach    {
761cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
77*75964c75SGreg Roach        assert($tree instanceof Tree);
781cfe16bdSGreg Roach
791cfe16bdSGreg Roach        $user = $request->getAttribute('user');
80*75964c75SGreg Roach        assert($user instanceof UserInterface);
811cfe16bdSGreg 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
891cfe16bdSGreg Roach        Auth::checkComponentAccess($module, 'report', $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
1041cfe16bdSGreg Roach        switch ($format) {
1051cfe16bdSGreg Roach            default:
1061cfe16bdSGreg Roach            case 'HTML':
1071cfe16bdSGreg Roach                ob_start();
1081cfe16bdSGreg Roach                new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree);
1091cfe16bdSGreg Roach                $html = ob_get_clean();
1101cfe16bdSGreg Roach
1111cfe16bdSGreg Roach                $this->layout = 'layouts/report';
1121cfe16bdSGreg Roach
1131cfe16bdSGreg Roach                $response = $this->viewResponse('report-page', [
1141cfe16bdSGreg Roach                    'content' => $html,
1151cfe16bdSGreg Roach                    'title'   => I18N::translate('Report'),
1161cfe16bdSGreg Roach                ]);
1171cfe16bdSGreg Roach
1181cfe16bdSGreg Roach                if ($destination === 'download') {
1191cfe16bdSGreg Roach                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
1201cfe16bdSGreg Roach                }
1211cfe16bdSGreg Roach
1221cfe16bdSGreg Roach                return $response;
1231cfe16bdSGreg Roach
1241cfe16bdSGreg Roach            case 'PDF':
1251cfe16bdSGreg Roach                ob_start();
1261cfe16bdSGreg Roach                new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree);
1271cfe16bdSGreg Roach                $pdf = ob_get_clean();
1281cfe16bdSGreg Roach
1291cfe16bdSGreg Roach                $headers = ['Content-Type' => 'application/pdf'];
1301cfe16bdSGreg Roach
1311cfe16bdSGreg Roach                if ($destination === 'download') {
1321cfe16bdSGreg Roach                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
1331cfe16bdSGreg Roach                }
1341cfe16bdSGreg Roach
1351cfe16bdSGreg Roach                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
1361cfe16bdSGreg Roach        }
1371cfe16bdSGreg Roach    }
1381cfe16bdSGreg Roach}
139