1*1cfe16bdSGreg Roach<?php 2*1cfe16bdSGreg Roach 3*1cfe16bdSGreg Roach/** 4*1cfe16bdSGreg Roach * webtrees: online genealogy 5*1cfe16bdSGreg Roach * Copyright (C) 2019 webtrees development team 6*1cfe16bdSGreg Roach * This program is free software: you can redistribute it and/or modify 7*1cfe16bdSGreg Roach * it under the terms of the GNU General Public License as published by 8*1cfe16bdSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*1cfe16bdSGreg Roach * (at your option) any later version. 10*1cfe16bdSGreg Roach * This program is distributed in the hope that it will be useful, 11*1cfe16bdSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*1cfe16bdSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*1cfe16bdSGreg Roach * GNU General Public License for more details. 14*1cfe16bdSGreg Roach * You should have received a copy of the GNU General Public License 15*1cfe16bdSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*1cfe16bdSGreg Roach */ 17*1cfe16bdSGreg Roach 18*1cfe16bdSGreg Roachdeclare(strict_types=1); 19*1cfe16bdSGreg Roach 20*1cfe16bdSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21*1cfe16bdSGreg Roach 22*1cfe16bdSGreg Roachuse Fig\Http\Message\StatusCodeInterface; 23*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Auth; 24*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 25*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 26*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N; 27*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface; 28*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportHtml; 29*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserGenerate; 30*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportPdf; 31*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 32*1cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree; 33*1cfe16bdSGreg Roachuse InvalidArgumentException; 34*1cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface; 35*1cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 36*1cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 37*1cfe16bdSGreg Roach 38*1cfe16bdSGreg Roachuse function addcslashes; 39*1cfe16bdSGreg Roachuse function assert; 40*1cfe16bdSGreg Roachuse function ob_get_clean; 41*1cfe16bdSGreg Roachuse function ob_start; 42*1cfe16bdSGreg Roachuse function redirect; 43*1cfe16bdSGreg Roachuse function response; 44*1cfe16bdSGreg Roachuse function route; 45*1cfe16bdSGreg Roach 46*1cfe16bdSGreg Roach/** 47*1cfe16bdSGreg Roach * Show all available reports. 48*1cfe16bdSGreg Roach */ 49*1cfe16bdSGreg Roachclass ReportGenerate implements RequestHandlerInterface 50*1cfe16bdSGreg Roach{ 51*1cfe16bdSGreg Roach use ViewResponseTrait; 52*1cfe16bdSGreg Roach 53*1cfe16bdSGreg Roach /** 54*1cfe16bdSGreg Roach * @var ModuleService 55*1cfe16bdSGreg Roach */ 56*1cfe16bdSGreg Roach private $module_service; 57*1cfe16bdSGreg Roach 58*1cfe16bdSGreg Roach /** 59*1cfe16bdSGreg Roach * ReportEngineController constructor. 60*1cfe16bdSGreg Roach * 61*1cfe16bdSGreg Roach * @param ModuleService $module_service 62*1cfe16bdSGreg Roach */ 63*1cfe16bdSGreg Roach public function __construct(ModuleService $module_service) 64*1cfe16bdSGreg Roach { 65*1cfe16bdSGreg Roach $this->module_service = $module_service; 66*1cfe16bdSGreg Roach } 67*1cfe16bdSGreg Roach 68*1cfe16bdSGreg Roach /** 69*1cfe16bdSGreg Roach * A list of available reports. 70*1cfe16bdSGreg Roach * 71*1cfe16bdSGreg Roach * @param ServerRequestInterface $request 72*1cfe16bdSGreg Roach * 73*1cfe16bdSGreg Roach * @return ResponseInterface 74*1cfe16bdSGreg Roach */ 75*1cfe16bdSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 76*1cfe16bdSGreg Roach { 77*1cfe16bdSGreg Roach $tree = $request->getAttribute('tree'); 78*1cfe16bdSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 79*1cfe16bdSGreg Roach 80*1cfe16bdSGreg Roach $user = $request->getAttribute('user'); 81*1cfe16bdSGreg Roach assert($user instanceof UserInterface, new InvalidArgumentException()); 82*1cfe16bdSGreg Roach 83*1cfe16bdSGreg Roach $report = $request->getAttribute('report'); 84*1cfe16bdSGreg Roach $module = $this->module_service->findByName($report); 85*1cfe16bdSGreg Roach 86*1cfe16bdSGreg Roach if (!$module instanceof ModuleReportInterface) { 87*1cfe16bdSGreg Roach return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); 88*1cfe16bdSGreg Roach } 89*1cfe16bdSGreg Roach 90*1cfe16bdSGreg Roach Auth::checkComponentAccess($module, 'report', $tree, $user); 91*1cfe16bdSGreg Roach 92*1cfe16bdSGreg Roach $varnames = $request->getQueryParams()['varnames'] ?? []; 93*1cfe16bdSGreg Roach $vars = $request->getQueryParams()['vars'] ?? []; 94*1cfe16bdSGreg Roach $variables = []; 95*1cfe16bdSGreg Roach 96*1cfe16bdSGreg Roach foreach ($varnames as $name) { 97*1cfe16bdSGreg Roach $variables[$name]['id'] = $vars[$name] ?? ''; 98*1cfe16bdSGreg Roach } 99*1cfe16bdSGreg Roach 100*1cfe16bdSGreg Roach $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); 101*1cfe16bdSGreg Roach 102*1cfe16bdSGreg Roach $format = $request->getQueryParams()['format'] ?? ''; 103*1cfe16bdSGreg Roach $destination = $request->getQueryParams()['destination'] ?? ''; 104*1cfe16bdSGreg Roach 105*1cfe16bdSGreg Roach switch ($format) { 106*1cfe16bdSGreg Roach default: 107*1cfe16bdSGreg Roach case 'HTML': 108*1cfe16bdSGreg Roach ob_start(); 109*1cfe16bdSGreg Roach new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree); 110*1cfe16bdSGreg Roach $html = ob_get_clean(); 111*1cfe16bdSGreg Roach 112*1cfe16bdSGreg Roach $this->layout = 'layouts/report'; 113*1cfe16bdSGreg Roach 114*1cfe16bdSGreg Roach $response = $this->viewResponse('report-page', [ 115*1cfe16bdSGreg Roach 'content' => $html, 116*1cfe16bdSGreg Roach 'title' => I18N::translate('Report'), 117*1cfe16bdSGreg Roach ]); 118*1cfe16bdSGreg Roach 119*1cfe16bdSGreg Roach if ($destination === 'download') { 120*1cfe16bdSGreg Roach $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"'); 121*1cfe16bdSGreg Roach } 122*1cfe16bdSGreg Roach 123*1cfe16bdSGreg Roach return $response; 124*1cfe16bdSGreg Roach 125*1cfe16bdSGreg Roach case 'PDF': 126*1cfe16bdSGreg Roach ob_start(); 127*1cfe16bdSGreg Roach new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree); 128*1cfe16bdSGreg Roach $pdf = ob_get_clean(); 129*1cfe16bdSGreg Roach 130*1cfe16bdSGreg Roach $headers = ['Content-Type' => 'application/pdf']; 131*1cfe16bdSGreg Roach 132*1cfe16bdSGreg Roach if ($destination === 'download') { 133*1cfe16bdSGreg Roach $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"'; 134*1cfe16bdSGreg Roach } 135*1cfe16bdSGreg Roach 136*1cfe16bdSGreg Roach return response($pdf, StatusCodeInterface::STATUS_OK, $headers); 137*1cfe16bdSGreg Roach } 138*1cfe16bdSGreg Roach } 139*1cfe16bdSGreg Roach} 140