xref: /webtrees/app/Report/ReportParserGenerate.php (revision 79529c8723487d2a0827fad6f7691d99838bd42c)
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;
33299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
34*79529c87SGreg Roachuse stdClass;
355809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
36a6f13a4aSGreg Roach
37a6f13a4aSGreg Roach/**
38a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
39a6f13a4aSGreg Roach */
40c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
41c1010edaSGreg Roach{
42a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
43a6f13a4aSGreg Roach    private $process_footnote = true;
44a6f13a4aSGreg Roach
45a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
46a6f13a4aSGreg Roach    private $print_data = false;
47a6f13a4aSGreg Roach
48a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
4913abd6f3SGreg Roach    private $print_data_stack = [];
50a6f13a4aSGreg Roach
5176692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
52a6f13a4aSGreg Roach    private $process_gedcoms = 0;
53a6f13a4aSGreg Roach
5476692c8bSGreg Roach    /** @var int Are we processing conditionals */
55a6f13a4aSGreg Roach    private $process_ifs = 0;
56a6f13a4aSGreg Roach
5776692c8bSGreg Roach    /** @var int Are we processing repeats */
58a6f13a4aSGreg Roach    private $process_repeats = 0;
59a6f13a4aSGreg Roach
60a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
61a6f13a4aSGreg Roach    private $repeat_bytes = 0;
62a6f13a4aSGreg Roach
635b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
6413abd6f3SGreg Roach    private $repeats = [];
65a6f13a4aSGreg Roach
66a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
6713abd6f3SGreg Roach    private $repeats_stack = [];
68a6f13a4aSGreg Roach
69e8e7866bSGreg Roach    /** @var ReportBase[] Nested repeating data */
7013abd6f3SGreg Roach    private $wt_report_stack = [];
71e8e7866bSGreg Roach
72e8e7866bSGreg Roach    /** @var resource Nested repeating data */
73e8e7866bSGreg Roach    private $parser;
74e8e7866bSGreg Roach
75e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
7613abd6f3SGreg Roach    private $parser_stack = [];
77e8e7866bSGreg Roach
78a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
79a6f13a4aSGreg Roach    private $gedrec = '';
80a6f13a4aSGreg Roach
81a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
8213abd6f3SGreg Roach    private $gedrec_stack = [];
83a6f13a4aSGreg Roach
84a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
85a6f13a4aSGreg Roach    private $current_element;
86a6f13a4aSGreg Roach
87a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
88a6f13a4aSGreg Roach    private $footnote_element;
89a6f13a4aSGreg Roach
90a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
91a6f13a4aSGreg Roach    private $fact = '';
92a6f13a4aSGreg Roach
93a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
94a6f13a4aSGreg Roach    private $desc = '';
95a6f13a4aSGreg Roach
96a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
97a6f13a4aSGreg Roach    private $type = '';
98a6f13a4aSGreg Roach
99a6f13a4aSGreg Roach    /** @var int The current generational level */
100a6f13a4aSGreg Roach    private $generation = 1;
101a6f13a4aSGreg Roach
102a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
10313abd6f3SGreg Roach    private $list = [];
104a6f13a4aSGreg Roach
105a6f13a4aSGreg Roach    /** @var int Number of items in lists */
106a6f13a4aSGreg Roach    private $list_total = 0;
107a6f13a4aSGreg Roach
108a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
109a6f13a4aSGreg Roach    private $list_private = 0;
110a6f13a4aSGreg Roach
111299d100dSGreg Roach    /** @var string The filename of the XML report */
112299d100dSGreg Roach    protected $report;
113299d100dSGreg Roach
114e8e7866bSGreg Roach    /** @var ReportBase A factory for creating report elements */
115e8e7866bSGreg Roach    private $report_root;
116e8e7866bSGreg Roach
1175b084b24SGreg Roach    /** @var ReportBaseElement Nested report elements */
118e8e7866bSGreg Roach    private $wt_report;
119e8e7866bSGreg Roach
120d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1212118c0e3SGreg Roach    private $vars;
122d1286247SGreg Roach
123299d100dSGreg Roach    /** @var Tree The current tree */
124299d100dSGreg Roach    private $tree;
125299d100dSGreg Roach
12676692c8bSGreg Roach    /**
12776692c8bSGreg Roach     * Create a parser for a report
12876692c8bSGreg Roach     *
12976692c8bSGreg Roach     * @param string     $report The XML filename
13076692c8bSGreg Roach     * @param ReportBase $report_root
13176692c8bSGreg Roach     * @param string[][] $vars
132299d100dSGreg Roach     * @param Tree       $tree
13376692c8bSGreg Roach     */
134c1010edaSGreg Roach    public function __construct($report, ReportBase $report_root, array $vars, Tree $tree)
135c1010edaSGreg Roach    {
136299d100dSGreg Roach        $this->report          = $report;
137e8e7866bSGreg Roach        $this->report_root     = $report_root;
138e8e7866bSGreg Roach        $this->wt_report       = $report_root;
13959f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
140d1286247SGreg Roach        $this->vars            = $vars;
141299d100dSGreg Roach        $this->tree            = $tree;
142299d100dSGreg Roach
143299d100dSGreg Roach        parent::__construct($report, $report_root, $vars, $tree);
144a6f13a4aSGreg Roach    }
145a6f13a4aSGreg Roach
146a6f13a4aSGreg Roach    /**
147a6f13a4aSGreg Roach     * XML start element handler
148a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
149a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
150a6f13a4aSGreg Roach     *
151a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
152a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
153a6f13a4aSGreg Roach     * @param array    $attrs  an array of key value pairs for the attributes
154a6f13a4aSGreg Roach     */
155c1010edaSGreg Roach    protected function startElement($parser, $name, $attrs)
156c1010edaSGreg Roach    {
15713abd6f3SGreg Roach        $newattrs = [];
158a6f13a4aSGreg Roach
159a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
160a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
161d1286247SGreg Roach                if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
162d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
163a6f13a4aSGreg Roach                }
164a6f13a4aSGreg Roach            }
165a6f13a4aSGreg Roach            $newattrs[$key] = $value;
166a6f13a4aSGreg Roach        }
167a6f13a4aSGreg Roach        $attrs = $newattrs;
1687a6ee1acSGreg 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')) {
169a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
170a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
171a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
172a6f13a4aSGreg Roach                $this->$start_method($attrs);
173a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
174a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
175a6f13a4aSGreg Roach            }
176a6f13a4aSGreg Roach        }
177a6f13a4aSGreg Roach    }
178a6f13a4aSGreg Roach
179a6f13a4aSGreg Roach    /**
180a6f13a4aSGreg Roach     * XML end element handler
181a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
182a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
183a6f13a4aSGreg Roach     *
184a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
185a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
186a6f13a4aSGreg Roach     */
187c1010edaSGreg Roach    protected function endElement($parser, $name)
188c1010edaSGreg Roach    {
1897a6ee1acSGreg 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')) {
190a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
191a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
192a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
193a6f13a4aSGreg Roach                $this->$end_method();
194a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
195a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
196a6f13a4aSGreg Roach            }
197a6f13a4aSGreg Roach        }
198a6f13a4aSGreg Roach    }
199a6f13a4aSGreg Roach
200a6f13a4aSGreg Roach    /**
201a6f13a4aSGreg Roach     * XML character data handler
202a6f13a4aSGreg Roach     *
203a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
204a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
205a6f13a4aSGreg Roach     */
206c1010edaSGreg Roach    protected function characterData($parser, $data)
207c1010edaSGreg Roach    {
208e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
209a6f13a4aSGreg Roach            $this->current_element->addText($data);
210a6f13a4aSGreg Roach        }
211a6f13a4aSGreg Roach    }
212a6f13a4aSGreg Roach
213a6f13a4aSGreg Roach    /**
21476692c8bSGreg Roach     * XML <style>
215a6f13a4aSGreg Roach     *
216a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
217a6f13a4aSGreg Roach     */
218c1010edaSGreg Roach    private function styleStartHandler($attrs)
219c1010edaSGreg Roach    {
220a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
221a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
222a6f13a4aSGreg Roach        }
223a6f13a4aSGreg Roach
224a6f13a4aSGreg Roach        // array Style that will be passed on
22513abd6f3SGreg Roach        $s = [];
226a6f13a4aSGreg Roach
227a6f13a4aSGreg Roach        // string Name af the style
228a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
229a6f13a4aSGreg Roach
230a6f13a4aSGreg Roach        // string Name of the DEFAULT font
231e8e7866bSGreg Roach        $s['font'] = $this->wt_report->defaultFont;
232a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
233a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
234a6f13a4aSGreg Roach        }
235a6f13a4aSGreg Roach
236a6f13a4aSGreg Roach        // int The size of the font in points
237e8e7866bSGreg Roach        $s['size'] = $this->wt_report->defaultFontSize;
238a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
239a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
240a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
241a6f13a4aSGreg Roach
242a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2437a6ee1acSGreg Roach        $s['style'] = '';
244a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
245a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
246a6f13a4aSGreg Roach        }
247a6f13a4aSGreg Roach
248e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
249a6f13a4aSGreg Roach    }
250a6f13a4aSGreg Roach
251a6f13a4aSGreg Roach    /**
25276692c8bSGreg Roach     * XML <Doc>
253a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
254a6f13a4aSGreg Roach     *
255a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
256a6f13a4aSGreg Roach     */
257c1010edaSGreg Roach    private function docStartHandler($attrs)
258c1010edaSGreg Roach    {
259e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
260a6f13a4aSGreg Roach
261a6f13a4aSGreg Roach        // Custom page width
262a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
263e8e7866bSGreg Roach            $this->wt_report->pagew = (int) $attrs['customwidth'];
264a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
265a6f13a4aSGreg Roach        // Custom Page height
266a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
267e8e7866bSGreg Roach            $this->wt_report->pageh = (int) $attrs['customheight'];
268a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
269a6f13a4aSGreg Roach
270a6f13a4aSGreg Roach        // Left Margin
271a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2727a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
273e8e7866bSGreg Roach                $this->wt_report->leftmargin = 0;
274a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
275e8e7866bSGreg 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))
276a6f13a4aSGreg Roach            }
277a6f13a4aSGreg Roach        }
278a6f13a4aSGreg Roach        // Right Margin
279a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
2807a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
281e8e7866bSGreg Roach                $this->wt_report->rightmargin = 0;
282a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
283e8e7866bSGreg 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))
284a6f13a4aSGreg Roach            }
285a6f13a4aSGreg Roach        }
286a6f13a4aSGreg Roach        // Top Margin
287a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
2887a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
289e8e7866bSGreg Roach                $this->wt_report->topmargin = 0;
290a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
291e8e7866bSGreg 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))
292a6f13a4aSGreg Roach            }
293a6f13a4aSGreg Roach        }
294a6f13a4aSGreg Roach        // Bottom Margin
295a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
2967a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
297e8e7866bSGreg Roach                $this->wt_report->bottommargin = 0;
298a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
299e8e7866bSGreg 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))
300a6f13a4aSGreg Roach            }
301a6f13a4aSGreg Roach        }
302a6f13a4aSGreg Roach        // Header Margin
303a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3047a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
305e8e7866bSGreg Roach                $this->wt_report->headermargin = 0;
306a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
307e8e7866bSGreg 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))
308a6f13a4aSGreg Roach            }
309a6f13a4aSGreg Roach        }
310a6f13a4aSGreg Roach        // Footer Margin
311a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3127a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
313e8e7866bSGreg Roach                $this->wt_report->footermargin = 0;
314a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
315e8e7866bSGreg 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))
316a6f13a4aSGreg Roach            }
317a6f13a4aSGreg Roach        }
318a6f13a4aSGreg Roach
319a6f13a4aSGreg Roach        // Page Orientation
320a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
3217a6ee1acSGreg Roach            if ($attrs['orientation'] == 'landscape') {
3227a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
3237a6ee1acSGreg Roach            } elseif ($attrs['orientation'] == 'portrait') {
3247a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
325a6f13a4aSGreg Roach            }
326a6f13a4aSGreg Roach        }
327a6f13a4aSGreg Roach        // Page Size
328a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
329e8e7866bSGreg Roach            $this->wt_report->pageFormat = strtoupper($attrs['pageSize']);
330a6f13a4aSGreg Roach        }
331a6f13a4aSGreg Roach
332a6f13a4aSGreg Roach        // Show Generated By...
333a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3347a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
335e8e7866bSGreg Roach                $this->wt_report->showGenText = false;
3367a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
337e8e7866bSGreg Roach                $this->wt_report->showGenText = true;
338a6f13a4aSGreg Roach            }
339a6f13a4aSGreg Roach        }
340a6f13a4aSGreg Roach
341e8e7866bSGreg Roach        $this->wt_report->setup();
342a6f13a4aSGreg Roach    }
343a6f13a4aSGreg Roach
344a6f13a4aSGreg Roach    /**
34576692c8bSGreg Roach     * XML </Doc>
346a6f13a4aSGreg Roach     */
347c1010edaSGreg Roach    private function docEndHandler()
348c1010edaSGreg Roach    {
349e8e7866bSGreg Roach        $this->wt_report->run();
350a6f13a4aSGreg Roach    }
351a6f13a4aSGreg Roach
352a6f13a4aSGreg Roach    /**
35376692c8bSGreg Roach     * XML <Header>
354a6f13a4aSGreg Roach     */
355c1010edaSGreg Roach    private function headerStartHandler()
356c1010edaSGreg Roach    {
357a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
358e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3597a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
360a6f13a4aSGreg Roach    }
361a6f13a4aSGreg Roach
362a6f13a4aSGreg Roach    /**
36376692c8bSGreg Roach     * XML <PageHeader>
364a6f13a4aSGreg Roach     */
365c1010edaSGreg Roach    private function pageHeaderStartHandler()
366c1010edaSGreg Roach    {
3679b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
368a6f13a4aSGreg Roach        $this->print_data         = false;
3699b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
370e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
371a6f13a4aSGreg Roach    }
372a6f13a4aSGreg Roach
373a6f13a4aSGreg Roach    /**
37476692c8bSGreg Roach     * XML <pageHeaderEndHandler>
375a6f13a4aSGreg Roach     */
376c1010edaSGreg Roach    private function pageHeaderEndHandler()
377c1010edaSGreg Roach    {
378a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
379e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
380e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
381e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
382a6f13a4aSGreg Roach    }
383a6f13a4aSGreg Roach
384a6f13a4aSGreg Roach    /**
38576692c8bSGreg Roach     * XML <bodyStartHandler>
386a6f13a4aSGreg Roach     */
387c1010edaSGreg Roach    private function bodyStartHandler()
388c1010edaSGreg Roach    {
3897a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
390a6f13a4aSGreg Roach    }
391a6f13a4aSGreg Roach
392a6f13a4aSGreg Roach    /**
39376692c8bSGreg Roach     * XML <footerStartHandler>
394a6f13a4aSGreg Roach     */
395c1010edaSGreg Roach    private function footerStartHandler()
396c1010edaSGreg Roach    {
3977a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
398a6f13a4aSGreg Roach    }
399a6f13a4aSGreg Roach
400a6f13a4aSGreg Roach    /**
40176692c8bSGreg Roach     * XML <Cell>
402a6f13a4aSGreg Roach     *
403a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
404a6f13a4aSGreg Roach     */
405c1010edaSGreg Roach    private function cellStartHandler($attrs)
406c1010edaSGreg Roach    {
407a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4087a6ee1acSGreg Roach        $align = '';
409a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
410a6f13a4aSGreg Roach            $align = $attrs['align'];
411a6f13a4aSGreg Roach            // RTL supported left/right alignment
4127a6ee1acSGreg Roach            if ($align == 'rightrtl') {
413e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4147a6ee1acSGreg Roach                    $align = 'left';
415a6f13a4aSGreg Roach                } else {
4167a6ee1acSGreg Roach                    $align = 'right';
417a6f13a4aSGreg Roach                }
4187a6ee1acSGreg Roach            } elseif ($align == 'leftrtl') {
419e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4207a6ee1acSGreg Roach                    $align = 'right';
421a6f13a4aSGreg Roach                } else {
4227a6ee1acSGreg Roach                    $align = 'left';
423a6f13a4aSGreg Roach                }
424a6f13a4aSGreg Roach            }
425a6f13a4aSGreg Roach        }
426a6f13a4aSGreg Roach
427a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4287a6ee1acSGreg Roach        $bgcolor = '';
429a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
430a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
431a6f13a4aSGreg Roach        }
432a6f13a4aSGreg Roach
433a6f13a4aSGreg Roach        // int Whether or not the background should be painted
434a6f13a4aSGreg Roach        $fill = 1;
435a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4367a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
437a6f13a4aSGreg Roach                $fill = 0;
4387a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
439a6f13a4aSGreg Roach                $fill = 1;
440a6f13a4aSGreg Roach            }
441a6f13a4aSGreg Roach        }
442a6f13a4aSGreg Roach
443a6f13a4aSGreg Roach        $reseth = true;
444a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
445a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4467a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
447a6f13a4aSGreg Roach                $reseth = false;
4487a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
449a6f13a4aSGreg Roach                $reseth = true;
450a6f13a4aSGreg Roach            }
451a6f13a4aSGreg Roach        }
452a6f13a4aSGreg Roach
453a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
454a6f13a4aSGreg Roach        $border = 0;
455a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
456a6f13a4aSGreg Roach            $border = $attrs['border'];
457a6f13a4aSGreg Roach        }
458a6f13a4aSGreg Roach        // string Border color in HTML code
4597a6ee1acSGreg Roach        $bocolor = '';
460a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
461a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
462a6f13a4aSGreg Roach        }
463a6f13a4aSGreg Roach
464a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
465a6f13a4aSGreg Roach        $height = 0;
466a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
467a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
468a6f13a4aSGreg Roach        }
469a6f13a4aSGreg 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.
470a6f13a4aSGreg Roach        $width = 0;
471a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
472a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
473a6f13a4aSGreg Roach        }
474a6f13a4aSGreg Roach
475a6f13a4aSGreg Roach        // int Stretch carachter mode
476a6f13a4aSGreg Roach        $stretch = 0;
477a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
478a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
479a6f13a4aSGreg Roach        }
480a6f13a4aSGreg Roach
481a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
4827a6ee1acSGreg Roach        $left = '.';
483a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
4847a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
4857a6ee1acSGreg Roach                $left = '.';
486a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
487a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
4887a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
489a6f13a4aSGreg Roach                $left = 0;
490a6f13a4aSGreg Roach            }
491a6f13a4aSGreg Roach        }
492a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
4937a6ee1acSGreg Roach        $top = '.';
494a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
4957a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
4967a6ee1acSGreg Roach                $top = '.';
497a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
498a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
4997a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
500a6f13a4aSGreg Roach                $top = 0;
501a6f13a4aSGreg Roach            }
502a6f13a4aSGreg Roach        }
503a6f13a4aSGreg Roach
504a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5057a6ee1acSGreg Roach        $style = '';
506a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
507a6f13a4aSGreg Roach            $style = $attrs['style'];
508a6f13a4aSGreg Roach        }
509a6f13a4aSGreg Roach
510a6f13a4aSGreg Roach        // string Text color in html code
5117a6ee1acSGreg Roach        $tcolor = '';
512a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
513a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
514a6f13a4aSGreg Roach        }
515a6f13a4aSGreg Roach
516a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
517a6f13a4aSGreg Roach        $ln = 0;
518a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
519a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
520a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5217a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
522a6f13a4aSGreg Roach                $ln = 0;
523a6f13a4aSGreg Roach            }
524a6f13a4aSGreg Roach        }
525a6f13a4aSGreg Roach
5267a6ee1acSGreg Roach        if ($align == 'left') {
5277a6ee1acSGreg Roach            $align = 'L';
5287a6ee1acSGreg Roach        } elseif ($align == 'right') {
5297a6ee1acSGreg Roach            $align = 'R';
5307a6ee1acSGreg Roach        } elseif ($align == 'center') {
5317a6ee1acSGreg Roach            $align = 'C';
5327a6ee1acSGreg Roach        } elseif ($align == 'justify') {
5337a6ee1acSGreg Roach            $align = 'J';
534a6f13a4aSGreg Roach        }
535a6f13a4aSGreg Roach
5369b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
537a6f13a4aSGreg Roach        $this->print_data         = true;
538a6f13a4aSGreg Roach
539e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
540a6f13a4aSGreg Roach            $width,
541a6f13a4aSGreg Roach            $height,
542a6f13a4aSGreg Roach            $border,
543a6f13a4aSGreg Roach            $align,
544a6f13a4aSGreg Roach            $bgcolor,
545a6f13a4aSGreg Roach            $style,
546a6f13a4aSGreg Roach            $ln,
547a6f13a4aSGreg Roach            $top,
548a6f13a4aSGreg Roach            $left,
549a6f13a4aSGreg Roach            $fill,
550a6f13a4aSGreg Roach            $stretch,
551a6f13a4aSGreg Roach            $bocolor,
552a6f13a4aSGreg Roach            $tcolor,
553a6f13a4aSGreg Roach            $reseth
554a6f13a4aSGreg Roach        );
555a6f13a4aSGreg Roach    }
556a6f13a4aSGreg Roach
557a6f13a4aSGreg Roach    /**
55876692c8bSGreg Roach     * XML </Cell>
559a6f13a4aSGreg Roach     */
560c1010edaSGreg Roach    private function cellEndHandler()
561c1010edaSGreg Roach    {
562a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
563e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
564a6f13a4aSGreg Roach    }
565a6f13a4aSGreg Roach
566a6f13a4aSGreg Roach    /**
567a6f13a4aSGreg Roach     * XML <Now /> element handler
568a6f13a4aSGreg Roach     */
569c1010edaSGreg Roach    private function nowStartHandler()
570c1010edaSGreg Roach    {
5713d7a8a4cSGreg Roach        $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
572a6f13a4aSGreg Roach        $this->current_element->addText($g->display());
573a6f13a4aSGreg Roach    }
574a6f13a4aSGreg Roach
575a6f13a4aSGreg Roach    /**
576a6f13a4aSGreg Roach     * XML <PageNum /> element handler
577a6f13a4aSGreg Roach     */
578c1010edaSGreg Roach    private function pageNumStartHandler()
579c1010edaSGreg Roach    {
5807a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
581a6f13a4aSGreg Roach    }
582a6f13a4aSGreg Roach
583a6f13a4aSGreg Roach    /**
584a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
585a6f13a4aSGreg Roach     */
586c1010edaSGreg Roach    private function totalPagesStartHandler()
587c1010edaSGreg Roach    {
5887a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
589a6f13a4aSGreg Roach    }
590a6f13a4aSGreg Roach
591a6f13a4aSGreg Roach    /**
592a6f13a4aSGreg Roach     * Called at the start of an element.
593a6f13a4aSGreg Roach     *
594a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
595a6f13a4aSGreg Roach     */
596c1010edaSGreg Roach    private function gedcomStartHandler($attrs)
597c1010edaSGreg Roach    {
598a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
599a6f13a4aSGreg Roach            $this->process_gedcoms++;
600a6f13a4aSGreg Roach
601a6f13a4aSGreg Roach            return;
602a6f13a4aSGreg Roach        }
603a6f13a4aSGreg Roach
604a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6057a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6067a6ee1acSGreg Roach        $tags      = explode(':', $tag);
607a6f13a4aSGreg Roach        $newgedrec = '';
608a6f13a4aSGreg Roach        if (count($tags) < 2) {
609299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
610299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
611a6f13a4aSGreg Roach        }
612a6f13a4aSGreg Roach        if (empty($newgedrec)) {
613a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
614a6f13a4aSGreg Roach            $newgedrec = '';
615a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6167a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
617d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
618d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
619a6f13a4aSGreg Roach                    } else {
620299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
621299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
622a6f13a4aSGreg Roach                    }
623a6f13a4aSGreg Roach                } else {
6247a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
62513abd6f3SGreg Roach                        $gmatch = [];
626a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
627299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
628299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
629a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
630a6f13a4aSGreg Roach                        } else {
631a6f13a4aSGreg Roach                            $newgedrec = '';
632a6f13a4aSGreg Roach                            break;
633a6f13a4aSGreg Roach                        }
634a6f13a4aSGreg Roach                    } else {
6357a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
636a6f13a4aSGreg Roach                        $level     = $temp[0] + 1;
6373d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
638a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
639a6f13a4aSGreg Roach                    }
640a6f13a4aSGreg Roach                }
641a6f13a4aSGreg Roach            }
642a6f13a4aSGreg Roach        }
643a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6449b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
645a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
646a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
647a6f13a4aSGreg Roach                $this->fact = $match[2];
648a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
649a6f13a4aSGreg Roach            }
650a6f13a4aSGreg Roach        } else {
651a6f13a4aSGreg Roach            $this->process_gedcoms++;
652a6f13a4aSGreg Roach        }
653a6f13a4aSGreg Roach    }
654a6f13a4aSGreg Roach
655a6f13a4aSGreg Roach    /**
656a6f13a4aSGreg Roach     * Called at the end of an element.
657a6f13a4aSGreg Roach     */
658c1010edaSGreg Roach    private function gedcomEndHandler()
659c1010edaSGreg Roach    {
660a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
661a6f13a4aSGreg Roach            $this->process_gedcoms--;
662a6f13a4aSGreg Roach        } else {
663a6f13a4aSGreg Roach            list($this->gedrec, $this->fact, $this->desc) = array_pop($this->gedrec_stack);
664a6f13a4aSGreg Roach        }
665a6f13a4aSGreg Roach    }
666a6f13a4aSGreg Roach
667a6f13a4aSGreg Roach    /**
66876692c8bSGreg Roach     * XML <textBoxStartHandler>
669a6f13a4aSGreg Roach     *
670a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
671a6f13a4aSGreg Roach     */
672c1010edaSGreg Roach    private function textBoxStartHandler($attrs)
673c1010edaSGreg Roach    {
674a6f13a4aSGreg Roach        // string Background color code
6757a6ee1acSGreg Roach        $bgcolor = '';
676a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
677a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
678a6f13a4aSGreg Roach        }
679a6f13a4aSGreg Roach
680a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
681a6f13a4aSGreg Roach        $fill = true;
682a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
6837a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
684a6f13a4aSGreg Roach                $fill = false;
6857a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
686a6f13a4aSGreg Roach                $fill = true;
687a6f13a4aSGreg Roach            }
688a6f13a4aSGreg Roach        }
689a6f13a4aSGreg Roach
690a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
691a6f13a4aSGreg Roach        $border = false;
692a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
6937a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
694a6f13a4aSGreg Roach                $border = true;
6957a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
696a6f13a4aSGreg Roach                $border = false;
697a6f13a4aSGreg Roach            }
698a6f13a4aSGreg Roach        }
699a6f13a4aSGreg Roach
700a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
701a6f13a4aSGreg Roach        $height = 0;
702a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
703a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
704a6f13a4aSGreg Roach        }
705a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
706a6f13a4aSGreg Roach        $width = 0;
707a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
708a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
709a6f13a4aSGreg Roach        }
710a6f13a4aSGreg Roach
711a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
7127a6ee1acSGreg Roach        $left = '.';
713a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7147a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
7157a6ee1acSGreg Roach                $left = '.';
716a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
717a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7187a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
719a6f13a4aSGreg Roach                $left = 0;
720a6f13a4aSGreg Roach            }
721a6f13a4aSGreg Roach        }
722a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
7237a6ee1acSGreg Roach        $top = '.';
724a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7257a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
7267a6ee1acSGreg Roach                $top = '.';
727a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
728a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7297a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
730a6f13a4aSGreg Roach                $top = 0;
731a6f13a4aSGreg Roach            }
732a6f13a4aSGreg Roach        }
733a6f13a4aSGreg 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
734a6f13a4aSGreg Roach        $newline = false;
735a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7367a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
737a6f13a4aSGreg Roach                $newline = true;
7387a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
739a6f13a4aSGreg Roach                $newline = false;
740a6f13a4aSGreg Roach            }
741a6f13a4aSGreg Roach        }
742a6f13a4aSGreg Roach        // boolean
743a6f13a4aSGreg Roach        $pagecheck = true;
744a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7457a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
746a6f13a4aSGreg Roach                $pagecheck = false;
7477a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
748a6f13a4aSGreg Roach                $pagecheck = true;
749a6f13a4aSGreg Roach            }
750a6f13a4aSGreg Roach        }
751a6f13a4aSGreg Roach        // boolean Cell padding
752a6f13a4aSGreg Roach        $padding = true;
753a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
7547a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
755a6f13a4aSGreg Roach                $padding = false;
7567a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
757a6f13a4aSGreg Roach                $padding = true;
758a6f13a4aSGreg Roach            }
759a6f13a4aSGreg Roach        }
760a6f13a4aSGreg Roach        // boolean Reset this box Height
761a6f13a4aSGreg Roach        $reseth = false;
762a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
7637a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
764a6f13a4aSGreg Roach                $reseth = true;
7657a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
766a6f13a4aSGreg Roach                $reseth = false;
767a6f13a4aSGreg Roach            }
768a6f13a4aSGreg Roach        }
769a6f13a4aSGreg Roach
770a6f13a4aSGreg Roach        // string Style of rendering
7717a6ee1acSGreg Roach        $style = '';
772a6f13a4aSGreg Roach
7739b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
774a6f13a4aSGreg Roach        $this->print_data         = false;
775a6f13a4aSGreg Roach
7769b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
777e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
778a6f13a4aSGreg Roach            $width,
779a6f13a4aSGreg Roach            $height,
780a6f13a4aSGreg Roach            $border,
781a6f13a4aSGreg Roach            $bgcolor,
782a6f13a4aSGreg Roach            $newline,
783a6f13a4aSGreg Roach            $left,
784a6f13a4aSGreg Roach            $top,
785a6f13a4aSGreg Roach            $pagecheck,
786a6f13a4aSGreg Roach            $style,
787a6f13a4aSGreg Roach            $fill,
788a6f13a4aSGreg Roach            $padding,
789a6f13a4aSGreg Roach            $reseth
790a6f13a4aSGreg Roach        );
791a6f13a4aSGreg Roach    }
792a6f13a4aSGreg Roach
793a6f13a4aSGreg Roach    /**
79476692c8bSGreg Roach     * XML <textBoxEndHandler>
795a6f13a4aSGreg Roach     */
796c1010edaSGreg Roach    private function textBoxEndHandler()
797c1010edaSGreg Roach    {
798a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
799e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
800e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
801e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
802a6f13a4aSGreg Roach    }
803a6f13a4aSGreg Roach
804a6f13a4aSGreg Roach    /**
80576692c8bSGreg Roach     * XLM <Text>.
80676692c8bSGreg Roach     *
807a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
808a6f13a4aSGreg Roach     */
809c1010edaSGreg Roach    private function textStartHandler($attrs)
810c1010edaSGreg Roach    {
8119b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
812a6f13a4aSGreg Roach        $this->print_data         = true;
813a6f13a4aSGreg Roach
814a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8157a6ee1acSGreg Roach        $style = '';
816a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
817a6f13a4aSGreg Roach            $style = $attrs['style'];
818a6f13a4aSGreg Roach        }
819a6f13a4aSGreg Roach
820a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8217a6ee1acSGreg Roach        $color = '';
822a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
823a6f13a4aSGreg Roach            $color = $attrs['color'];
824a6f13a4aSGreg Roach        }
825a6f13a4aSGreg Roach
826e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
827a6f13a4aSGreg Roach    }
828a6f13a4aSGreg Roach
829a6f13a4aSGreg Roach    /**
83076692c8bSGreg Roach     * XML </Text>
831a6f13a4aSGreg Roach     */
832c1010edaSGreg Roach    private function textEndHandler()
833c1010edaSGreg Roach    {
834a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
835e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
836a6f13a4aSGreg Roach    }
837a6f13a4aSGreg Roach
838a6f13a4aSGreg Roach    /**
83976692c8bSGreg Roach     * XML <GetPersonName/>
840a6f13a4aSGreg Roach     * Get the name
841a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
842a6f13a4aSGreg Roach     * 2. id is set with a record id
843a6f13a4aSGreg Roach     *
844a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
845a6f13a4aSGreg Roach     */
846c1010edaSGreg Roach    private function getPersonNameStartHandler($attrs)
847c1010edaSGreg Roach    {
8487a6ee1acSGreg Roach        $id    = '';
84913abd6f3SGreg Roach        $match = [];
850a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
8517a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
852a6f13a4aSGreg Roach                $id = $match[1];
853a6f13a4aSGreg Roach            }
854a6f13a4aSGreg Roach        } else {
8557a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
856d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
857d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
858a6f13a4aSGreg Roach                }
859a6f13a4aSGreg Roach            } else {
8607a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
86113abd6f3SGreg Roach                    $gmatch = [];
862a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
863a6f13a4aSGreg Roach                        $id = $gmatch[1];
864a6f13a4aSGreg Roach                    }
865a6f13a4aSGreg Roach                } else {
866a6f13a4aSGreg Roach                    $id = $attrs['id'];
867a6f13a4aSGreg Roach                }
868a6f13a4aSGreg Roach            }
869a6f13a4aSGreg Roach        }
870a6f13a4aSGreg Roach        if (!empty($id)) {
871299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
872a6f13a4aSGreg Roach            if (is_null($record)) {
873a6f13a4aSGreg Roach                return;
874a6f13a4aSGreg Roach            }
875a6f13a4aSGreg Roach            if (!$record->canShowName()) {
876a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
877a6f13a4aSGreg Roach            } else {
878a6f13a4aSGreg Roach                $name = $record->getFullName();
879a6f13a4aSGreg Roach                $name = preg_replace(
880c1010edaSGreg Roach                    [
881c1010edaSGreg Roach                        '/<span class="starredname">/',
882c1010edaSGreg Roach                        '/<\/span><\/span>/',
883c1010edaSGreg Roach                        '/<\/span>/',
884c1010edaSGreg Roach                    ],
885c1010edaSGreg Roach                    [
886c1010edaSGreg Roach                        '«',
887c1010edaSGreg Roach                        '',
888c1010edaSGreg Roach                        '»',
889c1010edaSGreg Roach                    ],
890a6f13a4aSGreg Roach                    $name
891a6f13a4aSGreg Roach                );
892a6f13a4aSGreg Roach                $name = strip_tags($name);
893a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
894a6f13a4aSGreg Roach                    if (mb_strlen($name) > $attrs['truncate']) {
895faffe0b0SGreg Roach                        $name = mb_substr($name, 0, $attrs['truncate'] - 1) . '…';
896a6f13a4aSGreg Roach                    }
897a6f13a4aSGreg Roach                } else {
898a6f13a4aSGreg Roach                    $addname = $record->getAddName();
899a6f13a4aSGreg Roach                    $addname = preg_replace(
900c1010edaSGreg Roach                        [
901c1010edaSGreg Roach                            '/<span class="starredname">/',
902c1010edaSGreg Roach                            '/<\/span><\/span>/',
903c1010edaSGreg Roach                            '/<\/span>/',
904c1010edaSGreg Roach                        ],
905c1010edaSGreg Roach                        [
906c1010edaSGreg Roach                            '«',
907c1010edaSGreg Roach                            '',
908c1010edaSGreg Roach                            '»',
909c1010edaSGreg Roach                        ],
910a6f13a4aSGreg Roach                        $addname
911a6f13a4aSGreg Roach                    );
912a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
913a6f13a4aSGreg Roach                    if (!empty($addname)) {
9147a6ee1acSGreg Roach                        $name .= ' ' . $addname;
915a6f13a4aSGreg Roach                    }
916a6f13a4aSGreg Roach                }
917a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
918a6f13a4aSGreg Roach            }
919a6f13a4aSGreg Roach        }
920a6f13a4aSGreg Roach    }
921a6f13a4aSGreg Roach
922a6f13a4aSGreg Roach    /**
92376692c8bSGreg Roach     * XML <GedcomValue/>
924a6f13a4aSGreg Roach     *
925a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
926a6f13a4aSGreg Roach     */
927c1010edaSGreg Roach    private function gedcomValueStartHandler($attrs)
928c1010edaSGreg Roach    {
9297a6ee1acSGreg Roach        $id    = '';
93013abd6f3SGreg Roach        $match = [];
9317a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
932a6f13a4aSGreg Roach            $id = $match[1];
933a6f13a4aSGreg Roach        }
934a6f13a4aSGreg Roach
9357a6ee1acSGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] == '1') {
9367a6ee1acSGreg Roach            $useBreak = '1';
937a6f13a4aSGreg Roach        } else {
9387a6ee1acSGreg Roach            $useBreak = '0';
939a6f13a4aSGreg Roach        }
940a6f13a4aSGreg Roach
941a6f13a4aSGreg Roach        $tag = $attrs['tag'];
942a6f13a4aSGreg Roach        if (!empty($tag)) {
9437a6ee1acSGreg Roach            if ($tag == '@desc') {
944a6f13a4aSGreg Roach                $value = $this->desc;
945a6f13a4aSGreg Roach                $value = trim($value);
946a6f13a4aSGreg Roach                $this->current_element->addText($value);
947a6f13a4aSGreg Roach            }
9487a6ee1acSGreg Roach            if ($tag == '@id') {
949a6f13a4aSGreg Roach                $this->current_element->addText($id);
950a6f13a4aSGreg Roach            } else {
9517a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
952a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
9537a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
954a6f13a4aSGreg Roach                    $level = $temp[0];
955a6f13a4aSGreg Roach                    if ($level == 0) {
956a6f13a4aSGreg Roach                        $level++;
957a6f13a4aSGreg Roach                    }
958a6f13a4aSGreg Roach                } else {
959a6f13a4aSGreg Roach                    $level = $attrs['level'];
960a6f13a4aSGreg Roach                }
961a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
9623d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
963a6f13a4aSGreg Roach                switch (end($tags)) {
964a6f13a4aSGreg Roach                    case 'DATE':
965a6f13a4aSGreg Roach                        $tmp   = new Date($value);
966a6f13a4aSGreg Roach                        $value = $tmp->display();
967a6f13a4aSGreg Roach                        break;
968a6f13a4aSGreg Roach                    case 'PLAC':
969299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
970a6f13a4aSGreg Roach                        $value = $tmp->getShortName();
971a6f13a4aSGreg Roach                        break;
972a6f13a4aSGreg Roach                }
9737a6ee1acSGreg Roach                if ($useBreak == '1') {
974a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
975a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
976a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
977a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
978a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
979a6f13a4aSGreg Roach                    if (substr($value, 0, 6) == '<br>') {
980a6f13a4aSGreg Roach                        $value = substr($value, 6);
981a6f13a4aSGreg Roach                    }
982a6f13a4aSGreg Roach                }
983d4d660b7SGreg Roach                $tmp = explode(':', $tag);
984c1010edaSGreg Roach                if (in_array(end($tmp), [
985c1010edaSGreg Roach                    'NOTE',
986c1010edaSGreg Roach                    'TEXT',
987c1010edaSGreg Roach                ])) {
988299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
989a4d703aeSGreg Roach                }
990a6f13a4aSGreg Roach                $this->current_element->addText($value);
991a6f13a4aSGreg Roach            }
992a6f13a4aSGreg Roach        }
993a6f13a4aSGreg Roach    }
994a6f13a4aSGreg Roach
995a6f13a4aSGreg Roach    /**
99676692c8bSGreg Roach     * XML <RepeatTag>
997a6f13a4aSGreg Roach     *
998a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
999a6f13a4aSGreg Roach     */
1000c1010edaSGreg Roach    private function repeatTagStartHandler($attrs)
1001c1010edaSGreg Roach    {
1002a6f13a4aSGreg Roach        $this->process_repeats++;
1003a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1004a6f13a4aSGreg Roach            return;
1005a6f13a4aSGreg Roach        }
1006a6f13a4aSGreg Roach
10079b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
100813abd6f3SGreg Roach        $this->repeats         = [];
1009e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1010a6f13a4aSGreg Roach
10117a6ee1acSGreg Roach        $tag = '';
1012a6f13a4aSGreg Roach        if (isset($attrs['tag'])) {
1013a6f13a4aSGreg Roach            $tag = $attrs['tag'];
1014a6f13a4aSGreg Roach        }
1015a6f13a4aSGreg Roach        if (!empty($tag)) {
10167a6ee1acSGreg Roach            if ($tag == '@desc') {
1017a6f13a4aSGreg Roach                $value = $this->desc;
1018a6f13a4aSGreg Roach                $value = trim($value);
1019a6f13a4aSGreg Roach                $this->current_element->addText($value);
1020a6f13a4aSGreg Roach            } else {
10217a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10227a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10237a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1024a6f13a4aSGreg Roach                $level = $temp[0];
1025a6f13a4aSGreg Roach                if ($level == 0) {
1026a6f13a4aSGreg Roach                    $level++;
1027a6f13a4aSGreg Roach                }
1028a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1029a6f13a4aSGreg Roach                $t      = $tag;
1030a6f13a4aSGreg Roach                $count  = count($tags);
1031a6f13a4aSGreg Roach                $i      = 0;
1032a6f13a4aSGreg Roach                while ($i < $count) {
1033a6f13a4aSGreg Roach                    $t = $tags[$i];
1034a6f13a4aSGreg Roach                    if (!empty($t)) {
1035a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10363d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1037a6f13a4aSGreg Roach                            if (empty($subrec)) {
1038a6f13a4aSGreg Roach                                $level--;
10393d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1040a6f13a4aSGreg Roach                                if (empty($subrec)) {
1041a6f13a4aSGreg Roach                                    return;
1042a6f13a4aSGreg Roach                                }
1043a6f13a4aSGreg Roach                            }
1044a6f13a4aSGreg Roach                        }
1045a6f13a4aSGreg Roach                        $level++;
1046a6f13a4aSGreg Roach                    }
1047a6f13a4aSGreg Roach                    $i++;
1048a6f13a4aSGreg Roach                }
1049a6f13a4aSGreg Roach                $level--;
1050a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1051a6f13a4aSGreg Roach                $i     = 0;
1052a6f13a4aSGreg Roach                while ($i < $count) {
1053a6f13a4aSGreg Roach                    $i++;
1054a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1055a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
1056a9007102SGreg Roach                    if (preg_match('/^\d ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1057299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1058a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1059a9007102SGreg Roach                            continue;
1060a9007102SGreg Roach                        }
1061a9007102SGreg Roach                    }
1062a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1063a6f13a4aSGreg Roach                }
1064a6f13a4aSGreg Roach            }
1065a6f13a4aSGreg Roach        }
1066a6f13a4aSGreg Roach    }
1067a6f13a4aSGreg Roach
1068a6f13a4aSGreg Roach    /**
106976692c8bSGreg Roach     * XML </ RepeatTag>
1070a6f13a4aSGreg Roach     */
1071c1010edaSGreg Roach    private function repeatTagEndHandler()
1072c1010edaSGreg Roach    {
1073a6f13a4aSGreg Roach        $this->process_repeats--;
1074a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1075a6f13a4aSGreg Roach            return;
1076a6f13a4aSGreg Roach        }
1077a6f13a4aSGreg Roach
1078a6f13a4aSGreg Roach        // Check if there is anything to repeat
1079a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1080a6f13a4aSGreg Roach            // No need to load them if not used...
1081a6f13a4aSGreg Roach
1082a6f13a4aSGreg Roach            $lineoffset = 0;
1083a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1084a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1085a6f13a4aSGreg Roach            }
1086a6f13a4aSGreg Roach            //-- read the xml from the file
1087299d100dSGreg Roach            $lines = file($this->report);
10887a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1089a6f13a4aSGreg Roach                $lineoffset--;
1090a6f13a4aSGreg Roach            }
1091a6f13a4aSGreg Roach            $lineoffset++;
1092a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1093a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1094a6f13a4aSGreg Roach            // RepeatTag Level counter
1095a6f13a4aSGreg Roach            $count = 1;
1096a6f13a4aSGreg Roach            while (0 < $count) {
10977a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1098a6f13a4aSGreg Roach                    $count++;
10997a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1100a6f13a4aSGreg Roach                    $count--;
1101a6f13a4aSGreg Roach                }
1102a6f13a4aSGreg Roach                if (0 < $count) {
1103a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1104a6f13a4aSGreg Roach                }
1105a6f13a4aSGreg Roach                $line_nr++;
1106a6f13a4aSGreg Roach            }
1107a6f13a4aSGreg Roach            // No need to drag this
1108a6f13a4aSGreg Roach            unset($lines);
1109a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1110a6f13a4aSGreg Roach            // Save original values
11119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1112a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1113a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1114a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1115a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1116e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1117a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
1118c1010edaSGreg Roach                xml_set_element_handler($repeat_parser, [
1119c1010edaSGreg Roach                    $this,
1120c1010edaSGreg Roach                    'startElement',
1121c1010edaSGreg Roach                ], [
1122c1010edaSGreg Roach                    $this,
1123c1010edaSGreg Roach                    'endElement',
1124c1010edaSGreg Roach                ]);
1125c1010edaSGreg Roach                xml_set_character_data_handler($repeat_parser, [
1126c1010edaSGreg Roach                    $this,
1127c1010edaSGreg Roach                    'characterData',
1128c1010edaSGreg Roach                ]);
1129a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1130a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1131a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1132a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1133a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1134a6f13a4aSGreg Roach                    ));
1135a6f13a4aSGreg Roach                }
1136a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1137a6f13a4aSGreg Roach            }
1138a6f13a4aSGreg Roach            // Restore original values
1139a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1140e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1141a6f13a4aSGreg Roach        }
1142a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1143a6f13a4aSGreg Roach    }
1144a6f13a4aSGreg Roach
1145a6f13a4aSGreg Roach    /**
1146a6f13a4aSGreg Roach     * Variable lookup
1147a6f13a4aSGreg Roach     * Retrieve predefined variables :
1148a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1149a6f13a4aSGreg Roach     *        1 EVEN This is a description
1150a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1151a6f13a4aSGreg Roach     * $ I18N::translate('....')
1152a6f13a4aSGreg Roach     * $ language_settings[]
1153a6f13a4aSGreg Roach     *
1154a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1155a6f13a4aSGreg Roach     */
1156c1010edaSGreg Roach    private function varStartHandler($attrs)
1157c1010edaSGreg Roach    {
1158a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
1159e8e7866bSGreg 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));
1160a6f13a4aSGreg Roach        }
1161a6f13a4aSGreg Roach
1162a6f13a4aSGreg Roach        $var = $attrs['var'];
1163a6f13a4aSGreg Roach        // SetVar element preset variables
1164d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1165d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1166a6f13a4aSGreg Roach        } else {
1167a6f13a4aSGreg Roach            $tfact = $this->fact;
11687a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1169a6f13a4aSGreg Roach                // Use :
1170a6f13a4aSGreg Roach                // n TYPE This text if string
1171a6f13a4aSGreg Roach                $tfact = $this->type;
1172a6f13a4aSGreg Roach            }
1173c1010edaSGreg Roach            $var = str_replace([
1174c1010edaSGreg Roach                '@fact',
1175c1010edaSGreg Roach                '@desc',
1176c1010edaSGreg Roach            ], [
1177c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1178c1010edaSGreg Roach                $this->desc,
1179c1010edaSGreg Roach            ], $var);
1180a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1181a6f13a4aSGreg Roach                $var = I18N::number($match[1]);
1182a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1183a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1184a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1185a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1186a6f13a4aSGreg Roach            }
1187a6f13a4aSGreg Roach        }
1188a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1189a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
11907a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1191a6f13a4aSGreg Roach                $g   = new Date($var);
1192a6f13a4aSGreg Roach                $var = $g->display();
1193a6f13a4aSGreg Roach            }
1194a6f13a4aSGreg Roach        }
1195a6f13a4aSGreg Roach        $this->current_element->addText($var);
11962836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1197a6f13a4aSGreg Roach    }
1198a6f13a4aSGreg Roach
1199a6f13a4aSGreg Roach    /**
120076692c8bSGreg Roach     * XML <Facts>
120176692c8bSGreg Roach     *
1202a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1203a6f13a4aSGreg Roach     */
1204c1010edaSGreg Roach    private function factsStartHandler($attrs)
1205c1010edaSGreg Roach    {
1206a6f13a4aSGreg Roach        $this->process_repeats++;
1207a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1208a6f13a4aSGreg Roach            return;
1209a6f13a4aSGreg Roach        }
1210a6f13a4aSGreg Roach
12119b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
121213abd6f3SGreg Roach        $this->repeats         = [];
1213e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1214a6f13a4aSGreg Roach
12157a6ee1acSGreg Roach        $id    = '';
121613abd6f3SGreg Roach        $match = [];
12177a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1218a6f13a4aSGreg Roach            $id = $match[1];
1219a6f13a4aSGreg Roach        }
12207a6ee1acSGreg Roach        $tag = '';
1221a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1222a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1223a6f13a4aSGreg Roach        }
12247a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1225d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1226a6f13a4aSGreg Roach        }
1227a6f13a4aSGreg Roach
1228299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1229a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1230a6f13a4aSGreg Roach            $facts = $record->getFacts();
12313d7a8a4cSGreg Roach            Functions::sortFacts($facts);
123213abd6f3SGreg Roach            $this->repeats = [];
1233a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1234a6f13a4aSGreg Roach            foreach ($facts as $event) {
1235a6f13a4aSGreg Roach                if (!in_array($event->getTag(), $nonfacts)) {
1236a6f13a4aSGreg Roach                    $this->repeats[] = $event->getGedcom();
1237a6f13a4aSGreg Roach                }
1238a6f13a4aSGreg Roach            }
1239a6f13a4aSGreg Roach        } else {
1240a6f13a4aSGreg Roach            foreach ($record->getFacts() as $fact) {
1241a6f13a4aSGreg Roach                if ($fact->isPendingAddition() && $fact->getTag() !== 'CHAN') {
1242a6f13a4aSGreg Roach                    $this->repeats[] = $fact->getGedcom();
1243a6f13a4aSGreg Roach                }
1244a6f13a4aSGreg Roach            }
1245a6f13a4aSGreg Roach        }
1246a6f13a4aSGreg Roach    }
1247a6f13a4aSGreg Roach
1248a6f13a4aSGreg Roach    /**
124976692c8bSGreg Roach     * XML </Facts>
1250a6f13a4aSGreg Roach     */
1251c1010edaSGreg Roach    private function factsEndHandler()
1252c1010edaSGreg Roach    {
1253a6f13a4aSGreg Roach        $this->process_repeats--;
1254a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1255a6f13a4aSGreg Roach            return;
1256a6f13a4aSGreg Roach        }
1257a6f13a4aSGreg Roach
1258a6f13a4aSGreg Roach        // Check if there is anything to repeat
1259a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1260e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1261a6f13a4aSGreg Roach            $lineoffset = 0;
1262a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1263a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1264a6f13a4aSGreg Roach            }
1265a6f13a4aSGreg Roach
1266a6f13a4aSGreg Roach            //-- read the xml from the file
1267299d100dSGreg Roach            $lines = file($this->report);
1268a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1269a6f13a4aSGreg Roach                $lineoffset--;
1270a6f13a4aSGreg Roach            }
1271a6f13a4aSGreg Roach            $lineoffset++;
1272a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1273a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1274a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1275a6f13a4aSGreg Roach            while ($line_nr < $i) {
1276a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1277a6f13a4aSGreg Roach                $line_nr++;
1278a6f13a4aSGreg Roach            }
1279a6f13a4aSGreg Roach            // No need to drag this
1280a6f13a4aSGreg Roach            unset($lines);
1281a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1282a6f13a4aSGreg Roach            // Save original values
12839b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1284a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1285a6f13a4aSGreg Roach            $count                = count($this->repeats);
1286a6f13a4aSGreg Roach            $i                    = 0;
1287a6f13a4aSGreg Roach            while ($i < $count) {
1288a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1289a6f13a4aSGreg Roach                $this->fact   = '';
1290a6f13a4aSGreg Roach                $this->desc   = '';
1291a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1292a6f13a4aSGreg Roach                    $this->fact = $match[1];
1293a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
129413abd6f3SGreg Roach                        $tmatch = [];
1295a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1296a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1297a6f13a4aSGreg Roach                        } else {
1298a6f13a4aSGreg Roach                            $this->type = ' ';
1299a6f13a4aSGreg Roach                        }
1300a6f13a4aSGreg Roach                    }
1301a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13023d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1303a6f13a4aSGreg Roach                }
1304a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1305e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1306a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
1307c1010edaSGreg Roach                xml_set_element_handler($repeat_parser, [
1308c1010edaSGreg Roach                    $this,
1309c1010edaSGreg Roach                    'startElement',
1310c1010edaSGreg Roach                ], [
1311c1010edaSGreg Roach                    $this,
1312c1010edaSGreg Roach                    'endElement',
1313c1010edaSGreg Roach                ]);
1314c1010edaSGreg Roach                xml_set_character_data_handler($repeat_parser, [
1315c1010edaSGreg Roach                    $this,
1316c1010edaSGreg Roach                    'characterData',
1317c1010edaSGreg Roach                ]);
1318a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1319a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1320a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1321a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1322a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1323a6f13a4aSGreg Roach                    ));
1324a6f13a4aSGreg Roach                }
1325a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1326a6f13a4aSGreg Roach                $i++;
1327a6f13a4aSGreg Roach            }
1328a6f13a4aSGreg Roach            // Restore original values
1329e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1330a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1331a6f13a4aSGreg Roach        }
1332a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1333a6f13a4aSGreg Roach    }
1334a6f13a4aSGreg Roach
1335a6f13a4aSGreg Roach    /**
1336a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1337d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1338a6f13a4aSGreg Roach     *
1339a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1340a6f13a4aSGreg Roach     */
1341c1010edaSGreg Roach    private function setVarStartHandler($attrs)
1342c1010edaSGreg Roach    {
1343a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
1344a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1345a6f13a4aSGreg Roach        }
1346a6f13a4aSGreg Roach
1347a6f13a4aSGreg Roach        $name  = $attrs['name'];
1348a6f13a4aSGreg Roach        $value = $attrs['value'];
134913abd6f3SGreg Roach        $match = [];
1350a6f13a4aSGreg Roach        // Current GEDCOM record strings
13517a6ee1acSGreg Roach        if ($value == '@ID') {
13527a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1353a6f13a4aSGreg Roach                $value = $match[1];
1354a6f13a4aSGreg Roach            }
13557a6ee1acSGreg Roach        } elseif ($value == '@fact') {
1356a6f13a4aSGreg Roach            $value = $this->fact;
13577a6ee1acSGreg Roach        } elseif ($value == '@desc') {
1358a6f13a4aSGreg Roach            $value = $this->desc;
13597a6ee1acSGreg Roach        } elseif ($value == '@generation') {
1360a6f13a4aSGreg Roach            $value = $this->generation;
1361a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
136213abd6f3SGreg Roach            $gmatch = [];
1363a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
13647a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1365a6f13a4aSGreg Roach            }
1366a6f13a4aSGreg Roach        }
1367a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1368d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1369a6f13a4aSGreg Roach        }
1370a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1371a6f13a4aSGreg Roach        $i     = 0;
1372a6f13a4aSGreg Roach        while ($i < $count) {
1373d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
13747a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1375a6f13a4aSGreg Roach            $i++;
1376a6f13a4aSGreg Roach        }
1377a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1378a6f13a4aSGreg Roach            $value = I18N::number($match[1]);
1379a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1380a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1381a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1382a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1383a6f13a4aSGreg Roach        }
1384a6f13a4aSGreg Roach        // Arithmetic functions
1385a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
1386a6f13a4aSGreg Roach            switch ($match[2]) {
13877a6ee1acSGreg Roach                case '+':
1388a6f13a4aSGreg Roach                    $t     = $match[1] + $match[3];
13897a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1390a6f13a4aSGreg Roach                    break;
13917a6ee1acSGreg Roach                case '-':
1392a6f13a4aSGreg Roach                    $t     = $match[1] - $match[3];
13937a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1394a6f13a4aSGreg Roach                    break;
13957a6ee1acSGreg Roach                case '*':
1396a6f13a4aSGreg Roach                    $t     = $match[1] * $match[3];
13977a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1398a6f13a4aSGreg Roach                    break;
13997a6ee1acSGreg Roach                case '/':
1400a6f13a4aSGreg Roach                    $t     = $match[1] / $match[3];
14017a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1402a6f13a4aSGreg Roach                    break;
1403a6f13a4aSGreg Roach            }
1404a6f13a4aSGreg Roach        }
14057a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14067a6ee1acSGreg Roach            $value = '';
1407a6f13a4aSGreg Roach        }
1408d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1409a6f13a4aSGreg Roach    }
1410a6f13a4aSGreg Roach
1411a6f13a4aSGreg Roach    /**
1412a6f13a4aSGreg Roach     * XML <if > start element
1413a6f13a4aSGreg Roach     *
1414a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1415a6f13a4aSGreg Roach     */
1416c1010edaSGreg Roach    private function ifStartHandler($attrs)
1417c1010edaSGreg Roach    {
1418a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1419a6f13a4aSGreg Roach            $this->process_ifs++;
1420a6f13a4aSGreg Roach
1421a6f13a4aSGreg Roach            return;
1422a6f13a4aSGreg Roach        }
1423a6f13a4aSGreg Roach
1424a6f13a4aSGreg Roach        $condition = $attrs['condition'];
142582759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1426c1010edaSGreg Roach        $condition = str_replace([
1427c1010edaSGreg Roach            ' LT ',
1428c1010edaSGreg Roach            ' GT ',
1429c1010edaSGreg Roach        ], [
1430c1010edaSGreg Roach            '<',
1431c1010edaSGreg Roach            '>',
1432c1010edaSGreg Roach        ], $condition);
1433a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
14347a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
143513abd6f3SGreg Roach        $match     = [];
1436a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1437a6f13a4aSGreg Roach        $i         = 0;
1438a6f13a4aSGreg Roach        while ($i < $count) {
1439a6f13a4aSGreg Roach            $id    = $match[$i][1];
1440a6f13a4aSGreg Roach            $value = '""';
14417a6ee1acSGreg Roach            if ($id == 'ID') {
14427a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1443a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1444a6f13a4aSGreg Roach                }
14457a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1446a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
14477a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1448a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
14497a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1450a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1451a6f13a4aSGreg Roach            } else {
14527a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1453a6f13a4aSGreg Roach                $level = $temp[0];
1454a6f13a4aSGreg Roach                if ($level == 0) {
1455a6f13a4aSGreg Roach                    $level++;
1456a6f13a4aSGreg Roach                }
14573d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1458a6f13a4aSGreg Roach                if (empty($value)) {
1459a6f13a4aSGreg Roach                    $level++;
14603d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1461a6f13a4aSGreg Roach                }
14625e8c88c1SGreg Roach                $value = preg_replace('/^@(' . WT_REGEX_XREF . ')@$/', '$1', $value);
14635e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1464a6f13a4aSGreg Roach            }
1465a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1466a6f13a4aSGreg Roach            $i++;
1467a6f13a4aSGreg Roach        }
14685809450fSGreg Roach
146959f2f229SGreg Roach        $ret = (new ExpressionLanguage())->evaluate($condition);
14705809450fSGreg Roach
1471a6f13a4aSGreg Roach        if (!$ret) {
1472a6f13a4aSGreg Roach            $this->process_ifs++;
1473a6f13a4aSGreg Roach        }
1474a6f13a4aSGreg Roach    }
1475a6f13a4aSGreg Roach
1476a6f13a4aSGreg Roach    /**
1477a6f13a4aSGreg Roach     * XML <if /> end element
1478a6f13a4aSGreg Roach     */
1479c1010edaSGreg Roach    private function ifEndHandler()
1480c1010edaSGreg Roach    {
1481a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1482a6f13a4aSGreg Roach            $this->process_ifs--;
1483a6f13a4aSGreg Roach        }
1484a6f13a4aSGreg Roach    }
1485a6f13a4aSGreg Roach
1486a6f13a4aSGreg Roach    /**
1487a6f13a4aSGreg Roach     * XML <Footnote > start element
1488a6f13a4aSGreg Roach     * Collect the Footnote links
1489a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1490a6f13a4aSGreg Roach     *
1491a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1492a6f13a4aSGreg Roach     */
1493c1010edaSGreg Roach    private function footnoteStartHandler($attrs)
1494c1010edaSGreg Roach    {
14957a6ee1acSGreg Roach        $id = '';
14967a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1497a6f13a4aSGreg Roach            $id = $match[2];
1498a6f13a4aSGreg Roach        }
1499299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1500a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15019b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1502a6f13a4aSGreg Roach            $this->print_data         = true;
15037a6ee1acSGreg Roach            $style                    = '';
1504a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1505a6f13a4aSGreg Roach                $style = $attrs['style'];
1506a6f13a4aSGreg Roach            }
1507a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1508e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1509a6f13a4aSGreg Roach        } else {
1510a6f13a4aSGreg Roach            $this->print_data       = false;
1511a6f13a4aSGreg Roach            $this->process_footnote = false;
1512a6f13a4aSGreg Roach        }
1513a6f13a4aSGreg Roach    }
1514a6f13a4aSGreg Roach
1515a6f13a4aSGreg Roach    /**
1516a6f13a4aSGreg Roach     * XML <Footnote /> end element
1517a6f13a4aSGreg Roach     * Print the collected Footnote data
1518a6f13a4aSGreg Roach     */
1519c1010edaSGreg Roach    private function footnoteEndHandler()
1520c1010edaSGreg Roach    {
1521a6f13a4aSGreg Roach        if ($this->process_footnote) {
1522a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1523a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1524a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1525e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1526a6f13a4aSGreg Roach            }
1527a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1528a6f13a4aSGreg Roach        } else {
1529a6f13a4aSGreg Roach            $this->process_footnote = true;
1530a6f13a4aSGreg Roach        }
1531a6f13a4aSGreg Roach    }
1532a6f13a4aSGreg Roach
1533a6f13a4aSGreg Roach    /**
1534a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
1535a6f13a4aSGreg Roach     */
1536c1010edaSGreg Roach    private function footnoteTextsStartHandler()
1537c1010edaSGreg Roach    {
15387a6ee1acSGreg Roach        $temp = 'footnotetexts';
1539e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1540a6f13a4aSGreg Roach    }
1541a6f13a4aSGreg Roach
1542a6f13a4aSGreg Roach    /**
1543a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
1544a6f13a4aSGreg Roach     */
1545c1010edaSGreg Roach    private function brStartHandler()
1546c1010edaSGreg Roach    {
1547a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1548a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1549a6f13a4aSGreg Roach        }
1550a6f13a4aSGreg Roach    }
1551a6f13a4aSGreg Roach
1552a6f13a4aSGreg Roach    /**
1553a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
1554a6f13a4aSGreg Roach     */
1555c1010edaSGreg Roach    private function spStartHandler()
1556c1010edaSGreg Roach    {
1557a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1558a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1559a6f13a4aSGreg Roach        }
1560a6f13a4aSGreg Roach    }
1561a6f13a4aSGreg Roach
1562a6f13a4aSGreg Roach    /**
156376692c8bSGreg Roach     * XML <HighlightedImage/>
156476692c8bSGreg Roach     *
1565a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1566a6f13a4aSGreg Roach     */
1567c1010edaSGreg Roach    private function highlightedImageStartHandler($attrs)
1568c1010edaSGreg Roach    {
1569a6f13a4aSGreg Roach        $id    = '';
157013abd6f3SGreg Roach        $match = [];
15717a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1572a6f13a4aSGreg Roach            $id = $match[1];
1573a6f13a4aSGreg Roach        }
1574a6f13a4aSGreg Roach
1575a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
1576a6f13a4aSGreg Roach        $top = '.';
1577a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
1578a6f13a4aSGreg Roach            if ($attrs['top'] === '0') {
1579a6f13a4aSGreg Roach                $top = 0;
1580a6f13a4aSGreg Roach            } elseif ($attrs['top'] === '.') {
1581a6f13a4aSGreg Roach                $top = '.';
1582a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
1583a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
1584a6f13a4aSGreg Roach            }
1585a6f13a4aSGreg Roach        }
1586a6f13a4aSGreg Roach
1587a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1588a6f13a4aSGreg Roach        $left = '.';
1589a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
1590a6f13a4aSGreg Roach            if ($attrs['left'] === '0') {
1591a6f13a4aSGreg Roach                $left = 0;
1592a6f13a4aSGreg Roach            } elseif ($attrs['left'] === '.') {
1593a6f13a4aSGreg Roach                $left = '.';
1594a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
1595a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
1596a6f13a4aSGreg Roach            }
1597a6f13a4aSGreg Roach        }
1598a6f13a4aSGreg Roach
1599a6f13a4aSGreg Roach        // string Align the image in left, center, right
1600a6f13a4aSGreg Roach        $align = '';
1601a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
1602a6f13a4aSGreg Roach            $align = $attrs['align'];
1603a6f13a4aSGreg Roach        }
1604a6f13a4aSGreg Roach
1605a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
1606a6f13a4aSGreg Roach        $ln = '';
1607a6f13a4aSGreg Roach        if (!empty($attrs['ln'])) {
1608a6f13a4aSGreg Roach            $ln = $attrs['ln'];
1609a6f13a4aSGreg Roach        }
1610a6f13a4aSGreg Roach
1611a6f13a4aSGreg Roach        $width  = 0;
1612a6f13a4aSGreg Roach        $height = 0;
1613a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
1614a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
1615a6f13a4aSGreg Roach        }
1616a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
1617a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
1618a6f13a4aSGreg Roach        }
1619a6f13a4aSGreg Roach
1620299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16214a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
162286a63f51SGreg Roach
162386a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1624c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1625c1010edaSGreg Roach                0,
1626c1010edaSGreg Roach                0,
1627c1010edaSGreg Roach            ];
1628a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16293c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16303c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1631a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16323c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16333c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1634a6f13a4aSGreg Roach            } else {
16353c3b90deSGreg Roach                $width  = $attributes[0];
16363c3b90deSGreg Roach                $height = $attributes[1];
1637a6f13a4aSGreg Roach            }
16384a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1639e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1640a6f13a4aSGreg Roach        }
1641a6f13a4aSGreg Roach    }
1642a6f13a4aSGreg Roach
1643a6f13a4aSGreg Roach    /**
164476692c8bSGreg Roach     * XML <Image/>
164576692c8bSGreg Roach     *
1646a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1647a6f13a4aSGreg Roach     */
1648c1010edaSGreg Roach    private function imageStartHandler($attrs)
1649c1010edaSGreg Roach    {
1650a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
1651a6f13a4aSGreg Roach        $top = '.';
1652a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
16537a6ee1acSGreg Roach            if ($attrs['top'] === '0') {
1654a6f13a4aSGreg Roach                $top = 0;
1655a6f13a4aSGreg Roach            } elseif ($attrs['top'] === '.') {
1656a6f13a4aSGreg Roach                $top = '.';
1657a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
1658a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
1659a6f13a4aSGreg Roach            }
1660a6f13a4aSGreg Roach        }
1661a6f13a4aSGreg Roach
1662a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1663a6f13a4aSGreg Roach        $left = '.';
1664a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
1665a6f13a4aSGreg Roach            if ($attrs['left'] === '0') {
1666a6f13a4aSGreg Roach                $left = 0;
1667a6f13a4aSGreg Roach            } elseif ($attrs['left'] === '.') {
1668a6f13a4aSGreg Roach                $left = '.';
1669a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
1670a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
1671a6f13a4aSGreg Roach            }
1672a6f13a4aSGreg Roach        }
1673a6f13a4aSGreg Roach
1674a6f13a4aSGreg Roach        // string Align the image in left, center, right
1675a6f13a4aSGreg Roach        $align = '';
1676a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
1677a6f13a4aSGreg Roach            $align = $attrs['align'];
1678a6f13a4aSGreg Roach        }
1679a6f13a4aSGreg Roach
1680a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
1681a6f13a4aSGreg Roach        $ln = 'T';
1682a6f13a4aSGreg Roach        if (!empty($attrs['ln'])) {
1683a6f13a4aSGreg Roach            $ln = $attrs['ln'];
1684a6f13a4aSGreg Roach        }
1685a6f13a4aSGreg Roach
1686a6f13a4aSGreg Roach        $width  = 0;
1687a6f13a4aSGreg Roach        $height = 0;
1688a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
1689a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
1690a6f13a4aSGreg Roach        }
1691a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
1692a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
1693a6f13a4aSGreg Roach        }
1694a6f13a4aSGreg Roach
1695a6f13a4aSGreg Roach        $file = '';
1696a6f13a4aSGreg Roach        if (!empty($attrs['file'])) {
1697a6f13a4aSGreg Roach            $file = $attrs['file'];
1698a6f13a4aSGreg Roach        }
16997a6ee1acSGreg Roach        if ($file == '@FILE') {
170013abd6f3SGreg Roach            $match = [];
1701a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1702299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17034a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1704cdf416fbSGreg Roach
17054a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1706c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1707c1010edaSGreg Roach                        0,
1708c1010edaSGreg Roach                        0,
1709c1010edaSGreg Roach                    ];
1710a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17113c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17123c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1713a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17143c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17153c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1716a6f13a4aSGreg Roach                    } else {
17173c3b90deSGreg Roach                        $width  = $attributes[0];
17183c3b90deSGreg Roach                        $height = $attributes[1];
1719a6f13a4aSGreg Roach                    }
17204a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1721e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1722a6f13a4aSGreg Roach                }
1723a6f13a4aSGreg Roach            }
1724a6f13a4aSGreg Roach        } else {
17257a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1726a6f13a4aSGreg Roach                $size = getimagesize($file);
1727a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1728a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1729a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1730a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1731a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1732a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1733a6f13a4aSGreg Roach                } else {
1734a6f13a4aSGreg Roach                    $width  = $size[0];
1735a6f13a4aSGreg Roach                    $height = $size[1];
1736a6f13a4aSGreg Roach                }
1737e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1738e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1739a6f13a4aSGreg Roach            }
1740a6f13a4aSGreg Roach        }
1741a6f13a4aSGreg Roach    }
1742a6f13a4aSGreg Roach
1743a6f13a4aSGreg Roach    /**
1744a6f13a4aSGreg Roach     * XML <Line> element handler
1745a6f13a4aSGreg Roach     *
1746a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1747a6f13a4aSGreg Roach     */
1748c1010edaSGreg Roach    private function lineStartHandler($attrs)
1749c1010edaSGreg Roach    {
1750a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
17517a6ee1acSGreg Roach        $x1 = '.';
1752a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17537a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1754a6f13a4aSGreg Roach                $x1 = 0;
17557a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
17567a6ee1acSGreg Roach                $x1 = '.';
1757a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1758a6f13a4aSGreg Roach                $x1 = (int) $attrs['x1'];
1759a6f13a4aSGreg Roach            }
1760a6f13a4aSGreg Roach        }
1761a6f13a4aSGreg Roach        // Start vertical position, current position (default)
17627a6ee1acSGreg Roach        $y1 = '.';
1763a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17647a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1765a6f13a4aSGreg Roach                $y1 = 0;
17667a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
17677a6ee1acSGreg Roach                $y1 = '.';
1768a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1769a6f13a4aSGreg Roach                $y1 = (int) $attrs['y1'];
1770a6f13a4aSGreg Roach            }
1771a6f13a4aSGreg Roach        }
1772a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
17737a6ee1acSGreg Roach        $x2 = '.';
1774a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
17757a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1776a6f13a4aSGreg Roach                $x2 = 0;
17777a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
17787a6ee1acSGreg Roach                $x2 = '.';
1779a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1780a6f13a4aSGreg Roach                $x2 = (int) $attrs['x2'];
1781a6f13a4aSGreg Roach            }
1782a6f13a4aSGreg Roach        }
1783a6f13a4aSGreg Roach        // End vertical position
17847a6ee1acSGreg Roach        $y2 = '.';
1785a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
17867a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1787a6f13a4aSGreg Roach                $y2 = 0;
17887a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
17897a6ee1acSGreg Roach                $y2 = '.';
1790a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1791a6f13a4aSGreg Roach                $y2 = (int) $attrs['y2'];
1792a6f13a4aSGreg Roach            }
1793a6f13a4aSGreg Roach        }
1794a6f13a4aSGreg Roach
1795e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1796e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1797a6f13a4aSGreg Roach    }
1798a6f13a4aSGreg Roach
1799a6f13a4aSGreg Roach    /**
180076692c8bSGreg Roach     * XML <List>
1801a6f13a4aSGreg Roach     *
1802a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
1803a6f13a4aSGreg Roach     */
1804c1010edaSGreg Roach    private function listStartHandler($attrs)
1805c1010edaSGreg Roach    {
1806a6f13a4aSGreg Roach        $this->process_repeats++;
1807a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1808a6f13a4aSGreg Roach            return;
1809a6f13a4aSGreg Roach        }
1810a6f13a4aSGreg Roach
181113abd6f3SGreg Roach        $match = [];
1812a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1813a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1814a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1815d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1816a6f13a4aSGreg Roach                $sortby = trim($sortby);
1817a6f13a4aSGreg Roach            }
1818a6f13a4aSGreg Roach        } else {
18197a6ee1acSGreg Roach            $sortby = 'NAME';
1820a6f13a4aSGreg Roach        }
1821a6f13a4aSGreg Roach
1822a6f13a4aSGreg Roach        if (isset($attrs['list'])) {
1823a6f13a4aSGreg Roach            $listname = $attrs['list'];
1824a6f13a4aSGreg Roach        } else {
18257a6ee1acSGreg Roach            $listname = 'individual';
1826a6f13a4aSGreg Roach        }
1827a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1828a6f13a4aSGreg Roach        switch ($listname) {
18297a6ee1acSGreg Roach            case 'pending':
1830a6f13a4aSGreg Roach                $rows       = Database::prepare(
18315d0bc43dSGreg Roach                    "SELECT xref, CASE new_gedcom WHEN '' THEN old_gedcom ELSE new_gedcom END AS gedcom" .
18325d0bc43dSGreg Roach                    " FROM `##change`" . " WHERE (xref, change_id) IN (" .
18335d0bc43dSGreg Roach                    "  SELECT xref, MAX(change_id)" .
18345d0bc43dSGreg Roach                    "  FROM `##change`" .
18355d0bc43dSGreg Roach                    "  WHERE status = 'pending' AND gedcom_id = :tree_id" .
18365d0bc43dSGreg Roach                    "  GROUP BY xref" .
18375d0bc43dSGreg Roach                    " )"
183813abd6f3SGreg Roach                )->execute([
1839299d100dSGreg Roach                    'tree_id' => $this->tree->getTreeId(),
184013abd6f3SGreg Roach                ])->fetchAll();
184113abd6f3SGreg Roach                $this->list = [];
1842a6f13a4aSGreg Roach                foreach ($rows as $row) {
1843299d100dSGreg Roach                    $this->list[] = GedcomRecord::getInstance($row->xref, $this->tree, $row->gedcom);
1844a6f13a4aSGreg Roach                }
1845a6f13a4aSGreg Roach                break;
1846a6f13a4aSGreg Roach            case 'individual':
184776156db1SGreg Roach                $sql_select   = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` ";
1848a6f13a4aSGreg Roach                $sql_join     = "";
1849825006d2SGreg Roach                $sql_where    = " WHERE i_file = :tree_id";
1850a6f13a4aSGreg Roach                $sql_order_by = "";
1851299d100dSGreg Roach                $sql_params   = ['tree_id' => $this->tree->getTreeId()];
1852a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1853a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
185482759250SGreg Roach                        $value = $this->substituteVars($value, false);
1855a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1856a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1857a6f13a4aSGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)";
1858b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
18595d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1860a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
18617a6ee1acSGreg Roach                            if ($match[2] == 'LTE') {
18625d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
18635d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1864a6f13a4aSGreg Roach                            } else {
18655d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
18665d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1867a6f13a4aSGreg Roach                            }
1868a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
1869a6f13a4aSGreg Roach                                $sortby       = "";
1870a6f13a4aSGreg Roach                                $sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1";
1871a6f13a4aSGreg Roach                            }
1872a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1873a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
1874a6f13a4aSGreg Roach                            // Do nothing, unless you have to
1875a6f13a4aSGreg Roach                            if ($match[1] != '' || $sortby == 'NAME') {
1876a6f13a4aSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)";
1877a6f13a4aSGreg Roach                                // Search the DB only if there is any name supplied
18787a6ee1acSGreg Roach                                if ($match[1] != '') {
18797a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
18805d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
18815d0bc43dSGreg Roach                                        $sql_where                       .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
18825d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
1883a6f13a4aSGreg Roach                                    }
1884a6f13a4aSGreg Roach                                }
1885a6f13a4aSGreg Roach                                // Let the DB do the name sorting even when no name was entered
18867a6ee1acSGreg Roach                                if ($sortby == 'NAME') {
18877a6ee1acSGreg Roach                                    $sortby       = '';
18887a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1889a6f13a4aSGreg Roach                                }
1890a6f13a4aSGreg Roach                            }
1891a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1892a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
18935d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom";
1894b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1895b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1896a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1897a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1898a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)";
1899a6f13a4aSGreg 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)";
19005d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19015d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1902a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
1903a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19045d0bc43dSGreg Roach                            $sql_where                       .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19055d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
19065d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
19075d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
1908a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
1909a6f13a4aSGreg Roach                        }
1910a6f13a4aSGreg Roach                    }
1911a6f13a4aSGreg Roach                }
1912a6f13a4aSGreg Roach
191313abd6f3SGreg Roach                $this->list = [];
1914a6f13a4aSGreg Roach                $rows       = Database::prepare(
1915a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
19165d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
1917a6f13a4aSGreg Roach
1918a6f13a4aSGreg Roach                foreach ($rows as $row) {
1919299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1920a6f13a4aSGreg Roach                }
1921a6f13a4aSGreg Roach                break;
1922a6f13a4aSGreg Roach
1923a6f13a4aSGreg Roach            case 'family':
192476156db1SGreg Roach                $sql_select   = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`";
1925a6f13a4aSGreg Roach                $sql_join     = "";
1926825006d2SGreg Roach                $sql_where    = " WHERE f_file = :tree_id";
1927a6f13a4aSGreg Roach                $sql_order_by = "";
1928299d100dSGreg Roach                $sql_params   = ['tree_id' => $this->tree->getTreeId()];
1929a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1930a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
193182759250SGreg Roach                        $value = $this->substituteVars($value, false);
1932a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1933a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1934a9eb55f8SGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)";
1935b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
19365d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1937a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
19387a6ee1acSGreg Roach                            if ($match[2] == 'LTE') {
19395d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19405d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1941a6f13a4aSGreg Roach                            } else {
19425d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19435d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1944a6f13a4aSGreg Roach                            }
1945a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
19467a6ee1acSGreg Roach                                $sortby       = '';
19477a6ee1acSGreg Roach                                $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1";
1948a6f13a4aSGreg Roach                            }
1949a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1950a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19515d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom";
1952b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1953b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1954a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1955a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19565d0bc43dSGreg Roach                            // Do nothing, unless you have to
19575d0bc43dSGreg Roach                            if ($match[1] != '' || $sortby == 'NAME') {
19585d0bc43dSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)";
19595d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
19607a6ee1acSGreg Roach                                if ($match[1] != '') {
19617a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19625d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19635d0bc43dSGreg Roach                                        $sql_where                       .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19645d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
19655d0bc43dSGreg Roach                                    }
19665d0bc43dSGreg Roach                                }
19675d0bc43dSGreg Roach                                // Let the DB do the name sorting even when no name was entered
19687a6ee1acSGreg Roach                                if ($sortby == 'NAME') {
19697a6ee1acSGreg Roach                                    $sortby       = '';
19707a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1971a6f13a4aSGreg Roach                                }
19725d0bc43dSGreg Roach                            }
1973a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1974a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1975a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)";
1976a6f13a4aSGreg 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)";
19775d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19785d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1979a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
1980a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19815d0bc43dSGreg Roach                            $sql_where                       .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19825d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
19835d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
19845d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
1985a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
1986a6f13a4aSGreg Roach                        }
1987a6f13a4aSGreg Roach                    }
1988a6f13a4aSGreg Roach                }
1989a6f13a4aSGreg Roach
199013abd6f3SGreg Roach                $this->list = [];
1991a6f13a4aSGreg Roach                $rows       = Database::prepare(
1992a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
19935d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
1994a6f13a4aSGreg Roach
1995a6f13a4aSGreg Roach                foreach ($rows as $row) {
1996299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
1997a6f13a4aSGreg Roach                }
1998a6f13a4aSGreg Roach                break;
1999a6f13a4aSGreg Roach
2000a6f13a4aSGreg Roach            default:
2001a6f13a4aSGreg Roach                throw new \DomainException('Invalid list name: ' . $listname);
2002a6f13a4aSGreg Roach        }
2003a6f13a4aSGreg Roach
200413abd6f3SGreg Roach        $filters  = [];
200513abd6f3SGreg Roach        $filters2 = [];
2006a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2007a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2008a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2009a6f13a4aSGreg Roach                    $condition = $value;
2010a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2011a6f13a4aSGreg Roach                        $id    = $match[1];
2012a6f13a4aSGreg Roach                        $value = "''";
20137a6ee1acSGreg Roach                        if ($id == 'ID') {
20147a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2015a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2016a6f13a4aSGreg Roach                            }
20177a6ee1acSGreg Roach                        } elseif ($id == 'fact') {
2018a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
20197a6ee1acSGreg Roach                        } elseif ($id == 'desc') {
2020a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2021a6f13a4aSGreg Roach                        } else {
2022a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20237a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2024a6f13a4aSGreg Roach                            }
2025a6f13a4aSGreg Roach                        }
2026a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2027a6f13a4aSGreg Roach                    }
2028a6f13a4aSGreg Roach                    //-- handle regular expressions
2029a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2030a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2031a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2032a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2033a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2034d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2035a6f13a4aSGreg Roach                            $val = trim($val);
2036a6f13a4aSGreg Roach                        }
2037a6f13a4aSGreg Roach                        if ($val) {
20387a6ee1acSGreg Roach                            $searchstr = '';
20397a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2040a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2041a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2042a6f13a4aSGreg Roach                                $level = 1;
2043a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2044a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2045a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2046a6f13a4aSGreg Roach                                    }
2047a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
20487a6ee1acSGreg Roach                                    if ($t == 'EMAIL' || $t == '_EMAIL') {
20497a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2050a6f13a4aSGreg Roach                                    }
20517a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2052a6f13a4aSGreg Roach                                    $level++;
2053a6f13a4aSGreg Roach                                }
2054a6f13a4aSGreg Roach                            } else {
20557a6ee1acSGreg Roach                                if ($tag == 'EMAIL' || $tag == '_EMAIL') {
20567a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2057a6f13a4aSGreg Roach                                }
2058a6f13a4aSGreg Roach                                $t         = $tag;
20597a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2060a6f13a4aSGreg Roach                            }
2061a6f13a4aSGreg Roach                            switch ($expr) {
20627a6ee1acSGreg Roach                                case 'CONTAINS':
20637a6ee1acSGreg Roach                                    if ($t == 'PLAC') {
2064a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2065a6f13a4aSGreg Roach                                    } else {
2066a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2067a6f13a4aSGreg Roach                                    }
2068a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2069a6f13a4aSGreg Roach                                    break;
2070a6f13a4aSGreg Roach                                default:
2071c1010edaSGreg Roach                                    $filters2[] = [
2072c1010edaSGreg Roach                                        'tag'  => $tag,
2073c1010edaSGreg Roach                                        'expr' => $expr,
2074c1010edaSGreg Roach                                        'val'  => $val,
2075c1010edaSGreg Roach                                    ];
2076a6f13a4aSGreg Roach                                    break;
2077a6f13a4aSGreg Roach                            }
2078a6f13a4aSGreg Roach                        }
2079a6f13a4aSGreg Roach                    }
2080a6f13a4aSGreg Roach                }
2081a6f13a4aSGreg Roach            }
2082a6f13a4aSGreg Roach        }
2083a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2084a6f13a4aSGreg Roach        if ($filters) {
2085a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2086a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2087299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2088a6f13a4aSGreg Roach                        unset($this->list[$key]);
2089a6f13a4aSGreg Roach                        break;
2090a6f13a4aSGreg Roach                    }
2091a6f13a4aSGreg Roach                }
2092a6f13a4aSGreg Roach            }
2093a6f13a4aSGreg Roach        }
2094a6f13a4aSGreg Roach        if ($filters2) {
209513abd6f3SGreg Roach            $mylist = [];
2096a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2097a6f13a4aSGreg Roach                $key  = $indi->getXref();
2098299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2099a6f13a4aSGreg Roach                $keep = true;
2100a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2101a6f13a4aSGreg Roach                    if ($keep) {
2102a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2103a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2104a6f13a4aSGreg Roach                        $val  = $filter['val'];
2105a6f13a4aSGreg Roach                        if ($val == "''") {
21067a6ee1acSGreg Roach                            $val = '';
2107a6f13a4aSGreg Roach                        }
21087a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2109a6f13a4aSGreg Roach                        $t    = end($tags);
21103d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2111a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
21127a6ee1acSGreg Roach                        if ($t == 'EMAIL' && empty($v)) {
21137a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21147a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2115a6f13a4aSGreg Roach                            $t    = end($tags);
21163d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2117a6f13a4aSGreg Roach                        }
2118a6f13a4aSGreg Roach
2119a6f13a4aSGreg Roach                        switch ($expr) {
21207a6ee1acSGreg Roach                            case 'GTE':
21217a6ee1acSGreg Roach                                if ($t == 'DATE') {
2122a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2123a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2124a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2125a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2126a6f13a4aSGreg Roach                                    $keep = true;
2127a6f13a4aSGreg Roach                                }
2128a6f13a4aSGreg Roach                                break;
21297a6ee1acSGreg Roach                            case 'LTE':
21307a6ee1acSGreg Roach                                if ($t == 'DATE') {
2131a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2132a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2133a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2134a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2135a6f13a4aSGreg Roach                                    $keep = true;
2136a6f13a4aSGreg Roach                                }
2137a6f13a4aSGreg Roach                                break;
2138a6f13a4aSGreg Roach                            default:
2139a6f13a4aSGreg Roach                                if ($v == $val) {
2140a6f13a4aSGreg Roach                                    $keep = true;
2141a6f13a4aSGreg Roach                                } else {
2142a6f13a4aSGreg Roach                                    $keep = false;
2143a6f13a4aSGreg Roach                                }
2144a6f13a4aSGreg Roach                                break;
2145a6f13a4aSGreg Roach                        }
2146a6f13a4aSGreg Roach                    }
2147a6f13a4aSGreg Roach                }
2148a6f13a4aSGreg Roach                if ($keep) {
2149a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2150a6f13a4aSGreg Roach                }
2151a6f13a4aSGreg Roach            }
2152a6f13a4aSGreg Roach            $this->list = $mylist;
2153a6f13a4aSGreg Roach        }
2154a6f13a4aSGreg Roach
2155a6f13a4aSGreg Roach        switch ($sortby) {
2156a6f13a4aSGreg Roach            case 'NAME':
2157a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2158a6f13a4aSGreg Roach                break;
2159a6f13a4aSGreg Roach            case 'CHAN':
2160a6f13a4aSGreg Roach                uasort($this->list, function (GedcomRecord $x, GedcomRecord $y) {
2161a6f13a4aSGreg Roach                    return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2162a6f13a4aSGreg Roach                });
2163a6f13a4aSGreg Roach                break;
2164a6f13a4aSGreg Roach            case 'BIRT:DATE':
2165a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2166a6f13a4aSGreg Roach                break;
2167a6f13a4aSGreg Roach            case 'DEAT:DATE':
2168a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2169a6f13a4aSGreg Roach                break;
2170a6f13a4aSGreg Roach            case 'MARR:DATE':
21715d0bc43dSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2172a6f13a4aSGreg Roach                break;
2173a6f13a4aSGreg Roach            default:
2174a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2175a6f13a4aSGreg Roach                break;
2176a6f13a4aSGreg Roach        }
2177a6f13a4aSGreg Roach
21789b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2179e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2180a6f13a4aSGreg Roach    }
2181a6f13a4aSGreg Roach
2182a6f13a4aSGreg Roach    /**
218376692c8bSGreg Roach     * XML <List>
2184a6f13a4aSGreg Roach     */
2185c1010edaSGreg Roach    private function listEndHandler()
2186c1010edaSGreg Roach    {
2187a6f13a4aSGreg Roach        $this->process_repeats--;
2188a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2189a6f13a4aSGreg Roach            return;
2190a6f13a4aSGreg Roach        }
2191a6f13a4aSGreg Roach
2192a6f13a4aSGreg Roach        // Check if there is any list
2193a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2194a6f13a4aSGreg Roach            $lineoffset = 0;
2195a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2196a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2197a6f13a4aSGreg Roach            }
2198a6f13a4aSGreg Roach            //-- read the xml from the file
2199299d100dSGreg Roach            $lines = file($this->report);
22007a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2201a6f13a4aSGreg Roach                $lineoffset--;
2202a6f13a4aSGreg Roach            }
2203a6f13a4aSGreg Roach            $lineoffset++;
2204a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2205a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2206a6f13a4aSGreg Roach            // List Level counter
2207a6f13a4aSGreg Roach            $count = 1;
2208a6f13a4aSGreg Roach            while (0 < $count) {
22097a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2210a6f13a4aSGreg Roach                    $count++;
22117a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2212a6f13a4aSGreg Roach                    $count--;
2213a6f13a4aSGreg Roach                }
2214a6f13a4aSGreg Roach                if (0 < $count) {
2215a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2216a6f13a4aSGreg Roach                }
2217a6f13a4aSGreg Roach                $line_nr++;
2218a6f13a4aSGreg Roach            }
2219a6f13a4aSGreg Roach            // No need to drag this
2220a6f13a4aSGreg Roach            unset($lines);
22217a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2222a6f13a4aSGreg Roach            // Save original values
22239b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2224a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2225a6f13a4aSGreg Roach
2226a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2227a6f13a4aSGreg Roach            $this->list_private = 0;
2228a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2229a6f13a4aSGreg Roach                if ($record->canShow()) {
2230a6f13a4aSGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->getTree()));
2231a6f13a4aSGreg Roach                    //-- start the sax parser
2232a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2233e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2234a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
2235c1010edaSGreg Roach                    xml_set_element_handler($repeat_parser, [
2236c1010edaSGreg Roach                        $this,
2237c1010edaSGreg Roach                        'startElement',
2238c1010edaSGreg Roach                    ], [
2239c1010edaSGreg Roach                        $this,
2240c1010edaSGreg Roach                        'endElement',
2241c1010edaSGreg Roach                    ]);
2242c1010edaSGreg Roach                    xml_set_character_data_handler($repeat_parser, [
2243c1010edaSGreg Roach                        $this,
2244c1010edaSGreg Roach                        'characterData',
2245c1010edaSGreg Roach                    ]);
2246a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
2247a6f13a4aSGreg Roach                        throw new \DomainException(sprintf(
2248a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2249a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2250a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2251a6f13a4aSGreg Roach                        ));
2252a6f13a4aSGreg Roach                    }
2253a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2254a6f13a4aSGreg Roach                } else {
2255a6f13a4aSGreg Roach                    $this->list_private++;
2256a6f13a4aSGreg Roach                }
2257a6f13a4aSGreg Roach            }
225813abd6f3SGreg Roach            $this->list   = [];
2259e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2260a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2261a6f13a4aSGreg Roach        }
2262a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2263a6f13a4aSGreg Roach    }
2264a6f13a4aSGreg Roach
2265a6f13a4aSGreg Roach    /**
2266a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2267a6f13a4aSGreg Roach     * Prints the total number of records in a list
2268a6f13a4aSGreg Roach     * The total number is collected from
2269a6f13a4aSGreg Roach     * List and Relatives
2270a6f13a4aSGreg Roach     */
2271c1010edaSGreg Roach    private function listTotalStartHandler()
2272c1010edaSGreg Roach    {
2273a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2274a6f13a4aSGreg Roach            $this->current_element->addText($this->list_total);
2275a6f13a4aSGreg Roach        } else {
22767a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2277a6f13a4aSGreg Roach        }
2278a6f13a4aSGreg Roach    }
2279a6f13a4aSGreg Roach
2280a6f13a4aSGreg Roach    /**
228176692c8bSGreg Roach     * XML <Relatives>
228276692c8bSGreg Roach     *
2283a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
2284a6f13a4aSGreg Roach     */
2285c1010edaSGreg Roach    private function relativesStartHandler($attrs)
2286c1010edaSGreg Roach    {
2287a6f13a4aSGreg Roach        $this->process_repeats++;
2288a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2289a6f13a4aSGreg Roach            return;
2290a6f13a4aSGreg Roach        }
2291a6f13a4aSGreg Roach
22927a6ee1acSGreg Roach        $sortby = 'NAME';
2293a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
2294a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
2295a6f13a4aSGreg Roach        }
229613abd6f3SGreg Roach        $match = [];
2297a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2298d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2299a6f13a4aSGreg Roach            $sortby = trim($sortby);
2300a6f13a4aSGreg Roach        }
2301a6f13a4aSGreg Roach
2302a6f13a4aSGreg Roach        $maxgen = -1;
2303a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2304a6f13a4aSGreg Roach            $maxgen = $attrs['maxgen'];
2305a6f13a4aSGreg Roach        }
23067a6ee1acSGreg Roach        if ($maxgen == '*') {
2307a6f13a4aSGreg Roach            $maxgen = -1;
2308a6f13a4aSGreg Roach        }
2309a6f13a4aSGreg Roach
23107a6ee1acSGreg Roach        $group = 'child-family';
2311a6f13a4aSGreg Roach        if (isset($attrs['group'])) {
2312a6f13a4aSGreg Roach            $group = $attrs['group'];
2313a6f13a4aSGreg Roach        }
2314a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2315d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2316a6f13a4aSGreg Roach            $group = trim($group);
2317a6f13a4aSGreg Roach        }
2318a6f13a4aSGreg Roach
23197a6ee1acSGreg Roach        $id = '';
2320a6f13a4aSGreg Roach        if (isset($attrs['id'])) {
2321a6f13a4aSGreg Roach            $id = $attrs['id'];
2322a6f13a4aSGreg Roach        }
2323a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2324d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2325a6f13a4aSGreg Roach            $id = trim($id);
2326a6f13a4aSGreg Roach        }
2327a6f13a4aSGreg Roach
232813abd6f3SGreg Roach        $this->list = [];
2329299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2330a6f13a4aSGreg Roach        if (!empty($person)) {
2331a6f13a4aSGreg Roach            $this->list[$id] = $person;
2332a6f13a4aSGreg Roach            switch ($group) {
23337a6ee1acSGreg Roach                case 'child-family':
2334a6f13a4aSGreg Roach                    foreach ($person->getChildFamilies() as $family) {
2335a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2336a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2337a6f13a4aSGreg Roach                        if (!empty($husband)) {
2338a6f13a4aSGreg Roach                            $this->list[$husband->getXref()] = $husband;
2339a6f13a4aSGreg Roach                        }
2340a6f13a4aSGreg Roach                        if (!empty($wife)) {
2341a6f13a4aSGreg Roach                            $this->list[$wife->getXref()] = $wife;
2342a6f13a4aSGreg Roach                        }
2343a6f13a4aSGreg Roach                        $children = $family->getChildren();
2344a6f13a4aSGreg Roach                        foreach ($children as $child) {
2345a6f13a4aSGreg Roach                            if (!empty($child)) {
2346a6f13a4aSGreg Roach                                $this->list[$child->getXref()] = $child;
2347a6f13a4aSGreg Roach                            }
2348a6f13a4aSGreg Roach                        }
2349a6f13a4aSGreg Roach                    }
2350a6f13a4aSGreg Roach                    break;
23517a6ee1acSGreg Roach                case 'spouse-family':
2352a6f13a4aSGreg Roach                    foreach ($person->getSpouseFamilies() as $family) {
2353a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2354a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2355a6f13a4aSGreg Roach                        if (!empty($husband)) {
2356a6f13a4aSGreg Roach                            $this->list[$husband->getXref()] = $husband;
2357a6f13a4aSGreg Roach                        }
2358a6f13a4aSGreg Roach                        if (!empty($wife)) {
2359a6f13a4aSGreg Roach                            $this->list[$wife->getXref()] = $wife;
2360a6f13a4aSGreg Roach                        }
2361a6f13a4aSGreg Roach                        $children = $family->getChildren();
2362a6f13a4aSGreg Roach                        foreach ($children as $child) {
2363a6f13a4aSGreg Roach                            if (!empty($child)) {
2364a6f13a4aSGreg Roach                                $this->list[$child->getXref()] = $child;
2365a6f13a4aSGreg Roach                            }
2366a6f13a4aSGreg Roach                        }
2367a6f13a4aSGreg Roach                    }
2368a6f13a4aSGreg Roach                    break;
23697a6ee1acSGreg Roach                case 'direct-ancestors':
23703d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2371a6f13a4aSGreg Roach                    break;
23727a6ee1acSGreg Roach                case 'ancestors':
23733d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2374a6f13a4aSGreg Roach                    break;
23757a6ee1acSGreg Roach                case 'descendants':
2376a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
23773d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2378a6f13a4aSGreg Roach                    break;
23797a6ee1acSGreg Roach                case 'all':
23803d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
23813d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2382a6f13a4aSGreg Roach                    break;
2383a6f13a4aSGreg Roach            }
2384a6f13a4aSGreg Roach        }
2385a6f13a4aSGreg Roach
2386a6f13a4aSGreg Roach        switch ($sortby) {
2387a6f13a4aSGreg Roach            case 'NAME':
2388a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2389a6f13a4aSGreg Roach                break;
2390a6f13a4aSGreg Roach            case 'BIRT:DATE':
2391a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2392a6f13a4aSGreg Roach                break;
2393a6f13a4aSGreg Roach            case 'DEAT:DATE':
2394a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2395a6f13a4aSGreg Roach                break;
2396a6f13a4aSGreg Roach            case 'generation':
239713abd6f3SGreg Roach                $newarray = [];
2398a6f13a4aSGreg Roach                reset($this->list);
2399a6f13a4aSGreg Roach                $genCounter = 1;
2400a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2401a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2402a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2403a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2404*79529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2405a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2406a6f13a4aSGreg Roach                        }
2407a6f13a4aSGreg Roach                    }
2408a6f13a4aSGreg Roach                    $genCounter++;
2409a6f13a4aSGreg Roach                }
2410a6f13a4aSGreg Roach                $this->list = $newarray;
2411a6f13a4aSGreg Roach                break;
2412a6f13a4aSGreg Roach            default:
2413a6f13a4aSGreg Roach                // unsorted
2414a6f13a4aSGreg Roach                break;
2415a6f13a4aSGreg Roach        }
24169b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2417e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2418a6f13a4aSGreg Roach    }
2419a6f13a4aSGreg Roach
2420a6f13a4aSGreg Roach    /**
242176692c8bSGreg Roach     * XML </ Relatives>
2422a6f13a4aSGreg Roach     */
2423c1010edaSGreg Roach    private function relativesEndHandler()
2424c1010edaSGreg Roach    {
2425a6f13a4aSGreg Roach        $this->process_repeats--;
2426a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2427a6f13a4aSGreg Roach            return;
2428a6f13a4aSGreg Roach        }
2429a6f13a4aSGreg Roach
2430a6f13a4aSGreg Roach        // Check if there is any relatives
2431a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2432a6f13a4aSGreg Roach            $lineoffset = 0;
2433a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2434a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2435a6f13a4aSGreg Roach            }
2436a6f13a4aSGreg Roach            //-- read the xml from the file
2437299d100dSGreg Roach            $lines = file($this->report);
24387a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2439a6f13a4aSGreg Roach                $lineoffset--;
2440a6f13a4aSGreg Roach            }
2441a6f13a4aSGreg Roach            $lineoffset++;
2442a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2443a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2444a6f13a4aSGreg Roach            // Relatives Level counter
2445a6f13a4aSGreg Roach            $count = 1;
2446a6f13a4aSGreg Roach            while (0 < $count) {
24477a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2448a6f13a4aSGreg Roach                    $count++;
24497a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2450a6f13a4aSGreg Roach                    $count--;
2451a6f13a4aSGreg Roach                }
2452a6f13a4aSGreg Roach                if (0 < $count) {
2453a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2454a6f13a4aSGreg Roach                }
2455a6f13a4aSGreg Roach                $line_nr++;
2456a6f13a4aSGreg Roach            }
2457a6f13a4aSGreg Roach            // No need to drag this
2458a6f13a4aSGreg Roach            unset($lines);
2459a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2460a6f13a4aSGreg Roach            // Save original values
24619b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2462a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2463a6f13a4aSGreg Roach
2464a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2465a6f13a4aSGreg Roach            $this->list_private = 0;
2466a6f13a4aSGreg Roach            foreach ($this->list as $key => $value) {
2467a6f13a4aSGreg Roach                if (isset($value->generation)) {
2468a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2469a6f13a4aSGreg Roach                }
2470299d100dSGreg Roach                $tmp          = GedcomRecord::getInstance($key, $this->tree);
2471299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2472a6f13a4aSGreg Roach
2473a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2474e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2475a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
2476c1010edaSGreg Roach                xml_set_element_handler($repeat_parser, [
2477c1010edaSGreg Roach                    $this,
2478c1010edaSGreg Roach                    'startElement',
2479c1010edaSGreg Roach                ], [
2480c1010edaSGreg Roach                    $this,
2481c1010edaSGreg Roach                    'endElement',
2482c1010edaSGreg Roach                ]);
2483c1010edaSGreg Roach                xml_set_character_data_handler($repeat_parser, [
2484c1010edaSGreg Roach                    $this,
2485c1010edaSGreg Roach                    'characterData',
2486c1010edaSGreg Roach                ]);
2487a6f13a4aSGreg Roach
2488a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
24897a6ee1acSGreg 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)));
2490a6f13a4aSGreg Roach                }
2491a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2492a6f13a4aSGreg Roach            }
2493a6f13a4aSGreg Roach            // Clean up the list array
249413abd6f3SGreg Roach            $this->list   = [];
2495e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2496a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2497a6f13a4aSGreg Roach        }
2498a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2499a6f13a4aSGreg Roach    }
2500a6f13a4aSGreg Roach
2501a6f13a4aSGreg Roach    /**
2502a6f13a4aSGreg Roach     * XML <Generation /> element handler
2503a6f13a4aSGreg Roach     * Prints the number of generations
2504a6f13a4aSGreg Roach     */
2505c1010edaSGreg Roach    private function generationStartHandler()
2506c1010edaSGreg Roach    {
2507a6f13a4aSGreg Roach        $this->current_element->addText($this->generation);
2508a6f13a4aSGreg Roach    }
2509a6f13a4aSGreg Roach
2510a6f13a4aSGreg Roach    /**
2511a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2512a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
2513a6f13a4aSGreg Roach     */
2514c1010edaSGreg Roach    private function newPageStartHandler()
2515c1010edaSGreg Roach    {
25167a6ee1acSGreg Roach        $temp = 'addpage';
2517e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2518a6f13a4aSGreg Roach    }
2519a6f13a4aSGreg Roach
2520a6f13a4aSGreg Roach    /**
252176692c8bSGreg Roach     * XML <html>
252276692c8bSGreg Roach     *
2523a6f13a4aSGreg Roach     * @param string  $tag   HTML tag name
252476692c8bSGreg Roach     * @param array[] $attrs an array of key value pairs for the attributes
2525a6f13a4aSGreg Roach     */
2526c1010edaSGreg Roach    private function htmlStartHandler($tag, $attrs)
2527c1010edaSGreg Roach    {
25287a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2529a6f13a4aSGreg Roach            return;
2530a6f13a4aSGreg Roach        }
25319b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2532e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2533e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2534a6f13a4aSGreg Roach
25359b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2536a6f13a4aSGreg Roach        $this->print_data         = true;
2537a6f13a4aSGreg Roach    }
2538a6f13a4aSGreg Roach
2539a6f13a4aSGreg Roach    /**
254076692c8bSGreg Roach     * XML </html>
254176692c8bSGreg Roach     *
2542a6f13a4aSGreg Roach     * @param string $tag
2543a6f13a4aSGreg Roach     */
2544c1010edaSGreg Roach    private function htmlEndHandler($tag)
2545c1010edaSGreg Roach    {
25467a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2547a6f13a4aSGreg Roach            return;
2548a6f13a4aSGreg Roach        }
2549a6f13a4aSGreg Roach
2550a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2551e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2552e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
2553e8e7866bSGreg Roach        if (!is_null($this->wt_report)) {
2554e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2555a6f13a4aSGreg Roach        } else {
2556e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2557a6f13a4aSGreg Roach        }
2558a6f13a4aSGreg Roach    }
2559a6f13a4aSGreg Roach
2560a6f13a4aSGreg Roach    /**
2561a6f13a4aSGreg Roach     * Handle <Input>
2562a6f13a4aSGreg Roach     */
2563c1010edaSGreg Roach    private function inputStartHandler()
2564c1010edaSGreg Roach    {
2565a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2566a6f13a4aSGreg Roach    }
2567a6f13a4aSGreg Roach
2568a6f13a4aSGreg Roach    /**
2569a6f13a4aSGreg Roach     * Handle </Input>
2570a6f13a4aSGreg Roach     */
2571c1010edaSGreg Roach    private function inputEndHandler()
2572c1010edaSGreg Roach    {
2573a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2574a6f13a4aSGreg Roach    }
2575a6f13a4aSGreg Roach
2576a6f13a4aSGreg Roach    /**
2577a6f13a4aSGreg Roach     * Handle <Report>
2578a6f13a4aSGreg Roach     */
2579c1010edaSGreg Roach    private function reportStartHandler()
2580c1010edaSGreg Roach    {
2581a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2582a6f13a4aSGreg Roach    }
2583a6f13a4aSGreg Roach
2584a6f13a4aSGreg Roach    /**
2585a6f13a4aSGreg Roach     * Handle </Report>
2586a6f13a4aSGreg Roach     */
2587c1010edaSGreg Roach    private function reportEndHandler()
2588c1010edaSGreg Roach    {
2589a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2590a6f13a4aSGreg Roach    }
2591a6f13a4aSGreg Roach
2592a6f13a4aSGreg Roach    /**
259376692c8bSGreg Roach     * XML </titleEndHandler>
2594a6f13a4aSGreg Roach     */
2595c1010edaSGreg Roach    private function titleEndHandler()
2596c1010edaSGreg Roach    {
25972836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2598a6f13a4aSGreg Roach    }
2599a6f13a4aSGreg Roach
2600a6f13a4aSGreg Roach    /**
260176692c8bSGreg Roach     * XML </descriptionEndHandler>
2602a6f13a4aSGreg Roach     */
2603c1010edaSGreg Roach    private function descriptionEndHandler()
2604c1010edaSGreg Roach    {
26052836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2606a6f13a4aSGreg Roach    }
2607729ce104SGreg Roach
2608729ce104SGreg Roach    /**
260976692c8bSGreg Roach     * Create a list of all descendants.
261076692c8bSGreg Roach     *
2611729ce104SGreg Roach     * @param string[] $list
2612729ce104SGreg Roach     * @param string   $pid
2613729ce104SGreg Roach     * @param bool     $parents
2614729ce104SGreg Roach     * @param int      $generations
2615729ce104SGreg Roach     */
2616c1010edaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1)
2617c1010edaSGreg Roach    {
2618299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2619729ce104SGreg Roach        if ($person === null) {
2620729ce104SGreg Roach            return;
2621729ce104SGreg Roach        }
2622729ce104SGreg Roach        if (!isset($list[$pid])) {
2623729ce104SGreg Roach            $list[$pid] = $person;
2624729ce104SGreg Roach        }
2625729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2626729ce104SGreg Roach            $list[$pid]->generation = 0;
2627729ce104SGreg Roach        }
2628729ce104SGreg Roach        foreach ($person->getSpouseFamilies() as $family) {
2629729ce104SGreg Roach            if ($parents) {
2630729ce104SGreg Roach                $husband = $family->getHusband();
2631729ce104SGreg Roach                $wife    = $family->getWife();
2632729ce104SGreg Roach                if ($husband) {
2633729ce104SGreg Roach                    $list[$husband->getXref()] = $husband;
2634729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2635729ce104SGreg Roach                        $list[$husband->getXref()]->generation = $list[$pid]->generation - 1;
2636729ce104SGreg Roach                    } else {
2637729ce104SGreg Roach                        $list[$husband->getXref()]->generation = 1;
2638729ce104SGreg Roach                    }
2639729ce104SGreg Roach                }
2640729ce104SGreg Roach                if ($wife) {
2641729ce104SGreg Roach                    $list[$wife->getXref()] = $wife;
2642729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2643729ce104SGreg Roach                        $list[$wife->getXref()]->generation = $list[$pid]->generation - 1;
2644729ce104SGreg Roach                    } else {
2645729ce104SGreg Roach                        $list[$wife->getXref()]->generation = 1;
2646729ce104SGreg Roach                    }
2647729ce104SGreg Roach                }
2648729ce104SGreg Roach            }
2649729ce104SGreg Roach            $children = $family->getChildren();
2650729ce104SGreg Roach            foreach ($children as $child) {
2651729ce104SGreg Roach                if ($child) {
2652729ce104SGreg Roach                    $list[$child->getXref()] = $child;
2653729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2654729ce104SGreg Roach                        $list[$child->getXref()]->generation = $list[$pid]->generation + 1;
2655729ce104SGreg Roach                    } else {
2656729ce104SGreg Roach                        $list[$child->getXref()]->generation = 2;
2657729ce104SGreg Roach                    }
2658729ce104SGreg Roach                }
2659729ce104SGreg Roach            }
2660729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2661729ce104SGreg Roach                foreach ($children as $child) {
26623d7a8a4cSGreg Roach                    $this->addDescendancy($list, $child->getXref(), $parents, $generations); // recurse on the childs family
2663729ce104SGreg Roach                }
2664729ce104SGreg Roach            }
2665729ce104SGreg Roach        }
2666729ce104SGreg Roach    }
2667729ce104SGreg Roach
2668729ce104SGreg Roach    /**
266976692c8bSGreg Roach     * Create a list of all ancestors.
267076692c8bSGreg Roach     *
2671729ce104SGreg Roach     * @param string[] $list
2672729ce104SGreg Roach     * @param string   $pid
2673729ce104SGreg Roach     * @param bool     $children
2674729ce104SGreg Roach     * @param int      $generations
2675729ce104SGreg Roach     */
2676c1010edaSGreg Roach    private function addAncestors(&$list, $pid, $children = false, $generations = -1)
2677c1010edaSGreg Roach    {
267813abd6f3SGreg Roach        $genlist                = [$pid];
2679729ce104SGreg Roach        $list[$pid]->generation = 1;
2680729ce104SGreg Roach        while (count($genlist) > 0) {
2681729ce104SGreg Roach            $id = array_shift($genlist);
2682729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2683729ce104SGreg Roach                continue; // id can be something like “empty7”
2684729ce104SGreg Roach            }
2685299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
2686729ce104SGreg Roach            foreach ($person->getChildFamilies() as $family) {
2687729ce104SGreg Roach                $husband = $family->getHusband();
2688729ce104SGreg Roach                $wife    = $family->getWife();
2689729ce104SGreg Roach                if ($husband) {
2690729ce104SGreg Roach                    $list[$husband->getXref()]             = $husband;
2691729ce104SGreg Roach                    $list[$husband->getXref()]->generation = $list[$id]->generation + 1;
2692729ce104SGreg Roach                }
2693729ce104SGreg Roach                if ($wife) {
2694729ce104SGreg Roach                    $list[$wife->getXref()]             = $wife;
2695729ce104SGreg Roach                    $list[$wife->getXref()]->generation = $list[$id]->generation + 1;
2696729ce104SGreg Roach                }
2697729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2698729ce104SGreg Roach                    if ($husband) {
26999b3dd960SGreg Roach                        $genlist[] = $husband->getXref();
2700729ce104SGreg Roach                    }
2701729ce104SGreg Roach                    if ($wife) {
27029b3dd960SGreg Roach                        $genlist[] = $wife->getXref();
2703729ce104SGreg Roach                    }
2704729ce104SGreg Roach                }
2705729ce104SGreg Roach                if ($children) {
2706729ce104SGreg Roach                    foreach ($family->getChildren() as $child) {
2707729ce104SGreg Roach                        $list[$child->getXref()] = $child;
2708729ce104SGreg Roach                        if (isset($list[$id]->generation)) {
2709729ce104SGreg Roach                            $list[$child->getXref()]->generation = $list[$id]->generation;
2710729ce104SGreg Roach                        } else {
2711729ce104SGreg Roach                            $list[$child->getXref()]->generation = 1;
2712729ce104SGreg Roach                        }
2713729ce104SGreg Roach                    }
2714729ce104SGreg Roach                }
2715729ce104SGreg Roach            }
2716729ce104SGreg Roach        }
2717729ce104SGreg Roach    }
2718729ce104SGreg Roach
2719729ce104SGreg Roach    /**
2720729ce104SGreg Roach     * get gedcom tag value
2721729ce104SGreg Roach     *
2722729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2723729ce104SGreg 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
2724729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2725729ce104SGreg Roach     *
2726729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2727729ce104SGreg Roach     */
2728c1010edaSGreg Roach    private function getGedcomValue($tag, $level, $gedrec)
2729c1010edaSGreg Roach    {
2730729ce104SGreg Roach        if (empty($gedrec)) {
2731729ce104SGreg Roach            return '';
2732729ce104SGreg Roach        }
2733729ce104SGreg Roach        $tags      = explode(':', $tag);
2734729ce104SGreg Roach        $origlevel = $level;
2735729ce104SGreg Roach        if ($level == 0) {
27363c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2737729ce104SGreg Roach        }
2738729ce104SGreg Roach
2739729ce104SGreg Roach        $subrec = $gedrec;
2740729ce104SGreg Roach        foreach ($tags as $t) {
2741729ce104SGreg Roach            $lastsubrec = $subrec;
27423d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2743729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2744729ce104SGreg Roach                $level--;
27453d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2746729ce104SGreg Roach            }
2747729ce104SGreg Roach            if (empty($subrec)) {
27487a6ee1acSGreg Roach                if ($t == 'TITL') {
27493d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2750729ce104SGreg Roach                    if (!empty($subrec)) {
27517a6ee1acSGreg Roach                        $t = 'ABBR';
2752729ce104SGreg Roach                    }
2753729ce104SGreg Roach                }
2754729ce104SGreg Roach                if (empty($subrec)) {
2755729ce104SGreg Roach                    if ($level > 0) {
2756729ce104SGreg Roach                        $level--;
2757729ce104SGreg Roach                    }
27583d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2759729ce104SGreg Roach                    if (empty($subrec)) {
2760729ce104SGreg Roach                        return '';
2761729ce104SGreg Roach                    }
2762729ce104SGreg Roach                }
2763729ce104SGreg Roach            }
2764729ce104SGreg Roach            $level++;
2765729ce104SGreg Roach        }
2766729ce104SGreg Roach        $level--;
2767729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2768729ce104SGreg Roach        if ($ct == 0) {
2769729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2770729ce104SGreg Roach        }
2771729ce104SGreg Roach        if ($ct == 0) {
2772729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2773729ce104SGreg Roach        }
2774729ce104SGreg Roach        if ($ct > 0) {
2775729ce104SGreg Roach            $value = trim($match[1]);
2776729ce104SGreg Roach            if ($t == 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2777299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2778729ce104SGreg Roach                if ($note) {
2779729ce104SGreg Roach                    $value = $note->getNote();
2780729ce104SGreg Roach                } else {
2781729ce104SGreg Roach                    //-- set the value to the id without the @
2782729ce104SGreg Roach                    $value = $match[1];
2783729ce104SGreg Roach                }
2784729ce104SGreg Roach            }
27857a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
27863d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2787729ce104SGreg Roach            }
2788729ce104SGreg Roach
2789729ce104SGreg Roach            return $value;
2790729ce104SGreg Roach        }
2791729ce104SGreg Roach
27927a6ee1acSGreg Roach        return '';
2793729ce104SGreg Roach    }
2794d1286247SGreg Roach
2795d1286247SGreg Roach    /**
2796d1286247SGreg Roach     * Replace variable identifiers with their values.
2797d1286247SGreg Roach     *
2798d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
279982759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2800d1286247SGreg Roach     *
2801d1286247SGreg Roach     * @return string
2802d1286247SGreg Roach     */
2803c1010edaSGreg Roach    private function substituteVars($expression, $quote)
2804c1010edaSGreg Roach    {
2805d1286247SGreg Roach        return preg_replace_callback(
2806d1286247SGreg Roach            '/\$(\w+)/',
28072118c0e3SGreg Roach            function ($matches) use ($quote) {
28082118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
280982759250SGreg Roach                    if ($quote) {
28102118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
281182759250SGreg Roach                    } else {
28122118c0e3SGreg Roach                        return $this->vars[$matches[1]]['id'];
281382759250SGreg Roach                    }
2814d1286247SGreg Roach                } else {
2815d1286247SGreg Roach                    Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28163d7a8a4cSGreg Roach
2817d1286247SGreg Roach                    return '$' . $matches[1];
2818d1286247SGreg Roach                }
2819d1286247SGreg Roach            },
2820d1286247SGreg Roach            $expression
2821d1286247SGreg Roach        );
2822d1286247SGreg Roach    }
2823a6f13a4aSGreg Roach}
2824