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