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