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