xref: /webtrees/app/Report/ReportParserSetup.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
1ef0d468bSGreg Roach<?php
2ef0d468bSGreg Roach/**
3ef0d468bSGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5ef0d468bSGreg Roach * This program is free software: you can redistribute it and/or modify
6ef0d468bSGreg Roach * it under the terms of the GNU General Public License as published by
7ef0d468bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8ef0d468bSGreg Roach * (at your option) any later version.
9ef0d468bSGreg Roach * This program is distributed in the hope that it will be useful,
10ef0d468bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11ef0d468bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12ef0d468bSGreg Roach * GNU General Public License for more details.
13ef0d468bSGreg Roach * You should have received a copy of the GNU General Public License
14ef0d468bSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15ef0d468bSGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
1976692c8bSGreg Roach
20ef0d468bSGreg Roachuse Fisharebest\Webtrees\Auth;
21ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
22ef0d468bSGreg Roach
23ef0d468bSGreg Roach/**
24a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
25ef0d468bSGreg Roach */
26c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
27c1010edaSGreg Roach{
28ef0d468bSGreg Roach    /** @var array An array of report options/parameters */
2913abd6f3SGreg Roach    private $data = [];
30ef0d468bSGreg Roach
31ef0d468bSGreg Roach    /** @var string[] An array of input attributes */
32ef0d468bSGreg Roach    private $input;
33ef0d468bSGreg Roach
34ef0d468bSGreg Roach    /**
35ef0d468bSGreg Roach     * Return the parsed data.
36ef0d468bSGreg Roach     *
37ef0d468bSGreg Roach     * @return array
38ef0d468bSGreg Roach     */
398f53f488SRico Sonntag    public function reportProperties(): array
40c1010edaSGreg Roach    {
41ef0d468bSGreg Roach        return $this->data;
42ef0d468bSGreg Roach    }
43ef0d468bSGreg Roach
44ef0d468bSGreg Roach    /**
45ef0d468bSGreg Roach     * Process <Report>
46ef0d468bSGreg Roach     *
47ef0d468bSGreg Roach     * @param string[] $attrs
4883cdc021SGreg Roach     *
4983cdc021SGreg Roach     * @return void
50ef0d468bSGreg Roach     */
51208e9f76SGreg Roach    protected function reportStartHandler(array $attrs)
52c1010edaSGreg Roach    {
5347fa3e15SGreg Roach        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
54ef0d468bSGreg Roach
5547fa3e15SGreg Roach        $this->data['icon'] = $attrs['icon'] ?? '';
56ef0d468bSGreg Roach    }
57ef0d468bSGreg Roach
58ef0d468bSGreg Roach    /**
59ef0d468bSGreg Roach     * Process <var var="">
60ef0d468bSGreg Roach     *
61ef0d468bSGreg Roach     * @param string[] $attrs
6283cdc021SGreg Roach     *
6383cdc021SGreg Roach     * @return void
64ef0d468bSGreg Roach     */
65208e9f76SGreg Roach    protected function varStartHandler(array $attrs)
66c1010edaSGreg Roach    {
67ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
68da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
69ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
70ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
71a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
72ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
73ef0d468bSGreg Roach        } else {
74ef0d468bSGreg Roach            $this->text .= $attrs['var'];
75ef0d468bSGreg Roach        }
76ef0d468bSGreg Roach    }
77ef0d468bSGreg Roach
78ef0d468bSGreg Roach    /**
79ef0d468bSGreg Roach     * Process <Title>
8083cdc021SGreg Roach     *
8183cdc021SGreg Roach     * @return void
82ef0d468bSGreg Roach     */
83c1010edaSGreg Roach    protected function titleStartHandler()
84c1010edaSGreg Roach    {
85ef0d468bSGreg Roach        $this->text = '';
86ef0d468bSGreg Roach    }
87ef0d468bSGreg Roach
88ef0d468bSGreg Roach    /**
89ef0d468bSGreg Roach     * Process </Title>
9083cdc021SGreg Roach     *
9183cdc021SGreg Roach     * @return void
92ef0d468bSGreg Roach     */
93c1010edaSGreg Roach    protected function titleEndHandler()
94c1010edaSGreg Roach    {
95ef0d468bSGreg Roach        $this->data['title'] = $this->text;
9647fa3e15SGreg Roach
97ef0d468bSGreg Roach        $this->text = '';
98ef0d468bSGreg Roach    }
99ef0d468bSGreg Roach
100ef0d468bSGreg Roach    /**
101ef0d468bSGreg Roach     * Process </Description>
10283cdc021SGreg Roach     *
10383cdc021SGreg Roach     * @return void
104ef0d468bSGreg Roach     */
105c1010edaSGreg Roach    protected function descriptionEndHandler()
106c1010edaSGreg Roach    {
107ef0d468bSGreg Roach        $this->data['description'] = $this->text;
10847fa3e15SGreg Roach
109ef0d468bSGreg Roach        $this->text = '';
110ef0d468bSGreg Roach    }
111ef0d468bSGreg Roach
112ef0d468bSGreg Roach    /**
113ef0d468bSGreg Roach     * Process <Input>
114ef0d468bSGreg Roach     *
115ef0d468bSGreg Roach     * @param string[] $attrs
11683cdc021SGreg Roach     *
11783cdc021SGreg Roach     * @return void
118ef0d468bSGreg Roach     */
119208e9f76SGreg Roach    protected function inputStartHandler(array $attrs)
120c1010edaSGreg Roach    {
121ef0d468bSGreg Roach        $this->text  = '';
12213abd6f3SGreg Roach        $this->input = [
123ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
124ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
125ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
126ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
127ef0d468bSGreg Roach            'default' => '',
128ef0d468bSGreg Roach            'value'   => '',
12913abd6f3SGreg Roach        ];
130ef0d468bSGreg Roach
131ef0d468bSGreg Roach        if (isset($attrs['default'])) {
132ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
133ef0d468bSGreg Roach                $this->input['default'] = date('d M Y');
134ef0d468bSGreg Roach            } else {
13513abd6f3SGreg Roach                $match = [];
136ef0d468bSGreg Roach                if (preg_match('/NOW\s*([+\-])\s*(\d+)/', $attrs['default'], $match) > 0) {
137ef0d468bSGreg Roach                    $plus = 1;
138ef0d468bSGreg Roach                    if ($match[1] === '-') {
139ef0d468bSGreg Roach                        $plus = -1;
140ef0d468bSGreg Roach                    }
141ef0d468bSGreg Roach                    $this->input['default'] = date('d M Y', WT_TIMESTAMP + $plus * 60 * 60 * 24 * $match[2]);
142ef0d468bSGreg Roach                } else {
143ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
144ef0d468bSGreg Roach                }
145ef0d468bSGreg Roach            }
146ef0d468bSGreg Roach        }
147ef0d468bSGreg Roach    }
148ef0d468bSGreg Roach
149ef0d468bSGreg Roach    /**
150ef0d468bSGreg Roach     * Process </Input>
15183cdc021SGreg Roach     *
15283cdc021SGreg Roach     * @return void
153ef0d468bSGreg Roach     */
154c1010edaSGreg Roach    protected function inputEndHandler()
155c1010edaSGreg Roach    {
156ef0d468bSGreg Roach        $this->input['value'] = $this->text;
157ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
15813abd6f3SGreg Roach            $this->data['inputs'] = [];
159ef0d468bSGreg Roach        }
160ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
161ef0d468bSGreg Roach        $this->text             = '';
162ef0d468bSGreg Roach    }
163ef0d468bSGreg Roach}
164