xref: /webtrees/app/Http/RequestHandlers/ReportSetupPage.php (revision 5896ce5f7fc2b1aae72f1bec79eab33d0221f249)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Html;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Module\ModuleReportInterface;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Report\ReportParserSetup;
29use Fisharebest\Webtrees\Services\ModuleService;
30use Fisharebest\Webtrees\Validator;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function redirect;
36use function route;
37
38/**
39 * Get parameters for a report.
40 */
41class ReportSetupPage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    private ModuleService $module_service;
46
47    /**
48     * ReportEngineController constructor.
49     *
50     * @param ModuleService $module_service
51     */
52    public function __construct(ModuleService $module_service)
53    {
54        $this->module_service = $module_service;
55    }
56
57    /**
58     * @param ServerRequestInterface $request
59     *
60     * @return ResponseInterface
61     */
62    public function handle(ServerRequestInterface $request): ResponseInterface
63    {
64        $tree = Validator::attributes($request)->tree();
65        $user = Validator::attributes($request)->user();
66
67        $report = Validator::attributes($request)->string('report');
68        $module = $this->module_service->findByName($report);
69
70        if (!$module instanceof ModuleReportInterface) {
71            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
72        }
73
74        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
75
76        $xref = Validator::queryParams($request)->isXref()->string('xref', '');
77
78        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
79
80        $report_array = (new ReportParserSetup($xml_filename))->reportProperties();
81        $description  = $report_array['description'];
82        $title        = $report_array['title'];
83
84        $inputs = [];
85
86        foreach ($report_array['inputs'] ?? [] as $n => $input) {
87            $input += [
88                'type'    => 'text',
89                'default' => '',
90                'lookup'  => '',
91                'extra'   => '',
92            ];
93
94            $attributes = [
95                'id'    => 'input-' . $n,
96                'name'  => 'vars[' . $input['name'] . ']',
97                'class' => $input['type'] === 'checkbox' ? 'form-control-check' : 'form-control',
98            ];
99
100            switch ($input['lookup']) {
101                case 'INDI':
102                    $input['control'] = view('components/select-individual', [
103                        'id'         => 'input-' . $n,
104                        'name'       => 'vars[' . $input['name'] . ']',
105                        'individual' => Registry::individualFactory()->make($xref, $tree),
106                        'tree'       => $tree,
107                        'required'   => true,
108                    ]);
109                    break;
110
111                case 'FAM':
112                    $input['control'] = view('components/select-family', [
113                        'id'       => 'input-' . $n,
114                        'name'     => 'vars[' . $input['name'] . ']',
115                        'family'   => Registry::familyFactory()->make($xref, $tree),
116                        'tree'     => $tree,
117                        'required' => true,
118                    ]);
119                    break;
120
121                case 'SOUR':
122                    $input['control'] = view('components/select-source', [
123                        'id'       => 'input-' . $n,
124                        'name'     => 'vars[' . $input['name'] . ']',
125                        'family'   => Registry::sourceFactory()->make($xref, $tree),
126                        'tree'     => $tree,
127                        'required' => true,
128                    ]);
129                    break;
130
131                case 'DATE':
132                    // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
133                    $dmy = I18N::language()->dateOrder();
134
135                    $attributes += [
136                        'type'     => 'text',
137                        'value'    => $input['default'],
138                        'dir'      => 'ltr',
139                        'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")'
140                    ];
141                    $input['control'] = '<input ' . Html::attributes($attributes) . '>';
142                    $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]);
143                    break;
144
145                default:
146                    switch ($input['type']) {
147                        case 'text':
148                            $attributes += [
149                                'type'  => 'text',
150                                'value' => $input['default'],
151                            ];
152                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
153                            break;
154
155                        case 'checkbox':
156                            $attributes += [
157                                'type'    => 'checkbox',
158                                'checked' => (bool) $input['default'],
159                            ];
160                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
161                            break;
162
163                        case 'select':
164                            $options = [];
165                            foreach (explode('|', $input['options']) as $option) {
166                                [$key, $value] = explode('=>', $option);
167                                if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) {
168                                    $number        = (float) $match[1];
169                                    $precision     = (int) ($match[3] ?? 0);
170                                    $options[$key] = I18N::number($number, $precision);
171                                } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
172                                    $options[$key] = I18N::translate($match[1]);
173                                } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
174                                    $options[$key] = I18N::translateContext($match[1], $match[2]);
175                                }
176                            }
177                            $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]);
178                            break;
179                    }
180            }
181
182            $inputs[] = $input;
183        }
184
185        $destination = $user->getPreference('default-report-destination', 'view');
186        $format      = $user->getPreference('default-report-format', 'PDF');
187
188        return $this->viewResponse('report-setup-page', [
189            'description' => $description,
190            'destination' => $destination,
191            'format'      => $format,
192            'inputs'      => $inputs,
193            'report'      => $report,
194            'title'       => $title,
195            'tree'        => $tree,
196        ]);
197    }
198}
199