xref: /webtrees/app/Http/RequestHandlers/ReportGenerate.php (revision 14981035a86b1c0b3df76e6eccff5bd50d0359df)
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
45*14981035SGreg Roachuse const PHP_VERSION_ID;
46*14981035SGreg Roach
471cfe16bdSGreg Roach/**
481cfe16bdSGreg Roach * Show all available reports.
491cfe16bdSGreg Roach */
501cfe16bdSGreg Roachclass ReportGenerate implements RequestHandlerInterface
511cfe16bdSGreg Roach{
521cfe16bdSGreg Roach    use ViewResponseTrait;
531cfe16bdSGreg Roach
541cfe16bdSGreg Roach    /**
551cfe16bdSGreg Roach     * @var ModuleService
561cfe16bdSGreg Roach     */
571cfe16bdSGreg Roach    private $module_service;
581cfe16bdSGreg Roach
591cfe16bdSGreg Roach    /**
601cfe16bdSGreg Roach     * ReportEngineController constructor.
611cfe16bdSGreg Roach     *
621cfe16bdSGreg Roach     * @param ModuleService $module_service
631cfe16bdSGreg Roach     */
641cfe16bdSGreg Roach    public function __construct(ModuleService $module_service)
651cfe16bdSGreg Roach    {
661cfe16bdSGreg Roach        $this->module_service = $module_service;
671cfe16bdSGreg Roach    }
681cfe16bdSGreg Roach
691cfe16bdSGreg Roach    /**
701cfe16bdSGreg Roach     * A list of available reports.
711cfe16bdSGreg Roach     *
721cfe16bdSGreg Roach     * @param ServerRequestInterface $request
731cfe16bdSGreg Roach     *
741cfe16bdSGreg Roach     * @return ResponseInterface
751cfe16bdSGreg Roach     */
761cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
771cfe16bdSGreg Roach    {
781cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
7975964c75SGreg Roach        assert($tree instanceof Tree);
801cfe16bdSGreg Roach
811cfe16bdSGreg Roach        $user = $request->getAttribute('user');
8275964c75SGreg Roach        assert($user instanceof UserInterface);
831cfe16bdSGreg Roach
841cfe16bdSGreg Roach        $report = $request->getAttribute('report');
851cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
861cfe16bdSGreg Roach
871cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
881cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
891cfe16bdSGreg Roach        }
901cfe16bdSGreg Roach
911cfe16bdSGreg Roach        Auth::checkComponentAccess($module, 'report', $tree, $user);
921cfe16bdSGreg Roach
931cfe16bdSGreg Roach        $varnames  = $request->getQueryParams()['varnames'] ?? [];
941cfe16bdSGreg Roach        $vars      = $request->getQueryParams()['vars'] ?? [];
951cfe16bdSGreg Roach        $variables = [];
961cfe16bdSGreg Roach
971cfe16bdSGreg Roach        foreach ($varnames as $name) {
981cfe16bdSGreg Roach            $variables[$name]['id'] = $vars[$name] ?? '';
991cfe16bdSGreg Roach        }
1001cfe16bdSGreg Roach
1011cfe16bdSGreg Roach        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
1021cfe16bdSGreg Roach
1031cfe16bdSGreg Roach        $format      = $request->getQueryParams()['format'] ?? '';
1041cfe16bdSGreg Roach        $destination = $request->getQueryParams()['destination'] ?? '';
1051cfe16bdSGreg Roach
1061cfe16bdSGreg Roach        switch ($format) {
1071cfe16bdSGreg Roach            default:
1081cfe16bdSGreg Roach            case 'HTML':
1091cfe16bdSGreg Roach                ob_start();
1101cfe16bdSGreg Roach                new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree);
1111cfe16bdSGreg Roach                $html = ob_get_clean();
1121cfe16bdSGreg Roach
1131cfe16bdSGreg Roach                $this->layout = 'layouts/report';
1141cfe16bdSGreg Roach
1151cfe16bdSGreg Roach                $response = $this->viewResponse('report-page', [
1161cfe16bdSGreg Roach                    'content' => $html,
1171cfe16bdSGreg Roach                    'title'   => I18N::translate('Report'),
1181cfe16bdSGreg Roach                ]);
1191cfe16bdSGreg Roach
1201cfe16bdSGreg Roach                if ($destination === 'download') {
1211cfe16bdSGreg Roach                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
1221cfe16bdSGreg Roach                }
1231cfe16bdSGreg Roach
1241cfe16bdSGreg Roach                return $response;
1251cfe16bdSGreg Roach
1261cfe16bdSGreg Roach            case 'PDF':
127*14981035SGreg Roach                if (PHP_VERSION_ID >= 70400) {
128*14981035SGreg Roach                    $pr    = 'https://github.com/tecnickcom/TCPDF/pull/137';
129*14981035SGreg Roach                    $error = 'PDF reports do not currently work on PHP >= 7.4';
130*14981035SGreg Roach                    $error .= '<br>';
131*14981035SGreg Roach                    $error .= 'Waiting for <a href="' . $pr . '" class="alert-link">' . $pr . '</a>';
132*14981035SGreg Roach
133*14981035SGreg Roach                    return $this->viewResponse('errors/unhandled-exception', [
134*14981035SGreg Roach                        'error' => $error,
135*14981035SGreg Roach                        'title' => 'TCPDF error',
136*14981035SGreg Roach                        'tree' => $tree,
137*14981035SGreg Roach                    ]);
138*14981035SGreg Roach                }
139*14981035SGreg Roach
1401cfe16bdSGreg Roach                ob_start();
1411cfe16bdSGreg Roach                new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree);
1421cfe16bdSGreg Roach                $pdf = ob_get_clean();
1431cfe16bdSGreg Roach
1441cfe16bdSGreg Roach                $headers = ['Content-Type' => 'application/pdf'];
1451cfe16bdSGreg Roach
1461cfe16bdSGreg Roach                if ($destination === 'download') {
1471cfe16bdSGreg Roach                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
1481cfe16bdSGreg Roach                }
1491cfe16bdSGreg Roach
1501cfe16bdSGreg Roach                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
1511cfe16bdSGreg Roach        }
1521cfe16bdSGreg Roach    }
1531cfe16bdSGreg Roach}
154