xref: /webtrees/app/Report/ReportParserGenerate.php (revision 7f5c2944f84cdc914a20ed1e961f9b81e401f78b)
1a6f13a4aSGreg Roach<?php
2a6f13a4aSGreg Roach/**
3a6f13a4aSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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
206ccdf4f0SGreg Roachuse DomainException;
21a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
23a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
24a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
25a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
263d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
279a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
28a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
32d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
33a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Media;
34729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
35a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
36299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
37195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
38195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
39a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
405985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
419a9e551aSGreg Roachuse Illuminate\Support\Str;
4279529c87SGreg Roachuse stdClass;
43c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
445809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
45a6f13a4aSGreg Roach
46a6f13a4aSGreg Roach/**
47a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
48a6f13a4aSGreg Roach */
49c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
50c1010edaSGreg Roach{
51a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
52a6f13a4aSGreg Roach    private $process_footnote = true;
53a6f13a4aSGreg Roach
54a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
55a6f13a4aSGreg Roach    private $print_data = false;
56a6f13a4aSGreg Roach
57a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5813abd6f3SGreg Roach    private $print_data_stack = [];
59a6f13a4aSGreg Roach
6076692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
61a6f13a4aSGreg Roach    private $process_gedcoms = 0;
62a6f13a4aSGreg Roach
6376692c8bSGreg Roach    /** @var int Are we processing conditionals */
64a6f13a4aSGreg Roach    private $process_ifs = 0;
65a6f13a4aSGreg Roach
6676692c8bSGreg Roach    /** @var int Are we processing repeats */
67a6f13a4aSGreg Roach    private $process_repeats = 0;
68a6f13a4aSGreg Roach
69a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
70a6f13a4aSGreg Roach    private $repeat_bytes = 0;
71a6f13a4aSGreg Roach
725b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
7313abd6f3SGreg Roach    private $repeats = [];
74a6f13a4aSGreg Roach
75a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
7613abd6f3SGreg Roach    private $repeats_stack = [];
77a6f13a4aSGreg Roach
78208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
7913abd6f3SGreg Roach    private $wt_report_stack = [];
80e8e7866bSGreg Roach
81e8e7866bSGreg Roach    /** @var resource Nested repeating data */
82e8e7866bSGreg Roach    private $parser;
83e8e7866bSGreg Roach
84e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
8513abd6f3SGreg Roach    private $parser_stack = [];
86e8e7866bSGreg Roach
87a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
88a6f13a4aSGreg Roach    private $gedrec = '';
89a6f13a4aSGreg Roach
90a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
9113abd6f3SGreg Roach    private $gedrec_stack = [];
92a6f13a4aSGreg Roach
93a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
94a6f13a4aSGreg Roach    private $current_element;
95a6f13a4aSGreg Roach
96a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
97a6f13a4aSGreg Roach    private $footnote_element;
98a6f13a4aSGreg Roach
99a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
100a6f13a4aSGreg Roach    private $fact = '';
101a6f13a4aSGreg Roach
102a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
103a6f13a4aSGreg Roach    private $desc = '';
104a6f13a4aSGreg Roach
105a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
106a6f13a4aSGreg Roach    private $type = '';
107a6f13a4aSGreg Roach
108a6f13a4aSGreg Roach    /** @var int The current generational level */
109a6f13a4aSGreg Roach    private $generation = 1;
110a6f13a4aSGreg Roach
111a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
11213abd6f3SGreg Roach    private $list = [];
113a6f13a4aSGreg Roach
114a6f13a4aSGreg Roach    /** @var int Number of items in lists */
115a6f13a4aSGreg Roach    private $list_total = 0;
116a6f13a4aSGreg Roach
117a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
118a6f13a4aSGreg Roach    private $list_private = 0;
119a6f13a4aSGreg Roach
120299d100dSGreg Roach    /** @var string The filename of the XML report */
121299d100dSGreg Roach    protected $report;
122299d100dSGreg Roach
123208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
124e8e7866bSGreg Roach    private $report_root;
125e8e7866bSGreg Roach
126208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
127e8e7866bSGreg Roach    private $wt_report;
128e8e7866bSGreg Roach
129d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1302118c0e3SGreg Roach    private $vars;
131d1286247SGreg Roach
132299d100dSGreg Roach    /** @var Tree The current tree */
133299d100dSGreg Roach    private $tree;
134299d100dSGreg Roach
13576692c8bSGreg Roach    /**
13676692c8bSGreg Roach     * Create a parser for a report
13776692c8bSGreg Roach     *
13876692c8bSGreg Roach     * @param string         $report The XML filename
139208e9f76SGreg Roach     * @param AbstractReport $report_root
14076692c8bSGreg Roach     * @param string[][]     $vars
141299d100dSGreg Roach     * @param Tree           $tree
14276692c8bSGreg Roach     */
143208e9f76SGreg Roach    public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree)
144c1010edaSGreg Roach    {
145299d100dSGreg Roach        $this->report          = $report;
146e8e7866bSGreg Roach        $this->report_root     = $report_root;
147e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14859f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
149d1286247SGreg Roach        $this->vars            = $vars;
150299d100dSGreg Roach        $this->tree            = $tree;
151299d100dSGreg Roach
15276f666f4SGreg Roach        parent::__construct($report);
153a6f13a4aSGreg Roach    }
154a6f13a4aSGreg Roach
155a6f13a4aSGreg Roach    /**
156a6f13a4aSGreg Roach     * XML start element handler
157a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
158a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
159a6f13a4aSGreg Roach     *
160a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
161a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1628a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
16318d7a90dSGreg Roach     *
16418d7a90dSGreg Roach     * @return void
165a6f13a4aSGreg Roach     */
166af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
167c1010edaSGreg Roach    {
16813abd6f3SGreg Roach        $newattrs = [];
169a6f13a4aSGreg Roach
170a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
171a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
172e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
173d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
174a6f13a4aSGreg Roach                }
175a6f13a4aSGreg Roach            }
176a6f13a4aSGreg Roach            $newattrs[$key] = $value;
177a6f13a4aSGreg Roach        }
178a6f13a4aSGreg Roach        $attrs = $newattrs;
1797a6ee1acSGreg 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')) {
180a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
181a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
182208e9f76SGreg Roach
183a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
184a6f13a4aSGreg Roach                $this->$start_method($attrs);
185a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
186a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
187a6f13a4aSGreg Roach            }
188a6f13a4aSGreg Roach        }
189a6f13a4aSGreg Roach    }
190a6f13a4aSGreg Roach
191a6f13a4aSGreg Roach    /**
192a6f13a4aSGreg Roach     * XML end element handler
193a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
194a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
195a6f13a4aSGreg Roach     *
196a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
197a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
19818d7a90dSGreg Roach     *
19918d7a90dSGreg Roach     * @return void
200a6f13a4aSGreg Roach     */
201af14d238SGreg Roach    protected function endElement($parser, string $name): void
202c1010edaSGreg Roach    {
2037a6ee1acSGreg 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')) {
204a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
205a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
206a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
207a6f13a4aSGreg Roach                $this->$end_method();
208a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
209a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
210a6f13a4aSGreg Roach            }
211a6f13a4aSGreg Roach        }
212a6f13a4aSGreg Roach    }
213a6f13a4aSGreg Roach
214a6f13a4aSGreg Roach    /**
215a6f13a4aSGreg Roach     * XML character data handler
216a6f13a4aSGreg Roach     *
217a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
218a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
21918d7a90dSGreg Roach     *
22018d7a90dSGreg Roach     * @return void
221a6f13a4aSGreg Roach     */
222af14d238SGreg Roach    protected function characterData($parser, $data): void
223c1010edaSGreg Roach    {
224e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
225a6f13a4aSGreg Roach            $this->current_element->addText($data);
226a6f13a4aSGreg Roach        }
227a6f13a4aSGreg Roach    }
228a6f13a4aSGreg Roach
229a6f13a4aSGreg Roach    /**
23076692c8bSGreg Roach     * XML <style>
231a6f13a4aSGreg Roach     *
232c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2338ba2e626SGreg Roach     *
2348ba2e626SGreg Roach     * @return void
235a6f13a4aSGreg Roach     */
236b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
237c1010edaSGreg Roach    {
238a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2396ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
240a6f13a4aSGreg Roach        }
241a6f13a4aSGreg Roach
242a6f13a4aSGreg Roach        // array Style that will be passed on
24313abd6f3SGreg Roach        $s = [];
244a6f13a4aSGreg Roach
245a6f13a4aSGreg Roach        // string Name af the style
246a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
247a6f13a4aSGreg Roach
248a6f13a4aSGreg Roach        // string Name of the DEFAULT font
249208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
250a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
251a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
252a6f13a4aSGreg Roach        }
253a6f13a4aSGreg Roach
254a6f13a4aSGreg Roach        // int The size of the font in points
255208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
256a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
257a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
258a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
259a6f13a4aSGreg Roach
260a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2617a6ee1acSGreg Roach        $s['style'] = '';
262a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
263a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
264a6f13a4aSGreg Roach        }
265a6f13a4aSGreg Roach
266e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
267a6f13a4aSGreg Roach    }
268a6f13a4aSGreg Roach
269a6f13a4aSGreg Roach    /**
27076692c8bSGreg Roach     * XML <Doc>
271a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
272a6f13a4aSGreg Roach     *
273c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
2748ba2e626SGreg Roach     *
2758ba2e626SGreg Roach     * @return void
276a6f13a4aSGreg Roach     */
277b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
278c1010edaSGreg Roach    {
279e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
280a6f13a4aSGreg Roach
281a6f13a4aSGreg Roach        // Custom page width
282a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
283208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
284a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
285a6f13a4aSGreg Roach        // Custom Page height
286a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
287208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
288a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
289a6f13a4aSGreg Roach
290a6f13a4aSGreg Roach        // Left Margin
291a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2927a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
293208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
294a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
295208e9f76SGreg 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))
296a6f13a4aSGreg Roach            }
297a6f13a4aSGreg Roach        }
298a6f13a4aSGreg Roach        // Right Margin
299a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3007a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
301208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
302a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
303208e9f76SGreg 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))
304a6f13a4aSGreg Roach            }
305a6f13a4aSGreg Roach        }
306a6f13a4aSGreg Roach        // Top Margin
307a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3087a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
309208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
310a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
311208e9f76SGreg 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))
312a6f13a4aSGreg Roach            }
313a6f13a4aSGreg Roach        }
314a6f13a4aSGreg Roach        // Bottom Margin
315a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3167a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
317208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
318a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
319208e9f76SGreg 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))
320a6f13a4aSGreg Roach            }
321a6f13a4aSGreg Roach        }
322a6f13a4aSGreg Roach        // Header Margin
323a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3247a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
325208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
326a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
327208e9f76SGreg 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))
328a6f13a4aSGreg Roach            }
329a6f13a4aSGreg Roach        }
330a6f13a4aSGreg Roach        // Footer Margin
331a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3327a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
333208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
334a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
335208e9f76SGreg 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))
336a6f13a4aSGreg Roach            }
337a6f13a4aSGreg Roach        }
338a6f13a4aSGreg Roach
339a6f13a4aSGreg Roach        // Page Orientation
340a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
341044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3427a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
343044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3447a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
345a6f13a4aSGreg Roach            }
346a6f13a4aSGreg Roach        }
347a6f13a4aSGreg Roach        // Page Size
348a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
349208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
350a6f13a4aSGreg Roach        }
351a6f13a4aSGreg Roach
352a6f13a4aSGreg Roach        // Show Generated By...
353a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3547a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
355208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3567a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
357208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
358a6f13a4aSGreg Roach            }
359a6f13a4aSGreg Roach        }
360a6f13a4aSGreg Roach
361e8e7866bSGreg Roach        $this->wt_report->setup();
362a6f13a4aSGreg Roach    }
363a6f13a4aSGreg Roach
364a6f13a4aSGreg Roach    /**
36576692c8bSGreg Roach     * XML </Doc>
3668ba2e626SGreg Roach     *
3678ba2e626SGreg Roach     * @return void
368a6f13a4aSGreg Roach     */
369b702978eSGreg Roach    protected function docEndHandler(): void
370c1010edaSGreg Roach    {
371e8e7866bSGreg Roach        $this->wt_report->run();
372a6f13a4aSGreg Roach    }
373a6f13a4aSGreg Roach
374a6f13a4aSGreg Roach    /**
37576692c8bSGreg Roach     * XML <Header>
3768ba2e626SGreg Roach     *
3778ba2e626SGreg Roach     * @return void
378a6f13a4aSGreg Roach     */
379b702978eSGreg Roach    protected function headerStartHandler(): void
380c1010edaSGreg Roach    {
381a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
382e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3837a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
384a6f13a4aSGreg Roach    }
385a6f13a4aSGreg Roach
386a6f13a4aSGreg Roach    /**
38776692c8bSGreg Roach     * XML <PageHeader>
3888ba2e626SGreg Roach     *
3898ba2e626SGreg Roach     * @return void
390a6f13a4aSGreg Roach     */
391b702978eSGreg Roach    protected function pageHeaderStartHandler(): void
392c1010edaSGreg Roach    {
3939b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
394a6f13a4aSGreg Roach        $this->print_data         = false;
3959b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
396e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
397a6f13a4aSGreg Roach    }
398a6f13a4aSGreg Roach
399a6f13a4aSGreg Roach    /**
40076692c8bSGreg Roach     * XML <pageHeaderEndHandler>
4018ba2e626SGreg Roach     *
4028ba2e626SGreg Roach     * @return void
403a6f13a4aSGreg Roach     */
404b702978eSGreg Roach    protected function pageHeaderEndHandler(): void
405c1010edaSGreg Roach    {
406a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
407e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
408e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
409e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
410a6f13a4aSGreg Roach    }
411a6f13a4aSGreg Roach
412a6f13a4aSGreg Roach    /**
41376692c8bSGreg Roach     * XML <bodyStartHandler>
4148ba2e626SGreg Roach     *
4158ba2e626SGreg Roach     * @return void
416a6f13a4aSGreg Roach     */
417b702978eSGreg Roach    protected function bodyStartHandler(): void
418c1010edaSGreg Roach    {
4197a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
420a6f13a4aSGreg Roach    }
421a6f13a4aSGreg Roach
422a6f13a4aSGreg Roach    /**
42376692c8bSGreg Roach     * XML <footerStartHandler>
4248ba2e626SGreg Roach     *
4258ba2e626SGreg Roach     * @return void
426a6f13a4aSGreg Roach     */
427b702978eSGreg Roach    protected function footerStartHandler(): void
428c1010edaSGreg Roach    {
4297a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
430a6f13a4aSGreg Roach    }
431a6f13a4aSGreg Roach
432a6f13a4aSGreg Roach    /**
43376692c8bSGreg Roach     * XML <Cell>
434a6f13a4aSGreg Roach     *
435c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4368ba2e626SGreg Roach     *
4378ba2e626SGreg Roach     * @return void
438a6f13a4aSGreg Roach     */
439b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
440c1010edaSGreg Roach    {
441a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4427a6ee1acSGreg Roach        $align = '';
443a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
444a6f13a4aSGreg Roach            $align = $attrs['align'];
445a6f13a4aSGreg Roach            // RTL supported left/right alignment
446044416d2SGreg Roach            if ($align === 'rightrtl') {
447e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4487a6ee1acSGreg Roach                    $align = 'left';
449a6f13a4aSGreg Roach                } else {
4507a6ee1acSGreg Roach                    $align = 'right';
451a6f13a4aSGreg Roach                }
452044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
453e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4547a6ee1acSGreg Roach                    $align = 'right';
455a6f13a4aSGreg Roach                } else {
4567a6ee1acSGreg Roach                    $align = 'left';
457a6f13a4aSGreg Roach                }
458a6f13a4aSGreg Roach            }
459a6f13a4aSGreg Roach        }
460a6f13a4aSGreg Roach
461a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4627a6ee1acSGreg Roach        $bgcolor = '';
463a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
464a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
465a6f13a4aSGreg Roach        }
466a6f13a4aSGreg Roach
467a6f13a4aSGreg Roach        // int Whether or not the background should be painted
468a6f13a4aSGreg Roach        $fill = 1;
469a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4707a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
471a6f13a4aSGreg Roach                $fill = 0;
4727a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
473a6f13a4aSGreg Roach                $fill = 1;
474a6f13a4aSGreg Roach            }
475a6f13a4aSGreg Roach        }
476a6f13a4aSGreg Roach
477a6f13a4aSGreg Roach        $reseth = true;
478a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
479a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4807a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
481a6f13a4aSGreg Roach                $reseth = false;
4827a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
483a6f13a4aSGreg Roach                $reseth = true;
484a6f13a4aSGreg Roach            }
485a6f13a4aSGreg Roach        }
486a6f13a4aSGreg Roach
487a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
488a6f13a4aSGreg Roach        $border = 0;
489a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
490a6f13a4aSGreg Roach            $border = $attrs['border'];
491a6f13a4aSGreg Roach        }
492a6f13a4aSGreg Roach        // string Border color in HTML code
4937a6ee1acSGreg Roach        $bocolor = '';
494a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
495a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
496a6f13a4aSGreg Roach        }
497a6f13a4aSGreg Roach
498a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
499a6f13a4aSGreg Roach        $height = 0;
500a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
501589feda3SGreg Roach            $height = $attrs['height'];
502a6f13a4aSGreg Roach        }
503a6f13a4aSGreg 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.
504a6f13a4aSGreg Roach        $width = 0;
505a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
506589feda3SGreg Roach            $width = $attrs['width'];
507a6f13a4aSGreg Roach        }
508a6f13a4aSGreg Roach
509a6f13a4aSGreg Roach        // int Stretch carachter mode
510a6f13a4aSGreg Roach        $stretch = 0;
511a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
512a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
513a6f13a4aSGreg Roach        }
514a6f13a4aSGreg Roach
515a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
516c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
517a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5187a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
519c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
520a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
521a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5227a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
523a6f13a4aSGreg Roach                $left = 0;
524a6f13a4aSGreg Roach            }
525a6f13a4aSGreg Roach        }
526a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
527c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
528a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5297a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
530c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
531a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
532a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5337a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
534a6f13a4aSGreg Roach                $top = 0;
535a6f13a4aSGreg Roach            }
536a6f13a4aSGreg Roach        }
537a6f13a4aSGreg Roach
538a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5397a6ee1acSGreg Roach        $style = '';
540a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
541a6f13a4aSGreg Roach            $style = $attrs['style'];
542a6f13a4aSGreg Roach        }
543a6f13a4aSGreg Roach
544a6f13a4aSGreg Roach        // string Text color in html code
5457a6ee1acSGreg Roach        $tcolor = '';
546a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
547a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
548a6f13a4aSGreg Roach        }
549a6f13a4aSGreg Roach
550a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
551a6f13a4aSGreg Roach        $ln = 0;
552a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
553a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
554a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5557a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
556a6f13a4aSGreg Roach                $ln = 0;
557a6f13a4aSGreg Roach            }
558a6f13a4aSGreg Roach        }
559a6f13a4aSGreg Roach
560044416d2SGreg Roach        if ($align === 'left') {
5617a6ee1acSGreg Roach            $align = 'L';
562044416d2SGreg Roach        } elseif ($align === 'right') {
5637a6ee1acSGreg Roach            $align = 'R';
564044416d2SGreg Roach        } elseif ($align === 'center') {
5657a6ee1acSGreg Roach            $align = 'C';
566044416d2SGreg Roach        } elseif ($align === 'justify') {
5677a6ee1acSGreg Roach            $align = 'J';
568a6f13a4aSGreg Roach        }
569a6f13a4aSGreg Roach
5709b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
571a6f13a4aSGreg Roach        $this->print_data         = true;
572a6f13a4aSGreg Roach
573e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
574a6f13a4aSGreg Roach            $width,
575a6f13a4aSGreg Roach            $height,
576a6f13a4aSGreg Roach            $border,
577a6f13a4aSGreg Roach            $align,
578a6f13a4aSGreg Roach            $bgcolor,
579a6f13a4aSGreg Roach            $style,
580a6f13a4aSGreg Roach            $ln,
581a6f13a4aSGreg Roach            $top,
582a6f13a4aSGreg Roach            $left,
583a6f13a4aSGreg Roach            $fill,
584a6f13a4aSGreg Roach            $stretch,
585a6f13a4aSGreg Roach            $bocolor,
586a6f13a4aSGreg Roach            $tcolor,
587a6f13a4aSGreg Roach            $reseth
588a6f13a4aSGreg Roach        );
589a6f13a4aSGreg Roach    }
590a6f13a4aSGreg Roach
591a6f13a4aSGreg Roach    /**
59276692c8bSGreg Roach     * XML </Cell>
5938ba2e626SGreg Roach     *
5948ba2e626SGreg Roach     * @return void
595a6f13a4aSGreg Roach     */
596b702978eSGreg Roach    protected function cellEndHandler(): void
597c1010edaSGreg Roach    {
598a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
599e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
600a6f13a4aSGreg Roach    }
601a6f13a4aSGreg Roach
602a6f13a4aSGreg Roach    /**
603a6f13a4aSGreg Roach     * XML <Now /> element handler
6048ba2e626SGreg Roach     *
6058ba2e626SGreg Roach     * @return void
606a6f13a4aSGreg Roach     */
607b702978eSGreg Roach    protected function nowStartHandler(): void
608c1010edaSGreg Roach    {
6094459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
610a6f13a4aSGreg Roach    }
611a6f13a4aSGreg Roach
612a6f13a4aSGreg Roach    /**
613a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6148ba2e626SGreg Roach     *
6158ba2e626SGreg Roach     * @return void
616a6f13a4aSGreg Roach     */
617b702978eSGreg Roach    protected function pageNumStartHandler(): void
618c1010edaSGreg Roach    {
6197a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
620a6f13a4aSGreg Roach    }
621a6f13a4aSGreg Roach
622a6f13a4aSGreg Roach    /**
623a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6248ba2e626SGreg Roach     *
6258ba2e626SGreg Roach     * @return void
626a6f13a4aSGreg Roach     */
627b702978eSGreg Roach    protected function totalPagesStartHandler(): void
628c1010edaSGreg Roach    {
6297a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
630a6f13a4aSGreg Roach    }
631a6f13a4aSGreg Roach
632a6f13a4aSGreg Roach    /**
633a6f13a4aSGreg Roach     * Called at the start of an element.
634a6f13a4aSGreg Roach     *
635c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6368ba2e626SGreg Roach     *
6378ba2e626SGreg Roach     * @return void
638a6f13a4aSGreg Roach     */
639b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
640c1010edaSGreg Roach    {
641a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
642a6f13a4aSGreg Roach            $this->process_gedcoms++;
643a6f13a4aSGreg Roach
644a6f13a4aSGreg Roach            return;
645a6f13a4aSGreg Roach        }
646a6f13a4aSGreg Roach
647a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6487a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6497a6ee1acSGreg Roach        $tags      = explode(':', $tag);
650a6f13a4aSGreg Roach        $newgedrec = '';
651a6f13a4aSGreg Roach        if (count($tags) < 2) {
652299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
653299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
654a6f13a4aSGreg Roach        }
655a6f13a4aSGreg Roach        if (empty($newgedrec)) {
656a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
657a6f13a4aSGreg Roach            $newgedrec = '';
658a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6597a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
660d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
661d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
662a6f13a4aSGreg Roach                    } else {
663299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
664299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
665a6f13a4aSGreg Roach                    }
666a6f13a4aSGreg Roach                } else {
6677a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66813abd6f3SGreg Roach                        $gmatch = [];
669a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
670299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
671299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
672a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
673a6f13a4aSGreg Roach                        } else {
674a6f13a4aSGreg Roach                            $newgedrec = '';
675a6f13a4aSGreg Roach                            break;
676a6f13a4aSGreg Roach                        }
677a6f13a4aSGreg Roach                    } else {
6787a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
6799a3accd7SGreg Roach                        $level     = 1 + (int) $temp[0];
6803d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
681a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
682a6f13a4aSGreg Roach                    }
683a6f13a4aSGreg Roach                }
684a6f13a4aSGreg Roach            }
685a6f13a4aSGreg Roach        }
686a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6879b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
688a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
689a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
690a6f13a4aSGreg Roach                $this->fact = $match[2];
691a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
692a6f13a4aSGreg Roach            }
693a6f13a4aSGreg Roach        } else {
694a6f13a4aSGreg Roach            $this->process_gedcoms++;
695a6f13a4aSGreg Roach        }
696a6f13a4aSGreg Roach    }
697a6f13a4aSGreg Roach
698a6f13a4aSGreg Roach    /**
699a6f13a4aSGreg Roach     * Called at the end of an element.
7008ba2e626SGreg Roach     *
7018ba2e626SGreg Roach     * @return void
702a6f13a4aSGreg Roach     */
703b702978eSGreg Roach    protected function gedcomEndHandler(): void
704c1010edaSGreg Roach    {
705a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
706a6f13a4aSGreg Roach            $this->process_gedcoms--;
707a6f13a4aSGreg Roach        } else {
70865e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
709a6f13a4aSGreg Roach        }
710a6f13a4aSGreg Roach    }
711a6f13a4aSGreg Roach
712a6f13a4aSGreg Roach    /**
71376692c8bSGreg Roach     * XML <textBoxStartHandler>
714a6f13a4aSGreg Roach     *
715c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7168ba2e626SGreg Roach     *
7178ba2e626SGreg Roach     * @return void
718a6f13a4aSGreg Roach     */
719b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
720c1010edaSGreg Roach    {
721a6f13a4aSGreg Roach        // string Background color code
7227a6ee1acSGreg Roach        $bgcolor = '';
723a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
724a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
725a6f13a4aSGreg Roach        }
726a6f13a4aSGreg Roach
727a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
728a6f13a4aSGreg Roach        $fill = true;
729a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7307a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
731a6f13a4aSGreg Roach                $fill = false;
7327a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
733a6f13a4aSGreg Roach                $fill = true;
734a6f13a4aSGreg Roach            }
735a6f13a4aSGreg Roach        }
736a6f13a4aSGreg Roach
737a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
738a6f13a4aSGreg Roach        $border = false;
739a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7407a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
741a6f13a4aSGreg Roach                $border = true;
7427a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
743a6f13a4aSGreg Roach                $border = false;
744a6f13a4aSGreg Roach            }
745a6f13a4aSGreg Roach        }
746a6f13a4aSGreg Roach
747a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
748a6f13a4aSGreg Roach        $height = 0;
749a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
750a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
751a6f13a4aSGreg Roach        }
752a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
753a6f13a4aSGreg Roach        $width = 0;
754a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
755a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
756a6f13a4aSGreg Roach        }
757a6f13a4aSGreg Roach
758a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
759c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
760a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7617a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
762c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
763a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
764a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7657a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
766a6f13a4aSGreg Roach                $left = 0;
767a6f13a4aSGreg Roach            }
768a6f13a4aSGreg Roach        }
769a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
770c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
771a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7727a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
773c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
774a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
775a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7767a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
777a6f13a4aSGreg Roach                $top = 0;
778a6f13a4aSGreg Roach            }
779a6f13a4aSGreg Roach        }
780a6f13a4aSGreg 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
781a6f13a4aSGreg Roach        $newline = false;
782a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7837a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
784a6f13a4aSGreg Roach                $newline = true;
7857a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
786a6f13a4aSGreg Roach                $newline = false;
787a6f13a4aSGreg Roach            }
788a6f13a4aSGreg Roach        }
789a6f13a4aSGreg Roach        // boolean
790a6f13a4aSGreg Roach        $pagecheck = true;
791a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7927a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
793a6f13a4aSGreg Roach                $pagecheck = false;
7947a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
795a6f13a4aSGreg Roach                $pagecheck = true;
796a6f13a4aSGreg Roach            }
797a6f13a4aSGreg Roach        }
798a6f13a4aSGreg Roach        // boolean Cell padding
799a6f13a4aSGreg Roach        $padding = true;
800a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8017a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
802a6f13a4aSGreg Roach                $padding = false;
8037a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
804a6f13a4aSGreg Roach                $padding = true;
805a6f13a4aSGreg Roach            }
806a6f13a4aSGreg Roach        }
807a6f13a4aSGreg Roach        // boolean Reset this box Height
808a6f13a4aSGreg Roach        $reseth = false;
809a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8107a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
811a6f13a4aSGreg Roach                $reseth = true;
8127a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
813a6f13a4aSGreg Roach                $reseth = false;
814a6f13a4aSGreg Roach            }
815a6f13a4aSGreg Roach        }
816a6f13a4aSGreg Roach
817a6f13a4aSGreg Roach        // string Style of rendering
8187a6ee1acSGreg Roach        $style = '';
819a6f13a4aSGreg Roach
8209b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
821a6f13a4aSGreg Roach        $this->print_data         = false;
822a6f13a4aSGreg Roach
8239b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
824e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
825a6f13a4aSGreg Roach            $width,
826a6f13a4aSGreg Roach            $height,
827a6f13a4aSGreg Roach            $border,
828a6f13a4aSGreg Roach            $bgcolor,
829a6f13a4aSGreg Roach            $newline,
830a6f13a4aSGreg Roach            $left,
831a6f13a4aSGreg Roach            $top,
832a6f13a4aSGreg Roach            $pagecheck,
833a6f13a4aSGreg Roach            $style,
834a6f13a4aSGreg Roach            $fill,
835a6f13a4aSGreg Roach            $padding,
836a6f13a4aSGreg Roach            $reseth
837a6f13a4aSGreg Roach        );
838a6f13a4aSGreg Roach    }
839a6f13a4aSGreg Roach
840a6f13a4aSGreg Roach    /**
84176692c8bSGreg Roach     * XML <textBoxEndHandler>
8428ba2e626SGreg Roach     *
8438ba2e626SGreg Roach     * @return void
844a6f13a4aSGreg Roach     */
845b702978eSGreg Roach    protected function textBoxEndHandler(): void
846c1010edaSGreg Roach    {
847a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
848e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
849e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
850e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
851a6f13a4aSGreg Roach    }
852a6f13a4aSGreg Roach
853a6f13a4aSGreg Roach    /**
85476692c8bSGreg Roach     * XLM <Text>.
85576692c8bSGreg Roach     *
856c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8578ba2e626SGreg Roach     *
8588ba2e626SGreg Roach     * @return void
859a6f13a4aSGreg Roach     */
860b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
861c1010edaSGreg Roach    {
8629b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
863a6f13a4aSGreg Roach        $this->print_data         = true;
864a6f13a4aSGreg Roach
865a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8667a6ee1acSGreg Roach        $style = '';
867a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
868a6f13a4aSGreg Roach            $style = $attrs['style'];
869a6f13a4aSGreg Roach        }
870a6f13a4aSGreg Roach
871a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8727a6ee1acSGreg Roach        $color = '';
873a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
874a6f13a4aSGreg Roach            $color = $attrs['color'];
875a6f13a4aSGreg Roach        }
876a6f13a4aSGreg Roach
877e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
878a6f13a4aSGreg Roach    }
879a6f13a4aSGreg Roach
880a6f13a4aSGreg Roach    /**
88176692c8bSGreg Roach     * XML </Text>
8828ba2e626SGreg Roach     *
8838ba2e626SGreg Roach     * @return void
884a6f13a4aSGreg Roach     */
885b702978eSGreg Roach    protected function textEndHandler(): void
886c1010edaSGreg Roach    {
887a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
888e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
889a6f13a4aSGreg Roach    }
890a6f13a4aSGreg Roach
891a6f13a4aSGreg Roach    /**
89276692c8bSGreg Roach     * XML <GetPersonName/>
893a6f13a4aSGreg Roach     * Get the name
894a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
895a6f13a4aSGreg Roach     * 2. id is set with a record id
896a6f13a4aSGreg Roach     *
897c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8988ba2e626SGreg Roach     *
8998ba2e626SGreg Roach     * @return void
900a6f13a4aSGreg Roach     */
901b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
902c1010edaSGreg Roach    {
9037a6ee1acSGreg Roach        $id    = '';
90413abd6f3SGreg Roach        $match = [];
905a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9067a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
907a6f13a4aSGreg Roach                $id = $match[1];
908a6f13a4aSGreg Roach            }
909a6f13a4aSGreg Roach        } else {
9107a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
911d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
912d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
913a6f13a4aSGreg Roach                }
914a6f13a4aSGreg Roach            } else {
9157a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91613abd6f3SGreg Roach                    $gmatch = [];
917a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
918a6f13a4aSGreg Roach                        $id = $gmatch[1];
919a6f13a4aSGreg Roach                    }
920a6f13a4aSGreg Roach                } else {
921a6f13a4aSGreg Roach                    $id = $attrs['id'];
922a6f13a4aSGreg Roach                }
923a6f13a4aSGreg Roach            }
924a6f13a4aSGreg Roach        }
925a6f13a4aSGreg Roach        if (!empty($id)) {
926299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9278f038c36SRico Sonntag            if ($record === null) {
928a6f13a4aSGreg Roach                return;
929a6f13a4aSGreg Roach            }
930a6f13a4aSGreg Roach            if (!$record->canShowName()) {
931a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
932a6f13a4aSGreg Roach            } else {
93339ca88baSGreg Roach                $name = $record->fullName();
934a6f13a4aSGreg Roach                $name = preg_replace(
935c1010edaSGreg Roach                    [
936c1010edaSGreg Roach                        '/<span class="starredname">/',
937c1010edaSGreg Roach                        '/<\/span><\/span>/',
938c1010edaSGreg Roach                        '/<\/span>/',
939c1010edaSGreg Roach                    ],
940c1010edaSGreg Roach                    [
941c1010edaSGreg Roach                        '«',
942c1010edaSGreg Roach                        '',
943c1010edaSGreg Roach                        '»',
944c1010edaSGreg Roach                    ],
945a6f13a4aSGreg Roach                    $name
946a6f13a4aSGreg Roach                );
947a6f13a4aSGreg Roach                $name = strip_tags($name);
948a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
9499a9e551aSGreg Roach                    $name = Str::limit($name, $attrs['truncate'], I18N::translate('…'));
950a6f13a4aSGreg Roach                } else {
95139ca88baSGreg Roach                    $addname = $record->alternateName();
952a6f13a4aSGreg Roach                    $addname = preg_replace(
953c1010edaSGreg Roach                        [
954c1010edaSGreg Roach                            '/<span class="starredname">/',
955c1010edaSGreg Roach                            '/<\/span><\/span>/',
956c1010edaSGreg Roach                            '/<\/span>/',
957c1010edaSGreg Roach                        ],
958c1010edaSGreg Roach                        [
959c1010edaSGreg Roach                            '«',
960c1010edaSGreg Roach                            '',
961c1010edaSGreg Roach                            '»',
962c1010edaSGreg Roach                        ],
963a6f13a4aSGreg Roach                        $addname
964a6f13a4aSGreg Roach                    );
965a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
966a6f13a4aSGreg Roach                    if (!empty($addname)) {
9677a6ee1acSGreg Roach                        $name .= ' ' . $addname;
968a6f13a4aSGreg Roach                    }
969a6f13a4aSGreg Roach                }
970a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
971a6f13a4aSGreg Roach            }
972a6f13a4aSGreg Roach        }
973a6f13a4aSGreg Roach    }
974a6f13a4aSGreg Roach
975a6f13a4aSGreg Roach    /**
97676692c8bSGreg Roach     * XML <GedcomValue/>
977a6f13a4aSGreg Roach     *
978c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9798ba2e626SGreg Roach     *
9808ba2e626SGreg Roach     * @return void
981a6f13a4aSGreg Roach     */
982b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
983c1010edaSGreg Roach    {
9847a6ee1acSGreg Roach        $id    = '';
98513abd6f3SGreg Roach        $match = [];
9867a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
987a6f13a4aSGreg Roach            $id = $match[1];
988a6f13a4aSGreg Roach        }
989a6f13a4aSGreg Roach
990044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9917a6ee1acSGreg Roach            $useBreak = '1';
992a6f13a4aSGreg Roach        } else {
9937a6ee1acSGreg Roach            $useBreak = '0';
994a6f13a4aSGreg Roach        }
995a6f13a4aSGreg Roach
996a6f13a4aSGreg Roach        $tag = $attrs['tag'];
997a6f13a4aSGreg Roach        if (!empty($tag)) {
998044416d2SGreg Roach            if ($tag === '@desc') {
999a6f13a4aSGreg Roach                $value = $this->desc;
1000a6f13a4aSGreg Roach                $value = trim($value);
1001a6f13a4aSGreg Roach                $this->current_element->addText($value);
1002a6f13a4aSGreg Roach            }
1003044416d2SGreg Roach            if ($tag === '@id') {
1004a6f13a4aSGreg Roach                $this->current_element->addText($id);
1005a6f13a4aSGreg Roach            } else {
10067a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1007a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
10087a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
1009a6f13a4aSGreg Roach                    $level = $temp[0];
1010a6f13a4aSGreg Roach                    if ($level == 0) {
1011a6f13a4aSGreg Roach                        $level++;
1012a6f13a4aSGreg Roach                    }
1013a6f13a4aSGreg Roach                } else {
1014a6f13a4aSGreg Roach                    $level = $attrs['level'];
1015a6f13a4aSGreg Roach                }
1016a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10173d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1018a6f13a4aSGreg Roach                switch (end($tags)) {
1019a6f13a4aSGreg Roach                    case 'DATE':
1020a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1021a6f13a4aSGreg Roach                        $value = $tmp->display();
1022a6f13a4aSGreg Roach                        break;
1023a6f13a4aSGreg Roach                    case 'PLAC':
1024299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1025392561bbSGreg Roach                        $value = $tmp->shortName();
1026a6f13a4aSGreg Roach                        break;
1027a6f13a4aSGreg Roach                }
1028044416d2SGreg Roach                if ($useBreak === '1') {
1029a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1030a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1031a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1032a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1033a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10343b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10353b3cfeeaSGreg Roach                        $value = substr($value, 4);
1036a6f13a4aSGreg Roach                    }
1037a6f13a4aSGreg Roach                }
1038d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1039c1010edaSGreg Roach                if (in_array(end($tmp), [
1040c1010edaSGreg Roach                    'NOTE',
1041c1010edaSGreg Roach                    'TEXT',
1042c1010edaSGreg Roach                ])) {
1043299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1044a4d703aeSGreg Roach                }
1045a2a485c3SGreg Roach
1046a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
10479c32db08SGreg Roach                    $value = strip_tags($value);
1048a2a485c3SGreg Roach                    $value = Str::limit($value, $attrs['truncate'], I18N::translate('…'));
1049a2a485c3SGreg Roach                }
1050a6f13a4aSGreg Roach                $this->current_element->addText($value);
1051a6f13a4aSGreg Roach            }
1052a6f13a4aSGreg Roach        }
1053a6f13a4aSGreg Roach    }
1054a6f13a4aSGreg Roach
1055a6f13a4aSGreg Roach    /**
105676692c8bSGreg Roach     * XML <RepeatTag>
1057a6f13a4aSGreg Roach     *
1058c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10598ba2e626SGreg Roach     *
10608ba2e626SGreg Roach     * @return void
1061a6f13a4aSGreg Roach     */
1062b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1063c1010edaSGreg Roach    {
1064a6f13a4aSGreg Roach        $this->process_repeats++;
1065a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1066a6f13a4aSGreg Roach            return;
1067a6f13a4aSGreg Roach        }
1068a6f13a4aSGreg Roach
10699b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
107013abd6f3SGreg Roach        $this->repeats         = [];
1071e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1072a6f13a4aSGreg Roach
1073e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1074a6f13a4aSGreg Roach        if (!empty($tag)) {
1075044416d2SGreg Roach            if ($tag === '@desc') {
1076a6f13a4aSGreg Roach                $value = $this->desc;
1077a6f13a4aSGreg Roach                $value = trim($value);
1078a6f13a4aSGreg Roach                $this->current_element->addText($value);
1079a6f13a4aSGreg Roach            } else {
10807a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10817a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10827a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1083a6f13a4aSGreg Roach                $level = $temp[0];
1084a6f13a4aSGreg Roach                if ($level == 0) {
1085a6f13a4aSGreg Roach                    $level++;
1086a6f13a4aSGreg Roach                }
1087a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1088a6f13a4aSGreg Roach                $t      = $tag;
1089a6f13a4aSGreg Roach                $count  = count($tags);
1090a6f13a4aSGreg Roach                $i      = 0;
1091a6f13a4aSGreg Roach                while ($i < $count) {
1092a6f13a4aSGreg Roach                    $t = $tags[$i];
1093a6f13a4aSGreg Roach                    if (!empty($t)) {
1094a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10953d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1096a6f13a4aSGreg Roach                            if (empty($subrec)) {
1097a6f13a4aSGreg Roach                                $level--;
10983d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1099a6f13a4aSGreg Roach                                if (empty($subrec)) {
1100a6f13a4aSGreg Roach                                    return;
1101a6f13a4aSGreg Roach                                }
1102a6f13a4aSGreg Roach                            }
1103a6f13a4aSGreg Roach                        }
1104a6f13a4aSGreg Roach                        $level++;
1105a6f13a4aSGreg Roach                    }
1106a6f13a4aSGreg Roach                    $i++;
1107a6f13a4aSGreg Roach                }
1108a6f13a4aSGreg Roach                $level--;
1109a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1110a6f13a4aSGreg Roach                $i     = 0;
1111a6f13a4aSGreg Roach                while ($i < $count) {
1112a6f13a4aSGreg Roach                    $i++;
1113a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1114a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11158d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1116299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1117a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1118a9007102SGreg Roach                            continue;
1119a9007102SGreg Roach                        }
1120a9007102SGreg Roach                    }
1121a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1122a6f13a4aSGreg Roach                }
1123a6f13a4aSGreg Roach            }
1124a6f13a4aSGreg Roach        }
1125a6f13a4aSGreg Roach    }
1126a6f13a4aSGreg Roach
1127a6f13a4aSGreg Roach    /**
112876692c8bSGreg Roach     * XML </ RepeatTag>
11298ba2e626SGreg Roach     *
11308ba2e626SGreg Roach     * @return void
1131a6f13a4aSGreg Roach     */
1132b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1133c1010edaSGreg Roach    {
1134a6f13a4aSGreg Roach        $this->process_repeats--;
1135a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1136a6f13a4aSGreg Roach            return;
1137a6f13a4aSGreg Roach        }
1138a6f13a4aSGreg Roach
1139a6f13a4aSGreg Roach        // Check if there is anything to repeat
1140a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1141a6f13a4aSGreg Roach            // No need to load them if not used...
1142a6f13a4aSGreg Roach
1143a6f13a4aSGreg Roach            $lineoffset = 0;
1144a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1145a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1146a6f13a4aSGreg Roach            }
1147a6f13a4aSGreg Roach            //-- read the xml from the file
1148299d100dSGreg Roach            $lines = file($this->report);
11497a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1150a6f13a4aSGreg Roach                $lineoffset--;
1151a6f13a4aSGreg Roach            }
1152a6f13a4aSGreg Roach            $lineoffset++;
1153a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1154a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1155a6f13a4aSGreg Roach            // RepeatTag Level counter
1156a6f13a4aSGreg Roach            $count = 1;
1157a6f13a4aSGreg Roach            while (0 < $count) {
11587a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1159a6f13a4aSGreg Roach                    $count++;
11607a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1161a6f13a4aSGreg Roach                    $count--;
1162a6f13a4aSGreg Roach                }
1163a6f13a4aSGreg Roach                if (0 < $count) {
1164a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1165a6f13a4aSGreg Roach                }
1166a6f13a4aSGreg Roach                $line_nr++;
1167a6f13a4aSGreg Roach            }
1168a6f13a4aSGreg Roach            // No need to drag this
1169a6f13a4aSGreg Roach            unset($lines);
1170a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1171a6f13a4aSGreg Roach            // Save original values
11729b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1173a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1174a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1175a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1176a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1177e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1178a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11791aa04befSGreg Roach
11801aa04befSGreg Roach                xml_set_element_handler(
11811aa04befSGreg Roach                    $repeat_parser,
11829d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11831aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11841aa04befSGreg Roach                    },
11859d454b6bSGreg Roach                    function ($parser, string $name): void {
11861aa04befSGreg Roach                        $this->endElement($parser, $name);
11871aa04befSGreg Roach                    }
11881aa04befSGreg Roach                );
11891aa04befSGreg Roach
11901aa04befSGreg Roach                xml_set_character_data_handler(
11911aa04befSGreg Roach                    $repeat_parser,
11929d454b6bSGreg Roach                    function ($parser, string $data): void {
11931aa04befSGreg Roach                        $this->characterData($parser, $data);
11941aa04befSGreg Roach                    }
11951aa04befSGreg Roach                );
11961aa04befSGreg Roach
1197a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
11986ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1199a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1200a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1201a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1202a6f13a4aSGreg Roach                    ));
1203a6f13a4aSGreg Roach                }
1204a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1205a6f13a4aSGreg Roach            }
1206a6f13a4aSGreg Roach            // Restore original values
1207a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1208e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1209a6f13a4aSGreg Roach        }
121065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1211a6f13a4aSGreg Roach    }
1212a6f13a4aSGreg Roach
1213a6f13a4aSGreg Roach    /**
1214a6f13a4aSGreg Roach     * Variable lookup
1215a6f13a4aSGreg Roach     * Retrieve predefined variables :
1216a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1217a6f13a4aSGreg Roach     *        1 EVEN This is a description
1218a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1219a6f13a4aSGreg Roach     * $ I18N::translate('....')
1220a6f13a4aSGreg Roach     * $ language_settings[]
1221a6f13a4aSGreg Roach     *
1222c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12238ba2e626SGreg Roach     *
12248ba2e626SGreg Roach     * @return void
1225a6f13a4aSGreg Roach     */
1226b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1227c1010edaSGreg Roach    {
1228a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12296ccdf4f0SGreg 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));
1230a6f13a4aSGreg Roach        }
1231a6f13a4aSGreg Roach
1232a6f13a4aSGreg Roach        $var = $attrs['var'];
1233a6f13a4aSGreg Roach        // SetVar element preset variables
1234d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1235d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1236a6f13a4aSGreg Roach        } else {
1237a6f13a4aSGreg Roach            $tfact = $this->fact;
12387a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1239a6f13a4aSGreg Roach                // Use :
1240a6f13a4aSGreg Roach                // n TYPE This text if string
1241a6f13a4aSGreg Roach                $tfact = $this->type;
1242a6f13a4aSGreg Roach            }
1243c1010edaSGreg Roach            $var = str_replace([
1244c1010edaSGreg Roach                '@fact',
1245c1010edaSGreg Roach                '@desc',
1246c1010edaSGreg Roach            ], [
1247c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1248c1010edaSGreg Roach                $this->desc,
1249c1010edaSGreg Roach            ], $var);
1250a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1251da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1252a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1253a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1254a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1255a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1256a6f13a4aSGreg Roach            }
1257a6f13a4aSGreg Roach        }
1258a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1259a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12607a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1261a6f13a4aSGreg Roach                $g   = new Date($var);
1262a6f13a4aSGreg Roach                $var = $g->display();
1263a6f13a4aSGreg Roach            }
1264a6f13a4aSGreg Roach        }
1265a6f13a4aSGreg Roach        $this->current_element->addText($var);
12662836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1267a6f13a4aSGreg Roach    }
1268a6f13a4aSGreg Roach
1269a6f13a4aSGreg Roach    /**
127076692c8bSGreg Roach     * XML <Facts>
127176692c8bSGreg Roach     *
1272c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12738ba2e626SGreg Roach     *
12748ba2e626SGreg Roach     * @return void
1275a6f13a4aSGreg Roach     */
1276b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1277c1010edaSGreg Roach    {
1278a6f13a4aSGreg Roach        $this->process_repeats++;
1279a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1280a6f13a4aSGreg Roach            return;
1281a6f13a4aSGreg Roach        }
1282a6f13a4aSGreg Roach
12839b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
128413abd6f3SGreg Roach        $this->repeats         = [];
1285e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1286a6f13a4aSGreg Roach
12877a6ee1acSGreg Roach        $id    = '';
128813abd6f3SGreg Roach        $match = [];
12897a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1290a6f13a4aSGreg Roach            $id = $match[1];
1291a6f13a4aSGreg Roach        }
12927a6ee1acSGreg Roach        $tag = '';
1293a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1294a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1295a6f13a4aSGreg Roach        }
12967a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1297d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1298a6f13a4aSGreg Roach        }
1299a6f13a4aSGreg Roach
1300299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1301a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1302820b62dfSGreg Roach            $facts = $record->facts([], true);
130313abd6f3SGreg Roach            $this->repeats = [];
1304a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1305195b5e75SGreg Roach            foreach ($facts as $fact) {
1306e364afe4SGreg Roach                if (!in_array($fact->getTag(), $nonfacts, true)) {
1307195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1308a6f13a4aSGreg Roach                }
1309a6f13a4aSGreg Roach            }
1310a6f13a4aSGreg Roach        } else {
131130158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1312195b5e75SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1313138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1314a6f13a4aSGreg Roach                }
1315a6f13a4aSGreg Roach            }
1316a6f13a4aSGreg Roach        }
1317a6f13a4aSGreg Roach    }
1318a6f13a4aSGreg Roach
1319a6f13a4aSGreg Roach    /**
132076692c8bSGreg Roach     * XML </Facts>
13218ba2e626SGreg Roach     *
13228ba2e626SGreg Roach     * @return void
1323a6f13a4aSGreg Roach     */
1324b702978eSGreg Roach    protected function factsEndHandler(): void
1325c1010edaSGreg Roach    {
1326a6f13a4aSGreg Roach        $this->process_repeats--;
1327a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1328a6f13a4aSGreg Roach            return;
1329a6f13a4aSGreg Roach        }
1330a6f13a4aSGreg Roach
1331a6f13a4aSGreg Roach        // Check if there is anything to repeat
1332a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1333e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1334a6f13a4aSGreg Roach            $lineoffset = 0;
1335a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1336a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1337a6f13a4aSGreg Roach            }
1338a6f13a4aSGreg Roach
1339a6f13a4aSGreg Roach            //-- read the xml from the file
1340299d100dSGreg Roach            $lines = file($this->report);
1341a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1342a6f13a4aSGreg Roach                $lineoffset--;
1343a6f13a4aSGreg Roach            }
1344a6f13a4aSGreg Roach            $lineoffset++;
1345a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1346a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1347a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1348a6f13a4aSGreg Roach            while ($line_nr < $i) {
1349a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1350a6f13a4aSGreg Roach                $line_nr++;
1351a6f13a4aSGreg Roach            }
1352a6f13a4aSGreg Roach            // No need to drag this
1353a6f13a4aSGreg Roach            unset($lines);
1354a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1355a6f13a4aSGreg Roach            // Save original values
13569b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1357a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1358a6f13a4aSGreg Roach            $count                = count($this->repeats);
1359a6f13a4aSGreg Roach            $i                    = 0;
1360a6f13a4aSGreg Roach            while ($i < $count) {
1361a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1362a6f13a4aSGreg Roach                $this->fact   = '';
1363a6f13a4aSGreg Roach                $this->desc   = '';
1364a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1365a6f13a4aSGreg Roach                    $this->fact = $match[1];
1366a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
136713abd6f3SGreg Roach                        $tmatch = [];
1368a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1369a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1370a6f13a4aSGreg Roach                        } else {
1371a6f13a4aSGreg Roach                            $this->type = ' ';
1372a6f13a4aSGreg Roach                        }
1373a6f13a4aSGreg Roach                    }
1374a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13753d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1376a6f13a4aSGreg Roach                }
1377a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1378e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1379a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13801aa04befSGreg Roach
13811aa04befSGreg Roach                xml_set_element_handler(
13821aa04befSGreg Roach                    $repeat_parser,
13839d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13841aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13851aa04befSGreg Roach                    },
13869d454b6bSGreg Roach                    function ($parser, string $name): void {
13871aa04befSGreg Roach                        $this->endElement($parser, $name);
13881aa04befSGreg Roach                    }
13891aa04befSGreg Roach                );
13901aa04befSGreg Roach
13911aa04befSGreg Roach                xml_set_character_data_handler(
13921aa04befSGreg Roach                    $repeat_parser,
13939d454b6bSGreg Roach                    function ($parser, string $data): void {
13941aa04befSGreg Roach                        $this->characterData($parser, $data);
13951aa04befSGreg Roach                    }
13961aa04befSGreg Roach                );
13971aa04befSGreg Roach
1398a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
13996ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1400a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1401a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1402a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1403a6f13a4aSGreg Roach                    ));
1404a6f13a4aSGreg Roach                }
1405a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1406a6f13a4aSGreg Roach                $i++;
1407a6f13a4aSGreg Roach            }
1408a6f13a4aSGreg Roach            // Restore original values
1409e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1410a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1411a6f13a4aSGreg Roach        }
141265e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1413a6f13a4aSGreg Roach    }
1414a6f13a4aSGreg Roach
1415a6f13a4aSGreg Roach    /**
1416a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1417d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1418a6f13a4aSGreg Roach     *
1419c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14208ba2e626SGreg Roach     *
14218ba2e626SGreg Roach     * @return void
1422a6f13a4aSGreg Roach     */
1423b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1424c1010edaSGreg Roach    {
1425a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14266ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1427a6f13a4aSGreg Roach        }
1428a6f13a4aSGreg Roach
1429a6f13a4aSGreg Roach        $name  = $attrs['name'];
1430a6f13a4aSGreg Roach        $value = $attrs['value'];
143113abd6f3SGreg Roach        $match = [];
1432a6f13a4aSGreg Roach        // Current GEDCOM record strings
1433044416d2SGreg Roach        if ($value === '@ID') {
14347a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1435a6f13a4aSGreg Roach                $value = $match[1];
1436a6f13a4aSGreg Roach            }
1437044416d2SGreg Roach        } elseif ($value === '@fact') {
1438a6f13a4aSGreg Roach            $value = $this->fact;
1439044416d2SGreg Roach        } elseif ($value === '@desc') {
1440a6f13a4aSGreg Roach            $value = $this->desc;
1441044416d2SGreg Roach        } elseif ($value === '@generation') {
1442589feda3SGreg Roach            $value = (string) $this->generation;
1443a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144413abd6f3SGreg Roach            $gmatch = [];
1445a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14467a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1447a6f13a4aSGreg Roach            }
1448a6f13a4aSGreg Roach        }
1449a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1450d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1451a6f13a4aSGreg Roach        }
1452a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1453a6f13a4aSGreg Roach        $i     = 0;
1454a6f13a4aSGreg Roach        while ($i < $count) {
1455d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14567a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1457a6f13a4aSGreg Roach            $i++;
1458a6f13a4aSGreg Roach        }
1459a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1460da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1461a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1462a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1463a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1464a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1465a6f13a4aSGreg Roach        }
146652868398SGreg Roach
1467a6f13a4aSGreg Roach        // Arithmetic functions
1468a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
146952868398SGreg Roach            // Create an expression language with the functions used by our reports.
147052868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1471c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1472c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
147352868398SGreg Roach
147452868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1475a6f13a4aSGreg Roach        }
147652868398SGreg Roach
14777a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14787a6ee1acSGreg Roach            $value = '';
1479a6f13a4aSGreg Roach        }
1480d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1481a6f13a4aSGreg Roach    }
1482a6f13a4aSGreg Roach
1483a6f13a4aSGreg Roach    /**
1484a6f13a4aSGreg Roach     * XML <if > start element
1485a6f13a4aSGreg Roach     *
1486c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14878ba2e626SGreg Roach     *
14888ba2e626SGreg Roach     * @return void
1489a6f13a4aSGreg Roach     */
1490b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1491c1010edaSGreg Roach    {
1492a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1493a6f13a4aSGreg Roach            $this->process_ifs++;
1494a6f13a4aSGreg Roach
1495a6f13a4aSGreg Roach            return;
1496a6f13a4aSGreg Roach        }
1497a6f13a4aSGreg Roach
1498a6f13a4aSGreg Roach        $condition = $attrs['condition'];
149982759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1500c1010edaSGreg Roach        $condition = str_replace([
1501c1010edaSGreg Roach            ' LT ',
1502c1010edaSGreg Roach            ' GT ',
1503c1010edaSGreg Roach        ], [
1504c1010edaSGreg Roach            '<',
1505c1010edaSGreg Roach            '>',
1506c1010edaSGreg Roach        ], $condition);
1507a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15087a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
150913abd6f3SGreg Roach        $match     = [];
1510a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1511a6f13a4aSGreg Roach        $i         = 0;
1512a6f13a4aSGreg Roach        while ($i < $count) {
1513a6f13a4aSGreg Roach            $id    = $match[$i][1];
1514a6f13a4aSGreg Roach            $value = '""';
1515044416d2SGreg Roach            if ($id === 'ID') {
15167a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1517a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1518a6f13a4aSGreg Roach                }
15197a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1520a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15217a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1522a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15237a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1524a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1525a6f13a4aSGreg Roach            } else {
15267a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1527a6f13a4aSGreg Roach                $level = $temp[0];
1528a6f13a4aSGreg Roach                if ($level == 0) {
1529a6f13a4aSGreg Roach                    $level++;
1530a6f13a4aSGreg Roach                }
15313d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1532a6f13a4aSGreg Roach                if (empty($value)) {
1533a6f13a4aSGreg Roach                    $level++;
15343d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1535a6f13a4aSGreg Roach                }
15368d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15375e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1538a6f13a4aSGreg Roach            }
1539a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1540a6f13a4aSGreg Roach            $i++;
1541a6f13a4aSGreg Roach        }
15425809450fSGreg Roach
1543cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1544cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1545c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1546c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1547cb63a60eSGreg Roach
1548cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15495809450fSGreg Roach
1550a6f13a4aSGreg Roach        if (!$ret) {
1551a6f13a4aSGreg Roach            $this->process_ifs++;
1552a6f13a4aSGreg Roach        }
1553a6f13a4aSGreg Roach    }
1554a6f13a4aSGreg Roach
1555a6f13a4aSGreg Roach    /**
1556a6f13a4aSGreg Roach     * XML <if /> end element
15578ba2e626SGreg Roach     *
15588ba2e626SGreg Roach     * @return void
1559a6f13a4aSGreg Roach     */
1560b702978eSGreg Roach    protected function ifEndHandler(): void
1561c1010edaSGreg Roach    {
1562a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1563a6f13a4aSGreg Roach            $this->process_ifs--;
1564a6f13a4aSGreg Roach        }
1565a6f13a4aSGreg Roach    }
1566a6f13a4aSGreg Roach
1567a6f13a4aSGreg Roach    /**
1568a6f13a4aSGreg Roach     * XML <Footnote > start element
1569a6f13a4aSGreg Roach     * Collect the Footnote links
1570a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1571a6f13a4aSGreg Roach     *
1572c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15738ba2e626SGreg Roach     *
15748ba2e626SGreg Roach     * @return void
1575a6f13a4aSGreg Roach     */
1576b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1577c1010edaSGreg Roach    {
15787a6ee1acSGreg Roach        $id = '';
15797a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1580a6f13a4aSGreg Roach            $id = $match[2];
1581a6f13a4aSGreg Roach        }
1582299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1583a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15849b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1585a6f13a4aSGreg Roach            $this->print_data         = true;
15867a6ee1acSGreg Roach            $style                    = '';
1587a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1588a6f13a4aSGreg Roach                $style = $attrs['style'];
1589a6f13a4aSGreg Roach            }
1590a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1591e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1592a6f13a4aSGreg Roach        } else {
1593a6f13a4aSGreg Roach            $this->print_data       = false;
1594a6f13a4aSGreg Roach            $this->process_footnote = false;
1595a6f13a4aSGreg Roach        }
1596a6f13a4aSGreg Roach    }
1597a6f13a4aSGreg Roach
1598a6f13a4aSGreg Roach    /**
1599a6f13a4aSGreg Roach     * XML <Footnote /> end element
1600a6f13a4aSGreg Roach     * Print the collected Footnote data
16018ba2e626SGreg Roach     *
16028ba2e626SGreg Roach     * @return void
1603a6f13a4aSGreg Roach     */
1604b702978eSGreg Roach    protected function footnoteEndHandler(): void
1605c1010edaSGreg Roach    {
1606a6f13a4aSGreg Roach        if ($this->process_footnote) {
1607a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1608a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1609a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1610e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1611a6f13a4aSGreg Roach            }
1612a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1613a6f13a4aSGreg Roach        } else {
1614a6f13a4aSGreg Roach            $this->process_footnote = true;
1615a6f13a4aSGreg Roach        }
1616a6f13a4aSGreg Roach    }
1617a6f13a4aSGreg Roach
1618a6f13a4aSGreg Roach    /**
1619a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16208ba2e626SGreg Roach     *
16218ba2e626SGreg Roach     * @return void
1622a6f13a4aSGreg Roach     */
1623b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1624c1010edaSGreg Roach    {
16257a6ee1acSGreg Roach        $temp = 'footnotetexts';
1626e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1627a6f13a4aSGreg Roach    }
1628a6f13a4aSGreg Roach
1629a6f13a4aSGreg Roach    /**
1630a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16318ba2e626SGreg Roach     *
16328ba2e626SGreg Roach     * @return void
1633a6f13a4aSGreg Roach     */
1634b702978eSGreg Roach    protected function brStartHandler(): void
1635c1010edaSGreg Roach    {
1636a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1637a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1638a6f13a4aSGreg Roach        }
1639a6f13a4aSGreg Roach    }
1640a6f13a4aSGreg Roach
1641a6f13a4aSGreg Roach    /**
1642a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16438ba2e626SGreg Roach     *
16448ba2e626SGreg Roach     * @return void
1645a6f13a4aSGreg Roach     */
1646b702978eSGreg Roach    protected function spStartHandler(): void
1647c1010edaSGreg Roach    {
1648a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1649a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1650a6f13a4aSGreg Roach        }
1651a6f13a4aSGreg Roach    }
1652a6f13a4aSGreg Roach
1653a6f13a4aSGreg Roach    /**
165476692c8bSGreg Roach     * XML <HighlightedImage/>
165576692c8bSGreg Roach     *
1656c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16578ba2e626SGreg Roach     *
16588ba2e626SGreg Roach     * @return void
1659a6f13a4aSGreg Roach     */
1660b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1661c1010edaSGreg Roach    {
1662a6f13a4aSGreg Roach        $id = '';
16637a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1664a6f13a4aSGreg Roach            $id = $match[1];
1665a6f13a4aSGreg Roach        }
1666a6f13a4aSGreg Roach
1667c21bdddcSGreg Roach        // Position the top corner of this box on the page
1668c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1669a6f13a4aSGreg Roach
1670c21bdddcSGreg Roach        // Position the left corner of this box on the page
1671c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1672a6f13a4aSGreg Roach
167383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1675a6f13a4aSGreg Roach
1676a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
167783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1678a6f13a4aSGreg Roach
167983cdc021SGreg Roach        // Width, height (or both).
1680c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1681c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1682a6f13a4aSGreg Roach
1683299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16844a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
168586a63f51SGreg Roach
168686a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1687c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1688c1010edaSGreg Roach                0,
1689c1010edaSGreg Roach                0,
1690c1010edaSGreg Roach            ];
1691a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16923c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16933c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1694a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16953c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16963c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1697a6f13a4aSGreg Roach            } else {
16983c3b90deSGreg Roach                $width  = $attributes[0];
16993c3b90deSGreg Roach                $height = $attributes[1];
1700a6f13a4aSGreg Roach            }
17014a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1702e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1703a6f13a4aSGreg Roach        }
1704a6f13a4aSGreg Roach    }
1705a6f13a4aSGreg Roach
1706a6f13a4aSGreg Roach    /**
170776692c8bSGreg Roach     * XML <Image/>
170876692c8bSGreg Roach     *
1709c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17108ba2e626SGreg Roach     *
17118ba2e626SGreg Roach     * @return void
1712a6f13a4aSGreg Roach     */
1713b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1714c1010edaSGreg Roach    {
171583cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1716c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1717a6f13a4aSGreg Roach
1718a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1719c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1720a6f13a4aSGreg Roach
172183cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172283cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1723a6f13a4aSGreg Roach
1724a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172583cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1726a6f13a4aSGreg Roach
172783cdc021SGreg Roach        // Width, height (or both).
1728c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1729c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1730a6f13a4aSGreg Roach
173183cdc021SGreg Roach        $file = $attrs['file'] ?? '';
173283cdc021SGreg Roach
1733044416d2SGreg Roach        if ($file === '@FILE') {
173413abd6f3SGreg Roach            $match = [];
1735a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1736299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17374a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1738cdf416fbSGreg Roach
17394a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1740c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1741c1010edaSGreg Roach                        0,
1742c1010edaSGreg Roach                        0,
1743c1010edaSGreg Roach                    ];
1744a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17453c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17463c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1747a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17483c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17493c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1750a6f13a4aSGreg Roach                    } else {
17513c3b90deSGreg Roach                        $width  = $attributes[0];
17523c3b90deSGreg Roach                        $height = $attributes[1];
1753a6f13a4aSGreg Roach                    }
17544a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1755e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1756a6f13a4aSGreg Roach                }
1757a6f13a4aSGreg Roach            }
1758a6f13a4aSGreg Roach        } else {
17597a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1760a6f13a4aSGreg Roach                $size = getimagesize($file);
1761a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1762a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1763a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1764a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1765a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1766a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1767a6f13a4aSGreg Roach                } else {
1768a6f13a4aSGreg Roach                    $width  = $size[0];
1769a6f13a4aSGreg Roach                    $height = $size[1];
1770a6f13a4aSGreg Roach                }
1771e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1772e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1773a6f13a4aSGreg Roach            }
1774a6f13a4aSGreg Roach        }
1775a6f13a4aSGreg Roach    }
1776a6f13a4aSGreg Roach
1777a6f13a4aSGreg Roach    /**
1778a6f13a4aSGreg Roach     * XML <Line> element handler
1779a6f13a4aSGreg Roach     *
1780c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17818ba2e626SGreg Roach     *
17828ba2e626SGreg Roach     * @return void
1783a6f13a4aSGreg Roach     */
1784b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1785c1010edaSGreg Roach    {
1786a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1787c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1788a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17897a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1790a6f13a4aSGreg Roach                $x1 = 0;
17917a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1792c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1793a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1794c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1795a6f13a4aSGreg Roach            }
1796a6f13a4aSGreg Roach        }
1797a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1798c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1799a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18007a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1801a6f13a4aSGreg Roach                $y1 = 0;
18027a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1803c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1804a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1805c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1806a6f13a4aSGreg Roach            }
1807a6f13a4aSGreg Roach        }
1808a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1809c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1810a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18117a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1812a6f13a4aSGreg Roach                $x2 = 0;
18137a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1814c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1815a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1816c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1817a6f13a4aSGreg Roach            }
1818a6f13a4aSGreg Roach        }
1819a6f13a4aSGreg Roach        // End vertical position
1820c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1821a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18227a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1823a6f13a4aSGreg Roach                $y2 = 0;
18247a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1825c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1826a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1827c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1828a6f13a4aSGreg Roach            }
1829a6f13a4aSGreg Roach        }
1830a6f13a4aSGreg Roach
1831e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1832e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1833a6f13a4aSGreg Roach    }
1834a6f13a4aSGreg Roach
1835a6f13a4aSGreg Roach    /**
183676692c8bSGreg Roach     * XML <List>
1837a6f13a4aSGreg Roach     *
1838c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18398ba2e626SGreg Roach     *
18408ba2e626SGreg Roach     * @return void
1841a6f13a4aSGreg Roach     */
1842b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1843c1010edaSGreg Roach    {
1844a6f13a4aSGreg Roach        $this->process_repeats++;
1845a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1846a6f13a4aSGreg Roach            return;
1847a6f13a4aSGreg Roach        }
1848a6f13a4aSGreg Roach
184913abd6f3SGreg Roach        $match = [];
1850a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1851a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1852a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1853d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1854a6f13a4aSGreg Roach                $sortby = trim($sortby);
1855a6f13a4aSGreg Roach            }
1856a6f13a4aSGreg Roach        } else {
18577a6ee1acSGreg Roach            $sortby = 'NAME';
1858a6f13a4aSGreg Roach        }
1859a6f13a4aSGreg Roach
1860e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1861195b5e75SGreg Roach
1862a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1863a6f13a4aSGreg Roach        switch ($listname) {
18647a6ee1acSGreg Roach            case 'pending':
18656c0bef3aSGreg Roach                $xrefs = DB::table('change')
1866195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1867a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1868195b5e75SGreg Roach                            ->from('change')
1869195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1870195b5e75SGreg Roach                            ->where('status', '=', 'pending')
1871*7f5c2944SGreg Roach                            ->groupBy(['xref']);
1872195b5e75SGreg Roach                    })
18736c0bef3aSGreg Roach                    ->pluck('xref');
1874195b5e75SGreg Roach
187513abd6f3SGreg Roach                $this->list = [];
18766c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18776c0bef3aSGreg Roach                    $this->list[] = GedcomRecord::getInstance($xref, $this->tree);
1878a6f13a4aSGreg Roach                }
1879a6f13a4aSGreg Roach                break;
1880a6f13a4aSGreg Roach            case 'individual':
18815985adfbSGreg Roach                $query = DB::table('individuals')
18825985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18835985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18845985adfbSGreg Roach                    ->distinct();
18855985adfbSGreg Roach
1886a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1887a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
188882759250SGreg Roach                        $value = $this->substituteVars($value, false);
1889a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1890a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
18910b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
18925985adfbSGreg Roach                                $join
18935985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
18945985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
18955985adfbSGreg Roach                            });
18965985adfbSGreg Roach
18975985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
18985985adfbSGreg Roach
1899a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19005985adfbSGreg Roach
1901044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19025985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1903a6f13a4aSGreg Roach                            } else {
19045985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1905a6f13a4aSGreg Roach                            }
19065985adfbSGreg Roach
19075985adfbSGreg Roach                            // This filter has been fully processed
19085985adfbSGreg Roach                            unset($attrs[$attr]);
19097ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19100b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19115985adfbSGreg Roach                                $join
19125985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19135985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19145985adfbSGreg Roach                            });
1915a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19167a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19175d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1918fbbe964bSGreg Roach                                $query->whereContains($attr . '.n_full', $name);
1919a6f13a4aSGreg Roach                            }
19205985adfbSGreg Roach
19215985adfbSGreg Roach                            // This filter has been fully processed
19225985adfbSGreg Roach                            unset($attrs[$attr]);
1923a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19245985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1925a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19265985adfbSGreg Roach
1927a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19285985adfbSGreg Roach
19295985adfbSGreg Roach                            // This filter has been fully processed
19305985adfbSGreg Roach                            unset($attrs[$attr]);
19312fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19329dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19335985adfbSGreg Roach                            $query
19340b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19355985adfbSGreg Roach                                    $join
19367ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19377ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19385985adfbSGreg Roach                                })
19390b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19407ee4bfadSGreg Roach                                    $join
19417ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19427ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19437ee4bfadSGreg Roach                                })
19447ee4bfadSGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
19456e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19465985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19477ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19486e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19496e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
19506e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
19516e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19526e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1953e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19547ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1955a6f13a4aSGreg Roach                        }
1956a6f13a4aSGreg Roach                    }
1957a6f13a4aSGreg Roach                }
1958a6f13a4aSGreg Roach
195913abd6f3SGreg Roach                $this->list = [];
1960a6f13a4aSGreg Roach
19615985adfbSGreg Roach                foreach ($query->get() 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':
196730fc2b1eSGreg Roach                $query = DB::table('families')
196830fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
196930fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
197030fc2b1eSGreg Roach                    ->distinct();
197130fc2b1eSGreg Roach
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)) {
19770b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
197830fc2b1eSGreg Roach                                $join
197930fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
198030fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
198130fc2b1eSGreg Roach                            });
198230fc2b1eSGreg Roach
198330fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
198430fc2b1eSGreg Roach
1985a6f13a4aSGreg Roach                            $date = new Date($match[3]);
198630fc2b1eSGreg Roach
1987044416d2SGreg Roach                            if ($match[2] === 'LTE') {
198830fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1989a6f13a4aSGreg Roach                            } else {
199030fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1991a6f13a4aSGreg Roach                            }
199230fc2b1eSGreg Roach
199330fc2b1eSGreg Roach                            // This filter has been fully processed
199430fc2b1eSGreg Roach                            unset($attrs[$attr]);
1995a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
199630fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
1997a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
199830fc2b1eSGreg Roach
1999a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
200030fc2b1eSGreg Roach
200130fc2b1eSGreg Roach                            // This filter has been fully processed
200230fc2b1eSGreg Roach                            unset($attrs[$attr]);
200330fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20043b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20050b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200630fc2b1eSGreg Roach                                    $join
200730fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20083b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
200930fc2b1eSGreg Roach                                            $query
201030fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
201130fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
201230fc2b1eSGreg Roach                                        });
201330fc2b1eSGreg Roach                                });
20145d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20157a6ee1acSGreg Roach                                if ($match[1] != '') {
20167a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20175d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2018fbbe964bSGreg Roach                                        $query->whereContains($attr . '.n_full', $name);
20195d0bc43dSGreg Roach                                    }
20205d0bc43dSGreg Roach                                }
2021a6f13a4aSGreg Roach                            }
202230fc2b1eSGreg Roach
202330fc2b1eSGreg Roach                            // This filter has been fully processed
202430fc2b1eSGreg Roach                            unset($attrs[$attr]);
20256e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20269dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
202730fc2b1eSGreg Roach                            $query
20280b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
202930fc2b1eSGreg Roach                                    $join
20306e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20316e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
203230fc2b1eSGreg Roach                                })
20330b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20346e5c0963SGreg Roach                                    $join
20356e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20366e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20376e5c0963SGreg Roach                                })
20386e5c0963SGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
20396e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
204030fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20416e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20426e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20436e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20446e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20456e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20466e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2047e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20486e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2049a6f13a4aSGreg Roach                        }
2050a6f13a4aSGreg Roach                    }
2051a6f13a4aSGreg Roach                }
2052a6f13a4aSGreg Roach
205313abd6f3SGreg Roach                $this->list = [];
2054a6f13a4aSGreg Roach
205530fc2b1eSGreg Roach                foreach ($query->get() as $row) {
2056299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2057a6f13a4aSGreg Roach                }
2058a6f13a4aSGreg Roach                break;
2059a6f13a4aSGreg Roach
2060a6f13a4aSGreg Roach            default:
20616ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2062a6f13a4aSGreg Roach        }
2063a6f13a4aSGreg Roach
206413abd6f3SGreg Roach        $filters  = [];
206513abd6f3SGreg Roach        $filters2 = [];
2066a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2067a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2068a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2069a6f13a4aSGreg Roach                    $condition = $value;
2070a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2071a6f13a4aSGreg Roach                        $id    = $match[1];
2072a6f13a4aSGreg Roach                        $value = "''";
2073044416d2SGreg Roach                        if ($id === 'ID') {
20747a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2075a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2076a6f13a4aSGreg Roach                            }
2077044416d2SGreg Roach                        } elseif ($id === 'fact') {
2078a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2079044416d2SGreg Roach                        } elseif ($id === 'desc') {
2080a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2081a6f13a4aSGreg Roach                        } else {
2082a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20837a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2084a6f13a4aSGreg Roach                            }
2085a6f13a4aSGreg Roach                        }
2086a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2087a6f13a4aSGreg Roach                    }
2088a6f13a4aSGreg Roach                    //-- handle regular expressions
2089a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2090a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2091a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2092a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2093a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2094d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2095a6f13a4aSGreg Roach                            $val = trim($val);
2096a6f13a4aSGreg Roach                        }
2097a6f13a4aSGreg Roach                        if ($val) {
20987a6ee1acSGreg Roach                            $searchstr = '';
20997a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2100a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2101a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2102a6f13a4aSGreg Roach                                $level = 1;
2103a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2104a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2105a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2106a6f13a4aSGreg Roach                                    }
2107a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2108044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21097a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2110a6f13a4aSGreg Roach                                    }
21117a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2112a6f13a4aSGreg Roach                                    $level++;
2113a6f13a4aSGreg Roach                                }
2114a6f13a4aSGreg Roach                            } else {
2115044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21167a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2117a6f13a4aSGreg Roach                                }
2118a6f13a4aSGreg Roach                                $t         = $tag;
21197a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2120a6f13a4aSGreg Roach                            }
2121a6f13a4aSGreg Roach                            switch ($expr) {
21227a6ee1acSGreg Roach                                case 'CONTAINS':
2123044416d2SGreg Roach                                    if ($t === 'PLAC') {
2124a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2125a6f13a4aSGreg Roach                                    } else {
2126a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2127a6f13a4aSGreg Roach                                    }
2128a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2129a6f13a4aSGreg Roach                                    break;
2130a6f13a4aSGreg Roach                                default:
2131c1010edaSGreg Roach                                    $filters2[] = [
2132c1010edaSGreg Roach                                        'tag'  => $tag,
2133c1010edaSGreg Roach                                        'expr' => $expr,
2134c1010edaSGreg Roach                                        'val'  => $val,
2135c1010edaSGreg Roach                                    ];
2136a6f13a4aSGreg Roach                                    break;
2137a6f13a4aSGreg Roach                            }
2138a6f13a4aSGreg Roach                        }
2139a6f13a4aSGreg Roach                    }
2140a6f13a4aSGreg Roach                }
2141a6f13a4aSGreg Roach            }
2142a6f13a4aSGreg Roach        }
2143a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2144a6f13a4aSGreg Roach        if ($filters) {
2145a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2146a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2147299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2148a6f13a4aSGreg Roach                        unset($this->list[$key]);
2149a6f13a4aSGreg Roach                        break;
2150a6f13a4aSGreg Roach                    }
2151a6f13a4aSGreg Roach                }
2152a6f13a4aSGreg Roach            }
2153a6f13a4aSGreg Roach        }
2154a6f13a4aSGreg Roach        if ($filters2) {
215513abd6f3SGreg Roach            $mylist = [];
2156a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2157c0935879SGreg Roach                $key  = $indi->xref();
2158299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2159a6f13a4aSGreg Roach                $keep = true;
2160a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2161a6f13a4aSGreg Roach                    if ($keep) {
2162a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2163a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2164a6f13a4aSGreg Roach                        $val  = $filter['val'];
2165a6f13a4aSGreg Roach                        if ($val == "''") {
21667a6ee1acSGreg Roach                            $val = '';
2167a6f13a4aSGreg Roach                        }
21687a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2169a6f13a4aSGreg Roach                        $t    = end($tags);
21703d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2171a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2172044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21737a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21747a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2175a6f13a4aSGreg Roach                            $t    = end($tags);
21763d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2177a6f13a4aSGreg Roach                        }
2178a6f13a4aSGreg Roach
2179a6f13a4aSGreg Roach                        switch ($expr) {
21807a6ee1acSGreg Roach                            case 'GTE':
2181044416d2SGreg Roach                                if ($t === 'DATE') {
2182a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2183a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2184a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2185a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2186a6f13a4aSGreg Roach                                    $keep = true;
2187a6f13a4aSGreg Roach                                }
2188a6f13a4aSGreg Roach                                break;
21897a6ee1acSGreg Roach                            case 'LTE':
2190044416d2SGreg Roach                                if ($t === 'DATE') {
2191a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2192a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2193a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2194a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2195a6f13a4aSGreg Roach                                    $keep = true;
2196a6f13a4aSGreg Roach                                }
2197a6f13a4aSGreg Roach                                break;
2198a6f13a4aSGreg Roach                            default:
2199a6f13a4aSGreg Roach                                if ($v == $val) {
2200a6f13a4aSGreg Roach                                    $keep = true;
2201a6f13a4aSGreg Roach                                } else {
2202a6f13a4aSGreg Roach                                    $keep = false;
2203a6f13a4aSGreg Roach                                }
2204a6f13a4aSGreg Roach                                break;
2205a6f13a4aSGreg Roach                        }
2206a6f13a4aSGreg Roach                    }
2207a6f13a4aSGreg Roach                }
2208a6f13a4aSGreg Roach                if ($keep) {
2209a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2210a6f13a4aSGreg Roach                }
2211a6f13a4aSGreg Roach            }
2212a6f13a4aSGreg Roach            $this->list = $mylist;
2213a6f13a4aSGreg Roach        }
2214a6f13a4aSGreg Roach
2215a6f13a4aSGreg Roach        switch ($sortby) {
2216a6f13a4aSGreg Roach            case 'NAME':
2217c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2218a6f13a4aSGreg Roach                break;
2219a6f13a4aSGreg Roach            case 'CHAN':
2220c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2221a6f13a4aSGreg Roach                break;
2222a6f13a4aSGreg Roach            case 'BIRT:DATE':
2223c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2224a6f13a4aSGreg Roach                break;
2225a6f13a4aSGreg Roach            case 'DEAT:DATE':
2226c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2227a6f13a4aSGreg Roach                break;
2228a6f13a4aSGreg Roach            case 'MARR:DATE':
2229c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2230a6f13a4aSGreg Roach                break;
2231a6f13a4aSGreg Roach            default:
2232a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2233a6f13a4aSGreg Roach                break;
2234a6f13a4aSGreg Roach        }
2235a6f13a4aSGreg Roach
22369b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2237e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2238a6f13a4aSGreg Roach    }
2239a6f13a4aSGreg Roach
2240a6f13a4aSGreg Roach    /**
224176692c8bSGreg Roach     * XML <List>
22428ba2e626SGreg Roach     *
22438ba2e626SGreg Roach     * @return void
2244a6f13a4aSGreg Roach     */
2245b702978eSGreg Roach    protected function listEndHandler(): void
2246c1010edaSGreg Roach    {
2247a6f13a4aSGreg Roach        $this->process_repeats--;
2248a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2249a6f13a4aSGreg Roach            return;
2250a6f13a4aSGreg Roach        }
2251a6f13a4aSGreg Roach
2252a6f13a4aSGreg Roach        // Check if there is any list
2253a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2254a6f13a4aSGreg Roach            $lineoffset = 0;
2255a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2256a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2257a6f13a4aSGreg Roach            }
2258a6f13a4aSGreg Roach            //-- read the xml from the file
2259299d100dSGreg Roach            $lines = file($this->report);
22607a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2261a6f13a4aSGreg Roach                $lineoffset--;
2262a6f13a4aSGreg Roach            }
2263a6f13a4aSGreg Roach            $lineoffset++;
2264a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2265a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2266a6f13a4aSGreg Roach            // List Level counter
2267a6f13a4aSGreg Roach            $count = 1;
2268a6f13a4aSGreg Roach            while (0 < $count) {
22697a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2270a6f13a4aSGreg Roach                    $count++;
22717a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2272a6f13a4aSGreg Roach                    $count--;
2273a6f13a4aSGreg Roach                }
2274a6f13a4aSGreg Roach                if (0 < $count) {
2275a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2276a6f13a4aSGreg Roach                }
2277a6f13a4aSGreg Roach                $line_nr++;
2278a6f13a4aSGreg Roach            }
2279a6f13a4aSGreg Roach            // No need to drag this
2280a6f13a4aSGreg Roach            unset($lines);
22817a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2282a6f13a4aSGreg Roach            // Save original values
22839b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2284a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2285a6f13a4aSGreg Roach
2286a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2287a6f13a4aSGreg Roach            $this->list_private = 0;
2288a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2289a6f13a4aSGreg Roach                if ($record->canShow()) {
2290f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2291a6f13a4aSGreg Roach                    //-- start the sax parser
2292a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2293e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2294a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22951aa04befSGreg Roach
22961aa04befSGreg Roach                    xml_set_element_handler(
22971aa04befSGreg Roach                        $repeat_parser,
22989d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
22991aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23001aa04befSGreg Roach                        },
23019d454b6bSGreg Roach                        function ($parser, string $name): void {
23021aa04befSGreg Roach                            $this->endElement($parser, $name);
23031aa04befSGreg Roach                        }
23041aa04befSGreg Roach                    );
23051aa04befSGreg Roach
23061aa04befSGreg Roach                    xml_set_character_data_handler(
23071aa04befSGreg Roach                        $repeat_parser,
23089d454b6bSGreg Roach                        function ($parser, string $data): void {
23091aa04befSGreg Roach                            $this->characterData($parser, $data);
23101aa04befSGreg Roach                        }
23111aa04befSGreg Roach                    );
23121aa04befSGreg Roach
2313a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23146ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2315a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2316a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2317a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2318a6f13a4aSGreg Roach                        ));
2319a6f13a4aSGreg Roach                    }
2320a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2321a6f13a4aSGreg Roach                } else {
2322a6f13a4aSGreg Roach                    $this->list_private++;
2323a6f13a4aSGreg Roach                }
2324a6f13a4aSGreg Roach            }
232513abd6f3SGreg Roach            $this->list   = [];
2326e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2327a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2328a6f13a4aSGreg Roach        }
232965e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2330a6f13a4aSGreg Roach    }
2331a6f13a4aSGreg Roach
2332a6f13a4aSGreg Roach    /**
2333a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2334a6f13a4aSGreg Roach     * Prints the total number of records in a list
2335a6f13a4aSGreg Roach     * The total number is collected from
2336a6f13a4aSGreg Roach     * List and Relatives
23378ba2e626SGreg Roach     *
23388ba2e626SGreg Roach     * @return void
2339a6f13a4aSGreg Roach     */
2340b702978eSGreg Roach    protected function listTotalStartHandler(): void
2341c1010edaSGreg Roach    {
2342a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2343589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2344a6f13a4aSGreg Roach        } else {
23457a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2346a6f13a4aSGreg Roach        }
2347a6f13a4aSGreg Roach    }
2348a6f13a4aSGreg Roach
2349a6f13a4aSGreg Roach    /**
235076692c8bSGreg Roach     * XML <Relatives>
235176692c8bSGreg Roach     *
2352c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23538ba2e626SGreg Roach     *
23548ba2e626SGreg Roach     * @return void
2355a6f13a4aSGreg Roach     */
2356b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2357c1010edaSGreg Roach    {
2358a6f13a4aSGreg Roach        $this->process_repeats++;
2359a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2360a6f13a4aSGreg Roach            return;
2361a6f13a4aSGreg Roach        }
2362a6f13a4aSGreg Roach
2363e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2364e364afe4SGreg Roach
236513abd6f3SGreg Roach        $match = [];
2366a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2367d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2368a6f13a4aSGreg Roach            $sortby = trim($sortby);
2369a6f13a4aSGreg Roach        }
2370a6f13a4aSGreg Roach
2371a6f13a4aSGreg Roach        $maxgen = -1;
2372a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2373c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2374a6f13a4aSGreg Roach        }
2375a6f13a4aSGreg Roach
2376e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2377e364afe4SGreg Roach
2378a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2379d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2380a6f13a4aSGreg Roach            $group = trim($group);
2381a6f13a4aSGreg Roach        }
2382a6f13a4aSGreg Roach
2383e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2384e364afe4SGreg Roach
2385a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2386d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2387a6f13a4aSGreg Roach            $id = trim($id);
2388a6f13a4aSGreg Roach        }
2389a6f13a4aSGreg Roach
239013abd6f3SGreg Roach        $this->list = [];
2391299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2392d965cc1aSGreg Roach        if ($person instanceof Individual) {
2393a6f13a4aSGreg Roach            $this->list[$id] = $person;
2394a6f13a4aSGreg Roach            switch ($group) {
23957a6ee1acSGreg Roach                case 'child-family':
239639ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2397820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2398820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2399a6f13a4aSGreg Roach                        }
2400820b62dfSGreg Roach
2401820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2402c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2403a6f13a4aSGreg Roach                        }
2404a6f13a4aSGreg Roach                    }
2405a6f13a4aSGreg Roach                    break;
24067a6ee1acSGreg Roach                case 'spouse-family':
240739ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2408820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2409820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2410a6f13a4aSGreg Roach                        }
2411820b62dfSGreg Roach
2412820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2413c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2414a6f13a4aSGreg Roach                        }
2415a6f13a4aSGreg Roach                    }
2416a6f13a4aSGreg Roach                    break;
24177a6ee1acSGreg Roach                case 'direct-ancestors':
24183d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2419a6f13a4aSGreg Roach                    break;
24207a6ee1acSGreg Roach                case 'ancestors':
24213d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2422a6f13a4aSGreg Roach                    break;
24237a6ee1acSGreg Roach                case 'descendants':
2424a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24253d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2426a6f13a4aSGreg Roach                    break;
24277a6ee1acSGreg Roach                case 'all':
24283d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24293d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2430a6f13a4aSGreg Roach                    break;
2431a6f13a4aSGreg Roach            }
2432a6f13a4aSGreg Roach        }
2433a6f13a4aSGreg Roach
2434a6f13a4aSGreg Roach        switch ($sortby) {
2435a6f13a4aSGreg Roach            case 'NAME':
2436c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2437a6f13a4aSGreg Roach                break;
2438a6f13a4aSGreg Roach            case 'BIRT:DATE':
2439c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2440a6f13a4aSGreg Roach                break;
2441a6f13a4aSGreg Roach            case 'DEAT:DATE':
2442c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2443a6f13a4aSGreg Roach                break;
2444a6f13a4aSGreg Roach            case 'generation':
244513abd6f3SGreg Roach                $newarray = [];
2446a6f13a4aSGreg Roach                reset($this->list);
2447a6f13a4aSGreg Roach                $genCounter = 1;
2448a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2449a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2450a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2451a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
245279529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2453a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2454a6f13a4aSGreg Roach                        }
2455a6f13a4aSGreg Roach                    }
2456a6f13a4aSGreg Roach                    $genCounter++;
2457a6f13a4aSGreg Roach                }
2458a6f13a4aSGreg Roach                $this->list = $newarray;
2459a6f13a4aSGreg Roach                break;
2460a6f13a4aSGreg Roach            default:
2461a6f13a4aSGreg Roach                // unsorted
2462a6f13a4aSGreg Roach                break;
2463a6f13a4aSGreg Roach        }
24649b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2465e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2466a6f13a4aSGreg Roach    }
2467a6f13a4aSGreg Roach
2468a6f13a4aSGreg Roach    /**
246976692c8bSGreg Roach     * XML </ Relatives>
24708ba2e626SGreg Roach     *
24718ba2e626SGreg Roach     * @return void
2472a6f13a4aSGreg Roach     */
2473b702978eSGreg Roach    protected function relativesEndHandler(): void
2474c1010edaSGreg Roach    {
2475a6f13a4aSGreg Roach        $this->process_repeats--;
2476a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2477a6f13a4aSGreg Roach            return;
2478a6f13a4aSGreg Roach        }
2479a6f13a4aSGreg Roach
2480a6f13a4aSGreg Roach        // Check if there is any relatives
2481a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2482a6f13a4aSGreg Roach            $lineoffset = 0;
2483a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2484a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2485a6f13a4aSGreg Roach            }
2486a6f13a4aSGreg Roach            //-- read the xml from the file
2487299d100dSGreg Roach            $lines = file($this->report);
24887a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2489a6f13a4aSGreg Roach                $lineoffset--;
2490a6f13a4aSGreg Roach            }
2491a6f13a4aSGreg Roach            $lineoffset++;
2492a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2493a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2494a6f13a4aSGreg Roach            // Relatives Level counter
2495a6f13a4aSGreg Roach            $count = 1;
2496a6f13a4aSGreg Roach            while (0 < $count) {
24977a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2498a6f13a4aSGreg Roach                    $count++;
24997a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2500a6f13a4aSGreg Roach                    $count--;
2501a6f13a4aSGreg Roach                }
2502a6f13a4aSGreg Roach                if (0 < $count) {
2503a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2504a6f13a4aSGreg Roach                }
2505a6f13a4aSGreg Roach                $line_nr++;
2506a6f13a4aSGreg Roach            }
2507a6f13a4aSGreg Roach            // No need to drag this
2508a6f13a4aSGreg Roach            unset($lines);
2509a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2510a6f13a4aSGreg Roach            // Save original values
25119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2512a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2513a6f13a4aSGreg Roach
2514a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2515a6f13a4aSGreg Roach            $this->list_private = 0;
2516b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2517a6f13a4aSGreg Roach                if (isset($value->generation)) {
2518a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2519a6f13a4aSGreg Roach                }
2520b092a991SGreg Roach                $tmp          = GedcomRecord::getInstance((string) $xref, $this->tree);
2521299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2522a6f13a4aSGreg Roach
2523a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2524e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2525a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25261aa04befSGreg Roach
25271aa04befSGreg Roach                xml_set_element_handler(
25281aa04befSGreg Roach                    $repeat_parser,
25299d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25301aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25311aa04befSGreg Roach                    },
25329d454b6bSGreg Roach                    function ($parser, string $name): void {
25331aa04befSGreg Roach                        $this->endElement($parser, $name);
25341aa04befSGreg Roach                    }
25351aa04befSGreg Roach                );
25361aa04befSGreg Roach
25371aa04befSGreg Roach                xml_set_character_data_handler(
25381aa04befSGreg Roach                    $repeat_parser,
25399d454b6bSGreg Roach                    function ($parser, string $data): void {
25401aa04befSGreg Roach                        $this->characterData($parser, $data);
25411aa04befSGreg Roach                    }
25421aa04befSGreg Roach                );
2543a6f13a4aSGreg Roach
2544a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25456ccdf4f0SGreg Roach                    throw new DomainException(sprintf('RelativesEHandler XML error: %s at line %d', xml_error_string(xml_get_error_code($repeat_parser)), xml_get_current_line_number($repeat_parser)));
2546a6f13a4aSGreg Roach                }
2547a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2548a6f13a4aSGreg Roach            }
2549a6f13a4aSGreg Roach            // Clean up the list array
255013abd6f3SGreg Roach            $this->list   = [];
2551e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2552a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2553a6f13a4aSGreg Roach        }
255465e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2555a6f13a4aSGreg Roach    }
2556a6f13a4aSGreg Roach
2557a6f13a4aSGreg Roach    /**
2558a6f13a4aSGreg Roach     * XML <Generation /> element handler
2559a6f13a4aSGreg Roach     * Prints the number of generations
25608ba2e626SGreg Roach     *
25618ba2e626SGreg Roach     * @return void
2562a6f13a4aSGreg Roach     */
2563b702978eSGreg Roach    protected function generationStartHandler(): void
2564c1010edaSGreg Roach    {
2565589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2566a6f13a4aSGreg Roach    }
2567a6f13a4aSGreg Roach
2568a6f13a4aSGreg Roach    /**
2569a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2570a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25718ba2e626SGreg Roach     *
25728ba2e626SGreg Roach     * @return void
2573a6f13a4aSGreg Roach     */
2574b702978eSGreg Roach    protected function newPageStartHandler(): void
2575c1010edaSGreg Roach    {
25767a6ee1acSGreg Roach        $temp = 'addpage';
2577e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2578a6f13a4aSGreg Roach    }
2579a6f13a4aSGreg Roach
2580a6f13a4aSGreg Roach    /**
258176692c8bSGreg Roach     * XML <html>
258276692c8bSGreg Roach     *
2583a6f13a4aSGreg Roach     * @param string   $tag   HTML tag name
2584c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
25858ba2e626SGreg Roach     *
25868ba2e626SGreg Roach     * @return void
2587a6f13a4aSGreg Roach     */
2588b702978eSGreg Roach    protected function htmlStartHandler(string $tag, array $attrs): void
2589c1010edaSGreg Roach    {
25907a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2591a6f13a4aSGreg Roach            return;
2592a6f13a4aSGreg Roach        }
25939b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2594e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2595e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2596a6f13a4aSGreg Roach
25979b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2598a6f13a4aSGreg Roach        $this->print_data         = true;
2599a6f13a4aSGreg Roach    }
2600a6f13a4aSGreg Roach
2601a6f13a4aSGreg Roach    /**
260276692c8bSGreg Roach     * XML </html>
260376692c8bSGreg Roach     *
2604a6f13a4aSGreg Roach     * @param string $tag
26058ba2e626SGreg Roach     *
26068ba2e626SGreg Roach     * @return void
2607a6f13a4aSGreg Roach     */
2608b702978eSGreg Roach    protected function htmlEndHandler($tag): void
2609c1010edaSGreg Roach    {
26107a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2611a6f13a4aSGreg Roach            return;
2612a6f13a4aSGreg Roach        }
2613a6f13a4aSGreg Roach
2614a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2615e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2616e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
26178f038c36SRico Sonntag        if ($this->wt_report !== null) {
2618e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2619a6f13a4aSGreg Roach        } else {
2620e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2621a6f13a4aSGreg Roach        }
2622a6f13a4aSGreg Roach    }
2623a6f13a4aSGreg Roach
2624a6f13a4aSGreg Roach    /**
2625a6f13a4aSGreg Roach     * Handle <Input>
26268ba2e626SGreg Roach     *
26278ba2e626SGreg Roach     * @return void
2628a6f13a4aSGreg Roach     */
2629b702978eSGreg Roach    protected function inputStartHandler(): void
2630c1010edaSGreg Roach    {
2631a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2632a6f13a4aSGreg Roach    }
2633a6f13a4aSGreg Roach
2634a6f13a4aSGreg Roach    /**
2635a6f13a4aSGreg Roach     * Handle </Input>
26368ba2e626SGreg Roach     *
26378ba2e626SGreg Roach     * @return void
2638a6f13a4aSGreg Roach     */
2639b702978eSGreg Roach    protected function inputEndHandler(): void
2640c1010edaSGreg Roach    {
2641a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2642a6f13a4aSGreg Roach    }
2643a6f13a4aSGreg Roach
2644a6f13a4aSGreg Roach    /**
2645a6f13a4aSGreg Roach     * Handle <Report>
26468ba2e626SGreg Roach     *
26478ba2e626SGreg Roach     * @return void
2648a6f13a4aSGreg Roach     */
2649b702978eSGreg Roach    protected function reportStartHandler(): void
2650c1010edaSGreg Roach    {
2651a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2652a6f13a4aSGreg Roach    }
2653a6f13a4aSGreg Roach
2654a6f13a4aSGreg Roach    /**
2655a6f13a4aSGreg Roach     * Handle </Report>
26568ba2e626SGreg Roach     *
26578ba2e626SGreg Roach     * @return void
2658a6f13a4aSGreg Roach     */
2659b702978eSGreg Roach    protected function reportEndHandler(): void
2660c1010edaSGreg Roach    {
2661a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2662a6f13a4aSGreg Roach    }
2663a6f13a4aSGreg Roach
2664a6f13a4aSGreg Roach    /**
266576692c8bSGreg Roach     * XML </titleEndHandler>
26668ba2e626SGreg Roach     *
26678ba2e626SGreg Roach     * @return void
2668a6f13a4aSGreg Roach     */
2669b702978eSGreg Roach    protected function titleEndHandler(): void
2670c1010edaSGreg Roach    {
26712836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2672a6f13a4aSGreg Roach    }
2673a6f13a4aSGreg Roach
2674a6f13a4aSGreg Roach    /**
267576692c8bSGreg Roach     * XML </descriptionEndHandler>
26768ba2e626SGreg Roach     *
26778ba2e626SGreg Roach     * @return void
2678a6f13a4aSGreg Roach     */
2679b702978eSGreg Roach    protected function descriptionEndHandler(): void
2680c1010edaSGreg Roach    {
26812836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2682a6f13a4aSGreg Roach    }
2683729ce104SGreg Roach
2684729ce104SGreg Roach    /**
268576692c8bSGreg Roach     * Create a list of all descendants.
268676692c8bSGreg Roach     *
2687729ce104SGreg Roach     * @param string[] $list
2688729ce104SGreg Roach     * @param string   $pid
2689729ce104SGreg Roach     * @param bool     $parents
2690729ce104SGreg Roach     * @param int      $generations
26918ba2e626SGreg Roach     *
26928ba2e626SGreg Roach     * @return void
2693729ce104SGreg Roach     */
26943b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2695c1010edaSGreg Roach    {
2696299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2697729ce104SGreg Roach        if ($person === null) {
2698729ce104SGreg Roach            return;
2699729ce104SGreg Roach        }
2700729ce104SGreg Roach        if (!isset($list[$pid])) {
2701729ce104SGreg Roach            $list[$pid] = $person;
2702729ce104SGreg Roach        }
2703729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2704729ce104SGreg Roach            $list[$pid]->generation = 0;
2705729ce104SGreg Roach        }
270639ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2707729ce104SGreg Roach            if ($parents) {
270839ca88baSGreg Roach                $husband = $family->husband();
270939ca88baSGreg Roach                $wife    = $family->wife();
2710729ce104SGreg Roach                if ($husband) {
2711c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2712729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2713c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2714729ce104SGreg Roach                    } else {
2715c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2716729ce104SGreg Roach                    }
2717729ce104SGreg Roach                }
2718729ce104SGreg Roach                if ($wife) {
2719c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2720729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2721c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2722729ce104SGreg Roach                    } else {
2723c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2724729ce104SGreg Roach                    }
2725729ce104SGreg Roach                }
2726729ce104SGreg Roach            }
2727820b62dfSGreg Roach
272839ca88baSGreg Roach            $children = $family->children();
2729820b62dfSGreg Roach
2730729ce104SGreg Roach            foreach ($children as $child) {
2731729ce104SGreg Roach                if ($child) {
2732c0935879SGreg Roach                    $list[$child->xref()] = $child;
2733820b62dfSGreg Roach
2734729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2735c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2736729ce104SGreg Roach                    } else {
2737c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2738729ce104SGreg Roach                    }
2739729ce104SGreg Roach                }
2740729ce104SGreg Roach            }
2741729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2742729ce104SGreg Roach                foreach ($children as $child) {
2743c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2744729ce104SGreg Roach                }
2745729ce104SGreg Roach            }
2746729ce104SGreg Roach        }
2747729ce104SGreg Roach    }
2748729ce104SGreg Roach
2749729ce104SGreg Roach    /**
275076692c8bSGreg Roach     * Create a list of all ancestors.
275176692c8bSGreg Roach     *
2752729ce104SGreg Roach     * @param string[] $list
2753729ce104SGreg Roach     * @param string   $pid
2754729ce104SGreg Roach     * @param bool     $children
2755729ce104SGreg Roach     * @param int      $generations
27568ba2e626SGreg Roach     *
27578ba2e626SGreg Roach     * @return void
2758729ce104SGreg Roach     */
27593b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2760c1010edaSGreg Roach    {
276113abd6f3SGreg Roach        $genlist                = [$pid];
2762729ce104SGreg Roach        $list[$pid]->generation = 1;
2763729ce104SGreg Roach        while (count($genlist) > 0) {
2764729ce104SGreg Roach            $id = array_shift($genlist);
2765729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2766729ce104SGreg Roach                continue; // id can be something like “empty7”
2767729ce104SGreg Roach            }
2768299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
276939ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
277039ca88baSGreg Roach                $husband = $family->husband();
277139ca88baSGreg Roach                $wife    = $family->wife();
2772729ce104SGreg Roach                if ($husband) {
2773c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2774c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2775729ce104SGreg Roach                }
2776729ce104SGreg Roach                if ($wife) {
2777c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2778c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2779729ce104SGreg Roach                }
2780729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2781729ce104SGreg Roach                    if ($husband) {
2782c0935879SGreg Roach                        $genlist[] = $husband->xref();
2783729ce104SGreg Roach                    }
2784729ce104SGreg Roach                    if ($wife) {
2785c0935879SGreg Roach                        $genlist[] = $wife->xref();
2786729ce104SGreg Roach                    }
2787729ce104SGreg Roach                }
2788729ce104SGreg Roach                if ($children) {
278939ca88baSGreg Roach                    foreach ($family->children() as $child) {
2790c0935879SGreg Roach                        $list[$child->xref()] = $child;
2791e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2792729ce104SGreg Roach                    }
2793729ce104SGreg Roach                }
2794729ce104SGreg Roach            }
2795729ce104SGreg Roach        }
2796729ce104SGreg Roach    }
2797729ce104SGreg Roach
2798729ce104SGreg Roach    /**
2799729ce104SGreg Roach     * get gedcom tag value
2800729ce104SGreg Roach     *
2801729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2802729ce104SGreg 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
2803729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2804729ce104SGreg Roach     *
2805729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2806729ce104SGreg Roach     */
28078f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2808c1010edaSGreg Roach    {
2809729ce104SGreg Roach        if (empty($gedrec)) {
2810729ce104SGreg Roach            return '';
2811729ce104SGreg Roach        }
2812729ce104SGreg Roach        $tags      = explode(':', $tag);
2813729ce104SGreg Roach        $origlevel = $level;
2814729ce104SGreg Roach        if ($level == 0) {
28153c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2816729ce104SGreg Roach        }
2817729ce104SGreg Roach
2818729ce104SGreg Roach        $subrec = $gedrec;
2819729ce104SGreg Roach        foreach ($tags as $t) {
2820729ce104SGreg Roach            $lastsubrec = $subrec;
28213d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2822729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2823729ce104SGreg Roach                $level--;
28243d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2825729ce104SGreg Roach            }
2826729ce104SGreg Roach            if (empty($subrec)) {
2827044416d2SGreg Roach                if ($t === 'TITL') {
28283d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2829729ce104SGreg Roach                    if (!empty($subrec)) {
28307a6ee1acSGreg Roach                        $t = 'ABBR';
2831729ce104SGreg Roach                    }
2832729ce104SGreg Roach                }
2833729ce104SGreg Roach                if (empty($subrec)) {
2834729ce104SGreg Roach                    if ($level > 0) {
2835729ce104SGreg Roach                        $level--;
2836729ce104SGreg Roach                    }
28373d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2838729ce104SGreg Roach                    if (empty($subrec)) {
2839729ce104SGreg Roach                        return '';
2840729ce104SGreg Roach                    }
2841729ce104SGreg Roach                }
2842729ce104SGreg Roach            }
2843729ce104SGreg Roach            $level++;
2844729ce104SGreg Roach        }
2845729ce104SGreg Roach        $level--;
2846729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2847729ce104SGreg Roach        if ($ct == 0) {
2848729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2849729ce104SGreg Roach        }
2850729ce104SGreg Roach        if ($ct == 0) {
2851729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2852729ce104SGreg Roach        }
2853729ce104SGreg Roach        if ($ct > 0) {
2854729ce104SGreg Roach            $value = trim($match[1]);
2855044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2856299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2857ff166e64SGreg Roach                if ($note instanceof Note) {
2858729ce104SGreg Roach                    $value = $note->getNote();
2859729ce104SGreg Roach                } else {
2860729ce104SGreg Roach                    //-- set the value to the id without the @
2861729ce104SGreg Roach                    $value = $match[1];
2862729ce104SGreg Roach                }
2863729ce104SGreg Roach            }
28647a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28653d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2866729ce104SGreg Roach            }
2867729ce104SGreg Roach
2868729ce104SGreg Roach            return $value;
2869729ce104SGreg Roach        }
2870729ce104SGreg Roach
28717a6ee1acSGreg Roach        return '';
2872729ce104SGreg Roach    }
2873d1286247SGreg Roach
2874d1286247SGreg Roach    /**
2875d1286247SGreg Roach     * Replace variable identifiers with their values.
2876d1286247SGreg Roach     *
2877d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
287882759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2879d1286247SGreg Roach     *
2880d1286247SGreg Roach     * @return string
2881d1286247SGreg Roach     */
28828f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2883c1010edaSGreg Roach    {
2884d1286247SGreg Roach        return preg_replace_callback(
2885d1286247SGreg Roach            '/\$(\w+)/',
288618d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28872118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
288882759250SGreg Roach                    if ($quote) {
28892118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2890b2ce94c6SRico Sonntag                    }
2891b2ce94c6SRico Sonntag
28922118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
289382759250SGreg Roach                }
2894b2ce94c6SRico Sonntag
2895d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28963d7a8a4cSGreg Roach
2897d1286247SGreg Roach                return '$' . $matches[1];
2898d1286247SGreg Roach            },
2899d1286247SGreg Roach            $expression
2900d1286247SGreg Roach        );
2901d1286247SGreg Roach    }
2902a6f13a4aSGreg Roach}
2903