xref: /webtrees/app/Report/ReportParserGenerate.php (revision 299d100d2d85cd31f10efd927570655dd165b510)
1a6f13a4aSGreg Roach<?php
2a6f13a4aSGreg Roach/**
3a6f13a4aSGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a6f13a4aSGreg Roach * This program is free software: you can redistribute it and/or modify
6a6f13a4aSGreg Roach * it under the terms of the GNU General Public License as published by
7a6f13a4aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a6f13a4aSGreg Roach * (at your option) any later version.
9a6f13a4aSGreg Roach * This program is distributed in the hope that it will be useful,
10a6f13a4aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a6f13a4aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a6f13a4aSGreg Roach * GNU General Public License for more details.
13a6f13a4aSGreg Roach * You should have received a copy of the GNU General Public License
14a6f13a4aSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a6f13a4aSGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
1776692c8bSGreg Roach
18a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
19a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Database;
20a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
21a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
22a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
25a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
26a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
28a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
29d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Media;
31729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
33*299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
34a6f13a4aSGreg Roach
35a6f13a4aSGreg Roach/**
36a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
37a6f13a4aSGreg Roach */
38a6f13a4aSGreg Roachclass ReportParserGenerate extends ReportParserBase {
39a6f13a4aSGreg Roach	/** @var bool Are we collecting data from <Footnote> elements  */
40a6f13a4aSGreg Roach	private $process_footnote = true;
41a6f13a4aSGreg Roach
42a6f13a4aSGreg Roach	/** @var bool Are we currently outputing data? */
43a6f13a4aSGreg Roach	private $print_data = false;
44a6f13a4aSGreg Roach
45a6f13a4aSGreg Roach	/** @var bool[] Push-down stack of $print_data */
4613abd6f3SGreg Roach	private $print_data_stack = [];
47a6f13a4aSGreg Roach
4876692c8bSGreg Roach	/** @var int Are we processing GEDCOM data */
49a6f13a4aSGreg Roach	private $process_gedcoms = 0;
50a6f13a4aSGreg Roach
5176692c8bSGreg Roach	/** @var int Are we processing conditionals */
52a6f13a4aSGreg Roach	private $process_ifs = 0;
53a6f13a4aSGreg Roach
5476692c8bSGreg Roach	/** @var int Are we processing repeats*/
55a6f13a4aSGreg Roach	private $process_repeats = 0;
56a6f13a4aSGreg Roach
57a6f13a4aSGreg Roach	/** @var int Quantity of data to repeat during loops */
58a6f13a4aSGreg Roach	private $repeat_bytes = 0;
59a6f13a4aSGreg Roach
60a6f13a4aSGreg Roach	/** @var array[] Repeated data when iterating over loops */
6113abd6f3SGreg Roach	private $repeats = [];
62a6f13a4aSGreg Roach
63a6f13a4aSGreg Roach	/** @var array[] Nested repeating data */
6413abd6f3SGreg Roach	private $repeats_stack = [];
65a6f13a4aSGreg Roach
66e8e7866bSGreg Roach	/** @var ReportBase[] Nested repeating data */
6713abd6f3SGreg Roach	private $wt_report_stack = [];
68e8e7866bSGreg Roach
69e8e7866bSGreg Roach	/** @var resource Nested repeating data */
70e8e7866bSGreg Roach	private $parser;
71e8e7866bSGreg Roach
72e8e7866bSGreg Roach	/** @var resource[] Nested repeating data */
7313abd6f3SGreg Roach	private $parser_stack = [];
74e8e7866bSGreg Roach
75a6f13a4aSGreg Roach	/** @var string The current GEDCOM record */
76a6f13a4aSGreg Roach	private $gedrec = '';
77a6f13a4aSGreg Roach
78a6f13a4aSGreg Roach	/** @var string[] Nested GEDCOM records */
7913abd6f3SGreg Roach	private $gedrec_stack = [];
80a6f13a4aSGreg Roach
81a6f13a4aSGreg Roach	/** @var ReportBaseElement The currently processed element */
82a6f13a4aSGreg Roach	private $current_element;
83a6f13a4aSGreg Roach
84a6f13a4aSGreg Roach	/** @var ReportBaseElement The currently processed element */
85a6f13a4aSGreg Roach	private $footnote_element;
86a6f13a4aSGreg Roach
87a6f13a4aSGreg Roach	/** @var string The GEDCOM fact currently being processed */
88a6f13a4aSGreg Roach	private $fact = '';
89a6f13a4aSGreg Roach
90a6f13a4aSGreg Roach	/** @var string The GEDCOM value currently being processed */
91a6f13a4aSGreg Roach	private $desc = '';
92a6f13a4aSGreg Roach
93a6f13a4aSGreg Roach	/** @var string The GEDCOM type currently being processed */
94a6f13a4aSGreg Roach	private $type = '';
95a6f13a4aSGreg Roach
96a6f13a4aSGreg Roach	/** @var int The current generational level */
97a6f13a4aSGreg Roach	private $generation = 1;
98a6f13a4aSGreg Roach
99a6f13a4aSGreg Roach	/** @var array Source data for processing lists */
10013abd6f3SGreg Roach	private $list = [];
101a6f13a4aSGreg Roach
102a6f13a4aSGreg Roach	/** @var int Number of items in lists */
103a6f13a4aSGreg Roach	private $list_total = 0;
104a6f13a4aSGreg Roach
105a6f13a4aSGreg Roach	/** @var int Number of items filtered from lists */
106a6f13a4aSGreg Roach	private $list_private = 0;
107a6f13a4aSGreg Roach
108*299d100dSGreg Roach	/** @var string The filename of the XML report */
109*299d100dSGreg Roach	protected $report;
110*299d100dSGreg Roach
111e8e7866bSGreg Roach	/** @var ReportBase A factory for creating report elements */
112e8e7866bSGreg Roach	private $report_root;
113e8e7866bSGreg Roach
114e8e7866bSGreg Roach	/** @var ReportBase Nested report elements */
115e8e7866bSGreg Roach	private $wt_report;
116e8e7866bSGreg Roach
117d1286247SGreg Roach	/** @var string[][] Variables defined in the report at run-time */
1182118c0e3SGreg Roach	private $vars;
119d1286247SGreg Roach
120*299d100dSGreg Roach	/** @var Tree The current tree */
121*299d100dSGreg Roach	private $tree;
122*299d100dSGreg Roach
12376692c8bSGreg Roach	/**
12476692c8bSGreg Roach	 * Create a parser for a report
12576692c8bSGreg Roach	 *
12676692c8bSGreg Roach	 * @param string     $report The XML filename
12776692c8bSGreg Roach	 * @param ReportBase $report_root
12876692c8bSGreg Roach	 * @param string[][] $vars
129*299d100dSGreg Roach	 * @param Tree       $tree
13076692c8bSGreg Roach	 */
131*299d100dSGreg Roach	public function __construct($report, ReportBase $report_root, array $vars, Tree $tree) {
132*299d100dSGreg Roach		$this->report          = $report;
133e8e7866bSGreg Roach		$this->report_root     = $report_root;
134e8e7866bSGreg Roach		$this->wt_report       = $report_root;
135a6f13a4aSGreg Roach		$this->current_element = new ReportBaseElement;
136d1286247SGreg Roach		$this->vars            = $vars;
137*299d100dSGreg Roach		$this->tree            = $tree;
138*299d100dSGreg Roach
139*299d100dSGreg Roach		parent::__construct($report, $report_root, $vars, $tree);
140a6f13a4aSGreg Roach	}
141a6f13a4aSGreg Roach
142a6f13a4aSGreg Roach	/**
143a6f13a4aSGreg Roach	 * XML start element handler
144a6f13a4aSGreg Roach	 *
145a6f13a4aSGreg Roach	 * This function is called whenever a starting element is reached
146a6f13a4aSGreg Roach	 * The element handler will be called if found, otherwise it must be HTML
147a6f13a4aSGreg Roach	 *
148a6f13a4aSGreg Roach	 * @param resource $parser the resource handler for the XML parser
149a6f13a4aSGreg Roach	 * @param string   $name   the name of the XML element parsed
150a6f13a4aSGreg Roach	 * @param array    $attrs  an array of key value pairs for the attributes
151a6f13a4aSGreg Roach	 */
1528edd1043SGreg Roach	protected function startElement($parser, $name, $attrs) {
15313abd6f3SGreg Roach		$newattrs = [];
154a6f13a4aSGreg Roach
155a6f13a4aSGreg Roach		foreach ($attrs as $key => $value) {
156a6f13a4aSGreg Roach			if (preg_match("/^\\$(\w+)$/", $value, $match)) {
157d1286247SGreg Roach				if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
158d1286247SGreg Roach					$value = $this->vars[$match[1]]['id'];
159a6f13a4aSGreg Roach				}
160a6f13a4aSGreg Roach			}
161a6f13a4aSGreg Roach			$newattrs[$key] = $value;
162a6f13a4aSGreg Roach		}
163a6f13a4aSGreg Roach		$attrs = $newattrs;
1647a6ee1acSGreg Roach		if ($this->process_footnote && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag')) {
165a6f13a4aSGreg Roach			$start_method = $name . 'StartHandler';
166a6f13a4aSGreg Roach			$end_method   = $name . 'EndHandler';
167a6f13a4aSGreg Roach			if (method_exists($this, $start_method)) {
168a6f13a4aSGreg Roach				$this->$start_method($attrs);
169a6f13a4aSGreg Roach			} elseif (!method_exists($this, $end_method)) {
170a6f13a4aSGreg Roach				$this->htmlStartHandler($name, $attrs);
171a6f13a4aSGreg Roach			}
172a6f13a4aSGreg Roach		}
173a6f13a4aSGreg Roach	}
174a6f13a4aSGreg Roach
175a6f13a4aSGreg Roach	/**
176a6f13a4aSGreg Roach	 * XML end element handler
177a6f13a4aSGreg Roach	 *
178a6f13a4aSGreg Roach	 * This function is called whenever an ending element is reached
179a6f13a4aSGreg Roach	 * The element handler will be called if found, otherwise it must be HTML
180a6f13a4aSGreg Roach	 *
181a6f13a4aSGreg Roach	 * @param resource $parser the resource handler for the XML parser
182a6f13a4aSGreg Roach	 * @param string   $name   the name of the XML element parsed
183a6f13a4aSGreg Roach	 */
1848edd1043SGreg Roach	protected function endElement($parser, $name) {
1857a6ee1acSGreg Roach		if (($this->process_footnote || $name === 'Footnote') && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag' || $name === 'List' || $name === 'Relatives')) {
186a6f13a4aSGreg Roach			$start_method = $name . 'StartHandler';
187a6f13a4aSGreg Roach			$end_method   = $name . 'EndHandler';
188a6f13a4aSGreg Roach			if (method_exists($this, $end_method)) {
189a6f13a4aSGreg Roach				$this->$end_method();
190a6f13a4aSGreg Roach			} elseif (!method_exists($this, $start_method)) {
191a6f13a4aSGreg Roach				$this->htmlEndHandler($name);
192a6f13a4aSGreg Roach			}
193a6f13a4aSGreg Roach		}
194a6f13a4aSGreg Roach	}
195a6f13a4aSGreg Roach
196a6f13a4aSGreg Roach	/**
197a6f13a4aSGreg Roach	 * XML character data handler
198a6f13a4aSGreg Roach	 *
199a6f13a4aSGreg Roach	 * @param resource $parser the resource handler for the XML parser
200a6f13a4aSGreg Roach	 * @param string   $data   the name of the XML element parsed
201a6f13a4aSGreg Roach	 */
2028edd1043SGreg Roach	protected function characterData($parser, $data) {
203e8e7866bSGreg Roach		if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
204a6f13a4aSGreg Roach			$this->current_element->addText($data);
205a6f13a4aSGreg Roach		}
206a6f13a4aSGreg Roach	}
207a6f13a4aSGreg Roach
208a6f13a4aSGreg Roach	/**
20976692c8bSGreg Roach	 * XML <style>
210a6f13a4aSGreg Roach	 *
211a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
212a6f13a4aSGreg Roach	 */
2138edd1043SGreg Roach	private function styleStartHandler($attrs) {
214a6f13a4aSGreg Roach		if (empty($attrs['name'])) {
215a6f13a4aSGreg Roach			throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
216a6f13a4aSGreg Roach		}
217a6f13a4aSGreg Roach
218a6f13a4aSGreg Roach		// array Style that will be passed on
21913abd6f3SGreg Roach		$s = [];
220a6f13a4aSGreg Roach
221a6f13a4aSGreg Roach		// string Name af the style
222a6f13a4aSGreg Roach		$s['name'] = $attrs['name'];
223a6f13a4aSGreg Roach
224a6f13a4aSGreg Roach		// string Name of the DEFAULT font
225e8e7866bSGreg Roach		$s['font'] = $this->wt_report->defaultFont;
226a6f13a4aSGreg Roach		if (!empty($attrs['font'])) {
227a6f13a4aSGreg Roach			$s['font'] = $attrs['font'];
228a6f13a4aSGreg Roach		}
229a6f13a4aSGreg Roach
230a6f13a4aSGreg Roach		// int The size of the font in points
231e8e7866bSGreg Roach		$s['size'] = $this->wt_report->defaultFontSize;
232a6f13a4aSGreg Roach		if (!empty($attrs['size'])) {
233a6f13a4aSGreg Roach			$s['size'] = (int) $attrs['size'];
234a6f13a4aSGreg Roach		} // Get it as int to ignore all decimal points or text (if any text then int(0))
235a6f13a4aSGreg Roach
236a6f13a4aSGreg Roach		// string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2377a6ee1acSGreg Roach		$s['style'] = '';
238a6f13a4aSGreg Roach		if (!empty($attrs['style'])) {
239a6f13a4aSGreg Roach			$s['style'] = $attrs['style'];
240a6f13a4aSGreg Roach		}
241a6f13a4aSGreg Roach
242e8e7866bSGreg Roach		$this->wt_report->addStyle($s);
243a6f13a4aSGreg Roach	}
244a6f13a4aSGreg Roach
245a6f13a4aSGreg Roach	/**
24676692c8bSGreg Roach	 * XML <Doc>
247a6f13a4aSGreg Roach	 *
248a6f13a4aSGreg Roach	 * Sets up the basics of the document proparties
249a6f13a4aSGreg Roach	 *
250a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
251a6f13a4aSGreg Roach	 */
2528edd1043SGreg Roach	private function docStartHandler($attrs) {
253e8e7866bSGreg Roach		$this->parser = $this->xml_parser;
254a6f13a4aSGreg Roach
255a6f13a4aSGreg Roach		// Custom page width
256a6f13a4aSGreg Roach		if (!empty($attrs['customwidth'])) {
257e8e7866bSGreg Roach			$this->wt_report->pagew = (int) $attrs['customwidth'];
258a6f13a4aSGreg Roach		} // Get it as int to ignore all decimal points or text (if any text then int(0))
259a6f13a4aSGreg Roach		// Custom Page height
260a6f13a4aSGreg Roach		if (!empty($attrs['customheight'])) {
261e8e7866bSGreg Roach			$this->wt_report->pageh = (int) $attrs['customheight'];
262a6f13a4aSGreg Roach		} // Get it as int to ignore all decimal points or text (if any text then int(0))
263a6f13a4aSGreg Roach
264a6f13a4aSGreg Roach		// Left Margin
265a6f13a4aSGreg Roach		if (isset($attrs['leftmargin'])) {
2667a6ee1acSGreg Roach			if ($attrs['leftmargin'] === '0') {
267e8e7866bSGreg Roach				$this->wt_report->leftmargin = 0;
268a6f13a4aSGreg Roach			} elseif (!empty($attrs['leftmargin'])) {
269e8e7866bSGreg Roach				$this->wt_report->leftmargin = (int) $attrs['leftmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
270a6f13a4aSGreg Roach			}
271a6f13a4aSGreg Roach		}
272a6f13a4aSGreg Roach		// Right Margin
273a6f13a4aSGreg Roach		if (isset($attrs['rightmargin'])) {
2747a6ee1acSGreg Roach			if ($attrs['rightmargin'] === '0') {
275e8e7866bSGreg Roach				$this->wt_report->rightmargin = 0;
276a6f13a4aSGreg Roach			} elseif (!empty($attrs['rightmargin'])) {
277e8e7866bSGreg Roach				$this->wt_report->rightmargin = (int) $attrs['rightmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
278a6f13a4aSGreg Roach			}
279a6f13a4aSGreg Roach		}
280a6f13a4aSGreg Roach		// Top Margin
281a6f13a4aSGreg Roach		if (isset($attrs['topmargin'])) {
2827a6ee1acSGreg Roach			if ($attrs['topmargin'] === '0') {
283e8e7866bSGreg Roach				$this->wt_report->topmargin = 0;
284a6f13a4aSGreg Roach			} elseif (!empty($attrs['topmargin'])) {
285e8e7866bSGreg Roach				$this->wt_report->topmargin = (int) $attrs['topmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
286a6f13a4aSGreg Roach			}
287a6f13a4aSGreg Roach		}
288a6f13a4aSGreg Roach		// Bottom Margin
289a6f13a4aSGreg Roach		if (isset($attrs['bottommargin'])) {
2907a6ee1acSGreg Roach			if ($attrs['bottommargin'] === '0') {
291e8e7866bSGreg Roach				$this->wt_report->bottommargin = 0;
292a6f13a4aSGreg Roach			} elseif (!empty($attrs['bottommargin'])) {
293e8e7866bSGreg Roach				$this->wt_report->bottommargin = (int) $attrs['bottommargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
294a6f13a4aSGreg Roach			}
295a6f13a4aSGreg Roach		}
296a6f13a4aSGreg Roach		// Header Margin
297a6f13a4aSGreg Roach		if (isset($attrs['headermargin'])) {
2987a6ee1acSGreg Roach			if ($attrs['headermargin'] === '0') {
299e8e7866bSGreg Roach				$this->wt_report->headermargin = 0;
300a6f13a4aSGreg Roach			} elseif (!empty($attrs['headermargin'])) {
301e8e7866bSGreg Roach				$this->wt_report->headermargin = (int) $attrs['headermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
302a6f13a4aSGreg Roach			}
303a6f13a4aSGreg Roach		}
304a6f13a4aSGreg Roach		// Footer Margin
305a6f13a4aSGreg Roach		if (isset($attrs['footermargin'])) {
3067a6ee1acSGreg Roach			if ($attrs['footermargin'] === '0') {
307e8e7866bSGreg Roach				$this->wt_report->footermargin = 0;
308a6f13a4aSGreg Roach			} elseif (!empty($attrs['footermargin'])) {
309e8e7866bSGreg Roach				$this->wt_report->footermargin = (int) $attrs['footermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
310a6f13a4aSGreg Roach			}
311a6f13a4aSGreg Roach		}
312a6f13a4aSGreg Roach
313a6f13a4aSGreg Roach		// Page Orientation
314a6f13a4aSGreg Roach		if (!empty($attrs['orientation'])) {
3157a6ee1acSGreg Roach			if ($attrs['orientation'] == 'landscape') {
3167a6ee1acSGreg Roach				$this->wt_report->orientation = 'landscape';
3177a6ee1acSGreg Roach			} elseif ($attrs['orientation'] == 'portrait') {
3187a6ee1acSGreg Roach				$this->wt_report->orientation = 'portrait';
319a6f13a4aSGreg Roach			}
320a6f13a4aSGreg Roach		}
321a6f13a4aSGreg Roach		// Page Size
322a6f13a4aSGreg Roach		if (!empty($attrs['pageSize'])) {
323e8e7866bSGreg Roach			$this->wt_report->pageFormat = strtoupper($attrs['pageSize']);
324a6f13a4aSGreg Roach		}
325a6f13a4aSGreg Roach
326a6f13a4aSGreg Roach		// Show Generated By...
327a6f13a4aSGreg Roach		if (isset($attrs['showGeneratedBy'])) {
3287a6ee1acSGreg Roach			if ($attrs['showGeneratedBy'] === '0') {
329e8e7866bSGreg Roach				$this->wt_report->showGenText = false;
3307a6ee1acSGreg Roach			} elseif ($attrs['showGeneratedBy'] === '1') {
331e8e7866bSGreg Roach				$this->wt_report->showGenText = true;
332a6f13a4aSGreg Roach			}
333a6f13a4aSGreg Roach		}
334a6f13a4aSGreg Roach
335e8e7866bSGreg Roach		$this->wt_report->setup();
336a6f13a4aSGreg Roach	}
337a6f13a4aSGreg Roach
338a6f13a4aSGreg Roach	/**
33976692c8bSGreg Roach	 * XML </Doc>
340a6f13a4aSGreg Roach	 */
3418edd1043SGreg Roach	private function docEndHandler() {
342e8e7866bSGreg Roach		$this->wt_report->run();
343a6f13a4aSGreg Roach	}
344a6f13a4aSGreg Roach
345a6f13a4aSGreg Roach	/**
34676692c8bSGreg Roach	 * XML <Header>
347a6f13a4aSGreg Roach	 */
3488edd1043SGreg Roach	private function headerStartHandler() {
349a6f13a4aSGreg Roach		// Clear the Header before any new elements are added
350e8e7866bSGreg Roach		$this->wt_report->clearHeader();
3517a6ee1acSGreg Roach		$this->wt_report->setProcessing('H');
352a6f13a4aSGreg Roach	}
353a6f13a4aSGreg Roach
354a6f13a4aSGreg Roach	/**
35576692c8bSGreg Roach	 * XML <PageHeader>
356a6f13a4aSGreg Roach	 */
3578edd1043SGreg Roach	private function pageHeaderStartHandler() {
358a6f13a4aSGreg Roach		array_push($this->print_data_stack, $this->print_data);
359a6f13a4aSGreg Roach		$this->print_data = false;
360e8e7866bSGreg Roach		array_push($this->wt_report_stack, $this->wt_report);
361e8e7866bSGreg Roach		$this->wt_report = $this->report_root->createPageHeader();
362a6f13a4aSGreg Roach	}
363a6f13a4aSGreg Roach
364a6f13a4aSGreg Roach	/**
36576692c8bSGreg Roach	 * XML <pageHeaderEndHandler>
366a6f13a4aSGreg Roach	 */
3678edd1043SGreg Roach	private function pageHeaderEndHandler() {
368a6f13a4aSGreg Roach		$this->print_data      = array_pop($this->print_data_stack);
369e8e7866bSGreg Roach		$this->current_element = $this->wt_report;
370e8e7866bSGreg Roach		$this->wt_report       = array_pop($this->wt_report_stack);
371e8e7866bSGreg Roach		$this->wt_report->addElement($this->current_element);
372a6f13a4aSGreg Roach	}
373a6f13a4aSGreg Roach
374a6f13a4aSGreg Roach	/**
37576692c8bSGreg Roach	 * XML <bodyStartHandler>
376a6f13a4aSGreg Roach	 */
3778edd1043SGreg Roach	private function bodyStartHandler() {
3787a6ee1acSGreg Roach		$this->wt_report->setProcessing('B');
379a6f13a4aSGreg Roach	}
380a6f13a4aSGreg Roach
381a6f13a4aSGreg Roach	/**
38276692c8bSGreg Roach	 * XML <footerStartHandler>
383a6f13a4aSGreg Roach	 */
3848edd1043SGreg Roach	private function footerStartHandler() {
3857a6ee1acSGreg Roach		$this->wt_report->setProcessing('F');
386a6f13a4aSGreg Roach	}
387a6f13a4aSGreg Roach
388a6f13a4aSGreg Roach	/**
38976692c8bSGreg Roach	 * XML <Cell>
390a6f13a4aSGreg Roach	 *
391a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
392a6f13a4aSGreg Roach	 */
3938edd1043SGreg Roach	private function cellStartHandler($attrs) {
394a6f13a4aSGreg Roach		// string The text alignment of the text in this box.
3957a6ee1acSGreg Roach		$align = '';
396a6f13a4aSGreg Roach		if (!empty($attrs['align'])) {
397a6f13a4aSGreg Roach			$align = $attrs['align'];
398a6f13a4aSGreg Roach			// RTL supported left/right alignment
3997a6ee1acSGreg Roach			if ($align == 'rightrtl') {
400e8e7866bSGreg Roach				if ($this->wt_report->rtl) {
4017a6ee1acSGreg Roach					$align = 'left';
402a6f13a4aSGreg Roach				} else {
4037a6ee1acSGreg Roach					$align = 'right';
404a6f13a4aSGreg Roach				}
4057a6ee1acSGreg Roach			} elseif ($align == 'leftrtl') {
406e8e7866bSGreg Roach				if ($this->wt_report->rtl) {
4077a6ee1acSGreg Roach					$align = 'right';
408a6f13a4aSGreg Roach				} else {
4097a6ee1acSGreg Roach					$align = 'left';
410a6f13a4aSGreg Roach				}
411a6f13a4aSGreg Roach			}
412a6f13a4aSGreg Roach		}
413a6f13a4aSGreg Roach
414a6f13a4aSGreg Roach		// string The color to fill the background of this cell
4157a6ee1acSGreg Roach		$bgcolor = '';
416a6f13a4aSGreg Roach		if (!empty($attrs['bgcolor'])) {
417a6f13a4aSGreg Roach			$bgcolor = $attrs['bgcolor'];
418a6f13a4aSGreg Roach		}
419a6f13a4aSGreg Roach
420a6f13a4aSGreg Roach		// int Whether or not the background should be painted
421a6f13a4aSGreg Roach		$fill = 1;
422a6f13a4aSGreg Roach		if (isset($attrs['fill'])) {
4237a6ee1acSGreg Roach			if ($attrs['fill'] === '0') {
424a6f13a4aSGreg Roach				$fill = 0;
4257a6ee1acSGreg Roach			} elseif ($attrs['fill'] === '1') {
426a6f13a4aSGreg Roach				$fill = 1;
427a6f13a4aSGreg Roach			}
428a6f13a4aSGreg Roach		}
429a6f13a4aSGreg Roach
430a6f13a4aSGreg Roach		$reseth = true;
431a6f13a4aSGreg Roach		// boolean   if true reset the last cell height (default true)
432a6f13a4aSGreg Roach		if (isset($attrs['reseth'])) {
4337a6ee1acSGreg Roach			if ($attrs['reseth'] === '0') {
434a6f13a4aSGreg Roach				$reseth = false;
4357a6ee1acSGreg Roach			} elseif ($attrs['reseth'] === '1') {
436a6f13a4aSGreg Roach				$reseth = true;
437a6f13a4aSGreg Roach			}
438a6f13a4aSGreg Roach		}
439a6f13a4aSGreg Roach
440a6f13a4aSGreg Roach		// mixed Whether or not a border should be printed around this box
441a6f13a4aSGreg Roach		$border = 0;
442a6f13a4aSGreg Roach		if (!empty($attrs['border'])) {
443a6f13a4aSGreg Roach			$border = $attrs['border'];
444a6f13a4aSGreg Roach		}
445a6f13a4aSGreg Roach		// string Border color in HTML code
4467a6ee1acSGreg Roach		$bocolor = '';
447a6f13a4aSGreg Roach		if (!empty($attrs['bocolor'])) {
448a6f13a4aSGreg Roach			$bocolor = $attrs['bocolor'];
449a6f13a4aSGreg Roach		}
450a6f13a4aSGreg Roach
451a6f13a4aSGreg Roach		// int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
452a6f13a4aSGreg Roach		$height = 0;
453a6f13a4aSGreg Roach		if (!empty($attrs['height'])) {
454a6f13a4aSGreg Roach			$height = (int) $attrs['height'];
455a6f13a4aSGreg Roach		}
456a6f13a4aSGreg Roach		// int Cell width (expressed in points) Setting the width to 0 will make it the width from the current location to the right margin.
457a6f13a4aSGreg Roach		$width = 0;
458a6f13a4aSGreg Roach		if (!empty($attrs['width'])) {
459a6f13a4aSGreg Roach			$width = (int) $attrs['width'];
460a6f13a4aSGreg Roach		}
461a6f13a4aSGreg Roach
462a6f13a4aSGreg Roach		// int Stretch carachter mode
463a6f13a4aSGreg Roach		$stretch = 0;
464a6f13a4aSGreg Roach		if (!empty($attrs['stretch'])) {
465a6f13a4aSGreg Roach			$stretch = (int) $attrs['stretch'];
466a6f13a4aSGreg Roach		}
467a6f13a4aSGreg Roach
468a6f13a4aSGreg Roach		// mixed Position the left corner of this box on the page. The default is the current position.
4697a6ee1acSGreg Roach		$left = '.';
470a6f13a4aSGreg Roach		if (isset($attrs['left'])) {
4717a6ee1acSGreg Roach			if ($attrs['left'] === '.') {
4727a6ee1acSGreg Roach				$left = '.';
473a6f13a4aSGreg Roach			} elseif (!empty($attrs['left'])) {
474a6f13a4aSGreg Roach				$left = (int) $attrs['left'];
4757a6ee1acSGreg Roach			} elseif ($attrs['left'] === '0') {
476a6f13a4aSGreg Roach				$left = 0;
477a6f13a4aSGreg Roach			}
478a6f13a4aSGreg Roach		}
479a6f13a4aSGreg Roach		// mixed Position the top corner of this box on the page. the default is the current position
4807a6ee1acSGreg Roach		$top = '.';
481a6f13a4aSGreg Roach		if (isset($attrs['top'])) {
4827a6ee1acSGreg Roach			if ($attrs['top'] === '.') {
4837a6ee1acSGreg Roach				$top = '.';
484a6f13a4aSGreg Roach			} elseif (!empty($attrs['top'])) {
485a6f13a4aSGreg Roach				$top = (int) $attrs['top'];
4867a6ee1acSGreg Roach			} elseif ($attrs['top'] === '0') {
487a6f13a4aSGreg Roach				$top = 0;
488a6f13a4aSGreg Roach			}
489a6f13a4aSGreg Roach		}
490a6f13a4aSGreg Roach
491a6f13a4aSGreg Roach		// string The name of the Style that should be used to render the text.
4927a6ee1acSGreg Roach		$style = '';
493a6f13a4aSGreg Roach		if (!empty($attrs['style'])) {
494a6f13a4aSGreg Roach			$style = $attrs['style'];
495a6f13a4aSGreg Roach		}
496a6f13a4aSGreg Roach
497a6f13a4aSGreg Roach		// string Text color in html code
4987a6ee1acSGreg Roach		$tcolor = '';
499a6f13a4aSGreg Roach		if (!empty($attrs['tcolor'])) {
500a6f13a4aSGreg Roach			$tcolor = $attrs['tcolor'];
501a6f13a4aSGreg Roach		}
502a6f13a4aSGreg Roach
503a6f13a4aSGreg Roach		// int Indicates where the current position should go after the call.
504a6f13a4aSGreg Roach		$ln = 0;
505a6f13a4aSGreg Roach		if (isset($attrs['newline'])) {
506a6f13a4aSGreg Roach			if (!empty($attrs['newline'])) {
507a6f13a4aSGreg Roach				$ln = (int) $attrs['newline'];
5087a6ee1acSGreg Roach			} elseif ($attrs['newline'] === '0') {
509a6f13a4aSGreg Roach				$ln = 0;
510a6f13a4aSGreg Roach			}
511a6f13a4aSGreg Roach		}
512a6f13a4aSGreg Roach
5137a6ee1acSGreg Roach		if ($align == 'left') {
5147a6ee1acSGreg Roach			$align = 'L';
5157a6ee1acSGreg Roach		} elseif ($align == 'right') {
5167a6ee1acSGreg Roach			$align = 'R';
5177a6ee1acSGreg Roach		} elseif ($align == 'center') {
5187a6ee1acSGreg Roach			$align = 'C';
5197a6ee1acSGreg Roach		} elseif ($align == 'justify') {
5207a6ee1acSGreg Roach			$align = 'J';
521a6f13a4aSGreg Roach		}
522a6f13a4aSGreg Roach
523a6f13a4aSGreg Roach		array_push($this->print_data_stack, $this->print_data);
524a6f13a4aSGreg Roach		$this->print_data = true;
525a6f13a4aSGreg Roach
526e8e7866bSGreg Roach		$this->current_element = $this->report_root->createCell(
527a6f13a4aSGreg Roach			$width,
528a6f13a4aSGreg Roach			$height,
529a6f13a4aSGreg Roach			$border,
530a6f13a4aSGreg Roach			$align,
531a6f13a4aSGreg Roach			$bgcolor,
532a6f13a4aSGreg Roach			$style,
533a6f13a4aSGreg Roach			$ln,
534a6f13a4aSGreg Roach			$top,
535a6f13a4aSGreg Roach			$left,
536a6f13a4aSGreg Roach			$fill,
537a6f13a4aSGreg Roach			$stretch,
538a6f13a4aSGreg Roach			$bocolor,
539a6f13a4aSGreg Roach			$tcolor,
540a6f13a4aSGreg Roach			$reseth
541a6f13a4aSGreg Roach		);
542a6f13a4aSGreg Roach	}
543a6f13a4aSGreg Roach
544a6f13a4aSGreg Roach	/**
54576692c8bSGreg Roach	 * XML </Cell>
546a6f13a4aSGreg Roach	 */
5478edd1043SGreg Roach	private function cellEndHandler() {
548a6f13a4aSGreg Roach		$this->print_data = array_pop($this->print_data_stack);
549e8e7866bSGreg Roach		$this->wt_report->addElement($this->current_element);
550a6f13a4aSGreg Roach	}
551a6f13a4aSGreg Roach
552a6f13a4aSGreg Roach	/**
553a6f13a4aSGreg Roach	 * XML <Now /> element handler
554a6f13a4aSGreg Roach	 */
5558edd1043SGreg Roach	private function nowStartHandler() {
5563d7a8a4cSGreg Roach		$g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
557a6f13a4aSGreg Roach		$this->current_element->addText($g->display());
558a6f13a4aSGreg Roach	}
559a6f13a4aSGreg Roach
560a6f13a4aSGreg Roach	/**
561a6f13a4aSGreg Roach	 * XML <PageNum /> element handler
562a6f13a4aSGreg Roach	 */
5638edd1043SGreg Roach	private function pageNumStartHandler() {
5647a6ee1acSGreg Roach		$this->current_element->addText('#PAGENUM#');
565a6f13a4aSGreg Roach	}
566a6f13a4aSGreg Roach
567a6f13a4aSGreg Roach	/**
568a6f13a4aSGreg Roach	 * XML <TotalPages /> element handler
569a6f13a4aSGreg Roach	 */
5708edd1043SGreg Roach	private function totalPagesStartHandler() {
5717a6ee1acSGreg Roach		$this->current_element->addText('{{:ptp:}}');
572a6f13a4aSGreg Roach	}
573a6f13a4aSGreg Roach
574a6f13a4aSGreg Roach	/**
575a6f13a4aSGreg Roach	 * Called at the start of an element.
576a6f13a4aSGreg Roach	 *
577a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
578a6f13a4aSGreg Roach	 */
5798edd1043SGreg Roach	private function gedcomStartHandler($attrs) {
580a6f13a4aSGreg Roach		if ($this->process_gedcoms > 0) {
581a6f13a4aSGreg Roach			$this->process_gedcoms++;
582a6f13a4aSGreg Roach
583a6f13a4aSGreg Roach			return;
584a6f13a4aSGreg Roach		}
585a6f13a4aSGreg Roach
586a6f13a4aSGreg Roach		$tag       = $attrs['id'];
5877a6ee1acSGreg Roach		$tag       = str_replace('@fact', $this->fact, $tag);
5887a6ee1acSGreg Roach		$tags      = explode(':', $tag);
589a6f13a4aSGreg Roach		$newgedrec = '';
590a6f13a4aSGreg Roach		if (count($tags) < 2) {
591*299d100dSGreg Roach			$tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
592*299d100dSGreg Roach			$newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
593a6f13a4aSGreg Roach		}
594a6f13a4aSGreg Roach		if (empty($newgedrec)) {
595a6f13a4aSGreg Roach			$tgedrec   = $this->gedrec;
596a6f13a4aSGreg Roach			$newgedrec = '';
597a6f13a4aSGreg Roach			foreach ($tags as $tag) {
5987a6ee1acSGreg Roach				if (preg_match('/\$(.+)/', $tag, $match)) {
599d1286247SGreg Roach					if (isset($this->vars[$match[1]]['gedcom'])) {
600d1286247SGreg Roach						$newgedrec = $this->vars[$match[1]]['gedcom'];
601a6f13a4aSGreg Roach					} else {
602*299d100dSGreg Roach						$tmp       = GedcomRecord::getInstance($match[1], $this->tree);
603*299d100dSGreg Roach						$newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
604a6f13a4aSGreg Roach					}
605a6f13a4aSGreg Roach				} else {
6067a6ee1acSGreg Roach					if (preg_match('/@(.+)/', $tag, $match)) {
60713abd6f3SGreg Roach						$gmatch = [];
608a6f13a4aSGreg Roach						if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
609*299d100dSGreg Roach							$tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
610*299d100dSGreg Roach							$newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
611a6f13a4aSGreg Roach							$tgedrec   = $newgedrec;
612a6f13a4aSGreg Roach						} else {
613a6f13a4aSGreg Roach							$newgedrec = '';
614a6f13a4aSGreg Roach							break;
615a6f13a4aSGreg Roach						}
616a6f13a4aSGreg Roach					} else {
6177a6ee1acSGreg Roach						$temp      = explode(' ', trim($tgedrec));
618a6f13a4aSGreg Roach						$level     = $temp[0] + 1;
6193d7a8a4cSGreg Roach						$newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
620a6f13a4aSGreg Roach						$tgedrec   = $newgedrec;
621a6f13a4aSGreg Roach					}
622a6f13a4aSGreg Roach				}
623a6f13a4aSGreg Roach			}
624a6f13a4aSGreg Roach		}
625a6f13a4aSGreg Roach		if (!empty($newgedrec)) {
62613abd6f3SGreg Roach			array_push($this->gedrec_stack, [$this->gedrec, $this->fact, $this->desc]);
627a6f13a4aSGreg Roach			$this->gedrec = $newgedrec;
628a6f13a4aSGreg Roach			if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
629a6f13a4aSGreg Roach				$this->fact = $match[2];
630a6f13a4aSGreg Roach				$this->desc = trim($match[3]);
631a6f13a4aSGreg Roach			}
632a6f13a4aSGreg Roach		} else {
633a6f13a4aSGreg Roach			$this->process_gedcoms++;
634a6f13a4aSGreg Roach		}
635a6f13a4aSGreg Roach	}
636a6f13a4aSGreg Roach
637a6f13a4aSGreg Roach	/**
638a6f13a4aSGreg Roach	 * Called at the end of an element.
639a6f13a4aSGreg Roach	 */
6408edd1043SGreg Roach	private function gedcomEndHandler() {
641a6f13a4aSGreg Roach		if ($this->process_gedcoms > 0) {
642a6f13a4aSGreg Roach			$this->process_gedcoms--;
643a6f13a4aSGreg Roach		} else {
644a6f13a4aSGreg Roach			list($this->gedrec, $this->fact, $this->desc) = array_pop($this->gedrec_stack);
645a6f13a4aSGreg Roach		}
646a6f13a4aSGreg Roach	}
647a6f13a4aSGreg Roach
648a6f13a4aSGreg Roach	/**
64976692c8bSGreg Roach	 * XML <textBoxStartHandler>
650a6f13a4aSGreg Roach	 *
651a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
652a6f13a4aSGreg Roach	 */
6538edd1043SGreg Roach	private function textBoxStartHandler($attrs) {
654a6f13a4aSGreg Roach		// string Background color code
6557a6ee1acSGreg Roach		$bgcolor = '';
656a6f13a4aSGreg Roach		if (!empty($attrs['bgcolor'])) {
657a6f13a4aSGreg Roach			$bgcolor = $attrs['bgcolor'];
658a6f13a4aSGreg Roach		}
659a6f13a4aSGreg Roach
660a6f13a4aSGreg Roach		// boolean Wether or not fill the background color
661a6f13a4aSGreg Roach		$fill = true;
662a6f13a4aSGreg Roach		if (isset($attrs['fill'])) {
6637a6ee1acSGreg Roach			if ($attrs['fill'] === '0') {
664a6f13a4aSGreg Roach				$fill = false;
6657a6ee1acSGreg Roach			} elseif ($attrs['fill'] === '1') {
666a6f13a4aSGreg Roach				$fill = true;
667a6f13a4aSGreg Roach			}
668a6f13a4aSGreg Roach		}
669a6f13a4aSGreg Roach
670a6f13a4aSGreg Roach		// var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
671a6f13a4aSGreg Roach		$border = false;
672a6f13a4aSGreg Roach		if (isset($attrs['border'])) {
6737a6ee1acSGreg Roach			if ($attrs['border'] === '1') {
674a6f13a4aSGreg Roach				$border = true;
6757a6ee1acSGreg Roach			} elseif ($attrs['border'] === '0') {
676a6f13a4aSGreg Roach				$border = false;
677a6f13a4aSGreg Roach			}
678a6f13a4aSGreg Roach		}
679a6f13a4aSGreg Roach
680a6f13a4aSGreg Roach		// int The starting height of this cell. If the text wraps the height will automatically be adjusted
681a6f13a4aSGreg Roach		$height = 0;
682a6f13a4aSGreg Roach		if (!empty($attrs['height'])) {
683a6f13a4aSGreg Roach			$height = (int) $attrs['height'];
684a6f13a4aSGreg Roach		}
685a6f13a4aSGreg Roach		// int Setting the width to 0 will make it the width from the current location to the margin
686a6f13a4aSGreg Roach		$width = 0;
687a6f13a4aSGreg Roach		if (!empty($attrs['width'])) {
688a6f13a4aSGreg Roach			$width = (int) $attrs['width'];
689a6f13a4aSGreg Roach		}
690a6f13a4aSGreg Roach
691a6f13a4aSGreg Roach		// mixed Position the left corner of this box on the page. The default is the current position.
6927a6ee1acSGreg Roach		$left = '.';
693a6f13a4aSGreg Roach		if (isset($attrs['left'])) {
6947a6ee1acSGreg Roach			if ($attrs['left'] === '.') {
6957a6ee1acSGreg Roach				$left = '.';
696a6f13a4aSGreg Roach			} elseif (!empty($attrs['left'])) {
697a6f13a4aSGreg Roach				$left = (int) $attrs['left'];
6987a6ee1acSGreg Roach			} elseif ($attrs['left'] === '0') {
699a6f13a4aSGreg Roach				$left = 0;
700a6f13a4aSGreg Roach			}
701a6f13a4aSGreg Roach		}
702a6f13a4aSGreg Roach		// mixed Position the top corner of this box on the page. the default is the current position
7037a6ee1acSGreg Roach		$top = '.';
704a6f13a4aSGreg Roach		if (isset($attrs['top'])) {
7057a6ee1acSGreg Roach			if ($attrs['top'] === '.') {
7067a6ee1acSGreg Roach				$top = '.';
707a6f13a4aSGreg Roach			} elseif (!empty($attrs['top'])) {
708a6f13a4aSGreg Roach				$top = (int) $attrs['top'];
7097a6ee1acSGreg Roach			} elseif ($attrs['top'] === '0') {
710a6f13a4aSGreg Roach				$top = 0;
711a6f13a4aSGreg Roach			}
712a6f13a4aSGreg Roach		}
713a6f13a4aSGreg Roach		// boolean After this box is finished rendering, should the next section of text start immediately after the this box or should it start on a new line under this box. 0 = no new line, 1 = force new line. Default is 0
714a6f13a4aSGreg Roach		$newline = false;
715a6f13a4aSGreg Roach		if (isset($attrs['newline'])) {
7167a6ee1acSGreg Roach			if ($attrs['newline'] === '1') {
717a6f13a4aSGreg Roach				$newline = true;
7187a6ee1acSGreg Roach			} elseif ($attrs['newline'] === '0') {
719a6f13a4aSGreg Roach				$newline = false;
720a6f13a4aSGreg Roach			}
721a6f13a4aSGreg Roach		}
722a6f13a4aSGreg Roach		// boolean
723a6f13a4aSGreg Roach		$pagecheck = true;
724a6f13a4aSGreg Roach		if (isset($attrs['pagecheck'])) {
7257a6ee1acSGreg Roach			if ($attrs['pagecheck'] === '0') {
726a6f13a4aSGreg Roach				$pagecheck = false;
7277a6ee1acSGreg Roach			} elseif ($attrs['pagecheck'] === '1') {
728a6f13a4aSGreg Roach				$pagecheck = true;
729a6f13a4aSGreg Roach			}
730a6f13a4aSGreg Roach		}
731a6f13a4aSGreg Roach		// boolean Cell padding
732a6f13a4aSGreg Roach		$padding = true;
733a6f13a4aSGreg Roach		if (isset($attrs['padding'])) {
7347a6ee1acSGreg Roach			if ($attrs['padding'] === '0') {
735a6f13a4aSGreg Roach				$padding = false;
7367a6ee1acSGreg Roach			} elseif ($attrs['padding'] === '1') {
737a6f13a4aSGreg Roach				$padding = true;
738a6f13a4aSGreg Roach			}
739a6f13a4aSGreg Roach		}
740a6f13a4aSGreg Roach		// boolean Reset this box Height
741a6f13a4aSGreg Roach		$reseth = false;
742a6f13a4aSGreg Roach		if (isset($attrs['reseth'])) {
7437a6ee1acSGreg Roach			if ($attrs['reseth'] === '1') {
744a6f13a4aSGreg Roach				$reseth = true;
7457a6ee1acSGreg Roach			} elseif ($attrs['reseth'] === '0') {
746a6f13a4aSGreg Roach				$reseth = false;
747a6f13a4aSGreg Roach			}
748a6f13a4aSGreg Roach		}
749a6f13a4aSGreg Roach
750a6f13a4aSGreg Roach		// string Style of rendering
7517a6ee1acSGreg Roach		$style = '';
752a6f13a4aSGreg Roach
753a6f13a4aSGreg Roach		array_push($this->print_data_stack, $this->print_data);
754a6f13a4aSGreg Roach		$this->print_data = false;
755a6f13a4aSGreg Roach
756e8e7866bSGreg Roach		array_push($this->wt_report_stack, $this->wt_report);
757e8e7866bSGreg Roach		$this->wt_report = $this->report_root->createTextBox(
758a6f13a4aSGreg Roach			$width,
759a6f13a4aSGreg Roach			$height,
760a6f13a4aSGreg Roach			$border,
761a6f13a4aSGreg Roach			$bgcolor,
762a6f13a4aSGreg Roach			$newline,
763a6f13a4aSGreg Roach			$left,
764a6f13a4aSGreg Roach			$top,
765a6f13a4aSGreg Roach			$pagecheck,
766a6f13a4aSGreg Roach			$style,
767a6f13a4aSGreg Roach			$fill,
768a6f13a4aSGreg Roach			$padding,
769a6f13a4aSGreg Roach			$reseth
770a6f13a4aSGreg Roach		);
771a6f13a4aSGreg Roach	}
772a6f13a4aSGreg Roach
773a6f13a4aSGreg Roach	/**
77476692c8bSGreg Roach	 * XML <textBoxEndHandler>
775a6f13a4aSGreg Roach	 */
7768edd1043SGreg Roach	private function textBoxEndHandler() {
777a6f13a4aSGreg Roach		$this->print_data      = array_pop($this->print_data_stack);
778e8e7866bSGreg Roach		$this->current_element = $this->wt_report;
779e8e7866bSGreg Roach		$this->wt_report       = array_pop($this->wt_report_stack);
780e8e7866bSGreg Roach		$this->wt_report->addElement($this->current_element);
781a6f13a4aSGreg Roach	}
782a6f13a4aSGreg Roach
783a6f13a4aSGreg Roach	/**
78476692c8bSGreg Roach	 * XLM <Text>.
78576692c8bSGreg Roach	 *
786a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
787a6f13a4aSGreg Roach	 */
7888edd1043SGreg Roach	private function textStartHandler($attrs) {
789a6f13a4aSGreg Roach		array_push($this->print_data_stack, $this->print_data);
790a6f13a4aSGreg Roach		$this->print_data = true;
791a6f13a4aSGreg Roach
792a6f13a4aSGreg Roach		// string The name of the Style that should be used to render the text.
7937a6ee1acSGreg Roach		$style = '';
794a6f13a4aSGreg Roach		if (!empty($attrs['style'])) {
795a6f13a4aSGreg Roach			$style = $attrs['style'];
796a6f13a4aSGreg Roach		}
797a6f13a4aSGreg Roach
798a6f13a4aSGreg Roach		// string  The color of the text - Keep the black color as default
7997a6ee1acSGreg Roach		$color = '';
800a6f13a4aSGreg Roach		if (!empty($attrs['color'])) {
801a6f13a4aSGreg Roach			$color = $attrs['color'];
802a6f13a4aSGreg Roach		}
803a6f13a4aSGreg Roach
804e8e7866bSGreg Roach		$this->current_element = $this->report_root->createText($style, $color);
805a6f13a4aSGreg Roach	}
806a6f13a4aSGreg Roach
807a6f13a4aSGreg Roach	/**
80876692c8bSGreg Roach	 * XML </Text>
809a6f13a4aSGreg Roach	 */
8108edd1043SGreg Roach	private function textEndHandler() {
811a6f13a4aSGreg Roach		$this->print_data = array_pop($this->print_data_stack);
812e8e7866bSGreg Roach		$this->wt_report->addElement($this->current_element);
813a6f13a4aSGreg Roach	}
814a6f13a4aSGreg Roach
815a6f13a4aSGreg Roach	/**
81676692c8bSGreg Roach	 * XML <GetPersonName/>
81776692c8bSGreg Roach	 *
818a6f13a4aSGreg Roach	 * Get the name
819a6f13a4aSGreg Roach	 * 1. id is empty - current GEDCOM record
820a6f13a4aSGreg Roach	 * 2. id is set with a record id
821a6f13a4aSGreg Roach	 *
822a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
823a6f13a4aSGreg Roach	 */
8248edd1043SGreg Roach	private function getPersonNameStartHandler($attrs) {
8257a6ee1acSGreg Roach		$id    = '';
82613abd6f3SGreg Roach		$match = [];
827a6f13a4aSGreg Roach		if (empty($attrs['id'])) {
8287a6ee1acSGreg Roach			if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
829a6f13a4aSGreg Roach				$id = $match[1];
830a6f13a4aSGreg Roach			}
831a6f13a4aSGreg Roach		} else {
8327a6ee1acSGreg Roach			if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
833d1286247SGreg Roach				if (isset($this->vars[$match[1]]['id'])) {
834d1286247SGreg Roach					$id = $this->vars[$match[1]]['id'];
835a6f13a4aSGreg Roach				}
836a6f13a4aSGreg Roach			} else {
8377a6ee1acSGreg Roach				if (preg_match('/@(.+)/', $attrs['id'], $match)) {
83813abd6f3SGreg Roach					$gmatch = [];
839a6f13a4aSGreg Roach					if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
840a6f13a4aSGreg Roach						$id = $gmatch[1];
841a6f13a4aSGreg Roach					}
842a6f13a4aSGreg Roach				} else {
843a6f13a4aSGreg Roach					$id = $attrs['id'];
844a6f13a4aSGreg Roach				}
845a6f13a4aSGreg Roach			}
846a6f13a4aSGreg Roach		}
847a6f13a4aSGreg Roach		if (!empty($id)) {
848*299d100dSGreg Roach			$record = GedcomRecord::getInstance($id, $this->tree);
849a6f13a4aSGreg Roach			if (is_null($record)) {
850a6f13a4aSGreg Roach				return;
851a6f13a4aSGreg Roach			}
852a6f13a4aSGreg Roach			if (!$record->canShowName()) {
853a6f13a4aSGreg Roach				$this->current_element->addText(I18N::translate('Private'));
854a6f13a4aSGreg Roach			} else {
855a6f13a4aSGreg Roach				$name = $record->getFullName();
856a6f13a4aSGreg Roach				$name = preg_replace(
85713abd6f3SGreg Roach					['/<span class="starredname">/', '/<\/span><\/span>/', '/<\/span>/'],
85813abd6f3SGreg Roach					['«', '', '»'],
859a6f13a4aSGreg Roach					$name
860a6f13a4aSGreg Roach				);
861a6f13a4aSGreg Roach				$name = strip_tags($name);
862a6f13a4aSGreg Roach				if (!empty($attrs['truncate'])) {
863a6f13a4aSGreg Roach					if (mb_strlen($name) > $attrs['truncate']) {
864a6f13a4aSGreg Roach						$name  = preg_replace("/\(.*\) ?/", '', $name); //removes () and text inbetween - what about ", [ and { etc?
865a6f13a4aSGreg Roach						$words = preg_split('/[, -]+/', $name); // names separated with space, comma or hyphen - any others?
866a6f13a4aSGreg Roach						$name  = $words[count($words) - 1];
867a6f13a4aSGreg Roach						for ($i = count($words) - 2; $i >= 0; $i--) {
868a6f13a4aSGreg Roach							$len = mb_strlen($name);
869a6f13a4aSGreg Roach							for ($j = count($words) - 3; $j >= 0; $j--) {
870a6f13a4aSGreg Roach								$len += mb_strlen($words[$j]);
871a6f13a4aSGreg Roach							}
872a6f13a4aSGreg Roach							if ($len > $attrs['truncate']) {
873a6f13a4aSGreg Roach								$first_letter = mb_substr($words[$i], 0, 1);
874a6f13a4aSGreg Roach								// Do not show " of nick-names
8757a6ee1acSGreg Roach								if ($first_letter != '"') {
876a6f13a4aSGreg Roach									$name = mb_substr($words[$i], 0, 1) . '. ' . $name;
877a6f13a4aSGreg Roach								}
878a6f13a4aSGreg Roach							} else {
879a6f13a4aSGreg Roach								$name = $words[$i] . ' ' . $name;
880a6f13a4aSGreg Roach							}
881a6f13a4aSGreg Roach						}
882a6f13a4aSGreg Roach					}
883a6f13a4aSGreg Roach				} else {
884a6f13a4aSGreg Roach					$addname = $record->getAddName();
885a6f13a4aSGreg Roach					$addname = preg_replace(
88613abd6f3SGreg Roach						['/<span class="starredname">/', '/<\/span><\/span>/', '/<\/span>/'],
88713abd6f3SGreg Roach						['«', '', '»'],
888a6f13a4aSGreg Roach						$addname
889a6f13a4aSGreg Roach					);
890a6f13a4aSGreg Roach					$addname = strip_tags($addname);
891a6f13a4aSGreg Roach					if (!empty($addname)) {
8927a6ee1acSGreg Roach						$name .= ' ' . $addname;
893a6f13a4aSGreg Roach					}
894a6f13a4aSGreg Roach				}
895a6f13a4aSGreg Roach				$this->current_element->addText(trim($name));
896a6f13a4aSGreg Roach			}
897a6f13a4aSGreg Roach		}
898a6f13a4aSGreg Roach	}
899a6f13a4aSGreg Roach
900a6f13a4aSGreg Roach	/**
90176692c8bSGreg Roach	 * XML <GedcomValue/>
902a6f13a4aSGreg Roach	 *
903a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
904a6f13a4aSGreg Roach	 */
9058edd1043SGreg Roach	private function gedcomValueStartHandler($attrs) {
9067a6ee1acSGreg Roach		$id    = '';
90713abd6f3SGreg Roach		$match = [];
9087a6ee1acSGreg Roach		if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
909a6f13a4aSGreg Roach			$id = $match[1];
910a6f13a4aSGreg Roach		}
911a6f13a4aSGreg Roach
9127a6ee1acSGreg Roach		if (isset($attrs['newline']) && $attrs['newline'] == '1') {
9137a6ee1acSGreg Roach			$useBreak = '1';
914a6f13a4aSGreg Roach		} else {
9157a6ee1acSGreg Roach			$useBreak = '0';
916a6f13a4aSGreg Roach		}
917a6f13a4aSGreg Roach
918a6f13a4aSGreg Roach		$tag = $attrs['tag'];
919a6f13a4aSGreg Roach		if (!empty($tag)) {
9207a6ee1acSGreg Roach			if ($tag == '@desc') {
921a6f13a4aSGreg Roach				$value = $this->desc;
922a6f13a4aSGreg Roach				$value = trim($value);
923a6f13a4aSGreg Roach				$this->current_element->addText($value);
924a6f13a4aSGreg Roach			}
9257a6ee1acSGreg Roach			if ($tag == '@id') {
926a6f13a4aSGreg Roach				$this->current_element->addText($id);
927a6f13a4aSGreg Roach			} else {
9287a6ee1acSGreg Roach				$tag = str_replace('@fact', $this->fact, $tag);
929a6f13a4aSGreg Roach				if (empty($attrs['level'])) {
9307a6ee1acSGreg Roach					$temp  = explode(' ', trim($this->gedrec));
931a6f13a4aSGreg Roach					$level = $temp[0];
932a6f13a4aSGreg Roach					if ($level == 0) {
933a6f13a4aSGreg Roach						$level++;
934a6f13a4aSGreg Roach					}
935a6f13a4aSGreg Roach				} else {
936a6f13a4aSGreg Roach					$level = $attrs['level'];
937a6f13a4aSGreg Roach				}
938a6f13a4aSGreg Roach				$tags  = preg_split('/[: ]/', $tag);
9393d7a8a4cSGreg Roach				$value = $this->getGedcomValue($tag, $level, $this->gedrec);
940a6f13a4aSGreg Roach				switch (end($tags)) {
941a6f13a4aSGreg Roach					case 'DATE':
942a6f13a4aSGreg Roach						$tmp   = new Date($value);
943a6f13a4aSGreg Roach						$value = $tmp->display();
944a6f13a4aSGreg Roach						break;
945a6f13a4aSGreg Roach					case 'PLAC':
946*299d100dSGreg Roach						$tmp   = new Place($value, $this->tree);
947a6f13a4aSGreg Roach						$value = $tmp->getShortName();
948a6f13a4aSGreg Roach						break;
949a6f13a4aSGreg Roach				}
9507a6ee1acSGreg Roach				if ($useBreak == '1') {
951a6f13a4aSGreg Roach					// Insert <br> when multiple dates exist.
952a6f13a4aSGreg Roach					// This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
953a6f13a4aSGreg Roach					$value = str_replace('(', '<br>(', $value);
954a6f13a4aSGreg Roach					$value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
955a6f13a4aSGreg Roach					$value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
956a6f13a4aSGreg Roach					if (substr($value, 0, 6) == '<br>') {
957a6f13a4aSGreg Roach						$value = substr($value, 6);
958a6f13a4aSGreg Roach					}
959a6f13a4aSGreg Roach				}
960d4d660b7SGreg Roach				$tmp = explode(':', $tag);
96113abd6f3SGreg Roach				if (in_array(end($tmp), ['NOTE', 'TEXT'])) {
962*299d100dSGreg Roach					$value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
963a4d703aeSGreg Roach				}
964a6f13a4aSGreg Roach				$this->current_element->addText($value);
965a6f13a4aSGreg Roach			}
966a6f13a4aSGreg Roach		}
967a6f13a4aSGreg Roach	}
968a6f13a4aSGreg Roach
969a6f13a4aSGreg Roach	/**
97076692c8bSGreg Roach	 * XML <RepeatTag>
971a6f13a4aSGreg Roach	 *
972a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
973a6f13a4aSGreg Roach	 */
9748edd1043SGreg Roach	private function repeatTagStartHandler($attrs) {
975a6f13a4aSGreg Roach		$this->process_repeats++;
976a6f13a4aSGreg Roach		if ($this->process_repeats > 1) {
977a6f13a4aSGreg Roach			return;
978a6f13a4aSGreg Roach		}
979a6f13a4aSGreg Roach
98013abd6f3SGreg Roach		array_push($this->repeats_stack, [$this->repeats, $this->repeat_bytes]);
98113abd6f3SGreg Roach		$this->repeats      = [];
982e8e7866bSGreg Roach		$this->repeat_bytes = xml_get_current_line_number($this->parser);
983a6f13a4aSGreg Roach
9847a6ee1acSGreg Roach		$tag = '';
985a6f13a4aSGreg Roach		if (isset($attrs['tag'])) {
986a6f13a4aSGreg Roach			$tag = $attrs['tag'];
987a6f13a4aSGreg Roach		}
988a6f13a4aSGreg Roach		if (!empty($tag)) {
9897a6ee1acSGreg Roach			if ($tag == '@desc') {
990a6f13a4aSGreg Roach				$value = $this->desc;
991a6f13a4aSGreg Roach				$value = trim($value);
992a6f13a4aSGreg Roach				$this->current_element->addText($value);
993a6f13a4aSGreg Roach			} else {
9947a6ee1acSGreg Roach				$tag   = str_replace('@fact', $this->fact, $tag);
9957a6ee1acSGreg Roach				$tags  = explode(':', $tag);
9967a6ee1acSGreg Roach				$temp  = explode(' ', trim($this->gedrec));
997a6f13a4aSGreg Roach				$level = $temp[0];
998a6f13a4aSGreg Roach				if ($level == 0) {
999a6f13a4aSGreg Roach					$level++;
1000a6f13a4aSGreg Roach				}
1001a6f13a4aSGreg Roach				$subrec = $this->gedrec;
1002a6f13a4aSGreg Roach				$t      = $tag;
1003a6f13a4aSGreg Roach				$count  = count($tags);
1004a6f13a4aSGreg Roach				$i      = 0;
1005a6f13a4aSGreg Roach				while ($i < $count) {
1006a6f13a4aSGreg Roach					$t = $tags[$i];
1007a6f13a4aSGreg Roach					if (!empty($t)) {
1008a6f13a4aSGreg Roach						if ($i < ($count - 1)) {
10093d7a8a4cSGreg Roach							$subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1010a6f13a4aSGreg Roach							if (empty($subrec)) {
1011a6f13a4aSGreg Roach								$level--;
10123d7a8a4cSGreg Roach								$subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1013a6f13a4aSGreg Roach								if (empty($subrec)) {
1014a6f13a4aSGreg Roach									return;
1015a6f13a4aSGreg Roach								}
1016a6f13a4aSGreg Roach							}
1017a6f13a4aSGreg Roach						}
1018a6f13a4aSGreg Roach						$level++;
1019a6f13a4aSGreg Roach					}
1020a6f13a4aSGreg Roach					$i++;
1021a6f13a4aSGreg Roach				}
1022a6f13a4aSGreg Roach				$level--;
1023a6f13a4aSGreg Roach				$count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1024a6f13a4aSGreg Roach				$i     = 0;
1025a6f13a4aSGreg Roach				while ($i < $count) {
1026a6f13a4aSGreg Roach					$i++;
1027a9007102SGreg Roach					// Privacy check - is this a link, and are we allowed to view the linked object?
1028a9007102SGreg Roach					$subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
1029a9007102SGreg Roach					if (preg_match('/^\d ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1030*299d100dSGreg Roach						$linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1031a9007102SGreg Roach						if ($linked_object && !$linked_object->canShow()) {
1032a9007102SGreg Roach							continue;
1033a9007102SGreg Roach						}
1034a9007102SGreg Roach					}
1035a9007102SGreg Roach					$this->repeats[] = $subrecord;
1036a6f13a4aSGreg Roach				}
1037a6f13a4aSGreg Roach			}
1038a6f13a4aSGreg Roach		}
1039a6f13a4aSGreg Roach	}
1040a6f13a4aSGreg Roach
1041a6f13a4aSGreg Roach	/**
104276692c8bSGreg Roach	 * XML </ RepeatTag>
1043a6f13a4aSGreg Roach	 */
10448edd1043SGreg Roach	private function repeatTagEndHandler() {
1045a6f13a4aSGreg Roach		$this->process_repeats--;
1046a6f13a4aSGreg Roach		if ($this->process_repeats > 0) {
1047a6f13a4aSGreg Roach			return;
1048a6f13a4aSGreg Roach		}
1049a6f13a4aSGreg Roach
1050a6f13a4aSGreg Roach		// Check if there is anything to repeat
1051a6f13a4aSGreg Roach		if (count($this->repeats) > 0) {
1052a6f13a4aSGreg Roach			// No need to load them if not used...
1053a6f13a4aSGreg Roach
1054a6f13a4aSGreg Roach			$lineoffset = 0;
1055a6f13a4aSGreg Roach			foreach ($this->repeats_stack as $rep) {
1056a6f13a4aSGreg Roach				$lineoffset += $rep[1];
1057a6f13a4aSGreg Roach			}
1058a6f13a4aSGreg Roach			//-- read the xml from the file
1059*299d100dSGreg Roach			$lines = file($this->report);
10607a6ee1acSGreg Roach			while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1061a6f13a4aSGreg Roach				$lineoffset--;
1062a6f13a4aSGreg Roach			}
1063a6f13a4aSGreg Roach			$lineoffset++;
1064a6f13a4aSGreg Roach			$reportxml = "<tempdoc>\n";
1065a6f13a4aSGreg Roach			$line_nr   = $lineoffset + $this->repeat_bytes;
1066a6f13a4aSGreg Roach			// RepeatTag Level counter
1067a6f13a4aSGreg Roach			$count = 1;
1068a6f13a4aSGreg Roach			while (0 < $count) {
10697a6ee1acSGreg Roach				if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1070a6f13a4aSGreg Roach					$count++;
10717a6ee1acSGreg Roach				} elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1072a6f13a4aSGreg Roach					$count--;
1073a6f13a4aSGreg Roach				}
1074a6f13a4aSGreg Roach				if (0 < $count) {
1075a6f13a4aSGreg Roach					$reportxml .= $lines[$line_nr];
1076a6f13a4aSGreg Roach				}
1077a6f13a4aSGreg Roach				$line_nr++;
1078a6f13a4aSGreg Roach			}
1079a6f13a4aSGreg Roach			// No need to drag this
1080a6f13a4aSGreg Roach			unset($lines);
1081a6f13a4aSGreg Roach			$reportxml .= "</tempdoc>\n";
1082a6f13a4aSGreg Roach			// Save original values
1083e8e7866bSGreg Roach			array_push($this->parser_stack, $this->parser);
1084a6f13a4aSGreg Roach			$oldgedrec = $this->gedrec;
1085a6f13a4aSGreg Roach			foreach ($this->repeats as $gedrec) {
1086a6f13a4aSGreg Roach				$this->gedrec  = $gedrec;
1087a6f13a4aSGreg Roach				$repeat_parser = xml_parser_create();
1088e8e7866bSGreg Roach				$this->parser  = $repeat_parser;
1089a6f13a4aSGreg Roach				xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
109013abd6f3SGreg Roach				xml_set_element_handler($repeat_parser, [$this, 'startElement'], [$this, 'endElement']);
109113abd6f3SGreg Roach				xml_set_character_data_handler($repeat_parser, [$this, 'characterData']);
1092a6f13a4aSGreg Roach				if (!xml_parse($repeat_parser, $reportxml, true)) {
1093a6f13a4aSGreg Roach					throw new \DomainException(sprintf(
1094a6f13a4aSGreg Roach						'RepeatTagEHandler XML error: %s at line %d',
1095a6f13a4aSGreg Roach						xml_error_string(xml_get_error_code($repeat_parser)),
1096a6f13a4aSGreg Roach						xml_get_current_line_number($repeat_parser)
1097a6f13a4aSGreg Roach					));
1098a6f13a4aSGreg Roach				}
1099a6f13a4aSGreg Roach				xml_parser_free($repeat_parser);
1100a6f13a4aSGreg Roach			}
1101a6f13a4aSGreg Roach			// Restore original values
1102a6f13a4aSGreg Roach			$this->gedrec = $oldgedrec;
1103e8e7866bSGreg Roach			$this->parser = array_pop($this->parser_stack);
1104a6f13a4aSGreg Roach		}
1105a6f13a4aSGreg Roach		list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1106a6f13a4aSGreg Roach	}
1107a6f13a4aSGreg Roach
1108a6f13a4aSGreg Roach	/**
1109a6f13a4aSGreg Roach	 * Variable lookup
1110a6f13a4aSGreg Roach	 *
1111a6f13a4aSGreg Roach	 * Retrieve predefined variables :
1112a6f13a4aSGreg Roach	 *
1113a6f13a4aSGreg Roach	 * @ desc GEDCOM fact description, example:
1114a6f13a4aSGreg Roach	 *        1 EVEN This is a description
1115a6f13a4aSGreg Roach	 * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1116a6f13a4aSGreg Roach	 * $ I18N::translate('....')
1117a6f13a4aSGreg Roach	 * $ language_settings[]
1118a6f13a4aSGreg Roach	 *
1119a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1120a6f13a4aSGreg Roach	 */
11218edd1043SGreg Roach	private function varStartHandler($attrs) {
1122a6f13a4aSGreg Roach		if (empty($attrs['var'])) {
1123e8e7866bSGreg Roach			throw new \DomainException('REPORT ERROR var: The attribute "var=" is missing or not set in the XML file on line: ' . xml_get_current_line_number($this->parser));
1124a6f13a4aSGreg Roach		}
1125a6f13a4aSGreg Roach
1126a6f13a4aSGreg Roach		$var = $attrs['var'];
1127a6f13a4aSGreg Roach		// SetVar element preset variables
1128d1286247SGreg Roach		if (!empty($this->vars[$var]['id'])) {
1129d1286247SGreg Roach			$var = $this->vars[$var]['id'];
1130a6f13a4aSGreg Roach		} else {
1131a6f13a4aSGreg Roach			$tfact = $this->fact;
11327a6ee1acSGreg Roach			if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1133a6f13a4aSGreg Roach				// Use :
1134a6f13a4aSGreg Roach				// n TYPE This text if string
1135a6f13a4aSGreg Roach				$tfact = $this->type;
1136a6f13a4aSGreg Roach			}
11377a6ee1acSGreg Roach			$var = str_replace(['@fact', '@desc'], [GedcomTag::getLabel($tfact), $this->desc], $var);
1138a6f13a4aSGreg Roach			if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1139a6f13a4aSGreg Roach				$var = I18N::number($match[1]);
1140a6f13a4aSGreg Roach			} elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1141a6f13a4aSGreg Roach				$var = I18N::translate($match[1]);
1142a4956c0eSGreg Roach			} elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1143a6f13a4aSGreg Roach				$var = I18N::translateContext($match[1], $match[2]);
1144a6f13a4aSGreg Roach			}
1145a6f13a4aSGreg Roach		}
1146a6f13a4aSGreg Roach		// Check if variable is set as a date and reformat the date
1147a6f13a4aSGreg Roach		if (isset($attrs['date'])) {
11487a6ee1acSGreg Roach			if ($attrs['date'] === '1') {
1149a6f13a4aSGreg Roach				$g   = new Date($var);
1150a6f13a4aSGreg Roach				$var = $g->display();
1151a6f13a4aSGreg Roach			}
1152a6f13a4aSGreg Roach		}
1153a6f13a4aSGreg Roach		$this->current_element->addText($var);
11542836aa05SGreg Roach		$this->text = $var; // Used for title/descriptio
1155a6f13a4aSGreg Roach	}
1156a6f13a4aSGreg Roach
1157a6f13a4aSGreg Roach	/**
115876692c8bSGreg Roach	 * XML <Facts>
115976692c8bSGreg Roach	 *
1160a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1161a6f13a4aSGreg Roach	 */
11628edd1043SGreg Roach	private function factsStartHandler($attrs) {
1163a6f13a4aSGreg Roach		$this->process_repeats++;
1164a6f13a4aSGreg Roach		if ($this->process_repeats > 1) {
1165a6f13a4aSGreg Roach			return;
1166a6f13a4aSGreg Roach		}
1167a6f13a4aSGreg Roach
116813abd6f3SGreg Roach		array_push($this->repeats_stack, [$this->repeats, $this->repeat_bytes]);
116913abd6f3SGreg Roach		$this->repeats      = [];
1170e8e7866bSGreg Roach		$this->repeat_bytes = xml_get_current_line_number($this->parser);
1171a6f13a4aSGreg Roach
11727a6ee1acSGreg Roach		$id    = '';
117313abd6f3SGreg Roach		$match = [];
11747a6ee1acSGreg Roach		if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1175a6f13a4aSGreg Roach			$id = $match[1];
1176a6f13a4aSGreg Roach		}
11777a6ee1acSGreg Roach		$tag = '';
1178a6f13a4aSGreg Roach		if (isset($attrs['ignore'])) {
1179a6f13a4aSGreg Roach			$tag .= $attrs['ignore'];
1180a6f13a4aSGreg Roach		}
11817a6ee1acSGreg Roach		if (preg_match('/\$(.+)/', $tag, $match)) {
1182d1286247SGreg Roach			$tag = $this->vars[$match[1]]['id'];
1183a6f13a4aSGreg Roach		}
1184a6f13a4aSGreg Roach
1185*299d100dSGreg Roach		$record = GedcomRecord::getInstance($id, $this->tree);
1186a6f13a4aSGreg Roach		if (empty($attrs['diff']) && !empty($id)) {
1187a6f13a4aSGreg Roach			$facts = $record->getFacts();
11883d7a8a4cSGreg Roach			Functions::sortFacts($facts);
118913abd6f3SGreg Roach			$this->repeats = [];
1190a6f13a4aSGreg Roach			$nonfacts      = explode(',', $tag);
1191a6f13a4aSGreg Roach			foreach ($facts as $event) {
1192a6f13a4aSGreg Roach				if (!in_array($event->getTag(), $nonfacts)) {
1193a6f13a4aSGreg Roach					$this->repeats[] = $event->getGedcom();
1194a6f13a4aSGreg Roach				}
1195a6f13a4aSGreg Roach			}
1196a6f13a4aSGreg Roach		} else {
1197a6f13a4aSGreg Roach			foreach ($record->getFacts() as $fact) {
1198a6f13a4aSGreg Roach				if ($fact->isPendingAddition() && $fact->getTag() !== 'CHAN') {
1199a6f13a4aSGreg Roach					$this->repeats[] = $fact->getGedcom();
1200a6f13a4aSGreg Roach				}
1201a6f13a4aSGreg Roach			}
1202a6f13a4aSGreg Roach		}
1203a6f13a4aSGreg Roach	}
1204a6f13a4aSGreg Roach
1205a6f13a4aSGreg Roach	/**
120676692c8bSGreg Roach	 * XML </Facts>
1207a6f13a4aSGreg Roach	 */
12088edd1043SGreg Roach	private function factsEndHandler() {
1209a6f13a4aSGreg Roach		$this->process_repeats--;
1210a6f13a4aSGreg Roach		if ($this->process_repeats > 0) {
1211a6f13a4aSGreg Roach			return;
1212a6f13a4aSGreg Roach		}
1213a6f13a4aSGreg Roach
1214a6f13a4aSGreg Roach		// Check if there is anything to repeat
1215a6f13a4aSGreg Roach		if (count($this->repeats) > 0) {
1216e8e7866bSGreg Roach			$line       = xml_get_current_line_number($this->parser) - 1;
1217a6f13a4aSGreg Roach			$lineoffset = 0;
1218a6f13a4aSGreg Roach			foreach ($this->repeats_stack as $rep) {
1219a6f13a4aSGreg Roach				$lineoffset += $rep[1];
1220a6f13a4aSGreg Roach			}
1221a6f13a4aSGreg Roach
1222a6f13a4aSGreg Roach			//-- read the xml from the file
1223*299d100dSGreg Roach			$lines = file($this->report);
1224a6f13a4aSGreg Roach			while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1225a6f13a4aSGreg Roach				$lineoffset--;
1226a6f13a4aSGreg Roach			}
1227a6f13a4aSGreg Roach			$lineoffset++;
1228a6f13a4aSGreg Roach			$reportxml = "<tempdoc>\n";
1229a6f13a4aSGreg Roach			$i         = $line + $lineoffset;
1230a6f13a4aSGreg Roach			$line_nr   = $this->repeat_bytes + $lineoffset;
1231a6f13a4aSGreg Roach			while ($line_nr < $i) {
1232a6f13a4aSGreg Roach				$reportxml .= $lines[$line_nr];
1233a6f13a4aSGreg Roach				$line_nr++;
1234a6f13a4aSGreg Roach			}
1235a6f13a4aSGreg Roach			// No need to drag this
1236a6f13a4aSGreg Roach			unset($lines);
1237a6f13a4aSGreg Roach			$reportxml .= "</tempdoc>\n";
1238a6f13a4aSGreg Roach			// Save original values
1239e8e7866bSGreg Roach			array_push($this->parser_stack, $this->parser);
1240a6f13a4aSGreg Roach			$oldgedrec = $this->gedrec;
1241a6f13a4aSGreg Roach			$count     = count($this->repeats);
1242a6f13a4aSGreg Roach			$i         = 0;
1243a6f13a4aSGreg Roach			while ($i < $count) {
1244a6f13a4aSGreg Roach				$this->gedrec = $this->repeats[$i];
1245a6f13a4aSGreg Roach				$this->fact   = '';
1246a6f13a4aSGreg Roach				$this->desc   = '';
1247a6f13a4aSGreg Roach				if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1248a6f13a4aSGreg Roach					$this->fact = $match[1];
1249a6f13a4aSGreg Roach					if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
125013abd6f3SGreg Roach						$tmatch = [];
1251a6f13a4aSGreg Roach						if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1252a6f13a4aSGreg Roach							$this->type = trim($tmatch[1]);
1253a6f13a4aSGreg Roach						} else {
1254a6f13a4aSGreg Roach							$this->type = ' ';
1255a6f13a4aSGreg Roach						}
1256a6f13a4aSGreg Roach					}
1257a6f13a4aSGreg Roach					$this->desc = trim($match[2]);
12583d7a8a4cSGreg Roach					$this->desc .= Functions::getCont(2, $this->gedrec);
1259a6f13a4aSGreg Roach				}
1260a6f13a4aSGreg Roach				$repeat_parser = xml_parser_create();
1261e8e7866bSGreg Roach				$this->parser  = $repeat_parser;
1262a6f13a4aSGreg Roach				xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
126313abd6f3SGreg Roach				xml_set_element_handler($repeat_parser, [$this, 'startElement'], [$this, 'endElement']);
126413abd6f3SGreg Roach				xml_set_character_data_handler($repeat_parser, [$this, 'characterData']);
1265a6f13a4aSGreg Roach				if (!xml_parse($repeat_parser, $reportxml, true)) {
1266a6f13a4aSGreg Roach					throw new \DomainException(sprintf(
1267a6f13a4aSGreg Roach						'FactsEHandler XML error: %s at line %d',
1268a6f13a4aSGreg Roach						xml_error_string(xml_get_error_code($repeat_parser)),
1269a6f13a4aSGreg Roach						xml_get_current_line_number($repeat_parser)
1270a6f13a4aSGreg Roach					));
1271a6f13a4aSGreg Roach				}
1272a6f13a4aSGreg Roach				xml_parser_free($repeat_parser);
1273a6f13a4aSGreg Roach				$i++;
1274a6f13a4aSGreg Roach			}
1275a6f13a4aSGreg Roach			// Restore original values
1276e8e7866bSGreg Roach			$this->parser = array_pop($this->parser_stack);
1277a6f13a4aSGreg Roach			$this->gedrec = $oldgedrec;
1278a6f13a4aSGreg Roach		}
1279a6f13a4aSGreg Roach		list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1280a6f13a4aSGreg Roach	}
1281a6f13a4aSGreg Roach
1282a6f13a4aSGreg Roach	/**
1283a6f13a4aSGreg Roach	 * Setting upp or changing variables in the XML
1284d1286247SGreg Roach	 * The XML variable name and value is stored in $this->vars
1285a6f13a4aSGreg Roach	 *
1286a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1287a6f13a4aSGreg Roach	 */
12888edd1043SGreg Roach	private function setVarStartHandler($attrs) {
1289a6f13a4aSGreg Roach		if (empty($attrs['name'])) {
1290a6f13a4aSGreg Roach			throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1291a6f13a4aSGreg Roach		}
1292a6f13a4aSGreg Roach
1293a6f13a4aSGreg Roach		$name  = $attrs['name'];
1294a6f13a4aSGreg Roach		$value = $attrs['value'];
129513abd6f3SGreg Roach		$match = [];
1296a6f13a4aSGreg Roach		// Current GEDCOM record strings
12977a6ee1acSGreg Roach		if ($value == '@ID') {
12987a6ee1acSGreg Roach			if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1299a6f13a4aSGreg Roach				$value = $match[1];
1300a6f13a4aSGreg Roach			}
13017a6ee1acSGreg Roach		} elseif ($value == '@fact') {
1302a6f13a4aSGreg Roach			$value = $this->fact;
13037a6ee1acSGreg Roach		} elseif ($value == '@desc') {
1304a6f13a4aSGreg Roach			$value = $this->desc;
13057a6ee1acSGreg Roach		} elseif ($value == '@generation') {
1306a6f13a4aSGreg Roach			$value = $this->generation;
1307a6f13a4aSGreg Roach		} elseif (preg_match("/@(\w+)/", $value, $match)) {
130813abd6f3SGreg Roach			$gmatch = [];
1309a6f13a4aSGreg Roach			if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
13107a6ee1acSGreg Roach				$value = str_replace('@', '', trim($gmatch[1]));
1311a6f13a4aSGreg Roach			}
1312a6f13a4aSGreg Roach		}
1313a6f13a4aSGreg Roach		if (preg_match("/\\$(\w+)/", $name, $match)) {
1314d1286247SGreg Roach			$name = $this->vars["'" . $match[1] . "'"]['id'];
1315a6f13a4aSGreg Roach		}
1316a6f13a4aSGreg Roach		$count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1317a6f13a4aSGreg Roach		$i     = 0;
1318a6f13a4aSGreg Roach		while ($i < $count) {
1319d1286247SGreg Roach			$t     = $this->vars[$match[$i][1]]['id'];
13207a6ee1acSGreg Roach			$value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1321a6f13a4aSGreg Roach			$i++;
1322a6f13a4aSGreg Roach		}
1323a6f13a4aSGreg Roach		if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1324a6f13a4aSGreg Roach			$value = I18N::number($match[1]);
1325a6f13a4aSGreg Roach		} elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1326a6f13a4aSGreg Roach			$value = I18N::translate($match[1]);
1327a4956c0eSGreg Roach		} elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1328a6f13a4aSGreg Roach			$value = I18N::translateContext($match[1], $match[2]);
1329a6f13a4aSGreg Roach		}
1330a6f13a4aSGreg Roach		// Arithmetic functions
1331a6f13a4aSGreg Roach		if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
1332a6f13a4aSGreg Roach			switch ($match[2]) {
13337a6ee1acSGreg Roach				case '+':
1334a6f13a4aSGreg Roach					$t     = $match[1] + $match[3];
13357a6ee1acSGreg Roach					$value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1336a6f13a4aSGreg Roach					break;
13377a6ee1acSGreg Roach				case '-':
1338a6f13a4aSGreg Roach					$t     = $match[1] - $match[3];
13397a6ee1acSGreg Roach					$value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1340a6f13a4aSGreg Roach					break;
13417a6ee1acSGreg Roach				case '*':
1342a6f13a4aSGreg Roach					$t     = $match[1] * $match[3];
13437a6ee1acSGreg Roach					$value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1344a6f13a4aSGreg Roach					break;
13457a6ee1acSGreg Roach				case '/':
1346a6f13a4aSGreg Roach					$t     = $match[1] / $match[3];
13477a6ee1acSGreg Roach					$value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1348a6f13a4aSGreg Roach					break;
1349a6f13a4aSGreg Roach			}
1350a6f13a4aSGreg Roach		}
13517a6ee1acSGreg Roach		if (strpos($value, '@') !== false) {
13527a6ee1acSGreg Roach			$value = '';
1353a6f13a4aSGreg Roach		}
1354d1286247SGreg Roach		$this->vars[$name]['id'] = $value;
1355a6f13a4aSGreg Roach	}
1356a6f13a4aSGreg Roach
1357a6f13a4aSGreg Roach	/**
1358a6f13a4aSGreg Roach	 * XML <if > start element
1359a6f13a4aSGreg Roach	 *
1360a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1361a6f13a4aSGreg Roach	 */
13628edd1043SGreg Roach	private function ifStartHandler($attrs) {
1363a6f13a4aSGreg Roach		if ($this->process_ifs > 0) {
1364a6f13a4aSGreg Roach			$this->process_ifs++;
1365a6f13a4aSGreg Roach
1366a6f13a4aSGreg Roach			return;
1367a6f13a4aSGreg Roach		}
1368a6f13a4aSGreg Roach
1369a6f13a4aSGreg Roach		$condition = $attrs['condition'];
137082759250SGreg Roach		$condition = $this->substituteVars($condition, true);
13717a6ee1acSGreg Roach		$condition = str_replace([' LT ', ' GT '], ['<', '>'], $condition);
1372a6f13a4aSGreg Roach		// Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
13737a6ee1acSGreg Roach		$condition = str_replace('@fact:', $this->fact . ':', $condition);
137413abd6f3SGreg Roach		$match     = [];
1375a6f13a4aSGreg Roach		$count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1376a6f13a4aSGreg Roach		$i         = 0;
1377a6f13a4aSGreg Roach		while ($i < $count) {
1378a6f13a4aSGreg Roach			$id    = $match[$i][1];
1379a6f13a4aSGreg Roach			$value = '""';
13807a6ee1acSGreg Roach			if ($id == 'ID') {
13817a6ee1acSGreg Roach				if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1382a6f13a4aSGreg Roach					$value = "'" . $match[1] . "'";
1383a6f13a4aSGreg Roach				}
13847a6ee1acSGreg Roach			} elseif ($id === 'fact') {
1385a6f13a4aSGreg Roach				$value = '"' . $this->fact . '"';
13867a6ee1acSGreg Roach			} elseif ($id === 'desc') {
1387a6f13a4aSGreg Roach				$value = '"' . addslashes($this->desc) . '"';
13887a6ee1acSGreg Roach			} elseif ($id === 'generation') {
1389a6f13a4aSGreg Roach				$value = '"' . $this->generation . '"';
1390a6f13a4aSGreg Roach			} else {
13917a6ee1acSGreg Roach				$temp  = explode(' ', trim($this->gedrec));
1392a6f13a4aSGreg Roach				$level = $temp[0];
1393a6f13a4aSGreg Roach				if ($level == 0) {
1394a6f13a4aSGreg Roach					$level++;
1395a6f13a4aSGreg Roach				}
13963d7a8a4cSGreg Roach				$value = $this->getGedcomValue($id, $level, $this->gedrec);
1397a6f13a4aSGreg Roach				if (empty($value)) {
1398a6f13a4aSGreg Roach					$level++;
13993d7a8a4cSGreg Roach					$value = $this->getGedcomValue($id, $level, $this->gedrec);
1400a6f13a4aSGreg Roach				}
14015e8c88c1SGreg Roach				$value = preg_replace('/^@(' . WT_REGEX_XREF . ')@$/', '$1', $value);
14025e8c88c1SGreg Roach				$value = '"' . addslashes($value) . '"';
1403a6f13a4aSGreg Roach			}
1404a6f13a4aSGreg Roach			$condition = str_replace("@$id", $value, $condition);
1405a6f13a4aSGreg Roach			$i++;
1406a6f13a4aSGreg Roach		}
14075e8c88c1SGreg Roach		$ret = eval("return (bool) ($condition);");
1408a6f13a4aSGreg Roach		if (!$ret) {
1409a6f13a4aSGreg Roach			$this->process_ifs++;
1410a6f13a4aSGreg Roach		}
1411a6f13a4aSGreg Roach	}
1412a6f13a4aSGreg Roach
1413a6f13a4aSGreg Roach	/**
1414a6f13a4aSGreg Roach	 * XML <if /> end element
1415a6f13a4aSGreg Roach	 */
14168edd1043SGreg Roach	private function ifEndHandler() {
1417a6f13a4aSGreg Roach		if ($this->process_ifs > 0) {
1418a6f13a4aSGreg Roach			$this->process_ifs--;
1419a6f13a4aSGreg Roach		}
1420a6f13a4aSGreg Roach	}
1421a6f13a4aSGreg Roach
1422a6f13a4aSGreg Roach	/**
1423a6f13a4aSGreg Roach	 * XML <Footnote > start element
1424a6f13a4aSGreg Roach	 * Collect the Footnote links
1425a6f13a4aSGreg Roach	 * GEDCOM Records that are protected by Privacy setting will be ignore
1426a6f13a4aSGreg Roach	 *
1427a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1428a6f13a4aSGreg Roach	 */
14298edd1043SGreg Roach	private function footnoteStartHandler($attrs) {
14307a6ee1acSGreg Roach		$id = '';
14317a6ee1acSGreg Roach		if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1432a6f13a4aSGreg Roach			$id = $match[2];
1433a6f13a4aSGreg Roach		}
1434*299d100dSGreg Roach		$record = GedcomRecord::getInstance($id, $this->tree);
1435a6f13a4aSGreg Roach		if ($record && $record->canShow()) {
1436a6f13a4aSGreg Roach			array_push($this->print_data_stack, $this->print_data);
1437a6f13a4aSGreg Roach			$this->print_data = true;
14387a6ee1acSGreg Roach			$style            = '';
1439a6f13a4aSGreg Roach			if (!empty($attrs['style'])) {
1440a6f13a4aSGreg Roach				$style = $attrs['style'];
1441a6f13a4aSGreg Roach			}
1442a6f13a4aSGreg Roach			$this->footnote_element = $this->current_element;
1443e8e7866bSGreg Roach			$this->current_element  = $this->report_root->createFootnote($style);
1444a6f13a4aSGreg Roach		} else {
1445a6f13a4aSGreg Roach			$this->print_data       = false;
1446a6f13a4aSGreg Roach			$this->process_footnote = false;
1447a6f13a4aSGreg Roach		}
1448a6f13a4aSGreg Roach	}
1449a6f13a4aSGreg Roach
1450a6f13a4aSGreg Roach	/**
1451a6f13a4aSGreg Roach	 * XML <Footnote /> end element
1452a6f13a4aSGreg Roach	 * Print the collected Footnote data
1453a6f13a4aSGreg Roach	 */
14548edd1043SGreg Roach	private function footnoteEndHandler() {
1455a6f13a4aSGreg Roach		if ($this->process_footnote) {
1456a6f13a4aSGreg Roach			$this->print_data = array_pop($this->print_data_stack);
1457a6f13a4aSGreg Roach			$temp             = trim($this->current_element->getValue());
1458a6f13a4aSGreg Roach			if (strlen($temp) > 3) {
1459e8e7866bSGreg Roach				$this->wt_report->addElement($this->current_element);
1460a6f13a4aSGreg Roach			}
1461a6f13a4aSGreg Roach			$this->current_element = $this->footnote_element;
1462a6f13a4aSGreg Roach		} else {
1463a6f13a4aSGreg Roach			$this->process_footnote = true;
1464a6f13a4aSGreg Roach		}
1465a6f13a4aSGreg Roach	}
1466a6f13a4aSGreg Roach
1467a6f13a4aSGreg Roach	/**
1468a6f13a4aSGreg Roach	 * XML <FootnoteTexts /> element
1469a6f13a4aSGreg Roach	 */
14708edd1043SGreg Roach	private function footnoteTextsStartHandler() {
14717a6ee1acSGreg Roach		$temp = 'footnotetexts';
1472e8e7866bSGreg Roach		$this->wt_report->addElement($temp);
1473a6f13a4aSGreg Roach	}
1474a6f13a4aSGreg Roach
1475a6f13a4aSGreg Roach	/**
1476a6f13a4aSGreg Roach	 * XML <AgeAtDeath /> element handler
1477a6f13a4aSGreg Roach	 */
14788edd1043SGreg Roach	private function ageAtDeathStartHandler() {
14793d7a8a4cSGreg Roach		// This duplicates functionality in FunctionsPrint::format_fact_date()
1480*299d100dSGreg Roach		global $factrec;
1481a6f13a4aSGreg Roach
148213abd6f3SGreg Roach		$match = [];
14837a6ee1acSGreg Roach		if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1484*299d100dSGreg Roach			$person = Individual::getInstance($match[1], $this->tree);
1485a6f13a4aSGreg Roach			// Recorded age
1486a6f13a4aSGreg Roach			if (preg_match('/\n2 AGE (.+)/', $factrec, $match)) {
1487a6f13a4aSGreg Roach				$fact_age = $match[1];
1488a6f13a4aSGreg Roach			} else {
1489a6f13a4aSGreg Roach				$fact_age = '';
1490a6f13a4aSGreg Roach			}
1491a6f13a4aSGreg Roach			if (preg_match('/\n2 HUSB\n3 AGE (.+)/', $factrec, $match)) {
1492a6f13a4aSGreg Roach				$husb_age = $match[1];
1493a6f13a4aSGreg Roach			} else {
1494a6f13a4aSGreg Roach				$husb_age = '';
1495a6f13a4aSGreg Roach			}
1496a6f13a4aSGreg Roach			if (preg_match('/\n2 WIFE\n3 AGE (.+)/', $factrec, $match)) {
1497a6f13a4aSGreg Roach				$wife_age = $match[1];
1498a6f13a4aSGreg Roach			} else {
1499a6f13a4aSGreg Roach				$wife_age = '';
1500a6f13a4aSGreg Roach			}
1501a6f13a4aSGreg Roach
1502a6f13a4aSGreg Roach			// Calculated age
1503a6f13a4aSGreg Roach			$birth_date = $person->getBirthDate();
1504a6f13a4aSGreg Roach			// Can't use getDeathDate(), as this also gives BURI/CREM events, which
1505a6f13a4aSGreg Roach			// wouldn't give the correct "days after death" result for people with
1506a6f13a4aSGreg Roach			// no DEAT.
1507a6f13a4aSGreg Roach			$death_event = $person->getFirstFact('DEAT');
1508a6f13a4aSGreg Roach			if ($death_event) {
1509a6f13a4aSGreg Roach				$death_date = $death_event->getDate();
1510a6f13a4aSGreg Roach			} else {
1511a6f13a4aSGreg Roach				$death_date = new Date('');
1512a6f13a4aSGreg Roach			}
1513a6f13a4aSGreg Roach			$value = '';
1514a6f13a4aSGreg Roach			if (Date::compare($birth_date, $death_date) <= 0 || !$person->isDead()) {
1515a6f13a4aSGreg Roach				$age = Date::getAgeGedcom($birth_date, $death_date);
1516a6f13a4aSGreg Roach				// Only show calculated age if it differs from recorded age
15177a6ee1acSGreg Roach				if ($age != '' && $age != '0d') {
1518a6f13a4aSGreg Roach					if ($fact_age != '' && $fact_age != $age || $fact_age == '' && $husb_age == '' && $wife_age == '' || $husb_age != '' && $person->getSex() == 'M' && $husb_age != $age || $wife_age != '' && $person->getSex() == 'F' && $wife_age != $age
1519a6f13a4aSGreg Roach					) {
1520d6aa7ab2SGreg Roach						$value  = FunctionsDate::getAgeAtEvent($age);
1521a6f13a4aSGreg Roach						$abbrev = substr($value, 0, strpos($value, ' ') + 5);
1522a6f13a4aSGreg Roach						if ($value !== $abbrev) {
1523a6f13a4aSGreg Roach							$value = $abbrev . '.';
1524a6f13a4aSGreg Roach						}
1525a6f13a4aSGreg Roach					}
1526a6f13a4aSGreg Roach				}
1527a6f13a4aSGreg Roach			}
1528a6f13a4aSGreg Roach			$this->current_element->addText($value);
1529a6f13a4aSGreg Roach		}
1530a6f13a4aSGreg Roach	}
1531a6f13a4aSGreg Roach
1532a6f13a4aSGreg Roach	/**
1533a6f13a4aSGreg Roach	 * XML element Forced line break handler - HTML code
1534a6f13a4aSGreg Roach	 */
15358edd1043SGreg Roach	private function brStartHandler() {
1536a6f13a4aSGreg Roach		if ($this->print_data && $this->process_gedcoms === 0) {
1537a6f13a4aSGreg Roach			$this->current_element->addText('<br>');
1538a6f13a4aSGreg Roach		}
1539a6f13a4aSGreg Roach	}
1540a6f13a4aSGreg Roach
1541a6f13a4aSGreg Roach	/**
1542a6f13a4aSGreg Roach	 * XML <sp />element Forced space handler
1543a6f13a4aSGreg Roach	 */
15448edd1043SGreg Roach	private function spStartHandler() {
1545a6f13a4aSGreg Roach		if ($this->print_data && $this->process_gedcoms === 0) {
1546a6f13a4aSGreg Roach			$this->current_element->addText(' ');
1547a6f13a4aSGreg Roach		}
1548a6f13a4aSGreg Roach	}
1549a6f13a4aSGreg Roach
1550a6f13a4aSGreg Roach	/**
155176692c8bSGreg Roach	 * XML <HighlightedImage/>
155276692c8bSGreg Roach	 *
1553a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1554a6f13a4aSGreg Roach	 */
15558edd1043SGreg Roach	private function highlightedImageStartHandler($attrs) {
1556a6f13a4aSGreg Roach		$id    = '';
155713abd6f3SGreg Roach		$match = [];
15587a6ee1acSGreg Roach		if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1559a6f13a4aSGreg Roach			$id = $match[1];
1560a6f13a4aSGreg Roach		}
1561a6f13a4aSGreg Roach
1562a6f13a4aSGreg Roach		// mixed Position the top corner of this box on the page. the default is the current position
1563a6f13a4aSGreg Roach		$top = '.';
1564a6f13a4aSGreg Roach		if (isset($attrs['top'])) {
1565a6f13a4aSGreg Roach			if ($attrs['top'] === '0') {
1566a6f13a4aSGreg Roach				$top = 0;
1567a6f13a4aSGreg Roach			} elseif ($attrs['top'] === '.') {
1568a6f13a4aSGreg Roach				$top = '.';
1569a6f13a4aSGreg Roach			} elseif (!empty($attrs['top'])) {
1570a6f13a4aSGreg Roach				$top = (int) $attrs['top'];
1571a6f13a4aSGreg Roach			}
1572a6f13a4aSGreg Roach		}
1573a6f13a4aSGreg Roach
1574a6f13a4aSGreg Roach		// mixed Position the left corner of this box on the page. the default is the current position
1575a6f13a4aSGreg Roach		$left = '.';
1576a6f13a4aSGreg Roach		if (isset($attrs['left'])) {
1577a6f13a4aSGreg Roach			if ($attrs['left'] === '0') {
1578a6f13a4aSGreg Roach				$left = 0;
1579a6f13a4aSGreg Roach			} elseif ($attrs['left'] === '.') {
1580a6f13a4aSGreg Roach				$left = '.';
1581a6f13a4aSGreg Roach			} elseif (!empty($attrs['left'])) {
1582a6f13a4aSGreg Roach				$left = (int) $attrs['left'];
1583a6f13a4aSGreg Roach			}
1584a6f13a4aSGreg Roach		}
1585a6f13a4aSGreg Roach
1586a6f13a4aSGreg Roach		// string Align the image in left, center, right
1587a6f13a4aSGreg Roach		$align = '';
1588a6f13a4aSGreg Roach		if (!empty($attrs['align'])) {
1589a6f13a4aSGreg Roach			$align = $attrs['align'];
1590a6f13a4aSGreg Roach		}
1591a6f13a4aSGreg Roach
1592a6f13a4aSGreg Roach		// string Next Line should be T:next to the image, N:next line
1593a6f13a4aSGreg Roach		$ln = '';
1594a6f13a4aSGreg Roach		if (!empty($attrs['ln'])) {
1595a6f13a4aSGreg Roach			$ln = $attrs['ln'];
1596a6f13a4aSGreg Roach		}
1597a6f13a4aSGreg Roach
1598a6f13a4aSGreg Roach		$width  = 0;
1599a6f13a4aSGreg Roach		$height = 0;
1600a6f13a4aSGreg Roach		if (!empty($attrs['width'])) {
1601a6f13a4aSGreg Roach			$width = (int) $attrs['width'];
1602a6f13a4aSGreg Roach		}
1603a6f13a4aSGreg Roach		if (!empty($attrs['height'])) {
1604a6f13a4aSGreg Roach			$height = (int) $attrs['height'];
1605a6f13a4aSGreg Roach		}
1606a6f13a4aSGreg Roach
1607*299d100dSGreg Roach		$person      = Individual::getInstance($id, $this->tree);
16084a9f750fSGreg Roach		$media_file = $person->findHighlightedMediaFile();
16094a9f750fSGreg Roach		if ($media_file) {
16104a9f750fSGreg Roach			$attributes = $media_file->getImageAttributes();
16114a9f750fSGreg Roach			if ($media_file->fileExists()) {
1612a6f13a4aSGreg Roach				if ($width > 0 && $height == 0) {
16133c3b90deSGreg Roach					$perc   = $width / $attributes[0];
16143c3b90deSGreg Roach					$height = round($attributes[1] * $perc);
1615a6f13a4aSGreg Roach				} elseif ($height > 0 && $width == 0) {
16163c3b90deSGreg Roach					$perc  = $height / $attributes[1];
16173c3b90deSGreg Roach					$width = round($attributes[0] * $perc);
1618a6f13a4aSGreg Roach				} else {
16193c3b90deSGreg Roach					$width  = $attributes[0];
16203c3b90deSGreg Roach					$height = $attributes[1];
1621a6f13a4aSGreg Roach				}
16224a9f750fSGreg Roach				$image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1623e8e7866bSGreg Roach				$this->wt_report->addElement($image);
1624a6f13a4aSGreg Roach			}
1625a6f13a4aSGreg Roach		}
1626a6f13a4aSGreg Roach	}
1627a6f13a4aSGreg Roach
1628a6f13a4aSGreg Roach	/**
162976692c8bSGreg Roach	 * XML <Image/>
163076692c8bSGreg Roach	 *
1631a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1632a6f13a4aSGreg Roach	 */
16338edd1043SGreg Roach	private function imageStartHandler($attrs) {
1634a6f13a4aSGreg Roach		// mixed Position the top corner of this box on the page. the default is the current position
1635a6f13a4aSGreg Roach		$top = '.';
1636a6f13a4aSGreg Roach		if (isset($attrs['top'])) {
16377a6ee1acSGreg Roach			if ($attrs['top'] === '0') {
1638a6f13a4aSGreg Roach				$top = 0;
1639a6f13a4aSGreg Roach			} elseif ($attrs['top'] === '.') {
1640a6f13a4aSGreg Roach				$top = '.';
1641a6f13a4aSGreg Roach			} elseif (!empty($attrs['top'])) {
1642a6f13a4aSGreg Roach				$top = (int) $attrs['top'];
1643a6f13a4aSGreg Roach			}
1644a6f13a4aSGreg Roach		}
1645a6f13a4aSGreg Roach
1646a6f13a4aSGreg Roach		// mixed Position the left corner of this box on the page. the default is the current position
1647a6f13a4aSGreg Roach		$left = '.';
1648a6f13a4aSGreg Roach		if (isset($attrs['left'])) {
1649a6f13a4aSGreg Roach			if ($attrs['left'] === '0') {
1650a6f13a4aSGreg Roach				$left = 0;
1651a6f13a4aSGreg Roach			} elseif ($attrs['left'] === '.') {
1652a6f13a4aSGreg Roach				$left = '.';
1653a6f13a4aSGreg Roach			} elseif (!empty($attrs['left'])) {
1654a6f13a4aSGreg Roach				$left = (int) $attrs['left'];
1655a6f13a4aSGreg Roach			}
1656a6f13a4aSGreg Roach		}
1657a6f13a4aSGreg Roach
1658a6f13a4aSGreg Roach		// string Align the image in left, center, right
1659a6f13a4aSGreg Roach		$align = '';
1660a6f13a4aSGreg Roach		if (!empty($attrs['align'])) {
1661a6f13a4aSGreg Roach			$align = $attrs['align'];
1662a6f13a4aSGreg Roach		}
1663a6f13a4aSGreg Roach
1664a6f13a4aSGreg Roach		// string Next Line should be T:next to the image, N:next line
1665a6f13a4aSGreg Roach		$ln = 'T';
1666a6f13a4aSGreg Roach		if (!empty($attrs['ln'])) {
1667a6f13a4aSGreg Roach			$ln = $attrs['ln'];
1668a6f13a4aSGreg Roach		}
1669a6f13a4aSGreg Roach
1670a6f13a4aSGreg Roach		$width  = 0;
1671a6f13a4aSGreg Roach		$height = 0;
1672a6f13a4aSGreg Roach		if (!empty($attrs['width'])) {
1673a6f13a4aSGreg Roach			$width = (int) $attrs['width'];
1674a6f13a4aSGreg Roach		}
1675a6f13a4aSGreg Roach		if (!empty($attrs['height'])) {
1676a6f13a4aSGreg Roach			$height = (int) $attrs['height'];
1677a6f13a4aSGreg Roach		}
1678a6f13a4aSGreg Roach
1679a6f13a4aSGreg Roach		$file = '';
1680a6f13a4aSGreg Roach		if (!empty($attrs['file'])) {
1681a6f13a4aSGreg Roach			$file = $attrs['file'];
1682a6f13a4aSGreg Roach		}
16837a6ee1acSGreg Roach		if ($file == '@FILE') {
168413abd6f3SGreg Roach			$match = [];
1685a6f13a4aSGreg Roach			if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1686*299d100dSGreg Roach				$mediaobject = Media::getInstance($match[1], $this->tree);
16874a9f750fSGreg Roach				$media_file  = $mediaobject->firstImageFile();
1688cdf416fbSGreg Roach
16894a9f750fSGreg Roach				if ($media_file !== null && $media_file->fileExists()) {
16904a9f750fSGreg Roach					$attributes = $media_file->getImageAttributes();
1691a6f13a4aSGreg Roach					if ($width > 0 && $height == 0) {
16923c3b90deSGreg Roach						$perc   = $width / $attributes[0];
16933c3b90deSGreg Roach						$height = round($attributes[1] * $perc);
1694a6f13a4aSGreg Roach					} elseif ($height > 0 && $width == 0) {
16953c3b90deSGreg Roach						$perc  = $height / $attributes[1];
16963c3b90deSGreg Roach						$width = round($attributes[0] * $perc);
1697a6f13a4aSGreg Roach					} else {
16983c3b90deSGreg Roach						$width  = $attributes[0];
16993c3b90deSGreg Roach						$height = $attributes[1];
1700a6f13a4aSGreg Roach					}
17014a9f750fSGreg Roach					$image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1702e8e7866bSGreg Roach					$this->wt_report->addElement($image);
1703a6f13a4aSGreg Roach				}
1704a6f13a4aSGreg Roach			}
1705a6f13a4aSGreg Roach		} else {
17067a6ee1acSGreg Roach			if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1707a6f13a4aSGreg Roach				$size = getimagesize($file);
1708a6f13a4aSGreg Roach				if ($width > 0 && $height == 0) {
1709a6f13a4aSGreg Roach					$perc   = $width / $size[0];
1710a6f13a4aSGreg Roach					$height = round($size[1] * $perc);
1711a6f13a4aSGreg Roach				} elseif ($height > 0 && $width == 0) {
1712a6f13a4aSGreg Roach					$perc  = $height / $size[1];
1713a6f13a4aSGreg Roach					$width = round($size[0] * $perc);
1714a6f13a4aSGreg Roach				} else {
1715a6f13a4aSGreg Roach					$width  = $size[0];
1716a6f13a4aSGreg Roach					$height = $size[1];
1717a6f13a4aSGreg Roach				}
1718e8e7866bSGreg Roach				$image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1719e8e7866bSGreg Roach				$this->wt_report->addElement($image);
1720a6f13a4aSGreg Roach			}
1721a6f13a4aSGreg Roach		}
1722a6f13a4aSGreg Roach	}
1723a6f13a4aSGreg Roach
1724a6f13a4aSGreg Roach	/**
1725a6f13a4aSGreg Roach	 * XML <Line> element handler
1726a6f13a4aSGreg Roach	 *
1727a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1728a6f13a4aSGreg Roach	 */
17298edd1043SGreg Roach	private function lineStartHandler($attrs) {
1730a6f13a4aSGreg Roach		// Start horizontal position, current position (default)
17317a6ee1acSGreg Roach		$x1 = '.';
1732a6f13a4aSGreg Roach		if (isset($attrs['x1'])) {
17337a6ee1acSGreg Roach			if ($attrs['x1'] === '0') {
1734a6f13a4aSGreg Roach				$x1 = 0;
17357a6ee1acSGreg Roach			} elseif ($attrs['x1'] === '.') {
17367a6ee1acSGreg Roach				$x1 = '.';
1737a6f13a4aSGreg Roach			} elseif (!empty($attrs['x1'])) {
1738a6f13a4aSGreg Roach				$x1 = (int) $attrs['x1'];
1739a6f13a4aSGreg Roach			}
1740a6f13a4aSGreg Roach		}
1741a6f13a4aSGreg Roach		// Start vertical position, current position (default)
17427a6ee1acSGreg Roach		$y1 = '.';
1743a6f13a4aSGreg Roach		if (isset($attrs['y1'])) {
17447a6ee1acSGreg Roach			if ($attrs['y1'] === '0') {
1745a6f13a4aSGreg Roach				$y1 = 0;
17467a6ee1acSGreg Roach			} elseif ($attrs['y1'] === '.') {
17477a6ee1acSGreg Roach				$y1 = '.';
1748a6f13a4aSGreg Roach			} elseif (!empty($attrs['y1'])) {
1749a6f13a4aSGreg Roach				$y1 = (int) $attrs['y1'];
1750a6f13a4aSGreg Roach			}
1751a6f13a4aSGreg Roach		}
1752a6f13a4aSGreg Roach		// End horizontal position, maximum width (default)
17537a6ee1acSGreg Roach		$x2 = '.';
1754a6f13a4aSGreg Roach		if (isset($attrs['x2'])) {
17557a6ee1acSGreg Roach			if ($attrs['x2'] === '0') {
1756a6f13a4aSGreg Roach				$x2 = 0;
17577a6ee1acSGreg Roach			} elseif ($attrs['x2'] === '.') {
17587a6ee1acSGreg Roach				$x2 = '.';
1759a6f13a4aSGreg Roach			} elseif (!empty($attrs['x2'])) {
1760a6f13a4aSGreg Roach				$x2 = (int) $attrs['x2'];
1761a6f13a4aSGreg Roach			}
1762a6f13a4aSGreg Roach		}
1763a6f13a4aSGreg Roach		// End vertical position
17647a6ee1acSGreg Roach		$y2 = '.';
1765a6f13a4aSGreg Roach		if (isset($attrs['y2'])) {
17667a6ee1acSGreg Roach			if ($attrs['y2'] === '0') {
1767a6f13a4aSGreg Roach				$y2 = 0;
17687a6ee1acSGreg Roach			} elseif ($attrs['y2'] === '.') {
17697a6ee1acSGreg Roach				$y2 = '.';
1770a6f13a4aSGreg Roach			} elseif (!empty($attrs['y2'])) {
1771a6f13a4aSGreg Roach				$y2 = (int) $attrs['y2'];
1772a6f13a4aSGreg Roach			}
1773a6f13a4aSGreg Roach		}
1774a6f13a4aSGreg Roach
1775e8e7866bSGreg Roach		$line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1776e8e7866bSGreg Roach		$this->wt_report->addElement($line);
1777a6f13a4aSGreg Roach	}
1778a6f13a4aSGreg Roach
1779a6f13a4aSGreg Roach	/**
178076692c8bSGreg Roach	 * XML <List>
1781a6f13a4aSGreg Roach	 *
1782a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
1783a6f13a4aSGreg Roach	 */
17848edd1043SGreg Roach	private function listStartHandler($attrs) {
1785a6f13a4aSGreg Roach		$this->process_repeats++;
1786a6f13a4aSGreg Roach		if ($this->process_repeats > 1) {
1787a6f13a4aSGreg Roach			return;
1788a6f13a4aSGreg Roach		}
1789a6f13a4aSGreg Roach
179013abd6f3SGreg Roach		$match = [];
1791a6f13a4aSGreg Roach		if (isset($attrs['sortby'])) {
1792a6f13a4aSGreg Roach			$sortby = $attrs['sortby'];
1793a6f13a4aSGreg Roach			if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1794d1286247SGreg Roach				$sortby = $this->vars[$match[1]]['id'];
1795a6f13a4aSGreg Roach				$sortby = trim($sortby);
1796a6f13a4aSGreg Roach			}
1797a6f13a4aSGreg Roach		} else {
17987a6ee1acSGreg Roach			$sortby = 'NAME';
1799a6f13a4aSGreg Roach		}
1800a6f13a4aSGreg Roach
1801a6f13a4aSGreg Roach		if (isset($attrs['list'])) {
1802a6f13a4aSGreg Roach			$listname = $attrs['list'];
1803a6f13a4aSGreg Roach		} else {
18047a6ee1acSGreg Roach			$listname = 'individual';
1805a6f13a4aSGreg Roach		}
1806a6f13a4aSGreg Roach		// Some filters/sorts can be applied using SQL, while others require PHP
1807a6f13a4aSGreg Roach		switch ($listname) {
18087a6ee1acSGreg Roach			case 'pending':
1809a6f13a4aSGreg Roach				$rows = Database::prepare(
18105d0bc43dSGreg Roach					"SELECT xref, CASE new_gedcom WHEN '' THEN old_gedcom ELSE new_gedcom END AS gedcom" .
18115d0bc43dSGreg Roach					" FROM `##change`" . " WHERE (xref, change_id) IN (" .
18125d0bc43dSGreg Roach					"  SELECT xref, MAX(change_id)" .
18135d0bc43dSGreg Roach					"  FROM `##change`" .
18145d0bc43dSGreg Roach					"  WHERE status = 'pending' AND gedcom_id = :tree_id" .
18155d0bc43dSGreg Roach					"  GROUP BY xref" .
18165d0bc43dSGreg Roach					" )"
181713abd6f3SGreg Roach				)->execute([
1818*299d100dSGreg Roach					'tree_id' => $this->tree->getTreeId(),
181913abd6f3SGreg Roach				])->fetchAll();
182013abd6f3SGreg Roach				$this->list = [];
1821a6f13a4aSGreg Roach				foreach ($rows as $row) {
1822*299d100dSGreg Roach					$this->list[] = GedcomRecord::getInstance($row->xref, $this->tree, $row->gedcom);
1823a6f13a4aSGreg Roach				}
1824a6f13a4aSGreg Roach				break;
1825a6f13a4aSGreg Roach			case 'individual':
182676156db1SGreg Roach				$sql_select   = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` ";
1827a6f13a4aSGreg Roach				$sql_join     = "";
1828825006d2SGreg Roach				$sql_where    = " WHERE i_file = :tree_id";
1829a6f13a4aSGreg Roach				$sql_order_by = "";
1830*299d100dSGreg Roach				$sql_params   = ['tree_id' => $this->tree->getTreeId()];
1831a6f13a4aSGreg Roach				foreach ($attrs as $attr => $value) {
1832a6f13a4aSGreg Roach					if (strpos($attr, 'filter') === 0 && $value) {
183382759250SGreg Roach						$value = $this->substituteVars($value, false);
1834a6f13a4aSGreg Roach						// Convert the various filters into SQL
1835a6f13a4aSGreg Roach						if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1836a6f13a4aSGreg Roach							$sql_join .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)";
1837b0d2e743SGreg Roach							$sql_where .= " AND {$attr}.d_fact = :{$attr}fact";
18385d0bc43dSGreg Roach							$sql_params[$attr . 'fact'] = $match[1];
1839a6f13a4aSGreg Roach							$date                       = new Date($match[3]);
18407a6ee1acSGreg Roach							if ($match[2] == 'LTE') {
18415d0bc43dSGreg Roach								$sql_where .= " AND {$attr}.d_julianday2 <= :{$attr}date";
18425d0bc43dSGreg Roach								$sql_params[$attr . 'date'] = $date->maximumJulianDay();
1843a6f13a4aSGreg Roach							} else {
18445d0bc43dSGreg Roach								$sql_where .= " AND {$attr}.d_julianday1 >= :{$attr}date";
18455d0bc43dSGreg Roach								$sql_params[$attr . 'date'] = $date->minimumJulianDay();
1846a6f13a4aSGreg Roach							}
1847a6f13a4aSGreg Roach							if ($sortby == $match[1]) {
1848a6f13a4aSGreg Roach								$sortby = "";
1849a6f13a4aSGreg Roach								$sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1";
1850a6f13a4aSGreg Roach							}
1851a6f13a4aSGreg Roach							unset($attrs[$attr]); // This filter has been fully processed
1852a6f13a4aSGreg Roach						} elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
1853a6f13a4aSGreg Roach							// Do nothing, unless you have to
1854a6f13a4aSGreg Roach							if ($match[1] != '' || $sortby == 'NAME') {
1855a6f13a4aSGreg Roach								$sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)";
1856a6f13a4aSGreg Roach								// Search the DB only if there is any name supplied
18577a6ee1acSGreg Roach								if ($match[1] != '') {
18587a6ee1acSGreg Roach									$names = explode(' ', $match[1]);
18595d0bc43dSGreg Roach									foreach ($names as $n => $name) {
18605d0bc43dSGreg Roach										$sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
18615d0bc43dSGreg Roach										$sql_params[$attr . 'name' . $n] = $name;
1862a6f13a4aSGreg Roach									}
1863a6f13a4aSGreg Roach								}
1864a6f13a4aSGreg Roach								// Let the DB do the name sorting even when no name was entered
18657a6ee1acSGreg Roach								if ($sortby == 'NAME') {
18667a6ee1acSGreg Roach									$sortby = '';
18677a6ee1acSGreg Roach									$sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1868a6f13a4aSGreg Roach								}
1869a6f13a4aSGreg Roach							}
1870a6f13a4aSGreg Roach							unset($attrs[$attr]); // This filter has been fully processed
1871a6f13a4aSGreg Roach						} elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
18725d0bc43dSGreg Roach							$sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom";
1873b4e512fdSGreg Roach							// PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1874b4e512fdSGreg Roach							$sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1875a6f13a4aSGreg Roach							unset($attrs[$attr]); // This filter has been fully processed
1876a6f13a4aSGreg Roach						} elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1877a6f13a4aSGreg Roach							$sql_join .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)";
1878a6f13a4aSGreg Roach							$sql_join .= " JOIN `##placelinks` AS {$attr}b ON ({$attr}a.p_file = {$attr}b.pl_file AND {$attr}b.pl_p_id = {$attr}a.p_id AND {$attr}b.pl_gid = i_id)";
18795d0bc43dSGreg Roach							$sql_where .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
18805d0bc43dSGreg Roach							$sql_params[$attr . 'place'] = $match[1];
1881a6f13a4aSGreg Roach							// Don't unset this filter. This is just initial filtering
1882a6f13a4aSGreg Roach						} elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
18835d0bc43dSGreg Roach							$sql_where .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
18845d0bc43dSGreg Roach							$sql_params[$attr . 'contains1'] = $match[1];
18855d0bc43dSGreg Roach							$sql_params[$attr . 'contains2'] = $match[2];
18865d0bc43dSGreg Roach							$sql_params[$attr . 'contains3'] = $match[3];
1887a6f13a4aSGreg Roach							// Don't unset this filter. This is just initial filtering
1888a6f13a4aSGreg Roach						}
1889a6f13a4aSGreg Roach					}
1890a6f13a4aSGreg Roach				}
1891a6f13a4aSGreg Roach
189213abd6f3SGreg Roach				$this->list = [];
1893a6f13a4aSGreg Roach				$rows       = Database::prepare(
1894a6f13a4aSGreg Roach					$sql_select . $sql_join . $sql_where . $sql_order_by
18955d0bc43dSGreg Roach				)->execute($sql_params)->fetchAll();
1896a6f13a4aSGreg Roach
1897a6f13a4aSGreg Roach				foreach ($rows as $row) {
1898*299d100dSGreg Roach					$this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1899a6f13a4aSGreg Roach				}
1900a6f13a4aSGreg Roach				break;
1901a6f13a4aSGreg Roach
1902a6f13a4aSGreg Roach			case 'family':
190376156db1SGreg Roach				$sql_select   = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`";
1904a6f13a4aSGreg Roach				$sql_join     = "";
1905825006d2SGreg Roach				$sql_where    = " WHERE f_file = :tree_id";
1906a6f13a4aSGreg Roach				$sql_order_by = "";
1907*299d100dSGreg Roach				$sql_params   = ['tree_id' => $this->tree->getTreeId()];
1908a6f13a4aSGreg Roach				foreach ($attrs as $attr => $value) {
1909a6f13a4aSGreg Roach					if (strpos($attr, 'filter') === 0 && $value) {
191082759250SGreg Roach						$value = $this->substituteVars($value, false);
1911a6f13a4aSGreg Roach						// Convert the various filters into SQL
1912a6f13a4aSGreg Roach						if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1913a9eb55f8SGreg Roach							$sql_join .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)";
1914b0d2e743SGreg Roach							$sql_where .= " AND {$attr}.d_fact = :{$attr}fact";
19155d0bc43dSGreg Roach							$sql_params[$attr . 'fact'] = $match[1];
1916a6f13a4aSGreg Roach							$date                       = new Date($match[3]);
19177a6ee1acSGreg Roach							if ($match[2] == 'LTE') {
19185d0bc43dSGreg Roach								$sql_where .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19195d0bc43dSGreg Roach								$sql_params[$attr . 'date'] = $date->maximumJulianDay();
1920a6f13a4aSGreg Roach							} else {
19215d0bc43dSGreg Roach								$sql_where .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19225d0bc43dSGreg Roach								$sql_params[$attr . 'date'] = $date->minimumJulianDay();
1923a6f13a4aSGreg Roach							}
1924a6f13a4aSGreg Roach							if ($sortby == $match[1]) {
19257a6ee1acSGreg Roach								$sortby = '';
19267a6ee1acSGreg Roach								$sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1";
1927a6f13a4aSGreg Roach							}
1928a6f13a4aSGreg Roach							unset($attrs[$attr]); // This filter has been fully processed
1929a6f13a4aSGreg Roach						} elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19305d0bc43dSGreg Roach							$sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom";
1931b4e512fdSGreg Roach							// PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1932b4e512fdSGreg Roach							$sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1933a6f13a4aSGreg Roach							unset($attrs[$attr]); // This filter has been fully processed
1934a6f13a4aSGreg Roach						} elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19355d0bc43dSGreg Roach							// Do nothing, unless you have to
19365d0bc43dSGreg Roach							if ($match[1] != '' || $sortby == 'NAME') {
19375d0bc43dSGreg Roach								$sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)";
19385d0bc43dSGreg Roach								// Search the DB only if there is any name supplied
19397a6ee1acSGreg Roach								if ($match[1] != '') {
19407a6ee1acSGreg Roach									$names = explode(' ', $match[1]);
19415d0bc43dSGreg Roach									foreach ($names as $n => $name) {
19425d0bc43dSGreg Roach										$sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19435d0bc43dSGreg Roach										$sql_params[$attr . 'name' . $n] = $name;
19445d0bc43dSGreg Roach									}
19455d0bc43dSGreg Roach								}
19465d0bc43dSGreg Roach								// Let the DB do the name sorting even when no name was entered
19477a6ee1acSGreg Roach								if ($sortby == 'NAME') {
19487a6ee1acSGreg Roach									$sortby = '';
19497a6ee1acSGreg Roach									$sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1950a6f13a4aSGreg Roach								}
19515d0bc43dSGreg Roach							}
1952a6f13a4aSGreg Roach							unset($attrs[$attr]); // This filter has been fully processed
1953a6f13a4aSGreg Roach						} elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1954a6f13a4aSGreg Roach							$sql_join .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)";
1955a6f13a4aSGreg Roach							$sql_join .= " JOIN `##placelinks` AS {$attr}b ON ({$attr}a.p_file={$attr}b.pl_file AND {$attr}b.pl_p_id={$attr}a.p_id AND {$attr}b.pl_gid=f_id)";
19565d0bc43dSGreg Roach							$sql_where .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19575d0bc43dSGreg Roach							$sql_params[$attr . 'place'] = $match[1];
1958a6f13a4aSGreg Roach							// Don't unset this filter. This is just initial filtering
1959a6f13a4aSGreg Roach						} elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19605d0bc43dSGreg Roach							$sql_where .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19615d0bc43dSGreg Roach							$sql_params[$attr . 'contains1'] = $match[1];
19625d0bc43dSGreg Roach							$sql_params[$attr . 'contains2'] = $match[2];
19635d0bc43dSGreg Roach							$sql_params[$attr . 'contains3'] = $match[3];
1964a6f13a4aSGreg Roach							// Don't unset this filter. This is just initial filtering
1965a6f13a4aSGreg Roach						}
1966a6f13a4aSGreg Roach					}
1967a6f13a4aSGreg Roach				}
1968a6f13a4aSGreg Roach
196913abd6f3SGreg Roach				$this->list = [];
1970a6f13a4aSGreg Roach				$rows       = Database::prepare(
1971a6f13a4aSGreg Roach					$sql_select . $sql_join . $sql_where . $sql_order_by
19725d0bc43dSGreg Roach				)->execute($sql_params)->fetchAll();
1973a6f13a4aSGreg Roach
1974a6f13a4aSGreg Roach				foreach ($rows as $row) {
1975*299d100dSGreg Roach					$this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
1976a6f13a4aSGreg Roach				}
1977a6f13a4aSGreg Roach				break;
1978a6f13a4aSGreg Roach
1979a6f13a4aSGreg Roach			default:
1980a6f13a4aSGreg Roach				throw new \DomainException('Invalid list name: ' . $listname);
1981a6f13a4aSGreg Roach		}
1982a6f13a4aSGreg Roach
198313abd6f3SGreg Roach		$filters  = [];
198413abd6f3SGreg Roach		$filters2 = [];
1985a6f13a4aSGreg Roach		if (isset($attrs['filter1']) && count($this->list) > 0) {
1986a6f13a4aSGreg Roach			foreach ($attrs as $key => $value) {
1987a6f13a4aSGreg Roach				if (preg_match("/filter(\d)/", $key)) {
1988a6f13a4aSGreg Roach					$condition = $value;
1989a6f13a4aSGreg Roach					if (preg_match("/@(\w+)/", $condition, $match)) {
1990a6f13a4aSGreg Roach						$id    = $match[1];
1991a6f13a4aSGreg Roach						$value = "''";
19927a6ee1acSGreg Roach						if ($id == 'ID') {
19937a6ee1acSGreg Roach							if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1994a6f13a4aSGreg Roach								$value = "'" . $match[1] . "'";
1995a6f13a4aSGreg Roach							}
19967a6ee1acSGreg Roach						} elseif ($id == 'fact') {
1997a6f13a4aSGreg Roach							$value = "'" . $this->fact . "'";
19987a6ee1acSGreg Roach						} elseif ($id == 'desc') {
1999a6f13a4aSGreg Roach							$value = "'" . $this->desc . "'";
2000a6f13a4aSGreg Roach						} else {
2001a6f13a4aSGreg Roach							if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20027a6ee1acSGreg Roach								$value = "'" . str_replace('@', '', trim($match[1])) . "'";
2003a6f13a4aSGreg Roach							}
2004a6f13a4aSGreg Roach						}
2005a6f13a4aSGreg Roach						$condition = preg_replace("/@$id/", $value, $condition);
2006a6f13a4aSGreg Roach					}
2007a6f13a4aSGreg Roach					//-- handle regular expressions
2008a6f13a4aSGreg Roach					if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2009a6f13a4aSGreg Roach						$tag  = trim($match[1]);
2010a6f13a4aSGreg Roach						$expr = trim($match[2]);
2011a6f13a4aSGreg Roach						$val  = trim($match[3]);
2012a6f13a4aSGreg Roach						if (preg_match("/\\$(\w+)/", $val, $match)) {
2013d1286247SGreg Roach							$val = $this->vars[$match[1]]['id'];
2014a6f13a4aSGreg Roach							$val = trim($val);
2015a6f13a4aSGreg Roach						}
2016a6f13a4aSGreg Roach						if ($val) {
20177a6ee1acSGreg Roach							$searchstr = '';
20187a6ee1acSGreg Roach							$tags      = explode(':', $tag);
2019a6f13a4aSGreg Roach							//-- only limit to a level number if we are specifically looking at a level
2020a6f13a4aSGreg Roach							if (count($tags) > 1) {
2021a6f13a4aSGreg Roach								$level = 1;
2022a6f13a4aSGreg Roach								foreach ($tags as $t) {
2023a6f13a4aSGreg Roach									if (!empty($searchstr)) {
2024a6f13a4aSGreg Roach										$searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2025a6f13a4aSGreg Roach									}
2026a6f13a4aSGreg Roach									//-- search for both EMAIL and _EMAIL... silly double gedcom standard
20277a6ee1acSGreg Roach									if ($t == 'EMAIL' || $t == '_EMAIL') {
20287a6ee1acSGreg Roach										$t = '_?EMAIL';
2029a6f13a4aSGreg Roach									}
20307a6ee1acSGreg Roach									$searchstr .= $level . ' ' . $t;
2031a6f13a4aSGreg Roach									$level++;
2032a6f13a4aSGreg Roach								}
2033a6f13a4aSGreg Roach							} else {
20347a6ee1acSGreg Roach								if ($tag == 'EMAIL' || $tag == '_EMAIL') {
20357a6ee1acSGreg Roach									$tag = '_?EMAIL';
2036a6f13a4aSGreg Roach								}
2037a6f13a4aSGreg Roach								$t         = $tag;
20387a6ee1acSGreg Roach								$searchstr = '1 ' . $tag;
2039a6f13a4aSGreg Roach							}
2040a6f13a4aSGreg Roach							switch ($expr) {
20417a6ee1acSGreg Roach								case 'CONTAINS':
20427a6ee1acSGreg Roach									if ($t == 'PLAC') {
2043a6f13a4aSGreg Roach										$searchstr .= "[^\n]*[, ]*" . $val;
2044a6f13a4aSGreg Roach									} else {
2045a6f13a4aSGreg Roach										$searchstr .= "[^\n]*" . $val;
2046a6f13a4aSGreg Roach									}
2047a6f13a4aSGreg Roach									$filters[] = $searchstr;
2048a6f13a4aSGreg Roach									break;
2049a6f13a4aSGreg Roach								default:
20507a6ee1acSGreg Roach									$filters2[] = ['tag' => $tag, 'expr' => $expr, 'val' => $val];
2051a6f13a4aSGreg Roach									break;
2052a6f13a4aSGreg Roach							}
2053a6f13a4aSGreg Roach						}
2054a6f13a4aSGreg Roach					}
2055a6f13a4aSGreg Roach				}
2056a6f13a4aSGreg Roach			}
2057a6f13a4aSGreg Roach		}
2058a6f13a4aSGreg Roach		//-- apply other filters to the list that could not be added to the search string
2059a6f13a4aSGreg Roach		if ($filters) {
2060a6f13a4aSGreg Roach			foreach ($this->list as $key => $record) {
2061a6f13a4aSGreg Roach				foreach ($filters as $filter) {
2062*299d100dSGreg Roach					if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2063a6f13a4aSGreg Roach						unset($this->list[$key]);
2064a6f13a4aSGreg Roach						break;
2065a6f13a4aSGreg Roach					}
2066a6f13a4aSGreg Roach				}
2067a6f13a4aSGreg Roach			}
2068a6f13a4aSGreg Roach		}
2069a6f13a4aSGreg Roach		if ($filters2) {
207013abd6f3SGreg Roach			$mylist = [];
2071a6f13a4aSGreg Roach			foreach ($this->list as $indi) {
2072a6f13a4aSGreg Roach				$key  = $indi->getXref();
2073*299d100dSGreg Roach				$grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2074a6f13a4aSGreg Roach				$keep = true;
2075a6f13a4aSGreg Roach				foreach ($filters2 as $filter) {
2076a6f13a4aSGreg Roach					if ($keep) {
2077a6f13a4aSGreg Roach						$tag  = $filter['tag'];
2078a6f13a4aSGreg Roach						$expr = $filter['expr'];
2079a6f13a4aSGreg Roach						$val  = $filter['val'];
2080a6f13a4aSGreg Roach						if ($val == "''") {
20817a6ee1acSGreg Roach							$val = '';
2082a6f13a4aSGreg Roach						}
20837a6ee1acSGreg Roach						$tags = explode(':', $tag);
2084a6f13a4aSGreg Roach						$t    = end($tags);
20853d7a8a4cSGreg Roach						$v    = $this->getGedcomValue($tag, 1, $grec);
2086a6f13a4aSGreg Roach						//-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
20877a6ee1acSGreg Roach						if ($t == 'EMAIL' && empty($v)) {
20887a6ee1acSGreg Roach							$tag  = str_replace('EMAIL', '_EMAIL', $tag);
20897a6ee1acSGreg Roach							$tags = explode(':', $tag);
2090a6f13a4aSGreg Roach							$t    = end($tags);
20913d7a8a4cSGreg Roach							$v    = Functions::getSubRecord(1, $tag, $grec);
2092a6f13a4aSGreg Roach						}
2093a6f13a4aSGreg Roach
2094a6f13a4aSGreg Roach						switch ($expr) {
20957a6ee1acSGreg Roach							case 'GTE':
20967a6ee1acSGreg Roach								if ($t == 'DATE') {
2097a6f13a4aSGreg Roach									$date1 = new Date($v);
2098a6f13a4aSGreg Roach									$date2 = new Date($val);
2099a6f13a4aSGreg Roach									$keep  = (Date::compare($date1, $date2) >= 0);
2100a6f13a4aSGreg Roach								} elseif ($val >= $v) {
2101a6f13a4aSGreg Roach									$keep = true;
2102a6f13a4aSGreg Roach								}
2103a6f13a4aSGreg Roach								break;
21047a6ee1acSGreg Roach							case 'LTE':
21057a6ee1acSGreg Roach								if ($t == 'DATE') {
2106a6f13a4aSGreg Roach									$date1 = new Date($v);
2107a6f13a4aSGreg Roach									$date2 = new Date($val);
2108a6f13a4aSGreg Roach									$keep  = (Date::compare($date1, $date2) <= 0);
2109a6f13a4aSGreg Roach								} elseif ($val >= $v) {
2110a6f13a4aSGreg Roach									$keep = true;
2111a6f13a4aSGreg Roach								}
2112a6f13a4aSGreg Roach								break;
2113a6f13a4aSGreg Roach							default:
2114a6f13a4aSGreg Roach								if ($v == $val) {
2115a6f13a4aSGreg Roach									$keep = true;
2116a6f13a4aSGreg Roach								} else {
2117a6f13a4aSGreg Roach									$keep = false;
2118a6f13a4aSGreg Roach								}
2119a6f13a4aSGreg Roach								break;
2120a6f13a4aSGreg Roach						}
2121a6f13a4aSGreg Roach					}
2122a6f13a4aSGreg Roach				}
2123a6f13a4aSGreg Roach				if ($keep) {
2124a6f13a4aSGreg Roach					$mylist[$key] = $indi;
2125a6f13a4aSGreg Roach				}
2126a6f13a4aSGreg Roach			}
2127a6f13a4aSGreg Roach			$this->list = $mylist;
2128a6f13a4aSGreg Roach		}
2129a6f13a4aSGreg Roach
2130a6f13a4aSGreg Roach		switch ($sortby) {
2131a6f13a4aSGreg Roach			case 'NAME':
2132a6f13a4aSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2133a6f13a4aSGreg Roach				break;
2134a6f13a4aSGreg Roach			case 'CHAN':
2135a6f13a4aSGreg Roach				uasort($this->list, function (GedcomRecord $x, GedcomRecord $y) {
2136a6f13a4aSGreg Roach					return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2137a6f13a4aSGreg Roach				});
2138a6f13a4aSGreg Roach				break;
2139a6f13a4aSGreg Roach			case 'BIRT:DATE':
2140a6f13a4aSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2141a6f13a4aSGreg Roach				break;
2142a6f13a4aSGreg Roach			case 'DEAT:DATE':
2143a6f13a4aSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2144a6f13a4aSGreg Roach				break;
2145a6f13a4aSGreg Roach			case 'MARR:DATE':
21465d0bc43dSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2147a6f13a4aSGreg Roach				break;
2148a6f13a4aSGreg Roach			default:
2149a6f13a4aSGreg Roach				// unsorted or already sorted by SQL
2150a6f13a4aSGreg Roach				break;
2151a6f13a4aSGreg Roach		}
2152a6f13a4aSGreg Roach
215313abd6f3SGreg Roach		array_push($this->repeats_stack, [$this->repeats, $this->repeat_bytes]);
2154e8e7866bSGreg Roach		$this->repeat_bytes = xml_get_current_line_number($this->parser) + 1;
2155a6f13a4aSGreg Roach	}
2156a6f13a4aSGreg Roach
2157a6f13a4aSGreg Roach	/**
215876692c8bSGreg Roach	 * XML <List>
2159a6f13a4aSGreg Roach	 */
21608edd1043SGreg Roach	private function listEndHandler() {
2161a6f13a4aSGreg Roach		$this->process_repeats--;
2162a6f13a4aSGreg Roach		if ($this->process_repeats > 0) {
2163a6f13a4aSGreg Roach			return;
2164a6f13a4aSGreg Roach		}
2165a6f13a4aSGreg Roach
2166a6f13a4aSGreg Roach		// Check if there is any list
2167a6f13a4aSGreg Roach		if (count($this->list) > 0) {
2168a6f13a4aSGreg Roach			$lineoffset = 0;
2169a6f13a4aSGreg Roach			foreach ($this->repeats_stack as $rep) {
2170a6f13a4aSGreg Roach				$lineoffset += $rep[1];
2171a6f13a4aSGreg Roach			}
2172a6f13a4aSGreg Roach			//-- read the xml from the file
2173*299d100dSGreg Roach			$lines = file($this->report);
21747a6ee1acSGreg Roach			while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2175a6f13a4aSGreg Roach				$lineoffset--;
2176a6f13a4aSGreg Roach			}
2177a6f13a4aSGreg Roach			$lineoffset++;
2178a6f13a4aSGreg Roach			$reportxml = "<tempdoc>\n";
2179a6f13a4aSGreg Roach			$line_nr   = $lineoffset + $this->repeat_bytes;
2180a6f13a4aSGreg Roach			// List Level counter
2181a6f13a4aSGreg Roach			$count = 1;
2182a6f13a4aSGreg Roach			while (0 < $count) {
21837a6ee1acSGreg Roach				if (strpos($lines[$line_nr], '<List') !== false) {
2184a6f13a4aSGreg Roach					$count++;
21857a6ee1acSGreg Roach				} elseif (strpos($lines[$line_nr], '</List') !== false) {
2186a6f13a4aSGreg Roach					$count--;
2187a6f13a4aSGreg Roach				}
2188a6f13a4aSGreg Roach				if (0 < $count) {
2189a6f13a4aSGreg Roach					$reportxml .= $lines[$line_nr];
2190a6f13a4aSGreg Roach				}
2191a6f13a4aSGreg Roach				$line_nr++;
2192a6f13a4aSGreg Roach			}
2193a6f13a4aSGreg Roach			// No need to drag this
2194a6f13a4aSGreg Roach			unset($lines);
21957a6ee1acSGreg Roach			$reportxml .= '</tempdoc>';
2196a6f13a4aSGreg Roach			// Save original values
2197e8e7866bSGreg Roach			array_push($this->parser_stack, $this->parser);
2198a6f13a4aSGreg Roach			$oldgedrec = $this->gedrec;
2199a6f13a4aSGreg Roach
2200a6f13a4aSGreg Roach			$this->list_total   = count($this->list);
2201a6f13a4aSGreg Roach			$this->list_private = 0;
2202a6f13a4aSGreg Roach			foreach ($this->list as $record) {
2203a6f13a4aSGreg Roach				if ($record->canShow()) {
2204a6f13a4aSGreg Roach					$this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->getTree()));
2205a6f13a4aSGreg Roach					//-- start the sax parser
2206a6f13a4aSGreg Roach					$repeat_parser = xml_parser_create();
2207e8e7866bSGreg Roach					$this->parser  = $repeat_parser;
2208a6f13a4aSGreg Roach					xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
220913abd6f3SGreg Roach					xml_set_element_handler($repeat_parser, [$this, 'startElement'], [$this, 'endElement']);
221013abd6f3SGreg Roach					xml_set_character_data_handler($repeat_parser, [$this, 'characterData']);
2211a6f13a4aSGreg Roach					if (!xml_parse($repeat_parser, $reportxml, true)) {
2212a6f13a4aSGreg Roach						throw new \DomainException(sprintf(
2213a6f13a4aSGreg Roach							'ListEHandler XML error: %s at line %d',
2214a6f13a4aSGreg Roach							xml_error_string(xml_get_error_code($repeat_parser)),
2215a6f13a4aSGreg Roach							xml_get_current_line_number($repeat_parser)
2216a6f13a4aSGreg Roach						));
2217a6f13a4aSGreg Roach					}
2218a6f13a4aSGreg Roach					xml_parser_free($repeat_parser);
2219a6f13a4aSGreg Roach				} else {
2220a6f13a4aSGreg Roach					$this->list_private++;
2221a6f13a4aSGreg Roach				}
2222a6f13a4aSGreg Roach			}
222313abd6f3SGreg Roach			$this->list   = [];
2224e8e7866bSGreg Roach			$this->parser = array_pop($this->parser_stack);
2225a6f13a4aSGreg Roach			$this->gedrec = $oldgedrec;
2226a6f13a4aSGreg Roach		}
2227a6f13a4aSGreg Roach		list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2228a6f13a4aSGreg Roach	}
2229a6f13a4aSGreg Roach
2230a6f13a4aSGreg Roach	/**
2231a6f13a4aSGreg Roach	 * XML <ListTotal> element handler
2232a6f13a4aSGreg Roach	 *
2233a6f13a4aSGreg Roach	 * Prints the total number of records in a list
2234a6f13a4aSGreg Roach	 * The total number is collected from
2235a6f13a4aSGreg Roach	 * List and Relatives
2236a6f13a4aSGreg Roach	 */
22378edd1043SGreg Roach	private function listTotalStartHandler() {
2238a6f13a4aSGreg Roach		if ($this->list_private == 0) {
2239a6f13a4aSGreg Roach			$this->current_element->addText($this->list_total);
2240a6f13a4aSGreg Roach		} else {
22417a6ee1acSGreg Roach			$this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2242a6f13a4aSGreg Roach		}
2243a6f13a4aSGreg Roach	}
2244a6f13a4aSGreg Roach
2245a6f13a4aSGreg Roach	/**
224676692c8bSGreg Roach	 * XML <Relatives>
224776692c8bSGreg Roach	 *
2248a6f13a4aSGreg Roach	 * @param array $attrs an array of key value pairs for the attributes
2249a6f13a4aSGreg Roach	 */
22508edd1043SGreg Roach	private function relativesStartHandler($attrs) {
2251a6f13a4aSGreg Roach		$this->process_repeats++;
2252a6f13a4aSGreg Roach		if ($this->process_repeats > 1) {
2253a6f13a4aSGreg Roach			return;
2254a6f13a4aSGreg Roach		}
2255a6f13a4aSGreg Roach
22567a6ee1acSGreg Roach		$sortby = 'NAME';
2257a6f13a4aSGreg Roach		if (isset($attrs['sortby'])) {
2258a6f13a4aSGreg Roach			$sortby = $attrs['sortby'];
2259a6f13a4aSGreg Roach		}
226013abd6f3SGreg Roach		$match = [];
2261a6f13a4aSGreg Roach		if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2262d1286247SGreg Roach			$sortby = $this->vars[$match[1]]['id'];
2263a6f13a4aSGreg Roach			$sortby = trim($sortby);
2264a6f13a4aSGreg Roach		}
2265a6f13a4aSGreg Roach
2266a6f13a4aSGreg Roach		$maxgen = -1;
2267a6f13a4aSGreg Roach		if (isset($attrs['maxgen'])) {
2268a6f13a4aSGreg Roach			$maxgen = $attrs['maxgen'];
2269a6f13a4aSGreg Roach		}
22707a6ee1acSGreg Roach		if ($maxgen == '*') {
2271a6f13a4aSGreg Roach			$maxgen = -1;
2272a6f13a4aSGreg Roach		}
2273a6f13a4aSGreg Roach
22747a6ee1acSGreg Roach		$group = 'child-family';
2275a6f13a4aSGreg Roach		if (isset($attrs['group'])) {
2276a6f13a4aSGreg Roach			$group = $attrs['group'];
2277a6f13a4aSGreg Roach		}
2278a6f13a4aSGreg Roach		if (preg_match("/\\$(\w+)/", $group, $match)) {
2279d1286247SGreg Roach			$group = $this->vars[$match[1]]['id'];
2280a6f13a4aSGreg Roach			$group = trim($group);
2281a6f13a4aSGreg Roach		}
2282a6f13a4aSGreg Roach
22837a6ee1acSGreg Roach		$id = '';
2284a6f13a4aSGreg Roach		if (isset($attrs['id'])) {
2285a6f13a4aSGreg Roach			$id = $attrs['id'];
2286a6f13a4aSGreg Roach		}
2287a6f13a4aSGreg Roach		if (preg_match("/\\$(\w+)/", $id, $match)) {
2288d1286247SGreg Roach			$id = $this->vars[$match[1]]['id'];
2289a6f13a4aSGreg Roach			$id = trim($id);
2290a6f13a4aSGreg Roach		}
2291a6f13a4aSGreg Roach
229213abd6f3SGreg Roach		$this->list = [];
2293*299d100dSGreg Roach		$person     = Individual::getInstance($id, $this->tree);
2294a6f13a4aSGreg Roach		if (!empty($person)) {
2295a6f13a4aSGreg Roach			$this->list[$id] = $person;
2296a6f13a4aSGreg Roach			switch ($group) {
22977a6ee1acSGreg Roach				case 'child-family':
2298a6f13a4aSGreg Roach					foreach ($person->getChildFamilies() as $family) {
2299a6f13a4aSGreg Roach						$husband = $family->getHusband();
2300a6f13a4aSGreg Roach						$wife    = $family->getWife();
2301a6f13a4aSGreg Roach						if (!empty($husband)) {
2302a6f13a4aSGreg Roach							$this->list[$husband->getXref()] = $husband;
2303a6f13a4aSGreg Roach						}
2304a6f13a4aSGreg Roach						if (!empty($wife)) {
2305a6f13a4aSGreg Roach							$this->list[$wife->getXref()] = $wife;
2306a6f13a4aSGreg Roach						}
2307a6f13a4aSGreg Roach						$children = $family->getChildren();
2308a6f13a4aSGreg Roach						foreach ($children as $child) {
2309a6f13a4aSGreg Roach							if (!empty($child)) {
2310a6f13a4aSGreg Roach								$this->list[$child->getXref()] = $child;
2311a6f13a4aSGreg Roach							}
2312a6f13a4aSGreg Roach						}
2313a6f13a4aSGreg Roach					}
2314a6f13a4aSGreg Roach					break;
23157a6ee1acSGreg Roach				case 'spouse-family':
2316a6f13a4aSGreg Roach					foreach ($person->getSpouseFamilies() as $family) {
2317a6f13a4aSGreg Roach						$husband = $family->getHusband();
2318a6f13a4aSGreg Roach						$wife    = $family->getWife();
2319a6f13a4aSGreg Roach						if (!empty($husband)) {
2320a6f13a4aSGreg Roach							$this->list[$husband->getXref()] = $husband;
2321a6f13a4aSGreg Roach						}
2322a6f13a4aSGreg Roach						if (!empty($wife)) {
2323a6f13a4aSGreg Roach							$this->list[$wife->getXref()] = $wife;
2324a6f13a4aSGreg Roach						}
2325a6f13a4aSGreg Roach						$children = $family->getChildren();
2326a6f13a4aSGreg Roach						foreach ($children as $child) {
2327a6f13a4aSGreg Roach							if (!empty($child)) {
2328a6f13a4aSGreg Roach								$this->list[$child->getXref()] = $child;
2329a6f13a4aSGreg Roach							}
2330a6f13a4aSGreg Roach						}
2331a6f13a4aSGreg Roach					}
2332a6f13a4aSGreg Roach					break;
23337a6ee1acSGreg Roach				case 'direct-ancestors':
23343d7a8a4cSGreg Roach					$this->addAncestors($this->list, $id, false, $maxgen);
2335a6f13a4aSGreg Roach					break;
23367a6ee1acSGreg Roach				case 'ancestors':
23373d7a8a4cSGreg Roach					$this->addAncestors($this->list, $id, true, $maxgen);
2338a6f13a4aSGreg Roach					break;
23397a6ee1acSGreg Roach				case 'descendants':
2340a6f13a4aSGreg Roach					$this->list[$id]->generation = 1;
23413d7a8a4cSGreg Roach					$this->addDescendancy($this->list, $id, false, $maxgen);
2342a6f13a4aSGreg Roach					break;
23437a6ee1acSGreg Roach				case 'all':
23443d7a8a4cSGreg Roach					$this->addAncestors($this->list, $id, true, $maxgen);
23453d7a8a4cSGreg Roach					$this->addDescendancy($this->list, $id, true, $maxgen);
2346a6f13a4aSGreg Roach					break;
2347a6f13a4aSGreg Roach			}
2348a6f13a4aSGreg Roach		}
2349a6f13a4aSGreg Roach
2350a6f13a4aSGreg Roach		switch ($sortby) {
2351a6f13a4aSGreg Roach			case 'NAME':
2352a6f13a4aSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2353a6f13a4aSGreg Roach				break;
2354a6f13a4aSGreg Roach			case 'BIRT:DATE':
2355a6f13a4aSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2356a6f13a4aSGreg Roach				break;
2357a6f13a4aSGreg Roach			case 'DEAT:DATE':
2358a6f13a4aSGreg Roach				uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2359a6f13a4aSGreg Roach				break;
2360a6f13a4aSGreg Roach			case 'generation':
236113abd6f3SGreg Roach				$newarray = [];
2362a6f13a4aSGreg Roach				reset($this->list);
2363a6f13a4aSGreg Roach				$genCounter = 1;
2364a6f13a4aSGreg Roach				while (count($newarray) < count($this->list)) {
2365a6f13a4aSGreg Roach					foreach ($this->list as $key => $value) {
2366a6f13a4aSGreg Roach						$this->generation = $value->generation;
2367a6f13a4aSGreg Roach						if ($this->generation == $genCounter) {
2368a6f13a4aSGreg Roach							$newarray[$key]             = new \stdClass;
2369a6f13a4aSGreg Roach							$newarray[$key]->generation = $this->generation;
2370a6f13a4aSGreg Roach						}
2371a6f13a4aSGreg Roach					}
2372a6f13a4aSGreg Roach					$genCounter++;
2373a6f13a4aSGreg Roach				}
2374a6f13a4aSGreg Roach				$this->list = $newarray;
2375a6f13a4aSGreg Roach				break;
2376a6f13a4aSGreg Roach			default:
2377a6f13a4aSGreg Roach				// unsorted
2378a6f13a4aSGreg Roach				break;
2379a6f13a4aSGreg Roach		}
238013abd6f3SGreg Roach		array_push($this->repeats_stack, [$this->repeats, $this->repeat_bytes]);
2381e8e7866bSGreg Roach		$this->repeat_bytes = xml_get_current_line_number($this->parser) + 1;
2382a6f13a4aSGreg Roach	}
2383a6f13a4aSGreg Roach
2384a6f13a4aSGreg Roach	/**
238576692c8bSGreg Roach	 * XML </ Relatives>
2386a6f13a4aSGreg Roach	 */
23878edd1043SGreg Roach	private function relativesEndHandler() {
2388a6f13a4aSGreg Roach		$this->process_repeats--;
2389a6f13a4aSGreg Roach		if ($this->process_repeats > 0) {
2390a6f13a4aSGreg Roach			return;
2391a6f13a4aSGreg Roach		}
2392a6f13a4aSGreg Roach
2393a6f13a4aSGreg Roach		// Check if there is any relatives
2394a6f13a4aSGreg Roach		if (count($this->list) > 0) {
2395a6f13a4aSGreg Roach			$lineoffset = 0;
2396a6f13a4aSGreg Roach			foreach ($this->repeats_stack as $rep) {
2397a6f13a4aSGreg Roach				$lineoffset += $rep[1];
2398a6f13a4aSGreg Roach			}
2399a6f13a4aSGreg Roach			//-- read the xml from the file
2400*299d100dSGreg Roach			$lines = file($this->report);
24017a6ee1acSGreg Roach			while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2402a6f13a4aSGreg Roach				$lineoffset--;
2403a6f13a4aSGreg Roach			}
2404a6f13a4aSGreg Roach			$lineoffset++;
2405a6f13a4aSGreg Roach			$reportxml = "<tempdoc>\n";
2406a6f13a4aSGreg Roach			$line_nr   = $lineoffset + $this->repeat_bytes;
2407a6f13a4aSGreg Roach			// Relatives Level counter
2408a6f13a4aSGreg Roach			$count = 1;
2409a6f13a4aSGreg Roach			while (0 < $count) {
24107a6ee1acSGreg Roach				if (strpos($lines[$line_nr], '<Relatives') !== false) {
2411a6f13a4aSGreg Roach					$count++;
24127a6ee1acSGreg Roach				} elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2413a6f13a4aSGreg Roach					$count--;
2414a6f13a4aSGreg Roach				}
2415a6f13a4aSGreg Roach				if (0 < $count) {
2416a6f13a4aSGreg Roach					$reportxml .= $lines[$line_nr];
2417a6f13a4aSGreg Roach				}
2418a6f13a4aSGreg Roach				$line_nr++;
2419a6f13a4aSGreg Roach			}
2420a6f13a4aSGreg Roach			// No need to drag this
2421a6f13a4aSGreg Roach			unset($lines);
2422a6f13a4aSGreg Roach			$reportxml .= "</tempdoc>\n";
2423a6f13a4aSGreg Roach			// Save original values
2424e8e7866bSGreg Roach			array_push($this->parser_stack, $this->parser);
2425a6f13a4aSGreg Roach			$oldgedrec = $this->gedrec;
2426a6f13a4aSGreg Roach
2427a6f13a4aSGreg Roach			$this->list_total   = count($this->list);
2428a6f13a4aSGreg Roach			$this->list_private = 0;
2429a6f13a4aSGreg Roach			foreach ($this->list as $key => $value) {
2430a6f13a4aSGreg Roach				if (isset($value->generation)) {
2431a6f13a4aSGreg Roach					$this->generation = $value->generation;
2432a6f13a4aSGreg Roach				}
2433*299d100dSGreg Roach				$tmp          = GedcomRecord::getInstance($key, $this->tree);
2434*299d100dSGreg Roach				$this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2435a6f13a4aSGreg Roach
2436a6f13a4aSGreg Roach				$repeat_parser = xml_parser_create();
2437e8e7866bSGreg Roach				$this->parser  = $repeat_parser;
2438a6f13a4aSGreg Roach				xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
243913abd6f3SGreg Roach				xml_set_element_handler($repeat_parser, [$this, 'startElement'], [$this, 'endElement']);
244013abd6f3SGreg Roach				xml_set_character_data_handler($repeat_parser, [$this, 'characterData']);
2441a6f13a4aSGreg Roach
2442a6f13a4aSGreg Roach				if (!xml_parse($repeat_parser, $reportxml, true)) {
24437a6ee1acSGreg Roach					throw new \DomainException(sprintf('RelativesEHandler XML error: %s at line %d', xml_error_string(xml_get_error_code($repeat_parser)), xml_get_current_line_number($repeat_parser)));
2444a6f13a4aSGreg Roach				}
2445a6f13a4aSGreg Roach				xml_parser_free($repeat_parser);
2446a6f13a4aSGreg Roach			}
2447a6f13a4aSGreg Roach			// Clean up the list array
244813abd6f3SGreg Roach			$this->list   = [];
2449e8e7866bSGreg Roach			$this->parser = array_pop($this->parser_stack);
2450a6f13a4aSGreg Roach			$this->gedrec = $oldgedrec;
2451a6f13a4aSGreg Roach		}
2452a6f13a4aSGreg Roach		list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2453a6f13a4aSGreg Roach	}
2454a6f13a4aSGreg Roach
2455a6f13a4aSGreg Roach	/**
2456a6f13a4aSGreg Roach	 * XML <Generation /> element handler
2457a6f13a4aSGreg Roach	 *
2458a6f13a4aSGreg Roach	 * Prints the number of generations
2459a6f13a4aSGreg Roach	 */
24608edd1043SGreg Roach	private function generationStartHandler() {
2461a6f13a4aSGreg Roach		$this->current_element->addText($this->generation);
2462a6f13a4aSGreg Roach	}
2463a6f13a4aSGreg Roach
2464a6f13a4aSGreg Roach	/**
2465a6f13a4aSGreg Roach	 * XML <NewPage /> element handler
2466a6f13a4aSGreg Roach	 *
2467a6f13a4aSGreg Roach	 * Has to be placed in an element (header, pageheader, body or footer)
2468a6f13a4aSGreg Roach	 */
24698edd1043SGreg Roach	private function newPageStartHandler() {
24707a6ee1acSGreg Roach		$temp = 'addpage';
2471e8e7866bSGreg Roach		$this->wt_report->addElement($temp);
2472a6f13a4aSGreg Roach	}
2473a6f13a4aSGreg Roach
2474a6f13a4aSGreg Roach	/**
247576692c8bSGreg Roach	 * XML <html>
247676692c8bSGreg Roach	 *
2477a6f13a4aSGreg Roach	 * @param string  $tag   HTML tag name
247876692c8bSGreg Roach	 * @param array[] $attrs an array of key value pairs for the attributes
2479a6f13a4aSGreg Roach	 */
24808edd1043SGreg Roach	private function htmlStartHandler($tag, $attrs) {
24817a6ee1acSGreg Roach		if ($tag === 'tempdoc') {
2482a6f13a4aSGreg Roach			return;
2483a6f13a4aSGreg Roach		}
2484e8e7866bSGreg Roach		array_push($this->wt_report_stack, $this->wt_report);
2485e8e7866bSGreg Roach		$this->wt_report       = $this->report_root->createHTML($tag, $attrs);
2486e8e7866bSGreg Roach		$this->current_element = $this->wt_report;
2487a6f13a4aSGreg Roach
2488a6f13a4aSGreg Roach		array_push($this->print_data_stack, $this->print_data);
2489a6f13a4aSGreg Roach		$this->print_data = true;
2490a6f13a4aSGreg Roach	}
2491a6f13a4aSGreg Roach
2492a6f13a4aSGreg Roach	/**
249376692c8bSGreg Roach	 * XML </html>
249476692c8bSGreg Roach	 *
2495a6f13a4aSGreg Roach	 * @param string $tag
2496a6f13a4aSGreg Roach	 */
24978edd1043SGreg Roach	private function htmlEndHandler($tag) {
24987a6ee1acSGreg Roach		if ($tag === 'tempdoc') {
2499a6f13a4aSGreg Roach			return;
2500a6f13a4aSGreg Roach		}
2501a6f13a4aSGreg Roach
2502a6f13a4aSGreg Roach		$this->print_data      = array_pop($this->print_data_stack);
2503e8e7866bSGreg Roach		$this->current_element = $this->wt_report;
2504e8e7866bSGreg Roach		$this->wt_report       = array_pop($this->wt_report_stack);
2505e8e7866bSGreg Roach		if (!is_null($this->wt_report)) {
2506e8e7866bSGreg Roach			$this->wt_report->addElement($this->current_element);
2507a6f13a4aSGreg Roach		} else {
2508e8e7866bSGreg Roach			$this->wt_report = $this->current_element;
2509a6f13a4aSGreg Roach		}
2510a6f13a4aSGreg Roach	}
2511a6f13a4aSGreg Roach
2512a6f13a4aSGreg Roach	/**
2513a6f13a4aSGreg Roach	 * Handle <Input>
2514a6f13a4aSGreg Roach	 */
25158edd1043SGreg Roach	private function inputStartHandler() {
2516a6f13a4aSGreg Roach		// Dummy function, to prevent the default HtmlStartHandler() being called
2517a6f13a4aSGreg Roach	}
2518a6f13a4aSGreg Roach
2519a6f13a4aSGreg Roach	/**
2520a6f13a4aSGreg Roach	 * Handle </Input>
2521a6f13a4aSGreg Roach	 */
25228edd1043SGreg Roach	private function inputEndHandler() {
2523a6f13a4aSGreg Roach		// Dummy function, to prevent the default HtmlEndHandler() being called
2524a6f13a4aSGreg Roach	}
2525a6f13a4aSGreg Roach
2526a6f13a4aSGreg Roach	/**
2527a6f13a4aSGreg Roach	 * Handle <Report>
2528a6f13a4aSGreg Roach	 */
25298edd1043SGreg Roach	private function reportStartHandler() {
2530a6f13a4aSGreg Roach		// Dummy function, to prevent the default HtmlStartHandler() being called
2531a6f13a4aSGreg Roach	}
2532a6f13a4aSGreg Roach
2533a6f13a4aSGreg Roach	/**
2534a6f13a4aSGreg Roach	 * Handle </Report>
2535a6f13a4aSGreg Roach	 */
25368edd1043SGreg Roach	private function reportEndHandler() {
2537a6f13a4aSGreg Roach		// Dummy function, to prevent the default HtmlEndHandler() being called
2538a6f13a4aSGreg Roach	}
2539a6f13a4aSGreg Roach
2540a6f13a4aSGreg Roach	/**
254176692c8bSGreg Roach	 * XML </titleEndHandler>
2542a6f13a4aSGreg Roach	 */
25438edd1043SGreg Roach	private function titleEndHandler() {
25442836aa05SGreg Roach		$this->report_root->addTitle($this->text);
2545a6f13a4aSGreg Roach	}
2546a6f13a4aSGreg Roach
2547a6f13a4aSGreg Roach	/**
254876692c8bSGreg Roach	 * XML </descriptionEndHandler>
2549a6f13a4aSGreg Roach	 */
25508edd1043SGreg Roach	private function descriptionEndHandler() {
25512836aa05SGreg Roach		$this->report_root->addDescription($this->text);
2552a6f13a4aSGreg Roach	}
2553729ce104SGreg Roach
2554729ce104SGreg Roach	/**
255576692c8bSGreg Roach	 * Create a list of all descendants.
255676692c8bSGreg Roach	 *
2557729ce104SGreg Roach	 * @param string[] $list
2558729ce104SGreg Roach	 * @param string   $pid
2559729ce104SGreg Roach	 * @param bool  $parents
2560729ce104SGreg Roach	 * @param int  $generations
2561729ce104SGreg Roach	 */
256282759250SGreg Roach	private function addDescendancy(&$list, $pid, $parents = false, $generations = -1) {
2563*299d100dSGreg Roach		$person = Individual::getInstance($pid, $this->tree);
2564729ce104SGreg Roach		if ($person === null) {
2565729ce104SGreg Roach			return;
2566729ce104SGreg Roach		}
2567729ce104SGreg Roach		if (!isset($list[$pid])) {
2568729ce104SGreg Roach			$list[$pid] = $person;
2569729ce104SGreg Roach		}
2570729ce104SGreg Roach		if (!isset($list[$pid]->generation)) {
2571729ce104SGreg Roach			$list[$pid]->generation = 0;
2572729ce104SGreg Roach		}
2573729ce104SGreg Roach		foreach ($person->getSpouseFamilies() as $family) {
2574729ce104SGreg Roach			if ($parents) {
2575729ce104SGreg Roach				$husband = $family->getHusband();
2576729ce104SGreg Roach				$wife    = $family->getWife();
2577729ce104SGreg Roach				if ($husband) {
2578729ce104SGreg Roach					$list[$husband->getXref()] = $husband;
2579729ce104SGreg Roach					if (isset($list[$pid]->generation)) {
2580729ce104SGreg Roach						$list[$husband->getXref()]->generation = $list[$pid]->generation - 1;
2581729ce104SGreg Roach					} else {
2582729ce104SGreg Roach						$list[$husband->getXref()]->generation = 1;
2583729ce104SGreg Roach					}
2584729ce104SGreg Roach				}
2585729ce104SGreg Roach				if ($wife) {
2586729ce104SGreg Roach					$list[$wife->getXref()] = $wife;
2587729ce104SGreg Roach					if (isset($list[$pid]->generation)) {
2588729ce104SGreg Roach						$list[$wife->getXref()]->generation = $list[$pid]->generation - 1;
2589729ce104SGreg Roach					} else {
2590729ce104SGreg Roach						$list[$wife->getXref()]->generation = 1;
2591729ce104SGreg Roach					}
2592729ce104SGreg Roach				}
2593729ce104SGreg Roach			}
2594729ce104SGreg Roach			$children = $family->getChildren();
2595729ce104SGreg Roach			foreach ($children as $child) {
2596729ce104SGreg Roach				if ($child) {
2597729ce104SGreg Roach					$list[$child->getXref()] = $child;
2598729ce104SGreg Roach					if (isset($list[$pid]->generation)) {
2599729ce104SGreg Roach						$list[$child->getXref()]->generation = $list[$pid]->generation + 1;
2600729ce104SGreg Roach					} else {
2601729ce104SGreg Roach						$list[$child->getXref()]->generation = 2;
2602729ce104SGreg Roach					}
2603729ce104SGreg Roach				}
2604729ce104SGreg Roach			}
2605729ce104SGreg Roach			if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2606729ce104SGreg Roach				foreach ($children as $child) {
26073d7a8a4cSGreg Roach					$this->addDescendancy($list, $child->getXref(), $parents, $generations); // recurse on the childs family
2608729ce104SGreg Roach				}
2609729ce104SGreg Roach			}
2610729ce104SGreg Roach		}
2611729ce104SGreg Roach	}
2612729ce104SGreg Roach
2613729ce104SGreg Roach	/**
261476692c8bSGreg Roach	 * Create a list of all ancestors.
261576692c8bSGreg Roach	 *
2616729ce104SGreg Roach	 * @param string[] $list
2617729ce104SGreg Roach	 * @param string   $pid
2618729ce104SGreg Roach	 * @param bool  $children
2619729ce104SGreg Roach	 * @param int  $generations
2620729ce104SGreg Roach	 */
262182759250SGreg Roach	private function addAncestors(&$list, $pid, $children = false, $generations = -1) {
262213abd6f3SGreg Roach		$genlist                = [$pid];
2623729ce104SGreg Roach		$list[$pid]->generation = 1;
2624729ce104SGreg Roach		while (count($genlist) > 0) {
2625729ce104SGreg Roach			$id = array_shift($genlist);
2626729ce104SGreg Roach			if (strpos($id, 'empty') === 0) {
2627729ce104SGreg Roach				continue; // id can be something like “empty7”
2628729ce104SGreg Roach			}
2629*299d100dSGreg Roach			$person = Individual::getInstance($id, $this->tree);
2630729ce104SGreg Roach			foreach ($person->getChildFamilies() as $family) {
2631729ce104SGreg Roach				$husband = $family->getHusband();
2632729ce104SGreg Roach				$wife    = $family->getWife();
2633729ce104SGreg Roach				if ($husband) {
2634729ce104SGreg Roach					$list[$husband->getXref()]             = $husband;
2635729ce104SGreg Roach					$list[$husband->getXref()]->generation = $list[$id]->generation + 1;
2636729ce104SGreg Roach				}
2637729ce104SGreg Roach				if ($wife) {
2638729ce104SGreg Roach					$list[$wife->getXref()]             = $wife;
2639729ce104SGreg Roach					$list[$wife->getXref()]->generation = $list[$id]->generation + 1;
2640729ce104SGreg Roach				}
2641729ce104SGreg Roach				if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2642729ce104SGreg Roach					if ($husband) {
2643729ce104SGreg Roach						array_push($genlist, $husband->getXref());
2644729ce104SGreg Roach					}
2645729ce104SGreg Roach					if ($wife) {
2646729ce104SGreg Roach						array_push($genlist, $wife->getXref());
2647729ce104SGreg Roach					}
2648729ce104SGreg Roach				}
2649729ce104SGreg Roach				if ($children) {
2650729ce104SGreg Roach					foreach ($family->getChildren() as $child) {
2651729ce104SGreg Roach						$list[$child->getXref()] = $child;
2652729ce104SGreg Roach						if (isset($list[$id]->generation)) {
2653729ce104SGreg Roach							$list[$child->getXref()]->generation = $list[$id]->generation;
2654729ce104SGreg Roach						} else {
2655729ce104SGreg Roach							$list[$child->getXref()]->generation = 1;
2656729ce104SGreg Roach						}
2657729ce104SGreg Roach					}
2658729ce104SGreg Roach				}
2659729ce104SGreg Roach			}
2660729ce104SGreg Roach		}
2661729ce104SGreg Roach	}
2662729ce104SGreg Roach
2663729ce104SGreg Roach	/**
2664729ce104SGreg Roach	 * get gedcom tag value
2665729ce104SGreg Roach	 *
2666729ce104SGreg Roach	 * @param string  $tag    The tag to find, use : to delineate subtags
2667729ce104SGreg Roach	 * @param int $level  The gedcom line level of the first tag to find, setting level to 0 will cause it to use 1+ the level of the incoming record
2668729ce104SGreg Roach	 * @param string  $gedrec The gedcom record to get the value from
2669729ce104SGreg Roach	 *
2670729ce104SGreg Roach	 * @return string the value of a gedcom tag from the given gedcom record
2671729ce104SGreg Roach	 */
267282759250SGreg Roach	private function getGedcomValue($tag, $level, $gedrec) {
2673729ce104SGreg Roach		if (empty($gedrec)) {
2674729ce104SGreg Roach			return '';
2675729ce104SGreg Roach		}
2676729ce104SGreg Roach		$tags      = explode(':', $tag);
2677729ce104SGreg Roach		$origlevel = $level;
2678729ce104SGreg Roach		if ($level == 0) {
26793c12f3e5SGreg Roach			$level = $gedrec[0] + 1;
2680729ce104SGreg Roach		}
2681729ce104SGreg Roach
2682729ce104SGreg Roach		$subrec = $gedrec;
2683729ce104SGreg Roach		foreach ($tags as $t) {
2684729ce104SGreg Roach			$lastsubrec = $subrec;
26853d7a8a4cSGreg Roach			$subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2686729ce104SGreg Roach			if (empty($subrec) && $origlevel == 0) {
2687729ce104SGreg Roach				$level--;
26883d7a8a4cSGreg Roach				$subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2689729ce104SGreg Roach			}
2690729ce104SGreg Roach			if (empty($subrec)) {
26917a6ee1acSGreg Roach				if ($t == 'TITL') {
26923d7a8a4cSGreg Roach					$subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2693729ce104SGreg Roach					if (!empty($subrec)) {
26947a6ee1acSGreg Roach						$t = 'ABBR';
2695729ce104SGreg Roach					}
2696729ce104SGreg Roach				}
2697729ce104SGreg Roach				if (empty($subrec)) {
2698729ce104SGreg Roach					if ($level > 0) {
2699729ce104SGreg Roach						$level--;
2700729ce104SGreg Roach					}
27013d7a8a4cSGreg Roach					$subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2702729ce104SGreg Roach					if (empty($subrec)) {
2703729ce104SGreg Roach						return '';
2704729ce104SGreg Roach					}
2705729ce104SGreg Roach				}
2706729ce104SGreg Roach			}
2707729ce104SGreg Roach			$level++;
2708729ce104SGreg Roach		}
2709729ce104SGreg Roach		$level--;
2710729ce104SGreg Roach		$ct = preg_match("/$level $t(.*)/", $subrec, $match);
2711729ce104SGreg Roach		if ($ct == 0) {
2712729ce104SGreg Roach			$ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2713729ce104SGreg Roach		}
2714729ce104SGreg Roach		if ($ct == 0) {
2715729ce104SGreg Roach			$ct = preg_match("/@ $t (.+)/", $subrec, $match);
2716729ce104SGreg Roach		}
2717729ce104SGreg Roach		if ($ct > 0) {
2718729ce104SGreg Roach			$value = trim($match[1]);
2719729ce104SGreg Roach			if ($t == 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2720*299d100dSGreg Roach				$note = Note::getInstance($match[1], $this->tree);
2721729ce104SGreg Roach				if ($note) {
2722729ce104SGreg Roach					$value = $note->getNote();
2723729ce104SGreg Roach				} else {
2724729ce104SGreg Roach					//-- set the value to the id without the @
2725729ce104SGreg Roach					$value = $match[1];
2726729ce104SGreg Roach				}
2727729ce104SGreg Roach			}
27287a6ee1acSGreg Roach			if ($level != 0 || $t != 'NOTE') {
27293d7a8a4cSGreg Roach				$value .= Functions::getCont($level + 1, $subrec);
2730729ce104SGreg Roach			}
2731729ce104SGreg Roach
2732729ce104SGreg Roach			return $value;
2733729ce104SGreg Roach		}
2734729ce104SGreg Roach
27357a6ee1acSGreg Roach		return '';
2736729ce104SGreg Roach	}
2737d1286247SGreg Roach
2738d1286247SGreg Roach	/**
2739d1286247SGreg Roach	 * Replace variable identifiers with their values.
2740d1286247SGreg Roach	 *
2741d1286247SGreg Roach	 * @param string $expression An expression such as "$foo == 123"
274282759250SGreg Roach	 * @param bool   $quote      Whether to add quotation marks
2743d1286247SGreg Roach	 *
2744d1286247SGreg Roach	 * @return string
2745d1286247SGreg Roach	 */
274682759250SGreg Roach	private function substituteVars($expression, $quote) {
2747d1286247SGreg Roach		return preg_replace_callback(
2748d1286247SGreg Roach			'/\$(\w+)/',
27492118c0e3SGreg Roach			function ($matches) use ($quote) {
27502118c0e3SGreg Roach				if (isset($this->vars[$matches[1]]['id'])) {
275182759250SGreg Roach					if ($quote) {
27522118c0e3SGreg Roach						return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
275382759250SGreg Roach					} else {
27542118c0e3SGreg Roach						return $this->vars[$matches[1]]['id'];
275582759250SGreg Roach					}
2756d1286247SGreg Roach				} else {
2757d1286247SGreg Roach					Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
27583d7a8a4cSGreg Roach
2759d1286247SGreg Roach					return '$' . $matches[1];
2760d1286247SGreg Roach				}
2761d1286247SGreg Roach			},
2762d1286247SGreg Roach			$expression
2763d1286247SGreg Roach		);
2764d1286247SGreg Roach	}
2765a6f13a4aSGreg Roach}
2766