xref: /webtrees/app/Report/ReportParserSetup.php (revision 208e9f762b2d87324d4f7e01791fd398ccf1cc27)
1ef0d468bSGreg Roach<?php
2ef0d468bSGreg Roach/**
3ef0d468bSGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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     */
51*208e9f76SGreg Roach    protected function reportStartHandler(array $attrs)
52c1010edaSGreg Roach    {
53ef0d468bSGreg Roach        $access = Auth::PRIV_PRIVATE;
54ef0d468bSGreg Roach        if (isset($attrs['access'])) {
557a6ee1acSGreg Roach            if (isset($$attrs['access'])) {
567a6ee1acSGreg Roach                $access = $$attrs['access'];
57ef0d468bSGreg Roach            }
58ef0d468bSGreg Roach        }
59ef0d468bSGreg Roach        $this->data['access'] = $access;
60ef0d468bSGreg Roach
61ef0d468bSGreg Roach        if (isset($attrs['icon'])) {
62ef0d468bSGreg Roach            $this->data['icon'] = $attrs['icon'];
63ef0d468bSGreg Roach        } else {
64ef0d468bSGreg Roach            $this->data['icon'] = '';
65ef0d468bSGreg Roach        }
66ef0d468bSGreg Roach    }
67ef0d468bSGreg Roach
68ef0d468bSGreg Roach    /**
69ef0d468bSGreg Roach     * Process <var var="">
70ef0d468bSGreg Roach     *
71ef0d468bSGreg Roach     * @param string[] $attrs
7283cdc021SGreg Roach     *
7383cdc021SGreg Roach     * @return void
74ef0d468bSGreg Roach     */
75*208e9f76SGreg Roach    protected function varStartHandler(array $attrs)
76c1010edaSGreg Roach    {
77ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
78da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
79ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
80ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
81a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
82ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
83ef0d468bSGreg Roach        } else {
84ef0d468bSGreg Roach            $this->text .= $attrs['var'];
85ef0d468bSGreg Roach        }
86ef0d468bSGreg Roach    }
87ef0d468bSGreg Roach
88ef0d468bSGreg Roach    /**
89ef0d468bSGreg Roach     * Process <Title>
9083cdc021SGreg Roach     *
9183cdc021SGreg Roach     * @return void
92ef0d468bSGreg Roach     */
93c1010edaSGreg Roach    protected function titleStartHandler()
94c1010edaSGreg Roach    {
95ef0d468bSGreg Roach        $this->text = '';
96ef0d468bSGreg Roach    }
97ef0d468bSGreg Roach
98ef0d468bSGreg Roach    /**
99ef0d468bSGreg Roach     * Process </Title>
10083cdc021SGreg Roach     *
10183cdc021SGreg Roach     * @return void
102ef0d468bSGreg Roach     */
103c1010edaSGreg Roach    protected function titleEndHandler()
104c1010edaSGreg Roach    {
105ef0d468bSGreg Roach        $this->data['title'] = $this->text;
106ef0d468bSGreg Roach        $this->text          = '';
107ef0d468bSGreg Roach    }
108ef0d468bSGreg Roach
109ef0d468bSGreg Roach    /**
110ef0d468bSGreg Roach     * Process </Description>
11183cdc021SGreg Roach     *
11283cdc021SGreg Roach     * @return void
113ef0d468bSGreg Roach     */
114c1010edaSGreg Roach    protected function descriptionEndHandler()
115c1010edaSGreg Roach    {
116ef0d468bSGreg Roach        $this->data['description'] = $this->text;
117ef0d468bSGreg Roach        $this->text                = '';
118ef0d468bSGreg Roach    }
119ef0d468bSGreg Roach
120ef0d468bSGreg Roach    /**
121ef0d468bSGreg Roach     * Process <Input>
122ef0d468bSGreg Roach     *
123ef0d468bSGreg Roach     * @param string[] $attrs
12483cdc021SGreg Roach     *
12583cdc021SGreg Roach     * @return void
126ef0d468bSGreg Roach     */
127*208e9f76SGreg Roach    protected function inputStartHandler(array $attrs)
128c1010edaSGreg Roach    {
129ef0d468bSGreg Roach        $this->text  = '';
13013abd6f3SGreg Roach        $this->input = [
131ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
132ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
133ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
134ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
135ef0d468bSGreg Roach            'default' => '',
136ef0d468bSGreg Roach            'value'   => '',
13713abd6f3SGreg Roach        ];
138ef0d468bSGreg Roach
139ef0d468bSGreg Roach        if (isset($attrs['default'])) {
140ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
141ef0d468bSGreg Roach                $this->input['default'] = date('d M Y');
142ef0d468bSGreg Roach            } else {
14313abd6f3SGreg Roach                $match = [];
144ef0d468bSGreg Roach                if (preg_match('/NOW\s*([+\-])\s*(\d+)/', $attrs['default'], $match) > 0) {
145ef0d468bSGreg Roach                    $plus = 1;
146ef0d468bSGreg Roach                    if ($match[1] === '-') {
147ef0d468bSGreg Roach                        $plus = -1;
148ef0d468bSGreg Roach                    }
149ef0d468bSGreg Roach                    $this->input['default'] = date('d M Y', WT_TIMESTAMP + $plus * 60 * 60 * 24 * $match[2]);
150ef0d468bSGreg Roach                } else {
151ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
152ef0d468bSGreg Roach                }
153ef0d468bSGreg Roach            }
154ef0d468bSGreg Roach        }
155ef0d468bSGreg Roach    }
156ef0d468bSGreg Roach
157ef0d468bSGreg Roach    /**
158ef0d468bSGreg Roach     * Process </Input>
15983cdc021SGreg Roach     *
16083cdc021SGreg Roach     * @return void
161ef0d468bSGreg Roach     */
162c1010edaSGreg Roach    protected function inputEndHandler()
163c1010edaSGreg Roach    {
164ef0d468bSGreg Roach        $this->input['value'] = $this->text;
165ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
16613abd6f3SGreg Roach            $this->data['inputs'] = [];
167ef0d468bSGreg Roach        }
168ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
169ef0d468bSGreg Roach        $this->text             = '';
170ef0d468bSGreg Roach    }
171ef0d468bSGreg Roach}
172