xref: /webtrees/app/Report/ReportParserSetup.php (revision a4956c0ee6be68ee78d69ce5b6f04290a58ad08f)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 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\Date;
20use Fisharebest\Webtrees\I18N;
21
22/**
23 * Class ReportParserSetup - parse a report.xml file and extract the setup options.
24 */
25class ReportParserSetup extends ReportParserBase {
26	/** @var array An array of report options/parameters */
27	private $data = array();
28
29	/** @var string[] An array of input attributes */
30	private $input;
31
32	/**
33	 * Return the parsed data.
34	 *
35	 * @return array
36	 */
37	public function reportProperties() {
38		return $this->data;
39	}
40
41	/**
42	 * Process <Report>
43	 *
44	 * @param string[] $attrs
45	 */
46	protected function reportStartHandler($attrs) {
47		$access = Auth::PRIV_PRIVATE;
48		if (isset($attrs['access'])) {
49			if (isset($$attrs["access"])) {
50				$access = $$attrs["access"];
51			}
52		}
53		$this->data['access'] = $access;
54
55		if (isset($attrs['icon'])) {
56			$this->data['icon'] = $attrs['icon'];
57		} else {
58			$this->data['icon'] = '';
59		}
60	}
61
62	/**
63	 * Process <var var="">
64	 *
65	 * @param string[] $attrs
66	 */
67	protected function varStartHandler($attrs) {
68		if (preg_match('/^I18N::number\((.+)\)$/', $attrs['var'], $match)) {
69			$this->text .=  I18N::number($match[1]);
70		} elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $attrs['var'], $match)) {
71			$this->text .=  I18N::translate($match[1]);
72		} elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $attrs['var'], $match)) {
73			$this->text .=  I18N::translateContext($match[1], $match[2]);
74		} else {
75			$this->text .= $attrs['var'];
76		}
77	}
78
79	/**
80	 * Process <Title>
81	 */
82	protected function titleStartHandler() {
83		$this->text = '';
84	}
85
86	/**
87	 * Process </Title>
88	 */
89	protected function titleEndHandler() {
90		$this->data['title'] = $this->text;
91		$this->text          = '';
92	}
93
94	/**
95	 * Process </Description>
96	 */
97	protected function descriptionEndHandler() {
98		$this->data['description'] = $this->text;
99		$this->text                = '';
100	}
101
102	/**
103	 * Process <Input>
104	 *
105	 * @param string[] $attrs
106	 */
107	protected function inputStartHandler($attrs) {
108		$this->text  = '';
109		$this->input = array(
110			'name'    => isset($attrs['name']) ? $attrs['name'] : '',
111			'type'    => isset($attrs['type']) ? $attrs['type'] : '',
112			'lookup'  => isset($attrs['lookup']) ? $attrs['lookup'] : '',
113			'options' => isset($attrs['options']) ? $attrs['options'] : '',
114			'default' => '',
115			'value'   => '',
116		);
117
118		if (isset($attrs['default'])) {
119			if ($attrs['default'] === 'NOW') {
120				$this->input['default'] = date('d M Y');
121			} else {
122				$match = array();
123				if (preg_match('/NOW\s*([+\-])\s*(\d+)/', $attrs['default'], $match) > 0) {
124					$plus = 1;
125					if ($match[1] === '-') {
126						$plus = -1;
127					}
128					$this->input['default'] = date('d M Y', WT_TIMESTAMP + $plus * 60 * 60 * 24 * $match[2]);
129				} else {
130					$this->input['default'] = $attrs['default'];
131				}
132			}
133		}
134	}
135
136	/**
137	 * Process </Input>
138	 */
139	protected function inputEndHandler() {
140		$this->input['value'] = $this->text;
141		if (!isset($this->data['inputs'])) {
142			$this->data['inputs'] = array();
143		}
144		$this->data['inputs'][] = $this->input;
145		$this->text             = '';
146	}
147}
148