xref: /webtrees/app/Report/ReportParserGenerate.php (revision b2ce94c6833c25934b40a7e6bc15985d50b5f42a)
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;
3479529c87SGreg Roachuse stdClass;
35cb63a60eSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionFunction;
365809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
37a6f13a4aSGreg Roach
38a6f13a4aSGreg Roach/**
39a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
40a6f13a4aSGreg Roach */
41c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
42c1010edaSGreg Roach{
43a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
44a6f13a4aSGreg Roach    private $process_footnote = true;
45a6f13a4aSGreg Roach
46a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
47a6f13a4aSGreg Roach    private $print_data = false;
48a6f13a4aSGreg Roach
49a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5013abd6f3SGreg Roach    private $print_data_stack = [];
51a6f13a4aSGreg Roach
5276692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
53a6f13a4aSGreg Roach    private $process_gedcoms = 0;
54a6f13a4aSGreg Roach
5576692c8bSGreg Roach    /** @var int Are we processing conditionals */
56a6f13a4aSGreg Roach    private $process_ifs = 0;
57a6f13a4aSGreg Roach
5876692c8bSGreg Roach    /** @var int Are we processing repeats */
59a6f13a4aSGreg Roach    private $process_repeats = 0;
60a6f13a4aSGreg Roach
61a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
62a6f13a4aSGreg Roach    private $repeat_bytes = 0;
63a6f13a4aSGreg Roach
645b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
6513abd6f3SGreg Roach    private $repeats = [];
66a6f13a4aSGreg Roach
67a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
6813abd6f3SGreg Roach    private $repeats_stack = [];
69a6f13a4aSGreg Roach
70e8e7866bSGreg Roach    /** @var ReportBase[] Nested repeating data */
7113abd6f3SGreg Roach    private $wt_report_stack = [];
72e8e7866bSGreg Roach
73e8e7866bSGreg Roach    /** @var resource Nested repeating data */
74e8e7866bSGreg Roach    private $parser;
75e8e7866bSGreg Roach
76e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
7713abd6f3SGreg Roach    private $parser_stack = [];
78e8e7866bSGreg Roach
79a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
80a6f13a4aSGreg Roach    private $gedrec = '';
81a6f13a4aSGreg Roach
82a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
8313abd6f3SGreg Roach    private $gedrec_stack = [];
84a6f13a4aSGreg Roach
85a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
86a6f13a4aSGreg Roach    private $current_element;
87a6f13a4aSGreg Roach
88a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
89a6f13a4aSGreg Roach    private $footnote_element;
90a6f13a4aSGreg Roach
91a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
92a6f13a4aSGreg Roach    private $fact = '';
93a6f13a4aSGreg Roach
94a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
95a6f13a4aSGreg Roach    private $desc = '';
96a6f13a4aSGreg Roach
97a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
98a6f13a4aSGreg Roach    private $type = '';
99a6f13a4aSGreg Roach
100a6f13a4aSGreg Roach    /** @var int The current generational level */
101a6f13a4aSGreg Roach    private $generation = 1;
102a6f13a4aSGreg Roach
103a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
10413abd6f3SGreg Roach    private $list = [];
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var int Number of items in lists */
107a6f13a4aSGreg Roach    private $list_total = 0;
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
110a6f13a4aSGreg Roach    private $list_private = 0;
111a6f13a4aSGreg Roach
112299d100dSGreg Roach    /** @var string The filename of the XML report */
113299d100dSGreg Roach    protected $report;
114299d100dSGreg Roach
115e8e7866bSGreg Roach    /** @var ReportBase A factory for creating report elements */
116e8e7866bSGreg Roach    private $report_root;
117e8e7866bSGreg Roach
1185b084b24SGreg Roach    /** @var ReportBaseElement Nested report elements */
119e8e7866bSGreg Roach    private $wt_report;
120e8e7866bSGreg Roach
121d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1222118c0e3SGreg Roach    private $vars;
123d1286247SGreg Roach
124299d100dSGreg Roach    /** @var Tree The current tree */
125299d100dSGreg Roach    private $tree;
126299d100dSGreg Roach
12776692c8bSGreg Roach    /**
12876692c8bSGreg Roach     * Create a parser for a report
12976692c8bSGreg Roach     *
13076692c8bSGreg Roach     * @param string     $report The XML filename
13176692c8bSGreg Roach     * @param ReportBase $report_root
13276692c8bSGreg Roach     * @param string[][] $vars
133299d100dSGreg Roach     * @param Tree       $tree
13476692c8bSGreg Roach     */
135c1010edaSGreg Roach    public function __construct($report, ReportBase $report_root, array $vars, Tree $tree)
136c1010edaSGreg Roach    {
137299d100dSGreg Roach        $this->report          = $report;
138e8e7866bSGreg Roach        $this->report_root     = $report_root;
139e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14059f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
141d1286247SGreg Roach        $this->vars            = $vars;
142299d100dSGreg Roach        $this->tree            = $tree;
143299d100dSGreg Roach
14476f666f4SGreg Roach        parent::__construct($report);
145a6f13a4aSGreg Roach    }
146a6f13a4aSGreg Roach
147a6f13a4aSGreg Roach    /**
148a6f13a4aSGreg Roach     * XML start element handler
149a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
150a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
151a6f13a4aSGreg Roach     *
152a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
153a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
154a6f13a4aSGreg Roach     * @param array    $attrs  an array of key value pairs for the attributes
155a6f13a4aSGreg Roach     */
156c1010edaSGreg Roach    protected function startElement($parser, $name, $attrs)
157c1010edaSGreg Roach    {
15813abd6f3SGreg Roach        $newattrs = [];
159a6f13a4aSGreg Roach
160a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
161a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
162d1286247SGreg Roach                if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
163d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
164a6f13a4aSGreg Roach                }
165a6f13a4aSGreg Roach            }
166a6f13a4aSGreg Roach            $newattrs[$key] = $value;
167a6f13a4aSGreg Roach        }
168a6f13a4aSGreg Roach        $attrs = $newattrs;
1697a6ee1acSGreg 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')) {
170a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
171a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
172a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
173a6f13a4aSGreg Roach                $this->$start_method($attrs);
174a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
175a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
176a6f13a4aSGreg Roach            }
177a6f13a4aSGreg Roach        }
178a6f13a4aSGreg Roach    }
179a6f13a4aSGreg Roach
180a6f13a4aSGreg Roach    /**
181a6f13a4aSGreg Roach     * XML end element handler
182a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
183a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
184a6f13a4aSGreg Roach     *
185a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
186a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
187a6f13a4aSGreg Roach     */
188c1010edaSGreg Roach    protected function endElement($parser, $name)
189c1010edaSGreg Roach    {
1907a6ee1acSGreg 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')) {
191a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
192a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
193a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
194a6f13a4aSGreg Roach                $this->$end_method();
195a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
196a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
197a6f13a4aSGreg Roach            }
198a6f13a4aSGreg Roach        }
199a6f13a4aSGreg Roach    }
200a6f13a4aSGreg Roach
201a6f13a4aSGreg Roach    /**
202a6f13a4aSGreg Roach     * XML character data handler
203a6f13a4aSGreg Roach     *
204a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
205a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
206a6f13a4aSGreg Roach     */
207c1010edaSGreg Roach    protected function characterData($parser, $data)
208c1010edaSGreg Roach    {
209e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
210a6f13a4aSGreg Roach            $this->current_element->addText($data);
211a6f13a4aSGreg Roach        }
212a6f13a4aSGreg Roach    }
213a6f13a4aSGreg Roach
214a6f13a4aSGreg Roach    /**
21576692c8bSGreg Roach     * XML <style>
216a6f13a4aSGreg Roach     *
217a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
2188ba2e626SGreg Roach     *
2198ba2e626SGreg Roach     * @return void
220a6f13a4aSGreg Roach     */
221c1010edaSGreg Roach    private function styleStartHandler($attrs)
222c1010edaSGreg Roach    {
223a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
224a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
225a6f13a4aSGreg Roach        }
226a6f13a4aSGreg Roach
227a6f13a4aSGreg Roach        // array Style that will be passed on
22813abd6f3SGreg Roach        $s = [];
229a6f13a4aSGreg Roach
230a6f13a4aSGreg Roach        // string Name af the style
231a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
232a6f13a4aSGreg Roach
233a6f13a4aSGreg Roach        // string Name of the DEFAULT font
234e8e7866bSGreg Roach        $s['font'] = $this->wt_report->defaultFont;
235a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
236a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
237a6f13a4aSGreg Roach        }
238a6f13a4aSGreg Roach
239a6f13a4aSGreg Roach        // int The size of the font in points
240e8e7866bSGreg Roach        $s['size'] = $this->wt_report->defaultFontSize;
241a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
242a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
243a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
244a6f13a4aSGreg Roach
245a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2467a6ee1acSGreg Roach        $s['style'] = '';
247a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
248a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
249a6f13a4aSGreg Roach        }
250a6f13a4aSGreg Roach
251e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
252a6f13a4aSGreg Roach    }
253a6f13a4aSGreg Roach
254a6f13a4aSGreg Roach    /**
25576692c8bSGreg Roach     * XML <Doc>
256a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
257a6f13a4aSGreg Roach     *
258a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
2598ba2e626SGreg Roach     *
2608ba2e626SGreg Roach     * @return void
261a6f13a4aSGreg Roach     */
262c1010edaSGreg Roach    private function docStartHandler($attrs)
263c1010edaSGreg Roach    {
264e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
265a6f13a4aSGreg Roach
266a6f13a4aSGreg Roach        // Custom page width
267a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
268e8e7866bSGreg Roach            $this->wt_report->pagew = (int) $attrs['customwidth'];
269a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
270a6f13a4aSGreg Roach        // Custom Page height
271a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
272e8e7866bSGreg Roach            $this->wt_report->pageh = (int) $attrs['customheight'];
273a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
274a6f13a4aSGreg Roach
275a6f13a4aSGreg Roach        // Left Margin
276a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2777a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
278e8e7866bSGreg Roach                $this->wt_report->leftmargin = 0;
279a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
280e8e7866bSGreg 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))
281a6f13a4aSGreg Roach            }
282a6f13a4aSGreg Roach        }
283a6f13a4aSGreg Roach        // Right Margin
284a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
2857a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
286e8e7866bSGreg Roach                $this->wt_report->rightmargin = 0;
287a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
288e8e7866bSGreg 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))
289a6f13a4aSGreg Roach            }
290a6f13a4aSGreg Roach        }
291a6f13a4aSGreg Roach        // Top Margin
292a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
2937a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
294e8e7866bSGreg Roach                $this->wt_report->topmargin = 0;
295a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
296e8e7866bSGreg 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))
297a6f13a4aSGreg Roach            }
298a6f13a4aSGreg Roach        }
299a6f13a4aSGreg Roach        // Bottom Margin
300a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3017a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
302e8e7866bSGreg Roach                $this->wt_report->bottommargin = 0;
303a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
304e8e7866bSGreg 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))
305a6f13a4aSGreg Roach            }
306a6f13a4aSGreg Roach        }
307a6f13a4aSGreg Roach        // Header Margin
308a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3097a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
310e8e7866bSGreg Roach                $this->wt_report->headermargin = 0;
311a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
312e8e7866bSGreg 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))
313a6f13a4aSGreg Roach            }
314a6f13a4aSGreg Roach        }
315a6f13a4aSGreg Roach        // Footer Margin
316a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3177a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
318e8e7866bSGreg Roach                $this->wt_report->footermargin = 0;
319a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
320e8e7866bSGreg 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))
321a6f13a4aSGreg Roach            }
322a6f13a4aSGreg Roach        }
323a6f13a4aSGreg Roach
324a6f13a4aSGreg Roach        // Page Orientation
325a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
3267a6ee1acSGreg Roach            if ($attrs['orientation'] == 'landscape') {
3277a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
3287a6ee1acSGreg Roach            } elseif ($attrs['orientation'] == 'portrait') {
3297a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
330a6f13a4aSGreg Roach            }
331a6f13a4aSGreg Roach        }
332a6f13a4aSGreg Roach        // Page Size
333a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
334e8e7866bSGreg Roach            $this->wt_report->pageFormat = strtoupper($attrs['pageSize']);
335a6f13a4aSGreg Roach        }
336a6f13a4aSGreg Roach
337a6f13a4aSGreg Roach        // Show Generated By...
338a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3397a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
340e8e7866bSGreg Roach                $this->wt_report->showGenText = false;
3417a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
342e8e7866bSGreg Roach                $this->wt_report->showGenText = true;
343a6f13a4aSGreg Roach            }
344a6f13a4aSGreg Roach        }
345a6f13a4aSGreg Roach
346e8e7866bSGreg Roach        $this->wt_report->setup();
347a6f13a4aSGreg Roach    }
348a6f13a4aSGreg Roach
349a6f13a4aSGreg Roach    /**
35076692c8bSGreg Roach     * XML </Doc>
3518ba2e626SGreg Roach     *
3528ba2e626SGreg Roach     * @return void
353a6f13a4aSGreg Roach     */
354c1010edaSGreg Roach    private function docEndHandler()
355c1010edaSGreg Roach    {
356e8e7866bSGreg Roach        $this->wt_report->run();
357a6f13a4aSGreg Roach    }
358a6f13a4aSGreg Roach
359a6f13a4aSGreg Roach    /**
36076692c8bSGreg Roach     * XML <Header>
3618ba2e626SGreg Roach     *
3628ba2e626SGreg Roach     * @return void
363a6f13a4aSGreg Roach     */
364c1010edaSGreg Roach    private function headerStartHandler()
365c1010edaSGreg Roach    {
366a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
367e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3687a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
369a6f13a4aSGreg Roach    }
370a6f13a4aSGreg Roach
371a6f13a4aSGreg Roach    /**
37276692c8bSGreg Roach     * XML <PageHeader>
3738ba2e626SGreg Roach     *
3748ba2e626SGreg Roach     * @return void
375a6f13a4aSGreg Roach     */
376c1010edaSGreg Roach    private function pageHeaderStartHandler()
377c1010edaSGreg Roach    {
3789b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
379a6f13a4aSGreg Roach        $this->print_data         = false;
3809b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
381e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
382a6f13a4aSGreg Roach    }
383a6f13a4aSGreg Roach
384a6f13a4aSGreg Roach    /**
38576692c8bSGreg Roach     * XML <pageHeaderEndHandler>
3868ba2e626SGreg Roach     *
3878ba2e626SGreg Roach     * @return void
388a6f13a4aSGreg Roach     */
389c1010edaSGreg Roach    private function pageHeaderEndHandler()
390c1010edaSGreg Roach    {
391a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
392e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
393e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
394e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
395a6f13a4aSGreg Roach    }
396a6f13a4aSGreg Roach
397a6f13a4aSGreg Roach    /**
39876692c8bSGreg Roach     * XML <bodyStartHandler>
3998ba2e626SGreg Roach     *
4008ba2e626SGreg Roach     * @return void
401a6f13a4aSGreg Roach     */
402c1010edaSGreg Roach    private function bodyStartHandler()
403c1010edaSGreg Roach    {
4047a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
405a6f13a4aSGreg Roach    }
406a6f13a4aSGreg Roach
407a6f13a4aSGreg Roach    /**
40876692c8bSGreg Roach     * XML <footerStartHandler>
4098ba2e626SGreg Roach     *
4108ba2e626SGreg Roach     * @return void
411a6f13a4aSGreg Roach     */
412c1010edaSGreg Roach    private function footerStartHandler()
413c1010edaSGreg Roach    {
4147a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
415a6f13a4aSGreg Roach    }
416a6f13a4aSGreg Roach
417a6f13a4aSGreg Roach    /**
41876692c8bSGreg Roach     * XML <Cell>
419a6f13a4aSGreg Roach     *
420a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
4218ba2e626SGreg Roach     *
4228ba2e626SGreg Roach     * @return void
423a6f13a4aSGreg Roach     */
424c1010edaSGreg Roach    private function cellStartHandler($attrs)
425c1010edaSGreg Roach    {
426a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4277a6ee1acSGreg Roach        $align = '';
428a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
429a6f13a4aSGreg Roach            $align = $attrs['align'];
430a6f13a4aSGreg Roach            // RTL supported left/right alignment
4317a6ee1acSGreg Roach            if ($align == 'rightrtl') {
432e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4337a6ee1acSGreg Roach                    $align = 'left';
434a6f13a4aSGreg Roach                } else {
4357a6ee1acSGreg Roach                    $align = 'right';
436a6f13a4aSGreg Roach                }
4377a6ee1acSGreg Roach            } elseif ($align == 'leftrtl') {
438e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4397a6ee1acSGreg Roach                    $align = 'right';
440a6f13a4aSGreg Roach                } else {
4417a6ee1acSGreg Roach                    $align = 'left';
442a6f13a4aSGreg Roach                }
443a6f13a4aSGreg Roach            }
444a6f13a4aSGreg Roach        }
445a6f13a4aSGreg Roach
446a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4477a6ee1acSGreg Roach        $bgcolor = '';
448a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
449a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
450a6f13a4aSGreg Roach        }
451a6f13a4aSGreg Roach
452a6f13a4aSGreg Roach        // int Whether or not the background should be painted
453a6f13a4aSGreg Roach        $fill = 1;
454a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4557a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
456a6f13a4aSGreg Roach                $fill = 0;
4577a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
458a6f13a4aSGreg Roach                $fill = 1;
459a6f13a4aSGreg Roach            }
460a6f13a4aSGreg Roach        }
461a6f13a4aSGreg Roach
462a6f13a4aSGreg Roach        $reseth = true;
463a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
464a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4657a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
466a6f13a4aSGreg Roach                $reseth = false;
4677a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
468a6f13a4aSGreg Roach                $reseth = true;
469a6f13a4aSGreg Roach            }
470a6f13a4aSGreg Roach        }
471a6f13a4aSGreg Roach
472a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
473a6f13a4aSGreg Roach        $border = 0;
474a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
475a6f13a4aSGreg Roach            $border = $attrs['border'];
476a6f13a4aSGreg Roach        }
477a6f13a4aSGreg Roach        // string Border color in HTML code
4787a6ee1acSGreg Roach        $bocolor = '';
479a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
480a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
481a6f13a4aSGreg Roach        }
482a6f13a4aSGreg Roach
483a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
484a6f13a4aSGreg Roach        $height = 0;
485a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
486a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
487a6f13a4aSGreg Roach        }
488a6f13a4aSGreg 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.
489a6f13a4aSGreg Roach        $width = 0;
490a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
491a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
492a6f13a4aSGreg Roach        }
493a6f13a4aSGreg Roach
494a6f13a4aSGreg Roach        // int Stretch carachter mode
495a6f13a4aSGreg Roach        $stretch = 0;
496a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
497a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
498a6f13a4aSGreg Roach        }
499a6f13a4aSGreg Roach
500a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
5017a6ee1acSGreg Roach        $left = '.';
502a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5037a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
5047a6ee1acSGreg Roach                $left = '.';
505a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
506a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5077a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
508a6f13a4aSGreg Roach                $left = 0;
509a6f13a4aSGreg Roach            }
510a6f13a4aSGreg Roach        }
511a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
5127a6ee1acSGreg Roach        $top = '.';
513a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5147a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
5157a6ee1acSGreg Roach                $top = '.';
516a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
517a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5187a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
519a6f13a4aSGreg Roach                $top = 0;
520a6f13a4aSGreg Roach            }
521a6f13a4aSGreg Roach        }
522a6f13a4aSGreg Roach
523a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5247a6ee1acSGreg Roach        $style = '';
525a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
526a6f13a4aSGreg Roach            $style = $attrs['style'];
527a6f13a4aSGreg Roach        }
528a6f13a4aSGreg Roach
529a6f13a4aSGreg Roach        // string Text color in html code
5307a6ee1acSGreg Roach        $tcolor = '';
531a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
532a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
533a6f13a4aSGreg Roach        }
534a6f13a4aSGreg Roach
535a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
536a6f13a4aSGreg Roach        $ln = 0;
537a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
538a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
539a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5407a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
541a6f13a4aSGreg Roach                $ln = 0;
542a6f13a4aSGreg Roach            }
543a6f13a4aSGreg Roach        }
544a6f13a4aSGreg Roach
5457a6ee1acSGreg Roach        if ($align == 'left') {
5467a6ee1acSGreg Roach            $align = 'L';
5477a6ee1acSGreg Roach        } elseif ($align == 'right') {
5487a6ee1acSGreg Roach            $align = 'R';
5497a6ee1acSGreg Roach        } elseif ($align == 'center') {
5507a6ee1acSGreg Roach            $align = 'C';
5517a6ee1acSGreg Roach        } elseif ($align == 'justify') {
5527a6ee1acSGreg Roach            $align = 'J';
553a6f13a4aSGreg Roach        }
554a6f13a4aSGreg Roach
5559b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
556a6f13a4aSGreg Roach        $this->print_data         = true;
557a6f13a4aSGreg Roach
558e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
559a6f13a4aSGreg Roach            $width,
560a6f13a4aSGreg Roach            $height,
561a6f13a4aSGreg Roach            $border,
562a6f13a4aSGreg Roach            $align,
563a6f13a4aSGreg Roach            $bgcolor,
564a6f13a4aSGreg Roach            $style,
565a6f13a4aSGreg Roach            $ln,
566a6f13a4aSGreg Roach            $top,
567a6f13a4aSGreg Roach            $left,
568a6f13a4aSGreg Roach            $fill,
569a6f13a4aSGreg Roach            $stretch,
570a6f13a4aSGreg Roach            $bocolor,
571a6f13a4aSGreg Roach            $tcolor,
572a6f13a4aSGreg Roach            $reseth
573a6f13a4aSGreg Roach        );
574a6f13a4aSGreg Roach    }
575a6f13a4aSGreg Roach
576a6f13a4aSGreg Roach    /**
57776692c8bSGreg Roach     * XML </Cell>
5788ba2e626SGreg Roach     *
5798ba2e626SGreg Roach     * @return void
580a6f13a4aSGreg Roach     */
581c1010edaSGreg Roach    private function cellEndHandler()
582c1010edaSGreg Roach    {
583a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
584e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
585a6f13a4aSGreg Roach    }
586a6f13a4aSGreg Roach
587a6f13a4aSGreg Roach    /**
588a6f13a4aSGreg Roach     * XML <Now /> element handler
5898ba2e626SGreg Roach     *
5908ba2e626SGreg Roach     * @return void
591a6f13a4aSGreg Roach     */
592c1010edaSGreg Roach    private function nowStartHandler()
593c1010edaSGreg Roach    {
5943d7a8a4cSGreg Roach        $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
595a6f13a4aSGreg Roach        $this->current_element->addText($g->display());
596a6f13a4aSGreg Roach    }
597a6f13a4aSGreg Roach
598a6f13a4aSGreg Roach    /**
599a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6008ba2e626SGreg Roach     *
6018ba2e626SGreg Roach     * @return void
602a6f13a4aSGreg Roach     */
603c1010edaSGreg Roach    private function pageNumStartHandler()
604c1010edaSGreg Roach    {
6057a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
606a6f13a4aSGreg Roach    }
607a6f13a4aSGreg Roach
608a6f13a4aSGreg Roach    /**
609a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6108ba2e626SGreg Roach     *
6118ba2e626SGreg Roach     * @return void
612a6f13a4aSGreg Roach     */
613c1010edaSGreg Roach    private function totalPagesStartHandler()
614c1010edaSGreg Roach    {
6157a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
616a6f13a4aSGreg Roach    }
617a6f13a4aSGreg Roach
618a6f13a4aSGreg Roach    /**
619a6f13a4aSGreg Roach     * Called at the start of an element.
620a6f13a4aSGreg Roach     *
621a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
6228ba2e626SGreg Roach     *
6238ba2e626SGreg Roach     * @return void
624a6f13a4aSGreg Roach     */
625c1010edaSGreg Roach    private function gedcomStartHandler($attrs)
626c1010edaSGreg Roach    {
627a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
628a6f13a4aSGreg Roach            $this->process_gedcoms++;
629a6f13a4aSGreg Roach
630a6f13a4aSGreg Roach            return;
631a6f13a4aSGreg Roach        }
632a6f13a4aSGreg Roach
633a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6347a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6357a6ee1acSGreg Roach        $tags      = explode(':', $tag);
636a6f13a4aSGreg Roach        $newgedrec = '';
637a6f13a4aSGreg Roach        if (count($tags) < 2) {
638299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
639299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
640a6f13a4aSGreg Roach        }
641a6f13a4aSGreg Roach        if (empty($newgedrec)) {
642a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
643a6f13a4aSGreg Roach            $newgedrec = '';
644a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6457a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
646d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
647d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
648a6f13a4aSGreg Roach                    } else {
649299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
650299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
651a6f13a4aSGreg Roach                    }
652a6f13a4aSGreg Roach                } else {
6537a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
65413abd6f3SGreg Roach                        $gmatch = [];
655a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
656299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
657299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
658a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
659a6f13a4aSGreg Roach                        } else {
660a6f13a4aSGreg Roach                            $newgedrec = '';
661a6f13a4aSGreg Roach                            break;
662a6f13a4aSGreg Roach                        }
663a6f13a4aSGreg Roach                    } else {
6647a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
665a6f13a4aSGreg Roach                        $level     = $temp[0] + 1;
6663d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
667a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
668a6f13a4aSGreg Roach                    }
669a6f13a4aSGreg Roach                }
670a6f13a4aSGreg Roach            }
671a6f13a4aSGreg Roach        }
672a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6739b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
674a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
675a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
676a6f13a4aSGreg Roach                $this->fact = $match[2];
677a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
678a6f13a4aSGreg Roach            }
679a6f13a4aSGreg Roach        } else {
680a6f13a4aSGreg Roach            $this->process_gedcoms++;
681a6f13a4aSGreg Roach        }
682a6f13a4aSGreg Roach    }
683a6f13a4aSGreg Roach
684a6f13a4aSGreg Roach    /**
685a6f13a4aSGreg Roach     * Called at the end of an element.
6868ba2e626SGreg Roach     *
6878ba2e626SGreg Roach     * @return void
688a6f13a4aSGreg Roach     */
689c1010edaSGreg Roach    private function gedcomEndHandler()
690c1010edaSGreg Roach    {
691a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
692a6f13a4aSGreg Roach            $this->process_gedcoms--;
693a6f13a4aSGreg Roach        } else {
694a6f13a4aSGreg Roach            list($this->gedrec, $this->fact, $this->desc) = array_pop($this->gedrec_stack);
695a6f13a4aSGreg Roach        }
696a6f13a4aSGreg Roach    }
697a6f13a4aSGreg Roach
698a6f13a4aSGreg Roach    /**
69976692c8bSGreg Roach     * XML <textBoxStartHandler>
700a6f13a4aSGreg Roach     *
701a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
7028ba2e626SGreg Roach     *
7038ba2e626SGreg Roach     * @return void
704a6f13a4aSGreg Roach     */
705c1010edaSGreg Roach    private function textBoxStartHandler($attrs)
706c1010edaSGreg Roach    {
707a6f13a4aSGreg Roach        // string Background color code
7087a6ee1acSGreg Roach        $bgcolor = '';
709a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
710a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
711a6f13a4aSGreg Roach        }
712a6f13a4aSGreg Roach
713a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
714a6f13a4aSGreg Roach        $fill = true;
715a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7167a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
717a6f13a4aSGreg Roach                $fill = false;
7187a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
719a6f13a4aSGreg Roach                $fill = true;
720a6f13a4aSGreg Roach            }
721a6f13a4aSGreg Roach        }
722a6f13a4aSGreg Roach
723a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
724a6f13a4aSGreg Roach        $border = false;
725a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7267a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
727a6f13a4aSGreg Roach                $border = true;
7287a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
729a6f13a4aSGreg Roach                $border = false;
730a6f13a4aSGreg Roach            }
731a6f13a4aSGreg Roach        }
732a6f13a4aSGreg Roach
733a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
734a6f13a4aSGreg Roach        $height = 0;
735a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
736a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
737a6f13a4aSGreg Roach        }
738a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
739a6f13a4aSGreg Roach        $width = 0;
740a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
741a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
742a6f13a4aSGreg Roach        }
743a6f13a4aSGreg Roach
744a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
7457a6ee1acSGreg Roach        $left = '.';
746a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7477a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
7487a6ee1acSGreg Roach                $left = '.';
749a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
750a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7517a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
752a6f13a4aSGreg Roach                $left = 0;
753a6f13a4aSGreg Roach            }
754a6f13a4aSGreg Roach        }
755a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
7567a6ee1acSGreg Roach        $top = '.';
757a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7587a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
7597a6ee1acSGreg Roach                $top = '.';
760a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
761a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7627a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
763a6f13a4aSGreg Roach                $top = 0;
764a6f13a4aSGreg Roach            }
765a6f13a4aSGreg Roach        }
766a6f13a4aSGreg 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
767a6f13a4aSGreg Roach        $newline = false;
768a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7697a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
770a6f13a4aSGreg Roach                $newline = true;
7717a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
772a6f13a4aSGreg Roach                $newline = false;
773a6f13a4aSGreg Roach            }
774a6f13a4aSGreg Roach        }
775a6f13a4aSGreg Roach        // boolean
776a6f13a4aSGreg Roach        $pagecheck = true;
777a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7787a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
779a6f13a4aSGreg Roach                $pagecheck = false;
7807a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
781a6f13a4aSGreg Roach                $pagecheck = true;
782a6f13a4aSGreg Roach            }
783a6f13a4aSGreg Roach        }
784a6f13a4aSGreg Roach        // boolean Cell padding
785a6f13a4aSGreg Roach        $padding = true;
786a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
7877a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
788a6f13a4aSGreg Roach                $padding = false;
7897a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
790a6f13a4aSGreg Roach                $padding = true;
791a6f13a4aSGreg Roach            }
792a6f13a4aSGreg Roach        }
793a6f13a4aSGreg Roach        // boolean Reset this box Height
794a6f13a4aSGreg Roach        $reseth = false;
795a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
7967a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
797a6f13a4aSGreg Roach                $reseth = true;
7987a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
799a6f13a4aSGreg Roach                $reseth = false;
800a6f13a4aSGreg Roach            }
801a6f13a4aSGreg Roach        }
802a6f13a4aSGreg Roach
803a6f13a4aSGreg Roach        // string Style of rendering
8047a6ee1acSGreg Roach        $style = '';
805a6f13a4aSGreg Roach
8069b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
807a6f13a4aSGreg Roach        $this->print_data         = false;
808a6f13a4aSGreg Roach
8099b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
810e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
811a6f13a4aSGreg Roach            $width,
812a6f13a4aSGreg Roach            $height,
813a6f13a4aSGreg Roach            $border,
814a6f13a4aSGreg Roach            $bgcolor,
815a6f13a4aSGreg Roach            $newline,
816a6f13a4aSGreg Roach            $left,
817a6f13a4aSGreg Roach            $top,
818a6f13a4aSGreg Roach            $pagecheck,
819a6f13a4aSGreg Roach            $style,
820a6f13a4aSGreg Roach            $fill,
821a6f13a4aSGreg Roach            $padding,
822a6f13a4aSGreg Roach            $reseth
823a6f13a4aSGreg Roach        );
824a6f13a4aSGreg Roach    }
825a6f13a4aSGreg Roach
826a6f13a4aSGreg Roach    /**
82776692c8bSGreg Roach     * XML <textBoxEndHandler>
8288ba2e626SGreg Roach     *
8298ba2e626SGreg Roach     * @return void
830a6f13a4aSGreg Roach     */
831c1010edaSGreg Roach    private function textBoxEndHandler()
832c1010edaSGreg Roach    {
833a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
834e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
835e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
836e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
837a6f13a4aSGreg Roach    }
838a6f13a4aSGreg Roach
839a6f13a4aSGreg Roach    /**
84076692c8bSGreg Roach     * XLM <Text>.
84176692c8bSGreg Roach     *
842a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
8438ba2e626SGreg Roach     *
8448ba2e626SGreg Roach     * @return void
845a6f13a4aSGreg Roach     */
846c1010edaSGreg Roach    private function textStartHandler($attrs)
847c1010edaSGreg Roach    {
8489b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
849a6f13a4aSGreg Roach        $this->print_data         = true;
850a6f13a4aSGreg Roach
851a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8527a6ee1acSGreg Roach        $style = '';
853a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
854a6f13a4aSGreg Roach            $style = $attrs['style'];
855a6f13a4aSGreg Roach        }
856a6f13a4aSGreg Roach
857a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8587a6ee1acSGreg Roach        $color = '';
859a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
860a6f13a4aSGreg Roach            $color = $attrs['color'];
861a6f13a4aSGreg Roach        }
862a6f13a4aSGreg Roach
863e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
864a6f13a4aSGreg Roach    }
865a6f13a4aSGreg Roach
866a6f13a4aSGreg Roach    /**
86776692c8bSGreg Roach     * XML </Text>
8688ba2e626SGreg Roach     *
8698ba2e626SGreg Roach     * @return void
870a6f13a4aSGreg Roach     */
871c1010edaSGreg Roach    private function textEndHandler()
872c1010edaSGreg Roach    {
873a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
874e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
875a6f13a4aSGreg Roach    }
876a6f13a4aSGreg Roach
877a6f13a4aSGreg Roach    /**
87876692c8bSGreg Roach     * XML <GetPersonName/>
879a6f13a4aSGreg Roach     * Get the name
880a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
881a6f13a4aSGreg Roach     * 2. id is set with a record id
882a6f13a4aSGreg Roach     *
883a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
8848ba2e626SGreg Roach     *
8858ba2e626SGreg Roach     * @return void
886a6f13a4aSGreg Roach     */
887c1010edaSGreg Roach    private function getPersonNameStartHandler($attrs)
888c1010edaSGreg Roach    {
8897a6ee1acSGreg Roach        $id    = '';
89013abd6f3SGreg Roach        $match = [];
891a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
8927a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
893a6f13a4aSGreg Roach                $id = $match[1];
894a6f13a4aSGreg Roach            }
895a6f13a4aSGreg Roach        } else {
8967a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
897d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
898d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
899a6f13a4aSGreg Roach                }
900a6f13a4aSGreg Roach            } else {
9017a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
90213abd6f3SGreg Roach                    $gmatch = [];
903a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
904a6f13a4aSGreg Roach                        $id = $gmatch[1];
905a6f13a4aSGreg Roach                    }
906a6f13a4aSGreg Roach                } else {
907a6f13a4aSGreg Roach                    $id = $attrs['id'];
908a6f13a4aSGreg Roach                }
909a6f13a4aSGreg Roach            }
910a6f13a4aSGreg Roach        }
911a6f13a4aSGreg Roach        if (!empty($id)) {
912299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9138f038c36SRico Sonntag            if ($record === null) {
914a6f13a4aSGreg Roach                return;
915a6f13a4aSGreg Roach            }
916a6f13a4aSGreg Roach            if (!$record->canShowName()) {
917a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
918a6f13a4aSGreg Roach            } else {
919a6f13a4aSGreg Roach                $name = $record->getFullName();
920a6f13a4aSGreg Roach                $name = preg_replace(
921c1010edaSGreg Roach                    [
922c1010edaSGreg Roach                        '/<span class="starredname">/',
923c1010edaSGreg Roach                        '/<\/span><\/span>/',
924c1010edaSGreg Roach                        '/<\/span>/',
925c1010edaSGreg Roach                    ],
926c1010edaSGreg Roach                    [
927c1010edaSGreg Roach                        '«',
928c1010edaSGreg Roach                        '',
929c1010edaSGreg Roach                        '»',
930c1010edaSGreg Roach                    ],
931a6f13a4aSGreg Roach                    $name
932a6f13a4aSGreg Roach                );
933a6f13a4aSGreg Roach                $name = strip_tags($name);
934a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
935a6f13a4aSGreg Roach                    if (mb_strlen($name) > $attrs['truncate']) {
936faffe0b0SGreg Roach                        $name = mb_substr($name, 0, $attrs['truncate'] - 1) . '…';
937a6f13a4aSGreg Roach                    }
938a6f13a4aSGreg Roach                } else {
939a6f13a4aSGreg Roach                    $addname = $record->getAddName();
940a6f13a4aSGreg Roach                    $addname = preg_replace(
941c1010edaSGreg Roach                        [
942c1010edaSGreg Roach                            '/<span class="starredname">/',
943c1010edaSGreg Roach                            '/<\/span><\/span>/',
944c1010edaSGreg Roach                            '/<\/span>/',
945c1010edaSGreg Roach                        ],
946c1010edaSGreg Roach                        [
947c1010edaSGreg Roach                            '«',
948c1010edaSGreg Roach                            '',
949c1010edaSGreg Roach                            '»',
950c1010edaSGreg Roach                        ],
951a6f13a4aSGreg Roach                        $addname
952a6f13a4aSGreg Roach                    );
953a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
954a6f13a4aSGreg Roach                    if (!empty($addname)) {
9557a6ee1acSGreg Roach                        $name .= ' ' . $addname;
956a6f13a4aSGreg Roach                    }
957a6f13a4aSGreg Roach                }
958a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
959a6f13a4aSGreg Roach            }
960a6f13a4aSGreg Roach        }
961a6f13a4aSGreg Roach    }
962a6f13a4aSGreg Roach
963a6f13a4aSGreg Roach    /**
96476692c8bSGreg Roach     * XML <GedcomValue/>
965a6f13a4aSGreg Roach     *
966a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
9678ba2e626SGreg Roach     *
9688ba2e626SGreg Roach     * @return void
969a6f13a4aSGreg Roach     */
970c1010edaSGreg Roach    private function gedcomValueStartHandler($attrs)
971c1010edaSGreg Roach    {
9727a6ee1acSGreg Roach        $id    = '';
97313abd6f3SGreg Roach        $match = [];
9747a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
975a6f13a4aSGreg Roach            $id = $match[1];
976a6f13a4aSGreg Roach        }
977a6f13a4aSGreg Roach
9787a6ee1acSGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] == '1') {
9797a6ee1acSGreg Roach            $useBreak = '1';
980a6f13a4aSGreg Roach        } else {
9817a6ee1acSGreg Roach            $useBreak = '0';
982a6f13a4aSGreg Roach        }
983a6f13a4aSGreg Roach
984a6f13a4aSGreg Roach        $tag = $attrs['tag'];
985a6f13a4aSGreg Roach        if (!empty($tag)) {
9867a6ee1acSGreg Roach            if ($tag == '@desc') {
987a6f13a4aSGreg Roach                $value = $this->desc;
988a6f13a4aSGreg Roach                $value = trim($value);
989a6f13a4aSGreg Roach                $this->current_element->addText($value);
990a6f13a4aSGreg Roach            }
9917a6ee1acSGreg Roach            if ($tag == '@id') {
992a6f13a4aSGreg Roach                $this->current_element->addText($id);
993a6f13a4aSGreg Roach            } else {
9947a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
995a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
9967a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
997a6f13a4aSGreg Roach                    $level = $temp[0];
998a6f13a4aSGreg Roach                    if ($level == 0) {
999a6f13a4aSGreg Roach                        $level++;
1000a6f13a4aSGreg Roach                    }
1001a6f13a4aSGreg Roach                } else {
1002a6f13a4aSGreg Roach                    $level = $attrs['level'];
1003a6f13a4aSGreg Roach                }
1004a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10053d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1006a6f13a4aSGreg Roach                switch (end($tags)) {
1007a6f13a4aSGreg Roach                    case 'DATE':
1008a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1009a6f13a4aSGreg Roach                        $value = $tmp->display();
1010a6f13a4aSGreg Roach                        break;
1011a6f13a4aSGreg Roach                    case 'PLAC':
1012299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1013a6f13a4aSGreg Roach                        $value = $tmp->getShortName();
1014a6f13a4aSGreg Roach                        break;
1015a6f13a4aSGreg Roach                }
10167a6ee1acSGreg Roach                if ($useBreak == '1') {
1017a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1018a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1019a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1020a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1021a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
1022a6f13a4aSGreg Roach                    if (substr($value, 0, 6) == '<br>') {
1023a6f13a4aSGreg Roach                        $value = substr($value, 6);
1024a6f13a4aSGreg Roach                    }
1025a6f13a4aSGreg Roach                }
1026d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1027c1010edaSGreg Roach                if (in_array(end($tmp), [
1028c1010edaSGreg Roach                    'NOTE',
1029c1010edaSGreg Roach                    'TEXT',
1030c1010edaSGreg Roach                ])) {
1031299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1032a4d703aeSGreg Roach                }
1033a6f13a4aSGreg Roach                $this->current_element->addText($value);
1034a6f13a4aSGreg Roach            }
1035a6f13a4aSGreg Roach        }
1036a6f13a4aSGreg Roach    }
1037a6f13a4aSGreg Roach
1038a6f13a4aSGreg Roach    /**
103976692c8bSGreg Roach     * XML <RepeatTag>
1040a6f13a4aSGreg Roach     *
1041a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
10428ba2e626SGreg Roach     *
10438ba2e626SGreg Roach     * @return void
1044a6f13a4aSGreg Roach     */
1045c1010edaSGreg Roach    private function repeatTagStartHandler($attrs)
1046c1010edaSGreg Roach    {
1047a6f13a4aSGreg Roach        $this->process_repeats++;
1048a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1049a6f13a4aSGreg Roach            return;
1050a6f13a4aSGreg Roach        }
1051a6f13a4aSGreg Roach
10529b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
105313abd6f3SGreg Roach        $this->repeats         = [];
1054e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1055a6f13a4aSGreg Roach
10567a6ee1acSGreg Roach        $tag = '';
1057a6f13a4aSGreg Roach        if (isset($attrs['tag'])) {
1058a6f13a4aSGreg Roach            $tag = $attrs['tag'];
1059a6f13a4aSGreg Roach        }
1060a6f13a4aSGreg Roach        if (!empty($tag)) {
10617a6ee1acSGreg Roach            if ($tag == '@desc') {
1062a6f13a4aSGreg Roach                $value = $this->desc;
1063a6f13a4aSGreg Roach                $value = trim($value);
1064a6f13a4aSGreg Roach                $this->current_element->addText($value);
1065a6f13a4aSGreg Roach            } else {
10667a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10677a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10687a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1069a6f13a4aSGreg Roach                $level = $temp[0];
1070a6f13a4aSGreg Roach                if ($level == 0) {
1071a6f13a4aSGreg Roach                    $level++;
1072a6f13a4aSGreg Roach                }
1073a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1074a6f13a4aSGreg Roach                $t      = $tag;
1075a6f13a4aSGreg Roach                $count  = count($tags);
1076a6f13a4aSGreg Roach                $i      = 0;
1077a6f13a4aSGreg Roach                while ($i < $count) {
1078a6f13a4aSGreg Roach                    $t = $tags[$i];
1079a6f13a4aSGreg Roach                    if (!empty($t)) {
1080a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10813d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1082a6f13a4aSGreg Roach                            if (empty($subrec)) {
1083a6f13a4aSGreg Roach                                $level--;
10843d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1085a6f13a4aSGreg Roach                                if (empty($subrec)) {
1086a6f13a4aSGreg Roach                                    return;
1087a6f13a4aSGreg Roach                                }
1088a6f13a4aSGreg Roach                            }
1089a6f13a4aSGreg Roach                        }
1090a6f13a4aSGreg Roach                        $level++;
1091a6f13a4aSGreg Roach                    }
1092a6f13a4aSGreg Roach                    $i++;
1093a6f13a4aSGreg Roach                }
1094a6f13a4aSGreg Roach                $level--;
1095a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1096a6f13a4aSGreg Roach                $i     = 0;
1097a6f13a4aSGreg Roach                while ($i < $count) {
1098a6f13a4aSGreg Roach                    $i++;
1099a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1100a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
1101a9007102SGreg Roach                    if (preg_match('/^\d ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1102299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1103a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1104a9007102SGreg Roach                            continue;
1105a9007102SGreg Roach                        }
1106a9007102SGreg Roach                    }
1107a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1108a6f13a4aSGreg Roach                }
1109a6f13a4aSGreg Roach            }
1110a6f13a4aSGreg Roach        }
1111a6f13a4aSGreg Roach    }
1112a6f13a4aSGreg Roach
1113a6f13a4aSGreg Roach    /**
111476692c8bSGreg Roach     * XML </ RepeatTag>
11158ba2e626SGreg Roach     *
11168ba2e626SGreg Roach     * @return void
1117a6f13a4aSGreg Roach     */
1118c1010edaSGreg Roach    private function repeatTagEndHandler()
1119c1010edaSGreg Roach    {
1120a6f13a4aSGreg Roach        $this->process_repeats--;
1121a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1122a6f13a4aSGreg Roach            return;
1123a6f13a4aSGreg Roach        }
1124a6f13a4aSGreg Roach
1125a6f13a4aSGreg Roach        // Check if there is anything to repeat
1126a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1127a6f13a4aSGreg Roach            // No need to load them if not used...
1128a6f13a4aSGreg Roach
1129a6f13a4aSGreg Roach            $lineoffset = 0;
1130a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1131a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1132a6f13a4aSGreg Roach            }
1133a6f13a4aSGreg Roach            //-- read the xml from the file
1134299d100dSGreg Roach            $lines = file($this->report);
11357a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1136a6f13a4aSGreg Roach                $lineoffset--;
1137a6f13a4aSGreg Roach            }
1138a6f13a4aSGreg Roach            $lineoffset++;
1139a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1140a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1141a6f13a4aSGreg Roach            // RepeatTag Level counter
1142a6f13a4aSGreg Roach            $count = 1;
1143a6f13a4aSGreg Roach            while (0 < $count) {
11447a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1145a6f13a4aSGreg Roach                    $count++;
11467a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1147a6f13a4aSGreg Roach                    $count--;
1148a6f13a4aSGreg Roach                }
1149a6f13a4aSGreg Roach                if (0 < $count) {
1150a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1151a6f13a4aSGreg Roach                }
1152a6f13a4aSGreg Roach                $line_nr++;
1153a6f13a4aSGreg Roach            }
1154a6f13a4aSGreg Roach            // No need to drag this
1155a6f13a4aSGreg Roach            unset($lines);
1156a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1157a6f13a4aSGreg Roach            // Save original values
11589b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1159a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1160a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1161a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1162a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1163e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1164a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
1165c1010edaSGreg Roach                xml_set_element_handler($repeat_parser, [
1166c1010edaSGreg Roach                    $this,
1167c1010edaSGreg Roach                    'startElement',
1168c1010edaSGreg Roach                ], [
1169c1010edaSGreg Roach                    $this,
1170c1010edaSGreg Roach                    'endElement',
1171c1010edaSGreg Roach                ]);
1172c1010edaSGreg Roach                xml_set_character_data_handler($repeat_parser, [
1173c1010edaSGreg Roach                    $this,
1174c1010edaSGreg Roach                    'characterData',
1175c1010edaSGreg Roach                ]);
1176a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1177a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1178a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1179a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1180a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1181a6f13a4aSGreg Roach                    ));
1182a6f13a4aSGreg Roach                }
1183a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1184a6f13a4aSGreg Roach            }
1185a6f13a4aSGreg Roach            // Restore original values
1186a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1187e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1188a6f13a4aSGreg Roach        }
1189a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1190a6f13a4aSGreg Roach    }
1191a6f13a4aSGreg Roach
1192a6f13a4aSGreg Roach    /**
1193a6f13a4aSGreg Roach     * Variable lookup
1194a6f13a4aSGreg Roach     * Retrieve predefined variables :
1195a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1196a6f13a4aSGreg Roach     *        1 EVEN This is a description
1197a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1198a6f13a4aSGreg Roach     * $ I18N::translate('....')
1199a6f13a4aSGreg Roach     * $ language_settings[]
1200a6f13a4aSGreg Roach     *
1201a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
12028ba2e626SGreg Roach     *
12038ba2e626SGreg Roach     * @return void
1204a6f13a4aSGreg Roach     */
1205c1010edaSGreg Roach    private function varStartHandler($attrs)
1206c1010edaSGreg Roach    {
1207a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
1208e8e7866bSGreg 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));
1209a6f13a4aSGreg Roach        }
1210a6f13a4aSGreg Roach
1211a6f13a4aSGreg Roach        $var = $attrs['var'];
1212a6f13a4aSGreg Roach        // SetVar element preset variables
1213d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1214d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1215a6f13a4aSGreg Roach        } else {
1216a6f13a4aSGreg Roach            $tfact = $this->fact;
12177a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1218a6f13a4aSGreg Roach                // Use :
1219a6f13a4aSGreg Roach                // n TYPE This text if string
1220a6f13a4aSGreg Roach                $tfact = $this->type;
1221a6f13a4aSGreg Roach            }
1222c1010edaSGreg Roach            $var = str_replace([
1223c1010edaSGreg Roach                '@fact',
1224c1010edaSGreg Roach                '@desc',
1225c1010edaSGreg Roach            ], [
1226c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1227c1010edaSGreg Roach                $this->desc,
1228c1010edaSGreg Roach            ], $var);
1229a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1230da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1231a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1232a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1233a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1234a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1235a6f13a4aSGreg Roach            }
1236a6f13a4aSGreg Roach        }
1237a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1238a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12397a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1240a6f13a4aSGreg Roach                $g   = new Date($var);
1241a6f13a4aSGreg Roach                $var = $g->display();
1242a6f13a4aSGreg Roach            }
1243a6f13a4aSGreg Roach        }
1244a6f13a4aSGreg Roach        $this->current_element->addText($var);
12452836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1246a6f13a4aSGreg Roach    }
1247a6f13a4aSGreg Roach
1248a6f13a4aSGreg Roach    /**
124976692c8bSGreg Roach     * XML <Facts>
125076692c8bSGreg Roach     *
1251a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
12528ba2e626SGreg Roach     *
12538ba2e626SGreg Roach     * @return void
1254a6f13a4aSGreg Roach     */
1255c1010edaSGreg Roach    private function factsStartHandler($attrs)
1256c1010edaSGreg Roach    {
1257a6f13a4aSGreg Roach        $this->process_repeats++;
1258a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1259a6f13a4aSGreg Roach            return;
1260a6f13a4aSGreg Roach        }
1261a6f13a4aSGreg Roach
12629b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
126313abd6f3SGreg Roach        $this->repeats         = [];
1264e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1265a6f13a4aSGreg Roach
12667a6ee1acSGreg Roach        $id    = '';
126713abd6f3SGreg Roach        $match = [];
12687a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1269a6f13a4aSGreg Roach            $id = $match[1];
1270a6f13a4aSGreg Roach        }
12717a6ee1acSGreg Roach        $tag = '';
1272a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1273a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1274a6f13a4aSGreg Roach        }
12757a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1276d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1277a6f13a4aSGreg Roach        }
1278a6f13a4aSGreg Roach
1279299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1280a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1281a6f13a4aSGreg Roach            $facts = $record->getFacts();
12823d7a8a4cSGreg Roach            Functions::sortFacts($facts);
128313abd6f3SGreg Roach            $this->repeats = [];
1284a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1285a6f13a4aSGreg Roach            foreach ($facts as $event) {
1286a6f13a4aSGreg Roach                if (!in_array($event->getTag(), $nonfacts)) {
1287a6f13a4aSGreg Roach                    $this->repeats[] = $event->getGedcom();
1288a6f13a4aSGreg Roach                }
1289a6f13a4aSGreg Roach            }
1290a6f13a4aSGreg Roach        } else {
1291a6f13a4aSGreg Roach            foreach ($record->getFacts() as $fact) {
1292a6f13a4aSGreg Roach                if ($fact->isPendingAddition() && $fact->getTag() !== 'CHAN') {
1293a6f13a4aSGreg Roach                    $this->repeats[] = $fact->getGedcom();
1294a6f13a4aSGreg Roach                }
1295a6f13a4aSGreg Roach            }
1296a6f13a4aSGreg Roach        }
1297a6f13a4aSGreg Roach    }
1298a6f13a4aSGreg Roach
1299a6f13a4aSGreg Roach    /**
130076692c8bSGreg Roach     * XML </Facts>
13018ba2e626SGreg Roach     *
13028ba2e626SGreg Roach     * @return void
1303a6f13a4aSGreg Roach     */
1304c1010edaSGreg Roach    private function factsEndHandler()
1305c1010edaSGreg Roach    {
1306a6f13a4aSGreg Roach        $this->process_repeats--;
1307a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1308a6f13a4aSGreg Roach            return;
1309a6f13a4aSGreg Roach        }
1310a6f13a4aSGreg Roach
1311a6f13a4aSGreg Roach        // Check if there is anything to repeat
1312a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1313e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1314a6f13a4aSGreg Roach            $lineoffset = 0;
1315a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1316a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1317a6f13a4aSGreg Roach            }
1318a6f13a4aSGreg Roach
1319a6f13a4aSGreg Roach            //-- read the xml from the file
1320299d100dSGreg Roach            $lines = file($this->report);
1321a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1322a6f13a4aSGreg Roach                $lineoffset--;
1323a6f13a4aSGreg Roach            }
1324a6f13a4aSGreg Roach            $lineoffset++;
1325a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1326a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1327a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1328a6f13a4aSGreg Roach            while ($line_nr < $i) {
1329a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1330a6f13a4aSGreg Roach                $line_nr++;
1331a6f13a4aSGreg Roach            }
1332a6f13a4aSGreg Roach            // No need to drag this
1333a6f13a4aSGreg Roach            unset($lines);
1334a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1335a6f13a4aSGreg Roach            // Save original values
13369b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1337a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1338a6f13a4aSGreg Roach            $count                = count($this->repeats);
1339a6f13a4aSGreg Roach            $i                    = 0;
1340a6f13a4aSGreg Roach            while ($i < $count) {
1341a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1342a6f13a4aSGreg Roach                $this->fact   = '';
1343a6f13a4aSGreg Roach                $this->desc   = '';
1344a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1345a6f13a4aSGreg Roach                    $this->fact = $match[1];
1346a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
134713abd6f3SGreg Roach                        $tmatch = [];
1348a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1349a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1350a6f13a4aSGreg Roach                        } else {
1351a6f13a4aSGreg Roach                            $this->type = ' ';
1352a6f13a4aSGreg Roach                        }
1353a6f13a4aSGreg Roach                    }
1354a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13553d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1356a6f13a4aSGreg Roach                }
1357a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1358e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1359a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
1360c1010edaSGreg Roach                xml_set_element_handler($repeat_parser, [
1361c1010edaSGreg Roach                    $this,
1362c1010edaSGreg Roach                    'startElement',
1363c1010edaSGreg Roach                ], [
1364c1010edaSGreg Roach                    $this,
1365c1010edaSGreg Roach                    'endElement',
1366c1010edaSGreg Roach                ]);
1367c1010edaSGreg Roach                xml_set_character_data_handler($repeat_parser, [
1368c1010edaSGreg Roach                    $this,
1369c1010edaSGreg Roach                    'characterData',
1370c1010edaSGreg Roach                ]);
1371a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1372a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1373a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1374a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1375a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1376a6f13a4aSGreg Roach                    ));
1377a6f13a4aSGreg Roach                }
1378a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1379a6f13a4aSGreg Roach                $i++;
1380a6f13a4aSGreg Roach            }
1381a6f13a4aSGreg Roach            // Restore original values
1382e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1383a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1384a6f13a4aSGreg Roach        }
1385a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1386a6f13a4aSGreg Roach    }
1387a6f13a4aSGreg Roach
1388a6f13a4aSGreg Roach    /**
1389a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1390d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1391a6f13a4aSGreg Roach     *
1392a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
13938ba2e626SGreg Roach     *
13948ba2e626SGreg Roach     * @return void
1395a6f13a4aSGreg Roach     */
1396c1010edaSGreg Roach    private function setVarStartHandler($attrs)
1397c1010edaSGreg Roach    {
1398a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
1399a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1400a6f13a4aSGreg Roach        }
1401a6f13a4aSGreg Roach
1402a6f13a4aSGreg Roach        $name  = $attrs['name'];
1403a6f13a4aSGreg Roach        $value = $attrs['value'];
140413abd6f3SGreg Roach        $match = [];
1405a6f13a4aSGreg Roach        // Current GEDCOM record strings
14067a6ee1acSGreg Roach        if ($value == '@ID') {
14077a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1408a6f13a4aSGreg Roach                $value = $match[1];
1409a6f13a4aSGreg Roach            }
14107a6ee1acSGreg Roach        } elseif ($value == '@fact') {
1411a6f13a4aSGreg Roach            $value = $this->fact;
14127a6ee1acSGreg Roach        } elseif ($value == '@desc') {
1413a6f13a4aSGreg Roach            $value = $this->desc;
14147a6ee1acSGreg Roach        } elseif ($value == '@generation') {
1415a6f13a4aSGreg Roach            $value = $this->generation;
1416a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
141713abd6f3SGreg Roach            $gmatch = [];
1418a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14197a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1420a6f13a4aSGreg Roach            }
1421a6f13a4aSGreg Roach        }
1422a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1423d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1424a6f13a4aSGreg Roach        }
1425a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1426a6f13a4aSGreg Roach        $i     = 0;
1427a6f13a4aSGreg Roach        while ($i < $count) {
1428d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14297a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1430a6f13a4aSGreg Roach            $i++;
1431a6f13a4aSGreg Roach        }
1432a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1433da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1434a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1435a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1436a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1437a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1438a6f13a4aSGreg Roach        }
1439a6f13a4aSGreg Roach        // Arithmetic functions
1440a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
1441a6f13a4aSGreg Roach            switch ($match[2]) {
14427a6ee1acSGreg Roach                case '+':
1443a6f13a4aSGreg Roach                    $t     = $match[1] + $match[3];
14447a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1445a6f13a4aSGreg Roach                    break;
14467a6ee1acSGreg Roach                case '-':
1447a6f13a4aSGreg Roach                    $t     = $match[1] - $match[3];
14487a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1449a6f13a4aSGreg Roach                    break;
14507a6ee1acSGreg Roach                case '*':
1451a6f13a4aSGreg Roach                    $t     = $match[1] * $match[3];
14527a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1453a6f13a4aSGreg Roach                    break;
14547a6ee1acSGreg Roach                case '/':
1455a6f13a4aSGreg Roach                    $t     = $match[1] / $match[3];
14567a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1457a6f13a4aSGreg Roach                    break;
1458a6f13a4aSGreg Roach            }
1459a6f13a4aSGreg Roach        }
14607a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14617a6ee1acSGreg Roach            $value = '';
1462a6f13a4aSGreg Roach        }
1463d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1464a6f13a4aSGreg Roach    }
1465a6f13a4aSGreg Roach
1466a6f13a4aSGreg Roach    /**
1467a6f13a4aSGreg Roach     * XML <if > start element
1468a6f13a4aSGreg Roach     *
1469a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
14708ba2e626SGreg Roach     *
14718ba2e626SGreg Roach     * @return void
1472a6f13a4aSGreg Roach     */
1473c1010edaSGreg Roach    private function ifStartHandler($attrs)
1474c1010edaSGreg Roach    {
1475a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1476a6f13a4aSGreg Roach            $this->process_ifs++;
1477a6f13a4aSGreg Roach
1478a6f13a4aSGreg Roach            return;
1479a6f13a4aSGreg Roach        }
1480a6f13a4aSGreg Roach
1481a6f13a4aSGreg Roach        $condition = $attrs['condition'];
148282759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1483c1010edaSGreg Roach        $condition = str_replace([
1484c1010edaSGreg Roach            ' LT ',
1485c1010edaSGreg Roach            ' GT ',
1486c1010edaSGreg Roach        ], [
1487c1010edaSGreg Roach            '<',
1488c1010edaSGreg Roach            '>',
1489c1010edaSGreg Roach        ], $condition);
1490a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
14917a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
149213abd6f3SGreg Roach        $match     = [];
1493a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1494a6f13a4aSGreg Roach        $i         = 0;
1495a6f13a4aSGreg Roach        while ($i < $count) {
1496a6f13a4aSGreg Roach            $id    = $match[$i][1];
1497a6f13a4aSGreg Roach            $value = '""';
14987a6ee1acSGreg Roach            if ($id == 'ID') {
14997a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1500a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1501a6f13a4aSGreg Roach                }
15027a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1503a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15047a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1505a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15067a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1507a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1508a6f13a4aSGreg Roach            } else {
15097a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1510a6f13a4aSGreg Roach                $level = $temp[0];
1511a6f13a4aSGreg Roach                if ($level == 0) {
1512a6f13a4aSGreg Roach                    $level++;
1513a6f13a4aSGreg Roach                }
15143d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1515a6f13a4aSGreg Roach                if (empty($value)) {
1516a6f13a4aSGreg Roach                    $level++;
15173d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1518a6f13a4aSGreg Roach                }
15195e8c88c1SGreg Roach                $value = preg_replace('/^@(' . WT_REGEX_XREF . ')@$/', '$1', $value);
15205e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1521a6f13a4aSGreg Roach            }
1522a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1523a6f13a4aSGreg Roach            $i++;
1524a6f13a4aSGreg Roach        }
15255809450fSGreg Roach
1526cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1527cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1528cb63a60eSGreg Roach        $expression_language  = new ExpressionLanguage(null, [$expression_provider]);
1529cb63a60eSGreg Roach
1530cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15315809450fSGreg Roach
1532a6f13a4aSGreg Roach        if (!$ret) {
1533a6f13a4aSGreg Roach            $this->process_ifs++;
1534a6f13a4aSGreg Roach        }
1535a6f13a4aSGreg Roach    }
1536a6f13a4aSGreg Roach
1537a6f13a4aSGreg Roach    /**
1538a6f13a4aSGreg Roach     * XML <if /> end element
15398ba2e626SGreg Roach     *
15408ba2e626SGreg Roach     * @return void
1541a6f13a4aSGreg Roach     */
1542c1010edaSGreg Roach    private function ifEndHandler()
1543c1010edaSGreg Roach    {
1544a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1545a6f13a4aSGreg Roach            $this->process_ifs--;
1546a6f13a4aSGreg Roach        }
1547a6f13a4aSGreg Roach    }
1548a6f13a4aSGreg Roach
1549a6f13a4aSGreg Roach    /**
1550a6f13a4aSGreg Roach     * XML <Footnote > start element
1551a6f13a4aSGreg Roach     * Collect the Footnote links
1552a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1553a6f13a4aSGreg Roach     *
1554a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
15558ba2e626SGreg Roach     *
15568ba2e626SGreg Roach     * @return void
1557a6f13a4aSGreg Roach     */
1558c1010edaSGreg Roach    private function footnoteStartHandler($attrs)
1559c1010edaSGreg Roach    {
15607a6ee1acSGreg Roach        $id = '';
15617a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1562a6f13a4aSGreg Roach            $id = $match[2];
1563a6f13a4aSGreg Roach        }
1564299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1565a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15669b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1567a6f13a4aSGreg Roach            $this->print_data         = true;
15687a6ee1acSGreg Roach            $style                    = '';
1569a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1570a6f13a4aSGreg Roach                $style = $attrs['style'];
1571a6f13a4aSGreg Roach            }
1572a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1573e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1574a6f13a4aSGreg Roach        } else {
1575a6f13a4aSGreg Roach            $this->print_data       = false;
1576a6f13a4aSGreg Roach            $this->process_footnote = false;
1577a6f13a4aSGreg Roach        }
1578a6f13a4aSGreg Roach    }
1579a6f13a4aSGreg Roach
1580a6f13a4aSGreg Roach    /**
1581a6f13a4aSGreg Roach     * XML <Footnote /> end element
1582a6f13a4aSGreg Roach     * Print the collected Footnote data
15838ba2e626SGreg Roach     *
15848ba2e626SGreg Roach     * @return void
1585a6f13a4aSGreg Roach     */
1586c1010edaSGreg Roach    private function footnoteEndHandler()
1587c1010edaSGreg Roach    {
1588a6f13a4aSGreg Roach        if ($this->process_footnote) {
1589a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1590a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1591a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1592e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1593a6f13a4aSGreg Roach            }
1594a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1595a6f13a4aSGreg Roach        } else {
1596a6f13a4aSGreg Roach            $this->process_footnote = true;
1597a6f13a4aSGreg Roach        }
1598a6f13a4aSGreg Roach    }
1599a6f13a4aSGreg Roach
1600a6f13a4aSGreg Roach    /**
1601a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16028ba2e626SGreg Roach     *
16038ba2e626SGreg Roach     * @return void
1604a6f13a4aSGreg Roach     */
1605c1010edaSGreg Roach    private function footnoteTextsStartHandler()
1606c1010edaSGreg Roach    {
16077a6ee1acSGreg Roach        $temp = 'footnotetexts';
1608e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1609a6f13a4aSGreg Roach    }
1610a6f13a4aSGreg Roach
1611a6f13a4aSGreg Roach    /**
1612a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16138ba2e626SGreg Roach     *
16148ba2e626SGreg Roach     * @return void
1615a6f13a4aSGreg Roach     */
1616c1010edaSGreg Roach    private function brStartHandler()
1617c1010edaSGreg Roach    {
1618a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1619a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1620a6f13a4aSGreg Roach        }
1621a6f13a4aSGreg Roach    }
1622a6f13a4aSGreg Roach
1623a6f13a4aSGreg Roach    /**
1624a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16258ba2e626SGreg Roach     *
16268ba2e626SGreg Roach     * @return void
1627a6f13a4aSGreg Roach     */
1628c1010edaSGreg Roach    private function spStartHandler()
1629c1010edaSGreg Roach    {
1630a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1631a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1632a6f13a4aSGreg Roach        }
1633a6f13a4aSGreg Roach    }
1634a6f13a4aSGreg Roach
1635a6f13a4aSGreg Roach    /**
163676692c8bSGreg Roach     * XML <HighlightedImage/>
163776692c8bSGreg Roach     *
1638a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
16398ba2e626SGreg Roach     *
16408ba2e626SGreg Roach     * @return void
1641a6f13a4aSGreg Roach     */
1642c1010edaSGreg Roach    private function highlightedImageStartHandler($attrs)
1643c1010edaSGreg Roach    {
1644a6f13a4aSGreg Roach        $id = '';
16457a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1646a6f13a4aSGreg Roach            $id = $match[1];
1647a6f13a4aSGreg Roach        }
1648a6f13a4aSGreg Roach
164983cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
165083cdc021SGreg Roach        $top = (int) ($attrs['top'] ?? -1);
1651a6f13a4aSGreg Roach
1652a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
165383cdc021SGreg Roach        $left = (int) ($attrs['left'] ?? -1);
1654a6f13a4aSGreg Roach
165583cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
165683cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1657a6f13a4aSGreg Roach
1658a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
165983cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1660a6f13a4aSGreg Roach
166183cdc021SGreg Roach        // Width, height (or both).
166283cdc021SGreg Roach        $width  = (int) ($attrs['width'] ?? 0);
166383cdc021SGreg Roach        $height = (int) ($attrs['height'] ?? 0);
1664a6f13a4aSGreg Roach
1665299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16664a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
166786a63f51SGreg Roach
166886a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1669c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1670c1010edaSGreg Roach                0,
1671c1010edaSGreg Roach                0,
1672c1010edaSGreg Roach            ];
1673a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16743c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16753c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1676a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16773c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16783c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1679a6f13a4aSGreg Roach            } else {
16803c3b90deSGreg Roach                $width  = $attributes[0];
16813c3b90deSGreg Roach                $height = $attributes[1];
1682a6f13a4aSGreg Roach            }
16834a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1684e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1685a6f13a4aSGreg Roach        }
1686a6f13a4aSGreg Roach    }
1687a6f13a4aSGreg Roach
1688a6f13a4aSGreg Roach    /**
168976692c8bSGreg Roach     * XML <Image/>
169076692c8bSGreg Roach     *
1691a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
16928ba2e626SGreg Roach     *
16938ba2e626SGreg Roach     * @return void
1694a6f13a4aSGreg Roach     */
1695c1010edaSGreg Roach    private function imageStartHandler($attrs)
1696c1010edaSGreg Roach    {
169783cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
169883cdc021SGreg Roach        $top = (int) ($attrs['top'] ?? -1);
1699a6f13a4aSGreg Roach
1700a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
170183cdc021SGreg Roach        $left = (int) ($attrs['left'] ?? -1);
1702a6f13a4aSGreg Roach
170383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
170483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1705a6f13a4aSGreg Roach
1706a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
170783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1708a6f13a4aSGreg Roach
170983cdc021SGreg Roach        // Width, height (or both).
171083cdc021SGreg Roach        $width  = (int) ($attrs['width'] ?? 0);
171183cdc021SGreg Roach        $height = (int) ($attrs['height'] ?? 0);
1712a6f13a4aSGreg Roach
171383cdc021SGreg Roach        $file = $attrs['file'] ?? '';
171483cdc021SGreg Roach
17157a6ee1acSGreg Roach        if ($file == '@FILE') {
171613abd6f3SGreg Roach            $match = [];
1717a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1718299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17194a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1720cdf416fbSGreg Roach
17214a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1722c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1723c1010edaSGreg Roach                        0,
1724c1010edaSGreg Roach                        0,
1725c1010edaSGreg Roach                    ];
1726a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17273c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17283c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1729a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17303c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17313c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1732a6f13a4aSGreg Roach                    } else {
17333c3b90deSGreg Roach                        $width  = $attributes[0];
17343c3b90deSGreg Roach                        $height = $attributes[1];
1735a6f13a4aSGreg Roach                    }
17364a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1737e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1738a6f13a4aSGreg Roach                }
1739a6f13a4aSGreg Roach            }
1740a6f13a4aSGreg Roach        } else {
17417a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1742a6f13a4aSGreg Roach                $size = getimagesize($file);
1743a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1744a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1745a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1746a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1747a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1748a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1749a6f13a4aSGreg Roach                } else {
1750a6f13a4aSGreg Roach                    $width  = $size[0];
1751a6f13a4aSGreg Roach                    $height = $size[1];
1752a6f13a4aSGreg Roach                }
1753e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1754e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1755a6f13a4aSGreg Roach            }
1756a6f13a4aSGreg Roach        }
1757a6f13a4aSGreg Roach    }
1758a6f13a4aSGreg Roach
1759a6f13a4aSGreg Roach    /**
1760a6f13a4aSGreg Roach     * XML <Line> element handler
1761a6f13a4aSGreg Roach     *
1762a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
17638ba2e626SGreg Roach     *
17648ba2e626SGreg Roach     * @return void
1765a6f13a4aSGreg Roach     */
1766c1010edaSGreg Roach    private function lineStartHandler($attrs)
1767c1010edaSGreg Roach    {
1768a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
17697a6ee1acSGreg Roach        $x1 = '.';
1770a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17717a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1772a6f13a4aSGreg Roach                $x1 = 0;
17737a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
17747a6ee1acSGreg Roach                $x1 = '.';
1775a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1776a6f13a4aSGreg Roach                $x1 = (int) $attrs['x1'];
1777a6f13a4aSGreg Roach            }
1778a6f13a4aSGreg Roach        }
1779a6f13a4aSGreg Roach        // Start vertical position, current position (default)
17807a6ee1acSGreg Roach        $y1 = '.';
1781a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17827a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1783a6f13a4aSGreg Roach                $y1 = 0;
17847a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
17857a6ee1acSGreg Roach                $y1 = '.';
1786a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1787a6f13a4aSGreg Roach                $y1 = (int) $attrs['y1'];
1788a6f13a4aSGreg Roach            }
1789a6f13a4aSGreg Roach        }
1790a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
17917a6ee1acSGreg Roach        $x2 = '.';
1792a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
17937a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1794a6f13a4aSGreg Roach                $x2 = 0;
17957a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
17967a6ee1acSGreg Roach                $x2 = '.';
1797a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1798a6f13a4aSGreg Roach                $x2 = (int) $attrs['x2'];
1799a6f13a4aSGreg Roach            }
1800a6f13a4aSGreg Roach        }
1801a6f13a4aSGreg Roach        // End vertical position
18027a6ee1acSGreg Roach        $y2 = '.';
1803a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18047a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1805a6f13a4aSGreg Roach                $y2 = 0;
18067a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
18077a6ee1acSGreg Roach                $y2 = '.';
1808a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1809a6f13a4aSGreg Roach                $y2 = (int) $attrs['y2'];
1810a6f13a4aSGreg Roach            }
1811a6f13a4aSGreg Roach        }
1812a6f13a4aSGreg Roach
1813e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1814e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1815a6f13a4aSGreg Roach    }
1816a6f13a4aSGreg Roach
1817a6f13a4aSGreg Roach    /**
181876692c8bSGreg Roach     * XML <List>
1819a6f13a4aSGreg Roach     *
1820a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
18218ba2e626SGreg Roach     *
18228ba2e626SGreg Roach     * @return void
1823a6f13a4aSGreg Roach     */
1824c1010edaSGreg Roach    private function listStartHandler($attrs)
1825c1010edaSGreg Roach    {
1826a6f13a4aSGreg Roach        $this->process_repeats++;
1827a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1828a6f13a4aSGreg Roach            return;
1829a6f13a4aSGreg Roach        }
1830a6f13a4aSGreg Roach
183113abd6f3SGreg Roach        $match = [];
1832a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1833a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1834a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1835d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1836a6f13a4aSGreg Roach                $sortby = trim($sortby);
1837a6f13a4aSGreg Roach            }
1838a6f13a4aSGreg Roach        } else {
18397a6ee1acSGreg Roach            $sortby = 'NAME';
1840a6f13a4aSGreg Roach        }
1841a6f13a4aSGreg Roach
1842a6f13a4aSGreg Roach        if (isset($attrs['list'])) {
1843a6f13a4aSGreg Roach            $listname = $attrs['list'];
1844a6f13a4aSGreg Roach        } else {
18457a6ee1acSGreg Roach            $listname = 'individual';
1846a6f13a4aSGreg Roach        }
1847a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1848a6f13a4aSGreg Roach        switch ($listname) {
18497a6ee1acSGreg Roach            case 'pending':
1850a6f13a4aSGreg Roach                $rows       = Database::prepare(
18515d0bc43dSGreg Roach                    "SELECT xref, CASE new_gedcom WHEN '' THEN old_gedcom ELSE new_gedcom END AS gedcom" .
18525d0bc43dSGreg Roach                    " FROM `##change`" . " WHERE (xref, change_id) IN (" .
18535d0bc43dSGreg Roach                    "  SELECT xref, MAX(change_id)" .
18545d0bc43dSGreg Roach                    "  FROM `##change`" .
18555d0bc43dSGreg Roach                    "  WHERE status = 'pending' AND gedcom_id = :tree_id" .
18565d0bc43dSGreg Roach                    "  GROUP BY xref" .
18575d0bc43dSGreg Roach                    " )"
185813abd6f3SGreg Roach                )->execute([
1859299d100dSGreg Roach                    'tree_id' => $this->tree->getTreeId(),
186013abd6f3SGreg Roach                ])->fetchAll();
186113abd6f3SGreg Roach                $this->list = [];
1862a6f13a4aSGreg Roach                foreach ($rows as $row) {
1863299d100dSGreg Roach                    $this->list[] = GedcomRecord::getInstance($row->xref, $this->tree, $row->gedcom);
1864a6f13a4aSGreg Roach                }
1865a6f13a4aSGreg Roach                break;
1866a6f13a4aSGreg Roach            case 'individual':
186776156db1SGreg Roach                $sql_select   = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` ";
1868a6f13a4aSGreg Roach                $sql_join     = "";
1869825006d2SGreg Roach                $sql_where    = " WHERE i_file = :tree_id";
1870a6f13a4aSGreg Roach                $sql_order_by = "";
1871299d100dSGreg Roach                $sql_params   = ['tree_id' => $this->tree->getTreeId()];
1872a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1873a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
187482759250SGreg Roach                        $value = $this->substituteVars($value, false);
1875a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1876a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1877a6f13a4aSGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)";
1878b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
18795d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1880a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
18817a6ee1acSGreg Roach                            if ($match[2] == 'LTE') {
18825d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
18835d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1884a6f13a4aSGreg Roach                            } else {
18855d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
18865d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1887a6f13a4aSGreg Roach                            }
1888a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
1889a6f13a4aSGreg Roach                                $sortby       = "";
1890a6f13a4aSGreg Roach                                $sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1";
1891a6f13a4aSGreg Roach                            }
1892a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1893a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
1894a6f13a4aSGreg Roach                            // Do nothing, unless you have to
1895a6f13a4aSGreg Roach                            if ($match[1] != '' || $sortby == 'NAME') {
1896a6f13a4aSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)";
1897a6f13a4aSGreg Roach                                // Search the DB only if there is any name supplied
18987a6ee1acSGreg Roach                                if ($match[1] != '') {
18997a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19005d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19015d0bc43dSGreg Roach                                        $sql_where                       .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19025d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
1903a6f13a4aSGreg Roach                                    }
1904a6f13a4aSGreg Roach                                }
1905a6f13a4aSGreg Roach                                // Let the DB do the name sorting even when no name was entered
19067a6ee1acSGreg Roach                                if ($sortby == 'NAME') {
19077a6ee1acSGreg Roach                                    $sortby       = '';
19087a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1909a6f13a4aSGreg Roach                                }
1910a6f13a4aSGreg Roach                            }
1911a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1912a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19135d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom";
1914b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1915b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1916a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1917a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1918a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)";
1919a6f13a4aSGreg 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)";
19205d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19215d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1922a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
1923a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19245d0bc43dSGreg Roach                            $sql_where                       .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19255d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
19265d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
19275d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
1928a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
1929a6f13a4aSGreg Roach                        }
1930a6f13a4aSGreg Roach                    }
1931a6f13a4aSGreg Roach                }
1932a6f13a4aSGreg Roach
193313abd6f3SGreg Roach                $this->list = [];
1934a6f13a4aSGreg Roach                $rows       = Database::prepare(
1935a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
19365d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
1937a6f13a4aSGreg Roach
1938a6f13a4aSGreg Roach                foreach ($rows as $row) {
1939299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1940a6f13a4aSGreg Roach                }
1941a6f13a4aSGreg Roach                break;
1942a6f13a4aSGreg Roach
1943a6f13a4aSGreg Roach            case 'family':
194476156db1SGreg Roach                $sql_select   = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`";
1945a6f13a4aSGreg Roach                $sql_join     = "";
1946825006d2SGreg Roach                $sql_where    = " WHERE f_file = :tree_id";
1947a6f13a4aSGreg Roach                $sql_order_by = "";
1948299d100dSGreg Roach                $sql_params   = ['tree_id' => $this->tree->getTreeId()];
1949a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1950a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
195182759250SGreg Roach                        $value = $this->substituteVars($value, false);
1952a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1953a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1954a9eb55f8SGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)";
1955b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
19565d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1957a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
19587a6ee1acSGreg Roach                            if ($match[2] == 'LTE') {
19595d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19605d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1961a6f13a4aSGreg Roach                            } else {
19625d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19635d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1964a6f13a4aSGreg Roach                            }
1965a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
19667a6ee1acSGreg Roach                                $sortby       = '';
19677a6ee1acSGreg Roach                                $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1";
1968a6f13a4aSGreg Roach                            }
1969a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1970a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19715d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom";
1972b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1973b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1974a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1975a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19765d0bc43dSGreg Roach                            // Do nothing, unless you have to
19775d0bc43dSGreg Roach                            if ($match[1] != '' || $sortby == 'NAME') {
19785d0bc43dSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)";
19795d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
19807a6ee1acSGreg Roach                                if ($match[1] != '') {
19817a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19825d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19835d0bc43dSGreg Roach                                        $sql_where                       .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19845d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
19855d0bc43dSGreg Roach                                    }
19865d0bc43dSGreg Roach                                }
19875d0bc43dSGreg Roach                                // Let the DB do the name sorting even when no name was entered
19887a6ee1acSGreg Roach                                if ($sortby == 'NAME') {
19897a6ee1acSGreg Roach                                    $sortby       = '';
19907a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1991a6f13a4aSGreg Roach                                }
19925d0bc43dSGreg Roach                            }
1993a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1994a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1995a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)";
1996a6f13a4aSGreg 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)";
19975d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19985d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1999a6f13a4aSGreg Roach                        // Don't unset this filter. This is just initial filtering
2000a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
20015d0bc43dSGreg Roach                            $sql_where                       .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
20025d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
20035d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
20045d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
2005a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
2006a6f13a4aSGreg Roach                        }
2007a6f13a4aSGreg Roach                    }
2008a6f13a4aSGreg Roach                }
2009a6f13a4aSGreg Roach
201013abd6f3SGreg Roach                $this->list = [];
2011a6f13a4aSGreg Roach                $rows       = Database::prepare(
2012a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
20135d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
2014a6f13a4aSGreg Roach
2015a6f13a4aSGreg Roach                foreach ($rows as $row) {
2016299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2017a6f13a4aSGreg Roach                }
2018a6f13a4aSGreg Roach                break;
2019a6f13a4aSGreg Roach
2020a6f13a4aSGreg Roach            default:
2021a6f13a4aSGreg Roach                throw new \DomainException('Invalid list name: ' . $listname);
2022a6f13a4aSGreg Roach        }
2023a6f13a4aSGreg Roach
202413abd6f3SGreg Roach        $filters  = [];
202513abd6f3SGreg Roach        $filters2 = [];
2026a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2027a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2028a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2029a6f13a4aSGreg Roach                    $condition = $value;
2030a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2031a6f13a4aSGreg Roach                        $id    = $match[1];
2032a6f13a4aSGreg Roach                        $value = "''";
20337a6ee1acSGreg Roach                        if ($id == 'ID') {
20347a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2035a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2036a6f13a4aSGreg Roach                            }
20377a6ee1acSGreg Roach                        } elseif ($id == 'fact') {
2038a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
20397a6ee1acSGreg Roach                        } elseif ($id == 'desc') {
2040a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2041a6f13a4aSGreg Roach                        } else {
2042a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20437a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2044a6f13a4aSGreg Roach                            }
2045a6f13a4aSGreg Roach                        }
2046a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2047a6f13a4aSGreg Roach                    }
2048a6f13a4aSGreg Roach                    //-- handle regular expressions
2049a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2050a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2051a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2052a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2053a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2054d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2055a6f13a4aSGreg Roach                            $val = trim($val);
2056a6f13a4aSGreg Roach                        }
2057a6f13a4aSGreg Roach                        if ($val) {
20587a6ee1acSGreg Roach                            $searchstr = '';
20597a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2060a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2061a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2062a6f13a4aSGreg Roach                                $level = 1;
2063a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2064a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2065a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2066a6f13a4aSGreg Roach                                    }
2067a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
20687a6ee1acSGreg Roach                                    if ($t == 'EMAIL' || $t == '_EMAIL') {
20697a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2070a6f13a4aSGreg Roach                                    }
20717a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2072a6f13a4aSGreg Roach                                    $level++;
2073a6f13a4aSGreg Roach                                }
2074a6f13a4aSGreg Roach                            } else {
20757a6ee1acSGreg Roach                                if ($tag == 'EMAIL' || $tag == '_EMAIL') {
20767a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2077a6f13a4aSGreg Roach                                }
2078a6f13a4aSGreg Roach                                $t         = $tag;
20797a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2080a6f13a4aSGreg Roach                            }
2081a6f13a4aSGreg Roach                            switch ($expr) {
20827a6ee1acSGreg Roach                                case 'CONTAINS':
20837a6ee1acSGreg Roach                                    if ($t == 'PLAC') {
2084a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2085a6f13a4aSGreg Roach                                    } else {
2086a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2087a6f13a4aSGreg Roach                                    }
2088a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2089a6f13a4aSGreg Roach                                    break;
2090a6f13a4aSGreg Roach                                default:
2091c1010edaSGreg Roach                                    $filters2[] = [
2092c1010edaSGreg Roach                                        'tag'  => $tag,
2093c1010edaSGreg Roach                                        'expr' => $expr,
2094c1010edaSGreg Roach                                        'val'  => $val,
2095c1010edaSGreg Roach                                    ];
2096a6f13a4aSGreg Roach                                    break;
2097a6f13a4aSGreg Roach                            }
2098a6f13a4aSGreg Roach                        }
2099a6f13a4aSGreg Roach                    }
2100a6f13a4aSGreg Roach                }
2101a6f13a4aSGreg Roach            }
2102a6f13a4aSGreg Roach        }
2103a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2104a6f13a4aSGreg Roach        if ($filters) {
2105a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2106a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2107299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2108a6f13a4aSGreg Roach                        unset($this->list[$key]);
2109a6f13a4aSGreg Roach                        break;
2110a6f13a4aSGreg Roach                    }
2111a6f13a4aSGreg Roach                }
2112a6f13a4aSGreg Roach            }
2113a6f13a4aSGreg Roach        }
2114a6f13a4aSGreg Roach        if ($filters2) {
211513abd6f3SGreg Roach            $mylist = [];
2116a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2117a6f13a4aSGreg Roach                $key  = $indi->getXref();
2118299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2119a6f13a4aSGreg Roach                $keep = true;
2120a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2121a6f13a4aSGreg Roach                    if ($keep) {
2122a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2123a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2124a6f13a4aSGreg Roach                        $val  = $filter['val'];
2125a6f13a4aSGreg Roach                        if ($val == "''") {
21267a6ee1acSGreg Roach                            $val = '';
2127a6f13a4aSGreg Roach                        }
21287a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2129a6f13a4aSGreg Roach                        $t    = end($tags);
21303d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2131a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
21327a6ee1acSGreg Roach                        if ($t == 'EMAIL' && empty($v)) {
21337a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21347a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2135a6f13a4aSGreg Roach                            $t    = end($tags);
21363d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2137a6f13a4aSGreg Roach                        }
2138a6f13a4aSGreg Roach
2139a6f13a4aSGreg Roach                        switch ($expr) {
21407a6ee1acSGreg Roach                            case 'GTE':
21417a6ee1acSGreg Roach                                if ($t == 'DATE') {
2142a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2143a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2144a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2145a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2146a6f13a4aSGreg Roach                                    $keep = true;
2147a6f13a4aSGreg Roach                                }
2148a6f13a4aSGreg Roach                                break;
21497a6ee1acSGreg Roach                            case 'LTE':
21507a6ee1acSGreg Roach                                if ($t == 'DATE') {
2151a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2152a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2153a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2154a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2155a6f13a4aSGreg Roach                                    $keep = true;
2156a6f13a4aSGreg Roach                                }
2157a6f13a4aSGreg Roach                                break;
2158a6f13a4aSGreg Roach                            default:
2159a6f13a4aSGreg Roach                                if ($v == $val) {
2160a6f13a4aSGreg Roach                                    $keep = true;
2161a6f13a4aSGreg Roach                                } else {
2162a6f13a4aSGreg Roach                                    $keep = false;
2163a6f13a4aSGreg Roach                                }
2164a6f13a4aSGreg Roach                                break;
2165a6f13a4aSGreg Roach                        }
2166a6f13a4aSGreg Roach                    }
2167a6f13a4aSGreg Roach                }
2168a6f13a4aSGreg Roach                if ($keep) {
2169a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2170a6f13a4aSGreg Roach                }
2171a6f13a4aSGreg Roach            }
2172a6f13a4aSGreg Roach            $this->list = $mylist;
2173a6f13a4aSGreg Roach        }
2174a6f13a4aSGreg Roach
2175a6f13a4aSGreg Roach        switch ($sortby) {
2176a6f13a4aSGreg Roach            case 'NAME':
2177a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2178a6f13a4aSGreg Roach                break;
2179a6f13a4aSGreg Roach            case 'CHAN':
2180a6f13a4aSGreg Roach                uasort($this->list, function (GedcomRecord $x, GedcomRecord $y) {
2181a6f13a4aSGreg Roach                    return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2182a6f13a4aSGreg Roach                });
2183a6f13a4aSGreg Roach                break;
2184a6f13a4aSGreg Roach            case 'BIRT:DATE':
2185a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2186a6f13a4aSGreg Roach                break;
2187a6f13a4aSGreg Roach            case 'DEAT:DATE':
2188a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2189a6f13a4aSGreg Roach                break;
2190a6f13a4aSGreg Roach            case 'MARR:DATE':
21915d0bc43dSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2192a6f13a4aSGreg Roach                break;
2193a6f13a4aSGreg Roach            default:
2194a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2195a6f13a4aSGreg Roach                break;
2196a6f13a4aSGreg Roach        }
2197a6f13a4aSGreg Roach
21989b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2199e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2200a6f13a4aSGreg Roach    }
2201a6f13a4aSGreg Roach
2202a6f13a4aSGreg Roach    /**
220376692c8bSGreg Roach     * XML <List>
22048ba2e626SGreg Roach     *
22058ba2e626SGreg Roach     * @return void
2206a6f13a4aSGreg Roach     */
2207c1010edaSGreg Roach    private function listEndHandler()
2208c1010edaSGreg Roach    {
2209a6f13a4aSGreg Roach        $this->process_repeats--;
2210a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2211a6f13a4aSGreg Roach            return;
2212a6f13a4aSGreg Roach        }
2213a6f13a4aSGreg Roach
2214a6f13a4aSGreg Roach        // Check if there is any list
2215a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2216a6f13a4aSGreg Roach            $lineoffset = 0;
2217a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2218a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2219a6f13a4aSGreg Roach            }
2220a6f13a4aSGreg Roach            //-- read the xml from the file
2221299d100dSGreg Roach            $lines = file($this->report);
22227a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2223a6f13a4aSGreg Roach                $lineoffset--;
2224a6f13a4aSGreg Roach            }
2225a6f13a4aSGreg Roach            $lineoffset++;
2226a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2227a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2228a6f13a4aSGreg Roach            // List Level counter
2229a6f13a4aSGreg Roach            $count = 1;
2230a6f13a4aSGreg Roach            while (0 < $count) {
22317a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2232a6f13a4aSGreg Roach                    $count++;
22337a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2234a6f13a4aSGreg Roach                    $count--;
2235a6f13a4aSGreg Roach                }
2236a6f13a4aSGreg Roach                if (0 < $count) {
2237a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2238a6f13a4aSGreg Roach                }
2239a6f13a4aSGreg Roach                $line_nr++;
2240a6f13a4aSGreg Roach            }
2241a6f13a4aSGreg Roach            // No need to drag this
2242a6f13a4aSGreg Roach            unset($lines);
22437a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2244a6f13a4aSGreg Roach            // Save original values
22459b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2246a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2247a6f13a4aSGreg Roach
2248a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2249a6f13a4aSGreg Roach            $this->list_private = 0;
2250a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2251a6f13a4aSGreg Roach                if ($record->canShow()) {
2252a6f13a4aSGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->getTree()));
2253a6f13a4aSGreg Roach                    //-- start the sax parser
2254a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2255e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2256a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
2257c1010edaSGreg Roach                    xml_set_element_handler($repeat_parser, [
2258c1010edaSGreg Roach                        $this,
2259c1010edaSGreg Roach                        'startElement',
2260c1010edaSGreg Roach                    ], [
2261c1010edaSGreg Roach                        $this,
2262c1010edaSGreg Roach                        'endElement',
2263c1010edaSGreg Roach                    ]);
2264c1010edaSGreg Roach                    xml_set_character_data_handler($repeat_parser, [
2265c1010edaSGreg Roach                        $this,
2266c1010edaSGreg Roach                        'characterData',
2267c1010edaSGreg Roach                    ]);
2268a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
2269a6f13a4aSGreg Roach                        throw new \DomainException(sprintf(
2270a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2271a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2272a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2273a6f13a4aSGreg Roach                        ));
2274a6f13a4aSGreg Roach                    }
2275a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2276a6f13a4aSGreg Roach                } else {
2277a6f13a4aSGreg Roach                    $this->list_private++;
2278a6f13a4aSGreg Roach                }
2279a6f13a4aSGreg Roach            }
228013abd6f3SGreg Roach            $this->list   = [];
2281e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2282a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2283a6f13a4aSGreg Roach        }
2284a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2285a6f13a4aSGreg Roach    }
2286a6f13a4aSGreg Roach
2287a6f13a4aSGreg Roach    /**
2288a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2289a6f13a4aSGreg Roach     * Prints the total number of records in a list
2290a6f13a4aSGreg Roach     * The total number is collected from
2291a6f13a4aSGreg Roach     * List and Relatives
22928ba2e626SGreg Roach     *
22938ba2e626SGreg Roach     * @return void
2294a6f13a4aSGreg Roach     */
2295c1010edaSGreg Roach    private function listTotalStartHandler()
2296c1010edaSGreg Roach    {
2297a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2298a6f13a4aSGreg Roach            $this->current_element->addText($this->list_total);
2299a6f13a4aSGreg Roach        } else {
23007a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2301a6f13a4aSGreg Roach        }
2302a6f13a4aSGreg Roach    }
2303a6f13a4aSGreg Roach
2304a6f13a4aSGreg Roach    /**
230576692c8bSGreg Roach     * XML <Relatives>
230676692c8bSGreg Roach     *
2307a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
23088ba2e626SGreg Roach     *
23098ba2e626SGreg Roach     * @return void
2310a6f13a4aSGreg Roach     */
2311c1010edaSGreg Roach    private function relativesStartHandler($attrs)
2312c1010edaSGreg Roach    {
2313a6f13a4aSGreg Roach        $this->process_repeats++;
2314a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2315a6f13a4aSGreg Roach            return;
2316a6f13a4aSGreg Roach        }
2317a6f13a4aSGreg Roach
23187a6ee1acSGreg Roach        $sortby = 'NAME';
2319a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
2320a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
2321a6f13a4aSGreg Roach        }
232213abd6f3SGreg Roach        $match = [];
2323a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2324d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2325a6f13a4aSGreg Roach            $sortby = trim($sortby);
2326a6f13a4aSGreg Roach        }
2327a6f13a4aSGreg Roach
2328a6f13a4aSGreg Roach        $maxgen = -1;
2329a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2330a6f13a4aSGreg Roach            $maxgen = $attrs['maxgen'];
2331a6f13a4aSGreg Roach        }
23327a6ee1acSGreg Roach        if ($maxgen == '*') {
2333a6f13a4aSGreg Roach            $maxgen = -1;
2334a6f13a4aSGreg Roach        }
2335a6f13a4aSGreg Roach
23367a6ee1acSGreg Roach        $group = 'child-family';
2337a6f13a4aSGreg Roach        if (isset($attrs['group'])) {
2338a6f13a4aSGreg Roach            $group = $attrs['group'];
2339a6f13a4aSGreg Roach        }
2340a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2341d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2342a6f13a4aSGreg Roach            $group = trim($group);
2343a6f13a4aSGreg Roach        }
2344a6f13a4aSGreg Roach
23457a6ee1acSGreg Roach        $id = '';
2346a6f13a4aSGreg Roach        if (isset($attrs['id'])) {
2347a6f13a4aSGreg Roach            $id = $attrs['id'];
2348a6f13a4aSGreg Roach        }
2349a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2350d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2351a6f13a4aSGreg Roach            $id = trim($id);
2352a6f13a4aSGreg Roach        }
2353a6f13a4aSGreg Roach
235413abd6f3SGreg Roach        $this->list = [];
2355299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2356a6f13a4aSGreg Roach        if (!empty($person)) {
2357a6f13a4aSGreg Roach            $this->list[$id] = $person;
2358a6f13a4aSGreg Roach            switch ($group) {
23597a6ee1acSGreg Roach                case 'child-family':
2360a6f13a4aSGreg Roach                    foreach ($person->getChildFamilies() as $family) {
2361a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2362a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2363a6f13a4aSGreg Roach                        if (!empty($husband)) {
2364a6f13a4aSGreg Roach                            $this->list[$husband->getXref()] = $husband;
2365a6f13a4aSGreg Roach                        }
2366a6f13a4aSGreg Roach                        if (!empty($wife)) {
2367a6f13a4aSGreg Roach                            $this->list[$wife->getXref()] = $wife;
2368a6f13a4aSGreg Roach                        }
2369a6f13a4aSGreg Roach                        $children = $family->getChildren();
2370a6f13a4aSGreg Roach                        foreach ($children as $child) {
2371a6f13a4aSGreg Roach                            if (!empty($child)) {
2372a6f13a4aSGreg Roach                                $this->list[$child->getXref()] = $child;
2373a6f13a4aSGreg Roach                            }
2374a6f13a4aSGreg Roach                        }
2375a6f13a4aSGreg Roach                    }
2376a6f13a4aSGreg Roach                    break;
23777a6ee1acSGreg Roach                case 'spouse-family':
2378a6f13a4aSGreg Roach                    foreach ($person->getSpouseFamilies() as $family) {
2379a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2380a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2381a6f13a4aSGreg Roach                        if (!empty($husband)) {
2382a6f13a4aSGreg Roach                            $this->list[$husband->getXref()] = $husband;
2383a6f13a4aSGreg Roach                        }
2384a6f13a4aSGreg Roach                        if (!empty($wife)) {
2385a6f13a4aSGreg Roach                            $this->list[$wife->getXref()] = $wife;
2386a6f13a4aSGreg Roach                        }
2387a6f13a4aSGreg Roach                        $children = $family->getChildren();
2388a6f13a4aSGreg Roach                        foreach ($children as $child) {
2389a6f13a4aSGreg Roach                            if (!empty($child)) {
2390a6f13a4aSGreg Roach                                $this->list[$child->getXref()] = $child;
2391a6f13a4aSGreg Roach                            }
2392a6f13a4aSGreg Roach                        }
2393a6f13a4aSGreg Roach                    }
2394a6f13a4aSGreg Roach                    break;
23957a6ee1acSGreg Roach                case 'direct-ancestors':
23963d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2397a6f13a4aSGreg Roach                    break;
23987a6ee1acSGreg Roach                case 'ancestors':
23993d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2400a6f13a4aSGreg Roach                    break;
24017a6ee1acSGreg Roach                case 'descendants':
2402a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24033d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2404a6f13a4aSGreg Roach                    break;
24057a6ee1acSGreg Roach                case 'all':
24063d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24073d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2408a6f13a4aSGreg Roach                    break;
2409a6f13a4aSGreg Roach            }
2410a6f13a4aSGreg Roach        }
2411a6f13a4aSGreg Roach
2412a6f13a4aSGreg Roach        switch ($sortby) {
2413a6f13a4aSGreg Roach            case 'NAME':
2414a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2415a6f13a4aSGreg Roach                break;
2416a6f13a4aSGreg Roach            case 'BIRT:DATE':
2417a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2418a6f13a4aSGreg Roach                break;
2419a6f13a4aSGreg Roach            case 'DEAT:DATE':
2420a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2421a6f13a4aSGreg Roach                break;
2422a6f13a4aSGreg Roach            case 'generation':
242313abd6f3SGreg Roach                $newarray = [];
2424a6f13a4aSGreg Roach                reset($this->list);
2425a6f13a4aSGreg Roach                $genCounter = 1;
2426a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2427a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2428a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2429a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
243079529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2431a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2432a6f13a4aSGreg Roach                        }
2433a6f13a4aSGreg Roach                    }
2434a6f13a4aSGreg Roach                    $genCounter++;
2435a6f13a4aSGreg Roach                }
2436a6f13a4aSGreg Roach                $this->list = $newarray;
2437a6f13a4aSGreg Roach                break;
2438a6f13a4aSGreg Roach            default:
2439a6f13a4aSGreg Roach                // unsorted
2440a6f13a4aSGreg Roach                break;
2441a6f13a4aSGreg Roach        }
24429b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2443e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2444a6f13a4aSGreg Roach    }
2445a6f13a4aSGreg Roach
2446a6f13a4aSGreg Roach    /**
244776692c8bSGreg Roach     * XML </ Relatives>
24488ba2e626SGreg Roach     *
24498ba2e626SGreg Roach     * @return void
2450a6f13a4aSGreg Roach     */
2451c1010edaSGreg Roach    private function relativesEndHandler()
2452c1010edaSGreg Roach    {
2453a6f13a4aSGreg Roach        $this->process_repeats--;
2454a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2455a6f13a4aSGreg Roach            return;
2456a6f13a4aSGreg Roach        }
2457a6f13a4aSGreg Roach
2458a6f13a4aSGreg Roach        // Check if there is any relatives
2459a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2460a6f13a4aSGreg Roach            $lineoffset = 0;
2461a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2462a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2463a6f13a4aSGreg Roach            }
2464a6f13a4aSGreg Roach            //-- read the xml from the file
2465299d100dSGreg Roach            $lines = file($this->report);
24667a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2467a6f13a4aSGreg Roach                $lineoffset--;
2468a6f13a4aSGreg Roach            }
2469a6f13a4aSGreg Roach            $lineoffset++;
2470a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2471a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2472a6f13a4aSGreg Roach            // Relatives Level counter
2473a6f13a4aSGreg Roach            $count = 1;
2474a6f13a4aSGreg Roach            while (0 < $count) {
24757a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2476a6f13a4aSGreg Roach                    $count++;
24777a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2478a6f13a4aSGreg Roach                    $count--;
2479a6f13a4aSGreg Roach                }
2480a6f13a4aSGreg Roach                if (0 < $count) {
2481a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2482a6f13a4aSGreg Roach                }
2483a6f13a4aSGreg Roach                $line_nr++;
2484a6f13a4aSGreg Roach            }
2485a6f13a4aSGreg Roach            // No need to drag this
2486a6f13a4aSGreg Roach            unset($lines);
2487a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2488a6f13a4aSGreg Roach            // Save original values
24899b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2490a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2491a6f13a4aSGreg Roach
2492a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2493a6f13a4aSGreg Roach            $this->list_private = 0;
2494a6f13a4aSGreg Roach            foreach ($this->list as $key => $value) {
2495a6f13a4aSGreg Roach                if (isset($value->generation)) {
2496a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2497a6f13a4aSGreg Roach                }
2498299d100dSGreg Roach                $tmp          = GedcomRecord::getInstance($key, $this->tree);
2499299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2500a6f13a4aSGreg Roach
2501a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2502e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2503a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
2504c1010edaSGreg Roach                xml_set_element_handler($repeat_parser, [
2505c1010edaSGreg Roach                    $this,
2506c1010edaSGreg Roach                    'startElement',
2507c1010edaSGreg Roach                ], [
2508c1010edaSGreg Roach                    $this,
2509c1010edaSGreg Roach                    'endElement',
2510c1010edaSGreg Roach                ]);
2511c1010edaSGreg Roach                xml_set_character_data_handler($repeat_parser, [
2512c1010edaSGreg Roach                    $this,
2513c1010edaSGreg Roach                    'characterData',
2514c1010edaSGreg Roach                ]);
2515a6f13a4aSGreg Roach
2516a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25177a6ee1acSGreg 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)));
2518a6f13a4aSGreg Roach                }
2519a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2520a6f13a4aSGreg Roach            }
2521a6f13a4aSGreg Roach            // Clean up the list array
252213abd6f3SGreg Roach            $this->list   = [];
2523e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2524a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2525a6f13a4aSGreg Roach        }
2526a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2527a6f13a4aSGreg Roach    }
2528a6f13a4aSGreg Roach
2529a6f13a4aSGreg Roach    /**
2530a6f13a4aSGreg Roach     * XML <Generation /> element handler
2531a6f13a4aSGreg Roach     * Prints the number of generations
25328ba2e626SGreg Roach     *
25338ba2e626SGreg Roach     * @return void
2534a6f13a4aSGreg Roach     */
2535c1010edaSGreg Roach    private function generationStartHandler()
2536c1010edaSGreg Roach    {
2537a6f13a4aSGreg Roach        $this->current_element->addText($this->generation);
2538a6f13a4aSGreg Roach    }
2539a6f13a4aSGreg Roach
2540a6f13a4aSGreg Roach    /**
2541a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2542a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25438ba2e626SGreg Roach     *
25448ba2e626SGreg Roach     * @return void
2545a6f13a4aSGreg Roach     */
2546c1010edaSGreg Roach    private function newPageStartHandler()
2547c1010edaSGreg Roach    {
25487a6ee1acSGreg Roach        $temp = 'addpage';
2549e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2550a6f13a4aSGreg Roach    }
2551a6f13a4aSGreg Roach
2552a6f13a4aSGreg Roach    /**
255376692c8bSGreg Roach     * XML <html>
255476692c8bSGreg Roach     *
2555a6f13a4aSGreg Roach     * @param string  $tag   HTML tag name
255676692c8bSGreg Roach     * @param array[] $attrs an array of key value pairs for the attributes
25578ba2e626SGreg Roach     *
25588ba2e626SGreg Roach     * @return void
2559a6f13a4aSGreg Roach     */
2560c1010edaSGreg Roach    private function htmlStartHandler($tag, $attrs)
2561c1010edaSGreg Roach    {
25627a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2563a6f13a4aSGreg Roach            return;
2564a6f13a4aSGreg Roach        }
25659b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2566e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2567e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2568a6f13a4aSGreg Roach
25699b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2570a6f13a4aSGreg Roach        $this->print_data         = true;
2571a6f13a4aSGreg Roach    }
2572a6f13a4aSGreg Roach
2573a6f13a4aSGreg Roach    /**
257476692c8bSGreg Roach     * XML </html>
257576692c8bSGreg Roach     *
2576a6f13a4aSGreg Roach     * @param string $tag
25778ba2e626SGreg Roach     *
25788ba2e626SGreg Roach     * @return void
2579a6f13a4aSGreg Roach     */
2580c1010edaSGreg Roach    private function htmlEndHandler($tag)
2581c1010edaSGreg Roach    {
25827a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2583a6f13a4aSGreg Roach            return;
2584a6f13a4aSGreg Roach        }
2585a6f13a4aSGreg Roach
2586a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2587e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2588e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
25898f038c36SRico Sonntag        if ($this->wt_report !== null) {
2590e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2591a6f13a4aSGreg Roach        } else {
2592e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2593a6f13a4aSGreg Roach        }
2594a6f13a4aSGreg Roach    }
2595a6f13a4aSGreg Roach
2596a6f13a4aSGreg Roach    /**
2597a6f13a4aSGreg Roach     * Handle <Input>
25988ba2e626SGreg Roach     *
25998ba2e626SGreg Roach     * @return void
2600a6f13a4aSGreg Roach     */
2601c1010edaSGreg Roach    private function inputStartHandler()
2602c1010edaSGreg Roach    {
2603a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2604a6f13a4aSGreg Roach    }
2605a6f13a4aSGreg Roach
2606a6f13a4aSGreg Roach    /**
2607a6f13a4aSGreg Roach     * Handle </Input>
26088ba2e626SGreg Roach     *
26098ba2e626SGreg Roach     * @return void
2610a6f13a4aSGreg Roach     */
2611c1010edaSGreg Roach    private function inputEndHandler()
2612c1010edaSGreg Roach    {
2613a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2614a6f13a4aSGreg Roach    }
2615a6f13a4aSGreg Roach
2616a6f13a4aSGreg Roach    /**
2617a6f13a4aSGreg Roach     * Handle <Report>
26188ba2e626SGreg Roach     *
26198ba2e626SGreg Roach     * @return void
2620a6f13a4aSGreg Roach     */
2621c1010edaSGreg Roach    private function reportStartHandler()
2622c1010edaSGreg Roach    {
2623a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2624a6f13a4aSGreg Roach    }
2625a6f13a4aSGreg Roach
2626a6f13a4aSGreg Roach    /**
2627a6f13a4aSGreg Roach     * Handle </Report>
26288ba2e626SGreg Roach     *
26298ba2e626SGreg Roach     * @return void
2630a6f13a4aSGreg Roach     */
2631c1010edaSGreg Roach    private function reportEndHandler()
2632c1010edaSGreg Roach    {
2633a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2634a6f13a4aSGreg Roach    }
2635a6f13a4aSGreg Roach
2636a6f13a4aSGreg Roach    /**
263776692c8bSGreg Roach     * XML </titleEndHandler>
26388ba2e626SGreg Roach     *
26398ba2e626SGreg Roach     * @return void
2640a6f13a4aSGreg Roach     */
2641c1010edaSGreg Roach    private function titleEndHandler()
2642c1010edaSGreg Roach    {
26432836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2644a6f13a4aSGreg Roach    }
2645a6f13a4aSGreg Roach
2646a6f13a4aSGreg Roach    /**
264776692c8bSGreg Roach     * XML </descriptionEndHandler>
26488ba2e626SGreg Roach     *
26498ba2e626SGreg Roach     * @return void
2650a6f13a4aSGreg Roach     */
2651c1010edaSGreg Roach    private function descriptionEndHandler()
2652c1010edaSGreg Roach    {
26532836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2654a6f13a4aSGreg Roach    }
2655729ce104SGreg Roach
2656729ce104SGreg Roach    /**
265776692c8bSGreg Roach     * Create a list of all descendants.
265876692c8bSGreg Roach     *
2659729ce104SGreg Roach     * @param string[] $list
2660729ce104SGreg Roach     * @param string   $pid
2661729ce104SGreg Roach     * @param bool     $parents
2662729ce104SGreg Roach     * @param int      $generations
26638ba2e626SGreg Roach     *
26648ba2e626SGreg Roach     * @return void
2665729ce104SGreg Roach     */
2666c1010edaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1)
2667c1010edaSGreg Roach    {
2668299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2669729ce104SGreg Roach        if ($person === null) {
2670729ce104SGreg Roach            return;
2671729ce104SGreg Roach        }
2672729ce104SGreg Roach        if (!isset($list[$pid])) {
2673729ce104SGreg Roach            $list[$pid] = $person;
2674729ce104SGreg Roach        }
2675729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2676729ce104SGreg Roach            $list[$pid]->generation = 0;
2677729ce104SGreg Roach        }
2678729ce104SGreg Roach        foreach ($person->getSpouseFamilies() as $family) {
2679729ce104SGreg Roach            if ($parents) {
2680729ce104SGreg Roach                $husband = $family->getHusband();
2681729ce104SGreg Roach                $wife    = $family->getWife();
2682729ce104SGreg Roach                if ($husband) {
2683729ce104SGreg Roach                    $list[$husband->getXref()] = $husband;
2684729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2685729ce104SGreg Roach                        $list[$husband->getXref()]->generation = $list[$pid]->generation - 1;
2686729ce104SGreg Roach                    } else {
2687729ce104SGreg Roach                        $list[$husband->getXref()]->generation = 1;
2688729ce104SGreg Roach                    }
2689729ce104SGreg Roach                }
2690729ce104SGreg Roach                if ($wife) {
2691729ce104SGreg Roach                    $list[$wife->getXref()] = $wife;
2692729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2693729ce104SGreg Roach                        $list[$wife->getXref()]->generation = $list[$pid]->generation - 1;
2694729ce104SGreg Roach                    } else {
2695729ce104SGreg Roach                        $list[$wife->getXref()]->generation = 1;
2696729ce104SGreg Roach                    }
2697729ce104SGreg Roach                }
2698729ce104SGreg Roach            }
2699729ce104SGreg Roach            $children = $family->getChildren();
2700729ce104SGreg Roach            foreach ($children as $child) {
2701729ce104SGreg Roach                if ($child) {
2702729ce104SGreg Roach                    $list[$child->getXref()] = $child;
2703729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2704729ce104SGreg Roach                        $list[$child->getXref()]->generation = $list[$pid]->generation + 1;
2705729ce104SGreg Roach                    } else {
2706729ce104SGreg Roach                        $list[$child->getXref()]->generation = 2;
2707729ce104SGreg Roach                    }
2708729ce104SGreg Roach                }
2709729ce104SGreg Roach            }
2710729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2711729ce104SGreg Roach                foreach ($children as $child) {
27123d7a8a4cSGreg Roach                    $this->addDescendancy($list, $child->getXref(), $parents, $generations); // recurse on the childs family
2713729ce104SGreg Roach                }
2714729ce104SGreg Roach            }
2715729ce104SGreg Roach        }
2716729ce104SGreg Roach    }
2717729ce104SGreg Roach
2718729ce104SGreg Roach    /**
271976692c8bSGreg Roach     * Create a list of all ancestors.
272076692c8bSGreg Roach     *
2721729ce104SGreg Roach     * @param string[] $list
2722729ce104SGreg Roach     * @param string   $pid
2723729ce104SGreg Roach     * @param bool     $children
2724729ce104SGreg Roach     * @param int      $generations
27258ba2e626SGreg Roach     *
27268ba2e626SGreg Roach     * @return void
2727729ce104SGreg Roach     */
2728c1010edaSGreg Roach    private function addAncestors(&$list, $pid, $children = false, $generations = -1)
2729c1010edaSGreg Roach    {
273013abd6f3SGreg Roach        $genlist                = [$pid];
2731729ce104SGreg Roach        $list[$pid]->generation = 1;
2732729ce104SGreg Roach        while (count($genlist) > 0) {
2733729ce104SGreg Roach            $id = array_shift($genlist);
2734729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2735729ce104SGreg Roach                continue; // id can be something like “empty7”
2736729ce104SGreg Roach            }
2737299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
2738729ce104SGreg Roach            foreach ($person->getChildFamilies() as $family) {
2739729ce104SGreg Roach                $husband = $family->getHusband();
2740729ce104SGreg Roach                $wife    = $family->getWife();
2741729ce104SGreg Roach                if ($husband) {
2742729ce104SGreg Roach                    $list[$husband->getXref()]             = $husband;
2743729ce104SGreg Roach                    $list[$husband->getXref()]->generation = $list[$id]->generation + 1;
2744729ce104SGreg Roach                }
2745729ce104SGreg Roach                if ($wife) {
2746729ce104SGreg Roach                    $list[$wife->getXref()]             = $wife;
2747729ce104SGreg Roach                    $list[$wife->getXref()]->generation = $list[$id]->generation + 1;
2748729ce104SGreg Roach                }
2749729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2750729ce104SGreg Roach                    if ($husband) {
27519b3dd960SGreg Roach                        $genlist[] = $husband->getXref();
2752729ce104SGreg Roach                    }
2753729ce104SGreg Roach                    if ($wife) {
27549b3dd960SGreg Roach                        $genlist[] = $wife->getXref();
2755729ce104SGreg Roach                    }
2756729ce104SGreg Roach                }
2757729ce104SGreg Roach                if ($children) {
2758729ce104SGreg Roach                    foreach ($family->getChildren() as $child) {
2759729ce104SGreg Roach                        $list[$child->getXref()] = $child;
2760729ce104SGreg Roach                        if (isset($list[$id]->generation)) {
2761729ce104SGreg Roach                            $list[$child->getXref()]->generation = $list[$id]->generation;
2762729ce104SGreg Roach                        } else {
2763729ce104SGreg Roach                            $list[$child->getXref()]->generation = 1;
2764729ce104SGreg Roach                        }
2765729ce104SGreg Roach                    }
2766729ce104SGreg Roach                }
2767729ce104SGreg Roach            }
2768729ce104SGreg Roach        }
2769729ce104SGreg Roach    }
2770729ce104SGreg Roach
2771729ce104SGreg Roach    /**
2772729ce104SGreg Roach     * get gedcom tag value
2773729ce104SGreg Roach     *
2774729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2775729ce104SGreg 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
2776729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2777729ce104SGreg Roach     *
2778729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2779729ce104SGreg Roach     */
27808f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2781c1010edaSGreg Roach    {
2782729ce104SGreg Roach        if (empty($gedrec)) {
2783729ce104SGreg Roach            return '';
2784729ce104SGreg Roach        }
2785729ce104SGreg Roach        $tags      = explode(':', $tag);
2786729ce104SGreg Roach        $origlevel = $level;
2787729ce104SGreg Roach        if ($level == 0) {
27883c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2789729ce104SGreg Roach        }
2790729ce104SGreg Roach
2791729ce104SGreg Roach        $subrec = $gedrec;
2792729ce104SGreg Roach        foreach ($tags as $t) {
2793729ce104SGreg Roach            $lastsubrec = $subrec;
27943d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2795729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2796729ce104SGreg Roach                $level--;
27973d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2798729ce104SGreg Roach            }
2799729ce104SGreg Roach            if (empty($subrec)) {
28007a6ee1acSGreg Roach                if ($t == 'TITL') {
28013d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2802729ce104SGreg Roach                    if (!empty($subrec)) {
28037a6ee1acSGreg Roach                        $t = 'ABBR';
2804729ce104SGreg Roach                    }
2805729ce104SGreg Roach                }
2806729ce104SGreg Roach                if (empty($subrec)) {
2807729ce104SGreg Roach                    if ($level > 0) {
2808729ce104SGreg Roach                        $level--;
2809729ce104SGreg Roach                    }
28103d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2811729ce104SGreg Roach                    if (empty($subrec)) {
2812729ce104SGreg Roach                        return '';
2813729ce104SGreg Roach                    }
2814729ce104SGreg Roach                }
2815729ce104SGreg Roach            }
2816729ce104SGreg Roach            $level++;
2817729ce104SGreg Roach        }
2818729ce104SGreg Roach        $level--;
2819729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2820729ce104SGreg Roach        if ($ct == 0) {
2821729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2822729ce104SGreg Roach        }
2823729ce104SGreg Roach        if ($ct == 0) {
2824729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2825729ce104SGreg Roach        }
2826729ce104SGreg Roach        if ($ct > 0) {
2827729ce104SGreg Roach            $value = trim($match[1]);
2828729ce104SGreg Roach            if ($t == 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2829299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2830729ce104SGreg Roach                if ($note) {
2831729ce104SGreg Roach                    $value = $note->getNote();
2832729ce104SGreg Roach                } else {
2833729ce104SGreg Roach                    //-- set the value to the id without the @
2834729ce104SGreg Roach                    $value = $match[1];
2835729ce104SGreg Roach                }
2836729ce104SGreg Roach            }
28377a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28383d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2839729ce104SGreg Roach            }
2840729ce104SGreg Roach
2841729ce104SGreg Roach            return $value;
2842729ce104SGreg Roach        }
2843729ce104SGreg Roach
28447a6ee1acSGreg Roach        return '';
2845729ce104SGreg Roach    }
2846d1286247SGreg Roach
2847d1286247SGreg Roach    /**
2848d1286247SGreg Roach     * Replace variable identifiers with their values.
2849d1286247SGreg Roach     *
2850d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
285182759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2852d1286247SGreg Roach     *
2853d1286247SGreg Roach     * @return string
2854d1286247SGreg Roach     */
28558f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2856c1010edaSGreg Roach    {
2857d1286247SGreg Roach        return preg_replace_callback(
2858d1286247SGreg Roach            '/\$(\w+)/',
28592118c0e3SGreg Roach            function ($matches) use ($quote) {
28602118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
286182759250SGreg Roach                    if ($quote) {
28622118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2863*b2ce94c6SRico Sonntag                    }
2864*b2ce94c6SRico Sonntag
28652118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
286682759250SGreg Roach                }
2867*b2ce94c6SRico Sonntag
2868d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28693d7a8a4cSGreg Roach
2870d1286247SGreg Roach                return '$' . $matches[1];
2871d1286247SGreg Roach            },
2872d1286247SGreg Roach            $expression
2873d1286247SGreg Roach        );
2874d1286247SGreg Roach    }
2875a6f13a4aSGreg Roach}
2876