xref: /webtrees/app/Report/ReportParserGenerate.php (revision c09358794665832df009408551d8543ccb6e3783)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
1976692c8bSGreg Roach
20a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
21a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Database;
22a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
23a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
24a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
253d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
263d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
28a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
31d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Media;
33729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
34a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
35299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
3679529c87SGreg Roachuse stdClass;
37c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
38cb63a60eSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionFunction;
395809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
40a6f13a4aSGreg Roach
41a6f13a4aSGreg Roach/**
42a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
43a6f13a4aSGreg Roach */
44c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
45c1010edaSGreg Roach{
46a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
47a6f13a4aSGreg Roach    private $process_footnote = true;
48a6f13a4aSGreg Roach
49a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
50a6f13a4aSGreg Roach    private $print_data = false;
51a6f13a4aSGreg Roach
52a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5313abd6f3SGreg Roach    private $print_data_stack = [];
54a6f13a4aSGreg Roach
5576692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
56a6f13a4aSGreg Roach    private $process_gedcoms = 0;
57a6f13a4aSGreg Roach
5876692c8bSGreg Roach    /** @var int Are we processing conditionals */
59a6f13a4aSGreg Roach    private $process_ifs = 0;
60a6f13a4aSGreg Roach
6176692c8bSGreg Roach    /** @var int Are we processing repeats */
62a6f13a4aSGreg Roach    private $process_repeats = 0;
63a6f13a4aSGreg Roach
64a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
65a6f13a4aSGreg Roach    private $repeat_bytes = 0;
66a6f13a4aSGreg Roach
675b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
6813abd6f3SGreg Roach    private $repeats = [];
69a6f13a4aSGreg Roach
70a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
7113abd6f3SGreg Roach    private $repeats_stack = [];
72a6f13a4aSGreg Roach
73208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
7413abd6f3SGreg Roach    private $wt_report_stack = [];
75e8e7866bSGreg Roach
76e8e7866bSGreg Roach    /** @var resource Nested repeating data */
77e8e7866bSGreg Roach    private $parser;
78e8e7866bSGreg Roach
79e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
8013abd6f3SGreg Roach    private $parser_stack = [];
81e8e7866bSGreg Roach
82a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
83a6f13a4aSGreg Roach    private $gedrec = '';
84a6f13a4aSGreg Roach
85a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
8613abd6f3SGreg Roach    private $gedrec_stack = [];
87a6f13a4aSGreg Roach
88a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
89a6f13a4aSGreg Roach    private $current_element;
90a6f13a4aSGreg Roach
91a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
92a6f13a4aSGreg Roach    private $footnote_element;
93a6f13a4aSGreg Roach
94a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
95a6f13a4aSGreg Roach    private $fact = '';
96a6f13a4aSGreg Roach
97a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
98a6f13a4aSGreg Roach    private $desc = '';
99a6f13a4aSGreg Roach
100a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
101a6f13a4aSGreg Roach    private $type = '';
102a6f13a4aSGreg Roach
103a6f13a4aSGreg Roach    /** @var int The current generational level */
104a6f13a4aSGreg Roach    private $generation = 1;
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
10713abd6f3SGreg Roach    private $list = [];
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var int Number of items in lists */
110a6f13a4aSGreg Roach    private $list_total = 0;
111a6f13a4aSGreg Roach
112a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
113a6f13a4aSGreg Roach    private $list_private = 0;
114a6f13a4aSGreg Roach
115299d100dSGreg Roach    /** @var string The filename of the XML report */
116299d100dSGreg Roach    protected $report;
117299d100dSGreg Roach
118208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
119e8e7866bSGreg Roach    private $report_root;
120e8e7866bSGreg Roach
121208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
122e8e7866bSGreg Roach    private $wt_report;
123e8e7866bSGreg Roach
124d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1252118c0e3SGreg Roach    private $vars;
126d1286247SGreg Roach
127299d100dSGreg Roach    /** @var Tree The current tree */
128299d100dSGreg Roach    private $tree;
129299d100dSGreg Roach
13076692c8bSGreg Roach    /**
13176692c8bSGreg Roach     * Create a parser for a report
13276692c8bSGreg Roach     *
13376692c8bSGreg Roach     * @param string         $report The XML filename
134208e9f76SGreg Roach     * @param AbstractReport $report_root
13576692c8bSGreg Roach     * @param string[][]     $vars
136299d100dSGreg Roach     * @param Tree           $tree
13776692c8bSGreg Roach     */
138208e9f76SGreg Roach    public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree)
139c1010edaSGreg Roach    {
140299d100dSGreg Roach        $this->report          = $report;
141e8e7866bSGreg Roach        $this->report_root     = $report_root;
142e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14359f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
144d1286247SGreg Roach        $this->vars            = $vars;
145299d100dSGreg Roach        $this->tree            = $tree;
146299d100dSGreg Roach
14776f666f4SGreg Roach        parent::__construct($report);
148a6f13a4aSGreg Roach    }
149a6f13a4aSGreg Roach
150a6f13a4aSGreg Roach    /**
151a6f13a4aSGreg Roach     * XML start element handler
152a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
153a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
154a6f13a4aSGreg Roach     *
155a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
156a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1578a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
15818d7a90dSGreg Roach     *
15918d7a90dSGreg Roach     * @return void
160a6f13a4aSGreg Roach     */
1618a4ee39cSGreg Roach    protected function startElement($parser, string $name, array $attrs)
162c1010edaSGreg Roach    {
16313abd6f3SGreg Roach        $newattrs = [];
164a6f13a4aSGreg Roach
165a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
166a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
167d1286247SGreg Roach                if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
168d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
169a6f13a4aSGreg Roach                }
170a6f13a4aSGreg Roach            }
171a6f13a4aSGreg Roach            $newattrs[$key] = $value;
172a6f13a4aSGreg Roach        }
173a6f13a4aSGreg Roach        $attrs = $newattrs;
1747a6ee1acSGreg 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')) {
175a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
176a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
177208e9f76SGreg Roach
178a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
179a6f13a4aSGreg Roach                $this->$start_method($attrs);
180a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
181a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
182a6f13a4aSGreg Roach            }
183a6f13a4aSGreg Roach        }
184a6f13a4aSGreg Roach    }
185a6f13a4aSGreg Roach
186a6f13a4aSGreg Roach    /**
187a6f13a4aSGreg Roach     * XML end element handler
188a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
189a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
190a6f13a4aSGreg Roach     *
191a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
192a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
19318d7a90dSGreg Roach     *
19418d7a90dSGreg Roach     * @return void
195a6f13a4aSGreg Roach     */
1968a4ee39cSGreg Roach    protected function endElement($parser, string $name)
197c1010edaSGreg Roach    {
1987a6ee1acSGreg 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')) {
199a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
200a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
201a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
202a6f13a4aSGreg Roach                $this->$end_method();
203a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
204a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
205a6f13a4aSGreg Roach            }
206a6f13a4aSGreg Roach        }
207a6f13a4aSGreg Roach    }
208a6f13a4aSGreg Roach
209a6f13a4aSGreg Roach    /**
210a6f13a4aSGreg Roach     * XML character data handler
211a6f13a4aSGreg Roach     *
212a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
213a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
21418d7a90dSGreg Roach     *
21518d7a90dSGreg Roach     * @return void
216a6f13a4aSGreg Roach     */
217c1010edaSGreg Roach    protected function characterData($parser, $data)
218c1010edaSGreg Roach    {
219e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
220a6f13a4aSGreg Roach            $this->current_element->addText($data);
221a6f13a4aSGreg Roach        }
222a6f13a4aSGreg Roach    }
223a6f13a4aSGreg Roach
224a6f13a4aSGreg Roach    /**
22576692c8bSGreg Roach     * XML <style>
226a6f13a4aSGreg Roach     *
227c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2288ba2e626SGreg Roach     *
2298ba2e626SGreg Roach     * @return void
230a6f13a4aSGreg Roach     */
231c0fe75acSGreg Roach    private function styleStartHandler(array $attrs)
232c1010edaSGreg Roach    {
233a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
234a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
235a6f13a4aSGreg Roach        }
236a6f13a4aSGreg Roach
237a6f13a4aSGreg Roach        // array Style that will be passed on
23813abd6f3SGreg Roach        $s = [];
239a6f13a4aSGreg Roach
240a6f13a4aSGreg Roach        // string Name af the style
241a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
242a6f13a4aSGreg Roach
243a6f13a4aSGreg Roach        // string Name of the DEFAULT font
244208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
245a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
246a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
247a6f13a4aSGreg Roach        }
248a6f13a4aSGreg Roach
249a6f13a4aSGreg Roach        // int The size of the font in points
250208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
251a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
252a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
253a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
254a6f13a4aSGreg Roach
255a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2567a6ee1acSGreg Roach        $s['style'] = '';
257a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
258a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
259a6f13a4aSGreg Roach        }
260a6f13a4aSGreg Roach
261e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
262a6f13a4aSGreg Roach    }
263a6f13a4aSGreg Roach
264a6f13a4aSGreg Roach    /**
26576692c8bSGreg Roach     * XML <Doc>
266a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
267a6f13a4aSGreg Roach     *
268c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
2698ba2e626SGreg Roach     *
2708ba2e626SGreg Roach     * @return void
271a6f13a4aSGreg Roach     */
272c0fe75acSGreg Roach    private function docStartHandler(array $attrs)
273c1010edaSGreg Roach    {
274e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
275a6f13a4aSGreg Roach
276a6f13a4aSGreg Roach        // Custom page width
277a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
278208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
279a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
280a6f13a4aSGreg Roach        // Custom Page height
281a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
282208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
283a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
284a6f13a4aSGreg Roach
285a6f13a4aSGreg Roach        // Left Margin
286a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2877a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
288208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
289a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
290208e9f76SGreg Roach                $this->wt_report->left_margin = (int) $attrs['leftmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
291a6f13a4aSGreg Roach            }
292a6f13a4aSGreg Roach        }
293a6f13a4aSGreg Roach        // Right Margin
294a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
2957a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
296208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
297a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
298208e9f76SGreg Roach                $this->wt_report->right_margin = (int) $attrs['rightmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
299a6f13a4aSGreg Roach            }
300a6f13a4aSGreg Roach        }
301a6f13a4aSGreg Roach        // Top Margin
302a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3037a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
304208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
305a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
306208e9f76SGreg Roach                $this->wt_report->top_margin = (int) $attrs['topmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
307a6f13a4aSGreg Roach            }
308a6f13a4aSGreg Roach        }
309a6f13a4aSGreg Roach        // Bottom Margin
310a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3117a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
312208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
313a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
314208e9f76SGreg Roach                $this->wt_report->bottom_margin = (int) $attrs['bottommargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
315a6f13a4aSGreg Roach            }
316a6f13a4aSGreg Roach        }
317a6f13a4aSGreg Roach        // Header Margin
318a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3197a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
320208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
321a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
322208e9f76SGreg Roach                $this->wt_report->header_margin = (int) $attrs['headermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
323a6f13a4aSGreg Roach            }
324a6f13a4aSGreg Roach        }
325a6f13a4aSGreg Roach        // Footer Margin
326a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3277a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
328208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
329a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
330208e9f76SGreg Roach                $this->wt_report->footer_margin = (int) $attrs['footermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
331a6f13a4aSGreg Roach            }
332a6f13a4aSGreg Roach        }
333a6f13a4aSGreg Roach
334a6f13a4aSGreg Roach        // Page Orientation
335a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
336044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3377a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
338044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3397a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
340a6f13a4aSGreg Roach            }
341a6f13a4aSGreg Roach        }
342a6f13a4aSGreg Roach        // Page Size
343a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
344208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
345a6f13a4aSGreg Roach        }
346a6f13a4aSGreg Roach
347a6f13a4aSGreg Roach        // Show Generated By...
348a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3497a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
350208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3517a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
352208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
353a6f13a4aSGreg Roach            }
354a6f13a4aSGreg Roach        }
355a6f13a4aSGreg Roach
356e8e7866bSGreg Roach        $this->wt_report->setup();
357a6f13a4aSGreg Roach    }
358a6f13a4aSGreg Roach
359a6f13a4aSGreg Roach    /**
36076692c8bSGreg Roach     * XML </Doc>
3618ba2e626SGreg Roach     *
3628ba2e626SGreg Roach     * @return void
363a6f13a4aSGreg Roach     */
364c1010edaSGreg Roach    private function docEndHandler()
365c1010edaSGreg Roach    {
366e8e7866bSGreg Roach        $this->wt_report->run();
367a6f13a4aSGreg Roach    }
368a6f13a4aSGreg Roach
369a6f13a4aSGreg Roach    /**
37076692c8bSGreg Roach     * XML <Header>
3718ba2e626SGreg Roach     *
3728ba2e626SGreg Roach     * @return void
373a6f13a4aSGreg Roach     */
374c1010edaSGreg Roach    private function headerStartHandler()
375c1010edaSGreg Roach    {
376a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
377e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3787a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
379a6f13a4aSGreg Roach    }
380a6f13a4aSGreg Roach
381a6f13a4aSGreg Roach    /**
38276692c8bSGreg Roach     * XML <PageHeader>
3838ba2e626SGreg Roach     *
3848ba2e626SGreg Roach     * @return void
385a6f13a4aSGreg Roach     */
386c1010edaSGreg Roach    private function pageHeaderStartHandler()
387c1010edaSGreg Roach    {
3889b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
389a6f13a4aSGreg Roach        $this->print_data         = false;
3909b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
391e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
392a6f13a4aSGreg Roach    }
393a6f13a4aSGreg Roach
394a6f13a4aSGreg Roach    /**
39576692c8bSGreg Roach     * XML <pageHeaderEndHandler>
3968ba2e626SGreg Roach     *
3978ba2e626SGreg Roach     * @return void
398a6f13a4aSGreg Roach     */
399c1010edaSGreg Roach    private function pageHeaderEndHandler()
400c1010edaSGreg Roach    {
401a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
402e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
403e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
404e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
405a6f13a4aSGreg Roach    }
406a6f13a4aSGreg Roach
407a6f13a4aSGreg Roach    /**
40876692c8bSGreg Roach     * XML <bodyStartHandler>
4098ba2e626SGreg Roach     *
4108ba2e626SGreg Roach     * @return void
411a6f13a4aSGreg Roach     */
412c1010edaSGreg Roach    private function bodyStartHandler()
413c1010edaSGreg Roach    {
4147a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
415a6f13a4aSGreg Roach    }
416a6f13a4aSGreg Roach
417a6f13a4aSGreg Roach    /**
41876692c8bSGreg Roach     * XML <footerStartHandler>
4198ba2e626SGreg Roach     *
4208ba2e626SGreg Roach     * @return void
421a6f13a4aSGreg Roach     */
422c1010edaSGreg Roach    private function footerStartHandler()
423c1010edaSGreg Roach    {
4247a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
425a6f13a4aSGreg Roach    }
426a6f13a4aSGreg Roach
427a6f13a4aSGreg Roach    /**
42876692c8bSGreg Roach     * XML <Cell>
429a6f13a4aSGreg Roach     *
430c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4318ba2e626SGreg Roach     *
4328ba2e626SGreg Roach     * @return void
433a6f13a4aSGreg Roach     */
434c0fe75acSGreg Roach    private function cellStartHandler(array $attrs)
435c1010edaSGreg Roach    {
436a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4377a6ee1acSGreg Roach        $align = '';
438a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
439a6f13a4aSGreg Roach            $align = $attrs['align'];
440a6f13a4aSGreg Roach            // RTL supported left/right alignment
441044416d2SGreg Roach            if ($align === 'rightrtl') {
442e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4437a6ee1acSGreg Roach                    $align = 'left';
444a6f13a4aSGreg Roach                } else {
4457a6ee1acSGreg Roach                    $align = 'right';
446a6f13a4aSGreg Roach                }
447044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
448e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4497a6ee1acSGreg Roach                    $align = 'right';
450a6f13a4aSGreg Roach                } else {
4517a6ee1acSGreg Roach                    $align = 'left';
452a6f13a4aSGreg Roach                }
453a6f13a4aSGreg Roach            }
454a6f13a4aSGreg Roach        }
455a6f13a4aSGreg Roach
456a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4577a6ee1acSGreg Roach        $bgcolor = '';
458a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
459a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
460a6f13a4aSGreg Roach        }
461a6f13a4aSGreg Roach
462a6f13a4aSGreg Roach        // int Whether or not the background should be painted
463a6f13a4aSGreg Roach        $fill = 1;
464a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4657a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
466a6f13a4aSGreg Roach                $fill = 0;
4677a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
468a6f13a4aSGreg Roach                $fill = 1;
469a6f13a4aSGreg Roach            }
470a6f13a4aSGreg Roach        }
471a6f13a4aSGreg Roach
472a6f13a4aSGreg Roach        $reseth = true;
473a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
474a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4757a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
476a6f13a4aSGreg Roach                $reseth = false;
4777a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
478a6f13a4aSGreg Roach                $reseth = true;
479a6f13a4aSGreg Roach            }
480a6f13a4aSGreg Roach        }
481a6f13a4aSGreg Roach
482a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
483a6f13a4aSGreg Roach        $border = 0;
484a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
485a6f13a4aSGreg Roach            $border = $attrs['border'];
486a6f13a4aSGreg Roach        }
487a6f13a4aSGreg Roach        // string Border color in HTML code
4887a6ee1acSGreg Roach        $bocolor = '';
489a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
490a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
491a6f13a4aSGreg Roach        }
492a6f13a4aSGreg Roach
493a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
494a6f13a4aSGreg Roach        $height = 0;
495a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
496589feda3SGreg Roach            $height = $attrs['height'];
497a6f13a4aSGreg Roach        }
498a6f13a4aSGreg 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.
499a6f13a4aSGreg Roach        $width = 0;
500a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
501589feda3SGreg Roach            $width = $attrs['width'];
502a6f13a4aSGreg Roach        }
503a6f13a4aSGreg Roach
504a6f13a4aSGreg Roach        // int Stretch carachter mode
505a6f13a4aSGreg Roach        $stretch = 0;
506a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
507a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
508a6f13a4aSGreg Roach        }
509a6f13a4aSGreg Roach
510a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
511c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
512a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5137a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
514c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
515a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
516a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5177a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
518a6f13a4aSGreg Roach                $left = 0;
519a6f13a4aSGreg Roach            }
520a6f13a4aSGreg Roach        }
521a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
522c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
523a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5247a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
525c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
526a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
527a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5287a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
529a6f13a4aSGreg Roach                $top = 0;
530a6f13a4aSGreg Roach            }
531a6f13a4aSGreg Roach        }
532a6f13a4aSGreg Roach
533a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5347a6ee1acSGreg Roach        $style = '';
535a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
536a6f13a4aSGreg Roach            $style = $attrs['style'];
537a6f13a4aSGreg Roach        }
538a6f13a4aSGreg Roach
539a6f13a4aSGreg Roach        // string Text color in html code
5407a6ee1acSGreg Roach        $tcolor = '';
541a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
542a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
543a6f13a4aSGreg Roach        }
544a6f13a4aSGreg Roach
545a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
546a6f13a4aSGreg Roach        $ln = 0;
547a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
548a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
549a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5507a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
551a6f13a4aSGreg Roach                $ln = 0;
552a6f13a4aSGreg Roach            }
553a6f13a4aSGreg Roach        }
554a6f13a4aSGreg Roach
555044416d2SGreg Roach        if ($align === 'left') {
5567a6ee1acSGreg Roach            $align = 'L';
557044416d2SGreg Roach        } elseif ($align === 'right') {
5587a6ee1acSGreg Roach            $align = 'R';
559044416d2SGreg Roach        } elseif ($align === 'center') {
5607a6ee1acSGreg Roach            $align = 'C';
561044416d2SGreg Roach        } elseif ($align === 'justify') {
5627a6ee1acSGreg Roach            $align = 'J';
563a6f13a4aSGreg Roach        }
564a6f13a4aSGreg Roach
5659b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
566a6f13a4aSGreg Roach        $this->print_data         = true;
567a6f13a4aSGreg Roach
568e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
569a6f13a4aSGreg Roach            $width,
570a6f13a4aSGreg Roach            $height,
571a6f13a4aSGreg Roach            $border,
572a6f13a4aSGreg Roach            $align,
573a6f13a4aSGreg Roach            $bgcolor,
574a6f13a4aSGreg Roach            $style,
575a6f13a4aSGreg Roach            $ln,
576a6f13a4aSGreg Roach            $top,
577a6f13a4aSGreg Roach            $left,
578a6f13a4aSGreg Roach            $fill,
579a6f13a4aSGreg Roach            $stretch,
580a6f13a4aSGreg Roach            $bocolor,
581a6f13a4aSGreg Roach            $tcolor,
582a6f13a4aSGreg Roach            $reseth
583a6f13a4aSGreg Roach        );
584a6f13a4aSGreg Roach    }
585a6f13a4aSGreg Roach
586a6f13a4aSGreg Roach    /**
58776692c8bSGreg Roach     * XML </Cell>
5888ba2e626SGreg Roach     *
5898ba2e626SGreg Roach     * @return void
590a6f13a4aSGreg Roach     */
591c1010edaSGreg Roach    private function cellEndHandler()
592c1010edaSGreg Roach    {
593a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
594e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
595a6f13a4aSGreg Roach    }
596a6f13a4aSGreg Roach
597a6f13a4aSGreg Roach    /**
598a6f13a4aSGreg Roach     * XML <Now /> element handler
5998ba2e626SGreg Roach     *
6008ba2e626SGreg Roach     * @return void
601a6f13a4aSGreg Roach     */
602c1010edaSGreg Roach    private function nowStartHandler()
603c1010edaSGreg Roach    {
6043d7a8a4cSGreg Roach        $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
605a6f13a4aSGreg Roach        $this->current_element->addText($g->display());
606a6f13a4aSGreg Roach    }
607a6f13a4aSGreg Roach
608a6f13a4aSGreg Roach    /**
609a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6108ba2e626SGreg Roach     *
6118ba2e626SGreg Roach     * @return void
612a6f13a4aSGreg Roach     */
613c1010edaSGreg Roach    private function pageNumStartHandler()
614c1010edaSGreg Roach    {
6157a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
616a6f13a4aSGreg Roach    }
617a6f13a4aSGreg Roach
618a6f13a4aSGreg Roach    /**
619a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6208ba2e626SGreg Roach     *
6218ba2e626SGreg Roach     * @return void
622a6f13a4aSGreg Roach     */
623c1010edaSGreg Roach    private function totalPagesStartHandler()
624c1010edaSGreg Roach    {
6257a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
626a6f13a4aSGreg Roach    }
627a6f13a4aSGreg Roach
628a6f13a4aSGreg Roach    /**
629a6f13a4aSGreg Roach     * Called at the start of an element.
630a6f13a4aSGreg Roach     *
631c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6328ba2e626SGreg Roach     *
6338ba2e626SGreg Roach     * @return void
634a6f13a4aSGreg Roach     */
635c0fe75acSGreg Roach    private function gedcomStartHandler(array $attrs)
636c1010edaSGreg Roach    {
637a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
638a6f13a4aSGreg Roach            $this->process_gedcoms++;
639a6f13a4aSGreg Roach
640a6f13a4aSGreg Roach            return;
641a6f13a4aSGreg Roach        }
642a6f13a4aSGreg Roach
643a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6447a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6457a6ee1acSGreg Roach        $tags      = explode(':', $tag);
646a6f13a4aSGreg Roach        $newgedrec = '';
647a6f13a4aSGreg Roach        if (count($tags) < 2) {
648299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
649299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
650a6f13a4aSGreg Roach        }
651a6f13a4aSGreg Roach        if (empty($newgedrec)) {
652a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
653a6f13a4aSGreg Roach            $newgedrec = '';
654a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6557a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
656d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
657d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
658a6f13a4aSGreg Roach                    } else {
659299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
660299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
661a6f13a4aSGreg Roach                    }
662a6f13a4aSGreg Roach                } else {
6637a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66413abd6f3SGreg Roach                        $gmatch = [];
665a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
666299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
667299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
668a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
669a6f13a4aSGreg Roach                        } else {
670a6f13a4aSGreg Roach                            $newgedrec = '';
671a6f13a4aSGreg Roach                            break;
672a6f13a4aSGreg Roach                        }
673a6f13a4aSGreg Roach                    } else {
6747a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
675a6f13a4aSGreg Roach                        $level     = $temp[0] + 1;
6763d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
677a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
678a6f13a4aSGreg Roach                    }
679a6f13a4aSGreg Roach                }
680a6f13a4aSGreg Roach            }
681a6f13a4aSGreg Roach        }
682a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6839b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
684a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
685a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
686a6f13a4aSGreg Roach                $this->fact = $match[2];
687a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
688a6f13a4aSGreg Roach            }
689a6f13a4aSGreg Roach        } else {
690a6f13a4aSGreg Roach            $this->process_gedcoms++;
691a6f13a4aSGreg Roach        }
692a6f13a4aSGreg Roach    }
693a6f13a4aSGreg Roach
694a6f13a4aSGreg Roach    /**
695a6f13a4aSGreg Roach     * Called at the end of an element.
6968ba2e626SGreg Roach     *
6978ba2e626SGreg Roach     * @return void
698a6f13a4aSGreg Roach     */
699c1010edaSGreg Roach    private function gedcomEndHandler()
700c1010edaSGreg Roach    {
701a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
702a6f13a4aSGreg Roach            $this->process_gedcoms--;
703a6f13a4aSGreg Roach        } else {
704a6f13a4aSGreg Roach            list($this->gedrec, $this->fact, $this->desc) = array_pop($this->gedrec_stack);
705a6f13a4aSGreg Roach        }
706a6f13a4aSGreg Roach    }
707a6f13a4aSGreg Roach
708a6f13a4aSGreg Roach    /**
70976692c8bSGreg Roach     * XML <textBoxStartHandler>
710a6f13a4aSGreg Roach     *
711c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7128ba2e626SGreg Roach     *
7138ba2e626SGreg Roach     * @return void
714a6f13a4aSGreg Roach     */
715c0fe75acSGreg Roach    private function textBoxStartHandler(array $attrs)
716c1010edaSGreg Roach    {
717a6f13a4aSGreg Roach        // string Background color code
7187a6ee1acSGreg Roach        $bgcolor = '';
719a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
720a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
721a6f13a4aSGreg Roach        }
722a6f13a4aSGreg Roach
723a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
724a6f13a4aSGreg Roach        $fill = true;
725a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7267a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
727a6f13a4aSGreg Roach                $fill = false;
7287a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
729a6f13a4aSGreg Roach                $fill = true;
730a6f13a4aSGreg Roach            }
731a6f13a4aSGreg Roach        }
732a6f13a4aSGreg Roach
733a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
734a6f13a4aSGreg Roach        $border = false;
735a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7367a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
737a6f13a4aSGreg Roach                $border = true;
7387a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
739a6f13a4aSGreg Roach                $border = false;
740a6f13a4aSGreg Roach            }
741a6f13a4aSGreg Roach        }
742a6f13a4aSGreg Roach
743a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
744a6f13a4aSGreg Roach        $height = 0;
745a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
746a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
747a6f13a4aSGreg Roach        }
748a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
749a6f13a4aSGreg Roach        $width = 0;
750a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
751a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
752a6f13a4aSGreg Roach        }
753a6f13a4aSGreg Roach
754a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
755c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
756a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7577a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
758c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
759a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
760a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7617a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
762a6f13a4aSGreg Roach                $left = 0;
763a6f13a4aSGreg Roach            }
764a6f13a4aSGreg Roach        }
765a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
766c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
767a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7687a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
769c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
770a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
771a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7727a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
773a6f13a4aSGreg Roach                $top = 0;
774a6f13a4aSGreg Roach            }
775a6f13a4aSGreg Roach        }
776a6f13a4aSGreg 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
777a6f13a4aSGreg Roach        $newline = false;
778a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7797a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
780a6f13a4aSGreg Roach                $newline = true;
7817a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
782a6f13a4aSGreg Roach                $newline = false;
783a6f13a4aSGreg Roach            }
784a6f13a4aSGreg Roach        }
785a6f13a4aSGreg Roach        // boolean
786a6f13a4aSGreg Roach        $pagecheck = true;
787a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7887a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
789a6f13a4aSGreg Roach                $pagecheck = false;
7907a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
791a6f13a4aSGreg Roach                $pagecheck = true;
792a6f13a4aSGreg Roach            }
793a6f13a4aSGreg Roach        }
794a6f13a4aSGreg Roach        // boolean Cell padding
795a6f13a4aSGreg Roach        $padding = true;
796a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
7977a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
798a6f13a4aSGreg Roach                $padding = false;
7997a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
800a6f13a4aSGreg Roach                $padding = true;
801a6f13a4aSGreg Roach            }
802a6f13a4aSGreg Roach        }
803a6f13a4aSGreg Roach        // boolean Reset this box Height
804a6f13a4aSGreg Roach        $reseth = false;
805a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8067a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
807a6f13a4aSGreg Roach                $reseth = true;
8087a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
809a6f13a4aSGreg Roach                $reseth = false;
810a6f13a4aSGreg Roach            }
811a6f13a4aSGreg Roach        }
812a6f13a4aSGreg Roach
813a6f13a4aSGreg Roach        // string Style of rendering
8147a6ee1acSGreg Roach        $style = '';
815a6f13a4aSGreg Roach
8169b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
817a6f13a4aSGreg Roach        $this->print_data         = false;
818a6f13a4aSGreg Roach
8199b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
820e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
821a6f13a4aSGreg Roach            $width,
822a6f13a4aSGreg Roach            $height,
823a6f13a4aSGreg Roach            $border,
824a6f13a4aSGreg Roach            $bgcolor,
825a6f13a4aSGreg Roach            $newline,
826a6f13a4aSGreg Roach            $left,
827a6f13a4aSGreg Roach            $top,
828a6f13a4aSGreg Roach            $pagecheck,
829a6f13a4aSGreg Roach            $style,
830a6f13a4aSGreg Roach            $fill,
831a6f13a4aSGreg Roach            $padding,
832a6f13a4aSGreg Roach            $reseth
833a6f13a4aSGreg Roach        );
834a6f13a4aSGreg Roach    }
835a6f13a4aSGreg Roach
836a6f13a4aSGreg Roach    /**
83776692c8bSGreg Roach     * XML <textBoxEndHandler>
8388ba2e626SGreg Roach     *
8398ba2e626SGreg Roach     * @return void
840a6f13a4aSGreg Roach     */
841c1010edaSGreg Roach    private function textBoxEndHandler()
842c1010edaSGreg Roach    {
843a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
844e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
845e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
846e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
847a6f13a4aSGreg Roach    }
848a6f13a4aSGreg Roach
849a6f13a4aSGreg Roach    /**
85076692c8bSGreg Roach     * XLM <Text>.
85176692c8bSGreg Roach     *
852c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8538ba2e626SGreg Roach     *
8548ba2e626SGreg Roach     * @return void
855a6f13a4aSGreg Roach     */
856c0fe75acSGreg Roach    private function textStartHandler(array $attrs)
857c1010edaSGreg Roach    {
8589b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
859a6f13a4aSGreg Roach        $this->print_data         = true;
860a6f13a4aSGreg Roach
861a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8627a6ee1acSGreg Roach        $style = '';
863a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
864a6f13a4aSGreg Roach            $style = $attrs['style'];
865a6f13a4aSGreg Roach        }
866a6f13a4aSGreg Roach
867a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8687a6ee1acSGreg Roach        $color = '';
869a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
870a6f13a4aSGreg Roach            $color = $attrs['color'];
871a6f13a4aSGreg Roach        }
872a6f13a4aSGreg Roach
873e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
874a6f13a4aSGreg Roach    }
875a6f13a4aSGreg Roach
876a6f13a4aSGreg Roach    /**
87776692c8bSGreg Roach     * XML </Text>
8788ba2e626SGreg Roach     *
8798ba2e626SGreg Roach     * @return void
880a6f13a4aSGreg Roach     */
881c1010edaSGreg Roach    private function textEndHandler()
882c1010edaSGreg Roach    {
883a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
884e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
885a6f13a4aSGreg Roach    }
886a6f13a4aSGreg Roach
887a6f13a4aSGreg Roach    /**
88876692c8bSGreg Roach     * XML <GetPersonName/>
889a6f13a4aSGreg Roach     * Get the name
890a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
891a6f13a4aSGreg Roach     * 2. id is set with a record id
892a6f13a4aSGreg Roach     *
893c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8948ba2e626SGreg Roach     *
8958ba2e626SGreg Roach     * @return void
896a6f13a4aSGreg Roach     */
897c0fe75acSGreg Roach    private function getPersonNameStartHandler(array $attrs)
898c1010edaSGreg Roach    {
8997a6ee1acSGreg Roach        $id    = '';
90013abd6f3SGreg Roach        $match = [];
901a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9027a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
903a6f13a4aSGreg Roach                $id = $match[1];
904a6f13a4aSGreg Roach            }
905a6f13a4aSGreg Roach        } else {
9067a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
907d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
908d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
909a6f13a4aSGreg Roach                }
910a6f13a4aSGreg Roach            } else {
9117a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91213abd6f3SGreg Roach                    $gmatch = [];
913a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
914a6f13a4aSGreg Roach                        $id = $gmatch[1];
915a6f13a4aSGreg Roach                    }
916a6f13a4aSGreg Roach                } else {
917a6f13a4aSGreg Roach                    $id = $attrs['id'];
918a6f13a4aSGreg Roach                }
919a6f13a4aSGreg Roach            }
920a6f13a4aSGreg Roach        }
921a6f13a4aSGreg Roach        if (!empty($id)) {
922299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9238f038c36SRico Sonntag            if ($record === null) {
924a6f13a4aSGreg Roach                return;
925a6f13a4aSGreg Roach            }
926a6f13a4aSGreg Roach            if (!$record->canShowName()) {
927a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
928a6f13a4aSGreg Roach            } else {
929a6f13a4aSGreg Roach                $name = $record->getFullName();
930a6f13a4aSGreg Roach                $name = preg_replace(
931c1010edaSGreg Roach                    [
932c1010edaSGreg Roach                        '/<span class="starredname">/',
933c1010edaSGreg Roach                        '/<\/span><\/span>/',
934c1010edaSGreg Roach                        '/<\/span>/',
935c1010edaSGreg Roach                    ],
936c1010edaSGreg Roach                    [
937c1010edaSGreg Roach                        '«',
938c1010edaSGreg Roach                        '',
939c1010edaSGreg Roach                        '»',
940c1010edaSGreg Roach                    ],
941a6f13a4aSGreg Roach                    $name
942a6f13a4aSGreg Roach                );
943a6f13a4aSGreg Roach                $name = strip_tags($name);
944a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
945a6f13a4aSGreg Roach                    if (mb_strlen($name) > $attrs['truncate']) {
946faffe0b0SGreg Roach                        $name = mb_substr($name, 0, $attrs['truncate'] - 1) . '…';
947a6f13a4aSGreg Roach                    }
948a6f13a4aSGreg Roach                } else {
949a6f13a4aSGreg Roach                    $addname = $record->getAddName();
950a6f13a4aSGreg Roach                    $addname = preg_replace(
951c1010edaSGreg Roach                        [
952c1010edaSGreg Roach                            '/<span class="starredname">/',
953c1010edaSGreg Roach                            '/<\/span><\/span>/',
954c1010edaSGreg Roach                            '/<\/span>/',
955c1010edaSGreg Roach                        ],
956c1010edaSGreg Roach                        [
957c1010edaSGreg Roach                            '«',
958c1010edaSGreg Roach                            '',
959c1010edaSGreg Roach                            '»',
960c1010edaSGreg Roach                        ],
961a6f13a4aSGreg Roach                        $addname
962a6f13a4aSGreg Roach                    );
963a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
964a6f13a4aSGreg Roach                    if (!empty($addname)) {
9657a6ee1acSGreg Roach                        $name .= ' ' . $addname;
966a6f13a4aSGreg Roach                    }
967a6f13a4aSGreg Roach                }
968a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
969a6f13a4aSGreg Roach            }
970a6f13a4aSGreg Roach        }
971a6f13a4aSGreg Roach    }
972a6f13a4aSGreg Roach
973a6f13a4aSGreg Roach    /**
97476692c8bSGreg Roach     * XML <GedcomValue/>
975a6f13a4aSGreg Roach     *
976c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9778ba2e626SGreg Roach     *
9788ba2e626SGreg Roach     * @return void
979a6f13a4aSGreg Roach     */
980c0fe75acSGreg Roach    private function gedcomValueStartHandler(array $attrs)
981c1010edaSGreg Roach    {
9827a6ee1acSGreg Roach        $id    = '';
98313abd6f3SGreg Roach        $match = [];
9847a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
985a6f13a4aSGreg Roach            $id = $match[1];
986a6f13a4aSGreg Roach        }
987a6f13a4aSGreg Roach
988044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9897a6ee1acSGreg Roach            $useBreak = '1';
990a6f13a4aSGreg Roach        } else {
9917a6ee1acSGreg Roach            $useBreak = '0';
992a6f13a4aSGreg Roach        }
993a6f13a4aSGreg Roach
994a6f13a4aSGreg Roach        $tag = $attrs['tag'];
995a6f13a4aSGreg Roach        if (!empty($tag)) {
996044416d2SGreg Roach            if ($tag === '@desc') {
997a6f13a4aSGreg Roach                $value = $this->desc;
998a6f13a4aSGreg Roach                $value = trim($value);
999a6f13a4aSGreg Roach                $this->current_element->addText($value);
1000a6f13a4aSGreg Roach            }
1001044416d2SGreg Roach            if ($tag === '@id') {
1002a6f13a4aSGreg Roach                $this->current_element->addText($id);
1003a6f13a4aSGreg Roach            } else {
10047a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1005a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
10067a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
1007a6f13a4aSGreg Roach                    $level = $temp[0];
1008a6f13a4aSGreg Roach                    if ($level == 0) {
1009a6f13a4aSGreg Roach                        $level++;
1010a6f13a4aSGreg Roach                    }
1011a6f13a4aSGreg Roach                } else {
1012a6f13a4aSGreg Roach                    $level = $attrs['level'];
1013a6f13a4aSGreg Roach                }
1014a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10153d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1016a6f13a4aSGreg Roach                switch (end($tags)) {
1017a6f13a4aSGreg Roach                    case 'DATE':
1018a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1019a6f13a4aSGreg Roach                        $value = $tmp->display();
1020a6f13a4aSGreg Roach                        break;
1021a6f13a4aSGreg Roach                    case 'PLAC':
1022299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1023a6f13a4aSGreg Roach                        $value = $tmp->getShortName();
1024a6f13a4aSGreg Roach                        break;
1025a6f13a4aSGreg Roach                }
1026044416d2SGreg Roach                if ($useBreak === '1') {
1027a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1028a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1029a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1030a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1031a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
1032044416d2SGreg Roach                    if (substr($value, 0, 6) === '<br>') {
1033a6f13a4aSGreg Roach                        $value = substr($value, 6);
1034a6f13a4aSGreg Roach                    }
1035a6f13a4aSGreg Roach                }
1036d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1037c1010edaSGreg Roach                if (in_array(end($tmp), [
1038c1010edaSGreg Roach                    'NOTE',
1039c1010edaSGreg Roach                    'TEXT',
1040c1010edaSGreg Roach                ])) {
1041299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1042a4d703aeSGreg Roach                }
1043a6f13a4aSGreg Roach                $this->current_element->addText($value);
1044a6f13a4aSGreg Roach            }
1045a6f13a4aSGreg Roach        }
1046a6f13a4aSGreg Roach    }
1047a6f13a4aSGreg Roach
1048a6f13a4aSGreg Roach    /**
104976692c8bSGreg Roach     * XML <RepeatTag>
1050a6f13a4aSGreg Roach     *
1051c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10528ba2e626SGreg Roach     *
10538ba2e626SGreg Roach     * @return void
1054a6f13a4aSGreg Roach     */
1055c0fe75acSGreg Roach    private function repeatTagStartHandler(array $attrs)
1056c1010edaSGreg Roach    {
1057a6f13a4aSGreg Roach        $this->process_repeats++;
1058a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1059a6f13a4aSGreg Roach            return;
1060a6f13a4aSGreg Roach        }
1061a6f13a4aSGreg Roach
10629b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
106313abd6f3SGreg Roach        $this->repeats         = [];
1064e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1065a6f13a4aSGreg Roach
10667a6ee1acSGreg Roach        $tag = '';
1067a6f13a4aSGreg Roach        if (isset($attrs['tag'])) {
1068a6f13a4aSGreg Roach            $tag = $attrs['tag'];
1069a6f13a4aSGreg Roach        }
1070a6f13a4aSGreg Roach        if (!empty($tag)) {
1071044416d2SGreg Roach            if ($tag === '@desc') {
1072a6f13a4aSGreg Roach                $value = $this->desc;
1073a6f13a4aSGreg Roach                $value = trim($value);
1074a6f13a4aSGreg Roach                $this->current_element->addText($value);
1075a6f13a4aSGreg Roach            } else {
10767a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10777a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10787a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1079a6f13a4aSGreg Roach                $level = $temp[0];
1080a6f13a4aSGreg Roach                if ($level == 0) {
1081a6f13a4aSGreg Roach                    $level++;
1082a6f13a4aSGreg Roach                }
1083a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1084a6f13a4aSGreg Roach                $t      = $tag;
1085a6f13a4aSGreg Roach                $count  = count($tags);
1086a6f13a4aSGreg Roach                $i      = 0;
1087a6f13a4aSGreg Roach                while ($i < $count) {
1088a6f13a4aSGreg Roach                    $t = $tags[$i];
1089a6f13a4aSGreg Roach                    if (!empty($t)) {
1090a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10913d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1092a6f13a4aSGreg Roach                            if (empty($subrec)) {
1093a6f13a4aSGreg Roach                                $level--;
10943d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1095a6f13a4aSGreg Roach                                if (empty($subrec)) {
1096a6f13a4aSGreg Roach                                    return;
1097a6f13a4aSGreg Roach                                }
1098a6f13a4aSGreg Roach                            }
1099a6f13a4aSGreg Roach                        }
1100a6f13a4aSGreg Roach                        $level++;
1101a6f13a4aSGreg Roach                    }
1102a6f13a4aSGreg Roach                    $i++;
1103a6f13a4aSGreg Roach                }
1104a6f13a4aSGreg Roach                $level--;
1105a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1106a6f13a4aSGreg Roach                $i     = 0;
1107a6f13a4aSGreg Roach                while ($i < $count) {
1108a6f13a4aSGreg Roach                    $i++;
1109a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1110a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
1111a9007102SGreg Roach                    if (preg_match('/^\d ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1112299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1113a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1114a9007102SGreg Roach                            continue;
1115a9007102SGreg Roach                        }
1116a9007102SGreg Roach                    }
1117a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1118a6f13a4aSGreg Roach                }
1119a6f13a4aSGreg Roach            }
1120a6f13a4aSGreg Roach        }
1121a6f13a4aSGreg Roach    }
1122a6f13a4aSGreg Roach
1123a6f13a4aSGreg Roach    /**
112476692c8bSGreg Roach     * XML </ RepeatTag>
11258ba2e626SGreg Roach     *
11268ba2e626SGreg Roach     * @return void
1127a6f13a4aSGreg Roach     */
1128c1010edaSGreg Roach    private function repeatTagEndHandler()
1129c1010edaSGreg Roach    {
1130a6f13a4aSGreg Roach        $this->process_repeats--;
1131a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1132a6f13a4aSGreg Roach            return;
1133a6f13a4aSGreg Roach        }
1134a6f13a4aSGreg Roach
1135a6f13a4aSGreg Roach        // Check if there is anything to repeat
1136a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1137a6f13a4aSGreg Roach            // No need to load them if not used...
1138a6f13a4aSGreg Roach
1139a6f13a4aSGreg Roach            $lineoffset = 0;
1140a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1141a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1142a6f13a4aSGreg Roach            }
1143a6f13a4aSGreg Roach            //-- read the xml from the file
1144299d100dSGreg Roach            $lines = file($this->report);
11457a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1146a6f13a4aSGreg Roach                $lineoffset--;
1147a6f13a4aSGreg Roach            }
1148a6f13a4aSGreg Roach            $lineoffset++;
1149a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1150a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1151a6f13a4aSGreg Roach            // RepeatTag Level counter
1152a6f13a4aSGreg Roach            $count = 1;
1153a6f13a4aSGreg Roach            while (0 < $count) {
11547a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1155a6f13a4aSGreg Roach                    $count++;
11567a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1157a6f13a4aSGreg Roach                    $count--;
1158a6f13a4aSGreg Roach                }
1159a6f13a4aSGreg Roach                if (0 < $count) {
1160a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1161a6f13a4aSGreg Roach                }
1162a6f13a4aSGreg Roach                $line_nr++;
1163a6f13a4aSGreg Roach            }
1164a6f13a4aSGreg Roach            // No need to drag this
1165a6f13a4aSGreg Roach            unset($lines);
1166a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1167a6f13a4aSGreg Roach            // Save original values
11689b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1169a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1170a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1171a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1172a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1173e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1174a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11751aa04befSGreg Roach
11761aa04befSGreg Roach                xml_set_element_handler(
11771aa04befSGreg Roach                    $repeat_parser,
11781aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
11791aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11801aa04befSGreg Roach                    },
11811aa04befSGreg Roach                    function ($parser, string $name) {
11821aa04befSGreg Roach                        $this->endElement($parser, $name);
11831aa04befSGreg Roach                    }
11841aa04befSGreg Roach                );
11851aa04befSGreg Roach
11861aa04befSGreg Roach                xml_set_character_data_handler(
11871aa04befSGreg Roach                    $repeat_parser,
11881aa04befSGreg Roach                    function ($parser, $data) {
11891aa04befSGreg Roach                        $this->characterData($parser, $data);
11901aa04befSGreg Roach                    }
11911aa04befSGreg Roach                );
11921aa04befSGreg Roach
1193a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1194a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1195a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1196a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1197a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1198a6f13a4aSGreg Roach                    ));
1199a6f13a4aSGreg Roach                }
1200a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1201a6f13a4aSGreg Roach            }
1202a6f13a4aSGreg Roach            // Restore original values
1203a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1204e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1205a6f13a4aSGreg Roach        }
1206a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1207a6f13a4aSGreg Roach    }
1208a6f13a4aSGreg Roach
1209a6f13a4aSGreg Roach    /**
1210a6f13a4aSGreg Roach     * Variable lookup
1211a6f13a4aSGreg Roach     * Retrieve predefined variables :
1212a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1213a6f13a4aSGreg Roach     *        1 EVEN This is a description
1214a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1215a6f13a4aSGreg Roach     * $ I18N::translate('....')
1216a6f13a4aSGreg Roach     * $ language_settings[]
1217a6f13a4aSGreg Roach     *
1218c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12198ba2e626SGreg Roach     *
12208ba2e626SGreg Roach     * @return void
1221a6f13a4aSGreg Roach     */
1222c0fe75acSGreg Roach    private function varStartHandler(array $attrs)
1223c1010edaSGreg Roach    {
1224a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
1225e8e7866bSGreg 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));
1226a6f13a4aSGreg Roach        }
1227a6f13a4aSGreg Roach
1228a6f13a4aSGreg Roach        $var = $attrs['var'];
1229a6f13a4aSGreg Roach        // SetVar element preset variables
1230d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1231d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1232a6f13a4aSGreg Roach        } else {
1233a6f13a4aSGreg Roach            $tfact = $this->fact;
12347a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1235a6f13a4aSGreg Roach                // Use :
1236a6f13a4aSGreg Roach                // n TYPE This text if string
1237a6f13a4aSGreg Roach                $tfact = $this->type;
1238a6f13a4aSGreg Roach            }
1239c1010edaSGreg Roach            $var = str_replace([
1240c1010edaSGreg Roach                '@fact',
1241c1010edaSGreg Roach                '@desc',
1242c1010edaSGreg Roach            ], [
1243c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1244c1010edaSGreg Roach                $this->desc,
1245c1010edaSGreg Roach            ], $var);
1246a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1247da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1248a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1249a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1250a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1251a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1252a6f13a4aSGreg Roach            }
1253a6f13a4aSGreg Roach        }
1254a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1255a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12567a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1257a6f13a4aSGreg Roach                $g   = new Date($var);
1258a6f13a4aSGreg Roach                $var = $g->display();
1259a6f13a4aSGreg Roach            }
1260a6f13a4aSGreg Roach        }
1261a6f13a4aSGreg Roach        $this->current_element->addText($var);
12622836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1263a6f13a4aSGreg Roach    }
1264a6f13a4aSGreg Roach
1265a6f13a4aSGreg Roach    /**
126676692c8bSGreg Roach     * XML <Facts>
126776692c8bSGreg Roach     *
1268c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12698ba2e626SGreg Roach     *
12708ba2e626SGreg Roach     * @return void
1271a6f13a4aSGreg Roach     */
1272c0fe75acSGreg Roach    private function factsStartHandler(array $attrs)
1273c1010edaSGreg Roach    {
1274a6f13a4aSGreg Roach        $this->process_repeats++;
1275a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1276a6f13a4aSGreg Roach            return;
1277a6f13a4aSGreg Roach        }
1278a6f13a4aSGreg Roach
12799b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
128013abd6f3SGreg Roach        $this->repeats         = [];
1281e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1282a6f13a4aSGreg Roach
12837a6ee1acSGreg Roach        $id    = '';
128413abd6f3SGreg Roach        $match = [];
12857a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1286a6f13a4aSGreg Roach            $id = $match[1];
1287a6f13a4aSGreg Roach        }
12887a6ee1acSGreg Roach        $tag = '';
1289a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1290a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1291a6f13a4aSGreg Roach        }
12927a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1293d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1294a6f13a4aSGreg Roach        }
1295a6f13a4aSGreg Roach
1296299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1297a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1298a6f13a4aSGreg Roach            $facts = $record->getFacts();
12993d7a8a4cSGreg Roach            Functions::sortFacts($facts);
130013abd6f3SGreg Roach            $this->repeats = [];
1301a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1302a6f13a4aSGreg Roach            foreach ($facts as $event) {
1303a6f13a4aSGreg Roach                if (!in_array($event->getTag(), $nonfacts)) {
1304138ca96cSGreg Roach                    $this->repeats[] = $event->gedcom();
1305a6f13a4aSGreg Roach                }
1306a6f13a4aSGreg Roach            }
1307a6f13a4aSGreg Roach        } else {
1308a6f13a4aSGreg Roach            foreach ($record->getFacts() as $fact) {
1309a6f13a4aSGreg Roach                if ($fact->isPendingAddition() && $fact->getTag() !== 'CHAN') {
1310138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1311a6f13a4aSGreg Roach                }
1312a6f13a4aSGreg Roach            }
1313a6f13a4aSGreg Roach        }
1314a6f13a4aSGreg Roach    }
1315a6f13a4aSGreg Roach
1316a6f13a4aSGreg Roach    /**
131776692c8bSGreg Roach     * XML </Facts>
13188ba2e626SGreg Roach     *
13198ba2e626SGreg Roach     * @return void
1320a6f13a4aSGreg Roach     */
1321c1010edaSGreg Roach    private function factsEndHandler()
1322c1010edaSGreg Roach    {
1323a6f13a4aSGreg Roach        $this->process_repeats--;
1324a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1325a6f13a4aSGreg Roach            return;
1326a6f13a4aSGreg Roach        }
1327a6f13a4aSGreg Roach
1328a6f13a4aSGreg Roach        // Check if there is anything to repeat
1329a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1330e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1331a6f13a4aSGreg Roach            $lineoffset = 0;
1332a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1333a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1334a6f13a4aSGreg Roach            }
1335a6f13a4aSGreg Roach
1336a6f13a4aSGreg Roach            //-- read the xml from the file
1337299d100dSGreg Roach            $lines = file($this->report);
1338a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1339a6f13a4aSGreg Roach                $lineoffset--;
1340a6f13a4aSGreg Roach            }
1341a6f13a4aSGreg Roach            $lineoffset++;
1342a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1343a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1344a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1345a6f13a4aSGreg Roach            while ($line_nr < $i) {
1346a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1347a6f13a4aSGreg Roach                $line_nr++;
1348a6f13a4aSGreg Roach            }
1349a6f13a4aSGreg Roach            // No need to drag this
1350a6f13a4aSGreg Roach            unset($lines);
1351a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1352a6f13a4aSGreg Roach            // Save original values
13539b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1354a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1355a6f13a4aSGreg Roach            $count                = count($this->repeats);
1356a6f13a4aSGreg Roach            $i                    = 0;
1357a6f13a4aSGreg Roach            while ($i < $count) {
1358a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1359a6f13a4aSGreg Roach                $this->fact   = '';
1360a6f13a4aSGreg Roach                $this->desc   = '';
1361a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1362a6f13a4aSGreg Roach                    $this->fact = $match[1];
1363a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
136413abd6f3SGreg Roach                        $tmatch = [];
1365a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1366a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1367a6f13a4aSGreg Roach                        } else {
1368a6f13a4aSGreg Roach                            $this->type = ' ';
1369a6f13a4aSGreg Roach                        }
1370a6f13a4aSGreg Roach                    }
1371a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13723d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1373a6f13a4aSGreg Roach                }
1374a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1375e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1376a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13771aa04befSGreg Roach
13781aa04befSGreg Roach                xml_set_element_handler(
13791aa04befSGreg Roach                    $repeat_parser,
13801aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
13811aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13821aa04befSGreg Roach                    },
13831aa04befSGreg Roach                    function ($parser, string $name) {
13841aa04befSGreg Roach                        $this->endElement($parser, $name);
13851aa04befSGreg Roach                    }
13861aa04befSGreg Roach                );
13871aa04befSGreg Roach
13881aa04befSGreg Roach                xml_set_character_data_handler(
13891aa04befSGreg Roach                    $repeat_parser,
13901aa04befSGreg Roach                    function ($parser, $data) {
13911aa04befSGreg Roach                        $this->characterData($parser, $data);
13921aa04befSGreg Roach                    }
13931aa04befSGreg Roach                );
13941aa04befSGreg Roach
1395a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1396a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1397a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1398a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1399a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1400a6f13a4aSGreg Roach                    ));
1401a6f13a4aSGreg Roach                }
1402a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1403a6f13a4aSGreg Roach                $i++;
1404a6f13a4aSGreg Roach            }
1405a6f13a4aSGreg Roach            // Restore original values
1406e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1407a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1408a6f13a4aSGreg Roach        }
1409a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1410a6f13a4aSGreg Roach    }
1411a6f13a4aSGreg Roach
1412a6f13a4aSGreg Roach    /**
1413a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1414d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1415a6f13a4aSGreg Roach     *
1416c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14178ba2e626SGreg Roach     *
14188ba2e626SGreg Roach     * @return void
1419a6f13a4aSGreg Roach     */
1420c0fe75acSGreg Roach    private function setVarStartHandler(array $attrs)
1421c1010edaSGreg Roach    {
1422a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
1423a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1424a6f13a4aSGreg Roach        }
1425a6f13a4aSGreg Roach
1426a6f13a4aSGreg Roach        $name  = $attrs['name'];
1427a6f13a4aSGreg Roach        $value = $attrs['value'];
142813abd6f3SGreg Roach        $match = [];
1429a6f13a4aSGreg Roach        // Current GEDCOM record strings
1430044416d2SGreg Roach        if ($value === '@ID') {
14317a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1432a6f13a4aSGreg Roach                $value = $match[1];
1433a6f13a4aSGreg Roach            }
1434044416d2SGreg Roach        } elseif ($value === '@fact') {
1435a6f13a4aSGreg Roach            $value = $this->fact;
1436044416d2SGreg Roach        } elseif ($value === '@desc') {
1437a6f13a4aSGreg Roach            $value = $this->desc;
1438044416d2SGreg Roach        } elseif ($value === '@generation') {
1439589feda3SGreg Roach            $value = (string) $this->generation;
1440a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144113abd6f3SGreg Roach            $gmatch = [];
1442a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14437a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1444a6f13a4aSGreg Roach            }
1445a6f13a4aSGreg Roach        }
1446a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1447d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1448a6f13a4aSGreg Roach        }
1449a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1450a6f13a4aSGreg Roach        $i     = 0;
1451a6f13a4aSGreg Roach        while ($i < $count) {
1452d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14537a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1454a6f13a4aSGreg Roach            $i++;
1455a6f13a4aSGreg Roach        }
1456a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1457da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1458a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1459a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1460a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1461a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1462a6f13a4aSGreg Roach        }
146352868398SGreg Roach
1464a6f13a4aSGreg Roach        // Arithmetic functions
1465a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
146652868398SGreg Roach            // Create an expression language with the functions used by our reports.
146752868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1468c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1469c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
147052868398SGreg Roach
147152868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1472a6f13a4aSGreg Roach        }
147352868398SGreg Roach
14747a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14757a6ee1acSGreg Roach            $value = '';
1476a6f13a4aSGreg Roach        }
1477d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1478a6f13a4aSGreg Roach    }
1479a6f13a4aSGreg Roach
1480a6f13a4aSGreg Roach    /**
1481a6f13a4aSGreg Roach     * XML <if > start element
1482a6f13a4aSGreg Roach     *
1483c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14848ba2e626SGreg Roach     *
14858ba2e626SGreg Roach     * @return void
1486a6f13a4aSGreg Roach     */
1487c0fe75acSGreg Roach    private function ifStartHandler(array $attrs)
1488c1010edaSGreg Roach    {
1489a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1490a6f13a4aSGreg Roach            $this->process_ifs++;
1491a6f13a4aSGreg Roach
1492a6f13a4aSGreg Roach            return;
1493a6f13a4aSGreg Roach        }
1494a6f13a4aSGreg Roach
1495a6f13a4aSGreg Roach        $condition = $attrs['condition'];
149682759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1497c1010edaSGreg Roach        $condition = str_replace([
1498c1010edaSGreg Roach            ' LT ',
1499c1010edaSGreg Roach            ' GT ',
1500c1010edaSGreg Roach        ], [
1501c1010edaSGreg Roach            '<',
1502c1010edaSGreg Roach            '>',
1503c1010edaSGreg Roach        ], $condition);
1504a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15057a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
150613abd6f3SGreg Roach        $match     = [];
1507a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1508a6f13a4aSGreg Roach        $i         = 0;
1509a6f13a4aSGreg Roach        while ($i < $count) {
1510a6f13a4aSGreg Roach            $id    = $match[$i][1];
1511a6f13a4aSGreg Roach            $value = '""';
1512044416d2SGreg Roach            if ($id === 'ID') {
15137a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1514a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1515a6f13a4aSGreg Roach                }
15167a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1517a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15187a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1519a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15207a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1521a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1522a6f13a4aSGreg Roach            } else {
15237a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1524a6f13a4aSGreg Roach                $level = $temp[0];
1525a6f13a4aSGreg Roach                if ($level == 0) {
1526a6f13a4aSGreg Roach                    $level++;
1527a6f13a4aSGreg Roach                }
15283d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1529a6f13a4aSGreg Roach                if (empty($value)) {
1530a6f13a4aSGreg Roach                    $level++;
15313d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1532a6f13a4aSGreg Roach                }
15335e8c88c1SGreg Roach                $value = preg_replace('/^@(' . WT_REGEX_XREF . ')@$/', '$1', $value);
15345e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1535a6f13a4aSGreg Roach            }
1536a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1537a6f13a4aSGreg Roach            $i++;
1538a6f13a4aSGreg Roach        }
15395809450fSGreg Roach
1540cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1541cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1542c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1543c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1544cb63a60eSGreg Roach
1545cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15465809450fSGreg Roach
1547a6f13a4aSGreg Roach        if (!$ret) {
1548a6f13a4aSGreg Roach            $this->process_ifs++;
1549a6f13a4aSGreg Roach        }
1550a6f13a4aSGreg Roach    }
1551a6f13a4aSGreg Roach
1552a6f13a4aSGreg Roach    /**
1553a6f13a4aSGreg Roach     * XML <if /> end element
15548ba2e626SGreg Roach     *
15558ba2e626SGreg Roach     * @return void
1556a6f13a4aSGreg Roach     */
1557c1010edaSGreg Roach    private function ifEndHandler()
1558c1010edaSGreg Roach    {
1559a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1560a6f13a4aSGreg Roach            $this->process_ifs--;
1561a6f13a4aSGreg Roach        }
1562a6f13a4aSGreg Roach    }
1563a6f13a4aSGreg Roach
1564a6f13a4aSGreg Roach    /**
1565a6f13a4aSGreg Roach     * XML <Footnote > start element
1566a6f13a4aSGreg Roach     * Collect the Footnote links
1567a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1568a6f13a4aSGreg Roach     *
1569c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15708ba2e626SGreg Roach     *
15718ba2e626SGreg Roach     * @return void
1572a6f13a4aSGreg Roach     */
1573c0fe75acSGreg Roach    private function footnoteStartHandler(array $attrs)
1574c1010edaSGreg Roach    {
15757a6ee1acSGreg Roach        $id = '';
15767a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1577a6f13a4aSGreg Roach            $id = $match[2];
1578a6f13a4aSGreg Roach        }
1579299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1580a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15819b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1582a6f13a4aSGreg Roach            $this->print_data         = true;
15837a6ee1acSGreg Roach            $style                    = '';
1584a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1585a6f13a4aSGreg Roach                $style = $attrs['style'];
1586a6f13a4aSGreg Roach            }
1587a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1588e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1589a6f13a4aSGreg Roach        } else {
1590a6f13a4aSGreg Roach            $this->print_data       = false;
1591a6f13a4aSGreg Roach            $this->process_footnote = false;
1592a6f13a4aSGreg Roach        }
1593a6f13a4aSGreg Roach    }
1594a6f13a4aSGreg Roach
1595a6f13a4aSGreg Roach    /**
1596a6f13a4aSGreg Roach     * XML <Footnote /> end element
1597a6f13a4aSGreg Roach     * Print the collected Footnote data
15988ba2e626SGreg Roach     *
15998ba2e626SGreg Roach     * @return void
1600a6f13a4aSGreg Roach     */
1601c1010edaSGreg Roach    private function footnoteEndHandler()
1602c1010edaSGreg Roach    {
1603a6f13a4aSGreg Roach        if ($this->process_footnote) {
1604a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1605a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1606a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1607e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1608a6f13a4aSGreg Roach            }
1609a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1610a6f13a4aSGreg Roach        } else {
1611a6f13a4aSGreg Roach            $this->process_footnote = true;
1612a6f13a4aSGreg Roach        }
1613a6f13a4aSGreg Roach    }
1614a6f13a4aSGreg Roach
1615a6f13a4aSGreg Roach    /**
1616a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16178ba2e626SGreg Roach     *
16188ba2e626SGreg Roach     * @return void
1619a6f13a4aSGreg Roach     */
1620c1010edaSGreg Roach    private function footnoteTextsStartHandler()
1621c1010edaSGreg Roach    {
16227a6ee1acSGreg Roach        $temp = 'footnotetexts';
1623e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1624a6f13a4aSGreg Roach    }
1625a6f13a4aSGreg Roach
1626a6f13a4aSGreg Roach    /**
1627a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16288ba2e626SGreg Roach     *
16298ba2e626SGreg Roach     * @return void
1630a6f13a4aSGreg Roach     */
1631c1010edaSGreg Roach    private function brStartHandler()
1632c1010edaSGreg Roach    {
1633a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1634a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1635a6f13a4aSGreg Roach        }
1636a6f13a4aSGreg Roach    }
1637a6f13a4aSGreg Roach
1638a6f13a4aSGreg Roach    /**
1639a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16408ba2e626SGreg Roach     *
16418ba2e626SGreg Roach     * @return void
1642a6f13a4aSGreg Roach     */
1643c1010edaSGreg Roach    private function spStartHandler()
1644c1010edaSGreg Roach    {
1645a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1646a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1647a6f13a4aSGreg Roach        }
1648a6f13a4aSGreg Roach    }
1649a6f13a4aSGreg Roach
1650a6f13a4aSGreg Roach    /**
165176692c8bSGreg Roach     * XML <HighlightedImage/>
165276692c8bSGreg Roach     *
1653c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16548ba2e626SGreg Roach     *
16558ba2e626SGreg Roach     * @return void
1656a6f13a4aSGreg Roach     */
1657c0fe75acSGreg Roach    private function highlightedImageStartHandler(array $attrs)
1658c1010edaSGreg Roach    {
1659a6f13a4aSGreg Roach        $id = '';
16607a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1661a6f13a4aSGreg Roach            $id = $match[1];
1662a6f13a4aSGreg Roach        }
1663a6f13a4aSGreg Roach
1664c21bdddcSGreg Roach        // Position the top corner of this box on the page
1665c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1666a6f13a4aSGreg Roach
1667c21bdddcSGreg Roach        // Position the left corner of this box on the page
1668c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1669a6f13a4aSGreg Roach
167083cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167183cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1672a6f13a4aSGreg Roach
1673a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
167483cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1675a6f13a4aSGreg Roach
167683cdc021SGreg Roach        // Width, height (or both).
1677c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1678c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1679a6f13a4aSGreg Roach
1680299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16814a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
168286a63f51SGreg Roach
168386a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1684c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1685c1010edaSGreg Roach                0,
1686c1010edaSGreg Roach                0,
1687c1010edaSGreg Roach            ];
1688a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16893c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16903c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1691a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16923c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16933c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1694a6f13a4aSGreg Roach            } else {
16953c3b90deSGreg Roach                $width  = $attributes[0];
16963c3b90deSGreg Roach                $height = $attributes[1];
1697a6f13a4aSGreg Roach            }
16984a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1699e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1700a6f13a4aSGreg Roach        }
1701a6f13a4aSGreg Roach    }
1702a6f13a4aSGreg Roach
1703a6f13a4aSGreg Roach    /**
170476692c8bSGreg Roach     * XML <Image/>
170576692c8bSGreg Roach     *
1706c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17078ba2e626SGreg Roach     *
17088ba2e626SGreg Roach     * @return void
1709a6f13a4aSGreg Roach     */
1710c0fe75acSGreg Roach    private function imageStartHandler(array $attrs)
1711c1010edaSGreg Roach    {
171283cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1713c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1714a6f13a4aSGreg Roach
1715a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1716c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1717a6f13a4aSGreg Roach
171883cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
171983cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1720a6f13a4aSGreg Roach
1721a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172283cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1723a6f13a4aSGreg Roach
172483cdc021SGreg Roach        // Width, height (or both).
1725c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1726c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1727a6f13a4aSGreg Roach
172883cdc021SGreg Roach        $file = $attrs['file'] ?? '';
172983cdc021SGreg Roach
1730044416d2SGreg Roach        if ($file === '@FILE') {
173113abd6f3SGreg Roach            $match = [];
1732a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1733299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17344a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1735cdf416fbSGreg Roach
17364a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1737c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1738c1010edaSGreg Roach                        0,
1739c1010edaSGreg Roach                        0,
1740c1010edaSGreg Roach                    ];
1741a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17423c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17433c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1744a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17453c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17463c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1747a6f13a4aSGreg Roach                    } else {
17483c3b90deSGreg Roach                        $width  = $attributes[0];
17493c3b90deSGreg Roach                        $height = $attributes[1];
1750a6f13a4aSGreg Roach                    }
17514a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1752e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1753a6f13a4aSGreg Roach                }
1754a6f13a4aSGreg Roach            }
1755a6f13a4aSGreg Roach        } else {
17567a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1757a6f13a4aSGreg Roach                $size = getimagesize($file);
1758a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1759a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1760a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1761a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1762a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1763a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1764a6f13a4aSGreg Roach                } else {
1765a6f13a4aSGreg Roach                    $width  = $size[0];
1766a6f13a4aSGreg Roach                    $height = $size[1];
1767a6f13a4aSGreg Roach                }
1768e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1769e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1770a6f13a4aSGreg Roach            }
1771a6f13a4aSGreg Roach        }
1772a6f13a4aSGreg Roach    }
1773a6f13a4aSGreg Roach
1774a6f13a4aSGreg Roach    /**
1775a6f13a4aSGreg Roach     * XML <Line> element handler
1776a6f13a4aSGreg Roach     *
1777c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17788ba2e626SGreg Roach     *
17798ba2e626SGreg Roach     * @return void
1780a6f13a4aSGreg Roach     */
1781c0fe75acSGreg Roach    private function lineStartHandler(array $attrs)
1782c1010edaSGreg Roach    {
1783a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1784c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1785a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17867a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1787a6f13a4aSGreg Roach                $x1 = 0;
17887a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1789c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1790a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1791c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1792a6f13a4aSGreg Roach            }
1793a6f13a4aSGreg Roach        }
1794a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1795c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1796a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17977a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1798a6f13a4aSGreg Roach                $y1 = 0;
17997a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1800c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1801a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1802c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1803a6f13a4aSGreg Roach            }
1804a6f13a4aSGreg Roach        }
1805a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1806c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1807a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18087a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1809a6f13a4aSGreg Roach                $x2 = 0;
18107a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1811c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1812a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1813c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1814a6f13a4aSGreg Roach            }
1815a6f13a4aSGreg Roach        }
1816a6f13a4aSGreg Roach        // End vertical position
1817c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1818a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18197a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1820a6f13a4aSGreg Roach                $y2 = 0;
18217a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1822c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1823a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1824c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1825a6f13a4aSGreg Roach            }
1826a6f13a4aSGreg Roach        }
1827a6f13a4aSGreg Roach
1828e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1829e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1830a6f13a4aSGreg Roach    }
1831a6f13a4aSGreg Roach
1832a6f13a4aSGreg Roach    /**
183376692c8bSGreg Roach     * XML <List>
1834a6f13a4aSGreg Roach     *
1835c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18368ba2e626SGreg Roach     *
18378ba2e626SGreg Roach     * @return void
1838a6f13a4aSGreg Roach     */
1839c0fe75acSGreg Roach    private function listStartHandler(array $attrs)
1840c1010edaSGreg Roach    {
1841a6f13a4aSGreg Roach        $this->process_repeats++;
1842a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1843a6f13a4aSGreg Roach            return;
1844a6f13a4aSGreg Roach        }
1845a6f13a4aSGreg Roach
184613abd6f3SGreg Roach        $match = [];
1847a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1848a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1849a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1850d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1851a6f13a4aSGreg Roach                $sortby = trim($sortby);
1852a6f13a4aSGreg Roach            }
1853a6f13a4aSGreg Roach        } else {
18547a6ee1acSGreg Roach            $sortby = 'NAME';
1855a6f13a4aSGreg Roach        }
1856a6f13a4aSGreg Roach
1857a6f13a4aSGreg Roach        if (isset($attrs['list'])) {
1858a6f13a4aSGreg Roach            $listname = $attrs['list'];
1859a6f13a4aSGreg Roach        } else {
18607a6ee1acSGreg Roach            $listname = 'individual';
1861a6f13a4aSGreg Roach        }
1862a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1863a6f13a4aSGreg Roach        switch ($listname) {
18647a6ee1acSGreg Roach            case 'pending':
1865a6f13a4aSGreg Roach                $rows = Database::prepare(
18665d0bc43dSGreg Roach                    "SELECT xref, CASE new_gedcom WHEN '' THEN old_gedcom ELSE new_gedcom END AS gedcom" .
18675d0bc43dSGreg Roach                    " FROM `##change`" . " WHERE (xref, change_id) IN (" .
18685d0bc43dSGreg Roach                    "  SELECT xref, MAX(change_id)" .
18695d0bc43dSGreg Roach                    "  FROM `##change`" .
18705d0bc43dSGreg Roach                    "  WHERE status = 'pending' AND gedcom_id = :tree_id" .
18715d0bc43dSGreg Roach                    "  GROUP BY xref" .
18725d0bc43dSGreg Roach                    " )"
187313abd6f3SGreg Roach                )->execute([
187472cf66d4SGreg Roach                    'tree_id' => $this->tree->id(),
187513abd6f3SGreg Roach                ])->fetchAll();
187613abd6f3SGreg Roach                $this->list = [];
1877a6f13a4aSGreg Roach                foreach ($rows as $row) {
1878299d100dSGreg Roach                    $this->list[] = GedcomRecord::getInstance($row->xref, $this->tree, $row->gedcom);
1879a6f13a4aSGreg Roach                }
1880a6f13a4aSGreg Roach                break;
1881a6f13a4aSGreg Roach            case 'individual':
188276156db1SGreg Roach                $sql_select   = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` ";
1883a6f13a4aSGreg Roach                $sql_join     = "";
1884825006d2SGreg Roach                $sql_where    = " WHERE i_file = :tree_id";
1885a6f13a4aSGreg Roach                $sql_order_by = "";
188672cf66d4SGreg Roach                $sql_params   = ['tree_id' => $this->tree->id()];
1887a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1888a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
188982759250SGreg Roach                        $value = $this->substituteVars($value, false);
1890a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1891a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1892a6f13a4aSGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)";
1893b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
18945d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1895a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
1896044416d2SGreg Roach                            if ($match[2] === 'LTE') {
18975d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
18985d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1899a6f13a4aSGreg Roach                            } else {
19005d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19015d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1902a6f13a4aSGreg Roach                            }
1903a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
1904a6f13a4aSGreg Roach                                $sortby = "";
1905a6f13a4aSGreg Roach                                $sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1";
1906a6f13a4aSGreg Roach                            }
1907a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1908a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
1909a6f13a4aSGreg Roach                            // Do nothing, unless you have to
1910044416d2SGreg Roach                            if ($match[1] != '' || $sortby === 'NAME') {
1911a6f13a4aSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)";
1912a6f13a4aSGreg Roach                                // Search the DB only if there is any name supplied
19137a6ee1acSGreg Roach                                if ($match[1] != '') {
19147a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19155d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19165d0bc43dSGreg Roach                                        $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19175d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
1918a6f13a4aSGreg Roach                                    }
1919a6f13a4aSGreg Roach                                }
1920a6f13a4aSGreg Roach                                // Let the DB do the name sorting even when no name was entered
1921044416d2SGreg Roach                                if ($sortby === 'NAME') {
19227a6ee1acSGreg Roach                                    $sortby = '';
19237a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1924a6f13a4aSGreg Roach                                }
1925a6f13a4aSGreg Roach                            }
1926a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1927a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19285d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom";
1929b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1930b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1931a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1932a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
19334f927915SGreg Roach                            // Don't unset this filter. This is just initial filtering
1934a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)";
1935a6f13a4aSGreg 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)";
19365d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19375d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1938a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19394f927915SGreg Roach                            // Don't unset this filter. This is just initial filtering
19405d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19415d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
19425d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
19435d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
1944a6f13a4aSGreg Roach                        }
1945a6f13a4aSGreg Roach                    }
1946a6f13a4aSGreg Roach                }
1947a6f13a4aSGreg Roach
194813abd6f3SGreg Roach                $this->list = [];
1949a6f13a4aSGreg Roach                $rows       = Database::prepare(
1950a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
19515d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
1952a6f13a4aSGreg Roach
1953a6f13a4aSGreg Roach                foreach ($rows as $row) {
1954299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1955a6f13a4aSGreg Roach                }
1956a6f13a4aSGreg Roach                break;
1957a6f13a4aSGreg Roach
1958a6f13a4aSGreg Roach            case 'family':
195976156db1SGreg Roach                $sql_select   = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`";
1960a6f13a4aSGreg Roach                $sql_join     = "";
1961825006d2SGreg Roach                $sql_where    = " WHERE f_file = :tree_id";
1962a6f13a4aSGreg Roach                $sql_order_by = "";
196372cf66d4SGreg Roach                $sql_params   = ['tree_id' => $this->tree->id()];
1964a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1965a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
196682759250SGreg Roach                        $value = $this->substituteVars($value, false);
1967a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1968a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1969a9eb55f8SGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)";
1970b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
19715d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1972a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
1973044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19745d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19755d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1976a6f13a4aSGreg Roach                            } else {
19775d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19785d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1979a6f13a4aSGreg Roach                            }
1980a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
19817a6ee1acSGreg Roach                                $sortby = '';
19827a6ee1acSGreg Roach                                $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1";
1983a6f13a4aSGreg Roach                            }
1984a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1985a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19865d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom";
1987b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1988b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1989a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1990a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19915d0bc43dSGreg Roach                            // Do nothing, unless you have to
1992044416d2SGreg Roach                            if ($match[1] != '' || $sortby === 'NAME') {
19935d0bc43dSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)";
19945d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
19957a6ee1acSGreg Roach                                if ($match[1] != '') {
19967a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19975d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19985d0bc43dSGreg Roach                                        $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19995d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
20005d0bc43dSGreg Roach                                    }
20015d0bc43dSGreg Roach                                }
20025d0bc43dSGreg Roach                                // Let the DB do the name sorting even when no name was entered
2003044416d2SGreg Roach                                if ($sortby === 'NAME') {
20047a6ee1acSGreg Roach                                    $sortby = '';
20057a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
2006a6f13a4aSGreg Roach                                }
20075d0bc43dSGreg Roach                            }
2008a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
2009a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
2010a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)";
2011a6f13a4aSGreg 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)";
20125d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
20135d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
2014a6f13a4aSGreg Roach                        // Don't unset this filter. This is just initial filtering
2015a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
20165d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
20175d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
20185d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
20195d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
2020a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
2021a6f13a4aSGreg Roach                        }
2022a6f13a4aSGreg Roach                    }
2023a6f13a4aSGreg Roach                }
2024a6f13a4aSGreg Roach
202513abd6f3SGreg Roach                $this->list = [];
2026a6f13a4aSGreg Roach                $rows       = Database::prepare(
2027a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
20285d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
2029a6f13a4aSGreg Roach
2030a6f13a4aSGreg Roach                foreach ($rows as $row) {
2031299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2032a6f13a4aSGreg Roach                }
2033a6f13a4aSGreg Roach                break;
2034a6f13a4aSGreg Roach
2035a6f13a4aSGreg Roach            default:
2036a6f13a4aSGreg Roach                throw new \DomainException('Invalid list name: ' . $listname);
2037a6f13a4aSGreg Roach        }
2038a6f13a4aSGreg Roach
203913abd6f3SGreg Roach        $filters  = [];
204013abd6f3SGreg Roach        $filters2 = [];
2041a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2042a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2043a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2044a6f13a4aSGreg Roach                    $condition = $value;
2045a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2046a6f13a4aSGreg Roach                        $id    = $match[1];
2047a6f13a4aSGreg Roach                        $value = "''";
2048044416d2SGreg Roach                        if ($id === 'ID') {
20497a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2050a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2051a6f13a4aSGreg Roach                            }
2052044416d2SGreg Roach                        } elseif ($id === 'fact') {
2053a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2054044416d2SGreg Roach                        } elseif ($id === 'desc') {
2055a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2056a6f13a4aSGreg Roach                        } else {
2057a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20587a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2059a6f13a4aSGreg Roach                            }
2060a6f13a4aSGreg Roach                        }
2061a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2062a6f13a4aSGreg Roach                    }
2063a6f13a4aSGreg Roach                    //-- handle regular expressions
2064a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2065a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2066a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2067a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2068a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2069d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2070a6f13a4aSGreg Roach                            $val = trim($val);
2071a6f13a4aSGreg Roach                        }
2072a6f13a4aSGreg Roach                        if ($val) {
20737a6ee1acSGreg Roach                            $searchstr = '';
20747a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2075a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2076a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2077a6f13a4aSGreg Roach                                $level = 1;
2078a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2079a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2080a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2081a6f13a4aSGreg Roach                                    }
2082a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2083044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
20847a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2085a6f13a4aSGreg Roach                                    }
20867a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2087a6f13a4aSGreg Roach                                    $level++;
2088a6f13a4aSGreg Roach                                }
2089a6f13a4aSGreg Roach                            } else {
2090044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
20917a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2092a6f13a4aSGreg Roach                                }
2093a6f13a4aSGreg Roach                                $t         = $tag;
20947a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2095a6f13a4aSGreg Roach                            }
2096a6f13a4aSGreg Roach                            switch ($expr) {
20977a6ee1acSGreg Roach                                case 'CONTAINS':
2098044416d2SGreg Roach                                    if ($t === 'PLAC') {
2099a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2100a6f13a4aSGreg Roach                                    } else {
2101a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2102a6f13a4aSGreg Roach                                    }
2103a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2104a6f13a4aSGreg Roach                                    break;
2105a6f13a4aSGreg Roach                                default:
2106c1010edaSGreg Roach                                    $filters2[] = [
2107c1010edaSGreg Roach                                        'tag'  => $tag,
2108c1010edaSGreg Roach                                        'expr' => $expr,
2109c1010edaSGreg Roach                                        'val'  => $val,
2110c1010edaSGreg Roach                                    ];
2111a6f13a4aSGreg Roach                                    break;
2112a6f13a4aSGreg Roach                            }
2113a6f13a4aSGreg Roach                        }
2114a6f13a4aSGreg Roach                    }
2115a6f13a4aSGreg Roach                }
2116a6f13a4aSGreg Roach            }
2117a6f13a4aSGreg Roach        }
2118a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2119a6f13a4aSGreg Roach        if ($filters) {
2120a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2121a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2122299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2123a6f13a4aSGreg Roach                        unset($this->list[$key]);
2124a6f13a4aSGreg Roach                        break;
2125a6f13a4aSGreg Roach                    }
2126a6f13a4aSGreg Roach                }
2127a6f13a4aSGreg Roach            }
2128a6f13a4aSGreg Roach        }
2129a6f13a4aSGreg Roach        if ($filters2) {
213013abd6f3SGreg Roach            $mylist = [];
2131a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2132*c0935879SGreg Roach                $key  = $indi->xref();
2133299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2134a6f13a4aSGreg Roach                $keep = true;
2135a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2136a6f13a4aSGreg Roach                    if ($keep) {
2137a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2138a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2139a6f13a4aSGreg Roach                        $val  = $filter['val'];
2140a6f13a4aSGreg Roach                        if ($val == "''") {
21417a6ee1acSGreg Roach                            $val = '';
2142a6f13a4aSGreg Roach                        }
21437a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2144a6f13a4aSGreg Roach                        $t    = end($tags);
21453d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2146a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2147044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21487a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21497a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2150a6f13a4aSGreg Roach                            $t    = end($tags);
21513d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2152a6f13a4aSGreg Roach                        }
2153a6f13a4aSGreg Roach
2154a6f13a4aSGreg Roach                        switch ($expr) {
21557a6ee1acSGreg Roach                            case 'GTE':
2156044416d2SGreg Roach                                if ($t === 'DATE') {
2157a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2158a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2159a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2160a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2161a6f13a4aSGreg Roach                                    $keep = true;
2162a6f13a4aSGreg Roach                                }
2163a6f13a4aSGreg Roach                                break;
21647a6ee1acSGreg Roach                            case 'LTE':
2165044416d2SGreg Roach                                if ($t === 'DATE') {
2166a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2167a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2168a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2169a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2170a6f13a4aSGreg Roach                                    $keep = true;
2171a6f13a4aSGreg Roach                                }
2172a6f13a4aSGreg Roach                                break;
2173a6f13a4aSGreg Roach                            default:
2174a6f13a4aSGreg Roach                                if ($v == $val) {
2175a6f13a4aSGreg Roach                                    $keep = true;
2176a6f13a4aSGreg Roach                                } else {
2177a6f13a4aSGreg Roach                                    $keep = false;
2178a6f13a4aSGreg Roach                                }
2179a6f13a4aSGreg Roach                                break;
2180a6f13a4aSGreg Roach                        }
2181a6f13a4aSGreg Roach                    }
2182a6f13a4aSGreg Roach                }
2183a6f13a4aSGreg Roach                if ($keep) {
2184a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2185a6f13a4aSGreg Roach                }
2186a6f13a4aSGreg Roach            }
2187a6f13a4aSGreg Roach            $this->list = $mylist;
2188a6f13a4aSGreg Roach        }
2189a6f13a4aSGreg Roach
2190a6f13a4aSGreg Roach        switch ($sortby) {
2191a6f13a4aSGreg Roach            case 'NAME':
2192a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2193a6f13a4aSGreg Roach                break;
2194a6f13a4aSGreg Roach            case 'CHAN':
219518d7a90dSGreg Roach                uasort($this->list, function (GedcomRecord $x, GedcomRecord $y): int {
2196a6f13a4aSGreg Roach                    return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2197a6f13a4aSGreg Roach                });
2198a6f13a4aSGreg Roach                break;
2199a6f13a4aSGreg Roach            case 'BIRT:DATE':
2200a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2201a6f13a4aSGreg Roach                break;
2202a6f13a4aSGreg Roach            case 'DEAT:DATE':
2203a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2204a6f13a4aSGreg Roach                break;
2205a6f13a4aSGreg Roach            case 'MARR:DATE':
22065d0bc43dSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2207a6f13a4aSGreg Roach                break;
2208a6f13a4aSGreg Roach            default:
2209a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2210a6f13a4aSGreg Roach                break;
2211a6f13a4aSGreg Roach        }
2212a6f13a4aSGreg Roach
22139b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2214e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2215a6f13a4aSGreg Roach    }
2216a6f13a4aSGreg Roach
2217a6f13a4aSGreg Roach    /**
221876692c8bSGreg Roach     * XML <List>
22198ba2e626SGreg Roach     *
22208ba2e626SGreg Roach     * @return void
2221a6f13a4aSGreg Roach     */
2222c1010edaSGreg Roach    private function listEndHandler()
2223c1010edaSGreg Roach    {
2224a6f13a4aSGreg Roach        $this->process_repeats--;
2225a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2226a6f13a4aSGreg Roach            return;
2227a6f13a4aSGreg Roach        }
2228a6f13a4aSGreg Roach
2229a6f13a4aSGreg Roach        // Check if there is any list
2230a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2231a6f13a4aSGreg Roach            $lineoffset = 0;
2232a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2233a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2234a6f13a4aSGreg Roach            }
2235a6f13a4aSGreg Roach            //-- read the xml from the file
2236299d100dSGreg Roach            $lines = file($this->report);
22377a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2238a6f13a4aSGreg Roach                $lineoffset--;
2239a6f13a4aSGreg Roach            }
2240a6f13a4aSGreg Roach            $lineoffset++;
2241a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2242a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2243a6f13a4aSGreg Roach            // List Level counter
2244a6f13a4aSGreg Roach            $count = 1;
2245a6f13a4aSGreg Roach            while (0 < $count) {
22467a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2247a6f13a4aSGreg Roach                    $count++;
22487a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2249a6f13a4aSGreg Roach                    $count--;
2250a6f13a4aSGreg Roach                }
2251a6f13a4aSGreg Roach                if (0 < $count) {
2252a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2253a6f13a4aSGreg Roach                }
2254a6f13a4aSGreg Roach                $line_nr++;
2255a6f13a4aSGreg Roach            }
2256a6f13a4aSGreg Roach            // No need to drag this
2257a6f13a4aSGreg Roach            unset($lines);
22587a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2259a6f13a4aSGreg Roach            // Save original values
22609b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2261a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2262a6f13a4aSGreg Roach
2263a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2264a6f13a4aSGreg Roach            $this->list_private = 0;
2265a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2266a6f13a4aSGreg Roach                if ($record->canShow()) {
2267a6f13a4aSGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->getTree()));
2268a6f13a4aSGreg Roach                    //-- start the sax parser
2269a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2270e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2271a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22721aa04befSGreg Roach
22731aa04befSGreg Roach                    xml_set_element_handler(
22741aa04befSGreg Roach                        $repeat_parser,
22751aa04befSGreg Roach                        function ($parser, string $name, array $attrs) {
22761aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
22771aa04befSGreg Roach                        },
22781aa04befSGreg Roach                        function ($parser, string $name) {
22791aa04befSGreg Roach                            $this->endElement($parser, $name);
22801aa04befSGreg Roach                        }
22811aa04befSGreg Roach                    );
22821aa04befSGreg Roach
22831aa04befSGreg Roach                    xml_set_character_data_handler(
22841aa04befSGreg Roach                        $repeat_parser,
22851aa04befSGreg Roach                        function ($parser, $data) {
22861aa04befSGreg Roach                            $this->characterData($parser, $data);
22871aa04befSGreg Roach                        }
22881aa04befSGreg Roach                    );
22891aa04befSGreg Roach
2290a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
2291a6f13a4aSGreg Roach                        throw new \DomainException(sprintf(
2292a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2293a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2294a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2295a6f13a4aSGreg Roach                        ));
2296a6f13a4aSGreg Roach                    }
2297a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2298a6f13a4aSGreg Roach                } else {
2299a6f13a4aSGreg Roach                    $this->list_private++;
2300a6f13a4aSGreg Roach                }
2301a6f13a4aSGreg Roach            }
230213abd6f3SGreg Roach            $this->list   = [];
2303e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2304a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2305a6f13a4aSGreg Roach        }
2306a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2307a6f13a4aSGreg Roach    }
2308a6f13a4aSGreg Roach
2309a6f13a4aSGreg Roach    /**
2310a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2311a6f13a4aSGreg Roach     * Prints the total number of records in a list
2312a6f13a4aSGreg Roach     * The total number is collected from
2313a6f13a4aSGreg Roach     * List and Relatives
23148ba2e626SGreg Roach     *
23158ba2e626SGreg Roach     * @return void
2316a6f13a4aSGreg Roach     */
2317c1010edaSGreg Roach    private function listTotalStartHandler()
2318c1010edaSGreg Roach    {
2319a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2320589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2321a6f13a4aSGreg Roach        } else {
23227a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2323a6f13a4aSGreg Roach        }
2324a6f13a4aSGreg Roach    }
2325a6f13a4aSGreg Roach
2326a6f13a4aSGreg Roach    /**
232776692c8bSGreg Roach     * XML <Relatives>
232876692c8bSGreg Roach     *
2329c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23308ba2e626SGreg Roach     *
23318ba2e626SGreg Roach     * @return void
2332a6f13a4aSGreg Roach     */
2333c0fe75acSGreg Roach    private function relativesStartHandler(array $attrs)
2334c1010edaSGreg Roach    {
2335a6f13a4aSGreg Roach        $this->process_repeats++;
2336a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2337a6f13a4aSGreg Roach            return;
2338a6f13a4aSGreg Roach        }
2339a6f13a4aSGreg Roach
23407a6ee1acSGreg Roach        $sortby = 'NAME';
2341a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
2342a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
2343a6f13a4aSGreg Roach        }
234413abd6f3SGreg Roach        $match = [];
2345a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2346d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2347a6f13a4aSGreg Roach            $sortby = trim($sortby);
2348a6f13a4aSGreg Roach        }
2349a6f13a4aSGreg Roach
2350a6f13a4aSGreg Roach        $maxgen = -1;
2351a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2352a6f13a4aSGreg Roach            $maxgen = $attrs['maxgen'];
2353a6f13a4aSGreg Roach        }
2354044416d2SGreg Roach        if ($maxgen === '*') {
2355a6f13a4aSGreg Roach            $maxgen = -1;
2356a6f13a4aSGreg Roach        }
2357a6f13a4aSGreg Roach
23587a6ee1acSGreg Roach        $group = 'child-family';
2359a6f13a4aSGreg Roach        if (isset($attrs['group'])) {
2360a6f13a4aSGreg Roach            $group = $attrs['group'];
2361a6f13a4aSGreg Roach        }
2362a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2363d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2364a6f13a4aSGreg Roach            $group = trim($group);
2365a6f13a4aSGreg Roach        }
2366a6f13a4aSGreg Roach
23677a6ee1acSGreg Roach        $id = '';
2368a6f13a4aSGreg Roach        if (isset($attrs['id'])) {
2369a6f13a4aSGreg Roach            $id = $attrs['id'];
2370a6f13a4aSGreg Roach        }
2371a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2372d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2373a6f13a4aSGreg Roach            $id = trim($id);
2374a6f13a4aSGreg Roach        }
2375a6f13a4aSGreg Roach
237613abd6f3SGreg Roach        $this->list = [];
2377299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2378a6f13a4aSGreg Roach        if (!empty($person)) {
2379a6f13a4aSGreg Roach            $this->list[$id] = $person;
2380a6f13a4aSGreg Roach            switch ($group) {
23817a6ee1acSGreg Roach                case 'child-family':
2382a6f13a4aSGreg Roach                    foreach ($person->getChildFamilies() as $family) {
2383a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2384a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2385a6f13a4aSGreg Roach                        if (!empty($husband)) {
2386*c0935879SGreg Roach                            $this->list[$husband->xref()] = $husband;
2387a6f13a4aSGreg Roach                        }
2388a6f13a4aSGreg Roach                        if (!empty($wife)) {
2389*c0935879SGreg Roach                            $this->list[$wife->xref()] = $wife;
2390a6f13a4aSGreg Roach                        }
2391a6f13a4aSGreg Roach                        $children = $family->getChildren();
2392a6f13a4aSGreg Roach                        foreach ($children as $child) {
2393a6f13a4aSGreg Roach                            if (!empty($child)) {
2394*c0935879SGreg Roach                                $this->list[$child->xref()] = $child;
2395a6f13a4aSGreg Roach                            }
2396a6f13a4aSGreg Roach                        }
2397a6f13a4aSGreg Roach                    }
2398a6f13a4aSGreg Roach                    break;
23997a6ee1acSGreg Roach                case 'spouse-family':
2400a6f13a4aSGreg Roach                    foreach ($person->getSpouseFamilies() as $family) {
2401a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2402a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2403a6f13a4aSGreg Roach                        if (!empty($husband)) {
2404*c0935879SGreg Roach                            $this->list[$husband->xref()] = $husband;
2405a6f13a4aSGreg Roach                        }
2406a6f13a4aSGreg Roach                        if (!empty($wife)) {
2407*c0935879SGreg Roach                            $this->list[$wife->xref()] = $wife;
2408a6f13a4aSGreg Roach                        }
2409a6f13a4aSGreg Roach                        $children = $family->getChildren();
2410a6f13a4aSGreg Roach                        foreach ($children as $child) {
2411a6f13a4aSGreg Roach                            if (!empty($child)) {
2412*c0935879SGreg Roach                                $this->list[$child->xref()] = $child;
2413a6f13a4aSGreg Roach                            }
2414a6f13a4aSGreg Roach                        }
2415a6f13a4aSGreg Roach                    }
2416a6f13a4aSGreg Roach                    break;
24177a6ee1acSGreg Roach                case 'direct-ancestors':
24183d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2419a6f13a4aSGreg Roach                    break;
24207a6ee1acSGreg Roach                case 'ancestors':
24213d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2422a6f13a4aSGreg Roach                    break;
24237a6ee1acSGreg Roach                case 'descendants':
2424a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24253d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2426a6f13a4aSGreg Roach                    break;
24277a6ee1acSGreg Roach                case 'all':
24283d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24293d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2430a6f13a4aSGreg Roach                    break;
2431a6f13a4aSGreg Roach            }
2432a6f13a4aSGreg Roach        }
2433a6f13a4aSGreg Roach
2434a6f13a4aSGreg Roach        switch ($sortby) {
2435a6f13a4aSGreg Roach            case 'NAME':
2436a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2437a6f13a4aSGreg Roach                break;
2438a6f13a4aSGreg Roach            case 'BIRT:DATE':
2439a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2440a6f13a4aSGreg Roach                break;
2441a6f13a4aSGreg Roach            case 'DEAT:DATE':
2442a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2443a6f13a4aSGreg Roach                break;
2444a6f13a4aSGreg Roach            case 'generation':
244513abd6f3SGreg Roach                $newarray = [];
2446a6f13a4aSGreg Roach                reset($this->list);
2447a6f13a4aSGreg Roach                $genCounter = 1;
2448a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2449a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2450a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2451a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
245279529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2453a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2454a6f13a4aSGreg Roach                        }
2455a6f13a4aSGreg Roach                    }
2456a6f13a4aSGreg Roach                    $genCounter++;
2457a6f13a4aSGreg Roach                }
2458a6f13a4aSGreg Roach                $this->list = $newarray;
2459a6f13a4aSGreg Roach                break;
2460a6f13a4aSGreg Roach            default:
2461a6f13a4aSGreg Roach                // unsorted
2462a6f13a4aSGreg Roach                break;
2463a6f13a4aSGreg Roach        }
24649b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2465e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2466a6f13a4aSGreg Roach    }
2467a6f13a4aSGreg Roach
2468a6f13a4aSGreg Roach    /**
246976692c8bSGreg Roach     * XML </ Relatives>
24708ba2e626SGreg Roach     *
24718ba2e626SGreg Roach     * @return void
2472a6f13a4aSGreg Roach     */
2473c1010edaSGreg Roach    private function relativesEndHandler()
2474c1010edaSGreg Roach    {
2475a6f13a4aSGreg Roach        $this->process_repeats--;
2476a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2477a6f13a4aSGreg Roach            return;
2478a6f13a4aSGreg Roach        }
2479a6f13a4aSGreg Roach
2480a6f13a4aSGreg Roach        // Check if there is any relatives
2481a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2482a6f13a4aSGreg Roach            $lineoffset = 0;
2483a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2484a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2485a6f13a4aSGreg Roach            }
2486a6f13a4aSGreg Roach            //-- read the xml from the file
2487299d100dSGreg Roach            $lines = file($this->report);
24887a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2489a6f13a4aSGreg Roach                $lineoffset--;
2490a6f13a4aSGreg Roach            }
2491a6f13a4aSGreg Roach            $lineoffset++;
2492a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2493a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2494a6f13a4aSGreg Roach            // Relatives Level counter
2495a6f13a4aSGreg Roach            $count = 1;
2496a6f13a4aSGreg Roach            while (0 < $count) {
24977a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2498a6f13a4aSGreg Roach                    $count++;
24997a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2500a6f13a4aSGreg Roach                    $count--;
2501a6f13a4aSGreg Roach                }
2502a6f13a4aSGreg Roach                if (0 < $count) {
2503a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2504a6f13a4aSGreg Roach                }
2505a6f13a4aSGreg Roach                $line_nr++;
2506a6f13a4aSGreg Roach            }
2507a6f13a4aSGreg Roach            // No need to drag this
2508a6f13a4aSGreg Roach            unset($lines);
2509a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2510a6f13a4aSGreg Roach            // Save original values
25119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2512a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2513a6f13a4aSGreg Roach
2514a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2515a6f13a4aSGreg Roach            $this->list_private = 0;
2516a6f13a4aSGreg Roach            foreach ($this->list as $key => $value) {
2517a6f13a4aSGreg Roach                if (isset($value->generation)) {
2518a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2519a6f13a4aSGreg Roach                }
2520299d100dSGreg Roach                $tmp          = GedcomRecord::getInstance($key, $this->tree);
2521299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2522a6f13a4aSGreg Roach
2523a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2524e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2525a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25261aa04befSGreg Roach
25271aa04befSGreg Roach                xml_set_element_handler(
25281aa04befSGreg Roach                    $repeat_parser,
25291aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
25301aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25311aa04befSGreg Roach                    },
25321aa04befSGreg Roach                    function ($parser, string $name) {
25331aa04befSGreg Roach                        $this->endElement($parser, $name);
25341aa04befSGreg Roach                    }
25351aa04befSGreg Roach                );
25361aa04befSGreg Roach
25371aa04befSGreg Roach                xml_set_character_data_handler(
25381aa04befSGreg Roach                    $repeat_parser,
25391aa04befSGreg Roach                    function ($parser, $data) {
25401aa04befSGreg Roach                        $this->characterData($parser, $data);
25411aa04befSGreg Roach                    }
25421aa04befSGreg Roach                );
2543a6f13a4aSGreg Roach
2544a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25457a6ee1acSGreg 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)));
2546a6f13a4aSGreg Roach                }
2547a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2548a6f13a4aSGreg Roach            }
2549a6f13a4aSGreg Roach            // Clean up the list array
255013abd6f3SGreg Roach            $this->list   = [];
2551e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2552a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2553a6f13a4aSGreg Roach        }
2554a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2555a6f13a4aSGreg Roach    }
2556a6f13a4aSGreg Roach
2557a6f13a4aSGreg Roach    /**
2558a6f13a4aSGreg Roach     * XML <Generation /> element handler
2559a6f13a4aSGreg Roach     * Prints the number of generations
25608ba2e626SGreg Roach     *
25618ba2e626SGreg Roach     * @return void
2562a6f13a4aSGreg Roach     */
2563c1010edaSGreg Roach    private function generationStartHandler()
2564c1010edaSGreg Roach    {
2565589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2566a6f13a4aSGreg Roach    }
2567a6f13a4aSGreg Roach
2568a6f13a4aSGreg Roach    /**
2569a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2570a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25718ba2e626SGreg Roach     *
25728ba2e626SGreg Roach     * @return void
2573a6f13a4aSGreg Roach     */
2574c1010edaSGreg Roach    private function newPageStartHandler()
2575c1010edaSGreg Roach    {
25767a6ee1acSGreg Roach        $temp = 'addpage';
2577e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2578a6f13a4aSGreg Roach    }
2579a6f13a4aSGreg Roach
2580a6f13a4aSGreg Roach    /**
258176692c8bSGreg Roach     * XML <html>
258276692c8bSGreg Roach     *
2583a6f13a4aSGreg Roach     * @param string   $tag   HTML tag name
2584c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
25858ba2e626SGreg Roach     *
25868ba2e626SGreg Roach     * @return void
2587a6f13a4aSGreg Roach     */
2588c0fe75acSGreg Roach    private function htmlStartHandler(string $tag, array $attrs)
2589c1010edaSGreg Roach    {
25907a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2591a6f13a4aSGreg Roach            return;
2592a6f13a4aSGreg Roach        }
25939b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2594e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2595e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2596a6f13a4aSGreg Roach
25979b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2598a6f13a4aSGreg Roach        $this->print_data         = true;
2599a6f13a4aSGreg Roach    }
2600a6f13a4aSGreg Roach
2601a6f13a4aSGreg Roach    /**
260276692c8bSGreg Roach     * XML </html>
260376692c8bSGreg Roach     *
2604a6f13a4aSGreg Roach     * @param string $tag
26058ba2e626SGreg Roach     *
26068ba2e626SGreg Roach     * @return void
2607a6f13a4aSGreg Roach     */
2608c1010edaSGreg Roach    private function htmlEndHandler($tag)
2609c1010edaSGreg Roach    {
26107a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2611a6f13a4aSGreg Roach            return;
2612a6f13a4aSGreg Roach        }
2613a6f13a4aSGreg Roach
2614a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2615e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2616e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
26178f038c36SRico Sonntag        if ($this->wt_report !== null) {
2618e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2619a6f13a4aSGreg Roach        } else {
2620e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2621a6f13a4aSGreg Roach        }
2622a6f13a4aSGreg Roach    }
2623a6f13a4aSGreg Roach
2624a6f13a4aSGreg Roach    /**
2625a6f13a4aSGreg Roach     * Handle <Input>
26268ba2e626SGreg Roach     *
26278ba2e626SGreg Roach     * @return void
2628a6f13a4aSGreg Roach     */
2629c1010edaSGreg Roach    private function inputStartHandler()
2630c1010edaSGreg Roach    {
2631a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2632a6f13a4aSGreg Roach    }
2633a6f13a4aSGreg Roach
2634a6f13a4aSGreg Roach    /**
2635a6f13a4aSGreg Roach     * Handle </Input>
26368ba2e626SGreg Roach     *
26378ba2e626SGreg Roach     * @return void
2638a6f13a4aSGreg Roach     */
2639c1010edaSGreg Roach    private function inputEndHandler()
2640c1010edaSGreg Roach    {
2641a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2642a6f13a4aSGreg Roach    }
2643a6f13a4aSGreg Roach
2644a6f13a4aSGreg Roach    /**
2645a6f13a4aSGreg Roach     * Handle <Report>
26468ba2e626SGreg Roach     *
26478ba2e626SGreg Roach     * @return void
2648a6f13a4aSGreg Roach     */
2649c1010edaSGreg Roach    private function reportStartHandler()
2650c1010edaSGreg Roach    {
2651a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2652a6f13a4aSGreg Roach    }
2653a6f13a4aSGreg Roach
2654a6f13a4aSGreg Roach    /**
2655a6f13a4aSGreg Roach     * Handle </Report>
26568ba2e626SGreg Roach     *
26578ba2e626SGreg Roach     * @return void
2658a6f13a4aSGreg Roach     */
2659c1010edaSGreg Roach    private function reportEndHandler()
2660c1010edaSGreg Roach    {
2661a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2662a6f13a4aSGreg Roach    }
2663a6f13a4aSGreg Roach
2664a6f13a4aSGreg Roach    /**
266576692c8bSGreg Roach     * XML </titleEndHandler>
26668ba2e626SGreg Roach     *
26678ba2e626SGreg Roach     * @return void
2668a6f13a4aSGreg Roach     */
2669c1010edaSGreg Roach    private function titleEndHandler()
2670c1010edaSGreg Roach    {
26712836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2672a6f13a4aSGreg Roach    }
2673a6f13a4aSGreg Roach
2674a6f13a4aSGreg Roach    /**
267576692c8bSGreg Roach     * XML </descriptionEndHandler>
26768ba2e626SGreg Roach     *
26778ba2e626SGreg Roach     * @return void
2678a6f13a4aSGreg Roach     */
2679c1010edaSGreg Roach    private function descriptionEndHandler()
2680c1010edaSGreg Roach    {
26812836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2682a6f13a4aSGreg Roach    }
2683729ce104SGreg Roach
2684729ce104SGreg Roach    /**
268576692c8bSGreg Roach     * Create a list of all descendants.
268676692c8bSGreg Roach     *
2687729ce104SGreg Roach     * @param string[] $list
2688729ce104SGreg Roach     * @param string   $pid
2689729ce104SGreg Roach     * @param bool     $parents
2690729ce104SGreg Roach     * @param int      $generations
26918ba2e626SGreg Roach     *
26928ba2e626SGreg Roach     * @return void
2693729ce104SGreg Roach     */
2694c1010edaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1)
2695c1010edaSGreg Roach    {
2696299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2697729ce104SGreg Roach        if ($person === null) {
2698729ce104SGreg Roach            return;
2699729ce104SGreg Roach        }
2700729ce104SGreg Roach        if (!isset($list[$pid])) {
2701729ce104SGreg Roach            $list[$pid] = $person;
2702729ce104SGreg Roach        }
2703729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2704729ce104SGreg Roach            $list[$pid]->generation = 0;
2705729ce104SGreg Roach        }
2706729ce104SGreg Roach        foreach ($person->getSpouseFamilies() as $family) {
2707729ce104SGreg Roach            if ($parents) {
2708729ce104SGreg Roach                $husband = $family->getHusband();
2709729ce104SGreg Roach                $wife    = $family->getWife();
2710729ce104SGreg Roach                if ($husband) {
2711*c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2712729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2713*c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2714729ce104SGreg Roach                    } else {
2715*c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2716729ce104SGreg Roach                    }
2717729ce104SGreg Roach                }
2718729ce104SGreg Roach                if ($wife) {
2719*c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2720729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2721*c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2722729ce104SGreg Roach                    } else {
2723*c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2724729ce104SGreg Roach                    }
2725729ce104SGreg Roach                }
2726729ce104SGreg Roach            }
2727729ce104SGreg Roach            $children = $family->getChildren();
2728729ce104SGreg Roach            foreach ($children as $child) {
2729729ce104SGreg Roach                if ($child) {
2730*c0935879SGreg Roach                    $list[$child->xref()] = $child;
2731729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2732*c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2733729ce104SGreg Roach                    } else {
2734*c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2735729ce104SGreg Roach                    }
2736729ce104SGreg Roach                }
2737729ce104SGreg Roach            }
2738729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2739729ce104SGreg Roach                foreach ($children as $child) {
2740*c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2741729ce104SGreg Roach                }
2742729ce104SGreg Roach            }
2743729ce104SGreg Roach        }
2744729ce104SGreg Roach    }
2745729ce104SGreg Roach
2746729ce104SGreg Roach    /**
274776692c8bSGreg Roach     * Create a list of all ancestors.
274876692c8bSGreg Roach     *
2749729ce104SGreg Roach     * @param string[] $list
2750729ce104SGreg Roach     * @param string   $pid
2751729ce104SGreg Roach     * @param bool     $children
2752729ce104SGreg Roach     * @param int      $generations
27538ba2e626SGreg Roach     *
27548ba2e626SGreg Roach     * @return void
2755729ce104SGreg Roach     */
2756c1010edaSGreg Roach    private function addAncestors(&$list, $pid, $children = false, $generations = -1)
2757c1010edaSGreg Roach    {
275813abd6f3SGreg Roach        $genlist                = [$pid];
2759729ce104SGreg Roach        $list[$pid]->generation = 1;
2760729ce104SGreg Roach        while (count($genlist) > 0) {
2761729ce104SGreg Roach            $id = array_shift($genlist);
2762729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2763729ce104SGreg Roach                continue; // id can be something like “empty7”
2764729ce104SGreg Roach            }
2765299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
2766729ce104SGreg Roach            foreach ($person->getChildFamilies() as $family) {
2767729ce104SGreg Roach                $husband = $family->getHusband();
2768729ce104SGreg Roach                $wife    = $family->getWife();
2769729ce104SGreg Roach                if ($husband) {
2770*c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2771*c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2772729ce104SGreg Roach                }
2773729ce104SGreg Roach                if ($wife) {
2774*c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2775*c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2776729ce104SGreg Roach                }
2777729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2778729ce104SGreg Roach                    if ($husband) {
2779*c0935879SGreg Roach                        $genlist[] = $husband->xref();
2780729ce104SGreg Roach                    }
2781729ce104SGreg Roach                    if ($wife) {
2782*c0935879SGreg Roach                        $genlist[] = $wife->xref();
2783729ce104SGreg Roach                    }
2784729ce104SGreg Roach                }
2785729ce104SGreg Roach                if ($children) {
2786729ce104SGreg Roach                    foreach ($family->getChildren() as $child) {
2787*c0935879SGreg Roach                        $list[$child->xref()] = $child;
2788729ce104SGreg Roach                        if (isset($list[$id]->generation)) {
2789*c0935879SGreg Roach                            $list[$child->xref()]->generation = $list[$id]->generation;
2790729ce104SGreg Roach                        } else {
2791*c0935879SGreg Roach                            $list[$child->xref()]->generation = 1;
2792729ce104SGreg Roach                        }
2793729ce104SGreg Roach                    }
2794729ce104SGreg Roach                }
2795729ce104SGreg Roach            }
2796729ce104SGreg Roach        }
2797729ce104SGreg Roach    }
2798729ce104SGreg Roach
2799729ce104SGreg Roach    /**
2800729ce104SGreg Roach     * get gedcom tag value
2801729ce104SGreg Roach     *
2802729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2803729ce104SGreg 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
2804729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2805729ce104SGreg Roach     *
2806729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2807729ce104SGreg Roach     */
28088f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2809c1010edaSGreg Roach    {
2810729ce104SGreg Roach        if (empty($gedrec)) {
2811729ce104SGreg Roach            return '';
2812729ce104SGreg Roach        }
2813729ce104SGreg Roach        $tags      = explode(':', $tag);
2814729ce104SGreg Roach        $origlevel = $level;
2815729ce104SGreg Roach        if ($level == 0) {
28163c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2817729ce104SGreg Roach        }
2818729ce104SGreg Roach
2819729ce104SGreg Roach        $subrec = $gedrec;
2820729ce104SGreg Roach        foreach ($tags as $t) {
2821729ce104SGreg Roach            $lastsubrec = $subrec;
28223d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2823729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2824729ce104SGreg Roach                $level--;
28253d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2826729ce104SGreg Roach            }
2827729ce104SGreg Roach            if (empty($subrec)) {
2828044416d2SGreg Roach                if ($t === 'TITL') {
28293d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2830729ce104SGreg Roach                    if (!empty($subrec)) {
28317a6ee1acSGreg Roach                        $t = 'ABBR';
2832729ce104SGreg Roach                    }
2833729ce104SGreg Roach                }
2834729ce104SGreg Roach                if (empty($subrec)) {
2835729ce104SGreg Roach                    if ($level > 0) {
2836729ce104SGreg Roach                        $level--;
2837729ce104SGreg Roach                    }
28383d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2839729ce104SGreg Roach                    if (empty($subrec)) {
2840729ce104SGreg Roach                        return '';
2841729ce104SGreg Roach                    }
2842729ce104SGreg Roach                }
2843729ce104SGreg Roach            }
2844729ce104SGreg Roach            $level++;
2845729ce104SGreg Roach        }
2846729ce104SGreg Roach        $level--;
2847729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2848729ce104SGreg Roach        if ($ct == 0) {
2849729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2850729ce104SGreg Roach        }
2851729ce104SGreg Roach        if ($ct == 0) {
2852729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2853729ce104SGreg Roach        }
2854729ce104SGreg Roach        if ($ct > 0) {
2855729ce104SGreg Roach            $value = trim($match[1]);
2856044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2857299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2858ff166e64SGreg Roach                if ($note instanceof Note) {
2859729ce104SGreg Roach                    $value = $note->getNote();
2860729ce104SGreg Roach                } else {
2861729ce104SGreg Roach                    //-- set the value to the id without the @
2862729ce104SGreg Roach                    $value = $match[1];
2863729ce104SGreg Roach                }
2864729ce104SGreg Roach            }
28657a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28663d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2867729ce104SGreg Roach            }
2868729ce104SGreg Roach
2869729ce104SGreg Roach            return $value;
2870729ce104SGreg Roach        }
2871729ce104SGreg Roach
28727a6ee1acSGreg Roach        return '';
2873729ce104SGreg Roach    }
2874d1286247SGreg Roach
2875d1286247SGreg Roach    /**
2876d1286247SGreg Roach     * Replace variable identifiers with their values.
2877d1286247SGreg Roach     *
2878d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
287982759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2880d1286247SGreg Roach     *
2881d1286247SGreg Roach     * @return string
2882d1286247SGreg Roach     */
28838f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2884c1010edaSGreg Roach    {
2885d1286247SGreg Roach        return preg_replace_callback(
2886d1286247SGreg Roach            '/\$(\w+)/',
288718d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28882118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
288982759250SGreg Roach                    if ($quote) {
28902118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2891b2ce94c6SRico Sonntag                    }
2892b2ce94c6SRico Sonntag
28932118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
289482759250SGreg Roach                }
2895b2ce94c6SRico Sonntag
2896d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28973d7a8a4cSGreg Roach
2898d1286247SGreg Roach                return '$' . $matches[1];
2899d1286247SGreg Roach            },
2900d1286247SGreg Roach            $expression
2901d1286247SGreg Roach        );
2902d1286247SGreg Roach    }
2903a6f13a4aSGreg Roach}
2904