xref: /webtrees/app/Report/ReportParserSetup.php (revision 6bd19c8ce39244f770147102c818d9c7d9e13667)
1ef0d468bSGreg Roach<?php
23976b470SGreg Roach
3ef0d468bSGreg Roach/**
4ef0d468bSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ef0d468bSGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
22ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
23d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
24ef0d468bSGreg Roach
25b6f35a76SGreg Roachuse function preg_match;
26b6f35a76SGreg Roachuse function strtoupper;
27b6f35a76SGreg Roach
28ef0d468bSGreg Roach/**
29a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
30ef0d468bSGreg Roach */
31c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
32c1010edaSGreg Roach{
3376d39c55SGreg Roach    /** @var array<string,string|array<string>> An array of report options/parameters */
3477bab461SGreg Roach    private array $data = [];
35ef0d468bSGreg Roach
3677bab461SGreg Roach    /** @var array<string> An array of input attributes */
3777bab461SGreg Roach    private array $input;
38ef0d468bSGreg Roach
39ef0d468bSGreg Roach    /**
40ef0d468bSGreg Roach     * Return the parsed data.
41ef0d468bSGreg Roach     *
42*6bd19c8cSGreg Roach     * @return array{"title":string,"description":string,"inputs":array<array{"name":string,"type":string,"lookup":string,"options":string,"default":string,"value":string}>}
43ef0d468bSGreg Roach     */
448f53f488SRico Sonntag    public function reportProperties(): array
45c1010edaSGreg Roach    {
46ef0d468bSGreg Roach        return $this->data;
47ef0d468bSGreg Roach    }
48ef0d468bSGreg Roach
49ef0d468bSGreg Roach    /**
50fab8f067SGreg Roach     * Handle <var var="" />
51ef0d468bSGreg Roach     *
5209482a55SGreg Roach     * @param array<string> $attrs
5383cdc021SGreg Roach     *
5483cdc021SGreg Roach     * @return void
55ef0d468bSGreg Roach     */
563b3cfeeaSGreg Roach    protected function varStartHandler(array $attrs): void
57c1010edaSGreg Roach    {
58ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
59da46f7cdSGreg Roach            $this->text .= I18N::number((int) $match[1]);
60ef0d468bSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
61ef0d468bSGreg Roach            $this->text .= I18N::translate($match[1]);
62a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
63ef0d468bSGreg Roach            $this->text .= I18N::translateContext($match[1], $match[2]);
64ef0d468bSGreg Roach        } else {
65ef0d468bSGreg Roach            $this->text .= $attrs['var'];
66ef0d468bSGreg Roach        }
67ef0d468bSGreg Roach    }
68ef0d468bSGreg Roach
69ef0d468bSGreg Roach    /**
70fab8f067SGreg Roach     * Handle <title>
7183cdc021SGreg Roach     *
7283cdc021SGreg Roach     * @return void
73ef0d468bSGreg Roach     */
743b3cfeeaSGreg Roach    protected function titleStartHandler(): void
75c1010edaSGreg Roach    {
76ef0d468bSGreg Roach        $this->text = '';
77ef0d468bSGreg Roach    }
78ef0d468bSGreg Roach
79ef0d468bSGreg Roach    /**
80fab8f067SGreg Roach     * Handle </title>
8183cdc021SGreg Roach     *
8283cdc021SGreg Roach     * @return void
83ef0d468bSGreg Roach     */
843b3cfeeaSGreg Roach    protected function titleEndHandler(): void
85c1010edaSGreg Roach    {
86ef0d468bSGreg Roach        $this->data['title'] = $this->text;
8747fa3e15SGreg Roach
88ef0d468bSGreg Roach        $this->text = '';
89ef0d468bSGreg Roach    }
90ef0d468bSGreg Roach
91ef0d468bSGreg Roach    /**
92fab8f067SGreg Roach     * Handle </description>
9383cdc021SGreg Roach     *
9483cdc021SGreg Roach     * @return void
95ef0d468bSGreg Roach     */
963b3cfeeaSGreg Roach    protected function descriptionEndHandler(): void
97c1010edaSGreg Roach    {
98ef0d468bSGreg Roach        $this->data['description'] = $this->text;
9947fa3e15SGreg Roach
100ef0d468bSGreg Roach        $this->text = '';
101ef0d468bSGreg Roach    }
102ef0d468bSGreg Roach
103ef0d468bSGreg Roach    /**
104fab8f067SGreg Roach     * Handle <input>
105ef0d468bSGreg Roach     *
10609482a55SGreg Roach     * @param array<string> $attrs
10783cdc021SGreg Roach     *
10883cdc021SGreg Roach     * @return void
109ef0d468bSGreg Roach     */
1103b3cfeeaSGreg Roach    protected function inputStartHandler(array $attrs): void
111c1010edaSGreg Roach    {
112ef0d468bSGreg Roach        $this->text  = '';
11313abd6f3SGreg Roach        $this->input = [
114ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
115ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
116ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
117ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
118ef0d468bSGreg Roach            'default' => '',
119ef0d468bSGreg Roach            'value'   => '',
12013abd6f3SGreg Roach        ];
121ef0d468bSGreg Roach
122ef0d468bSGreg Roach        if (isset($attrs['default'])) {
123ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
124d97083feSGreg Roach                $date = Registry::timestampFactory()->now();
125358abf11SGreg Roach                $this->input['default'] = strtoupper($date->format('d M Y'));
126ef0d468bSGreg Roach            } else {
12713abd6f3SGreg Roach                $match = [];
128ad98d39dSGreg Roach                if (preg_match('/NOW([+\-]\d+)/', $attrs['default'], $match) > 0) {
129d97083feSGreg Roach                    $date = Registry::timestampFactory()->now()->addDays((int)$match[1]);
130358abf11SGreg Roach                    $this->input['default'] = strtoupper($date->format('d M Y'));
131ef0d468bSGreg Roach                } else {
132ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
133ef0d468bSGreg Roach                }
134ef0d468bSGreg Roach            }
1354d314e6bSGreg Roach        } elseif ($attrs['name'] === 'pageSize') {
13665cf5706SGreg Roach            $this->input['default'] = I18N::locale()->territory()->paperSize();
137ef0d468bSGreg Roach        }
138ef0d468bSGreg Roach    }
139ef0d468bSGreg Roach
140ef0d468bSGreg Roach    /**
141fab8f067SGreg Roach     * Handle </input>
14283cdc021SGreg Roach     *
14383cdc021SGreg Roach     * @return void
144ef0d468bSGreg Roach     */
1453b3cfeeaSGreg Roach    protected function inputEndHandler(): void
146c1010edaSGreg Roach    {
147ef0d468bSGreg Roach        $this->input['value'] = $this->text;
148ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
14913abd6f3SGreg Roach            $this->data['inputs'] = [];
150ef0d468bSGreg Roach        }
151ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
152ef0d468bSGreg Roach        $this->text             = '';
153ef0d468bSGreg Roach    }
154ef0d468bSGreg Roach}
155