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\ModuleService; 31use Fisharebest\Webtrees\Tree; 32use Psr\Http\Message\ResponseInterface; 33use Psr\Http\Message\ServerRequestInterface; 34use Psr\Http\Server\RequestHandlerInterface; 35 36use function assert; 37use function redirect; 38use function route; 39 40/** 41 * Get parameters for a report. 42 */ 43class ReportSetupPage implements RequestHandlerInterface 44{ 45 use ViewResponseTrait; 46 47 /** 48 * @var ModuleService 49 */ 50 private $module_service; 51 52 /** 53 * ReportEngineController constructor. 54 * 55 * @param ModuleService $module_service 56 */ 57 public function __construct(ModuleService $module_service) 58 { 59 $this->module_service = $module_service; 60 } 61 62 /** 63 * @param ServerRequestInterface $request 64 * 65 * @return ResponseInterface 66 */ 67 public function handle(ServerRequestInterface $request): ResponseInterface 68 { 69 $tree = $request->getAttribute('tree'); 70 assert($tree instanceof Tree); 71 72 $user = $request->getAttribute('user'); 73 assert($user instanceof UserInterface); 74 75 $report = $request->getAttribute('report'); 76 $module = $this->module_service->findByName($report); 77 78 if (!$module instanceof ModuleReportInterface) { 79 return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); 80 } 81 82 Auth::checkComponentAccess($module, 'report', $tree, $user); 83 84 $xref = $request->getQueryParams()['xref'] ?? ''; 85 86 $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); 87 88 $report_array = (new ReportParserSetup($xml_filename))->reportProperties(); 89 $description = $report_array['description']; 90 $title = $report_array['title']; 91 92 $inputs = []; 93 94 foreach ($report_array['inputs'] ?? [] as $n => $input) { 95 $input += [ 96 'type' => 'text', 97 'default' => '', 98 'lookup' => '', 99 'extra' => '', 100 ]; 101 102 $attributes = [ 103 'id' => 'input-' . $n, 104 'name' => 'vars[' . $input['name'] . ']', 105 ]; 106 107 switch ($input['lookup']) { 108 case 'INDI': 109 $input['control'] = view('components/select-individual', [ 110 'id' => 'input-' . $n, 111 'name' => 'vars[' . $input['name'] . ']', 112 'individual' => Factory::individual()->make($xref, $tree), 113 'tree' => $tree, 114 'required' => true, 115 ]); 116 break; 117 118 case 'FAM': 119 $input['control'] = view('components/select-family', [ 120 'id' => 'input-' . $n, 121 'name' => 'vars[' . $input['name'] . ']', 122 'family' => Factory::family()->make($xref, $tree), 123 'tree' => $tree, 124 'required' => true, 125 ]); 126 break; 127 128 case 'SOUR': 129 $input['control'] = view('components/select-source', [ 130 'id' => 'input-' . $n, 131 'name' => 'vars[' . $input['name'] . ']', 132 'family' => Factory::source()->make($xref, $tree), 133 'tree' => $tree, 134 'required' => true, 135 ]); 136 break; 137 138 case 'DATE': 139 $attributes += [ 140 'type' => 'text', 141 'value' => $input['default'], 142 'dir' => 'ltr', 143 ]; 144 $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 145 $input['extra'] = '<a href="#" title="' . I18N::translate('Select a date') . '" class ="btn btn-link" onclick="' . e('return calendarWidget("calendar-widget-' . $n . '", "input-' . $n . '");') . '">' . view('icons/calendar') . '</a>' . 146 '<div id="calendar-widget-' . $n . '" style="position:absolute;visibility:hidden;background-color:white;z-index:1000;"></div>'; 147 break; 148 149 default: 150 switch ($input['type']) { 151 case 'text': 152 $attributes += [ 153 'type' => 'text', 154 'value' => $input['default'], 155 ]; 156 $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 157 break; 158 159 case 'checkbox': 160 $attributes += [ 161 'type' => 'checkbox', 162 'checked' => (bool) $input['default'], 163 ]; 164 $input['control'] = '<input ' . Html::attributes($attributes) . '>'; 165 break; 166 167 case 'select': 168 $options = []; 169 foreach (explode('|', $input['options']) as $option) { 170 [$key, $value] = explode('=>', $option); 171 if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) { 172 $number = (float) $match[1]; 173 $precision = (int) ($match[3] ?? 0); 174 $options[$key] = I18N::number($number, $precision); 175 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { 176 $options[$key] = I18N::translate($match[1]); 177 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { 178 $options[$key] = I18N::translateContext($match[1], $match[2]); 179 } 180 } 181 $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]); 182 break; 183 } 184 } 185 186 $inputs[] = $input; 187 } 188 189 $destination = $user->getPreference('default-report-destination', 'view'); 190 $format = $user->getPreference('default-report-format', 'PDF'); 191 192 return $this->viewResponse('report-setup-page', [ 193 'description' => $description, 194 'destination' => $destination, 195 'format' => $format, 196 'inputs' => $inputs, 197 'report' => $report, 198 'title' => $title, 199 'tree' => $tree, 200 ]); 201 } 202} 203