xref: /webtrees/app/Report/ReportParserSetup.php (revision 4459dc9a6d0c27769f8135175f3569e4fa287451)
1ef0d468bSGreg Roach<?php
2ef0d468bSGreg Roach/**
3ef0d468bSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg 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;
21*4459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
22ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
23ef0d468bSGreg Roach
24ef0d468bSGreg Roach/**
25a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
26ef0d468bSGreg Roach */
27c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
28c1010edaSGreg Roach{
29ef0d468bSGreg Roach    /** @var array An array of report options/parameters */
3013abd6f3SGreg Roach    private $data = [];
31ef0d468bSGreg Roach
32ef0d468bSGreg Roach    /** @var string[] An array of input attributes */
33ef0d468bSGreg Roach    private $input;
34ef0d468bSGreg Roach
35ef0d468bSGreg Roach    /**
36ef0d468bSGreg Roach     * Return the parsed data.
37ef0d468bSGreg Roach     *
38ef0d468bSGreg Roach     * @return array
39ef0d468bSGreg Roach     */
408f53f488SRico Sonntag    public function reportProperties(): array
41c1010edaSGreg Roach    {
42ef0d468bSGreg Roach        return $this->data;
43ef0d468bSGreg Roach    }
44ef0d468bSGreg Roach
45ef0d468bSGreg Roach    /**
46ef0d468bSGreg Roach     * Process <Report>
47ef0d468bSGreg Roach     *
48ef0d468bSGreg Roach     * @param string[] $attrs
4983cdc021SGreg Roach     *
5083cdc021SGreg Roach     * @return void
51ef0d468bSGreg Roach     */
52208e9f76SGreg Roach    protected function reportStartHandler(array $attrs)
53c1010edaSGreg Roach    {
5447fa3e15SGreg Roach        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
55ef0d468bSGreg Roach
5647fa3e15SGreg Roach        $this->data['icon'] = $attrs['icon'] ?? '';
57ef0d468bSGreg Roach    }
58ef0d468bSGreg Roach
59ef0d468bSGreg Roach    /**
60ef0d468bSGreg Roach     * Process <var var="">
61ef0d468bSGreg Roach     *
62ef0d468bSGreg Roach     * @param string[] $attrs
6383cdc021SGreg Roach     *
6483cdc021SGreg Roach     * @return void
65ef0d468bSGreg Roach     */
66208e9f76SGreg Roach    protected function varStartHandler(array $attrs)
67c1010edaSGreg Roach    {
68ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
69da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
70ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
71ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
72a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
73ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
74ef0d468bSGreg Roach        } else {
75ef0d468bSGreg Roach            $this->text .= $attrs['var'];
76ef0d468bSGreg Roach        }
77ef0d468bSGreg Roach    }
78ef0d468bSGreg Roach
79ef0d468bSGreg Roach    /**
80ef0d468bSGreg Roach     * Process <Title>
8183cdc021SGreg Roach     *
8283cdc021SGreg Roach     * @return void
83ef0d468bSGreg Roach     */
84c1010edaSGreg Roach    protected function titleStartHandler()
85c1010edaSGreg Roach    {
86ef0d468bSGreg Roach        $this->text = '';
87ef0d468bSGreg Roach    }
88ef0d468bSGreg Roach
89ef0d468bSGreg Roach    /**
90ef0d468bSGreg Roach     * Process </Title>
9183cdc021SGreg Roach     *
9283cdc021SGreg Roach     * @return void
93ef0d468bSGreg Roach     */
94c1010edaSGreg Roach    protected function titleEndHandler()
95c1010edaSGreg Roach    {
96ef0d468bSGreg Roach        $this->data['title'] = $this->text;
9747fa3e15SGreg Roach
98ef0d468bSGreg Roach        $this->text = '';
99ef0d468bSGreg Roach    }
100ef0d468bSGreg Roach
101ef0d468bSGreg Roach    /**
102ef0d468bSGreg Roach     * Process </Description>
10383cdc021SGreg Roach     *
10483cdc021SGreg Roach     * @return void
105ef0d468bSGreg Roach     */
106c1010edaSGreg Roach    protected function descriptionEndHandler()
107c1010edaSGreg Roach    {
108ef0d468bSGreg Roach        $this->data['description'] = $this->text;
10947fa3e15SGreg Roach
110ef0d468bSGreg Roach        $this->text = '';
111ef0d468bSGreg Roach    }
112ef0d468bSGreg Roach
113ef0d468bSGreg Roach    /**
114ef0d468bSGreg Roach     * Process <Input>
115ef0d468bSGreg Roach     *
116ef0d468bSGreg Roach     * @param string[] $attrs
11783cdc021SGreg Roach     *
11883cdc021SGreg Roach     * @return void
119ef0d468bSGreg Roach     */
120208e9f76SGreg Roach    protected function inputStartHandler(array $attrs)
121c1010edaSGreg Roach    {
122ef0d468bSGreg Roach        $this->text  = '';
12313abd6f3SGreg Roach        $this->input = [
124ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
125ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
126ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
127ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
128ef0d468bSGreg Roach            'default' => '',
129ef0d468bSGreg Roach            'value'   => '',
13013abd6f3SGreg Roach        ];
131ef0d468bSGreg Roach
132ef0d468bSGreg Roach        if (isset($attrs['default'])) {
133ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
134*4459dc9aSGreg Roach                $this->input['default'] = Carbon::now()->format('d M Y');
135ef0d468bSGreg Roach            } else {
13613abd6f3SGreg Roach                $match = [];
137ad98d39dSGreg Roach                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
138*4459dc9aSGreg Roach                    $this->input['default'] = Carbon::now()->addDays((int) $match[1])->format('d M Y');
139ef0d468bSGreg Roach                } else {
140ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
141ef0d468bSGreg Roach                }
142ef0d468bSGreg Roach            }
143ef0d468bSGreg Roach        }
144ef0d468bSGreg Roach    }
145ef0d468bSGreg Roach
146ef0d468bSGreg Roach    /**
147ef0d468bSGreg Roach     * Process </Input>
14883cdc021SGreg Roach     *
14983cdc021SGreg Roach     * @return void
150ef0d468bSGreg Roach     */
151c1010edaSGreg Roach    protected function inputEndHandler()
152c1010edaSGreg Roach    {
153ef0d468bSGreg Roach        $this->input['value'] = $this->text;
154ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
15513abd6f3SGreg Roach            $this->data['inputs'] = [];
156ef0d468bSGreg Roach        }
157ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
158ef0d468bSGreg Roach        $this->text             = '';
159ef0d468bSGreg Roach    }
160ef0d468bSGreg Roach}
161