xref: /webtrees/app/Report/ReportParserSetup.php (revision 7a6ee1ac7cd0821562d35b111a96b219b46d7899)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Report;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\I18N;
20
21/**
22 * Class ReportParserSetup - parse a report.xml file and extract the setup options.
23 */
24class ReportParserSetup extends ReportParserBase {
25	/** @var array An array of report options/parameters */
26	private $data = [];
27
28	/** @var string[] An array of input attributes */
29	private $input;
30
31	/**
32	 * Return the parsed data.
33	 *
34	 * @return array
35	 */
36	public function reportProperties() {
37		return $this->data;
38	}
39
40	/**
41	 * Process <Report>
42	 *
43	 * @param string[] $attrs
44	 */
45	protected function reportStartHandler($attrs) {
46		$access = Auth::PRIV_PRIVATE;
47		if (isset($attrs['access'])) {
48			if (isset($$attrs['access'])) {
49				$access = $$attrs['access'];
50			}
51		}
52		$this->data['access'] = $access;
53
54		if (isset($attrs['icon'])) {
55			$this->data['icon'] = $attrs['icon'];
56		} else {
57			$this->data['icon'] = '';
58		}
59	}
60
61	/**
62	 * Process <var var="">
63	 *
64	 * @param string[] $attrs
65	 */
66	protected function varStartHandler($attrs) {
67		if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
68			$this->text .= I18N::number($match[1]);
69		} elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
70			$this->text .= I18N::translate($match[1]);
71		} elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
72			$this->text .= I18N::translateContext($match[1], $match[2]);
73		} else {
74			$this->text .= $attrs['var'];
75		}
76	}
77
78	/**
79	 * Process <Title>
80	 */
81	protected function titleStartHandler() {
82		$this->text = '';
83	}
84
85	/**
86	 * Process </Title>
87	 */
88	protected function titleEndHandler() {
89		$this->data['title'] = $this->text;
90		$this->text          = '';
91	}
92
93	/**
94	 * Process </Description>
95	 */
96	protected function descriptionEndHandler() {
97		$this->data['description'] = $this->text;
98		$this->text                = '';
99	}
100
101	/**
102	 * Process <Input>
103	 *
104	 * @param string[] $attrs
105	 */
106	protected function inputStartHandler($attrs) {
107		$this->text  = '';
108		$this->input = [
109			'name'    => isset($attrs['name']) ? $attrs['name'] : '',
110			'type'    => isset($attrs['type']) ? $attrs['type'] : '',
111			'lookup'  => isset($attrs['lookup']) ? $attrs['lookup'] : '',
112			'options' => isset($attrs['options']) ? $attrs['options'] : '',
113			'default' => '',
114			'value'   => '',
115		];
116
117		if (isset($attrs['default'])) {
118			if ($attrs['default'] === 'NOW') {
119				$this->input['default'] = date('d M Y');
120			} else {
121				$match = [];
122				if (preg_match('/NOW\s*([+\-])\s*(\d+)/', $attrs['default'], $match) > 0) {
123					$plus = 1;
124					if ($match[1] === '-') {
125						$plus = -1;
126					}
127					$this->input['default'] = date('d M Y', WT_TIMESTAMP + $plus * 60 * 60 * 24 * $match[2]);
128				} else {
129					$this->input['default'] = $attrs['default'];
130				}
131			}
132		}
133	}
134
135	/**
136	 * Process </Input>
137	 */
138	protected function inputEndHandler() {
139		$this->input['value'] = $this->text;
140		if (!isset($this->data['inputs'])) {
141			$this->data['inputs'] = [];
142		}
143		$this->data['inputs'][] = $this->input;
144		$this->text             = '';
145	}
146}
147