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