xref: /webtrees/app/Http/RequestHandlers/ReportSetupPage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
11cfe16bdSGreg Roach<?php
21cfe16bdSGreg Roach
31cfe16bdSGreg Roach/**
41cfe16bdSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
290f898977SGreg Roachuse Fisharebest\Webtrees\Services\LocalizationService;
301cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
31*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
321cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface;
331cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
341cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
351cfe16bdSGreg Roach
361cfe16bdSGreg Roachuse function redirect;
371cfe16bdSGreg Roachuse function route;
381cfe16bdSGreg Roach
391cfe16bdSGreg Roach/**
401cfe16bdSGreg Roach * Get parameters for a report.
411cfe16bdSGreg Roach */
421cfe16bdSGreg Roachclass ReportSetupPage implements RequestHandlerInterface
431cfe16bdSGreg Roach{
441cfe16bdSGreg Roach    use ViewResponseTrait;
451cfe16bdSGreg Roach
46c4943cffSGreg Roach    private LocalizationService $localization_service;
470f898977SGreg Roach
48c4943cffSGreg Roach    private ModuleService $module_service;
491cfe16bdSGreg Roach
501cfe16bdSGreg Roach    /**
511cfe16bdSGreg Roach     * ReportEngineController constructor.
521cfe16bdSGreg Roach     *
530f898977SGreg Roach     * @param LocalizationService $localization_service
541cfe16bdSGreg Roach     * @param ModuleService       $module_service
551cfe16bdSGreg Roach     */
560f898977SGreg Roach    public function __construct(LocalizationService $localization_service, ModuleService $module_service)
571cfe16bdSGreg Roach    {
580f898977SGreg Roach        $this->localization_service = $localization_service;
591cfe16bdSGreg Roach        $this->module_service = $module_service;
601cfe16bdSGreg Roach    }
611cfe16bdSGreg Roach
621cfe16bdSGreg Roach    /**
631cfe16bdSGreg Roach     * @param ServerRequestInterface $request
641cfe16bdSGreg Roach     *
651cfe16bdSGreg Roach     * @return ResponseInterface
661cfe16bdSGreg Roach     */
671cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
681cfe16bdSGreg Roach    {
69*b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
70*b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
711cfe16bdSGreg Roach
72*b55cbc6bSGreg Roach        $report = Validator::attributes($request)->string('report');
731cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
741cfe16bdSGreg Roach
751cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
761cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
771cfe16bdSGreg Roach        }
781cfe16bdSGreg Roach
79ef483801SGreg Roach        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
801cfe16bdSGreg Roach
811cfe16bdSGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
821cfe16bdSGreg Roach
831cfe16bdSGreg Roach        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
841cfe16bdSGreg Roach
851cfe16bdSGreg Roach        $report_array = (new ReportParserSetup($xml_filename))->reportProperties();
861cfe16bdSGreg Roach        $description  = $report_array['description'];
871cfe16bdSGreg Roach        $title        = $report_array['title'];
881cfe16bdSGreg Roach
891cfe16bdSGreg Roach        $inputs = [];
901cfe16bdSGreg Roach
911cfe16bdSGreg Roach        foreach ($report_array['inputs'] ?? [] as $n => $input) {
921cfe16bdSGreg Roach            $input += [
931cfe16bdSGreg Roach                'type'    => 'text',
941cfe16bdSGreg Roach                'default' => '',
951cfe16bdSGreg Roach                'lookup'  => '',
961cfe16bdSGreg Roach                'extra'   => '',
971cfe16bdSGreg Roach            ];
981cfe16bdSGreg Roach
991cfe16bdSGreg Roach            $attributes = [
1001cfe16bdSGreg Roach                'id'    => 'input-' . $n,
1011cfe16bdSGreg Roach                'name'  => 'vars[' . $input['name'] . ']',
1020b1b6f81SGreg Roach                'class' => $input['type'] === 'checkbox' ? 'form-control-check' : 'form-control',
1031cfe16bdSGreg Roach            ];
1041cfe16bdSGreg Roach
1051cfe16bdSGreg Roach            switch ($input['lookup']) {
1061cfe16bdSGreg Roach                case 'INDI':
1071cfe16bdSGreg Roach                    $input['control'] = view('components/select-individual', [
1081cfe16bdSGreg Roach                        'id'         => 'input-' . $n,
1091cfe16bdSGreg Roach                        'name'       => 'vars[' . $input['name'] . ']',
1106b9cb339SGreg Roach                        'individual' => Registry::individualFactory()->make($xref, $tree),
1111cfe16bdSGreg Roach                        'tree'       => $tree,
1121cfe16bdSGreg Roach                        'required'   => true,
1131cfe16bdSGreg Roach                    ]);
1141cfe16bdSGreg Roach                    break;
1151cfe16bdSGreg Roach
1161cfe16bdSGreg Roach                case 'FAM':
1171cfe16bdSGreg Roach                    $input['control'] = view('components/select-family', [
1181cfe16bdSGreg Roach                        'id'       => 'input-' . $n,
1191cfe16bdSGreg Roach                        'name'     => 'vars[' . $input['name'] . ']',
1206b9cb339SGreg Roach                        'family'   => Registry::familyFactory()->make($xref, $tree),
1211cfe16bdSGreg Roach                        'tree'     => $tree,
1221cfe16bdSGreg Roach                        'required' => true,
1231cfe16bdSGreg Roach                    ]);
1241cfe16bdSGreg Roach                    break;
1251cfe16bdSGreg Roach
1261cfe16bdSGreg Roach                case 'SOUR':
1271cfe16bdSGreg Roach                    $input['control'] = view('components/select-source', [
1281cfe16bdSGreg Roach                        'id'       => 'input-' . $n,
1291cfe16bdSGreg Roach                        'name'     => 'vars[' . $input['name'] . ']',
1306b9cb339SGreg Roach                        'family'   => Registry::sourceFactory()->make($xref, $tree),
1311cfe16bdSGreg Roach                        'tree'     => $tree,
1321cfe16bdSGreg Roach                        'required' => true,
1331cfe16bdSGreg Roach                    ]);
1341cfe16bdSGreg Roach                    break;
1351cfe16bdSGreg Roach
1361cfe16bdSGreg Roach                case 'DATE':
1370f898977SGreg Roach                    // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
1380f898977SGreg Roach                    $dmy = $this->localization_service->dateFormatToOrder(I18N::dateFormat());
1390f898977SGreg Roach
1401cfe16bdSGreg Roach                    $attributes += [
1411cfe16bdSGreg Roach                        'type'     => 'text',
1421cfe16bdSGreg Roach                        'value'    => $input['default'],
1431cfe16bdSGreg Roach                        'dir'      => 'ltr',
144a7a3d6dbSGreg Roach                        'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")'
1451cfe16bdSGreg Roach                    ];
1461cfe16bdSGreg Roach                    $input['control'] = '<input ' . Html::attributes($attributes) . '>';
147f810ce3bSGreg Roach                    $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]);
1481cfe16bdSGreg Roach                    break;
1491cfe16bdSGreg Roach
1501cfe16bdSGreg Roach                default:
1511cfe16bdSGreg Roach                    switch ($input['type']) {
1521cfe16bdSGreg Roach                        case 'text':
1531cfe16bdSGreg Roach                            $attributes += [
1541cfe16bdSGreg Roach                                'type'  => 'text',
1551cfe16bdSGreg Roach                                'value' => $input['default'],
1561cfe16bdSGreg Roach                            ];
1571cfe16bdSGreg Roach                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1581cfe16bdSGreg Roach                            break;
1591cfe16bdSGreg Roach
1601cfe16bdSGreg Roach                        case 'checkbox':
1611cfe16bdSGreg Roach                            $attributes += [
1621cfe16bdSGreg Roach                                'type'    => 'checkbox',
1631cfe16bdSGreg Roach                                'checked' => (bool) $input['default'],
1641cfe16bdSGreg Roach                            ];
1651cfe16bdSGreg Roach                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1661cfe16bdSGreg Roach                            break;
1671cfe16bdSGreg Roach
1681cfe16bdSGreg Roach                        case 'select':
1691cfe16bdSGreg Roach                            $options = [];
1701cfe16bdSGreg Roach                            foreach (explode('|', $input['options']) as $option) {
1711cfe16bdSGreg Roach                                [$key, $value] = explode('=>', $option);
1721cfe16bdSGreg Roach                                if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) {
1731cfe16bdSGreg Roach                                    $number        = (float) $match[1];
1741cfe16bdSGreg Roach                                    $precision     = (int) ($match[3] ?? 0);
1751cfe16bdSGreg Roach                                    $options[$key] = I18N::number($number, $precision);
1761cfe16bdSGreg Roach                                } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1771cfe16bdSGreg Roach                                    $options[$key] = I18N::translate($match[1]);
1781cfe16bdSGreg Roach                                } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1791cfe16bdSGreg Roach                                    $options[$key] = I18N::translateContext($match[1], $match[2]);
1801cfe16bdSGreg Roach                                }
1811cfe16bdSGreg Roach                            }
1821cfe16bdSGreg Roach                            $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]);
1831cfe16bdSGreg Roach                            break;
1841cfe16bdSGreg Roach                    }
1851cfe16bdSGreg Roach            }
1861cfe16bdSGreg Roach
1871cfe16bdSGreg Roach            $inputs[] = $input;
1881cfe16bdSGreg Roach        }
1891cfe16bdSGreg Roach
1907f7be5ebSGreg Roach        $destination = $user->getPreference('default-report-destination', 'view');
1917f7be5ebSGreg Roach        $format      = $user->getPreference('default-report-format', 'PDF');
1927f7be5ebSGreg Roach
1931cfe16bdSGreg Roach        return $this->viewResponse('report-setup-page', [
1941cfe16bdSGreg Roach            'description' => $description,
1957f7be5ebSGreg Roach            'destination' => $destination,
1967f7be5ebSGreg Roach            'format'      => $format,
1971cfe16bdSGreg Roach            'inputs'      => $inputs,
1981cfe16bdSGreg Roach            'report'      => $report,
1991cfe16bdSGreg Roach            'title'       => $title,
2001cfe16bdSGreg Roach            'tree'        => $tree,
2011cfe16bdSGreg Roach        ]);
2021cfe16bdSGreg Roach    }
2031cfe16bdSGreg Roach}
204