xref: /webtrees/app/Report/ReportParserGenerate.php (revision 1aa04bef45581235e0d02d9ae70226815287841f)
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;
37cb63a60eSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionFunction;
385809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
39a6f13a4aSGreg Roach
40a6f13a4aSGreg Roach/**
41a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
42a6f13a4aSGreg Roach */
43c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
44c1010edaSGreg Roach{
45a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
46a6f13a4aSGreg Roach    private $process_footnote = true;
47a6f13a4aSGreg Roach
48a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
49a6f13a4aSGreg Roach    private $print_data = false;
50a6f13a4aSGreg Roach
51a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5213abd6f3SGreg Roach    private $print_data_stack = [];
53a6f13a4aSGreg Roach
5476692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
55a6f13a4aSGreg Roach    private $process_gedcoms = 0;
56a6f13a4aSGreg Roach
5776692c8bSGreg Roach    /** @var int Are we processing conditionals */
58a6f13a4aSGreg Roach    private $process_ifs = 0;
59a6f13a4aSGreg Roach
6076692c8bSGreg Roach    /** @var int Are we processing repeats */
61a6f13a4aSGreg Roach    private $process_repeats = 0;
62a6f13a4aSGreg Roach
63a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
64a6f13a4aSGreg Roach    private $repeat_bytes = 0;
65a6f13a4aSGreg Roach
665b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
6713abd6f3SGreg Roach    private $repeats = [];
68a6f13a4aSGreg Roach
69a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
7013abd6f3SGreg Roach    private $repeats_stack = [];
71a6f13a4aSGreg Roach
72208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
7313abd6f3SGreg Roach    private $wt_report_stack = [];
74e8e7866bSGreg Roach
75e8e7866bSGreg Roach    /** @var resource Nested repeating data */
76e8e7866bSGreg Roach    private $parser;
77e8e7866bSGreg Roach
78e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
7913abd6f3SGreg Roach    private $parser_stack = [];
80e8e7866bSGreg Roach
81a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
82a6f13a4aSGreg Roach    private $gedrec = '';
83a6f13a4aSGreg Roach
84a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
8513abd6f3SGreg Roach    private $gedrec_stack = [];
86a6f13a4aSGreg Roach
87a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
88a6f13a4aSGreg Roach    private $current_element;
89a6f13a4aSGreg Roach
90a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
91a6f13a4aSGreg Roach    private $footnote_element;
92a6f13a4aSGreg Roach
93a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
94a6f13a4aSGreg Roach    private $fact = '';
95a6f13a4aSGreg Roach
96a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
97a6f13a4aSGreg Roach    private $desc = '';
98a6f13a4aSGreg Roach
99a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
100a6f13a4aSGreg Roach    private $type = '';
101a6f13a4aSGreg Roach
102a6f13a4aSGreg Roach    /** @var int The current generational level */
103a6f13a4aSGreg Roach    private $generation = 1;
104a6f13a4aSGreg Roach
105a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
10613abd6f3SGreg Roach    private $list = [];
107a6f13a4aSGreg Roach
108a6f13a4aSGreg Roach    /** @var int Number of items in lists */
109a6f13a4aSGreg Roach    private $list_total = 0;
110a6f13a4aSGreg Roach
111a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
112a6f13a4aSGreg Roach    private $list_private = 0;
113a6f13a4aSGreg Roach
114299d100dSGreg Roach    /** @var string The filename of the XML report */
115299d100dSGreg Roach    protected $report;
116299d100dSGreg Roach
117208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
118e8e7866bSGreg Roach    private $report_root;
119e8e7866bSGreg Roach
120208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
121e8e7866bSGreg Roach    private $wt_report;
122e8e7866bSGreg Roach
123d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1242118c0e3SGreg Roach    private $vars;
125d1286247SGreg Roach
126299d100dSGreg Roach    /** @var Tree The current tree */
127299d100dSGreg Roach    private $tree;
128299d100dSGreg Roach
12976692c8bSGreg Roach    /**
13076692c8bSGreg Roach     * Create a parser for a report
13176692c8bSGreg Roach     *
13276692c8bSGreg Roach     * @param string         $report The XML filename
133208e9f76SGreg Roach     * @param AbstractReport $report_root
13476692c8bSGreg Roach     * @param string[][]     $vars
135299d100dSGreg Roach     * @param Tree           $tree
13676692c8bSGreg Roach     */
137208e9f76SGreg Roach    public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree)
138c1010edaSGreg Roach    {
139299d100dSGreg Roach        $this->report          = $report;
140e8e7866bSGreg Roach        $this->report_root     = $report_root;
141e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14259f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
143d1286247SGreg Roach        $this->vars            = $vars;
144299d100dSGreg Roach        $this->tree            = $tree;
145299d100dSGreg Roach
14676f666f4SGreg Roach        parent::__construct($report);
147a6f13a4aSGreg Roach    }
148a6f13a4aSGreg Roach
149a6f13a4aSGreg Roach    /**
150a6f13a4aSGreg Roach     * XML start element handler
151a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
152a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
153a6f13a4aSGreg Roach     *
154a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
155a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1568a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
15718d7a90dSGreg Roach     *
15818d7a90dSGreg Roach     * @return void
159a6f13a4aSGreg Roach     */
1608a4ee39cSGreg Roach    protected function startElement($parser, string $name, array $attrs)
161c1010edaSGreg Roach    {
16213abd6f3SGreg Roach        $newattrs = [];
163a6f13a4aSGreg Roach
164a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
165a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
166d1286247SGreg Roach                if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
167d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
168a6f13a4aSGreg Roach                }
169a6f13a4aSGreg Roach            }
170a6f13a4aSGreg Roach            $newattrs[$key] = $value;
171a6f13a4aSGreg Roach        }
172a6f13a4aSGreg Roach        $attrs = $newattrs;
1737a6ee1acSGreg 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')) {
174a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
175a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
176208e9f76SGreg Roach
177a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
178a6f13a4aSGreg Roach                $this->$start_method($attrs);
179a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
180a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
181a6f13a4aSGreg Roach            }
182a6f13a4aSGreg Roach        }
183a6f13a4aSGreg Roach    }
184a6f13a4aSGreg Roach
185a6f13a4aSGreg Roach    /**
186a6f13a4aSGreg Roach     * XML end element handler
187a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
188a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
189a6f13a4aSGreg Roach     *
190a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
191a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
19218d7a90dSGreg Roach     *
19318d7a90dSGreg Roach     * @return void
194a6f13a4aSGreg Roach     */
1958a4ee39cSGreg Roach    protected function endElement($parser, string $name)
196c1010edaSGreg Roach    {
1977a6ee1acSGreg 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')) {
198a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
199a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
200a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
201a6f13a4aSGreg Roach                $this->$end_method();
202a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
203a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
204a6f13a4aSGreg Roach            }
205a6f13a4aSGreg Roach        }
206a6f13a4aSGreg Roach    }
207a6f13a4aSGreg Roach
208a6f13a4aSGreg Roach    /**
209a6f13a4aSGreg Roach     * XML character data handler
210a6f13a4aSGreg Roach     *
211a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
212a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
21318d7a90dSGreg Roach     *
21418d7a90dSGreg Roach     * @return void
215a6f13a4aSGreg Roach     */
216c1010edaSGreg Roach    protected function characterData($parser, $data)
217c1010edaSGreg Roach    {
218e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
219a6f13a4aSGreg Roach            $this->current_element->addText($data);
220a6f13a4aSGreg Roach        }
221a6f13a4aSGreg Roach    }
222a6f13a4aSGreg Roach
223a6f13a4aSGreg Roach    /**
22476692c8bSGreg Roach     * XML <style>
225a6f13a4aSGreg Roach     *
226a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
2278ba2e626SGreg Roach     *
2288ba2e626SGreg Roach     * @return void
229a6f13a4aSGreg Roach     */
230c1010edaSGreg Roach    private function styleStartHandler($attrs)
231c1010edaSGreg Roach    {
232a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
233a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
234a6f13a4aSGreg Roach        }
235a6f13a4aSGreg Roach
236a6f13a4aSGreg Roach        // array Style that will be passed on
23713abd6f3SGreg Roach        $s = [];
238a6f13a4aSGreg Roach
239a6f13a4aSGreg Roach        // string Name af the style
240a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
241a6f13a4aSGreg Roach
242a6f13a4aSGreg Roach        // string Name of the DEFAULT font
243208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
244a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
245a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
246a6f13a4aSGreg Roach        }
247a6f13a4aSGreg Roach
248a6f13a4aSGreg Roach        // int The size of the font in points
249208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
250a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
251a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
252a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
253a6f13a4aSGreg Roach
254a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2557a6ee1acSGreg Roach        $s['style'] = '';
256a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
257a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
258a6f13a4aSGreg Roach        }
259a6f13a4aSGreg Roach
260e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
261a6f13a4aSGreg Roach    }
262a6f13a4aSGreg Roach
263a6f13a4aSGreg Roach    /**
26476692c8bSGreg Roach     * XML <Doc>
265a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
266a6f13a4aSGreg Roach     *
267a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
2688ba2e626SGreg Roach     *
2698ba2e626SGreg Roach     * @return void
270a6f13a4aSGreg Roach     */
271c1010edaSGreg Roach    private function docStartHandler($attrs)
272c1010edaSGreg Roach    {
273e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
274a6f13a4aSGreg Roach
275a6f13a4aSGreg Roach        // Custom page width
276a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
277208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
278a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
279a6f13a4aSGreg Roach        // Custom Page height
280a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
281208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
282a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
283a6f13a4aSGreg Roach
284a6f13a4aSGreg Roach        // Left Margin
285a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2867a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
287208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
288a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
289208e9f76SGreg 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))
290a6f13a4aSGreg Roach            }
291a6f13a4aSGreg Roach        }
292a6f13a4aSGreg Roach        // Right Margin
293a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
2947a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
295208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
296a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
297208e9f76SGreg 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))
298a6f13a4aSGreg Roach            }
299a6f13a4aSGreg Roach        }
300a6f13a4aSGreg Roach        // Top Margin
301a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3027a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
303208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
304a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
305208e9f76SGreg 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))
306a6f13a4aSGreg Roach            }
307a6f13a4aSGreg Roach        }
308a6f13a4aSGreg Roach        // Bottom Margin
309a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3107a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
311208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
312a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
313208e9f76SGreg 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))
314a6f13a4aSGreg Roach            }
315a6f13a4aSGreg Roach        }
316a6f13a4aSGreg Roach        // Header Margin
317a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3187a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
319208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
320a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
321208e9f76SGreg 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))
322a6f13a4aSGreg Roach            }
323a6f13a4aSGreg Roach        }
324a6f13a4aSGreg Roach        // Footer Margin
325a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3267a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
327208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
328a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
329208e9f76SGreg 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))
330a6f13a4aSGreg Roach            }
331a6f13a4aSGreg Roach        }
332a6f13a4aSGreg Roach
333a6f13a4aSGreg Roach        // Page Orientation
334a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
3357a6ee1acSGreg Roach            if ($attrs['orientation'] == 'landscape') {
3367a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
3377a6ee1acSGreg Roach            } elseif ($attrs['orientation'] == 'portrait') {
3387a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
339a6f13a4aSGreg Roach            }
340a6f13a4aSGreg Roach        }
341a6f13a4aSGreg Roach        // Page Size
342a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
343208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
344a6f13a4aSGreg Roach        }
345a6f13a4aSGreg Roach
346a6f13a4aSGreg Roach        // Show Generated By...
347a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3487a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
349208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3507a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
351208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
352a6f13a4aSGreg Roach            }
353a6f13a4aSGreg Roach        }
354a6f13a4aSGreg Roach
355e8e7866bSGreg Roach        $this->wt_report->setup();
356a6f13a4aSGreg Roach    }
357a6f13a4aSGreg Roach
358a6f13a4aSGreg Roach    /**
35976692c8bSGreg Roach     * XML </Doc>
3608ba2e626SGreg Roach     *
3618ba2e626SGreg Roach     * @return void
362a6f13a4aSGreg Roach     */
363c1010edaSGreg Roach    private function docEndHandler()
364c1010edaSGreg Roach    {
365e8e7866bSGreg Roach        $this->wt_report->run();
366a6f13a4aSGreg Roach    }
367a6f13a4aSGreg Roach
368a6f13a4aSGreg Roach    /**
36976692c8bSGreg Roach     * XML <Header>
3708ba2e626SGreg Roach     *
3718ba2e626SGreg Roach     * @return void
372a6f13a4aSGreg Roach     */
373c1010edaSGreg Roach    private function headerStartHandler()
374c1010edaSGreg Roach    {
375a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
376e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3777a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
378a6f13a4aSGreg Roach    }
379a6f13a4aSGreg Roach
380a6f13a4aSGreg Roach    /**
38176692c8bSGreg Roach     * XML <PageHeader>
3828ba2e626SGreg Roach     *
3838ba2e626SGreg Roach     * @return void
384a6f13a4aSGreg Roach     */
385c1010edaSGreg Roach    private function pageHeaderStartHandler()
386c1010edaSGreg Roach    {
3879b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
388a6f13a4aSGreg Roach        $this->print_data         = false;
3899b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
390e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
391a6f13a4aSGreg Roach    }
392a6f13a4aSGreg Roach
393a6f13a4aSGreg Roach    /**
39476692c8bSGreg Roach     * XML <pageHeaderEndHandler>
3958ba2e626SGreg Roach     *
3968ba2e626SGreg Roach     * @return void
397a6f13a4aSGreg Roach     */
398c1010edaSGreg Roach    private function pageHeaderEndHandler()
399c1010edaSGreg Roach    {
400a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
401e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
402e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
403e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
404a6f13a4aSGreg Roach    }
405a6f13a4aSGreg Roach
406a6f13a4aSGreg Roach    /**
40776692c8bSGreg Roach     * XML <bodyStartHandler>
4088ba2e626SGreg Roach     *
4098ba2e626SGreg Roach     * @return void
410a6f13a4aSGreg Roach     */
411c1010edaSGreg Roach    private function bodyStartHandler()
412c1010edaSGreg Roach    {
4137a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
414a6f13a4aSGreg Roach    }
415a6f13a4aSGreg Roach
416a6f13a4aSGreg Roach    /**
41776692c8bSGreg Roach     * XML <footerStartHandler>
4188ba2e626SGreg Roach     *
4198ba2e626SGreg Roach     * @return void
420a6f13a4aSGreg Roach     */
421c1010edaSGreg Roach    private function footerStartHandler()
422c1010edaSGreg Roach    {
4237a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
424a6f13a4aSGreg Roach    }
425a6f13a4aSGreg Roach
426a6f13a4aSGreg Roach    /**
42776692c8bSGreg Roach     * XML <Cell>
428a6f13a4aSGreg Roach     *
429a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
4308ba2e626SGreg Roach     *
4318ba2e626SGreg Roach     * @return void
432a6f13a4aSGreg Roach     */
433c1010edaSGreg Roach    private function cellStartHandler($attrs)
434c1010edaSGreg Roach    {
435a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4367a6ee1acSGreg Roach        $align = '';
437a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
438a6f13a4aSGreg Roach            $align = $attrs['align'];
439a6f13a4aSGreg Roach            // RTL supported left/right alignment
4407a6ee1acSGreg Roach            if ($align == 'rightrtl') {
441e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4427a6ee1acSGreg Roach                    $align = 'left';
443a6f13a4aSGreg Roach                } else {
4447a6ee1acSGreg Roach                    $align = 'right';
445a6f13a4aSGreg Roach                }
4467a6ee1acSGreg Roach            } elseif ($align == 'leftrtl') {
447e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4487a6ee1acSGreg Roach                    $align = 'right';
449a6f13a4aSGreg Roach                } else {
4507a6ee1acSGreg Roach                    $align = 'left';
451a6f13a4aSGreg Roach                }
452a6f13a4aSGreg Roach            }
453a6f13a4aSGreg Roach        }
454a6f13a4aSGreg Roach
455a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4567a6ee1acSGreg Roach        $bgcolor = '';
457a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
458a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
459a6f13a4aSGreg Roach        }
460a6f13a4aSGreg Roach
461a6f13a4aSGreg Roach        // int Whether or not the background should be painted
462a6f13a4aSGreg Roach        $fill = 1;
463a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4647a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
465a6f13a4aSGreg Roach                $fill = 0;
4667a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
467a6f13a4aSGreg Roach                $fill = 1;
468a6f13a4aSGreg Roach            }
469a6f13a4aSGreg Roach        }
470a6f13a4aSGreg Roach
471a6f13a4aSGreg Roach        $reseth = true;
472a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
473a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4747a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
475a6f13a4aSGreg Roach                $reseth = false;
4767a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
477a6f13a4aSGreg Roach                $reseth = true;
478a6f13a4aSGreg Roach            }
479a6f13a4aSGreg Roach        }
480a6f13a4aSGreg Roach
481a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
482a6f13a4aSGreg Roach        $border = 0;
483a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
484a6f13a4aSGreg Roach            $border = $attrs['border'];
485a6f13a4aSGreg Roach        }
486a6f13a4aSGreg Roach        // string Border color in HTML code
4877a6ee1acSGreg Roach        $bocolor = '';
488a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
489a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
490a6f13a4aSGreg Roach        }
491a6f13a4aSGreg Roach
492a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
493a6f13a4aSGreg Roach        $height = 0;
494a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
495589feda3SGreg Roach            $height = $attrs['height'];
496a6f13a4aSGreg Roach        }
497a6f13a4aSGreg 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.
498a6f13a4aSGreg Roach        $width = 0;
499a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
500589feda3SGreg Roach            $width = $attrs['width'];
501a6f13a4aSGreg Roach        }
502a6f13a4aSGreg Roach
503a6f13a4aSGreg Roach        // int Stretch carachter mode
504a6f13a4aSGreg Roach        $stretch = 0;
505a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
506a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
507a6f13a4aSGreg Roach        }
508a6f13a4aSGreg Roach
509a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
5107a6ee1acSGreg Roach        $left = '.';
511a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5127a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
5137a6ee1acSGreg Roach                $left = '.';
514a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
515a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5167a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
517a6f13a4aSGreg Roach                $left = 0;
518a6f13a4aSGreg Roach            }
519a6f13a4aSGreg Roach        }
520a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
5217a6ee1acSGreg Roach        $top = '.';
522a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5237a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
5247a6ee1acSGreg Roach                $top = '.';
525a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
526a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5277a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
528a6f13a4aSGreg Roach                $top = 0;
529a6f13a4aSGreg Roach            }
530a6f13a4aSGreg Roach        }
531a6f13a4aSGreg Roach
532a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5337a6ee1acSGreg Roach        $style = '';
534a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
535a6f13a4aSGreg Roach            $style = $attrs['style'];
536a6f13a4aSGreg Roach        }
537a6f13a4aSGreg Roach
538a6f13a4aSGreg Roach        // string Text color in html code
5397a6ee1acSGreg Roach        $tcolor = '';
540a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
541a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
542a6f13a4aSGreg Roach        }
543a6f13a4aSGreg Roach
544a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
545a6f13a4aSGreg Roach        $ln = 0;
546a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
547a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
548a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5497a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
550a6f13a4aSGreg Roach                $ln = 0;
551a6f13a4aSGreg Roach            }
552a6f13a4aSGreg Roach        }
553a6f13a4aSGreg Roach
5547a6ee1acSGreg Roach        if ($align == 'left') {
5557a6ee1acSGreg Roach            $align = 'L';
5567a6ee1acSGreg Roach        } elseif ($align == 'right') {
5577a6ee1acSGreg Roach            $align = 'R';
5587a6ee1acSGreg Roach        } elseif ($align == 'center') {
5597a6ee1acSGreg Roach            $align = 'C';
5607a6ee1acSGreg Roach        } elseif ($align == 'justify') {
5617a6ee1acSGreg Roach            $align = 'J';
562a6f13a4aSGreg Roach        }
563a6f13a4aSGreg Roach
5649b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
565a6f13a4aSGreg Roach        $this->print_data         = true;
566a6f13a4aSGreg Roach
567e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
568a6f13a4aSGreg Roach            $width,
569a6f13a4aSGreg Roach            $height,
570a6f13a4aSGreg Roach            $border,
571a6f13a4aSGreg Roach            $align,
572a6f13a4aSGreg Roach            $bgcolor,
573a6f13a4aSGreg Roach            $style,
574a6f13a4aSGreg Roach            $ln,
575a6f13a4aSGreg Roach            $top,
576a6f13a4aSGreg Roach            $left,
577a6f13a4aSGreg Roach            $fill,
578a6f13a4aSGreg Roach            $stretch,
579a6f13a4aSGreg Roach            $bocolor,
580a6f13a4aSGreg Roach            $tcolor,
581a6f13a4aSGreg Roach            $reseth
582a6f13a4aSGreg Roach        );
583a6f13a4aSGreg Roach    }
584a6f13a4aSGreg Roach
585a6f13a4aSGreg Roach    /**
58676692c8bSGreg Roach     * XML </Cell>
5878ba2e626SGreg Roach     *
5888ba2e626SGreg Roach     * @return void
589a6f13a4aSGreg Roach     */
590c1010edaSGreg Roach    private function cellEndHandler()
591c1010edaSGreg Roach    {
592a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
593e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
594a6f13a4aSGreg Roach    }
595a6f13a4aSGreg Roach
596a6f13a4aSGreg Roach    /**
597a6f13a4aSGreg Roach     * XML <Now /> element handler
5988ba2e626SGreg Roach     *
5998ba2e626SGreg Roach     * @return void
600a6f13a4aSGreg Roach     */
601c1010edaSGreg Roach    private function nowStartHandler()
602c1010edaSGreg Roach    {
6033d7a8a4cSGreg Roach        $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
604a6f13a4aSGreg Roach        $this->current_element->addText($g->display());
605a6f13a4aSGreg Roach    }
606a6f13a4aSGreg Roach
607a6f13a4aSGreg Roach    /**
608a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6098ba2e626SGreg Roach     *
6108ba2e626SGreg Roach     * @return void
611a6f13a4aSGreg Roach     */
612c1010edaSGreg Roach    private function pageNumStartHandler()
613c1010edaSGreg Roach    {
6147a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
615a6f13a4aSGreg Roach    }
616a6f13a4aSGreg Roach
617a6f13a4aSGreg Roach    /**
618a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6198ba2e626SGreg Roach     *
6208ba2e626SGreg Roach     * @return void
621a6f13a4aSGreg Roach     */
622c1010edaSGreg Roach    private function totalPagesStartHandler()
623c1010edaSGreg Roach    {
6247a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
625a6f13a4aSGreg Roach    }
626a6f13a4aSGreg Roach
627a6f13a4aSGreg Roach    /**
628a6f13a4aSGreg Roach     * Called at the start of an element.
629a6f13a4aSGreg Roach     *
630a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
6318ba2e626SGreg Roach     *
6328ba2e626SGreg Roach     * @return void
633a6f13a4aSGreg Roach     */
634c1010edaSGreg Roach    private function gedcomStartHandler($attrs)
635c1010edaSGreg Roach    {
636a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
637a6f13a4aSGreg Roach            $this->process_gedcoms++;
638a6f13a4aSGreg Roach
639a6f13a4aSGreg Roach            return;
640a6f13a4aSGreg Roach        }
641a6f13a4aSGreg Roach
642a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6437a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6447a6ee1acSGreg Roach        $tags      = explode(':', $tag);
645a6f13a4aSGreg Roach        $newgedrec = '';
646a6f13a4aSGreg Roach        if (count($tags) < 2) {
647299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
648299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
649a6f13a4aSGreg Roach        }
650a6f13a4aSGreg Roach        if (empty($newgedrec)) {
651a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
652a6f13a4aSGreg Roach            $newgedrec = '';
653a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6547a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
655d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
656d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
657a6f13a4aSGreg Roach                    } else {
658299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
659299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
660a6f13a4aSGreg Roach                    }
661a6f13a4aSGreg Roach                } else {
6627a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66313abd6f3SGreg Roach                        $gmatch = [];
664a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
665299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
666299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
667a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
668a6f13a4aSGreg Roach                        } else {
669a6f13a4aSGreg Roach                            $newgedrec = '';
670a6f13a4aSGreg Roach                            break;
671a6f13a4aSGreg Roach                        }
672a6f13a4aSGreg Roach                    } else {
6737a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
674a6f13a4aSGreg Roach                        $level     = $temp[0] + 1;
6753d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
676a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
677a6f13a4aSGreg Roach                    }
678a6f13a4aSGreg Roach                }
679a6f13a4aSGreg Roach            }
680a6f13a4aSGreg Roach        }
681a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6829b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
683a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
684a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
685a6f13a4aSGreg Roach                $this->fact = $match[2];
686a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
687a6f13a4aSGreg Roach            }
688a6f13a4aSGreg Roach        } else {
689a6f13a4aSGreg Roach            $this->process_gedcoms++;
690a6f13a4aSGreg Roach        }
691a6f13a4aSGreg Roach    }
692a6f13a4aSGreg Roach
693a6f13a4aSGreg Roach    /**
694a6f13a4aSGreg Roach     * Called at the end of an element.
6958ba2e626SGreg Roach     *
6968ba2e626SGreg Roach     * @return void
697a6f13a4aSGreg Roach     */
698c1010edaSGreg Roach    private function gedcomEndHandler()
699c1010edaSGreg Roach    {
700a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
701a6f13a4aSGreg Roach            $this->process_gedcoms--;
702a6f13a4aSGreg Roach        } else {
703a6f13a4aSGreg Roach            list($this->gedrec, $this->fact, $this->desc) = array_pop($this->gedrec_stack);
704a6f13a4aSGreg Roach        }
705a6f13a4aSGreg Roach    }
706a6f13a4aSGreg Roach
707a6f13a4aSGreg Roach    /**
70876692c8bSGreg Roach     * XML <textBoxStartHandler>
709a6f13a4aSGreg Roach     *
710a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
7118ba2e626SGreg Roach     *
7128ba2e626SGreg Roach     * @return void
713a6f13a4aSGreg Roach     */
714c1010edaSGreg Roach    private function textBoxStartHandler($attrs)
715c1010edaSGreg Roach    {
716a6f13a4aSGreg Roach        // string Background color code
7177a6ee1acSGreg Roach        $bgcolor = '';
718a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
719a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
720a6f13a4aSGreg Roach        }
721a6f13a4aSGreg Roach
722a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
723a6f13a4aSGreg Roach        $fill = true;
724a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7257a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
726a6f13a4aSGreg Roach                $fill = false;
7277a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
728a6f13a4aSGreg Roach                $fill = true;
729a6f13a4aSGreg Roach            }
730a6f13a4aSGreg Roach        }
731a6f13a4aSGreg Roach
732a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
733a6f13a4aSGreg Roach        $border = false;
734a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7357a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
736a6f13a4aSGreg Roach                $border = true;
7377a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
738a6f13a4aSGreg Roach                $border = false;
739a6f13a4aSGreg Roach            }
740a6f13a4aSGreg Roach        }
741a6f13a4aSGreg Roach
742a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
743a6f13a4aSGreg Roach        $height = 0;
744a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
745a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
746a6f13a4aSGreg Roach        }
747a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
748a6f13a4aSGreg Roach        $width = 0;
749a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
750a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
751a6f13a4aSGreg Roach        }
752a6f13a4aSGreg Roach
753a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
7547a6ee1acSGreg Roach        $left = '.';
755a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7567a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
7577a6ee1acSGreg Roach                $left = '.';
758a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
759a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7607a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
761a6f13a4aSGreg Roach                $left = 0;
762a6f13a4aSGreg Roach            }
763a6f13a4aSGreg Roach        }
764a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
7657a6ee1acSGreg Roach        $top = '.';
766a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7677a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
7687a6ee1acSGreg Roach                $top = '.';
769a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
770a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7717a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
772a6f13a4aSGreg Roach                $top = 0;
773a6f13a4aSGreg Roach            }
774a6f13a4aSGreg Roach        }
775a6f13a4aSGreg 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
776a6f13a4aSGreg Roach        $newline = false;
777a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7787a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
779a6f13a4aSGreg Roach                $newline = true;
7807a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
781a6f13a4aSGreg Roach                $newline = false;
782a6f13a4aSGreg Roach            }
783a6f13a4aSGreg Roach        }
784a6f13a4aSGreg Roach        // boolean
785a6f13a4aSGreg Roach        $pagecheck = true;
786a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7877a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
788a6f13a4aSGreg Roach                $pagecheck = false;
7897a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
790a6f13a4aSGreg Roach                $pagecheck = true;
791a6f13a4aSGreg Roach            }
792a6f13a4aSGreg Roach        }
793a6f13a4aSGreg Roach        // boolean Cell padding
794a6f13a4aSGreg Roach        $padding = true;
795a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
7967a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
797a6f13a4aSGreg Roach                $padding = false;
7987a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
799a6f13a4aSGreg Roach                $padding = true;
800a6f13a4aSGreg Roach            }
801a6f13a4aSGreg Roach        }
802a6f13a4aSGreg Roach        // boolean Reset this box Height
803a6f13a4aSGreg Roach        $reseth = false;
804a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8057a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
806a6f13a4aSGreg Roach                $reseth = true;
8077a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
808a6f13a4aSGreg Roach                $reseth = false;
809a6f13a4aSGreg Roach            }
810a6f13a4aSGreg Roach        }
811a6f13a4aSGreg Roach
812a6f13a4aSGreg Roach        // string Style of rendering
8137a6ee1acSGreg Roach        $style = '';
814a6f13a4aSGreg Roach
8159b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
816a6f13a4aSGreg Roach        $this->print_data         = false;
817a6f13a4aSGreg Roach
8189b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
819e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
820a6f13a4aSGreg Roach            $width,
821a6f13a4aSGreg Roach            $height,
822a6f13a4aSGreg Roach            $border,
823a6f13a4aSGreg Roach            $bgcolor,
824a6f13a4aSGreg Roach            $newline,
825a6f13a4aSGreg Roach            $left,
826a6f13a4aSGreg Roach            $top,
827a6f13a4aSGreg Roach            $pagecheck,
828a6f13a4aSGreg Roach            $style,
829a6f13a4aSGreg Roach            $fill,
830a6f13a4aSGreg Roach            $padding,
831a6f13a4aSGreg Roach            $reseth
832a6f13a4aSGreg Roach        );
833a6f13a4aSGreg Roach    }
834a6f13a4aSGreg Roach
835a6f13a4aSGreg Roach    /**
83676692c8bSGreg Roach     * XML <textBoxEndHandler>
8378ba2e626SGreg Roach     *
8388ba2e626SGreg Roach     * @return void
839a6f13a4aSGreg Roach     */
840c1010edaSGreg Roach    private function textBoxEndHandler()
841c1010edaSGreg Roach    {
842a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
843e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
844e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
845e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
846a6f13a4aSGreg Roach    }
847a6f13a4aSGreg Roach
848a6f13a4aSGreg Roach    /**
84976692c8bSGreg Roach     * XLM <Text>.
85076692c8bSGreg Roach     *
851a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
8528ba2e626SGreg Roach     *
8538ba2e626SGreg Roach     * @return void
854a6f13a4aSGreg Roach     */
855c1010edaSGreg Roach    private function textStartHandler($attrs)
856c1010edaSGreg Roach    {
8579b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
858a6f13a4aSGreg Roach        $this->print_data         = true;
859a6f13a4aSGreg Roach
860a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8617a6ee1acSGreg Roach        $style = '';
862a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
863a6f13a4aSGreg Roach            $style = $attrs['style'];
864a6f13a4aSGreg Roach        }
865a6f13a4aSGreg Roach
866a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8677a6ee1acSGreg Roach        $color = '';
868a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
869a6f13a4aSGreg Roach            $color = $attrs['color'];
870a6f13a4aSGreg Roach        }
871a6f13a4aSGreg Roach
872e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
873a6f13a4aSGreg Roach    }
874a6f13a4aSGreg Roach
875a6f13a4aSGreg Roach    /**
87676692c8bSGreg Roach     * XML </Text>
8778ba2e626SGreg Roach     *
8788ba2e626SGreg Roach     * @return void
879a6f13a4aSGreg Roach     */
880c1010edaSGreg Roach    private function textEndHandler()
881c1010edaSGreg Roach    {
882a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
883e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
884a6f13a4aSGreg Roach    }
885a6f13a4aSGreg Roach
886a6f13a4aSGreg Roach    /**
88776692c8bSGreg Roach     * XML <GetPersonName/>
888a6f13a4aSGreg Roach     * Get the name
889a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
890a6f13a4aSGreg Roach     * 2. id is set with a record id
891a6f13a4aSGreg Roach     *
892a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
8938ba2e626SGreg Roach     *
8948ba2e626SGreg Roach     * @return void
895a6f13a4aSGreg Roach     */
896c1010edaSGreg Roach    private function getPersonNameStartHandler($attrs)
897c1010edaSGreg Roach    {
8987a6ee1acSGreg Roach        $id    = '';
89913abd6f3SGreg Roach        $match = [];
900a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9017a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
902a6f13a4aSGreg Roach                $id = $match[1];
903a6f13a4aSGreg Roach            }
904a6f13a4aSGreg Roach        } else {
9057a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
906d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
907d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
908a6f13a4aSGreg Roach                }
909a6f13a4aSGreg Roach            } else {
9107a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91113abd6f3SGreg Roach                    $gmatch = [];
912a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
913a6f13a4aSGreg Roach                        $id = $gmatch[1];
914a6f13a4aSGreg Roach                    }
915a6f13a4aSGreg Roach                } else {
916a6f13a4aSGreg Roach                    $id = $attrs['id'];
917a6f13a4aSGreg Roach                }
918a6f13a4aSGreg Roach            }
919a6f13a4aSGreg Roach        }
920a6f13a4aSGreg Roach        if (!empty($id)) {
921299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9228f038c36SRico Sonntag            if ($record === null) {
923a6f13a4aSGreg Roach                return;
924a6f13a4aSGreg Roach            }
925a6f13a4aSGreg Roach            if (!$record->canShowName()) {
926a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
927a6f13a4aSGreg Roach            } else {
928a6f13a4aSGreg Roach                $name = $record->getFullName();
929a6f13a4aSGreg Roach                $name = preg_replace(
930c1010edaSGreg Roach                    [
931c1010edaSGreg Roach                        '/<span class="starredname">/',
932c1010edaSGreg Roach                        '/<\/span><\/span>/',
933c1010edaSGreg Roach                        '/<\/span>/',
934c1010edaSGreg Roach                    ],
935c1010edaSGreg Roach                    [
936c1010edaSGreg Roach                        '«',
937c1010edaSGreg Roach                        '',
938c1010edaSGreg Roach                        '»',
939c1010edaSGreg Roach                    ],
940a6f13a4aSGreg Roach                    $name
941a6f13a4aSGreg Roach                );
942a6f13a4aSGreg Roach                $name = strip_tags($name);
943a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
944a6f13a4aSGreg Roach                    if (mb_strlen($name) > $attrs['truncate']) {
945faffe0b0SGreg Roach                        $name = mb_substr($name, 0, $attrs['truncate'] - 1) . '…';
946a6f13a4aSGreg Roach                    }
947a6f13a4aSGreg Roach                } else {
948a6f13a4aSGreg Roach                    $addname = $record->getAddName();
949a6f13a4aSGreg Roach                    $addname = preg_replace(
950c1010edaSGreg Roach                        [
951c1010edaSGreg Roach                            '/<span class="starredname">/',
952c1010edaSGreg Roach                            '/<\/span><\/span>/',
953c1010edaSGreg Roach                            '/<\/span>/',
954c1010edaSGreg Roach                        ],
955c1010edaSGreg Roach                        [
956c1010edaSGreg Roach                            '«',
957c1010edaSGreg Roach                            '',
958c1010edaSGreg Roach                            '»',
959c1010edaSGreg Roach                        ],
960a6f13a4aSGreg Roach                        $addname
961a6f13a4aSGreg Roach                    );
962a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
963a6f13a4aSGreg Roach                    if (!empty($addname)) {
9647a6ee1acSGreg Roach                        $name .= ' ' . $addname;
965a6f13a4aSGreg Roach                    }
966a6f13a4aSGreg Roach                }
967a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
968a6f13a4aSGreg Roach            }
969a6f13a4aSGreg Roach        }
970a6f13a4aSGreg Roach    }
971a6f13a4aSGreg Roach
972a6f13a4aSGreg Roach    /**
97376692c8bSGreg Roach     * XML <GedcomValue/>
974a6f13a4aSGreg Roach     *
975a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
9768ba2e626SGreg Roach     *
9778ba2e626SGreg Roach     * @return void
978a6f13a4aSGreg Roach     */
979c1010edaSGreg Roach    private function gedcomValueStartHandler($attrs)
980c1010edaSGreg Roach    {
9817a6ee1acSGreg Roach        $id    = '';
98213abd6f3SGreg Roach        $match = [];
9837a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
984a6f13a4aSGreg Roach            $id = $match[1];
985a6f13a4aSGreg Roach        }
986a6f13a4aSGreg Roach
9877a6ee1acSGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] == '1') {
9887a6ee1acSGreg Roach            $useBreak = '1';
989a6f13a4aSGreg Roach        } else {
9907a6ee1acSGreg Roach            $useBreak = '0';
991a6f13a4aSGreg Roach        }
992a6f13a4aSGreg Roach
993a6f13a4aSGreg Roach        $tag = $attrs['tag'];
994a6f13a4aSGreg Roach        if (!empty($tag)) {
9957a6ee1acSGreg Roach            if ($tag == '@desc') {
996a6f13a4aSGreg Roach                $value = $this->desc;
997a6f13a4aSGreg Roach                $value = trim($value);
998a6f13a4aSGreg Roach                $this->current_element->addText($value);
999a6f13a4aSGreg Roach            }
10007a6ee1acSGreg Roach            if ($tag == '@id') {
1001a6f13a4aSGreg Roach                $this->current_element->addText($id);
1002a6f13a4aSGreg Roach            } else {
10037a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1004a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
10057a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
1006a6f13a4aSGreg Roach                    $level = $temp[0];
1007a6f13a4aSGreg Roach                    if ($level == 0) {
1008a6f13a4aSGreg Roach                        $level++;
1009a6f13a4aSGreg Roach                    }
1010a6f13a4aSGreg Roach                } else {
1011a6f13a4aSGreg Roach                    $level = $attrs['level'];
1012a6f13a4aSGreg Roach                }
1013a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10143d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1015a6f13a4aSGreg Roach                switch (end($tags)) {
1016a6f13a4aSGreg Roach                    case 'DATE':
1017a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1018a6f13a4aSGreg Roach                        $value = $tmp->display();
1019a6f13a4aSGreg Roach                        break;
1020a6f13a4aSGreg Roach                    case 'PLAC':
1021299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1022a6f13a4aSGreg Roach                        $value = $tmp->getShortName();
1023a6f13a4aSGreg Roach                        break;
1024a6f13a4aSGreg Roach                }
10257a6ee1acSGreg Roach                if ($useBreak == '1') {
1026a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1027a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1028a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1029a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1030a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
1031a6f13a4aSGreg Roach                    if (substr($value, 0, 6) == '<br>') {
1032a6f13a4aSGreg Roach                        $value = substr($value, 6);
1033a6f13a4aSGreg Roach                    }
1034a6f13a4aSGreg Roach                }
1035d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1036c1010edaSGreg Roach                if (in_array(end($tmp), [
1037c1010edaSGreg Roach                    'NOTE',
1038c1010edaSGreg Roach                    'TEXT',
1039c1010edaSGreg Roach                ])) {
1040299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1041a4d703aeSGreg Roach                }
1042a6f13a4aSGreg Roach                $this->current_element->addText($value);
1043a6f13a4aSGreg Roach            }
1044a6f13a4aSGreg Roach        }
1045a6f13a4aSGreg Roach    }
1046a6f13a4aSGreg Roach
1047a6f13a4aSGreg Roach    /**
104876692c8bSGreg Roach     * XML <RepeatTag>
1049a6f13a4aSGreg Roach     *
1050a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
10518ba2e626SGreg Roach     *
10528ba2e626SGreg Roach     * @return void
1053a6f13a4aSGreg Roach     */
1054c1010edaSGreg Roach    private function repeatTagStartHandler($attrs)
1055c1010edaSGreg Roach    {
1056a6f13a4aSGreg Roach        $this->process_repeats++;
1057a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1058a6f13a4aSGreg Roach            return;
1059a6f13a4aSGreg Roach        }
1060a6f13a4aSGreg Roach
10619b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
106213abd6f3SGreg Roach        $this->repeats         = [];
1063e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1064a6f13a4aSGreg Roach
10657a6ee1acSGreg Roach        $tag = '';
1066a6f13a4aSGreg Roach        if (isset($attrs['tag'])) {
1067a6f13a4aSGreg Roach            $tag = $attrs['tag'];
1068a6f13a4aSGreg Roach        }
1069a6f13a4aSGreg Roach        if (!empty($tag)) {
10707a6ee1acSGreg Roach            if ($tag == '@desc') {
1071a6f13a4aSGreg Roach                $value = $this->desc;
1072a6f13a4aSGreg Roach                $value = trim($value);
1073a6f13a4aSGreg Roach                $this->current_element->addText($value);
1074a6f13a4aSGreg Roach            } else {
10757a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10767a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10777a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1078a6f13a4aSGreg Roach                $level = $temp[0];
1079a6f13a4aSGreg Roach                if ($level == 0) {
1080a6f13a4aSGreg Roach                    $level++;
1081a6f13a4aSGreg Roach                }
1082a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1083a6f13a4aSGreg Roach                $t      = $tag;
1084a6f13a4aSGreg Roach                $count  = count($tags);
1085a6f13a4aSGreg Roach                $i      = 0;
1086a6f13a4aSGreg Roach                while ($i < $count) {
1087a6f13a4aSGreg Roach                    $t = $tags[$i];
1088a6f13a4aSGreg Roach                    if (!empty($t)) {
1089a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10903d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1091a6f13a4aSGreg Roach                            if (empty($subrec)) {
1092a6f13a4aSGreg Roach                                $level--;
10933d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1094a6f13a4aSGreg Roach                                if (empty($subrec)) {
1095a6f13a4aSGreg Roach                                    return;
1096a6f13a4aSGreg Roach                                }
1097a6f13a4aSGreg Roach                            }
1098a6f13a4aSGreg Roach                        }
1099a6f13a4aSGreg Roach                        $level++;
1100a6f13a4aSGreg Roach                    }
1101a6f13a4aSGreg Roach                    $i++;
1102a6f13a4aSGreg Roach                }
1103a6f13a4aSGreg Roach                $level--;
1104a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1105a6f13a4aSGreg Roach                $i     = 0;
1106a6f13a4aSGreg Roach                while ($i < $count) {
1107a6f13a4aSGreg Roach                    $i++;
1108a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1109a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
1110a9007102SGreg Roach                    if (preg_match('/^\d ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1111299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1112a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1113a9007102SGreg Roach                            continue;
1114a9007102SGreg Roach                        }
1115a9007102SGreg Roach                    }
1116a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1117a6f13a4aSGreg Roach                }
1118a6f13a4aSGreg Roach            }
1119a6f13a4aSGreg Roach        }
1120a6f13a4aSGreg Roach    }
1121a6f13a4aSGreg Roach
1122a6f13a4aSGreg Roach    /**
112376692c8bSGreg Roach     * XML </ RepeatTag>
11248ba2e626SGreg Roach     *
11258ba2e626SGreg Roach     * @return void
1126a6f13a4aSGreg Roach     */
1127c1010edaSGreg Roach    private function repeatTagEndHandler()
1128c1010edaSGreg Roach    {
1129a6f13a4aSGreg Roach        $this->process_repeats--;
1130a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1131a6f13a4aSGreg Roach            return;
1132a6f13a4aSGreg Roach        }
1133a6f13a4aSGreg Roach
1134a6f13a4aSGreg Roach        // Check if there is anything to repeat
1135a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1136a6f13a4aSGreg Roach            // No need to load them if not used...
1137a6f13a4aSGreg Roach
1138a6f13a4aSGreg Roach            $lineoffset = 0;
1139a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1140a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1141a6f13a4aSGreg Roach            }
1142a6f13a4aSGreg Roach            //-- read the xml from the file
1143299d100dSGreg Roach            $lines = file($this->report);
11447a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1145a6f13a4aSGreg Roach                $lineoffset--;
1146a6f13a4aSGreg Roach            }
1147a6f13a4aSGreg Roach            $lineoffset++;
1148a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1149a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1150a6f13a4aSGreg Roach            // RepeatTag Level counter
1151a6f13a4aSGreg Roach            $count = 1;
1152a6f13a4aSGreg Roach            while (0 < $count) {
11537a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1154a6f13a4aSGreg Roach                    $count++;
11557a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1156a6f13a4aSGreg Roach                    $count--;
1157a6f13a4aSGreg Roach                }
1158a6f13a4aSGreg Roach                if (0 < $count) {
1159a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1160a6f13a4aSGreg Roach                }
1161a6f13a4aSGreg Roach                $line_nr++;
1162a6f13a4aSGreg Roach            }
1163a6f13a4aSGreg Roach            // No need to drag this
1164a6f13a4aSGreg Roach            unset($lines);
1165a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1166a6f13a4aSGreg Roach            // Save original values
11679b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1168a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1169a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1170a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1171a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1172e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1173a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
1174*1aa04befSGreg Roach
1175*1aa04befSGreg Roach                xml_set_element_handler(
1176*1aa04befSGreg Roach                    $repeat_parser,
1177*1aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
1178*1aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
1179*1aa04befSGreg Roach                    },
1180*1aa04befSGreg Roach                    function ($parser, string $name) {
1181*1aa04befSGreg Roach                        $this->endElement($parser, $name);
1182*1aa04befSGreg Roach                    }
1183*1aa04befSGreg Roach                );
1184*1aa04befSGreg Roach
1185*1aa04befSGreg Roach                xml_set_character_data_handler(
1186*1aa04befSGreg Roach                    $repeat_parser,
1187*1aa04befSGreg Roach                    function ($parser, $data) {
1188*1aa04befSGreg Roach                        $this->characterData($parser, $data);
1189*1aa04befSGreg Roach                    }
1190*1aa04befSGreg Roach                );
1191*1aa04befSGreg Roach
1192a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1193a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1194a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1195a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1196a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1197a6f13a4aSGreg Roach                    ));
1198a6f13a4aSGreg Roach                }
1199a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1200a6f13a4aSGreg Roach            }
1201a6f13a4aSGreg Roach            // Restore original values
1202a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1203e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1204a6f13a4aSGreg Roach        }
1205a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1206a6f13a4aSGreg Roach    }
1207a6f13a4aSGreg Roach
1208a6f13a4aSGreg Roach    /**
1209a6f13a4aSGreg Roach     * Variable lookup
1210a6f13a4aSGreg Roach     * Retrieve predefined variables :
1211a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1212a6f13a4aSGreg Roach     *        1 EVEN This is a description
1213a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1214a6f13a4aSGreg Roach     * $ I18N::translate('....')
1215a6f13a4aSGreg Roach     * $ language_settings[]
1216a6f13a4aSGreg Roach     *
1217a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
12188ba2e626SGreg Roach     *
12198ba2e626SGreg Roach     * @return void
1220a6f13a4aSGreg Roach     */
1221c1010edaSGreg Roach    private function varStartHandler($attrs)
1222c1010edaSGreg Roach    {
1223a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
1224e8e7866bSGreg 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));
1225a6f13a4aSGreg Roach        }
1226a6f13a4aSGreg Roach
1227a6f13a4aSGreg Roach        $var = $attrs['var'];
1228a6f13a4aSGreg Roach        // SetVar element preset variables
1229d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1230d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1231a6f13a4aSGreg Roach        } else {
1232a6f13a4aSGreg Roach            $tfact = $this->fact;
12337a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1234a6f13a4aSGreg Roach                // Use :
1235a6f13a4aSGreg Roach                // n TYPE This text if string
1236a6f13a4aSGreg Roach                $tfact = $this->type;
1237a6f13a4aSGreg Roach            }
1238c1010edaSGreg Roach            $var = str_replace([
1239c1010edaSGreg Roach                '@fact',
1240c1010edaSGreg Roach                '@desc',
1241c1010edaSGreg Roach            ], [
1242c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1243c1010edaSGreg Roach                $this->desc,
1244c1010edaSGreg Roach            ], $var);
1245a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1246da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1247a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1248a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1249a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1250a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1251a6f13a4aSGreg Roach            }
1252a6f13a4aSGreg Roach        }
1253a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1254a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12557a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1256a6f13a4aSGreg Roach                $g   = new Date($var);
1257a6f13a4aSGreg Roach                $var = $g->display();
1258a6f13a4aSGreg Roach            }
1259a6f13a4aSGreg Roach        }
1260a6f13a4aSGreg Roach        $this->current_element->addText($var);
12612836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1262a6f13a4aSGreg Roach    }
1263a6f13a4aSGreg Roach
1264a6f13a4aSGreg Roach    /**
126576692c8bSGreg Roach     * XML <Facts>
126676692c8bSGreg Roach     *
1267a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
12688ba2e626SGreg Roach     *
12698ba2e626SGreg Roach     * @return void
1270a6f13a4aSGreg Roach     */
1271c1010edaSGreg Roach    private function factsStartHandler($attrs)
1272c1010edaSGreg Roach    {
1273a6f13a4aSGreg Roach        $this->process_repeats++;
1274a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1275a6f13a4aSGreg Roach            return;
1276a6f13a4aSGreg Roach        }
1277a6f13a4aSGreg Roach
12789b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
127913abd6f3SGreg Roach        $this->repeats         = [];
1280e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1281a6f13a4aSGreg Roach
12827a6ee1acSGreg Roach        $id    = '';
128313abd6f3SGreg Roach        $match = [];
12847a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1285a6f13a4aSGreg Roach            $id = $match[1];
1286a6f13a4aSGreg Roach        }
12877a6ee1acSGreg Roach        $tag = '';
1288a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1289a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1290a6f13a4aSGreg Roach        }
12917a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1292d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1293a6f13a4aSGreg Roach        }
1294a6f13a4aSGreg Roach
1295299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1296a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1297a6f13a4aSGreg Roach            $facts = $record->getFacts();
12983d7a8a4cSGreg Roach            Functions::sortFacts($facts);
129913abd6f3SGreg Roach            $this->repeats = [];
1300a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1301a6f13a4aSGreg Roach            foreach ($facts as $event) {
1302a6f13a4aSGreg Roach                if (!in_array($event->getTag(), $nonfacts)) {
1303a6f13a4aSGreg Roach                    $this->repeats[] = $event->getGedcom();
1304a6f13a4aSGreg Roach                }
1305a6f13a4aSGreg Roach            }
1306a6f13a4aSGreg Roach        } else {
1307a6f13a4aSGreg Roach            foreach ($record->getFacts() as $fact) {
1308a6f13a4aSGreg Roach                if ($fact->isPendingAddition() && $fact->getTag() !== 'CHAN') {
1309a6f13a4aSGreg Roach                    $this->repeats[] = $fact->getGedcom();
1310a6f13a4aSGreg Roach                }
1311a6f13a4aSGreg Roach            }
1312a6f13a4aSGreg Roach        }
1313a6f13a4aSGreg Roach    }
1314a6f13a4aSGreg Roach
1315a6f13a4aSGreg Roach    /**
131676692c8bSGreg Roach     * XML </Facts>
13178ba2e626SGreg Roach     *
13188ba2e626SGreg Roach     * @return void
1319a6f13a4aSGreg Roach     */
1320c1010edaSGreg Roach    private function factsEndHandler()
1321c1010edaSGreg Roach    {
1322a6f13a4aSGreg Roach        $this->process_repeats--;
1323a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1324a6f13a4aSGreg Roach            return;
1325a6f13a4aSGreg Roach        }
1326a6f13a4aSGreg Roach
1327a6f13a4aSGreg Roach        // Check if there is anything to repeat
1328a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1329e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1330a6f13a4aSGreg Roach            $lineoffset = 0;
1331a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1332a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1333a6f13a4aSGreg Roach            }
1334a6f13a4aSGreg Roach
1335a6f13a4aSGreg Roach            //-- read the xml from the file
1336299d100dSGreg Roach            $lines = file($this->report);
1337a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1338a6f13a4aSGreg Roach                $lineoffset--;
1339a6f13a4aSGreg Roach            }
1340a6f13a4aSGreg Roach            $lineoffset++;
1341a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1342a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1343a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1344a6f13a4aSGreg Roach            while ($line_nr < $i) {
1345a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1346a6f13a4aSGreg Roach                $line_nr++;
1347a6f13a4aSGreg Roach            }
1348a6f13a4aSGreg Roach            // No need to drag this
1349a6f13a4aSGreg Roach            unset($lines);
1350a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1351a6f13a4aSGreg Roach            // Save original values
13529b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1353a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1354a6f13a4aSGreg Roach            $count                = count($this->repeats);
1355a6f13a4aSGreg Roach            $i                    = 0;
1356a6f13a4aSGreg Roach            while ($i < $count) {
1357a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1358a6f13a4aSGreg Roach                $this->fact   = '';
1359a6f13a4aSGreg Roach                $this->desc   = '';
1360a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1361a6f13a4aSGreg Roach                    $this->fact = $match[1];
1362a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
136313abd6f3SGreg Roach                        $tmatch = [];
1364a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1365a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1366a6f13a4aSGreg Roach                        } else {
1367a6f13a4aSGreg Roach                            $this->type = ' ';
1368a6f13a4aSGreg Roach                        }
1369a6f13a4aSGreg Roach                    }
1370a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13713d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1372a6f13a4aSGreg Roach                }
1373a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1374e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1375a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
1376*1aa04befSGreg Roach
1377*1aa04befSGreg Roach                xml_set_element_handler(
1378*1aa04befSGreg Roach                    $repeat_parser,
1379*1aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
1380*1aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
1381*1aa04befSGreg Roach                    },
1382*1aa04befSGreg Roach                    function ($parser, string $name) {
1383*1aa04befSGreg Roach                        $this->endElement($parser, $name);
1384*1aa04befSGreg Roach                    }
1385*1aa04befSGreg Roach                );
1386*1aa04befSGreg Roach
1387*1aa04befSGreg Roach                xml_set_character_data_handler(
1388*1aa04befSGreg Roach                    $repeat_parser,
1389*1aa04befSGreg Roach                    function ($parser, $data) {
1390*1aa04befSGreg Roach                        $this->characterData($parser, $data);
1391*1aa04befSGreg Roach                    }
1392*1aa04befSGreg Roach                );
1393*1aa04befSGreg Roach
1394a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1395a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1396a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1397a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1398a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1399a6f13a4aSGreg Roach                    ));
1400a6f13a4aSGreg Roach                }
1401a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1402a6f13a4aSGreg Roach                $i++;
1403a6f13a4aSGreg Roach            }
1404a6f13a4aSGreg Roach            // Restore original values
1405e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1406a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1407a6f13a4aSGreg Roach        }
1408a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
1409a6f13a4aSGreg Roach    }
1410a6f13a4aSGreg Roach
1411a6f13a4aSGreg Roach    /**
1412a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1413d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1414a6f13a4aSGreg Roach     *
1415a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
14168ba2e626SGreg Roach     *
14178ba2e626SGreg Roach     * @return void
1418a6f13a4aSGreg Roach     */
1419c1010edaSGreg Roach    private function setVarStartHandler($attrs)
1420c1010edaSGreg Roach    {
1421a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
1422a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1423a6f13a4aSGreg Roach        }
1424a6f13a4aSGreg Roach
1425a6f13a4aSGreg Roach        $name  = $attrs['name'];
1426a6f13a4aSGreg Roach        $value = $attrs['value'];
142713abd6f3SGreg Roach        $match = [];
1428a6f13a4aSGreg Roach        // Current GEDCOM record strings
14297a6ee1acSGreg Roach        if ($value == '@ID') {
14307a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1431a6f13a4aSGreg Roach                $value = $match[1];
1432a6f13a4aSGreg Roach            }
14337a6ee1acSGreg Roach        } elseif ($value == '@fact') {
1434a6f13a4aSGreg Roach            $value = $this->fact;
14357a6ee1acSGreg Roach        } elseif ($value == '@desc') {
1436a6f13a4aSGreg Roach            $value = $this->desc;
14377a6ee1acSGreg Roach        } elseif ($value == '@generation') {
1438589feda3SGreg Roach            $value = (string) $this->generation;
1439a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144013abd6f3SGreg Roach            $gmatch = [];
1441a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14427a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1443a6f13a4aSGreg Roach            }
1444a6f13a4aSGreg Roach        }
1445a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1446d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1447a6f13a4aSGreg Roach        }
1448a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1449a6f13a4aSGreg Roach        $i     = 0;
1450a6f13a4aSGreg Roach        while ($i < $count) {
1451d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14527a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1453a6f13a4aSGreg Roach            $i++;
1454a6f13a4aSGreg Roach        }
1455a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1456da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1457a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1458a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1459a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1460a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1461a6f13a4aSGreg Roach        }
1462a6f13a4aSGreg Roach        // Arithmetic functions
1463a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
1464a6f13a4aSGreg Roach            switch ($match[2]) {
14657a6ee1acSGreg Roach                case '+':
1466a6f13a4aSGreg Roach                    $t     = $match[1] + $match[3];
14677a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1468a6f13a4aSGreg Roach                    break;
14697a6ee1acSGreg Roach                case '-':
1470a6f13a4aSGreg Roach                    $t     = $match[1] - $match[3];
14717a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1472a6f13a4aSGreg Roach                    break;
14737a6ee1acSGreg Roach                case '*':
1474a6f13a4aSGreg Roach                    $t     = $match[1] * $match[3];
14757a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1476a6f13a4aSGreg Roach                    break;
14777a6ee1acSGreg Roach                case '/':
1478a6f13a4aSGreg Roach                    $t     = $match[1] / $match[3];
14797a6ee1acSGreg Roach                    $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value);
1480a6f13a4aSGreg Roach                    break;
1481a6f13a4aSGreg Roach            }
1482a6f13a4aSGreg Roach        }
14837a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14847a6ee1acSGreg Roach            $value = '';
1485a6f13a4aSGreg Roach        }
1486d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1487a6f13a4aSGreg Roach    }
1488a6f13a4aSGreg Roach
1489a6f13a4aSGreg Roach    /**
1490a6f13a4aSGreg Roach     * XML <if > start element
1491a6f13a4aSGreg Roach     *
1492a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
14938ba2e626SGreg Roach     *
14948ba2e626SGreg Roach     * @return void
1495a6f13a4aSGreg Roach     */
1496c1010edaSGreg Roach    private function ifStartHandler($attrs)
1497c1010edaSGreg Roach    {
1498a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1499a6f13a4aSGreg Roach            $this->process_ifs++;
1500a6f13a4aSGreg Roach
1501a6f13a4aSGreg Roach            return;
1502a6f13a4aSGreg Roach        }
1503a6f13a4aSGreg Roach
1504a6f13a4aSGreg Roach        $condition = $attrs['condition'];
150582759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1506c1010edaSGreg Roach        $condition = str_replace([
1507c1010edaSGreg Roach            ' LT ',
1508c1010edaSGreg Roach            ' GT ',
1509c1010edaSGreg Roach        ], [
1510c1010edaSGreg Roach            '<',
1511c1010edaSGreg Roach            '>',
1512c1010edaSGreg Roach        ], $condition);
1513a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15147a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
151513abd6f3SGreg Roach        $match     = [];
1516a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1517a6f13a4aSGreg Roach        $i         = 0;
1518a6f13a4aSGreg Roach        while ($i < $count) {
1519a6f13a4aSGreg Roach            $id    = $match[$i][1];
1520a6f13a4aSGreg Roach            $value = '""';
15217a6ee1acSGreg Roach            if ($id == 'ID') {
15227a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1523a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1524a6f13a4aSGreg Roach                }
15257a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1526a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15277a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1528a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15297a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1530a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1531a6f13a4aSGreg Roach            } else {
15327a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1533a6f13a4aSGreg Roach                $level = $temp[0];
1534a6f13a4aSGreg Roach                if ($level == 0) {
1535a6f13a4aSGreg Roach                    $level++;
1536a6f13a4aSGreg Roach                }
15373d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1538a6f13a4aSGreg Roach                if (empty($value)) {
1539a6f13a4aSGreg Roach                    $level++;
15403d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1541a6f13a4aSGreg Roach                }
15425e8c88c1SGreg Roach                $value = preg_replace('/^@(' . WT_REGEX_XREF . ')@$/', '$1', $value);
15435e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1544a6f13a4aSGreg Roach            }
1545a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1546a6f13a4aSGreg Roach            $i++;
1547a6f13a4aSGreg Roach        }
15485809450fSGreg Roach
1549cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1550cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1551cb63a60eSGreg Roach        $expression_language  = new ExpressionLanguage(null, [$expression_provider]);
1552cb63a60eSGreg Roach
1553cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15545809450fSGreg Roach
1555a6f13a4aSGreg Roach        if (!$ret) {
1556a6f13a4aSGreg Roach            $this->process_ifs++;
1557a6f13a4aSGreg Roach        }
1558a6f13a4aSGreg Roach    }
1559a6f13a4aSGreg Roach
1560a6f13a4aSGreg Roach    /**
1561a6f13a4aSGreg Roach     * XML <if /> end element
15628ba2e626SGreg Roach     *
15638ba2e626SGreg Roach     * @return void
1564a6f13a4aSGreg Roach     */
1565c1010edaSGreg Roach    private function ifEndHandler()
1566c1010edaSGreg Roach    {
1567a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1568a6f13a4aSGreg Roach            $this->process_ifs--;
1569a6f13a4aSGreg Roach        }
1570a6f13a4aSGreg Roach    }
1571a6f13a4aSGreg Roach
1572a6f13a4aSGreg Roach    /**
1573a6f13a4aSGreg Roach     * XML <Footnote > start element
1574a6f13a4aSGreg Roach     * Collect the Footnote links
1575a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1576a6f13a4aSGreg Roach     *
1577a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
15788ba2e626SGreg Roach     *
15798ba2e626SGreg Roach     * @return void
1580a6f13a4aSGreg Roach     */
1581c1010edaSGreg Roach    private function footnoteStartHandler($attrs)
1582c1010edaSGreg Roach    {
15837a6ee1acSGreg Roach        $id = '';
15847a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1585a6f13a4aSGreg Roach            $id = $match[2];
1586a6f13a4aSGreg Roach        }
1587299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1588a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15899b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1590a6f13a4aSGreg Roach            $this->print_data         = true;
15917a6ee1acSGreg Roach            $style                    = '';
1592a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1593a6f13a4aSGreg Roach                $style = $attrs['style'];
1594a6f13a4aSGreg Roach            }
1595a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1596e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1597a6f13a4aSGreg Roach        } else {
1598a6f13a4aSGreg Roach            $this->print_data       = false;
1599a6f13a4aSGreg Roach            $this->process_footnote = false;
1600a6f13a4aSGreg Roach        }
1601a6f13a4aSGreg Roach    }
1602a6f13a4aSGreg Roach
1603a6f13a4aSGreg Roach    /**
1604a6f13a4aSGreg Roach     * XML <Footnote /> end element
1605a6f13a4aSGreg Roach     * Print the collected Footnote data
16068ba2e626SGreg Roach     *
16078ba2e626SGreg Roach     * @return void
1608a6f13a4aSGreg Roach     */
1609c1010edaSGreg Roach    private function footnoteEndHandler()
1610c1010edaSGreg Roach    {
1611a6f13a4aSGreg Roach        if ($this->process_footnote) {
1612a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1613a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1614a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1615e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1616a6f13a4aSGreg Roach            }
1617a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1618a6f13a4aSGreg Roach        } else {
1619a6f13a4aSGreg Roach            $this->process_footnote = true;
1620a6f13a4aSGreg Roach        }
1621a6f13a4aSGreg Roach    }
1622a6f13a4aSGreg Roach
1623a6f13a4aSGreg Roach    /**
1624a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16258ba2e626SGreg Roach     *
16268ba2e626SGreg Roach     * @return void
1627a6f13a4aSGreg Roach     */
1628c1010edaSGreg Roach    private function footnoteTextsStartHandler()
1629c1010edaSGreg Roach    {
16307a6ee1acSGreg Roach        $temp = 'footnotetexts';
1631e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1632a6f13a4aSGreg Roach    }
1633a6f13a4aSGreg Roach
1634a6f13a4aSGreg Roach    /**
1635a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16368ba2e626SGreg Roach     *
16378ba2e626SGreg Roach     * @return void
1638a6f13a4aSGreg Roach     */
1639c1010edaSGreg Roach    private function brStartHandler()
1640c1010edaSGreg Roach    {
1641a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1642a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1643a6f13a4aSGreg Roach        }
1644a6f13a4aSGreg Roach    }
1645a6f13a4aSGreg Roach
1646a6f13a4aSGreg Roach    /**
1647a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16488ba2e626SGreg Roach     *
16498ba2e626SGreg Roach     * @return void
1650a6f13a4aSGreg Roach     */
1651c1010edaSGreg Roach    private function spStartHandler()
1652c1010edaSGreg Roach    {
1653a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1654a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1655a6f13a4aSGreg Roach        }
1656a6f13a4aSGreg Roach    }
1657a6f13a4aSGreg Roach
1658a6f13a4aSGreg Roach    /**
165976692c8bSGreg Roach     * XML <HighlightedImage/>
166076692c8bSGreg Roach     *
1661a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
16628ba2e626SGreg Roach     *
16638ba2e626SGreg Roach     * @return void
1664a6f13a4aSGreg Roach     */
1665c1010edaSGreg Roach    private function highlightedImageStartHandler($attrs)
1666c1010edaSGreg Roach    {
1667a6f13a4aSGreg Roach        $id = '';
16687a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1669a6f13a4aSGreg Roach            $id = $match[1];
1670a6f13a4aSGreg Roach        }
1671a6f13a4aSGreg Roach
167283cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
167383cdc021SGreg Roach        $top = (int) ($attrs['top'] ?? -1);
1674a6f13a4aSGreg Roach
1675a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
167683cdc021SGreg Roach        $left = (int) ($attrs['left'] ?? -1);
1677a6f13a4aSGreg Roach
167883cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167983cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1680a6f13a4aSGreg Roach
1681a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
168283cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1683a6f13a4aSGreg Roach
168483cdc021SGreg Roach        // Width, height (or both).
168583cdc021SGreg Roach        $width  = (int) ($attrs['width'] ?? 0);
168683cdc021SGreg Roach        $height = (int) ($attrs['height'] ?? 0);
1687a6f13a4aSGreg Roach
1688299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16894a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
169086a63f51SGreg Roach
169186a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1692c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1693c1010edaSGreg Roach                0,
1694c1010edaSGreg Roach                0,
1695c1010edaSGreg Roach            ];
1696a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16973c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16983c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1699a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17003c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17013c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1702a6f13a4aSGreg Roach            } else {
17033c3b90deSGreg Roach                $width  = $attributes[0];
17043c3b90deSGreg Roach                $height = $attributes[1];
1705a6f13a4aSGreg Roach            }
17064a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1707e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1708a6f13a4aSGreg Roach        }
1709a6f13a4aSGreg Roach    }
1710a6f13a4aSGreg Roach
1711a6f13a4aSGreg Roach    /**
171276692c8bSGreg Roach     * XML <Image/>
171376692c8bSGreg Roach     *
1714a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
17158ba2e626SGreg Roach     *
17168ba2e626SGreg Roach     * @return void
1717a6f13a4aSGreg Roach     */
1718c1010edaSGreg Roach    private function imageStartHandler($attrs)
1719c1010edaSGreg Roach    {
172083cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
172183cdc021SGreg Roach        $top = (int) ($attrs['top'] ?? -1);
1722a6f13a4aSGreg Roach
1723a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
172483cdc021SGreg Roach        $left = (int) ($attrs['left'] ?? -1);
1725a6f13a4aSGreg Roach
172683cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172783cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1728a6f13a4aSGreg Roach
1729a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
173083cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1731a6f13a4aSGreg Roach
173283cdc021SGreg Roach        // Width, height (or both).
173383cdc021SGreg Roach        $width  = (int) ($attrs['width'] ?? 0);
173483cdc021SGreg Roach        $height = (int) ($attrs['height'] ?? 0);
1735a6f13a4aSGreg Roach
173683cdc021SGreg Roach        $file = $attrs['file'] ?? '';
173783cdc021SGreg Roach
17387a6ee1acSGreg Roach        if ($file == '@FILE') {
173913abd6f3SGreg Roach            $match = [];
1740a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1741299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17424a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1743cdf416fbSGreg Roach
17444a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1745c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1746c1010edaSGreg Roach                        0,
1747c1010edaSGreg Roach                        0,
1748c1010edaSGreg Roach                    ];
1749a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17503c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17513c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1752a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17533c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17543c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1755a6f13a4aSGreg Roach                    } else {
17563c3b90deSGreg Roach                        $width  = $attributes[0];
17573c3b90deSGreg Roach                        $height = $attributes[1];
1758a6f13a4aSGreg Roach                    }
17594a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1760e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1761a6f13a4aSGreg Roach                }
1762a6f13a4aSGreg Roach            }
1763a6f13a4aSGreg Roach        } else {
17647a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1765a6f13a4aSGreg Roach                $size = getimagesize($file);
1766a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1767a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1768a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1769a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1770a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1771a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1772a6f13a4aSGreg Roach                } else {
1773a6f13a4aSGreg Roach                    $width  = $size[0];
1774a6f13a4aSGreg Roach                    $height = $size[1];
1775a6f13a4aSGreg Roach                }
1776e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1777e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1778a6f13a4aSGreg Roach            }
1779a6f13a4aSGreg Roach        }
1780a6f13a4aSGreg Roach    }
1781a6f13a4aSGreg Roach
1782a6f13a4aSGreg Roach    /**
1783a6f13a4aSGreg Roach     * XML <Line> element handler
1784a6f13a4aSGreg Roach     *
1785a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
17868ba2e626SGreg Roach     *
17878ba2e626SGreg Roach     * @return void
1788a6f13a4aSGreg Roach     */
1789c1010edaSGreg Roach    private function lineStartHandler($attrs)
1790c1010edaSGreg Roach    {
1791a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
17927a6ee1acSGreg Roach        $x1 = '.';
1793a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17947a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1795a6f13a4aSGreg Roach                $x1 = 0;
17967a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
17977a6ee1acSGreg Roach                $x1 = '.';
1798a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1799a6f13a4aSGreg Roach                $x1 = (int) $attrs['x1'];
1800a6f13a4aSGreg Roach            }
1801a6f13a4aSGreg Roach        }
1802a6f13a4aSGreg Roach        // Start vertical position, current position (default)
18037a6ee1acSGreg Roach        $y1 = '.';
1804a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18057a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1806a6f13a4aSGreg Roach                $y1 = 0;
18077a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
18087a6ee1acSGreg Roach                $y1 = '.';
1809a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1810a6f13a4aSGreg Roach                $y1 = (int) $attrs['y1'];
1811a6f13a4aSGreg Roach            }
1812a6f13a4aSGreg Roach        }
1813a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
18147a6ee1acSGreg Roach        $x2 = '.';
1815a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18167a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1817a6f13a4aSGreg Roach                $x2 = 0;
18187a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
18197a6ee1acSGreg Roach                $x2 = '.';
1820a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1821a6f13a4aSGreg Roach                $x2 = (int) $attrs['x2'];
1822a6f13a4aSGreg Roach            }
1823a6f13a4aSGreg Roach        }
1824a6f13a4aSGreg Roach        // End vertical position
18257a6ee1acSGreg Roach        $y2 = '.';
1826a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18277a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1828a6f13a4aSGreg Roach                $y2 = 0;
18297a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
18307a6ee1acSGreg Roach                $y2 = '.';
1831a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1832a6f13a4aSGreg Roach                $y2 = (int) $attrs['y2'];
1833a6f13a4aSGreg Roach            }
1834a6f13a4aSGreg Roach        }
1835a6f13a4aSGreg Roach
1836e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1837e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1838a6f13a4aSGreg Roach    }
1839a6f13a4aSGreg Roach
1840a6f13a4aSGreg Roach    /**
184176692c8bSGreg Roach     * XML <List>
1842a6f13a4aSGreg Roach     *
1843a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
18448ba2e626SGreg Roach     *
18458ba2e626SGreg Roach     * @return void
1846a6f13a4aSGreg Roach     */
1847c1010edaSGreg Roach    private function listStartHandler($attrs)
1848c1010edaSGreg Roach    {
1849a6f13a4aSGreg Roach        $this->process_repeats++;
1850a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1851a6f13a4aSGreg Roach            return;
1852a6f13a4aSGreg Roach        }
1853a6f13a4aSGreg Roach
185413abd6f3SGreg Roach        $match = [];
1855a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1856a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1857a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1858d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1859a6f13a4aSGreg Roach                $sortby = trim($sortby);
1860a6f13a4aSGreg Roach            }
1861a6f13a4aSGreg Roach        } else {
18627a6ee1acSGreg Roach            $sortby = 'NAME';
1863a6f13a4aSGreg Roach        }
1864a6f13a4aSGreg Roach
1865a6f13a4aSGreg Roach        if (isset($attrs['list'])) {
1866a6f13a4aSGreg Roach            $listname = $attrs['list'];
1867a6f13a4aSGreg Roach        } else {
18687a6ee1acSGreg Roach            $listname = 'individual';
1869a6f13a4aSGreg Roach        }
1870a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1871a6f13a4aSGreg Roach        switch ($listname) {
18727a6ee1acSGreg Roach            case 'pending':
1873a6f13a4aSGreg Roach                $rows = Database::prepare(
18745d0bc43dSGreg Roach                    "SELECT xref, CASE new_gedcom WHEN '' THEN old_gedcom ELSE new_gedcom END AS gedcom" .
18755d0bc43dSGreg Roach                    " FROM `##change`" . " WHERE (xref, change_id) IN (" .
18765d0bc43dSGreg Roach                    "  SELECT xref, MAX(change_id)" .
18775d0bc43dSGreg Roach                    "  FROM `##change`" .
18785d0bc43dSGreg Roach                    "  WHERE status = 'pending' AND gedcom_id = :tree_id" .
18795d0bc43dSGreg Roach                    "  GROUP BY xref" .
18805d0bc43dSGreg Roach                    " )"
188113abd6f3SGreg Roach                )->execute([
1882299d100dSGreg Roach                    'tree_id' => $this->tree->getTreeId(),
188313abd6f3SGreg Roach                ])->fetchAll();
188413abd6f3SGreg Roach                $this->list = [];
1885a6f13a4aSGreg Roach                foreach ($rows as $row) {
1886299d100dSGreg Roach                    $this->list[] = GedcomRecord::getInstance($row->xref, $this->tree, $row->gedcom);
1887a6f13a4aSGreg Roach                }
1888a6f13a4aSGreg Roach                break;
1889a6f13a4aSGreg Roach            case 'individual':
189076156db1SGreg Roach                $sql_select   = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` ";
1891a6f13a4aSGreg Roach                $sql_join     = "";
1892825006d2SGreg Roach                $sql_where    = " WHERE i_file = :tree_id";
1893a6f13a4aSGreg Roach                $sql_order_by = "";
1894299d100dSGreg Roach                $sql_params   = ['tree_id' => $this->tree->getTreeId()];
1895a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1896a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
189782759250SGreg Roach                        $value = $this->substituteVars($value, false);
1898a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1899a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1900a6f13a4aSGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)";
1901b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
19025d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1903a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
19047a6ee1acSGreg Roach                            if ($match[2] == 'LTE') {
19055d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19065d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1907a6f13a4aSGreg Roach                            } else {
19085d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19095d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1910a6f13a4aSGreg Roach                            }
1911a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
1912a6f13a4aSGreg Roach                                $sortby = "";
1913a6f13a4aSGreg Roach                                $sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1";
1914a6f13a4aSGreg Roach                            }
1915a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1916a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
1917a6f13a4aSGreg Roach                            // Do nothing, unless you have to
1918a6f13a4aSGreg Roach                            if ($match[1] != '' || $sortby == 'NAME') {
1919a6f13a4aSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)";
1920a6f13a4aSGreg Roach                                // Search the DB only if there is any name supplied
19217a6ee1acSGreg Roach                                if ($match[1] != '') {
19227a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19235d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19245d0bc43dSGreg Roach                                        $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19255d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
1926a6f13a4aSGreg Roach                                    }
1927a6f13a4aSGreg Roach                                }
1928a6f13a4aSGreg Roach                                // Let the DB do the name sorting even when no name was entered
19297a6ee1acSGreg Roach                                if ($sortby == 'NAME') {
19307a6ee1acSGreg Roach                                    $sortby = '';
19317a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1932a6f13a4aSGreg Roach                                }
1933a6f13a4aSGreg Roach                            }
1934a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1935a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19365d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom";
1937b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1938b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1939a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1940a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
19414f927915SGreg Roach                            // Don't unset this filter. This is just initial filtering
1942a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)";
1943a6f13a4aSGreg 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)";
19445d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19455d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1946a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19474f927915SGreg Roach                            // Don't unset this filter. This is just initial filtering
19485d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19495d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
19505d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
19515d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
1952a6f13a4aSGreg Roach                        }
1953a6f13a4aSGreg Roach                    }
1954a6f13a4aSGreg Roach                }
1955a6f13a4aSGreg Roach
195613abd6f3SGreg Roach                $this->list = [];
1957a6f13a4aSGreg Roach                $rows       = Database::prepare(
1958a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
19595d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
1960a6f13a4aSGreg Roach
1961a6f13a4aSGreg Roach                foreach ($rows as $row) {
1962299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1963a6f13a4aSGreg Roach                }
1964a6f13a4aSGreg Roach                break;
1965a6f13a4aSGreg Roach
1966a6f13a4aSGreg Roach            case 'family':
196776156db1SGreg Roach                $sql_select   = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`";
1968a6f13a4aSGreg Roach                $sql_join     = "";
1969825006d2SGreg Roach                $sql_where    = " WHERE f_file = :tree_id";
1970a6f13a4aSGreg Roach                $sql_order_by = "";
1971299d100dSGreg Roach                $sql_params   = ['tree_id' => $this->tree->getTreeId()];
1972a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1973a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
197482759250SGreg Roach                        $value = $this->substituteVars($value, false);
1975a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1976a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1977a9eb55f8SGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)";
1978b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
19795d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1980a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
19817a6ee1acSGreg Roach                            if ($match[2] == 'LTE') {
19825d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19835d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1984a6f13a4aSGreg Roach                            } else {
19855d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19865d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1987a6f13a4aSGreg Roach                            }
1988a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
19897a6ee1acSGreg Roach                                $sortby = '';
19907a6ee1acSGreg Roach                                $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1";
1991a6f13a4aSGreg Roach                            }
1992a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1993a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19945d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom";
1995b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1996b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1997a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1998a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19995d0bc43dSGreg Roach                            // Do nothing, unless you have to
20005d0bc43dSGreg Roach                            if ($match[1] != '' || $sortby == 'NAME') {
20015d0bc43dSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)";
20025d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20037a6ee1acSGreg Roach                                if ($match[1] != '') {
20047a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20055d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
20065d0bc43dSGreg Roach                                        $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
20075d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
20085d0bc43dSGreg Roach                                    }
20095d0bc43dSGreg Roach                                }
20105d0bc43dSGreg Roach                                // Let the DB do the name sorting even when no name was entered
20117a6ee1acSGreg Roach                                if ($sortby == 'NAME') {
20127a6ee1acSGreg Roach                                    $sortby = '';
20137a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
2014a6f13a4aSGreg Roach                                }
20155d0bc43dSGreg Roach                            }
2016a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
2017a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
2018a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)";
2019a6f13a4aSGreg 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)";
20205d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
20215d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
2022a6f13a4aSGreg Roach                        // Don't unset this filter. This is just initial filtering
2023a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
20245d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
20255d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
20265d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
20275d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
2028a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
2029a6f13a4aSGreg Roach                        }
2030a6f13a4aSGreg Roach                    }
2031a6f13a4aSGreg Roach                }
2032a6f13a4aSGreg Roach
203313abd6f3SGreg Roach                $this->list = [];
2034a6f13a4aSGreg Roach                $rows       = Database::prepare(
2035a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
20365d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
2037a6f13a4aSGreg Roach
2038a6f13a4aSGreg Roach                foreach ($rows as $row) {
2039299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2040a6f13a4aSGreg Roach                }
2041a6f13a4aSGreg Roach                break;
2042a6f13a4aSGreg Roach
2043a6f13a4aSGreg Roach            default:
2044a6f13a4aSGreg Roach                throw new \DomainException('Invalid list name: ' . $listname);
2045a6f13a4aSGreg Roach        }
2046a6f13a4aSGreg Roach
204713abd6f3SGreg Roach        $filters  = [];
204813abd6f3SGreg Roach        $filters2 = [];
2049a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2050a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2051a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2052a6f13a4aSGreg Roach                    $condition = $value;
2053a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2054a6f13a4aSGreg Roach                        $id    = $match[1];
2055a6f13a4aSGreg Roach                        $value = "''";
20567a6ee1acSGreg Roach                        if ($id == 'ID') {
20577a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2058a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2059a6f13a4aSGreg Roach                            }
20607a6ee1acSGreg Roach                        } elseif ($id == 'fact') {
2061a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
20627a6ee1acSGreg Roach                        } elseif ($id == 'desc') {
2063a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2064a6f13a4aSGreg Roach                        } else {
2065a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20667a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2067a6f13a4aSGreg Roach                            }
2068a6f13a4aSGreg Roach                        }
2069a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2070a6f13a4aSGreg Roach                    }
2071a6f13a4aSGreg Roach                    //-- handle regular expressions
2072a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2073a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2074a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2075a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2076a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2077d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2078a6f13a4aSGreg Roach                            $val = trim($val);
2079a6f13a4aSGreg Roach                        }
2080a6f13a4aSGreg Roach                        if ($val) {
20817a6ee1acSGreg Roach                            $searchstr = '';
20827a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2083a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2084a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2085a6f13a4aSGreg Roach                                $level = 1;
2086a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2087a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2088a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2089a6f13a4aSGreg Roach                                    }
2090a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
20917a6ee1acSGreg Roach                                    if ($t == 'EMAIL' || $t == '_EMAIL') {
20927a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2093a6f13a4aSGreg Roach                                    }
20947a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2095a6f13a4aSGreg Roach                                    $level++;
2096a6f13a4aSGreg Roach                                }
2097a6f13a4aSGreg Roach                            } else {
20987a6ee1acSGreg Roach                                if ($tag == 'EMAIL' || $tag == '_EMAIL') {
20997a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2100a6f13a4aSGreg Roach                                }
2101a6f13a4aSGreg Roach                                $t         = $tag;
21027a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2103a6f13a4aSGreg Roach                            }
2104a6f13a4aSGreg Roach                            switch ($expr) {
21057a6ee1acSGreg Roach                                case 'CONTAINS':
21067a6ee1acSGreg Roach                                    if ($t == 'PLAC') {
2107a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2108a6f13a4aSGreg Roach                                    } else {
2109a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2110a6f13a4aSGreg Roach                                    }
2111a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2112a6f13a4aSGreg Roach                                    break;
2113a6f13a4aSGreg Roach                                default:
2114c1010edaSGreg Roach                                    $filters2[] = [
2115c1010edaSGreg Roach                                        'tag'  => $tag,
2116c1010edaSGreg Roach                                        'expr' => $expr,
2117c1010edaSGreg Roach                                        'val'  => $val,
2118c1010edaSGreg Roach                                    ];
2119a6f13a4aSGreg Roach                                    break;
2120a6f13a4aSGreg Roach                            }
2121a6f13a4aSGreg Roach                        }
2122a6f13a4aSGreg Roach                    }
2123a6f13a4aSGreg Roach                }
2124a6f13a4aSGreg Roach            }
2125a6f13a4aSGreg Roach        }
2126a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2127a6f13a4aSGreg Roach        if ($filters) {
2128a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2129a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2130299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2131a6f13a4aSGreg Roach                        unset($this->list[$key]);
2132a6f13a4aSGreg Roach                        break;
2133a6f13a4aSGreg Roach                    }
2134a6f13a4aSGreg Roach                }
2135a6f13a4aSGreg Roach            }
2136a6f13a4aSGreg Roach        }
2137a6f13a4aSGreg Roach        if ($filters2) {
213813abd6f3SGreg Roach            $mylist = [];
2139a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2140a6f13a4aSGreg Roach                $key  = $indi->getXref();
2141299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2142a6f13a4aSGreg Roach                $keep = true;
2143a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2144a6f13a4aSGreg Roach                    if ($keep) {
2145a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2146a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2147a6f13a4aSGreg Roach                        $val  = $filter['val'];
2148a6f13a4aSGreg Roach                        if ($val == "''") {
21497a6ee1acSGreg Roach                            $val = '';
2150a6f13a4aSGreg Roach                        }
21517a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2152a6f13a4aSGreg Roach                        $t    = end($tags);
21533d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2154a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
21557a6ee1acSGreg Roach                        if ($t == 'EMAIL' && empty($v)) {
21567a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21577a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2158a6f13a4aSGreg Roach                            $t    = end($tags);
21593d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2160a6f13a4aSGreg Roach                        }
2161a6f13a4aSGreg Roach
2162a6f13a4aSGreg Roach                        switch ($expr) {
21637a6ee1acSGreg Roach                            case 'GTE':
21647a6ee1acSGreg Roach                                if ($t == 'DATE') {
2165a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2166a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2167a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2168a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2169a6f13a4aSGreg Roach                                    $keep = true;
2170a6f13a4aSGreg Roach                                }
2171a6f13a4aSGreg Roach                                break;
21727a6ee1acSGreg Roach                            case 'LTE':
21737a6ee1acSGreg Roach                                if ($t == 'DATE') {
2174a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2175a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2176a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2177a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2178a6f13a4aSGreg Roach                                    $keep = true;
2179a6f13a4aSGreg Roach                                }
2180a6f13a4aSGreg Roach                                break;
2181a6f13a4aSGreg Roach                            default:
2182a6f13a4aSGreg Roach                                if ($v == $val) {
2183a6f13a4aSGreg Roach                                    $keep = true;
2184a6f13a4aSGreg Roach                                } else {
2185a6f13a4aSGreg Roach                                    $keep = false;
2186a6f13a4aSGreg Roach                                }
2187a6f13a4aSGreg Roach                                break;
2188a6f13a4aSGreg Roach                        }
2189a6f13a4aSGreg Roach                    }
2190a6f13a4aSGreg Roach                }
2191a6f13a4aSGreg Roach                if ($keep) {
2192a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2193a6f13a4aSGreg Roach                }
2194a6f13a4aSGreg Roach            }
2195a6f13a4aSGreg Roach            $this->list = $mylist;
2196a6f13a4aSGreg Roach        }
2197a6f13a4aSGreg Roach
2198a6f13a4aSGreg Roach        switch ($sortby) {
2199a6f13a4aSGreg Roach            case 'NAME':
2200a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2201a6f13a4aSGreg Roach                break;
2202a6f13a4aSGreg Roach            case 'CHAN':
220318d7a90dSGreg Roach                uasort($this->list, function (GedcomRecord $x, GedcomRecord $y): int {
2204a6f13a4aSGreg Roach                    return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2205a6f13a4aSGreg Roach                });
2206a6f13a4aSGreg Roach                break;
2207a6f13a4aSGreg Roach            case 'BIRT:DATE':
2208a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2209a6f13a4aSGreg Roach                break;
2210a6f13a4aSGreg Roach            case 'DEAT:DATE':
2211a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2212a6f13a4aSGreg Roach                break;
2213a6f13a4aSGreg Roach            case 'MARR:DATE':
22145d0bc43dSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2215a6f13a4aSGreg Roach                break;
2216a6f13a4aSGreg Roach            default:
2217a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2218a6f13a4aSGreg Roach                break;
2219a6f13a4aSGreg Roach        }
2220a6f13a4aSGreg Roach
22219b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2222e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2223a6f13a4aSGreg Roach    }
2224a6f13a4aSGreg Roach
2225a6f13a4aSGreg Roach    /**
222676692c8bSGreg Roach     * XML <List>
22278ba2e626SGreg Roach     *
22288ba2e626SGreg Roach     * @return void
2229a6f13a4aSGreg Roach     */
2230c1010edaSGreg Roach    private function listEndHandler()
2231c1010edaSGreg Roach    {
2232a6f13a4aSGreg Roach        $this->process_repeats--;
2233a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2234a6f13a4aSGreg Roach            return;
2235a6f13a4aSGreg Roach        }
2236a6f13a4aSGreg Roach
2237a6f13a4aSGreg Roach        // Check if there is any list
2238a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2239a6f13a4aSGreg Roach            $lineoffset = 0;
2240a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2241a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2242a6f13a4aSGreg Roach            }
2243a6f13a4aSGreg Roach            //-- read the xml from the file
2244299d100dSGreg Roach            $lines = file($this->report);
22457a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2246a6f13a4aSGreg Roach                $lineoffset--;
2247a6f13a4aSGreg Roach            }
2248a6f13a4aSGreg Roach            $lineoffset++;
2249a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2250a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2251a6f13a4aSGreg Roach            // List Level counter
2252a6f13a4aSGreg Roach            $count = 1;
2253a6f13a4aSGreg Roach            while (0 < $count) {
22547a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2255a6f13a4aSGreg Roach                    $count++;
22567a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2257a6f13a4aSGreg Roach                    $count--;
2258a6f13a4aSGreg Roach                }
2259a6f13a4aSGreg Roach                if (0 < $count) {
2260a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2261a6f13a4aSGreg Roach                }
2262a6f13a4aSGreg Roach                $line_nr++;
2263a6f13a4aSGreg Roach            }
2264a6f13a4aSGreg Roach            // No need to drag this
2265a6f13a4aSGreg Roach            unset($lines);
22667a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2267a6f13a4aSGreg Roach            // Save original values
22689b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2269a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2270a6f13a4aSGreg Roach
2271a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2272a6f13a4aSGreg Roach            $this->list_private = 0;
2273a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2274a6f13a4aSGreg Roach                if ($record->canShow()) {
2275a6f13a4aSGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->getTree()));
2276a6f13a4aSGreg Roach                    //-- start the sax parser
2277a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2278e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2279a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
2280*1aa04befSGreg Roach
2281*1aa04befSGreg Roach                    xml_set_element_handler(
2282*1aa04befSGreg Roach                        $repeat_parser,
2283*1aa04befSGreg Roach                        function ($parser, string $name, array $attrs) {
2284*1aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
2285*1aa04befSGreg Roach                        },
2286*1aa04befSGreg Roach                        function ($parser, string $name) {
2287*1aa04befSGreg Roach                            $this->endElement($parser, $name);
2288*1aa04befSGreg Roach                        }
2289*1aa04befSGreg Roach                    );
2290*1aa04befSGreg Roach
2291*1aa04befSGreg Roach                    xml_set_character_data_handler(
2292*1aa04befSGreg Roach                        $repeat_parser,
2293*1aa04befSGreg Roach                        function ($parser, $data) {
2294*1aa04befSGreg Roach                            $this->characterData($parser, $data);
2295*1aa04befSGreg Roach                        }
2296*1aa04befSGreg Roach                    );
2297*1aa04befSGreg Roach
2298a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
2299a6f13a4aSGreg Roach                        throw new \DomainException(sprintf(
2300a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2301a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2302a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2303a6f13a4aSGreg Roach                        ));
2304a6f13a4aSGreg Roach                    }
2305a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2306a6f13a4aSGreg Roach                } else {
2307a6f13a4aSGreg Roach                    $this->list_private++;
2308a6f13a4aSGreg Roach                }
2309a6f13a4aSGreg Roach            }
231013abd6f3SGreg Roach            $this->list   = [];
2311e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2312a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2313a6f13a4aSGreg Roach        }
2314a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2315a6f13a4aSGreg Roach    }
2316a6f13a4aSGreg Roach
2317a6f13a4aSGreg Roach    /**
2318a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2319a6f13a4aSGreg Roach     * Prints the total number of records in a list
2320a6f13a4aSGreg Roach     * The total number is collected from
2321a6f13a4aSGreg Roach     * List and Relatives
23228ba2e626SGreg Roach     *
23238ba2e626SGreg Roach     * @return void
2324a6f13a4aSGreg Roach     */
2325c1010edaSGreg Roach    private function listTotalStartHandler()
2326c1010edaSGreg Roach    {
2327a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2328589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2329a6f13a4aSGreg Roach        } else {
23307a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2331a6f13a4aSGreg Roach        }
2332a6f13a4aSGreg Roach    }
2333a6f13a4aSGreg Roach
2334a6f13a4aSGreg Roach    /**
233576692c8bSGreg Roach     * XML <Relatives>
233676692c8bSGreg Roach     *
2337a6f13a4aSGreg Roach     * @param array $attrs an array of key value pairs for the attributes
23388ba2e626SGreg Roach     *
23398ba2e626SGreg Roach     * @return void
2340a6f13a4aSGreg Roach     */
2341c1010edaSGreg Roach    private function relativesStartHandler($attrs)
2342c1010edaSGreg Roach    {
2343a6f13a4aSGreg Roach        $this->process_repeats++;
2344a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2345a6f13a4aSGreg Roach            return;
2346a6f13a4aSGreg Roach        }
2347a6f13a4aSGreg Roach
23487a6ee1acSGreg Roach        $sortby = 'NAME';
2349a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
2350a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
2351a6f13a4aSGreg Roach        }
235213abd6f3SGreg Roach        $match = [];
2353a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2354d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2355a6f13a4aSGreg Roach            $sortby = trim($sortby);
2356a6f13a4aSGreg Roach        }
2357a6f13a4aSGreg Roach
2358a6f13a4aSGreg Roach        $maxgen = -1;
2359a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2360a6f13a4aSGreg Roach            $maxgen = $attrs['maxgen'];
2361a6f13a4aSGreg Roach        }
23627a6ee1acSGreg Roach        if ($maxgen == '*') {
2363a6f13a4aSGreg Roach            $maxgen = -1;
2364a6f13a4aSGreg Roach        }
2365a6f13a4aSGreg Roach
23667a6ee1acSGreg Roach        $group = 'child-family';
2367a6f13a4aSGreg Roach        if (isset($attrs['group'])) {
2368a6f13a4aSGreg Roach            $group = $attrs['group'];
2369a6f13a4aSGreg Roach        }
2370a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2371d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2372a6f13a4aSGreg Roach            $group = trim($group);
2373a6f13a4aSGreg Roach        }
2374a6f13a4aSGreg Roach
23757a6ee1acSGreg Roach        $id = '';
2376a6f13a4aSGreg Roach        if (isset($attrs['id'])) {
2377a6f13a4aSGreg Roach            $id = $attrs['id'];
2378a6f13a4aSGreg Roach        }
2379a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2380d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2381a6f13a4aSGreg Roach            $id = trim($id);
2382a6f13a4aSGreg Roach        }
2383a6f13a4aSGreg Roach
238413abd6f3SGreg Roach        $this->list = [];
2385299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2386a6f13a4aSGreg Roach        if (!empty($person)) {
2387a6f13a4aSGreg Roach            $this->list[$id] = $person;
2388a6f13a4aSGreg Roach            switch ($group) {
23897a6ee1acSGreg Roach                case 'child-family':
2390a6f13a4aSGreg Roach                    foreach ($person->getChildFamilies() as $family) {
2391a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2392a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2393a6f13a4aSGreg Roach                        if (!empty($husband)) {
2394a6f13a4aSGreg Roach                            $this->list[$husband->getXref()] = $husband;
2395a6f13a4aSGreg Roach                        }
2396a6f13a4aSGreg Roach                        if (!empty($wife)) {
2397a6f13a4aSGreg Roach                            $this->list[$wife->getXref()] = $wife;
2398a6f13a4aSGreg Roach                        }
2399a6f13a4aSGreg Roach                        $children = $family->getChildren();
2400a6f13a4aSGreg Roach                        foreach ($children as $child) {
2401a6f13a4aSGreg Roach                            if (!empty($child)) {
2402a6f13a4aSGreg Roach                                $this->list[$child->getXref()] = $child;
2403a6f13a4aSGreg Roach                            }
2404a6f13a4aSGreg Roach                        }
2405a6f13a4aSGreg Roach                    }
2406a6f13a4aSGreg Roach                    break;
24077a6ee1acSGreg Roach                case 'spouse-family':
2408a6f13a4aSGreg Roach                    foreach ($person->getSpouseFamilies() as $family) {
2409a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2410a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2411a6f13a4aSGreg Roach                        if (!empty($husband)) {
2412a6f13a4aSGreg Roach                            $this->list[$husband->getXref()] = $husband;
2413a6f13a4aSGreg Roach                        }
2414a6f13a4aSGreg Roach                        if (!empty($wife)) {
2415a6f13a4aSGreg Roach                            $this->list[$wife->getXref()] = $wife;
2416a6f13a4aSGreg Roach                        }
2417a6f13a4aSGreg Roach                        $children = $family->getChildren();
2418a6f13a4aSGreg Roach                        foreach ($children as $child) {
2419a6f13a4aSGreg Roach                            if (!empty($child)) {
2420a6f13a4aSGreg Roach                                $this->list[$child->getXref()] = $child;
2421a6f13a4aSGreg Roach                            }
2422a6f13a4aSGreg Roach                        }
2423a6f13a4aSGreg Roach                    }
2424a6f13a4aSGreg Roach                    break;
24257a6ee1acSGreg Roach                case 'direct-ancestors':
24263d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2427a6f13a4aSGreg Roach                    break;
24287a6ee1acSGreg Roach                case 'ancestors':
24293d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2430a6f13a4aSGreg Roach                    break;
24317a6ee1acSGreg Roach                case 'descendants':
2432a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24333d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2434a6f13a4aSGreg Roach                    break;
24357a6ee1acSGreg Roach                case 'all':
24363d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24373d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2438a6f13a4aSGreg Roach                    break;
2439a6f13a4aSGreg Roach            }
2440a6f13a4aSGreg Roach        }
2441a6f13a4aSGreg Roach
2442a6f13a4aSGreg Roach        switch ($sortby) {
2443a6f13a4aSGreg Roach            case 'NAME':
2444a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2445a6f13a4aSGreg Roach                break;
2446a6f13a4aSGreg Roach            case 'BIRT:DATE':
2447a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2448a6f13a4aSGreg Roach                break;
2449a6f13a4aSGreg Roach            case 'DEAT:DATE':
2450a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2451a6f13a4aSGreg Roach                break;
2452a6f13a4aSGreg Roach            case 'generation':
245313abd6f3SGreg Roach                $newarray = [];
2454a6f13a4aSGreg Roach                reset($this->list);
2455a6f13a4aSGreg Roach                $genCounter = 1;
2456a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2457a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2458a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2459a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
246079529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2461a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2462a6f13a4aSGreg Roach                        }
2463a6f13a4aSGreg Roach                    }
2464a6f13a4aSGreg Roach                    $genCounter++;
2465a6f13a4aSGreg Roach                }
2466a6f13a4aSGreg Roach                $this->list = $newarray;
2467a6f13a4aSGreg Roach                break;
2468a6f13a4aSGreg Roach            default:
2469a6f13a4aSGreg Roach                // unsorted
2470a6f13a4aSGreg Roach                break;
2471a6f13a4aSGreg Roach        }
24729b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2473e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2474a6f13a4aSGreg Roach    }
2475a6f13a4aSGreg Roach
2476a6f13a4aSGreg Roach    /**
247776692c8bSGreg Roach     * XML </ Relatives>
24788ba2e626SGreg Roach     *
24798ba2e626SGreg Roach     * @return void
2480a6f13a4aSGreg Roach     */
2481c1010edaSGreg Roach    private function relativesEndHandler()
2482c1010edaSGreg Roach    {
2483a6f13a4aSGreg Roach        $this->process_repeats--;
2484a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2485a6f13a4aSGreg Roach            return;
2486a6f13a4aSGreg Roach        }
2487a6f13a4aSGreg Roach
2488a6f13a4aSGreg Roach        // Check if there is any relatives
2489a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2490a6f13a4aSGreg Roach            $lineoffset = 0;
2491a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2492a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2493a6f13a4aSGreg Roach            }
2494a6f13a4aSGreg Roach            //-- read the xml from the file
2495299d100dSGreg Roach            $lines = file($this->report);
24967a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2497a6f13a4aSGreg Roach                $lineoffset--;
2498a6f13a4aSGreg Roach            }
2499a6f13a4aSGreg Roach            $lineoffset++;
2500a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2501a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2502a6f13a4aSGreg Roach            // Relatives Level counter
2503a6f13a4aSGreg Roach            $count = 1;
2504a6f13a4aSGreg Roach            while (0 < $count) {
25057a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2506a6f13a4aSGreg Roach                    $count++;
25077a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2508a6f13a4aSGreg Roach                    $count--;
2509a6f13a4aSGreg Roach                }
2510a6f13a4aSGreg Roach                if (0 < $count) {
2511a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2512a6f13a4aSGreg Roach                }
2513a6f13a4aSGreg Roach                $line_nr++;
2514a6f13a4aSGreg Roach            }
2515a6f13a4aSGreg Roach            // No need to drag this
2516a6f13a4aSGreg Roach            unset($lines);
2517a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2518a6f13a4aSGreg Roach            // Save original values
25199b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2520a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2521a6f13a4aSGreg Roach
2522a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2523a6f13a4aSGreg Roach            $this->list_private = 0;
2524a6f13a4aSGreg Roach            foreach ($this->list as $key => $value) {
2525a6f13a4aSGreg Roach                if (isset($value->generation)) {
2526a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2527a6f13a4aSGreg Roach                }
2528299d100dSGreg Roach                $tmp          = GedcomRecord::getInstance($key, $this->tree);
2529299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2530a6f13a4aSGreg Roach
2531a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2532e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2533a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
2534*1aa04befSGreg Roach
2535*1aa04befSGreg Roach                xml_set_element_handler(
2536*1aa04befSGreg Roach                    $repeat_parser,
2537*1aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
2538*1aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
2539*1aa04befSGreg Roach                    },
2540*1aa04befSGreg Roach                    function ($parser, string $name) {
2541*1aa04befSGreg Roach                        $this->endElement($parser, $name);
2542*1aa04befSGreg Roach                    }
2543*1aa04befSGreg Roach                );
2544*1aa04befSGreg Roach
2545*1aa04befSGreg Roach                xml_set_character_data_handler(
2546*1aa04befSGreg Roach                    $repeat_parser,
2547*1aa04befSGreg Roach                    function ($parser, $data) {
2548*1aa04befSGreg Roach                        $this->characterData($parser, $data);
2549*1aa04befSGreg Roach                    }
2550*1aa04befSGreg Roach                );
2551a6f13a4aSGreg Roach
2552a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25537a6ee1acSGreg 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)));
2554a6f13a4aSGreg Roach                }
2555a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2556a6f13a4aSGreg Roach            }
2557a6f13a4aSGreg Roach            // Clean up the list array
255813abd6f3SGreg Roach            $this->list   = [];
2559e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2560a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2561a6f13a4aSGreg Roach        }
2562a6f13a4aSGreg Roach        list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack);
2563a6f13a4aSGreg Roach    }
2564a6f13a4aSGreg Roach
2565a6f13a4aSGreg Roach    /**
2566a6f13a4aSGreg Roach     * XML <Generation /> element handler
2567a6f13a4aSGreg Roach     * Prints the number of generations
25688ba2e626SGreg Roach     *
25698ba2e626SGreg Roach     * @return void
2570a6f13a4aSGreg Roach     */
2571c1010edaSGreg Roach    private function generationStartHandler()
2572c1010edaSGreg Roach    {
2573589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2574a6f13a4aSGreg Roach    }
2575a6f13a4aSGreg Roach
2576a6f13a4aSGreg Roach    /**
2577a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2578a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25798ba2e626SGreg Roach     *
25808ba2e626SGreg Roach     * @return void
2581a6f13a4aSGreg Roach     */
2582c1010edaSGreg Roach    private function newPageStartHandler()
2583c1010edaSGreg Roach    {
25847a6ee1acSGreg Roach        $temp = 'addpage';
2585e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2586a6f13a4aSGreg Roach    }
2587a6f13a4aSGreg Roach
2588a6f13a4aSGreg Roach    /**
258976692c8bSGreg Roach     * XML <html>
259076692c8bSGreg Roach     *
2591a6f13a4aSGreg Roach     * @param string  $tag   HTML tag name
259276692c8bSGreg Roach     * @param array[] $attrs an array of key value pairs for the attributes
25938ba2e626SGreg Roach     *
25948ba2e626SGreg Roach     * @return void
2595a6f13a4aSGreg Roach     */
2596c1010edaSGreg Roach    private function htmlStartHandler($tag, $attrs)
2597c1010edaSGreg Roach    {
25987a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2599a6f13a4aSGreg Roach            return;
2600a6f13a4aSGreg Roach        }
26019b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2602e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2603e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2604a6f13a4aSGreg Roach
26059b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2606a6f13a4aSGreg Roach        $this->print_data         = true;
2607a6f13a4aSGreg Roach    }
2608a6f13a4aSGreg Roach
2609a6f13a4aSGreg Roach    /**
261076692c8bSGreg Roach     * XML </html>
261176692c8bSGreg Roach     *
2612a6f13a4aSGreg Roach     * @param string $tag
26138ba2e626SGreg Roach     *
26148ba2e626SGreg Roach     * @return void
2615a6f13a4aSGreg Roach     */
2616c1010edaSGreg Roach    private function htmlEndHandler($tag)
2617c1010edaSGreg Roach    {
26187a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2619a6f13a4aSGreg Roach            return;
2620a6f13a4aSGreg Roach        }
2621a6f13a4aSGreg Roach
2622a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2623e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2624e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
26258f038c36SRico Sonntag        if ($this->wt_report !== null) {
2626e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2627a6f13a4aSGreg Roach        } else {
2628e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2629a6f13a4aSGreg Roach        }
2630a6f13a4aSGreg Roach    }
2631a6f13a4aSGreg Roach
2632a6f13a4aSGreg Roach    /**
2633a6f13a4aSGreg Roach     * Handle <Input>
26348ba2e626SGreg Roach     *
26358ba2e626SGreg Roach     * @return void
2636a6f13a4aSGreg Roach     */
2637c1010edaSGreg Roach    private function inputStartHandler()
2638c1010edaSGreg Roach    {
2639a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2640a6f13a4aSGreg Roach    }
2641a6f13a4aSGreg Roach
2642a6f13a4aSGreg Roach    /**
2643a6f13a4aSGreg Roach     * Handle </Input>
26448ba2e626SGreg Roach     *
26458ba2e626SGreg Roach     * @return void
2646a6f13a4aSGreg Roach     */
2647c1010edaSGreg Roach    private function inputEndHandler()
2648c1010edaSGreg Roach    {
2649a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2650a6f13a4aSGreg Roach    }
2651a6f13a4aSGreg Roach
2652a6f13a4aSGreg Roach    /**
2653a6f13a4aSGreg Roach     * Handle <Report>
26548ba2e626SGreg Roach     *
26558ba2e626SGreg Roach     * @return void
2656a6f13a4aSGreg Roach     */
2657c1010edaSGreg Roach    private function reportStartHandler()
2658c1010edaSGreg Roach    {
2659a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2660a6f13a4aSGreg Roach    }
2661a6f13a4aSGreg Roach
2662a6f13a4aSGreg Roach    /**
2663a6f13a4aSGreg Roach     * Handle </Report>
26648ba2e626SGreg Roach     *
26658ba2e626SGreg Roach     * @return void
2666a6f13a4aSGreg Roach     */
2667c1010edaSGreg Roach    private function reportEndHandler()
2668c1010edaSGreg Roach    {
2669a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2670a6f13a4aSGreg Roach    }
2671a6f13a4aSGreg Roach
2672a6f13a4aSGreg Roach    /**
267376692c8bSGreg Roach     * XML </titleEndHandler>
26748ba2e626SGreg Roach     *
26758ba2e626SGreg Roach     * @return void
2676a6f13a4aSGreg Roach     */
2677c1010edaSGreg Roach    private function titleEndHandler()
2678c1010edaSGreg Roach    {
26792836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2680a6f13a4aSGreg Roach    }
2681a6f13a4aSGreg Roach
2682a6f13a4aSGreg Roach    /**
268376692c8bSGreg Roach     * XML </descriptionEndHandler>
26848ba2e626SGreg Roach     *
26858ba2e626SGreg Roach     * @return void
2686a6f13a4aSGreg Roach     */
2687c1010edaSGreg Roach    private function descriptionEndHandler()
2688c1010edaSGreg Roach    {
26892836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2690a6f13a4aSGreg Roach    }
2691729ce104SGreg Roach
2692729ce104SGreg Roach    /**
269376692c8bSGreg Roach     * Create a list of all descendants.
269476692c8bSGreg Roach     *
2695729ce104SGreg Roach     * @param string[] $list
2696729ce104SGreg Roach     * @param string   $pid
2697729ce104SGreg Roach     * @param bool     $parents
2698729ce104SGreg Roach     * @param int      $generations
26998ba2e626SGreg Roach     *
27008ba2e626SGreg Roach     * @return void
2701729ce104SGreg Roach     */
2702c1010edaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1)
2703c1010edaSGreg Roach    {
2704299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2705729ce104SGreg Roach        if ($person === null) {
2706729ce104SGreg Roach            return;
2707729ce104SGreg Roach        }
2708729ce104SGreg Roach        if (!isset($list[$pid])) {
2709729ce104SGreg Roach            $list[$pid] = $person;
2710729ce104SGreg Roach        }
2711729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2712729ce104SGreg Roach            $list[$pid]->generation = 0;
2713729ce104SGreg Roach        }
2714729ce104SGreg Roach        foreach ($person->getSpouseFamilies() as $family) {
2715729ce104SGreg Roach            if ($parents) {
2716729ce104SGreg Roach                $husband = $family->getHusband();
2717729ce104SGreg Roach                $wife    = $family->getWife();
2718729ce104SGreg Roach                if ($husband) {
2719729ce104SGreg Roach                    $list[$husband->getXref()] = $husband;
2720729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2721729ce104SGreg Roach                        $list[$husband->getXref()]->generation = $list[$pid]->generation - 1;
2722729ce104SGreg Roach                    } else {
2723729ce104SGreg Roach                        $list[$husband->getXref()]->generation = 1;
2724729ce104SGreg Roach                    }
2725729ce104SGreg Roach                }
2726729ce104SGreg Roach                if ($wife) {
2727729ce104SGreg Roach                    $list[$wife->getXref()] = $wife;
2728729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2729729ce104SGreg Roach                        $list[$wife->getXref()]->generation = $list[$pid]->generation - 1;
2730729ce104SGreg Roach                    } else {
2731729ce104SGreg Roach                        $list[$wife->getXref()]->generation = 1;
2732729ce104SGreg Roach                    }
2733729ce104SGreg Roach                }
2734729ce104SGreg Roach            }
2735729ce104SGreg Roach            $children = $family->getChildren();
2736729ce104SGreg Roach            foreach ($children as $child) {
2737729ce104SGreg Roach                if ($child) {
2738729ce104SGreg Roach                    $list[$child->getXref()] = $child;
2739729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2740729ce104SGreg Roach                        $list[$child->getXref()]->generation = $list[$pid]->generation + 1;
2741729ce104SGreg Roach                    } else {
2742729ce104SGreg Roach                        $list[$child->getXref()]->generation = 2;
2743729ce104SGreg Roach                    }
2744729ce104SGreg Roach                }
2745729ce104SGreg Roach            }
2746729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2747729ce104SGreg Roach                foreach ($children as $child) {
27483d7a8a4cSGreg Roach                    $this->addDescendancy($list, $child->getXref(), $parents, $generations); // recurse on the childs family
2749729ce104SGreg Roach                }
2750729ce104SGreg Roach            }
2751729ce104SGreg Roach        }
2752729ce104SGreg Roach    }
2753729ce104SGreg Roach
2754729ce104SGreg Roach    /**
275576692c8bSGreg Roach     * Create a list of all ancestors.
275676692c8bSGreg Roach     *
2757729ce104SGreg Roach     * @param string[] $list
2758729ce104SGreg Roach     * @param string   $pid
2759729ce104SGreg Roach     * @param bool     $children
2760729ce104SGreg Roach     * @param int      $generations
27618ba2e626SGreg Roach     *
27628ba2e626SGreg Roach     * @return void
2763729ce104SGreg Roach     */
2764c1010edaSGreg Roach    private function addAncestors(&$list, $pid, $children = false, $generations = -1)
2765c1010edaSGreg Roach    {
276613abd6f3SGreg Roach        $genlist                = [$pid];
2767729ce104SGreg Roach        $list[$pid]->generation = 1;
2768729ce104SGreg Roach        while (count($genlist) > 0) {
2769729ce104SGreg Roach            $id = array_shift($genlist);
2770729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2771729ce104SGreg Roach                continue; // id can be something like “empty7”
2772729ce104SGreg Roach            }
2773299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
2774729ce104SGreg Roach            foreach ($person->getChildFamilies() as $family) {
2775729ce104SGreg Roach                $husband = $family->getHusband();
2776729ce104SGreg Roach                $wife    = $family->getWife();
2777729ce104SGreg Roach                if ($husband) {
2778729ce104SGreg Roach                    $list[$husband->getXref()]             = $husband;
2779729ce104SGreg Roach                    $list[$husband->getXref()]->generation = $list[$id]->generation + 1;
2780729ce104SGreg Roach                }
2781729ce104SGreg Roach                if ($wife) {
2782729ce104SGreg Roach                    $list[$wife->getXref()]             = $wife;
2783729ce104SGreg Roach                    $list[$wife->getXref()]->generation = $list[$id]->generation + 1;
2784729ce104SGreg Roach                }
2785729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2786729ce104SGreg Roach                    if ($husband) {
27879b3dd960SGreg Roach                        $genlist[] = $husband->getXref();
2788729ce104SGreg Roach                    }
2789729ce104SGreg Roach                    if ($wife) {
27909b3dd960SGreg Roach                        $genlist[] = $wife->getXref();
2791729ce104SGreg Roach                    }
2792729ce104SGreg Roach                }
2793729ce104SGreg Roach                if ($children) {
2794729ce104SGreg Roach                    foreach ($family->getChildren() as $child) {
2795729ce104SGreg Roach                        $list[$child->getXref()] = $child;
2796729ce104SGreg Roach                        if (isset($list[$id]->generation)) {
2797729ce104SGreg Roach                            $list[$child->getXref()]->generation = $list[$id]->generation;
2798729ce104SGreg Roach                        } else {
2799729ce104SGreg Roach                            $list[$child->getXref()]->generation = 1;
2800729ce104SGreg Roach                        }
2801729ce104SGreg Roach                    }
2802729ce104SGreg Roach                }
2803729ce104SGreg Roach            }
2804729ce104SGreg Roach        }
2805729ce104SGreg Roach    }
2806729ce104SGreg Roach
2807729ce104SGreg Roach    /**
2808729ce104SGreg Roach     * get gedcom tag value
2809729ce104SGreg Roach     *
2810729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2811729ce104SGreg 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
2812729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2813729ce104SGreg Roach     *
2814729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2815729ce104SGreg Roach     */
28168f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2817c1010edaSGreg Roach    {
2818729ce104SGreg Roach        if (empty($gedrec)) {
2819729ce104SGreg Roach            return '';
2820729ce104SGreg Roach        }
2821729ce104SGreg Roach        $tags      = explode(':', $tag);
2822729ce104SGreg Roach        $origlevel = $level;
2823729ce104SGreg Roach        if ($level == 0) {
28243c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2825729ce104SGreg Roach        }
2826729ce104SGreg Roach
2827729ce104SGreg Roach        $subrec = $gedrec;
2828729ce104SGreg Roach        foreach ($tags as $t) {
2829729ce104SGreg Roach            $lastsubrec = $subrec;
28303d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2831729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2832729ce104SGreg Roach                $level--;
28333d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2834729ce104SGreg Roach            }
2835729ce104SGreg Roach            if (empty($subrec)) {
28367a6ee1acSGreg Roach                if ($t == 'TITL') {
28373d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2838729ce104SGreg Roach                    if (!empty($subrec)) {
28397a6ee1acSGreg Roach                        $t = 'ABBR';
2840729ce104SGreg Roach                    }
2841729ce104SGreg Roach                }
2842729ce104SGreg Roach                if (empty($subrec)) {
2843729ce104SGreg Roach                    if ($level > 0) {
2844729ce104SGreg Roach                        $level--;
2845729ce104SGreg Roach                    }
28463d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2847729ce104SGreg Roach                    if (empty($subrec)) {
2848729ce104SGreg Roach                        return '';
2849729ce104SGreg Roach                    }
2850729ce104SGreg Roach                }
2851729ce104SGreg Roach            }
2852729ce104SGreg Roach            $level++;
2853729ce104SGreg Roach        }
2854729ce104SGreg Roach        $level--;
2855729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2856729ce104SGreg Roach        if ($ct == 0) {
2857729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2858729ce104SGreg Roach        }
2859729ce104SGreg Roach        if ($ct == 0) {
2860729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2861729ce104SGreg Roach        }
2862729ce104SGreg Roach        if ($ct > 0) {
2863729ce104SGreg Roach            $value = trim($match[1]);
2864729ce104SGreg Roach            if ($t == 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2865299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2866729ce104SGreg Roach                if ($note) {
2867729ce104SGreg Roach                    $value = $note->getNote();
2868729ce104SGreg Roach                } else {
2869729ce104SGreg Roach                    //-- set the value to the id without the @
2870729ce104SGreg Roach                    $value = $match[1];
2871729ce104SGreg Roach                }
2872729ce104SGreg Roach            }
28737a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28743d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2875729ce104SGreg Roach            }
2876729ce104SGreg Roach
2877729ce104SGreg Roach            return $value;
2878729ce104SGreg Roach        }
2879729ce104SGreg Roach
28807a6ee1acSGreg Roach        return '';
2881729ce104SGreg Roach    }
2882d1286247SGreg Roach
2883d1286247SGreg Roach    /**
2884d1286247SGreg Roach     * Replace variable identifiers with their values.
2885d1286247SGreg Roach     *
2886d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
288782759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2888d1286247SGreg Roach     *
2889d1286247SGreg Roach     * @return string
2890d1286247SGreg Roach     */
28918f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2892c1010edaSGreg Roach    {
2893d1286247SGreg Roach        return preg_replace_callback(
2894d1286247SGreg Roach            '/\$(\w+)/',
289518d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28962118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
289782759250SGreg Roach                    if ($quote) {
28982118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2899b2ce94c6SRico Sonntag                    }
2900b2ce94c6SRico Sonntag
29012118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
290282759250SGreg Roach                }
2903b2ce94c6SRico Sonntag
2904d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
29053d7a8a4cSGreg Roach
2906d1286247SGreg Roach                return '$' . $matches[1];
2907d1286247SGreg Roach            },
2908d1286247SGreg Roach            $expression
2909d1286247SGreg Roach        );
2910d1286247SGreg Roach    }
2911a6f13a4aSGreg Roach}
2912