xref: /webtrees/app/Report/ReportParserSetup.php (revision 67994fb087e1b24564a780e4ae8aeff801733e35)
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 */
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($attrs)
52    {
53        $access = Auth::PRIV_PRIVATE;
54        if (isset($attrs['access'])) {
55            if (isset($$attrs['access'])) {
56                $access = $$attrs['access'];
57            }
58        }
59        $this->data['access'] = $access;
60
61        if (isset($attrs['icon'])) {
62            $this->data['icon'] = $attrs['icon'];
63        } else {
64            $this->data['icon'] = '';
65        }
66    }
67
68    /**
69     * Process <var var="">
70     *
71     * @param string[] $attrs
72     *
73     * @return void
74     */
75    protected function varStartHandler($attrs)
76    {
77        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
78            $this->text .= I18N::number((int) $match[1]);
79        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
80            $this->text .= I18N::translate($match[1]);
81        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
82            $this->text .= I18N::translateContext($match[1], $match[2]);
83        } else {
84            $this->text .= $attrs['var'];
85        }
86    }
87
88    /**
89     * Process <Title>
90     *
91     * @return void
92     */
93    protected function titleStartHandler()
94    {
95        $this->text = '';
96    }
97
98    /**
99     * Process </Title>
100     *
101     * @return void
102     */
103    protected function titleEndHandler()
104    {
105        $this->data['title'] = $this->text;
106        $this->text          = '';
107    }
108
109    /**
110     * Process </Description>
111     *
112     * @return void
113     */
114    protected function descriptionEndHandler()
115    {
116        $this->data['description'] = $this->text;
117        $this->text                = '';
118    }
119
120    /**
121     * Process <Input>
122     *
123     * @param string[] $attrs
124     *
125     * @return void
126     */
127    protected function inputStartHandler($attrs)
128    {
129        $this->text  = '';
130        $this->input = [
131            'name'    => $attrs['name'] ?? '',
132            'type'    => $attrs['type'] ?? '',
133            'lookup'  => $attrs['lookup'] ?? '',
134            'options' => $attrs['options'] ?? '',
135            'default' => '',
136            'value'   => '',
137        ];
138
139        if (isset($attrs['default'])) {
140            if ($attrs['default'] === 'NOW') {
141                $this->input['default'] = date('d M Y');
142            } else {
143                $match = [];
144                if (preg_match('/NOW\s*([+\-])\s*(\d+)/', $attrs['default'], $match) > 0) {
145                    $plus = 1;
146                    if ($match[1] === '-') {
147                        $plus = -1;
148                    }
149                    $this->input['default'] = date('d M Y', WT_TIMESTAMP + $plus * 60 * 60 * 24 * $match[2]);
150                } else {
151                    $this->input['default'] = $attrs['default'];
152                }
153            }
154        }
155    }
156
157    /**
158     * Process </Input>
159     *
160     * @return void
161     */
162    protected function inputEndHandler()
163    {
164        $this->input['value'] = $this->text;
165        if (!isset($this->data['inputs'])) {
166            $this->data['inputs'] = [];
167        }
168        $this->data['inputs'][] = $this->input;
169        $this->text             = '';
170    }
171}
172