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