xref: /webtrees/app/Report/ReportParserSetup.php (revision 358abf110a17e8960ab32249732a06b610f2b352)
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
204d314e6bSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
21ef0d468bSGreg Roachuse Fisharebest\Webtrees\Auth;
224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
23ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
24ef0d468bSGreg Roach
25ef0d468bSGreg Roach/**
26a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
27ef0d468bSGreg Roach */
28c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
29c1010edaSGreg Roach{
30ef0d468bSGreg Roach    /** @var array An array of report options/parameters */
3113abd6f3SGreg Roach    private $data = [];
32ef0d468bSGreg Roach
33ef0d468bSGreg Roach    /** @var string[] An array of input attributes */
34ef0d468bSGreg Roach    private $input;
35ef0d468bSGreg Roach
36ef0d468bSGreg Roach    /**
37ef0d468bSGreg Roach     * Return the parsed data.
38ef0d468bSGreg Roach     *
39ef0d468bSGreg Roach     * @return array
40ef0d468bSGreg Roach     */
418f53f488SRico Sonntag    public function reportProperties(): array
42c1010edaSGreg Roach    {
43ef0d468bSGreg Roach        return $this->data;
44ef0d468bSGreg Roach    }
45ef0d468bSGreg Roach
46ef0d468bSGreg Roach    /**
47ef0d468bSGreg Roach     * Process <Report>
48ef0d468bSGreg Roach     *
49ef0d468bSGreg Roach     * @param string[] $attrs
5083cdc021SGreg Roach     *
5183cdc021SGreg Roach     * @return void
52ef0d468bSGreg Roach     */
533b3cfeeaSGreg Roach    protected function reportStartHandler(array $attrs): void
54c1010edaSGreg Roach    {
5547fa3e15SGreg Roach        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
56ef0d468bSGreg Roach
5747fa3e15SGreg Roach        $this->data['icon'] = $attrs['icon'] ?? '';
58ef0d468bSGreg Roach    }
59ef0d468bSGreg Roach
60ef0d468bSGreg Roach    /**
61ef0d468bSGreg Roach     * Process <var var="">
62ef0d468bSGreg Roach     *
63ef0d468bSGreg Roach     * @param string[] $attrs
6483cdc021SGreg Roach     *
6583cdc021SGreg Roach     * @return void
66ef0d468bSGreg Roach     */
673b3cfeeaSGreg Roach    protected function varStartHandler(array $attrs): void
68c1010edaSGreg Roach    {
69ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
70da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
71ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
72ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
73a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
74ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
75ef0d468bSGreg Roach        } else {
76ef0d468bSGreg Roach            $this->text .= $attrs['var'];
77ef0d468bSGreg Roach        }
78ef0d468bSGreg Roach    }
79ef0d468bSGreg Roach
80ef0d468bSGreg Roach    /**
81ef0d468bSGreg Roach     * Process <Title>
8283cdc021SGreg Roach     *
8383cdc021SGreg Roach     * @return void
84ef0d468bSGreg Roach     */
853b3cfeeaSGreg Roach    protected function titleStartHandler(): void
86c1010edaSGreg Roach    {
87ef0d468bSGreg Roach        $this->text = '';
88ef0d468bSGreg Roach    }
89ef0d468bSGreg Roach
90ef0d468bSGreg Roach    /**
91ef0d468bSGreg Roach     * Process </Title>
9283cdc021SGreg Roach     *
9383cdc021SGreg Roach     * @return void
94ef0d468bSGreg Roach     */
953b3cfeeaSGreg Roach    protected function titleEndHandler(): void
96c1010edaSGreg Roach    {
97ef0d468bSGreg Roach        $this->data['title'] = $this->text;
9847fa3e15SGreg Roach
99ef0d468bSGreg Roach        $this->text = '';
100ef0d468bSGreg Roach    }
101ef0d468bSGreg Roach
102ef0d468bSGreg Roach    /**
103ef0d468bSGreg Roach     * Process </Description>
10483cdc021SGreg Roach     *
10583cdc021SGreg Roach     * @return void
106ef0d468bSGreg Roach     */
1073b3cfeeaSGreg Roach    protected function descriptionEndHandler(): void
108c1010edaSGreg Roach    {
109ef0d468bSGreg Roach        $this->data['description'] = $this->text;
11047fa3e15SGreg Roach
111ef0d468bSGreg Roach        $this->text = '';
112ef0d468bSGreg Roach    }
113ef0d468bSGreg Roach
114ef0d468bSGreg Roach    /**
115ef0d468bSGreg Roach     * Process <Input>
116ef0d468bSGreg Roach     *
117ef0d468bSGreg Roach     * @param string[] $attrs
11883cdc021SGreg Roach     *
11983cdc021SGreg Roach     * @return void
120ef0d468bSGreg Roach     */
1213b3cfeeaSGreg Roach    protected function inputStartHandler(array $attrs): void
122c1010edaSGreg Roach    {
123ef0d468bSGreg Roach        $this->text  = '';
12413abd6f3SGreg Roach        $this->input = [
125ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
126ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
127ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
128ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
129ef0d468bSGreg Roach            'default' => '',
130ef0d468bSGreg Roach            'value'   => '',
13113abd6f3SGreg Roach        ];
132ef0d468bSGreg Roach
133ef0d468bSGreg Roach        if (isset($attrs['default'])) {
134ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
135*358abf11SGreg Roach                $date = Carbon::now();
136*358abf11SGreg Roach                $this->input['default'] = strtoupper($date->format('d M Y'));
137ef0d468bSGreg Roach            } else {
13813abd6f3SGreg Roach                $match = [];
139ad98d39dSGreg Roach                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
140*358abf11SGreg Roach                    $date = Carbon::now()->addDays((int) $match[1]);
141*358abf11SGreg Roach                    $this->input['default'] = strtoupper($date->format('d M Y'));
142ef0d468bSGreg Roach                } else {
143ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
144ef0d468bSGreg Roach                }
145ef0d468bSGreg Roach            }
1464d314e6bSGreg Roach        } elseif ($attrs['name'] === 'pageSize') {
1474d314e6bSGreg Roach            $this->input['default'] = app(LocaleInterface::class)->territory()->paperSize();
148ef0d468bSGreg Roach        }
149ef0d468bSGreg Roach    }
150ef0d468bSGreg Roach
151ef0d468bSGreg Roach    /**
152ef0d468bSGreg Roach     * Process </Input>
15383cdc021SGreg Roach     *
15483cdc021SGreg Roach     * @return void
155ef0d468bSGreg Roach     */
1563b3cfeeaSGreg Roach    protected function inputEndHandler(): void
157c1010edaSGreg Roach    {
158ef0d468bSGreg Roach        $this->input['value'] = $this->text;
159ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
16013abd6f3SGreg Roach            $this->data['inputs'] = [];
161ef0d468bSGreg Roach        }
162ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
163ef0d468bSGreg Roach        $this->text             = '';
164ef0d468bSGreg Roach    }
165ef0d468bSGreg Roach}
166