xref: /webtrees/app/Report/ReportParserSetup.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
1ef0d468bSGreg Roach<?php
23976b470SGreg 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 */
17*fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
224d314e6bSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
23ef0d468bSGreg Roachuse Fisharebest\Webtrees\Auth;
244459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
25ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
26ef0d468bSGreg Roach
27ef0d468bSGreg Roach/**
28a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
29ef0d468bSGreg Roach */
30c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
31c1010edaSGreg Roach{
32ef0d468bSGreg Roach    /** @var array An array of report options/parameters */
3313abd6f3SGreg Roach    private $data = [];
34ef0d468bSGreg Roach
35ef0d468bSGreg Roach    /** @var string[] An array of input attributes */
36ef0d468bSGreg Roach    private $input;
37ef0d468bSGreg Roach
38ef0d468bSGreg Roach    /**
39ef0d468bSGreg Roach     * Return the parsed data.
40ef0d468bSGreg Roach     *
41ef0d468bSGreg Roach     * @return array
42ef0d468bSGreg Roach     */
438f53f488SRico Sonntag    public function reportProperties(): array
44c1010edaSGreg Roach    {
45ef0d468bSGreg Roach        return $this->data;
46ef0d468bSGreg Roach    }
47ef0d468bSGreg Roach
48ef0d468bSGreg Roach    /**
49ef0d468bSGreg Roach     * Process <Report>
50ef0d468bSGreg Roach     *
51ef0d468bSGreg Roach     * @param string[] $attrs
5283cdc021SGreg Roach     *
5383cdc021SGreg Roach     * @return void
54ef0d468bSGreg Roach     */
553b3cfeeaSGreg Roach    protected function reportStartHandler(array $attrs): void
56c1010edaSGreg Roach    {
5747fa3e15SGreg Roach        $this->data['access'] = $attrs['access'] ?? Auth::PRIV_PRIVATE;
58ef0d468bSGreg Roach
5947fa3e15SGreg Roach        $this->data['icon'] = $attrs['icon'] ?? '';
60ef0d468bSGreg Roach    }
61ef0d468bSGreg Roach
62ef0d468bSGreg Roach    /**
63ef0d468bSGreg Roach     * Process <var var="">
64ef0d468bSGreg Roach     *
65ef0d468bSGreg Roach     * @param string[] $attrs
6683cdc021SGreg Roach     *
6783cdc021SGreg Roach     * @return void
68ef0d468bSGreg Roach     */
693b3cfeeaSGreg Roach    protected function varStartHandler(array $attrs): void
70c1010edaSGreg Roach    {
71ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
72da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
73ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
74ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
75a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
76ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
77ef0d468bSGreg Roach        } else {
78ef0d468bSGreg Roach            $this->text .= $attrs['var'];
79ef0d468bSGreg Roach        }
80ef0d468bSGreg Roach    }
81ef0d468bSGreg Roach
82ef0d468bSGreg Roach    /**
83ef0d468bSGreg Roach     * Process <Title>
8483cdc021SGreg Roach     *
8583cdc021SGreg Roach     * @return void
86ef0d468bSGreg Roach     */
873b3cfeeaSGreg Roach    protected function titleStartHandler(): void
88c1010edaSGreg Roach    {
89ef0d468bSGreg Roach        $this->text = '';
90ef0d468bSGreg Roach    }
91ef0d468bSGreg Roach
92ef0d468bSGreg Roach    /**
93ef0d468bSGreg Roach     * Process </Title>
9483cdc021SGreg Roach     *
9583cdc021SGreg Roach     * @return void
96ef0d468bSGreg Roach     */
973b3cfeeaSGreg Roach    protected function titleEndHandler(): void
98c1010edaSGreg Roach    {
99ef0d468bSGreg Roach        $this->data['title'] = $this->text;
10047fa3e15SGreg Roach
101ef0d468bSGreg Roach        $this->text = '';
102ef0d468bSGreg Roach    }
103ef0d468bSGreg Roach
104ef0d468bSGreg Roach    /**
105ef0d468bSGreg Roach     * Process </Description>
10683cdc021SGreg Roach     *
10783cdc021SGreg Roach     * @return void
108ef0d468bSGreg Roach     */
1093b3cfeeaSGreg Roach    protected function descriptionEndHandler(): void
110c1010edaSGreg Roach    {
111ef0d468bSGreg Roach        $this->data['description'] = $this->text;
11247fa3e15SGreg Roach
113ef0d468bSGreg Roach        $this->text = '';
114ef0d468bSGreg Roach    }
115ef0d468bSGreg Roach
116ef0d468bSGreg Roach    /**
117ef0d468bSGreg Roach     * Process <Input>
118ef0d468bSGreg Roach     *
119ef0d468bSGreg Roach     * @param string[] $attrs
12083cdc021SGreg Roach     *
12183cdc021SGreg Roach     * @return void
122ef0d468bSGreg Roach     */
1233b3cfeeaSGreg Roach    protected function inputStartHandler(array $attrs): void
124c1010edaSGreg Roach    {
125ef0d468bSGreg Roach        $this->text  = '';
12613abd6f3SGreg Roach        $this->input = [
127ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
128ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
129ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
130ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
131ef0d468bSGreg Roach            'default' => '',
132ef0d468bSGreg Roach            'value'   => '',
13313abd6f3SGreg Roach        ];
134ef0d468bSGreg Roach
135ef0d468bSGreg Roach        if (isset($attrs['default'])) {
136ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
137358abf11SGreg Roach                $date = Carbon::now();
138358abf11SGreg Roach                $this->input['default'] = strtoupper($date->format('d M Y'));
139ef0d468bSGreg Roach            } else {
14013abd6f3SGreg Roach                $match = [];
141ad98d39dSGreg Roach                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
142358abf11SGreg Roach                    $date = Carbon::now()->addDays((int) $match[1]);
143358abf11SGreg Roach                    $this->input['default'] = strtoupper($date->format('d M Y'));
144ef0d468bSGreg Roach                } else {
145ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
146ef0d468bSGreg Roach                }
147ef0d468bSGreg Roach            }
1484d314e6bSGreg Roach        } elseif ($attrs['name'] === 'pageSize') {
1494d314e6bSGreg Roach            $this->input['default'] = app(LocaleInterface::class)->territory()->paperSize();
150ef0d468bSGreg Roach        }
151ef0d468bSGreg Roach    }
152ef0d468bSGreg Roach
153ef0d468bSGreg Roach    /**
154ef0d468bSGreg Roach     * Process </Input>
15583cdc021SGreg Roach     *
15683cdc021SGreg Roach     * @return void
157ef0d468bSGreg Roach     */
1583b3cfeeaSGreg Roach    protected function inputEndHandler(): void
159c1010edaSGreg Roach    {
160ef0d468bSGreg Roach        $this->input['value'] = $this->text;
161ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
16213abd6f3SGreg Roach            $this->data['inputs'] = [];
163ef0d468bSGreg Roach        }
164ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
165ef0d468bSGreg Roach        $this->text             = '';
166ef0d468bSGreg Roach    }
167ef0d468bSGreg Roach}
168