xref: /webtrees/app/Report/ReportParserSetup.php (revision 380303ed773c417e7948eccb7cbe5421722d96c0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Carbon;
24use Fisharebest\Webtrees\I18N;
25
26/**
27 * Class ReportParserSetup - parse a report.xml file and extract the setup options.
28 */
29class ReportParserSetup extends ReportParserBase
30{
31    /** @var array An array of report options/parameters */
32    private $data = [];
33
34    /** @var string[] An array of input attributes */
35    private $input;
36
37    /**
38     * Return the parsed data.
39     *
40     * @return array
41     */
42    public function reportProperties(): array
43    {
44        return $this->data;
45    }
46
47    /**
48     * Process <Report>
49     *
50     * @param string[] $attrs
51     *
52     * @return void
53     */
54    protected function reportStartHandler(array $attrs): void
55    {
56        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
57
58        $this->data['icon'] = $attrs['icon'] ?? '';
59    }
60
61    /**
62     * Process <var var="">
63     *
64     * @param string[] $attrs
65     *
66     * @return void
67     */
68    protected function varStartHandler(array $attrs): void
69    {
70        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
71            $this->text .= I18N::number((int) $match[1]);
72        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
73            $this->text .= I18N::translate($match[1]);
74        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
75            $this->text .= I18N::translateContext($match[1], $match[2]);
76        } else {
77            $this->text .= $attrs['var'];
78        }
79    }
80
81    /**
82     * Process <Title>
83     *
84     * @return void
85     */
86    protected function titleStartHandler(): void
87    {
88        $this->text = '';
89    }
90
91    /**
92     * Process </Title>
93     *
94     * @return void
95     */
96    protected function titleEndHandler(): void
97    {
98        $this->data['title'] = $this->text;
99
100        $this->text = '';
101    }
102
103    /**
104     * Process </Description>
105     *
106     * @return void
107     */
108    protected function descriptionEndHandler(): void
109    {
110        $this->data['description'] = $this->text;
111
112        $this->text = '';
113    }
114
115    /**
116     * Process <Input>
117     *
118     * @param string[] $attrs
119     *
120     * @return void
121     */
122    protected function inputStartHandler(array $attrs): void
123    {
124        $this->text  = '';
125        $this->input = [
126            'name'    => $attrs['name'] ?? '',
127            'type'    => $attrs['type'] ?? '',
128            'lookup'  => $attrs['lookup'] ?? '',
129            'options' => $attrs['options'] ?? '',
130            'default' => '',
131            'value'   => '',
132        ];
133
134        if (isset($attrs['default'])) {
135            if ($attrs['default'] === 'NOW') {
136                $date = Carbon::now();
137                $this->input['default'] = strtoupper($date->format('d M Y'));
138            } else {
139                $match = [];
140                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
141                    $date = Carbon::now()->addDays((int) $match[1]);
142                    $this->input['default'] = strtoupper($date->format('d M Y'));
143                } else {
144                    $this->input['default'] = $attrs['default'];
145                }
146            }
147        } elseif ($attrs['name'] === 'pageSize') {
148            $this->input['default'] = I18N::locale()->territory()->paperSize();
149        }
150    }
151
152    /**
153     * Process </Input>
154     *
155     * @return void
156     */
157    protected function inputEndHandler(): void
158    {
159        $this->input['value'] = $this->text;
160        if (!isset($this->data['inputs'])) {
161            $this->data['inputs'] = [];
162        }
163        $this->data['inputs'][] = $this->input;
164        $this->text             = '';
165    }
166}
167