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