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 Fisharebest\Webtrees\Auth; 231cfe16bdSGreg Roachuse Fisharebest\Webtrees\Html; 241cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 251cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N; 261cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface; 276b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 281cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserSetup; 291cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 311cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface; 321cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 331cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 341cfe16bdSGreg Roach 351cfe16bdSGreg Roachuse function redirect; 361cfe16bdSGreg Roachuse function route; 371cfe16bdSGreg Roach 381cfe16bdSGreg Roach/** 391cfe16bdSGreg Roach * Get parameters for a report. 401cfe16bdSGreg Roach */ 411cfe16bdSGreg Roachclass ReportSetupPage implements RequestHandlerInterface 421cfe16bdSGreg Roach{ 431cfe16bdSGreg Roach use ViewResponseTrait; 441cfe16bdSGreg Roach 45c4943cffSGreg Roach private ModuleService $module_service; 461cfe16bdSGreg Roach 471cfe16bdSGreg Roach /** 481cfe16bdSGreg Roach * @param ModuleService $module_service 491cfe16bdSGreg Roach */ 504a9a6095SGreg Roach public function __construct(ModuleService $module_service) 511cfe16bdSGreg Roach { 521cfe16bdSGreg Roach $this->module_service = $module_service; 531cfe16bdSGreg Roach } 541cfe16bdSGreg Roach 551cfe16bdSGreg Roach /** 561cfe16bdSGreg Roach * @param ServerRequestInterface $request 571cfe16bdSGreg Roach * 581cfe16bdSGreg Roach * @return ResponseInterface 591cfe16bdSGreg Roach */ 601cfe16bdSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 611cfe16bdSGreg Roach { 62b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 63b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 641cfe16bdSGreg Roach 65b55cbc6bSGreg Roach $report = Validator::attributes($request)->string('report'); 661cfe16bdSGreg Roach $module = $this->module_service->findByName($report); 671cfe16bdSGreg Roach 681cfe16bdSGreg Roach if (!$module instanceof ModuleReportInterface) { 691cfe16bdSGreg Roach return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); 701cfe16bdSGreg Roach } 711cfe16bdSGreg Roach 72ef483801SGreg Roach Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user); 731cfe16bdSGreg Roach 74748dbe15SGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref', ''); 751cfe16bdSGreg Roach 761cfe16bdSGreg Roach $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); 771cfe16bdSGreg Roach 781cfe16bdSGreg Roach $report_array = (new ReportParserSetup($xml_filename))->reportProperties(); 791cfe16bdSGreg Roach $description = $report_array['description']; 801cfe16bdSGreg Roach $title = $report_array['title']; 811cfe16bdSGreg Roach 821cfe16bdSGreg Roach $inputs = []; 831cfe16bdSGreg Roach 841cfe16bdSGreg Roach foreach ($report_array['inputs'] ?? [] as $n => $input) { 851cfe16bdSGreg Roach $input += [ 861cfe16bdSGreg Roach 'type' => 'text', 871cfe16bdSGreg Roach 'default' => '', 881cfe16bdSGreg Roach 'lookup' => '', 891cfe16bdSGreg Roach 'extra' => '', 901cfe16bdSGreg Roach ]; 911cfe16bdSGreg Roach 921cfe16bdSGreg Roach $attributes = [ 931cfe16bdSGreg Roach 'id' => 'input-' . $n, 941cfe16bdSGreg Roach 'name' => 'vars[' . $input['name'] . ']', 950b1b6f81SGreg Roach 'class' => $input['type'] === 'checkbox' ? 'form-control-check' : 'form-control', 961cfe16bdSGreg Roach ]; 971cfe16bdSGreg Roach 981cfe16bdSGreg Roach switch ($input['lookup']) { 991cfe16bdSGreg Roach case 'INDI': 1001cfe16bdSGreg Roach $input['control'] = view('components/select-individual', [ 1011cfe16bdSGreg Roach 'id' => 'input-' . $n, 1021cfe16bdSGreg Roach 'name' => 'vars[' . $input['name'] . ']', 1036b9cb339SGreg Roach 'individual' => Registry::individualFactory()->make($xref, $tree), 1041cfe16bdSGreg Roach 'tree' => $tree, 1051cfe16bdSGreg Roach 'required' => true, 1061cfe16bdSGreg Roach ]); 1071cfe16bdSGreg Roach break; 1081cfe16bdSGreg Roach 1091cfe16bdSGreg Roach case 'FAM': 1101cfe16bdSGreg Roach $input['control'] = view('components/select-family', [ 1111cfe16bdSGreg Roach 'id' => 'input-' . $n, 1121cfe16bdSGreg Roach 'name' => 'vars[' . $input['name'] . ']', 1136b9cb339SGreg Roach 'family' => Registry::familyFactory()->make($xref, $tree), 1141cfe16bdSGreg Roach 'tree' => $tree, 1151cfe16bdSGreg Roach 'required' => true, 1161cfe16bdSGreg Roach ]); 1171cfe16bdSGreg Roach break; 1181cfe16bdSGreg Roach 1191cfe16bdSGreg Roach case 'SOUR': 1201cfe16bdSGreg Roach $input['control'] = view('components/select-source', [ 1211cfe16bdSGreg Roach 'id' => 'input-' . $n, 1221cfe16bdSGreg Roach 'name' => 'vars[' . $input['name'] . ']', 1236b9cb339SGreg Roach 'family' => Registry::sourceFactory()->make($xref, $tree), 1241cfe16bdSGreg Roach 'tree' => $tree, 1251cfe16bdSGreg Roach 'required' => true, 1261cfe16bdSGreg Roach ]); 1271cfe16bdSGreg Roach break; 1281cfe16bdSGreg Roach 1291cfe16bdSGreg Roach case 'DATE': 1300f898977SGreg Roach // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly. 1314a9a6095SGreg Roach $dmy = I18N::language()->dateOrder(); 1320f898977SGreg Roach 1331cfe16bdSGreg Roach $attributes += [ 1341cfe16bdSGreg Roach 'type' => 'text', 1351cfe16bdSGreg Roach 'value' => $input['default'], 1361cfe16bdSGreg Roach 'dir' => 'ltr', 137a7a3d6dbSGreg Roach 'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")' 1381cfe16bdSGreg Roach ]; 1391cfe16bdSGreg Roach $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 140f810ce3bSGreg Roach $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]); 1411cfe16bdSGreg Roach break; 1421cfe16bdSGreg Roach 1431cfe16bdSGreg Roach default: 1441cfe16bdSGreg Roach switch ($input['type']) { 1451cfe16bdSGreg Roach case 'text': 1461cfe16bdSGreg Roach $attributes += [ 1471cfe16bdSGreg Roach 'type' => 'text', 1481cfe16bdSGreg Roach 'value' => $input['default'], 1491cfe16bdSGreg Roach ]; 1501cfe16bdSGreg Roach $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 1511cfe16bdSGreg Roach break; 1521cfe16bdSGreg Roach 1531cfe16bdSGreg Roach case 'checkbox': 1541cfe16bdSGreg Roach $attributes += [ 1551cfe16bdSGreg Roach 'type' => 'checkbox', 1561cfe16bdSGreg Roach 'checked' => (bool) $input['default'], 1571cfe16bdSGreg Roach ]; 1581cfe16bdSGreg Roach $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 1591cfe16bdSGreg Roach break; 1601cfe16bdSGreg Roach 1611cfe16bdSGreg Roach case 'select': 1621cfe16bdSGreg Roach $options = []; 1631cfe16bdSGreg Roach foreach (explode('|', $input['options']) as $option) { 1641cfe16bdSGreg Roach [$key, $value] = explode('=>', $option); 1651cfe16bdSGreg Roach if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) { 1661cfe16bdSGreg Roach $number = (float) $match[1]; 1671cfe16bdSGreg Roach $precision = (int) ($match[3] ?? 0); 1681cfe16bdSGreg Roach $options[$key] = I18N::number($number, $precision); 1691cfe16bdSGreg Roach } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { 1701cfe16bdSGreg Roach $options[$key] = I18N::translate($match[1]); 1711cfe16bdSGreg Roach } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { 1721cfe16bdSGreg Roach $options[$key] = I18N::translateContext($match[1], $match[2]); 1731cfe16bdSGreg Roach } 1741cfe16bdSGreg Roach } 1751cfe16bdSGreg Roach $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]); 1761cfe16bdSGreg Roach break; 1771cfe16bdSGreg Roach } 1781cfe16bdSGreg Roach } 1791cfe16bdSGreg Roach 1801cfe16bdSGreg Roach $inputs[] = $input; 1811cfe16bdSGreg Roach } 1821cfe16bdSGreg Roach 1837f7be5ebSGreg Roach $destination = $user->getPreference('default-report-destination', 'view'); 1847f7be5ebSGreg Roach $format = $user->getPreference('default-report-format', 'PDF'); 1857f7be5ebSGreg Roach 1861cfe16bdSGreg Roach return $this->viewResponse('report-setup-page', [ 1871cfe16bdSGreg Roach 'description' => $description, 1887f7be5ebSGreg Roach 'destination' => $destination, 1897f7be5ebSGreg Roach 'format' => $format, 1901cfe16bdSGreg Roach 'inputs' => $inputs, 1911cfe16bdSGreg Roach 'report' => $report, 1921cfe16bdSGreg Roach 'title' => $title, 1931cfe16bdSGreg Roach 'tree' => $tree, 1941cfe16bdSGreg Roach ]); 1951cfe16bdSGreg Roach } 1961cfe16bdSGreg Roach} 197