11cfe16bdSGreg Roach<?php 21cfe16bdSGreg Roach 31cfe16bdSGreg Roach/** 41cfe16bdSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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\Http\ViewResponseTrait; 251cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N; 261cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface; 27b6f35a76SGreg Roachuse Fisharebest\Webtrees\Report\HtmlRenderer; 28b6f35a76SGreg Roachuse Fisharebest\Webtrees\Report\PdfRenderer; 2955134edcSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserGenerate; 301cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 321cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface; 331cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 341cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 351cfe16bdSGreg Roach 361cfe16bdSGreg Roachuse function addcslashes; 371cfe16bdSGreg Roachuse function ob_get_clean; 381cfe16bdSGreg Roachuse function ob_start; 391cfe16bdSGreg Roachuse function redirect; 401cfe16bdSGreg Roachuse function response; 411cfe16bdSGreg Roachuse function route; 421cfe16bdSGreg Roach 431cfe16bdSGreg Roach/** 441cfe16bdSGreg Roach * Show all available reports. 451cfe16bdSGreg Roach */ 461cfe16bdSGreg Roachclass ReportGenerate implements RequestHandlerInterface 471cfe16bdSGreg Roach{ 481cfe16bdSGreg Roach use ViewResponseTrait; 491cfe16bdSGreg Roach 5043f2f523SGreg Roach private ModuleService $module_service; 511cfe16bdSGreg Roach 521cfe16bdSGreg Roach /** 531cfe16bdSGreg Roach * @param ModuleService $module_service 541cfe16bdSGreg Roach */ 551cfe16bdSGreg Roach public function __construct(ModuleService $module_service) 561cfe16bdSGreg Roach { 571cfe16bdSGreg Roach $this->module_service = $module_service; 581cfe16bdSGreg Roach } 591cfe16bdSGreg Roach 601cfe16bdSGreg Roach /** 611cfe16bdSGreg Roach * A list of available reports. 621cfe16bdSGreg Roach * 631cfe16bdSGreg Roach * @param ServerRequestInterface $request 641cfe16bdSGreg Roach * 651cfe16bdSGreg Roach * @return ResponseInterface 661cfe16bdSGreg Roach */ 671cfe16bdSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 681cfe16bdSGreg Roach { 69b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 70b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 71b55cbc6bSGreg Roach $report = Validator::attributes($request)->string('report'); 721cfe16bdSGreg Roach $module = $this->module_service->findByName($report); 731cfe16bdSGreg Roach 741cfe16bdSGreg Roach if (!$module instanceof ModuleReportInterface) { 751cfe16bdSGreg Roach return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); 761cfe16bdSGreg Roach } 771cfe16bdSGreg Roach 78ef483801SGreg Roach Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user); 791cfe16bdSGreg Roach 80748dbe15SGreg Roach $varnames = Validator::queryParams($request)->array('varnames'); 81748dbe15SGreg Roach $vars = Validator::queryParams($request)->array('vars'); 821cfe16bdSGreg Roach $variables = []; 831cfe16bdSGreg Roach 841cfe16bdSGreg Roach foreach ($varnames as $name) { 851cfe16bdSGreg Roach $variables[$name]['id'] = $vars[$name] ?? ''; 861cfe16bdSGreg Roach } 871cfe16bdSGreg Roach 881cfe16bdSGreg Roach $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); 89748dbe15SGreg Roach $format = Validator::queryParams($request)->string('format'); 90748dbe15SGreg Roach $destination = Validator::queryParams($request)->string('destination'); 911cfe16bdSGreg Roach 927f7be5ebSGreg Roach $user->setPreference('default-report-destination', $destination); 937f7be5ebSGreg Roach $user->setPreference('default-report-format', $format); 947f7be5ebSGreg Roach 951cfe16bdSGreg Roach switch ($format) { 961cfe16bdSGreg Roach default: 971cfe16bdSGreg Roach case 'HTML': 981cfe16bdSGreg Roach ob_start(); 999458f20aSGreg Roach new ReportParserGenerate($xml_filename, new HtmlRenderer(), $variables, $tree); 1001cfe16bdSGreg Roach $html = ob_get_clean(); 1011cfe16bdSGreg Roach 1021cfe16bdSGreg Roach $this->layout = 'layouts/report'; 1031cfe16bdSGreg Roach 1041cfe16bdSGreg Roach $response = $this->viewResponse('report-page', [ 1051cfe16bdSGreg Roach 'content' => $html, 1061cfe16bdSGreg Roach 'title' => I18N::translate('Report'), 1071cfe16bdSGreg Roach ]); 1081cfe16bdSGreg Roach 1091cfe16bdSGreg Roach if ($destination === 'download') { 1106172e7f6SGreg Roach $response = $response->withHeader('content-disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"'); 1111cfe16bdSGreg Roach } 1121cfe16bdSGreg Roach 1131cfe16bdSGreg Roach return $response; 1141cfe16bdSGreg Roach 1151cfe16bdSGreg Roach case 'PDF': 1161cfe16bdSGreg Roach ob_start(); 1179458f20aSGreg Roach new ReportParserGenerate($xml_filename, new PdfRenderer(), $variables, $tree); 1181cfe16bdSGreg Roach $pdf = ob_get_clean(); 1191cfe16bdSGreg Roach 1206172e7f6SGreg Roach $headers = ['content-type' => 'application/pdf']; 1211cfe16bdSGreg Roach 1221cfe16bdSGreg Roach if ($destination === 'download') { 1236172e7f6SGreg Roach $headers['content-disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"'; 1241cfe16bdSGreg Roach } 1251cfe16bdSGreg Roach 1261cfe16bdSGreg Roach return response($pdf, StatusCodeInterface::STATUS_OK, $headers); 1271cfe16bdSGreg Roach } 1281cfe16bdSGreg Roach } 1291cfe16bdSGreg Roach} 130