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