xref: /webtrees/app/Report/ReportParserSetup.php (revision ff61e1a350c2fd27db8be2597d422fd4dfecc71f)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
1776692c8bSGreg Roach
18ef0d468bSGreg Roachuse Fisharebest\Webtrees\Auth;
19ef0d468bSGreg Roachuse Fisharebest\Webtrees\I18N;
20ef0d468bSGreg Roach
21ef0d468bSGreg Roach/**
22a6f13a4aSGreg Roach * Class ReportParserSetup - parse a report.xml file and extract the setup options.
23ef0d468bSGreg Roach */
24c1010edaSGreg Roachclass ReportParserSetup extends ReportParserBase
25c1010edaSGreg Roach{
26ef0d468bSGreg Roach    /** @var array An array of report options/parameters */
2713abd6f3SGreg Roach    private $data = [];
28ef0d468bSGreg Roach
29ef0d468bSGreg Roach    /** @var string[] An array of input attributes */
30ef0d468bSGreg Roach    private $input;
31ef0d468bSGreg Roach
32ef0d468bSGreg Roach    /**
33ef0d468bSGreg Roach     * Return the parsed data.
34ef0d468bSGreg Roach     *
35ef0d468bSGreg Roach     * @return array
36ef0d468bSGreg Roach     */
37c1010edaSGreg Roach    public function reportProperties()
38c1010edaSGreg Roach    {
39ef0d468bSGreg Roach        return $this->data;
40ef0d468bSGreg Roach    }
41ef0d468bSGreg Roach
42ef0d468bSGreg Roach    /**
43ef0d468bSGreg Roach     * Process <Report>
44ef0d468bSGreg Roach     *
45ef0d468bSGreg Roach     * @param string[] $attrs
46ef0d468bSGreg Roach     */
47c1010edaSGreg Roach    protected function reportStartHandler($attrs)
48c1010edaSGreg Roach    {
49ef0d468bSGreg Roach        $access = Auth::PRIV_PRIVATE;
50ef0d468bSGreg Roach        if (isset($attrs['access'])) {
517a6ee1acSGreg Roach            if (isset($$attrs['access'])) {
527a6ee1acSGreg Roach                $access = $$attrs['access'];
53ef0d468bSGreg Roach            }
54ef0d468bSGreg Roach        }
55ef0d468bSGreg Roach        $this->data['access'] = $access;
56ef0d468bSGreg Roach
57ef0d468bSGreg Roach        if (isset($attrs['icon'])) {
58ef0d468bSGreg Roach            $this->data['icon'] = $attrs['icon'];
59ef0d468bSGreg Roach        } else {
60ef0d468bSGreg Roach            $this->data['icon'] = '';
61ef0d468bSGreg Roach        }
62ef0d468bSGreg Roach    }
63ef0d468bSGreg Roach
64ef0d468bSGreg Roach    /**
65ef0d468bSGreg Roach     * Process <var var="">
66ef0d468bSGreg Roach     *
67ef0d468bSGreg Roach     * @param string[] $attrs
68ef0d468bSGreg Roach     */
69c1010edaSGreg Roach    protected function varStartHandler($attrs)
70c1010edaSGreg Roach    {
71ef0d468bSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
72ef0d468bSGreg Roach            $this->text .= I18N::number($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>
84ef0d468bSGreg Roach     */
85c1010edaSGreg Roach    protected function titleStartHandler()
86c1010edaSGreg Roach    {
87ef0d468bSGreg Roach        $this->text = '';
88ef0d468bSGreg Roach    }
89ef0d468bSGreg Roach
90ef0d468bSGreg Roach    /**
91ef0d468bSGreg Roach     * Process </Title>
92ef0d468bSGreg Roach     */
93c1010edaSGreg Roach    protected function titleEndHandler()
94c1010edaSGreg Roach    {
95ef0d468bSGreg Roach        $this->data['title'] = $this->text;
96ef0d468bSGreg Roach        $this->text          = '';
97ef0d468bSGreg Roach    }
98ef0d468bSGreg Roach
99ef0d468bSGreg Roach    /**
100ef0d468bSGreg Roach     * Process </Description>
101ef0d468bSGreg Roach     */
102c1010edaSGreg Roach    protected function descriptionEndHandler()
103c1010edaSGreg Roach    {
104ef0d468bSGreg Roach        $this->data['description'] = $this->text;
105ef0d468bSGreg Roach        $this->text                = '';
106ef0d468bSGreg Roach    }
107ef0d468bSGreg Roach
108ef0d468bSGreg Roach    /**
109ef0d468bSGreg Roach     * Process <Input>
110ef0d468bSGreg Roach     *
111ef0d468bSGreg Roach     * @param string[] $attrs
112ef0d468bSGreg Roach     */
113c1010edaSGreg Roach    protected function inputStartHandler($attrs)
114c1010edaSGreg Roach    {
115ef0d468bSGreg Roach        $this->text  = '';
11613abd6f3SGreg Roach        $this->input = [
117*ff61e1a3SGreg Roach            'name'    => $attrs['name'] ?? '',
118*ff61e1a3SGreg Roach            'type'    => $attrs['type'] ?? '',
119*ff61e1a3SGreg Roach            'lookup'  => $attrs['lookup'] ?? '',
120*ff61e1a3SGreg Roach            'options' => $attrs['options'] ?? '',
121ef0d468bSGreg Roach            'default' => '',
122ef0d468bSGreg Roach            'value'   => '',
12313abd6f3SGreg Roach        ];
124ef0d468bSGreg Roach
125ef0d468bSGreg Roach        if (isset($attrs['default'])) {
126ef0d468bSGreg Roach            if ($attrs['default'] === 'NOW') {
127ef0d468bSGreg Roach                $this->input['default'] = date('d M Y');
128ef0d468bSGreg Roach            } else {
12913abd6f3SGreg Roach                $match = [];
130ef0d468bSGreg Roach                if (preg_match('/NOW\s*([+\-])\s*(\d+)/', $attrs['default'], $match) > 0) {
131ef0d468bSGreg Roach                    $plus = 1;
132ef0d468bSGreg Roach                    if ($match[1] === '-') {
133ef0d468bSGreg Roach                        $plus = -1;
134ef0d468bSGreg Roach                    }
135ef0d468bSGreg Roach                    $this->input['default'] = date('d M Y', WT_TIMESTAMP + $plus * 60 * 60 * 24 * $match[2]);
136ef0d468bSGreg Roach                } else {
137ef0d468bSGreg Roach                    $this->input['default'] = $attrs['default'];
138ef0d468bSGreg Roach                }
139ef0d468bSGreg Roach            }
140ef0d468bSGreg Roach        }
141ef0d468bSGreg Roach    }
142ef0d468bSGreg Roach
143ef0d468bSGreg Roach    /**
144ef0d468bSGreg Roach     * Process </Input>
145ef0d468bSGreg Roach     */
146c1010edaSGreg Roach    protected function inputEndHandler()
147c1010edaSGreg Roach    {
148ef0d468bSGreg Roach        $this->input['value'] = $this->text;
149ef0d468bSGreg Roach        if (!isset($this->data['inputs'])) {
15013abd6f3SGreg Roach            $this->data['inputs'] = [];
151ef0d468bSGreg Roach        }
152ef0d468bSGreg Roach        $this->data['inputs'][] = $this->input;
153ef0d468bSGreg Roach        $this->text             = '';
154ef0d468bSGreg Roach    }
155ef0d468bSGreg Roach}
156