xref: /webtrees/app/Http/RequestHandlers/ReportSetupPage.php (revision 7f7be5ebddc34c2c0789769121d48fa5de6606c5)
11cfe16bdSGreg Roach<?php
21cfe16bdSGreg Roach
31cfe16bdSGreg Roach/**
41cfe16bdSGreg Roach * webtrees: online genealogy
51cfe16bdSGreg Roach * Copyright (C) 2019 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
151cfe16bdSGreg Roach * along with this program. If not, see <http://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\Family;
251cfe16bdSGreg Roachuse Fisharebest\Webtrees\Html;
261cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
271cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N;
281cfe16bdSGreg Roachuse Fisharebest\Webtrees\Individual;
291cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
301cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserSetup;
311cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
321cfe16bdSGreg Roachuse Fisharebest\Webtrees\Source;
331cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree;
341cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface;
351cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
361cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
371cfe16bdSGreg Roach
381cfe16bdSGreg Roachuse function assert;
391cfe16bdSGreg Roachuse function redirect;
401cfe16bdSGreg Roachuse function route;
411cfe16bdSGreg Roach
421cfe16bdSGreg Roach/**
431cfe16bdSGreg Roach * Get parameters for a report.
441cfe16bdSGreg Roach */
451cfe16bdSGreg Roachclass ReportSetupPage implements RequestHandlerInterface
461cfe16bdSGreg Roach{
471cfe16bdSGreg Roach    use ViewResponseTrait;
481cfe16bdSGreg Roach
491cfe16bdSGreg Roach    /**
501cfe16bdSGreg Roach     * @var ModuleService
511cfe16bdSGreg Roach     */
521cfe16bdSGreg Roach    private $module_service;
531cfe16bdSGreg Roach
541cfe16bdSGreg Roach    /**
551cfe16bdSGreg Roach     * ReportEngineController constructor.
561cfe16bdSGreg Roach     *
571cfe16bdSGreg Roach     * @param ModuleService $module_service
581cfe16bdSGreg Roach     */
591cfe16bdSGreg Roach    public function __construct(ModuleService $module_service)
601cfe16bdSGreg Roach    {
611cfe16bdSGreg Roach        $this->module_service = $module_service;
621cfe16bdSGreg Roach    }
631cfe16bdSGreg Roach
641cfe16bdSGreg Roach    /**
651cfe16bdSGreg Roach     * @param ServerRequestInterface $request
661cfe16bdSGreg Roach     *
671cfe16bdSGreg Roach     * @return ResponseInterface
681cfe16bdSGreg Roach     */
691cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
701cfe16bdSGreg Roach    {
711cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
7275964c75SGreg Roach        assert($tree instanceof Tree);
731cfe16bdSGreg Roach
741cfe16bdSGreg Roach        $user = $request->getAttribute('user');
7575964c75SGreg Roach        assert($user instanceof UserInterface);
761cfe16bdSGreg Roach
771cfe16bdSGreg Roach        $report = $request->getAttribute('report');
781cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
791cfe16bdSGreg Roach
801cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
811cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
821cfe16bdSGreg Roach        }
831cfe16bdSGreg Roach
841cfe16bdSGreg Roach        Auth::checkComponentAccess($module, 'report', $tree, $user);
851cfe16bdSGreg Roach
861cfe16bdSGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
871cfe16bdSGreg Roach
881cfe16bdSGreg Roach        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
891cfe16bdSGreg Roach
901cfe16bdSGreg Roach        $report_array = (new ReportParserSetup($xml_filename))->reportProperties();
911cfe16bdSGreg Roach        $description  = $report_array['description'];
921cfe16bdSGreg Roach        $title        = $report_array['title'];
931cfe16bdSGreg Roach
941cfe16bdSGreg Roach        $inputs = [];
951cfe16bdSGreg Roach
961cfe16bdSGreg Roach        foreach ($report_array['inputs'] ?? [] as $n => $input) {
971cfe16bdSGreg Roach            $input += [
981cfe16bdSGreg Roach                'type'    => 'text',
991cfe16bdSGreg Roach                'default' => '',
1001cfe16bdSGreg Roach                'lookup'  => '',
1011cfe16bdSGreg Roach                'extra'   => '',
1021cfe16bdSGreg Roach            ];
1031cfe16bdSGreg Roach
1041cfe16bdSGreg Roach            $attributes = [
1051cfe16bdSGreg Roach                'id'   => 'input-' . $n,
1061cfe16bdSGreg Roach                'name' => 'vars[' . $input['name'] . ']',
1071cfe16bdSGreg Roach            ];
1081cfe16bdSGreg Roach
1091cfe16bdSGreg Roach            switch ($input['lookup']) {
1101cfe16bdSGreg Roach                case 'INDI':
1111cfe16bdSGreg Roach                    $input['control'] = view('components/select-individual', [
1121cfe16bdSGreg Roach                        'id'         => 'input-' . $n,
1131cfe16bdSGreg Roach                        'name'       => 'vars[' . $input['name'] . ']',
1141cfe16bdSGreg Roach                        'individual' => Individual::getInstance($xref, $tree),
1151cfe16bdSGreg Roach                        'tree'       => $tree,
1161cfe16bdSGreg Roach                        'required'   => true,
1171cfe16bdSGreg Roach                    ]);
1181cfe16bdSGreg Roach                    break;
1191cfe16bdSGreg Roach
1201cfe16bdSGreg Roach                case 'FAM':
1211cfe16bdSGreg Roach                    $input['control'] = view('components/select-family', [
1221cfe16bdSGreg Roach                        'id'       => 'input-' . $n,
1231cfe16bdSGreg Roach                        'name'     => 'vars[' . $input['name'] . ']',
1241cfe16bdSGreg Roach                        'family'   => Family::getInstance($xref, $tree),
1251cfe16bdSGreg Roach                        'tree'     => $tree,
1261cfe16bdSGreg Roach                        'required' => true,
1271cfe16bdSGreg Roach                    ]);
1281cfe16bdSGreg Roach                    break;
1291cfe16bdSGreg Roach
1301cfe16bdSGreg Roach                case 'SOUR':
1311cfe16bdSGreg Roach                    $input['control'] = view('components/select-source', [
1321cfe16bdSGreg Roach                        'id'       => 'input-' . $n,
1331cfe16bdSGreg Roach                        'name'     => 'vars[' . $input['name'] . ']',
1341cfe16bdSGreg Roach                        'family'   => Source::getInstance($xref, $tree),
1351cfe16bdSGreg Roach                        'tree'     => $tree,
1361cfe16bdSGreg Roach                        'required' => true,
1371cfe16bdSGreg Roach                    ]);
1381cfe16bdSGreg Roach                    break;
1391cfe16bdSGreg Roach
1401cfe16bdSGreg Roach                case 'DATE':
1411cfe16bdSGreg Roach                    $attributes += [
1421cfe16bdSGreg Roach                        'type'  => 'text',
1431cfe16bdSGreg Roach                        'value' => $input['default'],
1441cfe16bdSGreg Roach                        'dir'   => 'ltr',
1451cfe16bdSGreg Roach                    ];
1461cfe16bdSGreg Roach                    $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1471cfe16bdSGreg Roach                    $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>' .
1481cfe16bdSGreg Roach                        '<div id="calendar-widget-' . $n . '" style="position:absolute;visibility:hidden;background-color:white;z-index:1000;"></div>';
1491cfe16bdSGreg Roach                    break;
1501cfe16bdSGreg Roach
1511cfe16bdSGreg Roach                default:
1521cfe16bdSGreg Roach                    switch ($input['type']) {
1531cfe16bdSGreg Roach                        case 'text':
1541cfe16bdSGreg Roach                            $attributes += [
1551cfe16bdSGreg Roach                                'type'  => 'text',
1561cfe16bdSGreg Roach                                'value' => $input['default'],
1571cfe16bdSGreg Roach                            ];
1581cfe16bdSGreg Roach                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1591cfe16bdSGreg Roach                            break;
1601cfe16bdSGreg Roach
1611cfe16bdSGreg Roach                        case 'checkbox':
1621cfe16bdSGreg Roach                            $attributes += [
1631cfe16bdSGreg Roach                                'type'    => 'checkbox',
1641cfe16bdSGreg Roach                                'checked' => (bool) $input['default'],
1651cfe16bdSGreg Roach                            ];
1661cfe16bdSGreg Roach                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
1671cfe16bdSGreg Roach                            break;
1681cfe16bdSGreg Roach
1691cfe16bdSGreg Roach                        case 'select':
1701cfe16bdSGreg Roach                            $options = [];
1711cfe16bdSGreg Roach                            foreach (explode('|', $input['options']) as $option) {
1721cfe16bdSGreg Roach                                [$key, $value] = explode('=>', $option);
1731cfe16bdSGreg Roach                                if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) {
1741cfe16bdSGreg Roach                                    $number        = (float) $match[1];
1751cfe16bdSGreg Roach                                    $precision     = (int) ($match[3] ?? 0);
1761cfe16bdSGreg Roach                                    $options[$key] = I18N::number($number, $precision);
1771cfe16bdSGreg Roach                                } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1781cfe16bdSGreg Roach                                    $options[$key] = I18N::translate($match[1]);
1791cfe16bdSGreg Roach                                } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1801cfe16bdSGreg Roach                                    $options[$key] = I18N::translateContext($match[1], $match[2]);
1811cfe16bdSGreg Roach                                }
1821cfe16bdSGreg Roach                            }
1831cfe16bdSGreg Roach                            $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]);
1841cfe16bdSGreg Roach                            break;
1851cfe16bdSGreg Roach                    }
1861cfe16bdSGreg Roach            }
1871cfe16bdSGreg Roach
1881cfe16bdSGreg Roach            $inputs[] = $input;
1891cfe16bdSGreg Roach        }
1901cfe16bdSGreg Roach
191*7f7be5ebSGreg Roach        $destination = $user->getPreference('default-report-destination', 'view');
192*7f7be5ebSGreg Roach        $format = $user->getPreference('default-report-format', 'PDF');
193*7f7be5ebSGreg Roach
1941cfe16bdSGreg Roach        return $this->viewResponse('report-setup-page', [
1951cfe16bdSGreg Roach            'description' => $description,
196*7f7be5ebSGreg Roach            'destination' => $destination,
197*7f7be5ebSGreg Roach            'format'      => $format,
1981cfe16bdSGreg Roach            'inputs'      => $inputs,
1991cfe16bdSGreg Roach            'report'      => $report,
2001cfe16bdSGreg Roach            'title'       => $title,
2011cfe16bdSGreg Roach            'tree'        => $tree,
2021cfe16bdSGreg Roach        ]);
2031cfe16bdSGreg Roach    }
2041cfe16bdSGreg Roach}
205