xref: /webtrees/app/Report/ReportParserSetup.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1ef0d468bSGreg Roach<?php
2*3976b470SGreg Roach
3ef0d468bSGreg Roach/**
4ef0d468bSGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6ef0d468bSGreg Roach * This program is free software: you can redistribute it and/or modify
7ef0d468bSGreg Roach * it under the terms of the GNU General Public License as published by
8ef0d468bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ef0d468bSGreg Roach * (at your option) any later version.
10ef0d468bSGreg Roach * This program is distributed in the hope that it will be useful,
11ef0d468bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ef0d468bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ef0d468bSGreg Roach * GNU General Public License for more details.
14ef0d468bSGreg Roach * You should have received a copy of the GNU General Public License
15ef0d468bSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16ef0d468bSGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2076692c8bSGreg Roach
214d314e6bSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
22ef0d468bSGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
25ef0d468bSGreg Roach
26ef0d468bSGreg Roach/**
27a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
28ef0d468bSGreg Roach */
29c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
30c1010edaSGreg Roach{
31ef0d468bSGreg Roach    /** @var array An array of report options/parameters */
3213abd6f3SGreg Roach    private $data = [];
33ef0d468bSGreg Roach
34ef0d468bSGreg Roach    /** @var string[] An array of input attributes */
35ef0d468bSGreg Roach    private $input;
36ef0d468bSGreg Roach
37ef0d468bSGreg Roach    /**
38ef0d468bSGreg Roach     * Return the parsed data.
39ef0d468bSGreg Roach     *
40ef0d468bSGreg Roach     * @return array
41ef0d468bSGreg Roach     */
428f53f488SRico Sonntag    public function reportProperties(): array
43c1010edaSGreg Roach    {
44ef0d468bSGreg Roach        return $this->data;
45ef0d468bSGreg Roach    }
46ef0d468bSGreg Roach
47ef0d468bSGreg Roach    /**
48ef0d468bSGreg Roach     * Process <Report>
49ef0d468bSGreg Roach     *
50ef0d468bSGreg Roach     * @param string[] $attrs
5183cdc021SGreg Roach     *
5283cdc021SGreg Roach     * @return void
53ef0d468bSGreg Roach     */
543b3cfeeaSGreg Roach    protected function reportStartHandler(array $attrs): void
55c1010edaSGreg Roach    {
5647fa3e15SGreg Roach        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
57ef0d468bSGreg Roach
5847fa3e15SGreg Roach        $this->data['icon'] = $attrs['icon'] ?? '';
59ef0d468bSGreg Roach    }
60ef0d468bSGreg Roach
61ef0d468bSGreg Roach    /**
62ef0d468bSGreg Roach     * Process <var var="">
63ef0d468bSGreg Roach     *
64ef0d468bSGreg Roach     * @param string[] $attrs
6583cdc021SGreg Roach     *
6683cdc021SGreg Roach     * @return void
67ef0d468bSGreg Roach     */
683b3cfeeaSGreg Roach    protected function varStartHandler(array $attrs): void
69c1010edaSGreg Roach    {
70ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
71da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
72ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
73ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
74a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
75ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
76ef0d468bSGreg Roach        } else {
77ef0d468bSGreg Roach            $this->text .= $attrs['var'];
78ef0d468bSGreg Roach        }
79ef0d468bSGreg Roach    }
80ef0d468bSGreg Roach
81ef0d468bSGreg Roach    /**
82ef0d468bSGreg Roach     * Process <Title>
8383cdc021SGreg Roach     *
8483cdc021SGreg Roach     * @return void
85ef0d468bSGreg Roach     */
863b3cfeeaSGreg Roach    protected function titleStartHandler(): void
87c1010edaSGreg Roach    {
88ef0d468bSGreg Roach        $this->text = '';
89ef0d468bSGreg Roach    }
90ef0d468bSGreg Roach
91ef0d468bSGreg Roach    /**
92ef0d468bSGreg Roach     * Process </Title>
9383cdc021SGreg Roach     *
9483cdc021SGreg Roach     * @return void
95ef0d468bSGreg Roach     */
963b3cfeeaSGreg Roach    protected function titleEndHandler(): void
97c1010edaSGreg Roach    {
98ef0d468bSGreg Roach        $this->data['title'] = $this->text;
9947fa3e15SGreg Roach
100ef0d468bSGreg Roach        $this->text = '';
101ef0d468bSGreg Roach    }
102ef0d468bSGreg Roach
103ef0d468bSGreg Roach    /**
104ef0d468bSGreg Roach     * Process </Description>
10583cdc021SGreg Roach     *
10683cdc021SGreg Roach     * @return void
107ef0d468bSGreg Roach     */
1083b3cfeeaSGreg Roach    protected function descriptionEndHandler(): void
109c1010edaSGreg Roach    {
110ef0d468bSGreg Roach        $this->data['description'] = $this->text;
11147fa3e15SGreg Roach
112ef0d468bSGreg Roach        $this->text = '';
113ef0d468bSGreg Roach    }
114ef0d468bSGreg Roach
115ef0d468bSGreg Roach    /**
116ef0d468bSGreg Roach     * Process <Input>
117ef0d468bSGreg Roach     *
118ef0d468bSGreg Roach     * @param string[] $attrs
11983cdc021SGreg Roach     *
12083cdc021SGreg Roach     * @return void
121ef0d468bSGreg Roach     */
1223b3cfeeaSGreg Roach    protected function inputStartHandler(array $attrs): void
123c1010edaSGreg Roach    {
124ef0d468bSGreg Roach        $this->text  = '';
12513abd6f3SGreg Roach        $this->input = [
126ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
127ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
128ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
129ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
130ef0d468bSGreg Roach            'default' => '',
131ef0d468bSGreg Roach            'value'   => '',
13213abd6f3SGreg Roach        ];
133ef0d468bSGreg Roach
134ef0d468bSGreg Roach        if (isset($attrs['default'])) {
135ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
136358abf11SGreg Roach                $date = Carbon::now();
137358abf11SGreg Roach                $this->input['default'] = strtoupper($date->format('d M Y'));
138ef0d468bSGreg Roach            } else {
13913abd6f3SGreg Roach                $match = [];
140ad98d39dSGreg Roach                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
141358abf11SGreg Roach                    $date = Carbon::now()->addDays((int) $match[1]);
142358abf11SGreg Roach                    $this->input['default'] = strtoupper($date->format('d M Y'));
143ef0d468bSGreg Roach                } else {
144ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
145ef0d468bSGreg Roach                }
146ef0d468bSGreg Roach            }
1474d314e6bSGreg Roach        } elseif ($attrs['name'] === 'pageSize') {
1484d314e6bSGreg Roach            $this->input['default'] = app(LocaleInterface::class)->territory()->paperSize();
149ef0d468bSGreg Roach        }
150ef0d468bSGreg Roach    }
151ef0d468bSGreg Roach
152ef0d468bSGreg Roach    /**
153ef0d468bSGreg Roach     * Process </Input>
15483cdc021SGreg Roach     *
15583cdc021SGreg Roach     * @return void
156ef0d468bSGreg Roach     */
1573b3cfeeaSGreg Roach    protected function inputEndHandler(): void
158c1010edaSGreg Roach    {
159ef0d468bSGreg Roach        $this->input['value'] = $this->text;
160ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
16113abd6f3SGreg Roach            $this->data['inputs'] = [];
162ef0d468bSGreg Roach        }
163ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
164ef0d468bSGreg Roach        $this->text             = '';
165ef0d468bSGreg Roach    }
166ef0d468bSGreg Roach}
167