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