1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\Factory; 25use Fisharebest\Webtrees\Html; 26use Fisharebest\Webtrees\Http\ViewResponseTrait; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Module\ModuleReportInterface; 29use Fisharebest\Webtrees\Report\ReportParserSetup; 30use Fisharebest\Webtrees\Services\LocalizationService; 31use Fisharebest\Webtrees\Services\ModuleService; 32use Fisharebest\Webtrees\Tree; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35use Psr\Http\Server\RequestHandlerInterface; 36 37use function assert; 38use function redirect; 39use function route; 40 41/** 42 * Get parameters for a report. 43 */ 44class ReportSetupPage implements RequestHandlerInterface 45{ 46 use ViewResponseTrait; 47 48 /** @var LocalizationService */ 49 private $localization_service; 50 51 /** @var ModuleService */ 52 private $module_service; 53 54 /** 55 * ReportEngineController constructor. 56 * 57 * @param LocalizationService $localization_service 58 * @param ModuleService $module_service 59 */ 60 public function __construct(LocalizationService $localization_service, ModuleService $module_service) 61 { 62 $this->localization_service = $localization_service; 63 $this->module_service = $module_service; 64 } 65 66 /** 67 * @param ServerRequestInterface $request 68 * 69 * @return ResponseInterface 70 */ 71 public function handle(ServerRequestInterface $request): ResponseInterface 72 { 73 $tree = $request->getAttribute('tree'); 74 assert($tree instanceof Tree); 75 76 $user = $request->getAttribute('user'); 77 assert($user instanceof UserInterface); 78 79 $report = $request->getAttribute('report'); 80 $module = $this->module_service->findByName($report); 81 82 if (!$module instanceof ModuleReportInterface) { 83 return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); 84 } 85 86 Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user); 87 88 $xref = $request->getQueryParams()['xref'] ?? ''; 89 90 $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); 91 92 $report_array = (new ReportParserSetup($xml_filename))->reportProperties(); 93 $description = $report_array['description']; 94 $title = $report_array['title']; 95 96 $inputs = []; 97 98 foreach ($report_array['inputs'] ?? [] as $n => $input) { 99 $input += [ 100 'type' => 'text', 101 'default' => '', 102 'lookup' => '', 103 'extra' => '', 104 ]; 105 106 $attributes = [ 107 'id' => 'input-' . $n, 108 'name' => 'vars[' . $input['name'] . ']', 109 'class' => 'form-control', 110 ]; 111 112 switch ($input['lookup']) { 113 case 'INDI': 114 $input['control'] = view('components/select-individual', [ 115 'id' => 'input-' . $n, 116 'name' => 'vars[' . $input['name'] . ']', 117 'individual' => Factory::individual()->make($xref, $tree), 118 'tree' => $tree, 119 'required' => true, 120 ]); 121 break; 122 123 case 'FAM': 124 $input['control'] = view('components/select-family', [ 125 'id' => 'input-' . $n, 126 'name' => 'vars[' . $input['name'] . ']', 127 'family' => Factory::family()->make($xref, $tree), 128 'tree' => $tree, 129 'required' => true, 130 ]); 131 break; 132 133 case 'SOUR': 134 $input['control'] = view('components/select-source', [ 135 'id' => 'input-' . $n, 136 'name' => 'vars[' . $input['name'] . ']', 137 'family' => Factory::source()->make($xref, $tree), 138 'tree' => $tree, 139 'required' => true, 140 ]); 141 break; 142 143 case 'DATE': 144 // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly. 145 $dmy = $this->localization_service->dateFormatToOrder(I18N::dateFormat()); 146 147 $attributes += [ 148 'type' => 'text', 149 'value' => $input['default'], 150 'dir' => 'ltr', 151 'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")' 152 ]; 153 $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 154 $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]); 155 break; 156 157 default: 158 switch ($input['type']) { 159 case 'text': 160 $attributes += [ 161 'type' => 'text', 162 'value' => $input['default'], 163 ]; 164 $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 165 break; 166 167 case 'checkbox': 168 $attributes += [ 169 'type' => 'checkbox', 170 'checked' => (bool) $input['default'], 171 ]; 172 $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 173 break; 174 175 case 'select': 176 $options = []; 177 foreach (explode('|', $input['options']) as $option) { 178 [$key, $value] = explode('=>', $option); 179 if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) { 180 $number = (float) $match[1]; 181 $precision = (int) ($match[3] ?? 0); 182 $options[$key] = I18N::number($number, $precision); 183 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { 184 $options[$key] = I18N::translate($match[1]); 185 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { 186 $options[$key] = I18N::translateContext($match[1], $match[2]); 187 } 188 } 189 $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]); 190 break; 191 } 192 } 193 194 $inputs[] = $input; 195 } 196 197 $destination = $user->getPreference('default-report-destination', 'view'); 198 $format = $user->getPreference('default-report-format', 'PDF'); 199 200 return $this->viewResponse('report-setup-page', [ 201 'description' => $description, 202 'destination' => $destination, 203 'format' => $format, 204 'inputs' => $inputs, 205 'report' => $report, 206 'title' => $title, 207 'tree' => $tree, 208 ]); 209 } 210} 211