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