xref: /webtrees/app/Report/ReportParserGenerate.php (revision 4e69366a139deb61a3f41ad48d0068262443eb94)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6a6f13a4aSGreg Roach * This program is free software: you can redistribute it and/or modify
7a6f13a4aSGreg Roach * it under the terms of the GNU General Public License as published by
8a6f13a4aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a6f13a4aSGreg Roach * (at your option) any later version.
10a6f13a4aSGreg Roach * This program is distributed in the hope that it will be useful,
11a6f13a4aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a6f13a4aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a6f13a4aSGreg Roach * GNU General Public License for more details.
14a6f13a4aSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a6f13a4aSGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
226ccdf4f0SGreg Roachuse DomainException;
23a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
244459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
25a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
2697def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
283d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
299a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
33d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
34a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
35729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
36a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
37d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
38299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
39195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
40195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
41a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
425985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
439a9e551aSGreg Roachuse Illuminate\Support\Str;
44f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
4531d9777aSGreg Roachuse LogicException;
4679529c87SGreg Roachuse stdClass;
47c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
485809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
49*4e69366aSGreg Roachuse XMLParser;
5059597b37SGreg Roach
51b6f35a76SGreg Roachuse function addcslashes;
52b6f35a76SGreg Roachuse function addslashes;
53b6f35a76SGreg Roachuse function array_pop;
54b6f35a76SGreg Roachuse function array_shift;
55b6f35a76SGreg Roachuse function assert;
56b19e047dSGreg Roachuse function call_user_func;
57b6f35a76SGreg Roachuse function count;
58b6f35a76SGreg Roachuse function end;
59b2448a1bSGreg Roachuse function explode;
60b6f35a76SGreg Roachuse function file;
61b6f35a76SGreg Roachuse function file_exists;
62b6f35a76SGreg Roachuse function getimagesize;
6329518ad2SGreg Roachuse function imagecreatefromstring;
6429518ad2SGreg Roachuse function imagesx;
6529518ad2SGreg Roachuse function imagesy;
66b6f35a76SGreg Roachuse function in_array;
67b19e047dSGreg Roachuse function method_exists;
68b6f35a76SGreg Roachuse function preg_match;
69b6f35a76SGreg Roachuse function preg_match_all;
70b6f35a76SGreg Roachuse function preg_replace;
71b6f35a76SGreg Roachuse function preg_replace_callback;
72b6f35a76SGreg Roachuse function preg_split;
73b6f35a76SGreg Roachuse function reset;
74b6f35a76SGreg Roachuse function round;
75b6f35a76SGreg Roachuse function sprintf;
76dec352c1SGreg Roachuse function str_contains;
77b6f35a76SGreg Roachuse function str_replace;
78dec352c1SGreg Roachuse function str_starts_with;
79b6f35a76SGreg Roachuse function strip_tags;
80b6f35a76SGreg Roachuse function strlen;
81b6f35a76SGreg Roachuse function strtoupper;
82b6f35a76SGreg Roachuse function substr;
83b2448a1bSGreg Roachuse function trim;
84b6f35a76SGreg Roachuse function uasort;
85b6f35a76SGreg Roachuse function xml_error_string;
86b6f35a76SGreg Roachuse function xml_get_current_line_number;
87b6f35a76SGreg Roachuse function xml_get_error_code;
88b6f35a76SGreg Roachuse function xml_parse;
89b6f35a76SGreg Roachuse function xml_parser_create;
90b6f35a76SGreg Roachuse function xml_parser_free;
91b6f35a76SGreg Roachuse function xml_parser_set_option;
92b6f35a76SGreg Roachuse function xml_set_character_data_handler;
93b6f35a76SGreg Roachuse function xml_set_element_handler;
94b6f35a76SGreg Roach
95b6f35a76SGreg Roachuse const PREG_SET_ORDER;
96b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9729518ad2SGreg Roach
98a6f13a4aSGreg Roach/**
99a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
100a6f13a4aSGreg Roach */
101c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
102c1010edaSGreg Roach{
103a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
104a6f13a4aSGreg Roach    private $process_footnote = true;
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
107a6f13a4aSGreg Roach    private $print_data = false;
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
11013abd6f3SGreg Roach    private $print_data_stack = [];
111a6f13a4aSGreg Roach
11276692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
113a6f13a4aSGreg Roach    private $process_gedcoms = 0;
114a6f13a4aSGreg Roach
11576692c8bSGreg Roach    /** @var int Are we processing conditionals */
116a6f13a4aSGreg Roach    private $process_ifs = 0;
117a6f13a4aSGreg Roach
11876692c8bSGreg Roach    /** @var int Are we processing repeats */
119a6f13a4aSGreg Roach    private $process_repeats = 0;
120a6f13a4aSGreg Roach
121a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
122a6f13a4aSGreg Roach    private $repeat_bytes = 0;
123a6f13a4aSGreg Roach
1245b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
12513abd6f3SGreg Roach    private $repeats = [];
126a6f13a4aSGreg Roach
127a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
12813abd6f3SGreg Roach    private $repeats_stack = [];
129a6f13a4aSGreg Roach
130b6f35a76SGreg Roach    /** @var AbstractRenderer[] Nested repeating data */
13113abd6f3SGreg Roach    private $wt_report_stack = [];
132e8e7866bSGreg Roach
133*4e69366aSGreg Roach    /** @var XMLParser (resource before PHP 8.0) Nested repeating data */
134e8e7866bSGreg Roach    private $parser;
135e8e7866bSGreg Roach
136*4e69366aSGreg Roach    /** @var XMLParser[] (resource[] before PHP 8.0) Nested repeating data */
137*4e69366aSGreg Roach    private array $parser_stack = [];
138e8e7866bSGreg Roach
139a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
140a6f13a4aSGreg Roach    private $gedrec = '';
141a6f13a4aSGreg Roach
142a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
143*4e69366aSGreg Roach    private array $gedrec_stack = [];
144a6f13a4aSGreg Roach
145a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
146a6f13a4aSGreg Roach    private $current_element;
147a6f13a4aSGreg Roach
148a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
149a6f13a4aSGreg Roach    private $footnote_element;
150a6f13a4aSGreg Roach
151a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
152a6f13a4aSGreg Roach    private $fact = '';
153a6f13a4aSGreg Roach
154a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
155a6f13a4aSGreg Roach    private $desc = '';
156a6f13a4aSGreg Roach
157a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
158a6f13a4aSGreg Roach    private $type = '';
159a6f13a4aSGreg Roach
160a6f13a4aSGreg Roach    /** @var int The current generational level */
161a6f13a4aSGreg Roach    private $generation = 1;
162a6f13a4aSGreg Roach
163a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
164*4e69366aSGreg Roach    private array $list = [];
165a6f13a4aSGreg Roach
166a6f13a4aSGreg Roach    /** @var int Number of items in lists */
167a6f13a4aSGreg Roach    private $list_total = 0;
168a6f13a4aSGreg Roach
169a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
170a6f13a4aSGreg Roach    private $list_private = 0;
171a6f13a4aSGreg Roach
172299d100dSGreg Roach    /** @var string The filename of the XML report */
173299d100dSGreg Roach    protected $report;
174299d100dSGreg Roach
175b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
176e8e7866bSGreg Roach    private $report_root;
177e8e7866bSGreg Roach
178b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
179e8e7866bSGreg Roach    private $wt_report;
180e8e7866bSGreg Roach
181d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
182*4e69366aSGreg Roach    private array $vars;
183d1286247SGreg Roach
184*4e69366aSGreg Roach    private Tree $tree;
185299d100dSGreg Roach
186*4e69366aSGreg Roach    private FilesystemOperator $data_filesystem;
187a04bb9a2SGreg Roach
18876692c8bSGreg Roach    /**
18976692c8bSGreg Roach     * Create a parser for a report
19076692c8bSGreg Roach     *
19176692c8bSGreg Roach     * @param string             $report The XML filename
192b6f35a76SGreg Roach     * @param AbstractRenderer   $report_root
19376692c8bSGreg Roach     * @param string[][]         $vars
194299d100dSGreg Roach     * @param Tree               $tree
195f7cf8a15SGreg Roach     * @param FilesystemOperator $data_filesystem
19676692c8bSGreg Roach     */
197a04bb9a2SGreg Roach    public function __construct(
198a04bb9a2SGreg Roach        string $report,
199b6f35a76SGreg Roach        AbstractRenderer $report_root,
200a04bb9a2SGreg Roach        array $vars,
201a04bb9a2SGreg Roach        Tree $tree,
202f7cf8a15SGreg Roach        FilesystemOperator $data_filesystem
203a04bb9a2SGreg Roach    ) {
204299d100dSGreg Roach        $this->report          = $report;
205e8e7866bSGreg Roach        $this->report_root     = $report_root;
206e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20759f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
208d1286247SGreg Roach        $this->vars            = $vars;
209299d100dSGreg Roach        $this->tree            = $tree;
210a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
211299d100dSGreg Roach
21276f666f4SGreg Roach        parent::__construct($report);
213a6f13a4aSGreg Roach    }
214a6f13a4aSGreg Roach
215a6f13a4aSGreg Roach    /**
216a6f13a4aSGreg Roach     * XML start element handler
217a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
218a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
219a6f13a4aSGreg Roach     *
220a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
221a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
2228a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
22318d7a90dSGreg Roach     *
22418d7a90dSGreg Roach     * @return void
225a6f13a4aSGreg Roach     */
226af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
227c1010edaSGreg Roach    {
22813abd6f3SGreg Roach        $newattrs = [];
229a6f13a4aSGreg Roach
230a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
231a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
232e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
233d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
234a6f13a4aSGreg Roach                }
235a6f13a4aSGreg Roach            }
236a6f13a4aSGreg Roach            $newattrs[$key] = $value;
237a6f13a4aSGreg Roach        }
238a6f13a4aSGreg Roach        $attrs = $newattrs;
2397a6ee1acSGreg 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')) {
240a0e2939aSGreg Roach            $method = $name . 'StartHandler';
241208e9f76SGreg Roach
242a0e2939aSGreg Roach            if (method_exists($this, $method)) {
243b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
244a6f13a4aSGreg Roach            }
245a6f13a4aSGreg Roach        }
246a6f13a4aSGreg Roach    }
247a6f13a4aSGreg Roach
248a6f13a4aSGreg Roach    /**
249a6f13a4aSGreg Roach     * XML end element handler
250a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
251a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
252a6f13a4aSGreg Roach     *
253a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
254a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
25518d7a90dSGreg Roach     *
25618d7a90dSGreg Roach     * @return void
257a6f13a4aSGreg Roach     */
258af14d238SGreg Roach    protected function endElement($parser, string $name): void
259c1010edaSGreg Roach    {
2607a6ee1acSGreg 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')) {
261a0e2939aSGreg Roach            $method = $name . 'EndHandler';
262a0e2939aSGreg Roach
263a0e2939aSGreg Roach            if (method_exists($this, $method)) {
264b19e047dSGreg Roach                call_user_func([$this, $method]);
265a6f13a4aSGreg Roach            }
266a6f13a4aSGreg Roach        }
267a6f13a4aSGreg Roach    }
268a6f13a4aSGreg Roach
269a6f13a4aSGreg Roach    /**
270a6f13a4aSGreg Roach     * XML character data handler
271a6f13a4aSGreg Roach     *
272a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
273a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
27418d7a90dSGreg Roach     *
27518d7a90dSGreg Roach     * @return void
276a6f13a4aSGreg Roach     */
27724f2a3afSGreg Roach    protected function characterData($parser, string $data): void
278c1010edaSGreg Roach    {
279e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
280a6f13a4aSGreg Roach            $this->current_element->addText($data);
281a6f13a4aSGreg Roach        }
282a6f13a4aSGreg Roach    }
283a6f13a4aSGreg Roach
284a6f13a4aSGreg Roach    /**
285fab8f067SGreg Roach     * Handle <style>
286a6f13a4aSGreg Roach     *
287fab8f067SGreg Roach     * @param string[] $attrs
2888ba2e626SGreg Roach     *
2898ba2e626SGreg Roach     * @return void
290a6f13a4aSGreg Roach     */
291b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
292c1010edaSGreg Roach    {
293a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2946ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
295a6f13a4aSGreg Roach        }
296a6f13a4aSGreg Roach
297a6f13a4aSGreg Roach        // array Style that will be passed on
29813abd6f3SGreg Roach        $s = [];
299a6f13a4aSGreg Roach
300a6f13a4aSGreg Roach        // string Name af the style
301a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
302a6f13a4aSGreg Roach
303a6f13a4aSGreg Roach        // string Name of the DEFAULT font
304208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
305a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
306a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
307a6f13a4aSGreg Roach        }
308a6f13a4aSGreg Roach
309a6f13a4aSGreg Roach        // int The size of the font in points
310208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
311a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
31259597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
31359597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
31459597b37SGreg Roach        }
315a6f13a4aSGreg Roach
316a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3177a6ee1acSGreg Roach        $s['style'] = '';
318a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
319a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
320a6f13a4aSGreg Roach        }
321a6f13a4aSGreg Roach
322e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
323a6f13a4aSGreg Roach    }
324a6f13a4aSGreg Roach
325a6f13a4aSGreg Roach    /**
326fab8f067SGreg Roach     * Handle <doc>
327a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
328a6f13a4aSGreg Roach     *
329fab8f067SGreg Roach     * @param string[] $attrs
3308ba2e626SGreg Roach     *
3318ba2e626SGreg Roach     * @return void
332a6f13a4aSGreg Roach     */
333b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
334c1010edaSGreg Roach    {
335e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
336a6f13a4aSGreg Roach
337a6f13a4aSGreg Roach        // Custom page width
338a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
339208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
340a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
341a6f13a4aSGreg Roach        // Custom Page height
342a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
343208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
344a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
345a6f13a4aSGreg Roach
346a6f13a4aSGreg Roach        // Left Margin
347a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
3487a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
349208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
350a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
351208e9f76SGreg 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))
352a6f13a4aSGreg Roach            }
353a6f13a4aSGreg Roach        }
354a6f13a4aSGreg Roach        // Right Margin
355a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3567a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
357208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
358a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
359208e9f76SGreg 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))
360a6f13a4aSGreg Roach            }
361a6f13a4aSGreg Roach        }
362a6f13a4aSGreg Roach        // Top Margin
363a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3647a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
365208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
366a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
367208e9f76SGreg 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))
368a6f13a4aSGreg Roach            }
369a6f13a4aSGreg Roach        }
370a6f13a4aSGreg Roach        // Bottom Margin
371a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3727a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
373208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
374a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
375208e9f76SGreg 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))
376a6f13a4aSGreg Roach            }
377a6f13a4aSGreg Roach        }
378a6f13a4aSGreg Roach        // Header Margin
379a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3807a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
381208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
382a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
383208e9f76SGreg 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))
384a6f13a4aSGreg Roach            }
385a6f13a4aSGreg Roach        }
386a6f13a4aSGreg Roach        // Footer Margin
387a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3887a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
389208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
390a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
391208e9f76SGreg 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))
392a6f13a4aSGreg Roach            }
393a6f13a4aSGreg Roach        }
394a6f13a4aSGreg Roach
395a6f13a4aSGreg Roach        // Page Orientation
396a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
397044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3987a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
399044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4007a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
401a6f13a4aSGreg Roach            }
402a6f13a4aSGreg Roach        }
403a6f13a4aSGreg Roach        // Page Size
404a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
405208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
406a6f13a4aSGreg Roach        }
407a6f13a4aSGreg Roach
408a6f13a4aSGreg Roach        // Show Generated By...
409a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4107a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
411208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4127a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
413208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
414a6f13a4aSGreg Roach            }
415a6f13a4aSGreg Roach        }
416a6f13a4aSGreg Roach
417e8e7866bSGreg Roach        $this->wt_report->setup();
418a6f13a4aSGreg Roach    }
419a6f13a4aSGreg Roach
420a6f13a4aSGreg Roach    /**
421fab8f067SGreg Roach     * Handle </doc>
4228ba2e626SGreg Roach     *
4238ba2e626SGreg Roach     * @return void
424a6f13a4aSGreg Roach     */
425b702978eSGreg Roach    protected function docEndHandler(): void
426c1010edaSGreg Roach    {
427e8e7866bSGreg Roach        $this->wt_report->run();
428a6f13a4aSGreg Roach    }
429a6f13a4aSGreg Roach
430a6f13a4aSGreg Roach    /**
431fab8f067SGreg Roach     * Handle <header>
4328ba2e626SGreg Roach     *
4338ba2e626SGreg Roach     * @return void
434a6f13a4aSGreg Roach     */
435b702978eSGreg Roach    protected function headerStartHandler(): void
436c1010edaSGreg Roach    {
437a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
438e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4397a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
440a6f13a4aSGreg Roach    }
441a6f13a4aSGreg Roach
442a6f13a4aSGreg Roach    /**
443fab8f067SGreg Roach     * Handle <body>
4448ba2e626SGreg Roach     *
4458ba2e626SGreg Roach     * @return void
446a6f13a4aSGreg Roach     */
447b702978eSGreg Roach    protected function bodyStartHandler(): void
448c1010edaSGreg Roach    {
4497a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
450a6f13a4aSGreg Roach    }
451a6f13a4aSGreg Roach
452a6f13a4aSGreg Roach    /**
453fab8f067SGreg Roach     * Handle <footer>
4548ba2e626SGreg Roach     *
4558ba2e626SGreg Roach     * @return void
456a6f13a4aSGreg Roach     */
457b702978eSGreg Roach    protected function footerStartHandler(): void
458c1010edaSGreg Roach    {
4597a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
460a6f13a4aSGreg Roach    }
461a6f13a4aSGreg Roach
462a6f13a4aSGreg Roach    /**
463fab8f067SGreg Roach     * Handle <cell>
464a6f13a4aSGreg Roach     *
465fab8f067SGreg Roach     * @param string[] $attrs
4668ba2e626SGreg Roach     *
4678ba2e626SGreg Roach     * @return void
468a6f13a4aSGreg Roach     */
469b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
470c1010edaSGreg Roach    {
471a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4727a6ee1acSGreg Roach        $align = '';
473a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
474a6f13a4aSGreg Roach            $align = $attrs['align'];
475a6f13a4aSGreg Roach            // RTL supported left/right alignment
476044416d2SGreg Roach            if ($align === 'rightrtl') {
477e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4787a6ee1acSGreg Roach                    $align = 'left';
479a6f13a4aSGreg Roach                } else {
4807a6ee1acSGreg Roach                    $align = 'right';
481a6f13a4aSGreg Roach                }
482044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
483e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4847a6ee1acSGreg Roach                    $align = 'right';
485a6f13a4aSGreg Roach                } else {
4867a6ee1acSGreg Roach                    $align = 'left';
487a6f13a4aSGreg Roach                }
488a6f13a4aSGreg Roach            }
489a6f13a4aSGreg Roach        }
490a6f13a4aSGreg Roach
491a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4927a6ee1acSGreg Roach        $bgcolor = '';
493a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
494a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
495a6f13a4aSGreg Roach        }
496a6f13a4aSGreg Roach
497a6f13a4aSGreg Roach        // int Whether or not the background should be painted
498a6f13a4aSGreg Roach        $fill = 1;
499a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
5007a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
501a6f13a4aSGreg Roach                $fill = 0;
5027a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
503a6f13a4aSGreg Roach                $fill = 1;
504a6f13a4aSGreg Roach            }
505a6f13a4aSGreg Roach        }
506a6f13a4aSGreg Roach
507a6f13a4aSGreg Roach        $reseth = true;
508a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
509a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
5107a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
511a6f13a4aSGreg Roach                $reseth = false;
5127a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
513a6f13a4aSGreg Roach                $reseth = true;
514a6f13a4aSGreg Roach            }
515a6f13a4aSGreg Roach        }
516a6f13a4aSGreg Roach
517a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
518a6f13a4aSGreg Roach        $border = 0;
519a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
520a6f13a4aSGreg Roach            $border = $attrs['border'];
521a6f13a4aSGreg Roach        }
522a6f13a4aSGreg Roach        // string Border color in HTML code
5237a6ee1acSGreg Roach        $bocolor = '';
524a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
525a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
526a6f13a4aSGreg Roach        }
527a6f13a4aSGreg Roach
528a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
529a6f13a4aSGreg Roach        $height = 0;
530a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
531589feda3SGreg Roach            $height = $attrs['height'];
532a6f13a4aSGreg Roach        }
533a6f13a4aSGreg 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.
534a6f13a4aSGreg Roach        $width = 0;
535a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
536589feda3SGreg Roach            $width = $attrs['width'];
537a6f13a4aSGreg Roach        }
538a6f13a4aSGreg Roach
539a6f13a4aSGreg Roach        // int Stretch carachter mode
540a6f13a4aSGreg Roach        $stretch = 0;
541a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
542a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
543a6f13a4aSGreg Roach        }
544a6f13a4aSGreg Roach
545a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
546c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
547a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5487a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
549c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
550a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
551a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5527a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
553a6f13a4aSGreg Roach                $left = 0;
554a6f13a4aSGreg Roach            }
555a6f13a4aSGreg Roach        }
556a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
557c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
558a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5597a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
560c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
561a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
562a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5637a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
564a6f13a4aSGreg Roach                $top = 0;
565a6f13a4aSGreg Roach            }
566a6f13a4aSGreg Roach        }
567a6f13a4aSGreg Roach
568a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5697a6ee1acSGreg Roach        $style = '';
570a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
571a6f13a4aSGreg Roach            $style = $attrs['style'];
572a6f13a4aSGreg Roach        }
573a6f13a4aSGreg Roach
574a6f13a4aSGreg Roach        // string Text color in html code
5757a6ee1acSGreg Roach        $tcolor = '';
576a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
577a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
578a6f13a4aSGreg Roach        }
579a6f13a4aSGreg Roach
580a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
581a6f13a4aSGreg Roach        $ln = 0;
582a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
583a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
584a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5857a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
586a6f13a4aSGreg Roach                $ln = 0;
587a6f13a4aSGreg Roach            }
588a6f13a4aSGreg Roach        }
589a6f13a4aSGreg Roach
590044416d2SGreg Roach        if ($align === 'left') {
5917a6ee1acSGreg Roach            $align = 'L';
592044416d2SGreg Roach        } elseif ($align === 'right') {
5937a6ee1acSGreg Roach            $align = 'R';
594044416d2SGreg Roach        } elseif ($align === 'center') {
5957a6ee1acSGreg Roach            $align = 'C';
596044416d2SGreg Roach        } elseif ($align === 'justify') {
5977a6ee1acSGreg Roach            $align = 'J';
598a6f13a4aSGreg Roach        }
599a6f13a4aSGreg Roach
6009b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
601a6f13a4aSGreg Roach        $this->print_data         = true;
602a6f13a4aSGreg Roach
603e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
60424f2a3afSGreg Roach            (int) $width,
60524f2a3afSGreg Roach            (int) $height,
606a6f13a4aSGreg Roach            $border,
607a6f13a4aSGreg Roach            $align,
608a6f13a4aSGreg Roach            $bgcolor,
609a6f13a4aSGreg Roach            $style,
610a6f13a4aSGreg Roach            $ln,
611a6f13a4aSGreg Roach            $top,
612a6f13a4aSGreg Roach            $left,
613a6f13a4aSGreg Roach            $fill,
614a6f13a4aSGreg Roach            $stretch,
615a6f13a4aSGreg Roach            $bocolor,
616a6f13a4aSGreg Roach            $tcolor,
617a6f13a4aSGreg Roach            $reseth
618a6f13a4aSGreg Roach        );
619a6f13a4aSGreg Roach    }
620a6f13a4aSGreg Roach
621a6f13a4aSGreg Roach    /**
622fab8f067SGreg Roach     * Handle </cell>
6238ba2e626SGreg Roach     *
6248ba2e626SGreg Roach     * @return void
625a6f13a4aSGreg Roach     */
626b702978eSGreg Roach    protected function cellEndHandler(): void
627c1010edaSGreg Roach    {
628a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
629e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
630a6f13a4aSGreg Roach    }
631a6f13a4aSGreg Roach
632a6f13a4aSGreg Roach    /**
633fab8f067SGreg Roach     * Handle <now />
6348ba2e626SGreg Roach     *
6358ba2e626SGreg Roach     * @return void
636a6f13a4aSGreg Roach     */
637b702978eSGreg Roach    protected function nowStartHandler(): void
638c1010edaSGreg Roach    {
6394459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
640a6f13a4aSGreg Roach    }
641a6f13a4aSGreg Roach
642a6f13a4aSGreg Roach    /**
643fab8f067SGreg Roach     * Handle <pageNum />
6448ba2e626SGreg Roach     *
6458ba2e626SGreg Roach     * @return void
646a6f13a4aSGreg Roach     */
647b702978eSGreg Roach    protected function pageNumStartHandler(): void
648c1010edaSGreg Roach    {
6497a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
650a6f13a4aSGreg Roach    }
651a6f13a4aSGreg Roach
652a6f13a4aSGreg Roach    /**
653fab8f067SGreg Roach     * Handle <totalPages />
6548ba2e626SGreg Roach     *
6558ba2e626SGreg Roach     * @return void
656a6f13a4aSGreg Roach     */
657b702978eSGreg Roach    protected function totalPagesStartHandler(): void
658c1010edaSGreg Roach    {
6597a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
660a6f13a4aSGreg Roach    }
661a6f13a4aSGreg Roach
662a6f13a4aSGreg Roach    /**
663a6f13a4aSGreg Roach     * Called at the start of an element.
664a6f13a4aSGreg Roach     *
665c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6668ba2e626SGreg Roach     *
6678ba2e626SGreg Roach     * @return void
668a6f13a4aSGreg Roach     */
669b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
670c1010edaSGreg Roach    {
671a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
672a6f13a4aSGreg Roach            $this->process_gedcoms++;
673a6f13a4aSGreg Roach
674a6f13a4aSGreg Roach            return;
675a6f13a4aSGreg Roach        }
676a6f13a4aSGreg Roach
677a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6787a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6797a6ee1acSGreg Roach        $tags      = explode(':', $tag);
680a6f13a4aSGreg Roach        $newgedrec = '';
681a6f13a4aSGreg Roach        if (count($tags) < 2) {
6826b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
683299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
684a6f13a4aSGreg Roach        }
685a6f13a4aSGreg Roach        if (empty($newgedrec)) {
686a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
687a6f13a4aSGreg Roach            $newgedrec = '';
688a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6897a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
690d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
691d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
692a6f13a4aSGreg Roach                    } else {
6936b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
694299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
695a6f13a4aSGreg Roach                    }
696a6f13a4aSGreg Roach                } else {
6977a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
69813abd6f3SGreg Roach                        $gmatch = [];
699a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7006b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
701299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
702a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
703a6f13a4aSGreg Roach                        } else {
704a6f13a4aSGreg Roach                            $newgedrec = '';
705a6f13a4aSGreg Roach                            break;
706a6f13a4aSGreg Roach                        }
707a6f13a4aSGreg Roach                    } else {
708b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
7093d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
710a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
711a6f13a4aSGreg Roach                    }
712a6f13a4aSGreg Roach                }
713a6f13a4aSGreg Roach            }
714a6f13a4aSGreg Roach        }
715a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7169b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
717a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
718a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
719a6f13a4aSGreg Roach                $this->fact = $match[2];
720a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
721a6f13a4aSGreg Roach            }
722a6f13a4aSGreg Roach        } else {
723a6f13a4aSGreg Roach            $this->process_gedcoms++;
724a6f13a4aSGreg Roach        }
725a6f13a4aSGreg Roach    }
726a6f13a4aSGreg Roach
727a6f13a4aSGreg Roach    /**
728a6f13a4aSGreg Roach     * Called at the end of an element.
7298ba2e626SGreg Roach     *
7308ba2e626SGreg Roach     * @return void
731a6f13a4aSGreg Roach     */
732b702978eSGreg Roach    protected function gedcomEndHandler(): void
733c1010edaSGreg Roach    {
734a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
735a6f13a4aSGreg Roach            $this->process_gedcoms--;
736a6f13a4aSGreg Roach        } else {
73765e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
738a6f13a4aSGreg Roach        }
739a6f13a4aSGreg Roach    }
740a6f13a4aSGreg Roach
741a6f13a4aSGreg Roach    /**
742fab8f067SGreg Roach     * Handle <textBox>
743a6f13a4aSGreg Roach     *
744fab8f067SGreg Roach     * @param string[] $attrs
7458ba2e626SGreg Roach     *
7468ba2e626SGreg Roach     * @return void
747a6f13a4aSGreg Roach     */
748b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
749c1010edaSGreg Roach    {
750a6f13a4aSGreg Roach        // string Background color code
7517a6ee1acSGreg Roach        $bgcolor = '';
752a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
753a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
754a6f13a4aSGreg Roach        }
755a6f13a4aSGreg Roach
756a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
757a6f13a4aSGreg Roach        $fill = true;
758a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7597a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
760a6f13a4aSGreg Roach                $fill = false;
7617a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
762a6f13a4aSGreg Roach                $fill = true;
763a6f13a4aSGreg Roach            }
764a6f13a4aSGreg Roach        }
765a6f13a4aSGreg Roach
766a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
767a6f13a4aSGreg Roach        $border = false;
768a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7697a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
770a6f13a4aSGreg Roach                $border = true;
7717a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
772a6f13a4aSGreg Roach                $border = false;
773a6f13a4aSGreg Roach            }
774a6f13a4aSGreg Roach        }
775a6f13a4aSGreg Roach
776a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
777a6f13a4aSGreg Roach        $height = 0;
778a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
779a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
780a6f13a4aSGreg Roach        }
781a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
782a6f13a4aSGreg Roach        $width = 0;
783a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
784a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
785a6f13a4aSGreg Roach        }
786a6f13a4aSGreg Roach
787a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
788c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
789a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7907a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
791c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
792a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
793a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7947a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
795a6f13a4aSGreg Roach                $left = 0;
796a6f13a4aSGreg Roach            }
797a6f13a4aSGreg Roach        }
798a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
799c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
800a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8017a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
802c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
803a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
804a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8057a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
806a6f13a4aSGreg Roach                $top = 0;
807a6f13a4aSGreg Roach            }
808a6f13a4aSGreg Roach        }
809a6f13a4aSGreg 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
810a6f13a4aSGreg Roach        $newline = false;
811a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8127a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
813a6f13a4aSGreg Roach                $newline = true;
8147a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
815a6f13a4aSGreg Roach                $newline = false;
816a6f13a4aSGreg Roach            }
817a6f13a4aSGreg Roach        }
818a6f13a4aSGreg Roach        // boolean
819a6f13a4aSGreg Roach        $pagecheck = true;
820a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8217a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
822a6f13a4aSGreg Roach                $pagecheck = false;
8237a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
824a6f13a4aSGreg Roach                $pagecheck = true;
825a6f13a4aSGreg Roach            }
826a6f13a4aSGreg Roach        }
827a6f13a4aSGreg Roach        // boolean Cell padding
828a6f13a4aSGreg Roach        $padding = true;
829a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8307a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
831a6f13a4aSGreg Roach                $padding = false;
8327a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
833a6f13a4aSGreg Roach                $padding = true;
834a6f13a4aSGreg Roach            }
835a6f13a4aSGreg Roach        }
836a6f13a4aSGreg Roach        // boolean Reset this box Height
837a6f13a4aSGreg Roach        $reseth = false;
838a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8397a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
840a6f13a4aSGreg Roach                $reseth = true;
8417a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
842a6f13a4aSGreg Roach                $reseth = false;
843a6f13a4aSGreg Roach            }
844a6f13a4aSGreg Roach        }
845a6f13a4aSGreg Roach
846a6f13a4aSGreg Roach        // string Style of rendering
8477a6ee1acSGreg Roach        $style = '';
848a6f13a4aSGreg Roach
8499b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
850a6f13a4aSGreg Roach        $this->print_data         = false;
851a6f13a4aSGreg Roach
8529b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
853e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
854a6f13a4aSGreg Roach            $width,
855a6f13a4aSGreg Roach            $height,
856a6f13a4aSGreg Roach            $border,
857a6f13a4aSGreg Roach            $bgcolor,
858a6f13a4aSGreg Roach            $newline,
859a6f13a4aSGreg Roach            $left,
860a6f13a4aSGreg Roach            $top,
861a6f13a4aSGreg Roach            $pagecheck,
862a6f13a4aSGreg Roach            $style,
863a6f13a4aSGreg Roach            $fill,
864a6f13a4aSGreg Roach            $padding,
865a6f13a4aSGreg Roach            $reseth
866a6f13a4aSGreg Roach        );
867a6f13a4aSGreg Roach    }
868a6f13a4aSGreg Roach
869a6f13a4aSGreg Roach    /**
870fab8f067SGreg Roach     * Handle <textBox>
8718ba2e626SGreg Roach     *
8728ba2e626SGreg Roach     * @return void
873a6f13a4aSGreg Roach     */
874b702978eSGreg Roach    protected function textBoxEndHandler(): void
875c1010edaSGreg Roach    {
876a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
877e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
87831d9777aSGreg Roach
87931d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
88031d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
88131d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
88231d9777aSGreg Roach
883e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
884e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
885a6f13a4aSGreg Roach    }
886a6f13a4aSGreg Roach
887a6f13a4aSGreg Roach    /**
88876692c8bSGreg Roach     * XLM <Text>.
88976692c8bSGreg Roach     *
890c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8918ba2e626SGreg Roach     *
8928ba2e626SGreg Roach     * @return void
893a6f13a4aSGreg Roach     */
894b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
895c1010edaSGreg Roach    {
8969b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
897a6f13a4aSGreg Roach        $this->print_data         = true;
898a6f13a4aSGreg Roach
899a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9007a6ee1acSGreg Roach        $style = '';
901a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
902a6f13a4aSGreg Roach            $style = $attrs['style'];
903a6f13a4aSGreg Roach        }
904a6f13a4aSGreg Roach
905a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9067a6ee1acSGreg Roach        $color = '';
907a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
908a6f13a4aSGreg Roach            $color = $attrs['color'];
909a6f13a4aSGreg Roach        }
910a6f13a4aSGreg Roach
911e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
912a6f13a4aSGreg Roach    }
913a6f13a4aSGreg Roach
914a6f13a4aSGreg Roach    /**
915fab8f067SGreg Roach     * Handle </text>
9168ba2e626SGreg Roach     *
9178ba2e626SGreg Roach     * @return void
918a6f13a4aSGreg Roach     */
919b702978eSGreg Roach    protected function textEndHandler(): void
920c1010edaSGreg Roach    {
921a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
922e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
923a6f13a4aSGreg Roach    }
924a6f13a4aSGreg Roach
925a6f13a4aSGreg Roach    /**
926fab8f067SGreg Roach     * Handle <getPersonName />
927a6f13a4aSGreg Roach     * Get the name
928a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
929a6f13a4aSGreg Roach     * 2. id is set with a record id
930a6f13a4aSGreg Roach     *
931c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9328ba2e626SGreg Roach     *
9338ba2e626SGreg Roach     * @return void
934a6f13a4aSGreg Roach     */
935b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
936c1010edaSGreg Roach    {
9377a6ee1acSGreg Roach        $id    = '';
93813abd6f3SGreg Roach        $match = [];
939a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9407a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
941a6f13a4aSGreg Roach                $id = $match[1];
942a6f13a4aSGreg Roach            }
943a6f13a4aSGreg Roach        } else {
9447a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
945d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
946d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
947a6f13a4aSGreg Roach                }
948a6f13a4aSGreg Roach            } else {
9497a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
95013abd6f3SGreg Roach                    $gmatch = [];
951a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
952a6f13a4aSGreg Roach                        $id = $gmatch[1];
953a6f13a4aSGreg Roach                    }
954a6f13a4aSGreg Roach                } else {
955a6f13a4aSGreg Roach                    $id = $attrs['id'];
956a6f13a4aSGreg Roach                }
957a6f13a4aSGreg Roach            }
958a6f13a4aSGreg Roach        }
959a6f13a4aSGreg Roach        if (!empty($id)) {
9606b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9618f038c36SRico Sonntag            if ($record === null) {
962a6f13a4aSGreg Roach                return;
963a6f13a4aSGreg Roach            }
964a6f13a4aSGreg Roach            if (!$record->canShowName()) {
965a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
966a6f13a4aSGreg Roach            } else {
96739ca88baSGreg Roach                $name = $record->fullName();
968a6f13a4aSGreg Roach                $name = strip_tags($name);
969a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
970381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
971a6f13a4aSGreg Roach                } else {
972911124f5SGreg Roach                    $addname = (string) $record->alternateName();
973a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
974a6f13a4aSGreg Roach                    if (!empty($addname)) {
9757a6ee1acSGreg Roach                        $name .= ' ' . $addname;
976a6f13a4aSGreg Roach                    }
977a6f13a4aSGreg Roach                }
978a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
979a6f13a4aSGreg Roach            }
980a6f13a4aSGreg Roach        }
981a6f13a4aSGreg Roach    }
982a6f13a4aSGreg Roach
983a6f13a4aSGreg Roach    /**
984fab8f067SGreg Roach     * Handle <gedcomValue />
985a6f13a4aSGreg Roach     *
986fab8f067SGreg Roach     * @param string[] $attrs
9878ba2e626SGreg Roach     *
9888ba2e626SGreg Roach     * @return void
989a6f13a4aSGreg Roach     */
990b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
991c1010edaSGreg Roach    {
9927a6ee1acSGreg Roach        $id    = '';
99313abd6f3SGreg Roach        $match = [];
9947a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
995a6f13a4aSGreg Roach            $id = $match[1];
996a6f13a4aSGreg Roach        }
997a6f13a4aSGreg Roach
998044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9997a6ee1acSGreg Roach            $useBreak = '1';
1000a6f13a4aSGreg Roach        } else {
10017a6ee1acSGreg Roach            $useBreak = '0';
1002a6f13a4aSGreg Roach        }
1003a6f13a4aSGreg Roach
1004a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1005a6f13a4aSGreg Roach        if (!empty($tag)) {
1006044416d2SGreg Roach            if ($tag === '@desc') {
1007a6f13a4aSGreg Roach                $value = $this->desc;
1008a6f13a4aSGreg Roach                $value = trim($value);
1009a6f13a4aSGreg Roach                $this->current_element->addText($value);
1010a6f13a4aSGreg Roach            }
1011044416d2SGreg Roach            if ($tag === '@id') {
1012a6f13a4aSGreg Roach                $this->current_element->addText($id);
1013a6f13a4aSGreg Roach            } else {
10147a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1015a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1016b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1017b2448a1bSGreg Roach                    if ($level === 0) {
1018a6f13a4aSGreg Roach                        $level++;
1019a6f13a4aSGreg Roach                    }
1020a6f13a4aSGreg Roach                } else {
1021b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1022a6f13a4aSGreg Roach                }
1023a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10243d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1025a6f13a4aSGreg Roach                switch (end($tags)) {
1026a6f13a4aSGreg Roach                    case 'DATE':
1027a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1028a6f13a4aSGreg Roach                        $value = $tmp->display();
1029a6f13a4aSGreg Roach                        break;
1030a6f13a4aSGreg Roach                    case 'PLAC':
1031299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1032392561bbSGreg Roach                        $value = $tmp->shortName();
1033a6f13a4aSGreg Roach                        break;
1034a6f13a4aSGreg Roach                }
1035044416d2SGreg Roach                if ($useBreak === '1') {
1036a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1037a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1038a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1039a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1040a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10413b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10423b3cfeeaSGreg Roach                        $value = substr($value, 4);
1043a6f13a4aSGreg Roach                    }
1044a6f13a4aSGreg Roach                }
1045d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10463cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10474d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
104808b2f88aSGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($this->tree)->convertToHtml($value));
10494d35caa7SGreg Roach                    } else {
105008b2f88aSGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($this->tree)->convertToHtml($value));
10514d35caa7SGreg Roach                    }
1052a4d703aeSGreg Roach                }
1053a2a485c3SGreg Roach
1054a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1055381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1056a2a485c3SGreg Roach                }
1057a6f13a4aSGreg Roach                $this->current_element->addText($value);
1058a6f13a4aSGreg Roach            }
1059a6f13a4aSGreg Roach        }
1060a6f13a4aSGreg Roach    }
1061a6f13a4aSGreg Roach
1062a6f13a4aSGreg Roach    /**
1063fab8f067SGreg Roach     * Handle <repeatTag>
1064a6f13a4aSGreg Roach     *
1065fab8f067SGreg Roach     * @param string[] $attrs
10668ba2e626SGreg Roach     *
10678ba2e626SGreg Roach     * @return void
1068a6f13a4aSGreg Roach     */
1069b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1070c1010edaSGreg Roach    {
1071a6f13a4aSGreg Roach        $this->process_repeats++;
1072a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1073a6f13a4aSGreg Roach            return;
1074a6f13a4aSGreg Roach        }
1075a6f13a4aSGreg Roach
10769b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
107713abd6f3SGreg Roach        $this->repeats         = [];
1078e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1079a6f13a4aSGreg Roach
1080e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1081a6f13a4aSGreg Roach        if (!empty($tag)) {
1082044416d2SGreg Roach            if ($tag === '@desc') {
1083a6f13a4aSGreg Roach                $value = $this->desc;
1084a6f13a4aSGreg Roach                $value = trim($value);
1085a6f13a4aSGreg Roach                $this->current_element->addText($value);
1086a6f13a4aSGreg Roach            } else {
10877a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10887a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1089b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1090b2448a1bSGreg Roach                if ($level === 0) {
1091a6f13a4aSGreg Roach                    $level++;
1092a6f13a4aSGreg Roach                }
1093a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1094a6f13a4aSGreg Roach                $t      = $tag;
1095a6f13a4aSGreg Roach                $count  = count($tags);
1096a6f13a4aSGreg Roach                $i      = 0;
1097a6f13a4aSGreg Roach                while ($i < $count) {
1098a6f13a4aSGreg Roach                    $t = $tags[$i];
1099a6f13a4aSGreg Roach                    if (!empty($t)) {
1100a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
11013d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1102a6f13a4aSGreg Roach                            if (empty($subrec)) {
1103a6f13a4aSGreg Roach                                $level--;
11043d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1105a6f13a4aSGreg Roach                                if (empty($subrec)) {
1106a6f13a4aSGreg Roach                                    return;
1107a6f13a4aSGreg Roach                                }
1108a6f13a4aSGreg Roach                            }
1109a6f13a4aSGreg Roach                        }
1110a6f13a4aSGreg Roach                        $level++;
1111a6f13a4aSGreg Roach                    }
1112a6f13a4aSGreg Roach                    $i++;
1113a6f13a4aSGreg Roach                }
1114a6f13a4aSGreg Roach                $level--;
1115a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1116a6f13a4aSGreg Roach                $i     = 0;
1117a6f13a4aSGreg Roach                while ($i < $count) {
1118a6f13a4aSGreg Roach                    $i++;
1119a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1120a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11218d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11226b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1123a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1124a9007102SGreg Roach                            continue;
1125a9007102SGreg Roach                        }
1126a9007102SGreg Roach                    }
1127a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1128a6f13a4aSGreg Roach                }
1129a6f13a4aSGreg Roach            }
1130a6f13a4aSGreg Roach        }
1131a6f13a4aSGreg Roach    }
1132a6f13a4aSGreg Roach
1133a6f13a4aSGreg Roach    /**
1134fab8f067SGreg Roach     * Handle </repeatTag>
11358ba2e626SGreg Roach     *
11368ba2e626SGreg Roach     * @return void
1137a6f13a4aSGreg Roach     */
1138b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1139c1010edaSGreg Roach    {
1140a6f13a4aSGreg Roach        $this->process_repeats--;
1141a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1142a6f13a4aSGreg Roach            return;
1143a6f13a4aSGreg Roach        }
1144a6f13a4aSGreg Roach
1145a6f13a4aSGreg Roach        // Check if there is anything to repeat
1146a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1147a6f13a4aSGreg Roach            // No need to load them if not used...
1148a6f13a4aSGreg Roach
1149a6f13a4aSGreg Roach            $lineoffset = 0;
1150a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1151a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1152a6f13a4aSGreg Roach            }
1153a6f13a4aSGreg Roach            //-- read the xml from the file
1154299d100dSGreg Roach            $lines = file($this->report);
1155dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1156a6f13a4aSGreg Roach                $lineoffset--;
1157a6f13a4aSGreg Roach            }
1158a6f13a4aSGreg Roach            $lineoffset++;
1159a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1160a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1161a6f13a4aSGreg Roach            // RepeatTag Level counter
1162a6f13a4aSGreg Roach            $count = 1;
1163a6f13a4aSGreg Roach            while (0 < $count) {
1164dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1165a6f13a4aSGreg Roach                    $count++;
1166dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1167a6f13a4aSGreg Roach                    $count--;
1168a6f13a4aSGreg Roach                }
1169a6f13a4aSGreg Roach                if (0 < $count) {
1170a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1171a6f13a4aSGreg Roach                }
1172a6f13a4aSGreg Roach                $line_nr++;
1173a6f13a4aSGreg Roach            }
1174a6f13a4aSGreg Roach            // No need to drag this
1175a6f13a4aSGreg Roach            unset($lines);
1176a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1177a6f13a4aSGreg Roach            // Save original values
11789b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1179a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1180a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1181a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1182a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1183e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1184a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11851aa04befSGreg Roach
11861aa04befSGreg Roach                xml_set_element_handler(
11871aa04befSGreg Roach                    $repeat_parser,
11889d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11891aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11901aa04befSGreg Roach                    },
11919d454b6bSGreg Roach                    function ($parser, string $name): void {
11921aa04befSGreg Roach                        $this->endElement($parser, $name);
11931aa04befSGreg Roach                    }
11941aa04befSGreg Roach                );
11951aa04befSGreg Roach
11961aa04befSGreg Roach                xml_set_character_data_handler(
11971aa04befSGreg Roach                    $repeat_parser,
11989d454b6bSGreg Roach                    function ($parser, string $data): void {
11991aa04befSGreg Roach                        $this->characterData($parser, $data);
12001aa04befSGreg Roach                    }
12011aa04befSGreg Roach                );
12021aa04befSGreg Roach
1203a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12046ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1205a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1206a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1207a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1208a6f13a4aSGreg Roach                    ));
1209a6f13a4aSGreg Roach                }
1210a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1211a6f13a4aSGreg Roach            }
1212a6f13a4aSGreg Roach            // Restore original values
1213a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1214e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1215a6f13a4aSGreg Roach        }
121665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1217a6f13a4aSGreg Roach    }
1218a6f13a4aSGreg Roach
1219a6f13a4aSGreg Roach    /**
1220a6f13a4aSGreg Roach     * Variable lookup
1221a6f13a4aSGreg Roach     * Retrieve predefined variables :
1222a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1223a6f13a4aSGreg Roach     *        1 EVEN This is a description
1224a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1225a6f13a4aSGreg Roach     * $ I18N::translate('....')
1226a6f13a4aSGreg Roach     * $ language_settings[]
1227a6f13a4aSGreg Roach     *
1228c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12298ba2e626SGreg Roach     *
12308ba2e626SGreg Roach     * @return void
1231a6f13a4aSGreg Roach     */
1232b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1233c1010edaSGreg Roach    {
1234a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12356ccdf4f0SGreg 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));
1236a6f13a4aSGreg Roach        }
1237a6f13a4aSGreg Roach
1238a6f13a4aSGreg Roach        $var = $attrs['var'];
1239a6f13a4aSGreg Roach        // SetVar element preset variables
1240d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1241d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1242a6f13a4aSGreg Roach        } else {
1243a6f13a4aSGreg Roach            $tfact = $this->fact;
12447a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1245a6f13a4aSGreg Roach                // Use :
1246a6f13a4aSGreg Roach                // n TYPE This text if string
1247a6f13a4aSGreg Roach                $tfact = $this->type;
124897def6bcSGreg Roach            } else {
124997def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
125097def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
125197def6bcSGreg Roach
125297def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
125397def6bcSGreg Roach                        $tfact = $element->label();
125497def6bcSGreg Roach                        break;
1255a6f13a4aSGreg Roach                    }
125697def6bcSGreg Roach                }
125797def6bcSGreg Roach            }
125897def6bcSGreg Roach
125997def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
126097def6bcSGreg Roach
1261a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1262da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1263a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1264a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1265a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1266a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1267a6f13a4aSGreg Roach            }
1268a6f13a4aSGreg Roach        }
1269a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1270a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12717a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1272a6f13a4aSGreg Roach                $g   = new Date($var);
1273a6f13a4aSGreg Roach                $var = $g->display();
1274a6f13a4aSGreg Roach            }
1275a6f13a4aSGreg Roach        }
1276a6f13a4aSGreg Roach        $this->current_element->addText($var);
12772836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1278a6f13a4aSGreg Roach    }
1279a6f13a4aSGreg Roach
1280a6f13a4aSGreg Roach    /**
1281fab8f067SGreg Roach     * Handle <facts>
128276692c8bSGreg Roach     *
1283fab8f067SGreg Roach     * @param string[] $attrs
12848ba2e626SGreg Roach     *
12858ba2e626SGreg Roach     * @return void
1286a6f13a4aSGreg Roach     */
1287b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1288c1010edaSGreg Roach    {
1289a6f13a4aSGreg Roach        $this->process_repeats++;
1290a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1291a6f13a4aSGreg Roach            return;
1292a6f13a4aSGreg Roach        }
1293a6f13a4aSGreg Roach
12949b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
129513abd6f3SGreg Roach        $this->repeats         = [];
1296e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1297a6f13a4aSGreg Roach
12987a6ee1acSGreg Roach        $id    = '';
129913abd6f3SGreg Roach        $match = [];
13007a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1301a6f13a4aSGreg Roach            $id = $match[1];
1302a6f13a4aSGreg Roach        }
13037a6ee1acSGreg Roach        $tag = '';
1304a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1305a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1306a6f13a4aSGreg Roach        }
13077a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1308d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1309a6f13a4aSGreg Roach        }
1310a6f13a4aSGreg Roach
13116b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1312a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1313820b62dfSGreg Roach            $facts = $record->facts([], true);
131413abd6f3SGreg Roach            $this->repeats = [];
1315a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1316195b5e75SGreg Roach            foreach ($facts as $fact) {
1317d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1318d0889c63SGreg Roach
1319d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1320195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1321a6f13a4aSGreg Roach                }
1322a6f13a4aSGreg Roach            }
1323a6f13a4aSGreg Roach        } else {
132430158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1325d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1326138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1327a6f13a4aSGreg Roach                }
1328a6f13a4aSGreg Roach            }
1329a6f13a4aSGreg Roach        }
1330a6f13a4aSGreg Roach    }
1331a6f13a4aSGreg Roach
1332a6f13a4aSGreg Roach    /**
1333fab8f067SGreg Roach     * Handle </facts>
13348ba2e626SGreg Roach     *
13358ba2e626SGreg Roach     * @return void
1336a6f13a4aSGreg Roach     */
1337b702978eSGreg Roach    protected function factsEndHandler(): void
1338c1010edaSGreg Roach    {
1339a6f13a4aSGreg Roach        $this->process_repeats--;
1340a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1341a6f13a4aSGreg Roach            return;
1342a6f13a4aSGreg Roach        }
1343a6f13a4aSGreg Roach
1344a6f13a4aSGreg Roach        // Check if there is anything to repeat
1345a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1346e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1347a6f13a4aSGreg Roach            $lineoffset = 0;
1348a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1349a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1350a6f13a4aSGreg Roach            }
1351a6f13a4aSGreg Roach
1352a6f13a4aSGreg Roach            //-- read the xml from the file
1353299d100dSGreg Roach            $lines = file($this->report);
1354dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1355a6f13a4aSGreg Roach                $lineoffset--;
1356a6f13a4aSGreg Roach            }
1357a6f13a4aSGreg Roach            $lineoffset++;
1358a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1359a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1360a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1361a6f13a4aSGreg Roach            while ($line_nr < $i) {
1362a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1363a6f13a4aSGreg Roach                $line_nr++;
1364a6f13a4aSGreg Roach            }
1365a6f13a4aSGreg Roach            // No need to drag this
1366a6f13a4aSGreg Roach            unset($lines);
1367a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1368a6f13a4aSGreg Roach            // Save original values
13699b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1370a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1371a6f13a4aSGreg Roach            $count                = count($this->repeats);
1372a6f13a4aSGreg Roach            $i                    = 0;
1373a6f13a4aSGreg Roach            while ($i < $count) {
1374a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1375a6f13a4aSGreg Roach                $this->fact   = '';
1376a6f13a4aSGreg Roach                $this->desc   = '';
1377a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1378a6f13a4aSGreg Roach                    $this->fact = $match[1];
1379a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
138013abd6f3SGreg Roach                        $tmatch = [];
1381a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1382a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1383a6f13a4aSGreg Roach                        } else {
1384a6f13a4aSGreg Roach                            $this->type = ' ';
1385a6f13a4aSGreg Roach                        }
1386a6f13a4aSGreg Roach                    }
1387a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13883d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1389a6f13a4aSGreg Roach                }
1390a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1391e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1392a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13931aa04befSGreg Roach
13941aa04befSGreg Roach                xml_set_element_handler(
13951aa04befSGreg Roach                    $repeat_parser,
13969d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13971aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13981aa04befSGreg Roach                    },
13999d454b6bSGreg Roach                    function ($parser, string $name): void {
14001aa04befSGreg Roach                        $this->endElement($parser, $name);
14011aa04befSGreg Roach                    }
14021aa04befSGreg Roach                );
14031aa04befSGreg Roach
14041aa04befSGreg Roach                xml_set_character_data_handler(
14051aa04befSGreg Roach                    $repeat_parser,
14069d454b6bSGreg Roach                    function ($parser, string $data): void {
14071aa04befSGreg Roach                        $this->characterData($parser, $data);
14081aa04befSGreg Roach                    }
14091aa04befSGreg Roach                );
14101aa04befSGreg Roach
1411a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14126ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1413a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1414a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1415a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1416a6f13a4aSGreg Roach                    ));
1417a6f13a4aSGreg Roach                }
1418a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1419a6f13a4aSGreg Roach                $i++;
1420a6f13a4aSGreg Roach            }
1421a6f13a4aSGreg Roach            // Restore original values
1422e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1423a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1424a6f13a4aSGreg Roach        }
142565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1426a6f13a4aSGreg Roach    }
1427a6f13a4aSGreg Roach
1428a6f13a4aSGreg Roach    /**
1429a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1430d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1431a6f13a4aSGreg Roach     *
1432c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14338ba2e626SGreg Roach     *
14348ba2e626SGreg Roach     * @return void
1435a6f13a4aSGreg Roach     */
1436b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1437c1010edaSGreg Roach    {
1438a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14396ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1440a6f13a4aSGreg Roach        }
1441a6f13a4aSGreg Roach
1442a6f13a4aSGreg Roach        $name  = $attrs['name'];
1443a6f13a4aSGreg Roach        $value = $attrs['value'];
144413abd6f3SGreg Roach        $match = [];
1445a6f13a4aSGreg Roach        // Current GEDCOM record strings
1446044416d2SGreg Roach        if ($value === '@ID') {
14477a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1448a6f13a4aSGreg Roach                $value = $match[1];
1449a6f13a4aSGreg Roach            }
1450044416d2SGreg Roach        } elseif ($value === '@fact') {
1451a6f13a4aSGreg Roach            $value = $this->fact;
1452044416d2SGreg Roach        } elseif ($value === '@desc') {
1453a6f13a4aSGreg Roach            $value = $this->desc;
1454044416d2SGreg Roach        } elseif ($value === '@generation') {
1455589feda3SGreg Roach            $value = (string) $this->generation;
1456a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
145713abd6f3SGreg Roach            $gmatch = [];
1458a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14597a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1460a6f13a4aSGreg Roach            }
1461a6f13a4aSGreg Roach        }
1462a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1463d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1464a6f13a4aSGreg Roach        }
1465a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1466a6f13a4aSGreg Roach        $i     = 0;
1467a6f13a4aSGreg Roach        while ($i < $count) {
1468d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14697a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1470a6f13a4aSGreg Roach            $i++;
1471a6f13a4aSGreg Roach        }
1472a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1473da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1474a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1475a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1476a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1477a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1478a6f13a4aSGreg Roach        }
147952868398SGreg Roach
1480a6f13a4aSGreg Roach        // Arithmetic functions
14813cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
148252868398SGreg Roach            // Create an expression language with the functions used by our reports.
148352868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1484c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1485c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
148652868398SGreg Roach
148752868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1488a6f13a4aSGreg Roach        }
148952868398SGreg Roach
1490dec352c1SGreg Roach        if (str_contains($value, '@')) {
14917a6ee1acSGreg Roach            $value = '';
1492a6f13a4aSGreg Roach        }
1493d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1494a6f13a4aSGreg Roach    }
1495a6f13a4aSGreg Roach
1496a6f13a4aSGreg Roach    /**
1497fab8f067SGreg Roach     * Handle <if>
1498a6f13a4aSGreg Roach     *
1499fab8f067SGreg Roach     * @param string[] $attrs
15008ba2e626SGreg Roach     *
15018ba2e626SGreg Roach     * @return void
1502a6f13a4aSGreg Roach     */
1503b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1504c1010edaSGreg Roach    {
1505a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1506a6f13a4aSGreg Roach            $this->process_ifs++;
1507a6f13a4aSGreg Roach
1508a6f13a4aSGreg Roach            return;
1509a6f13a4aSGreg Roach        }
1510a6f13a4aSGreg Roach
1511a6f13a4aSGreg Roach        $condition = $attrs['condition'];
151282759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1513c1010edaSGreg Roach        $condition = str_replace([
1514c1010edaSGreg Roach            ' LT ',
1515c1010edaSGreg Roach            ' GT ',
1516c1010edaSGreg Roach        ], [
1517c1010edaSGreg Roach            '<',
1518c1010edaSGreg Roach            '>',
1519c1010edaSGreg Roach        ], $condition);
15203cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15217a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
152213abd6f3SGreg Roach        $match     = [];
15233cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1524a6f13a4aSGreg Roach        $i         = 0;
1525a6f13a4aSGreg Roach        while ($i < $count) {
1526a6f13a4aSGreg Roach            $id    = $match[$i][1];
1527a6f13a4aSGreg Roach            $value = '""';
1528044416d2SGreg Roach            if ($id === 'ID') {
15297a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1530a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1531a6f13a4aSGreg Roach                }
15327a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1533a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15347a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1535a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15367a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1537a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1538a6f13a4aSGreg Roach            } else {
1539b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1540b2448a1bSGreg Roach                if ($level === 0) {
1541a6f13a4aSGreg Roach                    $level++;
1542a6f13a4aSGreg Roach                }
15433d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1544a6f13a4aSGreg Roach                if (empty($value)) {
1545a6f13a4aSGreg Roach                    $level++;
15463d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1547a6f13a4aSGreg Roach                }
15488d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15495e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1550a6f13a4aSGreg Roach            }
1551a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1552a6f13a4aSGreg Roach            $i++;
1553a6f13a4aSGreg Roach        }
15545809450fSGreg Roach
1555cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1556cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1557c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1558c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1559cb63a60eSGreg Roach
1560cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15615809450fSGreg Roach
1562a6f13a4aSGreg Roach        if (!$ret) {
1563a6f13a4aSGreg Roach            $this->process_ifs++;
1564a6f13a4aSGreg Roach        }
1565a6f13a4aSGreg Roach    }
1566a6f13a4aSGreg Roach
1567a6f13a4aSGreg Roach    /**
1568fab8f067SGreg Roach     * Handle </if>
15698ba2e626SGreg Roach     *
15708ba2e626SGreg Roach     * @return void
1571a6f13a4aSGreg Roach     */
1572b702978eSGreg Roach    protected function ifEndHandler(): void
1573c1010edaSGreg Roach    {
1574a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1575a6f13a4aSGreg Roach            $this->process_ifs--;
1576a6f13a4aSGreg Roach        }
1577a6f13a4aSGreg Roach    }
1578a6f13a4aSGreg Roach
1579a6f13a4aSGreg Roach    /**
1580fab8f067SGreg Roach     * Handle <footnote>
1581a6f13a4aSGreg Roach     * Collect the Footnote links
1582fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1583a6f13a4aSGreg Roach     *
1584fab8f067SGreg Roach     * @param string[] $attrs
15858ba2e626SGreg Roach     *
15868ba2e626SGreg Roach     * @return void
1587a6f13a4aSGreg Roach     */
1588b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1589c1010edaSGreg Roach    {
15907a6ee1acSGreg Roach        $id = '';
15917a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1592a6f13a4aSGreg Roach            $id = $match[2];
1593a6f13a4aSGreg Roach        }
15946b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1595a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15969b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1597a6f13a4aSGreg Roach            $this->print_data         = true;
15987a6ee1acSGreg Roach            $style                    = '';
1599a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1600a6f13a4aSGreg Roach                $style = $attrs['style'];
1601a6f13a4aSGreg Roach            }
1602a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1603e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1604a6f13a4aSGreg Roach        } else {
1605a6f13a4aSGreg Roach            $this->print_data       = false;
1606a6f13a4aSGreg Roach            $this->process_footnote = false;
1607a6f13a4aSGreg Roach        }
1608a6f13a4aSGreg Roach    }
1609a6f13a4aSGreg Roach
1610a6f13a4aSGreg Roach    /**
1611fab8f067SGreg Roach     * Handle </footnote>
1612a6f13a4aSGreg Roach     * Print the collected Footnote data
16138ba2e626SGreg Roach     *
16148ba2e626SGreg Roach     * @return void
1615a6f13a4aSGreg Roach     */
1616b702978eSGreg Roach    protected function footnoteEndHandler(): void
1617c1010edaSGreg Roach    {
1618a6f13a4aSGreg Roach        if ($this->process_footnote) {
1619a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1620a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1621a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1622e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1623a6f13a4aSGreg Roach            }
1624a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1625a6f13a4aSGreg Roach        } else {
1626a6f13a4aSGreg Roach            $this->process_footnote = true;
1627a6f13a4aSGreg Roach        }
1628a6f13a4aSGreg Roach    }
1629a6f13a4aSGreg Roach
1630a6f13a4aSGreg Roach    /**
1631fab8f067SGreg Roach     * Handle <footnoteTexts />
16328ba2e626SGreg Roach     *
16338ba2e626SGreg Roach     * @return void
1634a6f13a4aSGreg Roach     */
1635b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1636c1010edaSGreg Roach    {
16377a6ee1acSGreg Roach        $temp = 'footnotetexts';
1638e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1639a6f13a4aSGreg Roach    }
1640a6f13a4aSGreg Roach
1641a6f13a4aSGreg Roach    /**
1642a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16438ba2e626SGreg Roach     *
16448ba2e626SGreg Roach     * @return void
1645a6f13a4aSGreg Roach     */
1646b702978eSGreg Roach    protected function brStartHandler(): void
1647c1010edaSGreg Roach    {
1648a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1649a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1650a6f13a4aSGreg Roach        }
1651a6f13a4aSGreg Roach    }
1652a6f13a4aSGreg Roach
1653a6f13a4aSGreg Roach    /**
1654fab8f067SGreg Roach     * Handle <sp />
1655fab8f067SGreg Roach     * Forced space
16568ba2e626SGreg Roach     *
16578ba2e626SGreg Roach     * @return void
1658a6f13a4aSGreg Roach     */
1659b702978eSGreg Roach    protected function spStartHandler(): void
1660c1010edaSGreg Roach    {
1661a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1662a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1663a6f13a4aSGreg Roach        }
1664a6f13a4aSGreg Roach    }
1665a6f13a4aSGreg Roach
1666a6f13a4aSGreg Roach    /**
1667fab8f067SGreg Roach     * Handle <highlightedImage />
166876692c8bSGreg Roach     *
1669fab8f067SGreg Roach     * @param string[] $attrs
16708ba2e626SGreg Roach     *
16718ba2e626SGreg Roach     * @return void
1672a6f13a4aSGreg Roach     */
1673b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1674c1010edaSGreg Roach    {
1675a6f13a4aSGreg Roach        $id = '';
16767a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1677a6f13a4aSGreg Roach            $id = $match[1];
1678a6f13a4aSGreg Roach        }
1679a6f13a4aSGreg Roach
1680c21bdddcSGreg Roach        // Position the top corner of this box on the page
1681c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1682a6f13a4aSGreg Roach
1683c21bdddcSGreg Roach        // Position the left corner of this box on the page
1684c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1685a6f13a4aSGreg Roach
168683cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
168783cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1688a6f13a4aSGreg Roach
1689a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
169083cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1691a6f13a4aSGreg Roach
169283cdc021SGreg Roach        // Width, height (or both).
1693c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1694c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1695a6f13a4aSGreg Roach
16966b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
16974a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
169886a63f51SGreg Roach
1699a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1700a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1701b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
170229518ad2SGreg Roach
1703a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17043c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17053c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1706a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17073c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17083c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1709a6f13a4aSGreg Roach            } else {
17103c3b90deSGreg Roach                $width  = $attributes[0];
17113c3b90deSGreg Roach                $height = $attributes[1];
1712a6f13a4aSGreg Roach            }
1713a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1714e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1715a6f13a4aSGreg Roach        }
1716a6f13a4aSGreg Roach    }
1717a6f13a4aSGreg Roach
1718a6f13a4aSGreg Roach    /**
1719fab8f067SGreg Roach     * Handle <image/>
172076692c8bSGreg Roach     *
1721fab8f067SGreg Roach     * @param string[] $attrs
17228ba2e626SGreg Roach     *
17238ba2e626SGreg Roach     * @return void
1724a6f13a4aSGreg Roach     */
1725b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1726c1010edaSGreg Roach    {
172783cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1728c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1729a6f13a4aSGreg Roach
1730a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1731c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1732a6f13a4aSGreg Roach
173383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
173483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1735a6f13a4aSGreg Roach
1736a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
173783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1738a6f13a4aSGreg Roach
173983cdc021SGreg Roach        // Width, height (or both).
1740c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1741c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1742a6f13a4aSGreg Roach
174383cdc021SGreg Roach        $file = $attrs['file'] ?? '';
174483cdc021SGreg Roach
1745044416d2SGreg Roach        if ($file === '@FILE') {
174613abd6f3SGreg Roach            $match = [];
1747a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17486b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17494a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1750cdf416fbSGreg Roach
1751a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1752a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1753b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
175429518ad2SGreg Roach
1755a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17563c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17573c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1758a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17593c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17603c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1761a6f13a4aSGreg Roach                    } else {
17623c3b90deSGreg Roach                        $width  = $attributes[0];
17633c3b90deSGreg Roach                        $height = $attributes[1];
1764a6f13a4aSGreg Roach                    }
1765a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1766e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1767a6f13a4aSGreg Roach                }
1768a6f13a4aSGreg Roach            }
1769a6f13a4aSGreg Roach        } else {
17707a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1771a6f13a4aSGreg Roach                $size = getimagesize($file);
1772a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1773a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1774a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1775a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1776a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1777a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1778a6f13a4aSGreg Roach                } else {
1779a6f13a4aSGreg Roach                    $width  = $size[0];
1780a6f13a4aSGreg Roach                    $height = $size[1];
1781a6f13a4aSGreg Roach                }
1782e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1783e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1784a6f13a4aSGreg Roach            }
1785a6f13a4aSGreg Roach        }
1786a6f13a4aSGreg Roach    }
1787a6f13a4aSGreg Roach
1788a6f13a4aSGreg Roach    /**
1789fab8f067SGreg Roach     * Handle <line>
1790a6f13a4aSGreg Roach     *
1791fab8f067SGreg Roach     * @param string[] $attrs
17928ba2e626SGreg Roach     *
17938ba2e626SGreg Roach     * @return void
1794a6f13a4aSGreg Roach     */
1795b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1796c1010edaSGreg Roach    {
1797a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1798c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1799a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18007a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1801a6f13a4aSGreg Roach                $x1 = 0;
18027a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1803c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1804a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1805c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1806a6f13a4aSGreg Roach            }
1807a6f13a4aSGreg Roach        }
1808a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1809c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1810a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18117a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1812a6f13a4aSGreg Roach                $y1 = 0;
18137a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1814c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1815a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1816c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1817a6f13a4aSGreg Roach            }
1818a6f13a4aSGreg Roach        }
1819a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1820c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1821a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18227a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1823a6f13a4aSGreg Roach                $x2 = 0;
18247a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1825c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1826a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1827c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1828a6f13a4aSGreg Roach            }
1829a6f13a4aSGreg Roach        }
1830a6f13a4aSGreg Roach        // End vertical position
1831c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1832a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18337a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1834a6f13a4aSGreg Roach                $y2 = 0;
18357a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1836c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1837a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1838c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1839a6f13a4aSGreg Roach            }
1840a6f13a4aSGreg Roach        }
1841a6f13a4aSGreg Roach
1842e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1843e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1844a6f13a4aSGreg Roach    }
1845a6f13a4aSGreg Roach
1846a6f13a4aSGreg Roach    /**
1847fab8f067SGreg Roach     * Handle <list>
1848a6f13a4aSGreg Roach     *
1849fab8f067SGreg Roach     * @param string[] $attrs
18508ba2e626SGreg Roach     *
18518ba2e626SGreg Roach     * @return void
1852a6f13a4aSGreg Roach     */
1853b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1854c1010edaSGreg Roach    {
1855a6f13a4aSGreg Roach        $this->process_repeats++;
1856a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1857a6f13a4aSGreg Roach            return;
1858a6f13a4aSGreg Roach        }
1859a6f13a4aSGreg Roach
186013abd6f3SGreg Roach        $match = [];
1861a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1862a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1863a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1864d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1865a6f13a4aSGreg Roach                $sortby = trim($sortby);
1866a6f13a4aSGreg Roach            }
1867a6f13a4aSGreg Roach        } else {
18687a6ee1acSGreg Roach            $sortby = 'NAME';
1869a6f13a4aSGreg Roach        }
1870a6f13a4aSGreg Roach
1871e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1872195b5e75SGreg Roach
1873a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1874a6f13a4aSGreg Roach        switch ($listname) {
18757a6ee1acSGreg Roach            case 'pending':
18766c0bef3aSGreg Roach                $xrefs = DB::table('change')
1877195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1878a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1879195b5e75SGreg Roach                            ->from('change')
1880195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1881195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18827f5c2944SGreg Roach                            ->groupBy(['xref']);
1883195b5e75SGreg Roach                    })
18846c0bef3aSGreg Roach                    ->pluck('xref');
1885195b5e75SGreg Roach
188613abd6f3SGreg Roach                $this->list = [];
18876c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18886b9cb339SGreg Roach                    $this->list[] = Registry::gedcomRecordFactory()->make($xref, $this->tree);
1889a6f13a4aSGreg Roach                }
1890a6f13a4aSGreg Roach                break;
1891a6f13a4aSGreg Roach            case 'individual':
18925985adfbSGreg Roach                $query = DB::table('individuals')
18935985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18945985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18955985adfbSGreg Roach                    ->distinct();
18965985adfbSGreg Roach
1897a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1898dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
189982759250SGreg Roach                        $value = $this->substituteVars($value, false);
1900a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1901a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19020b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19035985adfbSGreg Roach                                $join
19045985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19055985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19065985adfbSGreg Roach                            });
19075985adfbSGreg Roach
19085985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19095985adfbSGreg Roach
1910a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19115985adfbSGreg Roach
1912044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19135985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1914a6f13a4aSGreg Roach                            } else {
19155985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1916a6f13a4aSGreg Roach                            }
19175985adfbSGreg Roach
19185985adfbSGreg Roach                            // This filter has been fully processed
19195985adfbSGreg Roach                            unset($attrs[$attr]);
19207ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19210b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19225985adfbSGreg Roach                                $join
19235985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19245985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19255985adfbSGreg Roach                            });
1926a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19277a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19285d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1929b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1930a6f13a4aSGreg Roach                            }
19315985adfbSGreg Roach
19325985adfbSGreg Roach                            // This filter has been fully processed
19335985adfbSGreg Roach                            unset($attrs[$attr]);
1934a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19355985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1936a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19375985adfbSGreg Roach
1938a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19395985adfbSGreg Roach
19405985adfbSGreg Roach                            // This filter has been fully processed
19415985adfbSGreg Roach                            unset($attrs[$attr]);
19422fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19439dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19445985adfbSGreg Roach                            $query
19450b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19465985adfbSGreg Roach                                    $join
19477ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19487ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19495985adfbSGreg Roach                                })
19500b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19517ee4bfadSGreg Roach                                    $join
19527ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19537ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19547ee4bfadSGreg Roach                                })
19559b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19566e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19575985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19587ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19596e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19606e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
196147a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19626e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19636e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1964e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19657ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1966a6f13a4aSGreg Roach                        }
1967a6f13a4aSGreg Roach                    }
1968a6f13a4aSGreg Roach                }
1969a6f13a4aSGreg Roach
197013abd6f3SGreg Roach                $this->list = [];
1971a6f13a4aSGreg Roach
19725985adfbSGreg Roach                foreach ($query->get() as $row) {
19736b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1974a6f13a4aSGreg Roach                }
1975a6f13a4aSGreg Roach                break;
1976a6f13a4aSGreg Roach
1977a6f13a4aSGreg Roach            case 'family':
197830fc2b1eSGreg Roach                $query = DB::table('families')
197930fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
198030fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
198130fc2b1eSGreg Roach                    ->distinct();
198230fc2b1eSGreg Roach
1983a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1984dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
198582759250SGreg Roach                        $value = $this->substituteVars($value, false);
1986a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1987a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19880b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
198930fc2b1eSGreg Roach                                $join
199030fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
199130fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
199230fc2b1eSGreg Roach                            });
199330fc2b1eSGreg Roach
199430fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
199530fc2b1eSGreg Roach
1996a6f13a4aSGreg Roach                            $date = new Date($match[3]);
199730fc2b1eSGreg Roach
1998044416d2SGreg Roach                            if ($match[2] === 'LTE') {
199930fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2000a6f13a4aSGreg Roach                            } else {
200130fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2002a6f13a4aSGreg Roach                            }
200330fc2b1eSGreg Roach
200430fc2b1eSGreg Roach                            // This filter has been fully processed
200530fc2b1eSGreg Roach                            unset($attrs[$attr]);
2006a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
200730fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2008a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
200930fc2b1eSGreg Roach
2010a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
201130fc2b1eSGreg Roach
201230fc2b1eSGreg Roach                            // This filter has been fully processed
201330fc2b1eSGreg Roach                            unset($attrs[$attr]);
201430fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20153b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20160b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
201730fc2b1eSGreg Roach                                    $join
201830fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20193b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
202030fc2b1eSGreg Roach                                            $query
202130fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
202230fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
202330fc2b1eSGreg Roach                                        });
202430fc2b1eSGreg Roach                                });
20255d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20267a6ee1acSGreg Roach                                if ($match[1] != '') {
20277a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20285d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2029b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20305d0bc43dSGreg Roach                                    }
20315d0bc43dSGreg Roach                                }
2032a6f13a4aSGreg Roach                            }
203330fc2b1eSGreg Roach
203430fc2b1eSGreg Roach                            // This filter has been fully processed
203530fc2b1eSGreg Roach                            unset($attrs[$attr]);
20366e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20379dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
203830fc2b1eSGreg Roach                            $query
20390b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
204030fc2b1eSGreg Roach                                    $join
20416e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20426e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
204330fc2b1eSGreg Roach                                })
20440b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20456e5c0963SGreg Roach                                    $join
20466e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20476e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20486e5c0963SGreg Roach                                })
2049b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20506e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
205130fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20526e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20536e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20546e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20556e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20566e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20576e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2058e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20596e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2060a6f13a4aSGreg Roach                        }
2061a6f13a4aSGreg Roach                    }
2062a6f13a4aSGreg Roach                }
2063a6f13a4aSGreg Roach
206413abd6f3SGreg Roach                $this->list = [];
2065a6f13a4aSGreg Roach
206630fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20676b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2068a6f13a4aSGreg Roach                }
2069a6f13a4aSGreg Roach                break;
2070a6f13a4aSGreg Roach
2071a6f13a4aSGreg Roach            default:
20726ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2073a6f13a4aSGreg Roach        }
2074a6f13a4aSGreg Roach
207513abd6f3SGreg Roach        $filters  = [];
207613abd6f3SGreg Roach        $filters2 = [];
2077a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2078a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2079a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2080a6f13a4aSGreg Roach                    $condition = $value;
2081a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2082a6f13a4aSGreg Roach                        $id    = $match[1];
2083a6f13a4aSGreg Roach                        $value = "''";
2084044416d2SGreg Roach                        if ($id === 'ID') {
20857a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2086a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2087a6f13a4aSGreg Roach                            }
2088044416d2SGreg Roach                        } elseif ($id === 'fact') {
2089a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2090044416d2SGreg Roach                        } elseif ($id === 'desc') {
2091a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2092a6f13a4aSGreg Roach                        } else {
2093a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20947a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2095a6f13a4aSGreg Roach                            }
2096a6f13a4aSGreg Roach                        }
2097a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2098a6f13a4aSGreg Roach                    }
2099a6f13a4aSGreg Roach                    //-- handle regular expressions
2100a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2101a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2102a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2103a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2104a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2105d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2106a6f13a4aSGreg Roach                            $val = trim($val);
2107a6f13a4aSGreg Roach                        }
2108a6f13a4aSGreg Roach                        if ($val) {
21097a6ee1acSGreg Roach                            $searchstr = '';
21107a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2111a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2112a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2113a6f13a4aSGreg Roach                                $level = 1;
2114f71a7dedSGreg Roach                                $t = 'XXXX';
2115a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2116a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2117a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2118a6f13a4aSGreg Roach                                    }
2119a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2120044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21217a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2122a6f13a4aSGreg Roach                                    }
21237a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2124a6f13a4aSGreg Roach                                    $level++;
2125a6f13a4aSGreg Roach                                }
2126a6f13a4aSGreg Roach                            } else {
2127044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21287a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2129a6f13a4aSGreg Roach                                }
2130a6f13a4aSGreg Roach                                $t         = $tag;
21317a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2132a6f13a4aSGreg Roach                            }
2133a6f13a4aSGreg Roach                            switch ($expr) {
21347a6ee1acSGreg Roach                                case 'CONTAINS':
2135044416d2SGreg Roach                                    if ($t === 'PLAC') {
2136a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2137a6f13a4aSGreg Roach                                    } else {
2138a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2139a6f13a4aSGreg Roach                                    }
2140a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2141a6f13a4aSGreg Roach                                    break;
2142a6f13a4aSGreg Roach                                default:
2143c1010edaSGreg Roach                                    $filters2[] = [
2144c1010edaSGreg Roach                                        'tag'  => $tag,
2145c1010edaSGreg Roach                                        'expr' => $expr,
2146c1010edaSGreg Roach                                        'val'  => $val,
2147c1010edaSGreg Roach                                    ];
2148a6f13a4aSGreg Roach                                    break;
2149a6f13a4aSGreg Roach                            }
2150a6f13a4aSGreg Roach                        }
2151a6f13a4aSGreg Roach                    }
2152a6f13a4aSGreg Roach                }
2153a6f13a4aSGreg Roach            }
2154a6f13a4aSGreg Roach        }
2155a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2156a6f13a4aSGreg Roach        if ($filters) {
2157a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2158a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2159299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2160a6f13a4aSGreg Roach                        unset($this->list[$key]);
2161a6f13a4aSGreg Roach                        break;
2162a6f13a4aSGreg Roach                    }
2163a6f13a4aSGreg Roach                }
2164a6f13a4aSGreg Roach            }
2165a6f13a4aSGreg Roach        }
2166a6f13a4aSGreg Roach        if ($filters2) {
216713abd6f3SGreg Roach            $mylist = [];
2168a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2169c0935879SGreg Roach                $key  = $indi->xref();
2170299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2171a6f13a4aSGreg Roach                $keep = true;
2172a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2173a6f13a4aSGreg Roach                    if ($keep) {
2174a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2175a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2176a6f13a4aSGreg Roach                        $val  = $filter['val'];
2177b2448a1bSGreg Roach                        if ($val === "''") {
21787a6ee1acSGreg Roach                            $val = '';
2179a6f13a4aSGreg Roach                        }
21807a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2181a6f13a4aSGreg Roach                        $t    = end($tags);
21823d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2183a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2184044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21857a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21867a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2187a6f13a4aSGreg Roach                            $t    = end($tags);
21883d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2189a6f13a4aSGreg Roach                        }
2190a6f13a4aSGreg Roach
2191a6f13a4aSGreg Roach                        switch ($expr) {
21927a6ee1acSGreg Roach                            case 'GTE':
2193044416d2SGreg Roach                                if ($t === 'DATE') {
2194a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2195a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2196a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2197a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2198a6f13a4aSGreg Roach                                    $keep = true;
2199a6f13a4aSGreg Roach                                }
2200a6f13a4aSGreg Roach                                break;
22017a6ee1acSGreg Roach                            case 'LTE':
2202044416d2SGreg Roach                                if ($t === 'DATE') {
2203a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2204a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2205a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2206a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2207a6f13a4aSGreg Roach                                    $keep = true;
2208a6f13a4aSGreg Roach                                }
2209a6f13a4aSGreg Roach                                break;
2210a6f13a4aSGreg Roach                            default:
2211a6f13a4aSGreg Roach                                if ($v == $val) {
2212a6f13a4aSGreg Roach                                    $keep = true;
2213a6f13a4aSGreg Roach                                } else {
2214a6f13a4aSGreg Roach                                    $keep = false;
2215a6f13a4aSGreg Roach                                }
2216a6f13a4aSGreg Roach                                break;
2217a6f13a4aSGreg Roach                        }
2218a6f13a4aSGreg Roach                    }
2219a6f13a4aSGreg Roach                }
2220a6f13a4aSGreg Roach                if ($keep) {
2221a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2222a6f13a4aSGreg Roach                }
2223a6f13a4aSGreg Roach            }
2224a6f13a4aSGreg Roach            $this->list = $mylist;
2225a6f13a4aSGreg Roach        }
2226a6f13a4aSGreg Roach
2227a6f13a4aSGreg Roach        switch ($sortby) {
2228a6f13a4aSGreg Roach            case 'NAME':
2229c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2230a6f13a4aSGreg Roach                break;
2231a6f13a4aSGreg Roach            case 'CHAN':
2232c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2233a6f13a4aSGreg Roach                break;
2234a6f13a4aSGreg Roach            case 'BIRT:DATE':
2235c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2236a6f13a4aSGreg Roach                break;
2237a6f13a4aSGreg Roach            case 'DEAT:DATE':
2238c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2239a6f13a4aSGreg Roach                break;
2240a6f13a4aSGreg Roach            case 'MARR:DATE':
2241c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2242a6f13a4aSGreg Roach                break;
2243a6f13a4aSGreg Roach            default:
2244a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2245a6f13a4aSGreg Roach                break;
2246a6f13a4aSGreg Roach        }
2247a6f13a4aSGreg Roach
22489b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2249e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2250a6f13a4aSGreg Roach    }
2251a6f13a4aSGreg Roach
2252a6f13a4aSGreg Roach    /**
2253fab8f067SGreg Roach     * Handle </list>
22548ba2e626SGreg Roach     *
22558ba2e626SGreg Roach     * @return void
2256a6f13a4aSGreg Roach     */
2257b702978eSGreg Roach    protected function listEndHandler(): void
2258c1010edaSGreg Roach    {
2259a6f13a4aSGreg Roach        $this->process_repeats--;
2260a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2261a6f13a4aSGreg Roach            return;
2262a6f13a4aSGreg Roach        }
2263a6f13a4aSGreg Roach
2264a6f13a4aSGreg Roach        // Check if there is any list
2265a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2266a6f13a4aSGreg Roach            $lineoffset = 0;
2267a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2268a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2269a6f13a4aSGreg Roach            }
2270a6f13a4aSGreg Roach            //-- read the xml from the file
2271299d100dSGreg Roach            $lines = file($this->report);
2272dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2273a6f13a4aSGreg Roach                $lineoffset--;
2274a6f13a4aSGreg Roach            }
2275a6f13a4aSGreg Roach            $lineoffset++;
2276a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2277a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2278a6f13a4aSGreg Roach            // List Level counter
2279a6f13a4aSGreg Roach            $count = 1;
2280a6f13a4aSGreg Roach            while (0 < $count) {
2281dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2282a6f13a4aSGreg Roach                    $count++;
2283dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2284a6f13a4aSGreg Roach                    $count--;
2285a6f13a4aSGreg Roach                }
2286a6f13a4aSGreg Roach                if (0 < $count) {
2287a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2288a6f13a4aSGreg Roach                }
2289a6f13a4aSGreg Roach                $line_nr++;
2290a6f13a4aSGreg Roach            }
2291a6f13a4aSGreg Roach            // No need to drag this
2292a6f13a4aSGreg Roach            unset($lines);
22937a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2294a6f13a4aSGreg Roach            // Save original values
22959b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2296a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2297a6f13a4aSGreg Roach
2298a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2299a6f13a4aSGreg Roach            $this->list_private = 0;
2300a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2301a6f13a4aSGreg Roach                if ($record->canShow()) {
2302f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2303a6f13a4aSGreg Roach                    //-- start the sax parser
2304a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2305e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2306a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23071aa04befSGreg Roach
23081aa04befSGreg Roach                    xml_set_element_handler(
23091aa04befSGreg Roach                        $repeat_parser,
23109d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23111aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23121aa04befSGreg Roach                        },
23139d454b6bSGreg Roach                        function ($parser, string $name): void {
23141aa04befSGreg Roach                            $this->endElement($parser, $name);
23151aa04befSGreg Roach                        }
23161aa04befSGreg Roach                    );
23171aa04befSGreg Roach
23181aa04befSGreg Roach                    xml_set_character_data_handler(
23191aa04befSGreg Roach                        $repeat_parser,
23209d454b6bSGreg Roach                        function ($parser, string $data): void {
23211aa04befSGreg Roach                            $this->characterData($parser, $data);
23221aa04befSGreg Roach                        }
23231aa04befSGreg Roach                    );
23241aa04befSGreg Roach
2325a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23266ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2327a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2328a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2329a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2330a6f13a4aSGreg Roach                        ));
2331a6f13a4aSGreg Roach                    }
2332a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2333a6f13a4aSGreg Roach                } else {
2334a6f13a4aSGreg Roach                    $this->list_private++;
2335a6f13a4aSGreg Roach                }
2336a6f13a4aSGreg Roach            }
233713abd6f3SGreg Roach            $this->list   = [];
2338e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2339a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2340a6f13a4aSGreg Roach        }
234165e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2342a6f13a4aSGreg Roach    }
2343a6f13a4aSGreg Roach
2344a6f13a4aSGreg Roach    /**
2345fab8f067SGreg Roach     * Handle <listTotal>
2346a6f13a4aSGreg Roach     * Prints the total number of records in a list
2347fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23488ba2e626SGreg Roach     *
23498ba2e626SGreg Roach     * @return void
2350a6f13a4aSGreg Roach     */
2351b702978eSGreg Roach    protected function listTotalStartHandler(): void
2352c1010edaSGreg Roach    {
2353a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2354589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2355a6f13a4aSGreg Roach        } else {
23567a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2357a6f13a4aSGreg Roach        }
2358a6f13a4aSGreg Roach    }
2359a6f13a4aSGreg Roach
2360a6f13a4aSGreg Roach    /**
2361fab8f067SGreg Roach     * Handle <relatives>
236276692c8bSGreg Roach     *
2363fab8f067SGreg Roach     * @param string[] $attrs
23648ba2e626SGreg Roach     *
23658ba2e626SGreg Roach     * @return void
2366a6f13a4aSGreg Roach     */
2367b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2368c1010edaSGreg Roach    {
2369a6f13a4aSGreg Roach        $this->process_repeats++;
2370a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2371a6f13a4aSGreg Roach            return;
2372a6f13a4aSGreg Roach        }
2373a6f13a4aSGreg Roach
2374e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2375e364afe4SGreg Roach
237613abd6f3SGreg Roach        $match = [];
2377a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2378d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2379a6f13a4aSGreg Roach            $sortby = trim($sortby);
2380a6f13a4aSGreg Roach        }
2381a6f13a4aSGreg Roach
2382a6f13a4aSGreg Roach        $maxgen = -1;
2383a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2384c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2385a6f13a4aSGreg Roach        }
2386a6f13a4aSGreg Roach
2387e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2388e364afe4SGreg Roach
2389a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2390d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2391a6f13a4aSGreg Roach            $group = trim($group);
2392a6f13a4aSGreg Roach        }
2393a6f13a4aSGreg Roach
2394e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2395e364afe4SGreg Roach
2396a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2397d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2398a6f13a4aSGreg Roach            $id = trim($id);
2399a6f13a4aSGreg Roach        }
2400a6f13a4aSGreg Roach
240113abd6f3SGreg Roach        $this->list = [];
24026b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2403d965cc1aSGreg Roach        if ($person instanceof Individual) {
2404a6f13a4aSGreg Roach            $this->list[$id] = $person;
2405a6f13a4aSGreg Roach            switch ($group) {
24067a6ee1acSGreg Roach                case 'child-family':
240739ca88baSGreg Roach                    foreach ($person->childFamilies() 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 'spouse-family':
241839ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2419820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2420820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2421a6f13a4aSGreg Roach                        }
2422820b62dfSGreg Roach
2423820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2424c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2425a6f13a4aSGreg Roach                        }
2426a6f13a4aSGreg Roach                    }
2427a6f13a4aSGreg Roach                    break;
24287a6ee1acSGreg Roach                case 'direct-ancestors':
24293d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2430a6f13a4aSGreg Roach                    break;
24317a6ee1acSGreg Roach                case 'ancestors':
24323d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2433a6f13a4aSGreg Roach                    break;
24347a6ee1acSGreg Roach                case 'descendants':
2435a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24363d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2437a6f13a4aSGreg Roach                    break;
24387a6ee1acSGreg Roach                case 'all':
24393d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24403d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2441a6f13a4aSGreg Roach                    break;
2442a6f13a4aSGreg Roach            }
2443a6f13a4aSGreg Roach        }
2444a6f13a4aSGreg Roach
2445a6f13a4aSGreg Roach        switch ($sortby) {
2446a6f13a4aSGreg Roach            case 'NAME':
2447c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2448a6f13a4aSGreg Roach                break;
2449a6f13a4aSGreg Roach            case 'BIRT:DATE':
2450c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2451a6f13a4aSGreg Roach                break;
2452a6f13a4aSGreg Roach            case 'DEAT:DATE':
2453c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2454a6f13a4aSGreg Roach                break;
2455a6f13a4aSGreg Roach            case 'generation':
245613abd6f3SGreg Roach                $newarray = [];
2457a6f13a4aSGreg Roach                reset($this->list);
2458a6f13a4aSGreg Roach                $genCounter = 1;
2459a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2460a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2461a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2462a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
246379529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2464a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2465a6f13a4aSGreg Roach                        }
2466a6f13a4aSGreg Roach                    }
2467a6f13a4aSGreg Roach                    $genCounter++;
2468a6f13a4aSGreg Roach                }
2469a6f13a4aSGreg Roach                $this->list = $newarray;
2470a6f13a4aSGreg Roach                break;
2471a6f13a4aSGreg Roach            default:
2472a6f13a4aSGreg Roach                // unsorted
2473a6f13a4aSGreg Roach                break;
2474a6f13a4aSGreg Roach        }
24759b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2476e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2477a6f13a4aSGreg Roach    }
2478a6f13a4aSGreg Roach
2479a6f13a4aSGreg Roach    /**
2480fab8f067SGreg Roach     * Handle </relatives>
24818ba2e626SGreg Roach     *
24828ba2e626SGreg Roach     * @return void
2483a6f13a4aSGreg Roach     */
2484b702978eSGreg Roach    protected function relativesEndHandler(): void
2485c1010edaSGreg Roach    {
2486a6f13a4aSGreg Roach        $this->process_repeats--;
2487a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2488a6f13a4aSGreg Roach            return;
2489a6f13a4aSGreg Roach        }
2490a6f13a4aSGreg Roach
2491a6f13a4aSGreg Roach        // Check if there is any relatives
2492a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2493a6f13a4aSGreg Roach            $lineoffset = 0;
2494a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2495a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2496a6f13a4aSGreg Roach            }
2497a6f13a4aSGreg Roach            //-- read the xml from the file
2498299d100dSGreg Roach            $lines = file($this->report);
2499dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2500a6f13a4aSGreg Roach                $lineoffset--;
2501a6f13a4aSGreg Roach            }
2502a6f13a4aSGreg Roach            $lineoffset++;
2503a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2504a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2505a6f13a4aSGreg Roach            // Relatives Level counter
2506a6f13a4aSGreg Roach            $count = 1;
2507a6f13a4aSGreg Roach            while (0 < $count) {
2508dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2509a6f13a4aSGreg Roach                    $count++;
2510dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2511a6f13a4aSGreg Roach                    $count--;
2512a6f13a4aSGreg Roach                }
2513a6f13a4aSGreg Roach                if (0 < $count) {
2514a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2515a6f13a4aSGreg Roach                }
2516a6f13a4aSGreg Roach                $line_nr++;
2517a6f13a4aSGreg Roach            }
2518a6f13a4aSGreg Roach            // No need to drag this
2519a6f13a4aSGreg Roach            unset($lines);
2520a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2521a6f13a4aSGreg Roach            // Save original values
25229b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2523a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2524a6f13a4aSGreg Roach
2525a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2526a6f13a4aSGreg Roach            $this->list_private = 0;
2527b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2528a6f13a4aSGreg Roach                if (isset($value->generation)) {
2529a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2530a6f13a4aSGreg Roach                }
25316b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2532299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2533a6f13a4aSGreg Roach
2534a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2535e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2536a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25371aa04befSGreg Roach
25381aa04befSGreg Roach                xml_set_element_handler(
25391aa04befSGreg Roach                    $repeat_parser,
25409d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25411aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25421aa04befSGreg Roach                    },
25439d454b6bSGreg Roach                    function ($parser, string $name): void {
25441aa04befSGreg Roach                        $this->endElement($parser, $name);
25451aa04befSGreg Roach                    }
25461aa04befSGreg Roach                );
25471aa04befSGreg Roach
25481aa04befSGreg Roach                xml_set_character_data_handler(
25491aa04befSGreg Roach                    $repeat_parser,
25509d454b6bSGreg Roach                    function ($parser, string $data): void {
25511aa04befSGreg Roach                        $this->characterData($parser, $data);
25521aa04befSGreg Roach                    }
25531aa04befSGreg Roach                );
2554a6f13a4aSGreg Roach
2555a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25566ccdf4f0SGreg 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)));
2557a6f13a4aSGreg Roach                }
2558a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2559a6f13a4aSGreg Roach            }
2560a6f13a4aSGreg Roach            // Clean up the list array
256113abd6f3SGreg Roach            $this->list   = [];
2562e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2563a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2564a6f13a4aSGreg Roach        }
256565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2566a6f13a4aSGreg Roach    }
2567a6f13a4aSGreg Roach
2568a6f13a4aSGreg Roach    /**
2569fab8f067SGreg Roach     * Handle <generation />
2570a6f13a4aSGreg Roach     * Prints the number of generations
25718ba2e626SGreg Roach     *
25728ba2e626SGreg Roach     * @return void
2573a6f13a4aSGreg Roach     */
2574b702978eSGreg Roach    protected function generationStartHandler(): void
2575c1010edaSGreg Roach    {
2576589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2577a6f13a4aSGreg Roach    }
2578a6f13a4aSGreg Roach
2579a6f13a4aSGreg Roach    /**
2580fab8f067SGreg Roach     * Handle <newPage />
2581a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
25828ba2e626SGreg Roach     *
25838ba2e626SGreg Roach     * @return void
2584a6f13a4aSGreg Roach     */
2585b702978eSGreg Roach    protected function newPageStartHandler(): void
2586c1010edaSGreg Roach    {
25877a6ee1acSGreg Roach        $temp = 'addpage';
2588e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2589a6f13a4aSGreg Roach    }
2590a6f13a4aSGreg Roach
2591a6f13a4aSGreg Roach    /**
2592fab8f067SGreg Roach     * Handle </title>
25938ba2e626SGreg Roach     *
25948ba2e626SGreg Roach     * @return void
2595a6f13a4aSGreg Roach     */
2596b702978eSGreg Roach    protected function titleEndHandler(): void
2597c1010edaSGreg Roach    {
25982836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2599a6f13a4aSGreg Roach    }
2600a6f13a4aSGreg Roach
2601a6f13a4aSGreg Roach    /**
2602fab8f067SGreg Roach     * Handle </description>
26038ba2e626SGreg Roach     *
26048ba2e626SGreg Roach     * @return void
2605a6f13a4aSGreg Roach     */
2606b702978eSGreg Roach    protected function descriptionEndHandler(): void
2607c1010edaSGreg Roach    {
26082836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2609a6f13a4aSGreg Roach    }
2610729ce104SGreg Roach
2611729ce104SGreg Roach    /**
261276692c8bSGreg Roach     * Create a list of all descendants.
261376692c8bSGreg Roach     *
2614729ce104SGreg Roach     * @param string[] $list
2615729ce104SGreg Roach     * @param string   $pid
2616729ce104SGreg Roach     * @param bool     $parents
2617729ce104SGreg Roach     * @param int      $generations
26188ba2e626SGreg Roach     *
26198ba2e626SGreg Roach     * @return void
2620729ce104SGreg Roach     */
26213b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2622c1010edaSGreg Roach    {
26236b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2624729ce104SGreg Roach        if ($person === null) {
2625729ce104SGreg Roach            return;
2626729ce104SGreg Roach        }
2627729ce104SGreg Roach        if (!isset($list[$pid])) {
2628729ce104SGreg Roach            $list[$pid] = $person;
2629729ce104SGreg Roach        }
2630729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2631729ce104SGreg Roach            $list[$pid]->generation = 0;
2632729ce104SGreg Roach        }
263339ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2634729ce104SGreg Roach            if ($parents) {
263539ca88baSGreg Roach                $husband = $family->husband();
263639ca88baSGreg Roach                $wife    = $family->wife();
2637729ce104SGreg Roach                if ($husband) {
2638c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2639729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2640c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2641729ce104SGreg Roach                    } else {
2642c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2643729ce104SGreg Roach                    }
2644729ce104SGreg Roach                }
2645729ce104SGreg Roach                if ($wife) {
2646c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2647729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2648c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2649729ce104SGreg Roach                    } else {
2650c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2651729ce104SGreg Roach                    }
2652729ce104SGreg Roach                }
2653729ce104SGreg Roach            }
2654820b62dfSGreg Roach
265539ca88baSGreg Roach            $children = $family->children();
2656820b62dfSGreg Roach
2657729ce104SGreg Roach            foreach ($children as $child) {
2658729ce104SGreg Roach                if ($child) {
2659c0935879SGreg Roach                    $list[$child->xref()] = $child;
2660820b62dfSGreg Roach
2661729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2662c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2663729ce104SGreg Roach                    } else {
2664c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2665729ce104SGreg Roach                    }
2666729ce104SGreg Roach                }
2667729ce104SGreg Roach            }
2668729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2669729ce104SGreg Roach                foreach ($children as $child) {
2670c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2671729ce104SGreg Roach                }
2672729ce104SGreg Roach            }
2673729ce104SGreg Roach        }
2674729ce104SGreg Roach    }
2675729ce104SGreg Roach
2676729ce104SGreg Roach    /**
267776692c8bSGreg Roach     * Create a list of all ancestors.
267876692c8bSGreg Roach     *
2679870ec5a3SGreg Roach     * @param array<stdClass> $list
2680729ce104SGreg Roach     * @param string          $pid
2681729ce104SGreg Roach     * @param bool            $children
2682729ce104SGreg Roach     * @param int             $generations
26838ba2e626SGreg Roach     *
26848ba2e626SGreg Roach     * @return void
2685729ce104SGreg Roach     */
26863b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2687c1010edaSGreg Roach    {
268813abd6f3SGreg Roach        $genlist                = [$pid];
2689729ce104SGreg Roach        $list[$pid]->generation = 1;
2690729ce104SGreg Roach        while (count($genlist) > 0) {
2691729ce104SGreg Roach            $id = array_shift($genlist);
2692dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2693729ce104SGreg Roach                continue; // id can be something like “empty7”
2694729ce104SGreg Roach            }
26956b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
269639ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
269739ca88baSGreg Roach                $husband = $family->husband();
269839ca88baSGreg Roach                $wife    = $family->wife();
2699729ce104SGreg Roach                if ($husband) {
2700c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2701c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2702729ce104SGreg Roach                }
2703729ce104SGreg Roach                if ($wife) {
2704c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2705c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2706729ce104SGreg Roach                }
2707729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2708729ce104SGreg Roach                    if ($husband) {
2709c0935879SGreg Roach                        $genlist[] = $husband->xref();
2710729ce104SGreg Roach                    }
2711729ce104SGreg Roach                    if ($wife) {
2712c0935879SGreg Roach                        $genlist[] = $wife->xref();
2713729ce104SGreg Roach                    }
2714729ce104SGreg Roach                }
2715729ce104SGreg Roach                if ($children) {
271639ca88baSGreg Roach                    foreach ($family->children() as $child) {
2717c0935879SGreg Roach                        $list[$child->xref()] = $child;
2718e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2719729ce104SGreg Roach                    }
2720729ce104SGreg Roach                }
2721729ce104SGreg Roach            }
2722729ce104SGreg Roach        }
2723729ce104SGreg Roach    }
2724729ce104SGreg Roach
2725729ce104SGreg Roach    /**
2726729ce104SGreg Roach     * get gedcom tag value
2727729ce104SGreg Roach     *
2728729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2729729ce104SGreg 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
2730729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2731729ce104SGreg Roach     *
2732729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2733729ce104SGreg Roach     */
2734b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2735c1010edaSGreg Roach    {
2736b2448a1bSGreg Roach        if ($gedrec === '') {
2737729ce104SGreg Roach            return '';
2738729ce104SGreg Roach        }
2739729ce104SGreg Roach        $tags      = explode(':', $tag);
2740729ce104SGreg Roach        $origlevel = $level;
2741b2448a1bSGreg Roach        if ($level === 0) {
27423c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2743729ce104SGreg Roach        }
2744729ce104SGreg Roach
2745729ce104SGreg Roach        $subrec = $gedrec;
2746f71a7dedSGreg Roach        $t = 'XXXX';
2747729ce104SGreg Roach        foreach ($tags as $t) {
2748729ce104SGreg Roach            $lastsubrec = $subrec;
27493d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2750729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2751729ce104SGreg Roach                $level--;
27523d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2753729ce104SGreg Roach            }
2754729ce104SGreg Roach            if (empty($subrec)) {
2755044416d2SGreg Roach                if ($t === 'TITL') {
27563d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2757729ce104SGreg Roach                    if (!empty($subrec)) {
27587a6ee1acSGreg Roach                        $t = 'ABBR';
2759729ce104SGreg Roach                    }
2760729ce104SGreg Roach                }
2761b2448a1bSGreg Roach                if ($subrec === '') {
2762729ce104SGreg Roach                    if ($level > 0) {
2763729ce104SGreg Roach                        $level--;
2764729ce104SGreg Roach                    }
27653d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2766b2448a1bSGreg Roach                    if ($subrec === '') {
2767729ce104SGreg Roach                        return '';
2768729ce104SGreg Roach                    }
2769729ce104SGreg Roach                }
2770729ce104SGreg Roach            }
2771729ce104SGreg Roach            $level++;
2772729ce104SGreg Roach        }
2773729ce104SGreg Roach        $level--;
2774729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2775f71a7dedSGreg Roach        if ($ct === 0) {
2776729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2777729ce104SGreg Roach        }
2778f71a7dedSGreg Roach        if ($ct === 0) {
2779729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2780729ce104SGreg Roach        }
2781729ce104SGreg Roach        if ($ct > 0) {
2782729ce104SGreg Roach            $value = trim($match[1]);
2783044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
27846b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2785ff166e64SGreg Roach                if ($note instanceof Note) {
2786729ce104SGreg Roach                    $value = $note->getNote();
2787729ce104SGreg Roach                } else {
2788729ce104SGreg Roach                    //-- set the value to the id without the @
2789729ce104SGreg Roach                    $value = $match[1];
2790729ce104SGreg Roach                }
2791729ce104SGreg Roach            }
2792f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
27933d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2794729ce104SGreg Roach            }
2795729ce104SGreg Roach
2796729ce104SGreg Roach            return $value;
2797729ce104SGreg Roach        }
2798729ce104SGreg Roach
27997a6ee1acSGreg Roach        return '';
2800729ce104SGreg Roach    }
2801d1286247SGreg Roach
2802d1286247SGreg Roach    /**
2803d1286247SGreg Roach     * Replace variable identifiers with their values.
2804d1286247SGreg Roach     *
2805d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
280682759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2807d1286247SGreg Roach     *
2808d1286247SGreg Roach     * @return string
2809d1286247SGreg Roach     */
28108f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2811c1010edaSGreg Roach    {
2812d1286247SGreg Roach        return preg_replace_callback(
2813d1286247SGreg Roach            '/\$(\w+)/',
281418d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28152118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
281682759250SGreg Roach                    if ($quote) {
28172118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2818b2ce94c6SRico Sonntag                    }
2819b2ce94c6SRico Sonntag
28202118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
282182759250SGreg Roach                }
2822b2ce94c6SRico Sonntag
2823d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28243d7a8a4cSGreg Roach
2825d1286247SGreg Roach                return '$' . $matches[1];
2826d1286247SGreg Roach            },
2827d1286247SGreg Roach            $expression
2828d1286247SGreg Roach        );
2829d1286247SGreg Roach    }
2830a6f13a4aSGreg Roach}
2831