xref: /webtrees/app/Http/RequestHandlers/ReportSetupPage.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
11cfe16bdSGreg Roach<?php
21cfe16bdSGreg Roach
31cfe16bdSGreg Roach/**
41cfe16bdSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg 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
15*89f7189bSGreg 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\Contracts\UserInterface;
241cfe16bdSGreg Roachuse Fisharebest\Webtrees\Html;
251cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
261cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N;
271cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
291cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserSetup;
300f898977SGreg Roachuse Fisharebest\Webtrees\Services\LocalizationService;
311cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
321cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree;
331cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface;
341cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
351cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
361cfe16bdSGreg Roach
371cfe16bdSGreg Roachuse function assert;
381cfe16bdSGreg Roachuse function redirect;
391cfe16bdSGreg Roachuse function route;
401cfe16bdSGreg Roach
411cfe16bdSGreg Roach/**
421cfe16bdSGreg Roach * Get parameters for a report.
431cfe16bdSGreg Roach */
441cfe16bdSGreg Roachclass ReportSetupPage implements RequestHandlerInterface
451cfe16bdSGreg Roach{
461cfe16bdSGreg Roach    use ViewResponseTrait;
471cfe16bdSGreg Roach
480f898977SGreg Roach    /** @var LocalizationService */
490f898977SGreg Roach    private $localization_service;
500f898977SGreg Roach
510f898977SGreg Roach    /** @var ModuleService */
521cfe16bdSGreg Roach    private $module_service;
531cfe16bdSGreg Roach
541cfe16bdSGreg Roach    /**
551cfe16bdSGreg Roach     * ReportEngineController constructor.
561cfe16bdSGreg Roach     *
570f898977SGreg Roach     * @param LocalizationService $localization_service
581cfe16bdSGreg Roach     * @param ModuleService       $module_service
591cfe16bdSGreg Roach     */
600f898977SGreg Roach    public function __construct(LocalizationService $localization_service, ModuleService $module_service)
611cfe16bdSGreg Roach    {
620f898977SGreg Roach        $this->localization_service = $localization_service;
631cfe16bdSGreg Roach        $this->module_service = $module_service;
641cfe16bdSGreg Roach    }
651cfe16bdSGreg Roach
661cfe16bdSGreg Roach    /**
671cfe16bdSGreg Roach     * @param ServerRequestInterface $request
681cfe16bdSGreg Roach     *
691cfe16bdSGreg Roach     * @return ResponseInterface
701cfe16bdSGreg Roach     */
711cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
721cfe16bdSGreg Roach    {
731cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
7475964c75SGreg Roach        assert($tree instanceof Tree);
751cfe16bdSGreg Roach
761cfe16bdSGreg Roach        $user = $request->getAttribute('user');
7775964c75SGreg Roach        assert($user instanceof UserInterface);
781cfe16bdSGreg Roach
791cfe16bdSGreg Roach        $report = $request->getAttribute('report');
801cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
811cfe16bdSGreg Roach
821cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
831cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
841cfe16bdSGreg Roach        }
851cfe16bdSGreg Roach
86ef483801SGreg Roach        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
871cfe16bdSGreg Roach
881cfe16bdSGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
891cfe16bdSGreg Roach
901cfe16bdSGreg Roach        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
911cfe16bdSGreg Roach
921cfe16bdSGreg Roach        $report_array = (new ReportParserSetup($xml_filename))->reportProperties();
931cfe16bdSGreg Roach        $description  = $report_array['description'];
941cfe16bdSGreg Roach        $title        = $report_array['title'];
951cfe16bdSGreg Roach
961cfe16bdSGreg Roach        $inputs = [];
971cfe16bdSGreg Roach
981cfe16bdSGreg Roach        foreach ($report_array['inputs'] ?? [] as $n => $input) {
991cfe16bdSGreg Roach            $input += [
1001cfe16bdSGreg Roach                'type'    => 'text',
1011cfe16bdSGreg Roach                'default' => '',
1021cfe16bdSGreg Roach                'lookup'  => '',
1031cfe16bdSGreg Roach                'extra'   => '',
1041cfe16bdSGreg Roach            ];
1051cfe16bdSGreg Roach
1061cfe16bdSGreg Roach            $attributes = [
1071cfe16bdSGreg Roach                'id'    => 'input-' . $n,
1081cfe16bdSGreg Roach                'name'  => 'vars[' . $input['name'] . ']',
1090b1b6f81SGreg Roach                'class' => $input['type'] === 'checkbox' ? 'form-control-check' : 'form-control',
1101cfe16bdSGreg Roach            ];
1111cfe16bdSGreg Roach
1121cfe16bdSGreg Roach            switch ($input['lookup']) {
1131cfe16bdSGreg Roach                case 'INDI':
1141cfe16bdSGreg Roach                    $input['control'] = view('components/select-individual', [
1151cfe16bdSGreg Roach                        'id'         => 'input-' . $n,
1161cfe16bdSGreg Roach                        'name'       => 'vars[' . $input['name'] . ']',
1176b9cb339SGreg Roach                        'individual' => Registry::individualFactory()->make($xref, $tree),
1181cfe16bdSGreg Roach                        'tree'       => $tree,
1191cfe16bdSGreg Roach                        'required'   => true,
1201cfe16bdSGreg Roach                    ]);
1211cfe16bdSGreg Roach                    break;
1221cfe16bdSGreg Roach
1231cfe16bdSGreg Roach                case 'FAM':
1241cfe16bdSGreg Roach                    $input['control'] = view('components/select-family', [
1251cfe16bdSGreg Roach                        'id'       => 'input-' . $n,
1261cfe16bdSGreg Roach                        'name'     => 'vars[' . $input['name'] . ']',
1276b9cb339SGreg Roach                        'family'   => Registry::familyFactory()->make($xref, $tree),
1281cfe16bdSGreg Roach                        'tree'     => $tree,
1291cfe16bdSGreg Roach                        'required' => true,
1301cfe16bdSGreg Roach                    ]);
1311cfe16bdSGreg Roach                    break;
1321cfe16bdSGreg Roach
1331cfe16bdSGreg Roach                case 'SOUR':
1341cfe16bdSGreg Roach                    $input['control'] = view('components/select-source', [
1351cfe16bdSGreg Roach                        'id'       => 'input-' . $n,
1361cfe16bdSGreg Roach                        'name'     => 'vars[' . $input['name'] . ']',
1376b9cb339SGreg Roach                        'family'   => Registry::sourceFactory()->make($xref, $tree),
1381cfe16bdSGreg Roach                        'tree'     => $tree,
1391cfe16bdSGreg Roach                        'required' => true,
1401cfe16bdSGreg Roach                    ]);
1411cfe16bdSGreg Roach                    break;
1421cfe16bdSGreg Roach
1431cfe16bdSGreg Roach                case 'DATE':
1440f898977SGreg Roach                    // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
1450f898977SGreg Roach                    $dmy = $this->localization_service->dateFormatToOrder(I18N::dateFormat());
1460f898977SGreg Roach
1471cfe16bdSGreg Roach                    $attributes += [
1481cfe16bdSGreg Roach                        'type'     => 'text',
1491cfe16bdSGreg Roach                        'value'    => $input['default'],
1501cfe16bdSGreg Roach                        'dir'      => 'ltr',
151a7a3d6dbSGreg Roach                        'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")'
1521cfe16bdSGreg Roach                    ];
1531cfe16bdSGreg Roach                    $input['control'] = '<input ' . Html::attributes($attributes) . '>';
154f810ce3bSGreg Roach                    $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]);
1551cfe16bdSGreg Roach                    break;
1561cfe16bdSGreg Roach
1571cfe16bdSGreg Roach                default:
1581cfe16bdSGreg Roach                    switch ($input['type']) {
1591cfe16bdSGreg Roach                        case 'text':
1601cfe16bdSGreg Roach                            $attributes += [
1611cfe16bdSGreg Roach                                'type'  => 'text',
1621cfe16bdSGreg Roach                                'value' => $input['default'],
1631cfe16bdSGreg Roach                            ];
1641cfe16bdSGreg Roach                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1651cfe16bdSGreg Roach                            break;
1661cfe16bdSGreg Roach
1671cfe16bdSGreg Roach                        case 'checkbox':
1681cfe16bdSGreg Roach                            $attributes += [
1691cfe16bdSGreg Roach                                'type'    => 'checkbox',
1701cfe16bdSGreg Roach                                'checked' => (bool) $input['default'],
1711cfe16bdSGreg Roach                            ];
1721cfe16bdSGreg Roach                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1731cfe16bdSGreg Roach                            break;
1741cfe16bdSGreg Roach
1751cfe16bdSGreg Roach                        case 'select':
1761cfe16bdSGreg Roach                            $options = [];
1771cfe16bdSGreg Roach                            foreach (explode('|', $input['options']) as $option) {
1781cfe16bdSGreg Roach                                [$key, $value] = explode('=>', $option);
1791cfe16bdSGreg Roach                                if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) {
1801cfe16bdSGreg Roach                                    $number        = (float) $match[1];
1811cfe16bdSGreg Roach                                    $precision     = (int) ($match[3] ?? 0);
1821cfe16bdSGreg Roach                                    $options[$key] = I18N::number($number, $precision);
1831cfe16bdSGreg Roach                                } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1841cfe16bdSGreg Roach                                    $options[$key] = I18N::translate($match[1]);
1851cfe16bdSGreg Roach                                } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1861cfe16bdSGreg Roach                                    $options[$key] = I18N::translateContext($match[1], $match[2]);
1871cfe16bdSGreg Roach                                }
1881cfe16bdSGreg Roach                            }
1891cfe16bdSGreg Roach                            $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]);
1901cfe16bdSGreg Roach                            break;
1911cfe16bdSGreg Roach                    }
1921cfe16bdSGreg Roach            }
1931cfe16bdSGreg Roach
1941cfe16bdSGreg Roach            $inputs[] = $input;
1951cfe16bdSGreg Roach        }
1961cfe16bdSGreg Roach
1977f7be5ebSGreg Roach        $destination = $user->getPreference('default-report-destination', 'view');
1987f7be5ebSGreg Roach        $format      = $user->getPreference('default-report-format', 'PDF');
1997f7be5ebSGreg Roach
2001cfe16bdSGreg Roach        return $this->viewResponse('report-setup-page', [
2011cfe16bdSGreg Roach            'description' => $description,
2027f7be5ebSGreg Roach            'destination' => $destination,
2037f7be5ebSGreg Roach            'format'      => $format,
2041cfe16bdSGreg Roach            'inputs'      => $inputs,
2051cfe16bdSGreg Roach            'report'      => $report,
2061cfe16bdSGreg Roach            'title'       => $title,
2071cfe16bdSGreg Roach            'tree'        => $tree,
2081cfe16bdSGreg Roach        ]);
2091cfe16bdSGreg Roach    }
2101cfe16bdSGreg Roach}
211