xref: /webtrees/app/Report/ReportParserSetup.php (revision 3eae54c8a20b98985facfcff8d58c982338bbf85)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Report;
19
20use Fisharebest\Localization\Locale\LocaleInterface;
21use Fisharebest\Webtrees\Auth;
22use Fisharebest\Webtrees\Carbon;
23use Fisharebest\Webtrees\I18N;
24
25/**
26 * Class ReportParserSetup - parse a report.xml file and extract the setup options.
27 */
28class ReportParserSetup extends ReportParserBase
29{
30    /** @var array An array of report options/parameters */
31    private $data = [];
32
33    /** @var string[] An array of input attributes */
34    private $input;
35
36    /**
37     * Return the parsed data.
38     *
39     * @return array
40     */
41    public function reportProperties(): array
42    {
43        return $this->data;
44    }
45
46    /**
47     * Process <Report>
48     *
49     * @param string[] $attrs
50     *
51     * @return void
52     */
53    protected function reportStartHandler(array $attrs): void
54    {
55        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
56
57        $this->data['icon'] = $attrs['icon'] ?? '';
58    }
59
60    /**
61     * Process <var var="">
62     *
63     * @param string[] $attrs
64     *
65     * @return void
66     */
67    protected function varStartHandler(array $attrs): void
68    {
69        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
70            $this->text .= I18N::number((int) $match[1]);
71        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
72            $this->text .= I18N::translate($match[1]);
73        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
74            $this->text .= I18N::translateContext($match[1], $match[2]);
75        } else {
76            $this->text .= $attrs['var'];
77        }
78    }
79
80    /**
81     * Process <Title>
82     *
83     * @return void
84     */
85    protected function titleStartHandler(): void
86    {
87        $this->text = '';
88    }
89
90    /**
91     * Process </Title>
92     *
93     * @return void
94     */
95    protected function titleEndHandler(): void
96    {
97        $this->data['title'] = $this->text;
98
99        $this->text = '';
100    }
101
102    /**
103     * Process </Description>
104     *
105     * @return void
106     */
107    protected function descriptionEndHandler(): void
108    {
109        $this->data['description'] = $this->text;
110
111        $this->text = '';
112    }
113
114    /**
115     * Process <Input>
116     *
117     * @param string[] $attrs
118     *
119     * @return void
120     */
121    protected function inputStartHandler(array $attrs): void
122    {
123        $this->text  = '';
124        $this->input = [
125            'name'    => $attrs['name'] ?? '',
126            'type'    => $attrs['type'] ?? '',
127            'lookup'  => $attrs['lookup'] ?? '',
128            'options' => $attrs['options'] ?? '',
129            'default' => '',
130            'value'   => '',
131        ];
132
133        if (isset($attrs['default'])) {
134            if ($attrs['default'] === 'NOW') {
135                $date = Carbon::now();
136                $this->input['default'] = strtoupper($date->format('d M Y'));
137            } else {
138                $match = [];
139                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
140                    $date = Carbon::now()->addDays((int) $match[1]);
141                    $this->input['default'] = strtoupper($date->format('d M Y'));
142                } else {
143                    $this->input['default'] = $attrs['default'];
144                }
145            }
146        } elseif ($attrs['name'] === 'pageSize') {
147            $this->input['default'] = app(LocaleInterface::class)->territory()->paperSize();
148        }
149    }
150
151    /**
152     * Process </Input>
153     *
154     * @return void
155     */
156    protected function inputEndHandler(): void
157    {
158        $this->input['value'] = $this->text;
159        if (!isset($this->data['inputs'])) {
160            $this->data['inputs'] = [];
161        }
162        $this->data['inputs'][] = $this->input;
163        $this->text             = '';
164    }
165}
166