xref: /webtrees/app/Report/ReportParserSetup.php (revision d50691af6602777cc2ff4d24a2bbc8004c32e358)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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\Report;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Carbon;
24use Fisharebest\Webtrees\I18N;
25
26use function preg_match;
27use function strtoupper;
28
29/**
30 * Class ReportParserSetup - parse a report.xml file and extract the setup options.
31 */
32class ReportParserSetup extends ReportParserBase
33{
34    /** @var array An array of report options/parameters */
35    private $data = [];
36
37    /** @var string[] An array of input attributes */
38    private $input;
39
40    /**
41     * Return the parsed data.
42     *
43     * @return array
44     */
45    public function reportProperties(): array
46    {
47        return $this->data;
48    }
49
50    /**
51     * Handle <var var="" />
52     *
53     * @param string[] $attrs
54     *
55     * @return void
56     */
57    protected function varStartHandler(array $attrs): void
58    {
59        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
60            $this->text .= I18N::number((int) $match[1]);
61        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
62            $this->text .= I18N::translate($match[1]);
63        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
64            $this->text .= I18N::translateContext($match[1], $match[2]);
65        } else {
66            $this->text .= $attrs['var'];
67        }
68    }
69
70    /**
71     * Handle <title>
72     *
73     * @return void
74     */
75    protected function titleStartHandler(): void
76    {
77        $this->text = '';
78    }
79
80    /**
81     * Handle </title>
82     *
83     * @return void
84     */
85    protected function titleEndHandler(): void
86    {
87        $this->data['title'] = $this->text;
88
89        $this->text = '';
90    }
91
92    /**
93     * Handle </description>
94     *
95     * @return void
96     */
97    protected function descriptionEndHandler(): void
98    {
99        $this->data['description'] = $this->text;
100
101        $this->text = '';
102    }
103
104    /**
105     * Handle <input>
106     *
107     * @param string[] $attrs
108     *
109     * @return void
110     */
111    protected function inputStartHandler(array $attrs): void
112    {
113        $this->text  = '';
114        $this->input = [
115            'name'    => $attrs['name'] ?? '',
116            'type'    => $attrs['type'] ?? '',
117            'lookup'  => $attrs['lookup'] ?? '',
118            'options' => $attrs['options'] ?? '',
119            'default' => '',
120            'value'   => '',
121        ];
122
123        if (isset($attrs['default'])) {
124            if ($attrs['default'] === 'NOW') {
125                $date = Carbon::now();
126                $this->input['default'] = strtoupper($date->format('d M Y'));
127            } else {
128                $match = [];
129                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
130                    $date = Carbon::now()->addDays((int) $match[1]);
131                    $this->input['default'] = strtoupper($date->format('d M Y'));
132                } else {
133                    $this->input['default'] = $attrs['default'];
134                }
135            }
136        } elseif ($attrs['name'] === 'pageSize') {
137            $this->input['default'] = I18N::locale()->territory()->paperSize();
138        }
139    }
140
141    /**
142     * Handle </input>
143     *
144     * @return void
145     */
146    protected function inputEndHandler(): void
147    {
148        $this->input['value'] = $this->text;
149        if (!isset($this->data['inputs'])) {
150            $this->data['inputs'] = [];
151        }
152        $this->data['inputs'][] = $this->input;
153        $this->text             = '';
154    }
155}
156