xref: /webtrees/app/Report/ReportParserGenerate.php (revision 9458f20a3c5e8e676f65488153a8d8a76b1773fb)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
24a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
2597def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
26ab2c9152SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
289a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
32d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
33a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
34729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
35a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
36d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
37299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
38195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
39195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
40a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
415985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
429a9e551aSGreg Roachuse Illuminate\Support\Str;
43f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
4431d9777aSGreg Roachuse LogicException;
45c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
465809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
474e69366aSGreg Roachuse XMLParser;
4859597b37SGreg Roach
49b6f35a76SGreg Roachuse function addcslashes;
50b6f35a76SGreg Roachuse function addslashes;
51b6f35a76SGreg Roachuse function array_pop;
52b6f35a76SGreg Roachuse function array_shift;
53b6f35a76SGreg Roachuse function assert;
54b19e047dSGreg Roachuse function call_user_func;
55b6f35a76SGreg Roachuse function count;
56b6f35a76SGreg Roachuse function end;
57b2448a1bSGreg Roachuse function explode;
58b6f35a76SGreg Roachuse function file;
59b6f35a76SGreg Roachuse function file_exists;
60b6f35a76SGreg Roachuse function getimagesize;
6129518ad2SGreg Roachuse function imagecreatefromstring;
6229518ad2SGreg Roachuse function imagesx;
6329518ad2SGreg Roachuse function imagesy;
64b6f35a76SGreg Roachuse function in_array;
65b19e047dSGreg Roachuse function method_exists;
66b6f35a76SGreg Roachuse function preg_match;
67b6f35a76SGreg Roachuse function preg_match_all;
68b6f35a76SGreg Roachuse function preg_replace;
69b6f35a76SGreg Roachuse function preg_replace_callback;
70b6f35a76SGreg Roachuse function preg_split;
71b6f35a76SGreg Roachuse function reset;
72b6f35a76SGreg Roachuse function round;
73b6f35a76SGreg Roachuse function sprintf;
74dec352c1SGreg Roachuse function str_contains;
75b6f35a76SGreg Roachuse function str_replace;
76dec352c1SGreg Roachuse function str_starts_with;
77b6f35a76SGreg Roachuse function strip_tags;
78b6f35a76SGreg Roachuse function strlen;
79b6f35a76SGreg Roachuse function strtoupper;
80b6f35a76SGreg Roachuse function substr;
81b2448a1bSGreg Roachuse function trim;
82b6f35a76SGreg Roachuse function uasort;
83b6f35a76SGreg Roachuse function xml_error_string;
84b6f35a76SGreg Roachuse function xml_get_current_line_number;
85b6f35a76SGreg Roachuse function xml_get_error_code;
86b6f35a76SGreg Roachuse function xml_parse;
87b6f35a76SGreg Roachuse function xml_parser_create;
88b6f35a76SGreg Roachuse function xml_parser_free;
89b6f35a76SGreg Roachuse function xml_parser_set_option;
90b6f35a76SGreg Roachuse function xml_set_character_data_handler;
91b6f35a76SGreg Roachuse function xml_set_element_handler;
92b6f35a76SGreg Roach
93b6f35a76SGreg Roachuse const PREG_SET_ORDER;
94b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9529518ad2SGreg Roach
96a6f13a4aSGreg Roach/**
97a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
98a6f13a4aSGreg Roach */
99c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
100c1010edaSGreg Roach{
101a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
102a6f13a4aSGreg Roach    private $process_footnote = true;
103a6f13a4aSGreg Roach
104a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
105a6f13a4aSGreg Roach    private $print_data = false;
106a6f13a4aSGreg Roach
107a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
10813abd6f3SGreg Roach    private $print_data_stack = [];
109a6f13a4aSGreg Roach
11076692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
111a6f13a4aSGreg Roach    private $process_gedcoms = 0;
112a6f13a4aSGreg Roach
11376692c8bSGreg Roach    /** @var int Are we processing conditionals */
114a6f13a4aSGreg Roach    private $process_ifs = 0;
115a6f13a4aSGreg Roach
11676692c8bSGreg Roach    /** @var int Are we processing repeats */
117a6f13a4aSGreg Roach    private $process_repeats = 0;
118a6f13a4aSGreg Roach
119a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
120a6f13a4aSGreg Roach    private $repeat_bytes = 0;
121a6f13a4aSGreg Roach
12209482a55SGreg Roach    /** @var array<string> Repeated data when iterating over loops */
12309482a55SGreg Roach    private array $repeats = [];
124a6f13a4aSGreg Roach
12509482a55SGreg Roach    /** @var array<int,array<int,array<string>|int>> Nested repeating data */
12609482a55SGreg Roach    private array $repeats_stack = [];
127a6f13a4aSGreg Roach
12809482a55SGreg Roach    /** @var array<AbstractRenderer> Nested repeating data */
12909482a55SGreg Roach    private array $wt_report_stack = [];
130e8e7866bSGreg Roach
1314e69366aSGreg Roach    /** @var XMLParser (resource before PHP 8.0) Nested repeating data */
132e8e7866bSGreg Roach    private $parser;
133e8e7866bSGreg Roach
1344e69366aSGreg Roach    /** @var XMLParser[] (resource[] before PHP 8.0) Nested repeating data */
1354e69366aSGreg Roach    private array $parser_stack = [];
136e8e7866bSGreg Roach
137a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
138a6f13a4aSGreg Roach    private $gedrec = '';
139a6f13a4aSGreg Roach
14009482a55SGreg Roach    /** @var array<string> Nested GEDCOM records */
1414e69366aSGreg Roach    private array $gedrec_stack = [];
142a6f13a4aSGreg Roach
143a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
144a6f13a4aSGreg Roach    private $current_element;
145a6f13a4aSGreg Roach
146a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
147a6f13a4aSGreg Roach    private $footnote_element;
148a6f13a4aSGreg Roach
149a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
150a6f13a4aSGreg Roach    private $fact = '';
151a6f13a4aSGreg Roach
152a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
153a6f13a4aSGreg Roach    private $desc = '';
154a6f13a4aSGreg Roach
155a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
156a6f13a4aSGreg Roach    private $type = '';
157a6f13a4aSGreg Roach
158a6f13a4aSGreg Roach    /** @var int The current generational level */
159a6f13a4aSGreg Roach    private $generation = 1;
160a6f13a4aSGreg Roach
16176d39c55SGreg Roach    /** @var array<static|GedcomRecord> Source data for processing lists */
1624e69366aSGreg Roach    private array $list = [];
163a6f13a4aSGreg Roach
164a6f13a4aSGreg Roach    /** @var int Number of items in lists */
165a6f13a4aSGreg Roach    private $list_total = 0;
166a6f13a4aSGreg Roach
167a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
168a6f13a4aSGreg Roach    private $list_private = 0;
169a6f13a4aSGreg Roach
170299d100dSGreg Roach    /** @var string The filename of the XML report */
171299d100dSGreg Roach    protected $report;
172299d100dSGreg Roach
173b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
174e8e7866bSGreg Roach    private $report_root;
175e8e7866bSGreg Roach
176b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
177e8e7866bSGreg Roach    private $wt_report;
178e8e7866bSGreg Roach
17909482a55SGreg Roach    /** @var array<array<string>> Variables defined in the report at run-time */
1804e69366aSGreg Roach    private array $vars;
181d1286247SGreg Roach
1824e69366aSGreg Roach    private Tree $tree;
183299d100dSGreg Roach
18476692c8bSGreg Roach    /**
18576692c8bSGreg Roach     * Create a parser for a report
18676692c8bSGreg Roach     *
18776692c8bSGreg Roach     * @param string               $report The XML filename
188b6f35a76SGreg Roach     * @param AbstractRenderer     $report_root
18909482a55SGreg Roach     * @param array<array<string>> $vars
190299d100dSGreg Roach     * @param Tree                 $tree
19176692c8bSGreg Roach     */
192*9458f20aSGreg Roach    public function __construct(string $report, AbstractRenderer $report_root, array $vars, Tree $tree)
193*9458f20aSGreg Roach    {
194299d100dSGreg Roach        $this->report          = $report;
195e8e7866bSGreg Roach        $this->report_root     = $report_root;
196e8e7866bSGreg Roach        $this->wt_report       = $report_root;
19759f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
198d1286247SGreg Roach        $this->vars            = $vars;
199299d100dSGreg Roach        $this->tree            = $tree;
200299d100dSGreg Roach
20176f666f4SGreg Roach        parent::__construct($report);
202a6f13a4aSGreg Roach    }
203a6f13a4aSGreg Roach
204a6f13a4aSGreg Roach    /**
205b315f3e1SGreg Roach     * get a gedcom subrecord
206b315f3e1SGreg Roach     *
207b315f3e1SGreg Roach     * searches a gedcom record and returns a subrecord of it. A subrecord is defined starting at a
208b315f3e1SGreg Roach     * line with level N and all subsequent lines greater than N until the next N level is reached.
209b315f3e1SGreg Roach     * For example, the following is a BIRT subrecord:
210b315f3e1SGreg Roach     * <code>1 BIRT
211b315f3e1SGreg Roach     * 2 DATE 1 JAN 1900
212b315f3e1SGreg Roach     * 2 PLAC Phoenix, Maricopa, Arizona</code>
213b315f3e1SGreg Roach     * The following example is the DATE subrecord of the above BIRT subrecord:
214b315f3e1SGreg Roach     * <code>2 DATE 1 JAN 1900</code>
215b315f3e1SGreg Roach     *
216b315f3e1SGreg Roach     * @param int    $level   the N level of the subrecord to get
217b315f3e1SGreg Roach     * @param string $tag     a gedcom tag or string to search for in the record (ie 1 BIRT or 2 DATE)
218b315f3e1SGreg Roach     * @param string $gedrec  the parent gedcom record to search in
219b315f3e1SGreg Roach     * @param int    $num     this allows you to specify which matching <var>$tag</var> to get. Oftentimes a
220b315f3e1SGreg Roach     *                        gedcom record will have more that 1 of the same type of subrecord. An individual may have
221b315f3e1SGreg Roach     *                        multiple events for example. Passing $num=1 would get the first 1. Passing $num=2 would get the
222b315f3e1SGreg Roach     *                        second one, etc.
223b315f3e1SGreg Roach     *
224b315f3e1SGreg Roach     * @return string the subrecord that was found or an empty string "" if not found.
225b315f3e1SGreg Roach     */
226b315f3e1SGreg Roach    public static function getSubRecord(int $level, string $tag, string $gedrec, int $num = 1): string
227b315f3e1SGreg Roach    {
228b315f3e1SGreg Roach        if ($gedrec === '') {
229b315f3e1SGreg Roach            return '';
230b315f3e1SGreg Roach        }
231b315f3e1SGreg Roach        // -- adding \n before and after gedrec
232b315f3e1SGreg Roach        $gedrec       = "\n" . $gedrec . "\n";
233b315f3e1SGreg Roach        $tag          = trim($tag);
234b315f3e1SGreg Roach        $searchTarget = "~[\n]" . $tag . "[\s]~";
235b315f3e1SGreg Roach        $ct           = preg_match_all($searchTarget, $gedrec, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
236b315f3e1SGreg Roach        if ($ct === 0) {
237b315f3e1SGreg Roach            return '';
238b315f3e1SGreg Roach        }
239b315f3e1SGreg Roach        if ($ct < $num) {
240b315f3e1SGreg Roach            return '';
241b315f3e1SGreg Roach        }
242b315f3e1SGreg Roach        $pos1 = $match[$num - 1][0][1];
243b315f3e1SGreg Roach        $pos2 = strpos($gedrec, "\n$level", $pos1 + 1);
244b315f3e1SGreg Roach        if (!$pos2) {
245b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\n1", $pos1 + 1);
246b315f3e1SGreg Roach        }
247b315f3e1SGreg Roach        if (!$pos2) {
248b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\nWT_", $pos1 + 1); // WT_SPOUSE, WT_FAMILY_ID ...
249b315f3e1SGreg Roach        }
250b315f3e1SGreg Roach        if (!$pos2) {
251b315f3e1SGreg Roach            return ltrim(substr($gedrec, $pos1));
252b315f3e1SGreg Roach        }
253b315f3e1SGreg Roach        $subrec = substr($gedrec, $pos1, $pos2 - $pos1);
254b315f3e1SGreg Roach
255b315f3e1SGreg Roach        return ltrim($subrec);
256b315f3e1SGreg Roach    }
257b315f3e1SGreg Roach
258b315f3e1SGreg Roach    /**
259b315f3e1SGreg Roach     * get CONT lines
260b315f3e1SGreg Roach     *
261b315f3e1SGreg Roach     * get the N+1 CONT or CONC lines of a gedcom subrecord
262b315f3e1SGreg Roach     *
263b315f3e1SGreg Roach     * @param int    $nlevel the level of the CONT lines to get
264b315f3e1SGreg Roach     * @param string $nrec   the gedcom subrecord to search in
265b315f3e1SGreg Roach     *
266b315f3e1SGreg Roach     * @return string a string with all CONT lines merged
267b315f3e1SGreg Roach     */
268b315f3e1SGreg Roach    public static function getCont(int $nlevel, string $nrec): string
269b315f3e1SGreg Roach    {
270b315f3e1SGreg Roach        $text = '';
271b315f3e1SGreg Roach
272b315f3e1SGreg Roach        $subrecords = explode("\n", $nrec);
273b315f3e1SGreg Roach        foreach ($subrecords as $thisSubrecord) {
274b315f3e1SGreg Roach            if (substr($thisSubrecord, 0, 2) !== $nlevel . ' ') {
275b315f3e1SGreg Roach                continue;
276b315f3e1SGreg Roach            }
277b315f3e1SGreg Roach            $subrecordType = substr($thisSubrecord, 2, 4);
278b315f3e1SGreg Roach            if ($subrecordType === 'CONT') {
279b315f3e1SGreg Roach                $text .= "\n" . substr($thisSubrecord, 7);
280b315f3e1SGreg Roach            }
281b315f3e1SGreg Roach        }
282b315f3e1SGreg Roach
283b315f3e1SGreg Roach        return $text;
284b315f3e1SGreg Roach    }
285b315f3e1SGreg Roach
286b315f3e1SGreg Roach    /**
287a6f13a4aSGreg Roach     * XML start element handler
288a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
289a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
290a6f13a4aSGreg Roach     *
291a6f13a4aSGreg Roach     * @param resource      $parser the resource handler for the XML parser
292a6f13a4aSGreg Roach     * @param string        $name   the name of the XML element parsed
29309482a55SGreg Roach     * @param array<string> $attrs  an array of key value pairs for the attributes
29418d7a90dSGreg Roach     *
29518d7a90dSGreg Roach     * @return void
296a6f13a4aSGreg Roach     */
297af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
298c1010edaSGreg Roach    {
29913abd6f3SGreg Roach        $newattrs = [];
300a6f13a4aSGreg Roach
301a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
302a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
303e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
304d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
305a6f13a4aSGreg Roach                }
306a6f13a4aSGreg Roach            }
307a6f13a4aSGreg Roach            $newattrs[$key] = $value;
308a6f13a4aSGreg Roach        }
309a6f13a4aSGreg Roach        $attrs = $newattrs;
3107a6ee1acSGreg 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')) {
311a0e2939aSGreg Roach            $method = $name . 'StartHandler';
312208e9f76SGreg Roach
313a0e2939aSGreg Roach            if (method_exists($this, $method)) {
314b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
315a6f13a4aSGreg Roach            }
316a6f13a4aSGreg Roach        }
317a6f13a4aSGreg Roach    }
318a6f13a4aSGreg Roach
319a6f13a4aSGreg Roach    /**
320a6f13a4aSGreg Roach     * XML end element handler
321a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
322a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
323a6f13a4aSGreg Roach     *
324a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
325a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
32618d7a90dSGreg Roach     *
32718d7a90dSGreg Roach     * @return void
328a6f13a4aSGreg Roach     */
329af14d238SGreg Roach    protected function endElement($parser, string $name): void
330c1010edaSGreg Roach    {
3317a6ee1acSGreg 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')) {
332a0e2939aSGreg Roach            $method = $name . 'EndHandler';
333a0e2939aSGreg Roach
334a0e2939aSGreg Roach            if (method_exists($this, $method)) {
335b19e047dSGreg Roach                call_user_func([$this, $method]);
336a6f13a4aSGreg Roach            }
337a6f13a4aSGreg Roach        }
338a6f13a4aSGreg Roach    }
339a6f13a4aSGreg Roach
340a6f13a4aSGreg Roach    /**
341a6f13a4aSGreg Roach     * XML character data handler
342a6f13a4aSGreg Roach     *
343a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
344a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
34518d7a90dSGreg Roach     *
34618d7a90dSGreg Roach     * @return void
347a6f13a4aSGreg Roach     */
34824f2a3afSGreg Roach    protected function characterData($parser, string $data): void
349c1010edaSGreg Roach    {
350e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
351a6f13a4aSGreg Roach            $this->current_element->addText($data);
352a6f13a4aSGreg Roach        }
353a6f13a4aSGreg Roach    }
354a6f13a4aSGreg Roach
355a6f13a4aSGreg Roach    /**
356fab8f067SGreg Roach     * Handle <style>
357a6f13a4aSGreg Roach     *
35809482a55SGreg Roach     * @param array<string> $attrs
3598ba2e626SGreg Roach     *
3608ba2e626SGreg Roach     * @return void
361a6f13a4aSGreg Roach     */
362b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
363c1010edaSGreg Roach    {
364a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
3656ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
366a6f13a4aSGreg Roach        }
367a6f13a4aSGreg Roach
368a6f13a4aSGreg Roach        // array Style that will be passed on
36913abd6f3SGreg Roach        $s = [];
370a6f13a4aSGreg Roach
371a6f13a4aSGreg Roach        // string Name af the style
372a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
373a6f13a4aSGreg Roach
374a6f13a4aSGreg Roach        // string Name of the DEFAULT font
375208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
376a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
377a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
378a6f13a4aSGreg Roach        }
379a6f13a4aSGreg Roach
380a6f13a4aSGreg Roach        // int The size of the font in points
381208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
382a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
38359597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
38459597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
38559597b37SGreg Roach        }
386a6f13a4aSGreg Roach
387a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3887a6ee1acSGreg Roach        $s['style'] = '';
389a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
390a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
391a6f13a4aSGreg Roach        }
392a6f13a4aSGreg Roach
393e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
394a6f13a4aSGreg Roach    }
395a6f13a4aSGreg Roach
396a6f13a4aSGreg Roach    /**
397fab8f067SGreg Roach     * Handle <doc>
398a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
399a6f13a4aSGreg Roach     *
40009482a55SGreg Roach     * @param array<string> $attrs
4018ba2e626SGreg Roach     *
4028ba2e626SGreg Roach     * @return void
403a6f13a4aSGreg Roach     */
404b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
405c1010edaSGreg Roach    {
406e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
407a6f13a4aSGreg Roach
408a6f13a4aSGreg Roach        // Custom page width
409a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
410208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
411a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
412a6f13a4aSGreg Roach        // Custom Page height
413a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
414208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
415a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
416a6f13a4aSGreg Roach
417a6f13a4aSGreg Roach        // Left Margin
418a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
4197a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
420208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
421a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
422208e9f76SGreg 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))
423a6f13a4aSGreg Roach            }
424a6f13a4aSGreg Roach        }
425a6f13a4aSGreg Roach        // Right Margin
426a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
4277a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
428208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
429a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
430208e9f76SGreg 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))
431a6f13a4aSGreg Roach            }
432a6f13a4aSGreg Roach        }
433a6f13a4aSGreg Roach        // Top Margin
434a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
4357a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
436208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
437a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
438208e9f76SGreg 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))
439a6f13a4aSGreg Roach            }
440a6f13a4aSGreg Roach        }
441a6f13a4aSGreg Roach        // Bottom Margin
442a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
4437a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
444208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
445a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
446208e9f76SGreg 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))
447a6f13a4aSGreg Roach            }
448a6f13a4aSGreg Roach        }
449a6f13a4aSGreg Roach        // Header Margin
450a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
4517a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
452208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
453a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
454208e9f76SGreg 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))
455a6f13a4aSGreg Roach            }
456a6f13a4aSGreg Roach        }
457a6f13a4aSGreg Roach        // Footer Margin
458a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
4597a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
460208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
461a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
462208e9f76SGreg 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))
463a6f13a4aSGreg Roach            }
464a6f13a4aSGreg Roach        }
465a6f13a4aSGreg Roach
466a6f13a4aSGreg Roach        // Page Orientation
467a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
468044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4697a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
470044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4717a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
472a6f13a4aSGreg Roach            }
473a6f13a4aSGreg Roach        }
474a6f13a4aSGreg Roach        // Page Size
475a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
476208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
477a6f13a4aSGreg Roach        }
478a6f13a4aSGreg Roach
479a6f13a4aSGreg Roach        // Show Generated By...
480a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4817a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
482208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4837a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
484208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
485a6f13a4aSGreg Roach            }
486a6f13a4aSGreg Roach        }
487a6f13a4aSGreg Roach
488e8e7866bSGreg Roach        $this->wt_report->setup();
489a6f13a4aSGreg Roach    }
490a6f13a4aSGreg Roach
491a6f13a4aSGreg Roach    /**
492fab8f067SGreg Roach     * Handle </doc>
4938ba2e626SGreg Roach     *
4948ba2e626SGreg Roach     * @return void
495a6f13a4aSGreg Roach     */
496b702978eSGreg Roach    protected function docEndHandler(): void
497c1010edaSGreg Roach    {
498e8e7866bSGreg Roach        $this->wt_report->run();
499a6f13a4aSGreg Roach    }
500a6f13a4aSGreg Roach
501a6f13a4aSGreg Roach    /**
502fab8f067SGreg Roach     * Handle <header>
5038ba2e626SGreg Roach     *
5048ba2e626SGreg Roach     * @return void
505a6f13a4aSGreg Roach     */
506b702978eSGreg Roach    protected function headerStartHandler(): void
507c1010edaSGreg Roach    {
508a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
509e8e7866bSGreg Roach        $this->wt_report->clearHeader();
5107a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
511a6f13a4aSGreg Roach    }
512a6f13a4aSGreg Roach
513a6f13a4aSGreg Roach    /**
514fab8f067SGreg Roach     * Handle <body>
5158ba2e626SGreg Roach     *
5168ba2e626SGreg Roach     * @return void
517a6f13a4aSGreg Roach     */
518b702978eSGreg Roach    protected function bodyStartHandler(): void
519c1010edaSGreg Roach    {
5207a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
521a6f13a4aSGreg Roach    }
522a6f13a4aSGreg Roach
523a6f13a4aSGreg Roach    /**
524fab8f067SGreg Roach     * Handle <footer>
5258ba2e626SGreg Roach     *
5268ba2e626SGreg Roach     * @return void
527a6f13a4aSGreg Roach     */
528b702978eSGreg Roach    protected function footerStartHandler(): void
529c1010edaSGreg Roach    {
5307a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
531a6f13a4aSGreg Roach    }
532a6f13a4aSGreg Roach
533a6f13a4aSGreg Roach    /**
534fab8f067SGreg Roach     * Handle <cell>
535a6f13a4aSGreg Roach     *
53677bab461SGreg Roach     * @param array<string,string> $attrs
5378ba2e626SGreg Roach     *
5388ba2e626SGreg Roach     * @return void
539a6f13a4aSGreg Roach     */
540b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
541c1010edaSGreg Roach    {
542a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
54377bab461SGreg Roach        $align = $attrs['align'] ?? '';
544a6f13a4aSGreg Roach        // RTL supported left/right alignment
545044416d2SGreg Roach        if ($align === 'rightrtl') {
546e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5477a6ee1acSGreg Roach                $align = 'left';
548a6f13a4aSGreg Roach            } else {
5497a6ee1acSGreg Roach                $align = 'right';
550a6f13a4aSGreg Roach            }
551044416d2SGreg Roach        } elseif ($align === 'leftrtl') {
552e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5537a6ee1acSGreg Roach                $align = 'right';
554a6f13a4aSGreg Roach            } else {
5557a6ee1acSGreg Roach                $align = 'left';
556a6f13a4aSGreg Roach            }
557a6f13a4aSGreg Roach        }
558a6f13a4aSGreg Roach
55977bab461SGreg Roach        // The color to fill the background of this cell
56077bab461SGreg Roach        $bgcolor = $attrs['bgcolor'] ?? '';
561a6f13a4aSGreg Roach
56277bab461SGreg Roach        // Whether the background should be painted
56377bab461SGreg Roach        $fill = (int) ($attrs['fill'] ?? '0');
564a6f13a4aSGreg Roach
56577bab461SGreg Roach        // If true reset the last cell height
56677bab461SGreg Roach        $reseth = (bool) ($attrs['reseth'] ?? '1');
567a6f13a4aSGreg Roach
56877bab461SGreg Roach        // Whether a border should be printed around this box
56977bab461SGreg Roach        $border = $attrs['border'] ?? '';
57077bab461SGreg Roach
571a6f13a4aSGreg Roach        // string Border color in HTML code
57277bab461SGreg Roach        $bocolor = $attrs['bocolor'] ?? '';
573a6f13a4aSGreg Roach
57477bab461SGreg Roach        // Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
57577bab461SGreg Roach        $height = (int) ($attrs['height'] ?? '0');
57677bab461SGreg Roach
577a6f13a4aSGreg 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.
57877bab461SGreg Roach        $width = (int) ($attrs['width'] ?? '0');
579a6f13a4aSGreg Roach
58077bab461SGreg Roach        // Stretch character mode
58177bab461SGreg Roach        $stretch = (int) ($attrs['stretch'] ?? '0');
582a6f13a4aSGreg Roach
583a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
584c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
585a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5867a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
587c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
588a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
589a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5907a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
591a6f13a4aSGreg Roach                $left = 0;
592a6f13a4aSGreg Roach            }
593a6f13a4aSGreg Roach        }
594a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
595c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
596a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5977a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
598c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
599a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
600a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
6017a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
602a6f13a4aSGreg Roach                $top = 0;
603a6f13a4aSGreg Roach            }
604a6f13a4aSGreg Roach        }
605a6f13a4aSGreg Roach
60677bab461SGreg Roach        // The name of the Style that should be used to render the text.
60777bab461SGreg Roach        $style = $attrs['style'] ?? '';
608a6f13a4aSGreg Roach
609a6f13a4aSGreg Roach        // string Text color in html code
61077bab461SGreg Roach        $tcolor = $attrs['tcolor'] ?? '';
611a6f13a4aSGreg Roach
612a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
613a6f13a4aSGreg Roach        $ln = 0;
614a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
615a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
616a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
6177a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
618a6f13a4aSGreg Roach                $ln = 0;
619a6f13a4aSGreg Roach            }
620a6f13a4aSGreg Roach        }
621a6f13a4aSGreg Roach
622044416d2SGreg Roach        if ($align === 'left') {
6237a6ee1acSGreg Roach            $align = 'L';
624044416d2SGreg Roach        } elseif ($align === 'right') {
6257a6ee1acSGreg Roach            $align = 'R';
626044416d2SGreg Roach        } elseif ($align === 'center') {
6277a6ee1acSGreg Roach            $align = 'C';
628044416d2SGreg Roach        } elseif ($align === 'justify') {
6297a6ee1acSGreg Roach            $align = 'J';
630a6f13a4aSGreg Roach        }
631a6f13a4aSGreg Roach
6329b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
633a6f13a4aSGreg Roach        $this->print_data         = true;
634a6f13a4aSGreg Roach
635e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
63624f2a3afSGreg Roach            (int) $width,
63724f2a3afSGreg Roach            (int) $height,
638a6f13a4aSGreg Roach            $border,
639a6f13a4aSGreg Roach            $align,
640a6f13a4aSGreg Roach            $bgcolor,
641a6f13a4aSGreg Roach            $style,
642a6f13a4aSGreg Roach            $ln,
643a6f13a4aSGreg Roach            $top,
644a6f13a4aSGreg Roach            $left,
645a6f13a4aSGreg Roach            $fill,
646a6f13a4aSGreg Roach            $stretch,
647a6f13a4aSGreg Roach            $bocolor,
648a6f13a4aSGreg Roach            $tcolor,
649a6f13a4aSGreg Roach            $reseth
650a6f13a4aSGreg Roach        );
651a6f13a4aSGreg Roach    }
652a6f13a4aSGreg Roach
653a6f13a4aSGreg Roach    /**
654fab8f067SGreg Roach     * Handle </cell>
6558ba2e626SGreg Roach     *
6568ba2e626SGreg Roach     * @return void
657a6f13a4aSGreg Roach     */
658b702978eSGreg Roach    protected function cellEndHandler(): void
659c1010edaSGreg Roach    {
660a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
661e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
662a6f13a4aSGreg Roach    }
663a6f13a4aSGreg Roach
664a6f13a4aSGreg Roach    /**
665fab8f067SGreg Roach     * Handle <now />
6668ba2e626SGreg Roach     *
6678ba2e626SGreg Roach     * @return void
668a6f13a4aSGreg Roach     */
669b702978eSGreg Roach    protected function nowStartHandler(): void
670c1010edaSGreg Roach    {
671d97083feSGreg Roach        $this->current_element->addText(Registry::timestampFactory()->now()->isoFormat('LLLL'));
672a6f13a4aSGreg Roach    }
673a6f13a4aSGreg Roach
674a6f13a4aSGreg Roach    /**
675fab8f067SGreg Roach     * Handle <pageNum />
6768ba2e626SGreg Roach     *
6778ba2e626SGreg Roach     * @return void
678a6f13a4aSGreg Roach     */
679b702978eSGreg Roach    protected function pageNumStartHandler(): void
680c1010edaSGreg Roach    {
6817a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
682a6f13a4aSGreg Roach    }
683a6f13a4aSGreg Roach
684a6f13a4aSGreg Roach    /**
685fab8f067SGreg Roach     * Handle <totalPages />
6868ba2e626SGreg Roach     *
6878ba2e626SGreg Roach     * @return void
688a6f13a4aSGreg Roach     */
689b702978eSGreg Roach    protected function totalPagesStartHandler(): void
690c1010edaSGreg Roach    {
6917a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
692a6f13a4aSGreg Roach    }
693a6f13a4aSGreg Roach
694a6f13a4aSGreg Roach    /**
695a6f13a4aSGreg Roach     * Called at the start of an element.
696a6f13a4aSGreg Roach     *
69709482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
6988ba2e626SGreg Roach     *
6998ba2e626SGreg Roach     * @return void
700a6f13a4aSGreg Roach     */
701b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
702c1010edaSGreg Roach    {
703a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
704a6f13a4aSGreg Roach            $this->process_gedcoms++;
705a6f13a4aSGreg Roach
706a6f13a4aSGreg Roach            return;
707a6f13a4aSGreg Roach        }
708a6f13a4aSGreg Roach
709a6f13a4aSGreg Roach        $tag       = $attrs['id'];
7107a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
7117a6ee1acSGreg Roach        $tags      = explode(':', $tag);
712a6f13a4aSGreg Roach        $newgedrec = '';
713a6f13a4aSGreg Roach        if (count($tags) < 2) {
7146b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
715299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
716a6f13a4aSGreg Roach        }
717a6f13a4aSGreg Roach        if (empty($newgedrec)) {
718a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
719a6f13a4aSGreg Roach            $newgedrec = '';
720a6f13a4aSGreg Roach            foreach ($tags as $tag) {
7217a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
722d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
723d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
724a6f13a4aSGreg Roach                    } else {
7256b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
726299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
727a6f13a4aSGreg Roach                    }
728a6f13a4aSGreg Roach                } else {
7297a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
73013abd6f3SGreg Roach                        $gmatch = [];
731a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7326b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
733299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
734a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
735a6f13a4aSGreg Roach                        } else {
736a6f13a4aSGreg Roach                            $newgedrec = '';
737a6f13a4aSGreg Roach                            break;
738a6f13a4aSGreg Roach                        }
739a6f13a4aSGreg Roach                    } else {
740b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
741b315f3e1SGreg Roach                        $newgedrec = self::getSubRecord($level, "$level $tag", $tgedrec);
742a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
743a6f13a4aSGreg Roach                    }
744a6f13a4aSGreg Roach                }
745a6f13a4aSGreg Roach            }
746a6f13a4aSGreg Roach        }
747a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7489b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
749a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
750a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
751a6f13a4aSGreg Roach                $this->fact = $match[2];
752a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
753a6f13a4aSGreg Roach            }
754a6f13a4aSGreg Roach        } else {
755a6f13a4aSGreg Roach            $this->process_gedcoms++;
756a6f13a4aSGreg Roach        }
757a6f13a4aSGreg Roach    }
758a6f13a4aSGreg Roach
759a6f13a4aSGreg Roach    /**
760a6f13a4aSGreg Roach     * Called at the end of an element.
7618ba2e626SGreg Roach     *
7628ba2e626SGreg Roach     * @return void
763a6f13a4aSGreg Roach     */
764b702978eSGreg Roach    protected function gedcomEndHandler(): void
765c1010edaSGreg Roach    {
766a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
767a6f13a4aSGreg Roach            $this->process_gedcoms--;
768a6f13a4aSGreg Roach        } else {
76965e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
770a6f13a4aSGreg Roach        }
771a6f13a4aSGreg Roach    }
772a6f13a4aSGreg Roach
773a6f13a4aSGreg Roach    /**
774fab8f067SGreg Roach     * Handle <textBox>
775a6f13a4aSGreg Roach     *
77609482a55SGreg Roach     * @param array<string> $attrs
7778ba2e626SGreg Roach     *
7788ba2e626SGreg Roach     * @return void
779a6f13a4aSGreg Roach     */
780b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
781c1010edaSGreg Roach    {
782a6f13a4aSGreg Roach        // string Background color code
7837a6ee1acSGreg Roach        $bgcolor = '';
784a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
785a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
786a6f13a4aSGreg Roach        }
787a6f13a4aSGreg Roach
788a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
789a6f13a4aSGreg Roach        $fill = true;
790a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7917a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
792a6f13a4aSGreg Roach                $fill = false;
7937a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
794a6f13a4aSGreg Roach                $fill = true;
795a6f13a4aSGreg Roach            }
796a6f13a4aSGreg Roach        }
797a6f13a4aSGreg Roach
798a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
799a6f13a4aSGreg Roach        $border = false;
800a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
8017a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
802a6f13a4aSGreg Roach                $border = true;
8037a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
804a6f13a4aSGreg Roach                $border = false;
805a6f13a4aSGreg Roach            }
806a6f13a4aSGreg Roach        }
807a6f13a4aSGreg Roach
808a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
809a6f13a4aSGreg Roach        $height = 0;
810a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
811a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
812a6f13a4aSGreg Roach        }
813a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
814a6f13a4aSGreg Roach        $width = 0;
815a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
816a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
817a6f13a4aSGreg Roach        }
818a6f13a4aSGreg Roach
819a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
820c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
821a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
8227a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
823c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
824a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
825a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
8267a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
827a6f13a4aSGreg Roach                $left = 0;
828a6f13a4aSGreg Roach            }
829a6f13a4aSGreg Roach        }
830a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
831c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
832a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8337a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
834c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
835a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
836a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8377a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
838a6f13a4aSGreg Roach                $top = 0;
839a6f13a4aSGreg Roach            }
840a6f13a4aSGreg Roach        }
841a6f13a4aSGreg 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
842a6f13a4aSGreg Roach        $newline = false;
843a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8447a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
845a6f13a4aSGreg Roach                $newline = true;
8467a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
847a6f13a4aSGreg Roach                $newline = false;
848a6f13a4aSGreg Roach            }
849a6f13a4aSGreg Roach        }
850a6f13a4aSGreg Roach        // boolean
851a6f13a4aSGreg Roach        $pagecheck = true;
852a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8537a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
854a6f13a4aSGreg Roach                $pagecheck = false;
8557a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
856a6f13a4aSGreg Roach                $pagecheck = true;
857a6f13a4aSGreg Roach            }
858a6f13a4aSGreg Roach        }
859a6f13a4aSGreg Roach        // boolean Cell padding
860a6f13a4aSGreg Roach        $padding = true;
861a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8627a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
863a6f13a4aSGreg Roach                $padding = false;
8647a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
865a6f13a4aSGreg Roach                $padding = true;
866a6f13a4aSGreg Roach            }
867a6f13a4aSGreg Roach        }
868a6f13a4aSGreg Roach        // boolean Reset this box Height
869a6f13a4aSGreg Roach        $reseth = false;
870a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8717a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
872a6f13a4aSGreg Roach                $reseth = true;
8737a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
874a6f13a4aSGreg Roach                $reseth = false;
875a6f13a4aSGreg Roach            }
876a6f13a4aSGreg Roach        }
877a6f13a4aSGreg Roach
878a6f13a4aSGreg Roach        // string Style of rendering
8797a6ee1acSGreg Roach        $style = '';
880a6f13a4aSGreg Roach
8819b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
882a6f13a4aSGreg Roach        $this->print_data         = false;
883a6f13a4aSGreg Roach
8849b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
885e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
886a6f13a4aSGreg Roach            $width,
887a6f13a4aSGreg Roach            $height,
888a6f13a4aSGreg Roach            $border,
889a6f13a4aSGreg Roach            $bgcolor,
890a6f13a4aSGreg Roach            $newline,
891a6f13a4aSGreg Roach            $left,
892a6f13a4aSGreg Roach            $top,
893a6f13a4aSGreg Roach            $pagecheck,
894a6f13a4aSGreg Roach            $style,
895a6f13a4aSGreg Roach            $fill,
896a6f13a4aSGreg Roach            $padding,
897a6f13a4aSGreg Roach            $reseth
898a6f13a4aSGreg Roach        );
899a6f13a4aSGreg Roach    }
900a6f13a4aSGreg Roach
901a6f13a4aSGreg Roach    /**
902fab8f067SGreg Roach     * Handle <textBox>
9038ba2e626SGreg Roach     *
9048ba2e626SGreg Roach     * @return void
905a6f13a4aSGreg Roach     */
906b702978eSGreg Roach    protected function textBoxEndHandler(): void
907c1010edaSGreg Roach    {
908a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
909e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
91031d9777aSGreg Roach
91131d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
91231d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
91331d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
91431d9777aSGreg Roach
915e8e7866bSGreg Roach        $this->wt_report = array_pop($this->wt_report_stack);
916e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
917a6f13a4aSGreg Roach    }
918a6f13a4aSGreg Roach
919a6f13a4aSGreg Roach    /**
92076692c8bSGreg Roach     * XLM <Text>.
92176692c8bSGreg Roach     *
92209482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9238ba2e626SGreg Roach     *
9248ba2e626SGreg Roach     * @return void
925a6f13a4aSGreg Roach     */
926b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
927c1010edaSGreg Roach    {
9289b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
929a6f13a4aSGreg Roach        $this->print_data         = true;
930a6f13a4aSGreg Roach
931a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9327a6ee1acSGreg Roach        $style = '';
933a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
934a6f13a4aSGreg Roach            $style = $attrs['style'];
935a6f13a4aSGreg Roach        }
936a6f13a4aSGreg Roach
937a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9387a6ee1acSGreg Roach        $color = '';
939a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
940a6f13a4aSGreg Roach            $color = $attrs['color'];
941a6f13a4aSGreg Roach        }
942a6f13a4aSGreg Roach
943e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
944a6f13a4aSGreg Roach    }
945a6f13a4aSGreg Roach
946a6f13a4aSGreg Roach    /**
947fab8f067SGreg Roach     * Handle </text>
9488ba2e626SGreg Roach     *
9498ba2e626SGreg Roach     * @return void
950a6f13a4aSGreg Roach     */
951b702978eSGreg Roach    protected function textEndHandler(): void
952c1010edaSGreg Roach    {
953a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
954e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
955a6f13a4aSGreg Roach    }
956a6f13a4aSGreg Roach
957a6f13a4aSGreg Roach    /**
958fab8f067SGreg Roach     * Handle <getPersonName />
959a6f13a4aSGreg Roach     * Get the name
960a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
961a6f13a4aSGreg Roach     * 2. id is set with a record id
962a6f13a4aSGreg Roach     *
96309482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9648ba2e626SGreg Roach     *
9658ba2e626SGreg Roach     * @return void
966a6f13a4aSGreg Roach     */
967b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
968c1010edaSGreg Roach    {
9697a6ee1acSGreg Roach        $id    = '';
97013abd6f3SGreg Roach        $match = [];
971a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9727a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
973a6f13a4aSGreg Roach                $id = $match[1];
974a6f13a4aSGreg Roach            }
975a6f13a4aSGreg Roach        } else {
9767a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
977d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
978d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
979a6f13a4aSGreg Roach                }
980a6f13a4aSGreg Roach            } else {
9817a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
98213abd6f3SGreg Roach                    $gmatch = [];
983a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
984a6f13a4aSGreg Roach                        $id = $gmatch[1];
985a6f13a4aSGreg Roach                    }
986a6f13a4aSGreg Roach                } else {
987a6f13a4aSGreg Roach                    $id = $attrs['id'];
988a6f13a4aSGreg Roach                }
989a6f13a4aSGreg Roach            }
990a6f13a4aSGreg Roach        }
991a6f13a4aSGreg Roach        if (!empty($id)) {
9926b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9938f038c36SRico Sonntag            if ($record === null) {
994a6f13a4aSGreg Roach                return;
995a6f13a4aSGreg Roach            }
996a6f13a4aSGreg Roach            if (!$record->canShowName()) {
997a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
998a6f13a4aSGreg Roach            } else {
99939ca88baSGreg Roach                $name = $record->fullName();
1000a6f13a4aSGreg Roach                $name = strip_tags($name);
1001a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
1002381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
1003a6f13a4aSGreg Roach                } else {
1004911124f5SGreg Roach                    $addname = (string) $record->alternateName();
1005a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
1006a6f13a4aSGreg Roach                    if (!empty($addname)) {
10077a6ee1acSGreg Roach                        $name .= ' ' . $addname;
1008a6f13a4aSGreg Roach                    }
1009a6f13a4aSGreg Roach                }
1010a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
1011a6f13a4aSGreg Roach            }
1012a6f13a4aSGreg Roach        }
1013a6f13a4aSGreg Roach    }
1014a6f13a4aSGreg Roach
1015a6f13a4aSGreg Roach    /**
1016fab8f067SGreg Roach     * Handle <gedcomValue />
1017a6f13a4aSGreg Roach     *
101809482a55SGreg Roach     * @param array<string> $attrs
10198ba2e626SGreg Roach     *
10208ba2e626SGreg Roach     * @return void
1021a6f13a4aSGreg Roach     */
1022b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1023c1010edaSGreg Roach    {
10247a6ee1acSGreg Roach        $id    = '';
102513abd6f3SGreg Roach        $match = [];
10267a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1027a6f13a4aSGreg Roach            $id = $match[1];
1028a6f13a4aSGreg Roach        }
1029a6f13a4aSGreg Roach
1030044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10317a6ee1acSGreg Roach            $useBreak = '1';
1032a6f13a4aSGreg Roach        } else {
10337a6ee1acSGreg Roach            $useBreak = '0';
1034a6f13a4aSGreg Roach        }
1035a6f13a4aSGreg Roach
1036a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1037a6f13a4aSGreg Roach        if (!empty($tag)) {
1038044416d2SGreg Roach            if ($tag === '@desc') {
1039a6f13a4aSGreg Roach                $value = $this->desc;
1040a6f13a4aSGreg Roach                $value = trim($value);
1041a6f13a4aSGreg Roach                $this->current_element->addText($value);
1042a6f13a4aSGreg Roach            }
1043044416d2SGreg Roach            if ($tag === '@id') {
1044a6f13a4aSGreg Roach                $this->current_element->addText($id);
1045a6f13a4aSGreg Roach            } else {
10467a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1047a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1048b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1049b2448a1bSGreg Roach                    if ($level === 0) {
1050a6f13a4aSGreg Roach                        $level++;
1051a6f13a4aSGreg Roach                    }
1052a6f13a4aSGreg Roach                } else {
1053b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1054a6f13a4aSGreg Roach                }
1055a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10563d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1057a6f13a4aSGreg Roach                switch (end($tags)) {
1058a6f13a4aSGreg Roach                    case 'DATE':
1059a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1060326dd299SGreg Roach                        $value = strip_tags($tmp->display());
1061a6f13a4aSGreg Roach                        break;
1062a6f13a4aSGreg Roach                    case 'PLAC':
1063299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1064392561bbSGreg Roach                        $value = $tmp->shortName();
1065a6f13a4aSGreg Roach                        break;
1066a6f13a4aSGreg Roach                }
1067044416d2SGreg Roach                if ($useBreak === '1') {
1068a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1069a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1070a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1071a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1072a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10733b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10743b3cfeeaSGreg Roach                        $value = substr($value, 4);
1075a6f13a4aSGreg Roach                    }
1076a6f13a4aSGreg Roach                }
1077d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10783cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10794d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
1080ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($value, $this->tree), ['br']);
10814d35caa7SGreg Roach                    } else {
1082ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($value, $this->tree), ['br']);
10834d35caa7SGreg Roach                    }
1084ab2c9152SGreg Roach                    $value = strtr($value, [MarkdownFactory::BREAK => ' ']);
1085a4d703aeSGreg Roach                }
1086a2a485c3SGreg Roach
1087a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1088381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1089a2a485c3SGreg Roach                }
1090a6f13a4aSGreg Roach                $this->current_element->addText($value);
1091a6f13a4aSGreg Roach            }
1092a6f13a4aSGreg Roach        }
1093a6f13a4aSGreg Roach    }
1094a6f13a4aSGreg Roach
1095a6f13a4aSGreg Roach    /**
1096fab8f067SGreg Roach     * Handle <repeatTag>
1097a6f13a4aSGreg Roach     *
109809482a55SGreg Roach     * @param array<string> $attrs
10998ba2e626SGreg Roach     *
11008ba2e626SGreg Roach     * @return void
1101a6f13a4aSGreg Roach     */
1102b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1103c1010edaSGreg Roach    {
1104a6f13a4aSGreg Roach        $this->process_repeats++;
1105a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1106a6f13a4aSGreg Roach            return;
1107a6f13a4aSGreg Roach        }
1108a6f13a4aSGreg Roach
11099b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
111013abd6f3SGreg Roach        $this->repeats         = [];
1111e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1112a6f13a4aSGreg Roach
1113e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1114a6f13a4aSGreg Roach        if (!empty($tag)) {
1115044416d2SGreg Roach            if ($tag === '@desc') {
1116a6f13a4aSGreg Roach                $value = $this->desc;
1117a6f13a4aSGreg Roach                $value = trim($value);
1118a6f13a4aSGreg Roach                $this->current_element->addText($value);
1119a6f13a4aSGreg Roach            } else {
11207a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11217a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1122b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1123b2448a1bSGreg Roach                if ($level === 0) {
1124a6f13a4aSGreg Roach                    $level++;
1125a6f13a4aSGreg Roach                }
1126a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1127a6f13a4aSGreg Roach                $t      = $tag;
1128a6f13a4aSGreg Roach                $count  = count($tags);
1129a6f13a4aSGreg Roach                $i      = 0;
1130a6f13a4aSGreg Roach                while ($i < $count) {
1131a6f13a4aSGreg Roach                    $t = $tags[$i];
1132a6f13a4aSGreg Roach                    if (!empty($t)) {
1133a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
1134b315f3e1SGreg Roach                            $subrec = self::getSubRecord($level, "$level $t", $subrec);
1135a6f13a4aSGreg Roach                            if (empty($subrec)) {
1136a6f13a4aSGreg Roach                                $level--;
1137b315f3e1SGreg Roach                                $subrec = self::getSubRecord($level, "@ $t", $this->gedrec);
1138a6f13a4aSGreg Roach                                if (empty($subrec)) {
1139a6f13a4aSGreg Roach                                    return;
1140a6f13a4aSGreg Roach                                }
1141a6f13a4aSGreg Roach                            }
1142a6f13a4aSGreg Roach                        }
1143a6f13a4aSGreg Roach                        $level++;
1144a6f13a4aSGreg Roach                    }
1145a6f13a4aSGreg Roach                    $i++;
1146a6f13a4aSGreg Roach                }
1147a6f13a4aSGreg Roach                $level--;
1148a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1149a6f13a4aSGreg Roach                $i     = 0;
1150a6f13a4aSGreg Roach                while ($i < $count) {
1151a6f13a4aSGreg Roach                    $i++;
1152a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1153b315f3e1SGreg Roach                    $subrecord = self::getSubRecord($level, "$level $t", $subrec, $i);
11548d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11556b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1156a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1157a9007102SGreg Roach                            continue;
1158a9007102SGreg Roach                        }
1159a9007102SGreg Roach                    }
1160a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1161a6f13a4aSGreg Roach                }
1162a6f13a4aSGreg Roach            }
1163a6f13a4aSGreg Roach        }
1164a6f13a4aSGreg Roach    }
1165a6f13a4aSGreg Roach
1166a6f13a4aSGreg Roach    /**
1167fab8f067SGreg Roach     * Handle </repeatTag>
11688ba2e626SGreg Roach     *
11698ba2e626SGreg Roach     * @return void
1170a6f13a4aSGreg Roach     */
1171b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1172c1010edaSGreg Roach    {
1173a6f13a4aSGreg Roach        $this->process_repeats--;
1174a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1175a6f13a4aSGreg Roach            return;
1176a6f13a4aSGreg Roach        }
1177a6f13a4aSGreg Roach
1178a6f13a4aSGreg Roach        // Check if there is anything to repeat
1179a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1180a6f13a4aSGreg Roach            // No need to load them if not used...
1181a6f13a4aSGreg Roach
1182a6f13a4aSGreg Roach            $lineoffset = 0;
1183a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1184a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1185a6f13a4aSGreg Roach            }
1186a6f13a4aSGreg Roach            //-- read the xml from the file
1187299d100dSGreg Roach            $lines = file($this->report);
1188dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1189a6f13a4aSGreg Roach                $lineoffset--;
1190a6f13a4aSGreg Roach            }
1191a6f13a4aSGreg Roach            $lineoffset++;
1192a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1193a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1194a6f13a4aSGreg Roach            // RepeatTag Level counter
1195a6f13a4aSGreg Roach            $count = 1;
1196a6f13a4aSGreg Roach            while (0 < $count) {
1197dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1198a6f13a4aSGreg Roach                    $count++;
1199dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1200a6f13a4aSGreg Roach                    $count--;
1201a6f13a4aSGreg Roach                }
1202a6f13a4aSGreg Roach                if (0 < $count) {
1203a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1204a6f13a4aSGreg Roach                }
1205a6f13a4aSGreg Roach                $line_nr++;
1206a6f13a4aSGreg Roach            }
1207a6f13a4aSGreg Roach            // No need to drag this
1208a6f13a4aSGreg Roach            unset($lines);
1209a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1210a6f13a4aSGreg Roach            // Save original values
12119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1212a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1213a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1214a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1215a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1216e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1217a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
12181aa04befSGreg Roach
12191aa04befSGreg Roach                xml_set_element_handler(
12201aa04befSGreg Roach                    $repeat_parser,
12219d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12221aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12231aa04befSGreg Roach                    },
12249d454b6bSGreg Roach                    function ($parser, string $name): void {
12251aa04befSGreg Roach                        $this->endElement($parser, $name);
12261aa04befSGreg Roach                    }
12271aa04befSGreg Roach                );
12281aa04befSGreg Roach
12291aa04befSGreg Roach                xml_set_character_data_handler(
12301aa04befSGreg Roach                    $repeat_parser,
12319d454b6bSGreg Roach                    function ($parser, string $data): void {
12321aa04befSGreg Roach                        $this->characterData($parser, $data);
12331aa04befSGreg Roach                    }
12341aa04befSGreg Roach                );
12351aa04befSGreg Roach
1236a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12376ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1238a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1239a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1240a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1241a6f13a4aSGreg Roach                    ));
1242a6f13a4aSGreg Roach                }
1243a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1244a6f13a4aSGreg Roach            }
1245a6f13a4aSGreg Roach            // Restore original values
1246a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1247e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1248a6f13a4aSGreg Roach        }
124965e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1250a6f13a4aSGreg Roach    }
1251a6f13a4aSGreg Roach
1252a6f13a4aSGreg Roach    /**
1253a6f13a4aSGreg Roach     * Variable lookup
1254a6f13a4aSGreg Roach     * Retrieve predefined variables :
1255a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1256a6f13a4aSGreg Roach     *        1 EVEN This is a description
1257a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1258a6f13a4aSGreg Roach     * $ I18N::translate('....')
1259a6f13a4aSGreg Roach     * $ language_settings[]
1260a6f13a4aSGreg Roach     *
126109482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
12628ba2e626SGreg Roach     *
12638ba2e626SGreg Roach     * @return void
1264a6f13a4aSGreg Roach     */
1265b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1266c1010edaSGreg Roach    {
1267a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12686ccdf4f0SGreg 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));
1269a6f13a4aSGreg Roach        }
1270a6f13a4aSGreg Roach
1271a6f13a4aSGreg Roach        $var = $attrs['var'];
1272a6f13a4aSGreg Roach        // SetVar element preset variables
1273d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1274d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1275a6f13a4aSGreg Roach        } else {
1276a6f13a4aSGreg Roach            $tfact = $this->fact;
12777a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1278a6f13a4aSGreg Roach                // Use :
1279a6f13a4aSGreg Roach                // n TYPE This text if string
1280a6f13a4aSGreg Roach                $tfact = $this->type;
128197def6bcSGreg Roach            } else {
128297def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
128397def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
128497def6bcSGreg Roach
128597def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
128697def6bcSGreg Roach                        $tfact = $element->label();
128797def6bcSGreg Roach                        break;
1288a6f13a4aSGreg Roach                    }
128997def6bcSGreg Roach                }
129097def6bcSGreg Roach            }
129197def6bcSGreg Roach
129297def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
129397def6bcSGreg Roach
1294a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1295da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1296a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1297a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1298a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1299a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1300a6f13a4aSGreg Roach            }
1301a6f13a4aSGreg Roach        }
1302a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1303a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
13047a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1305a6f13a4aSGreg Roach                $g   = new Date($var);
1306a6f13a4aSGreg Roach                $var = $g->display();
1307a6f13a4aSGreg Roach            }
1308a6f13a4aSGreg Roach        }
1309a6f13a4aSGreg Roach        $this->current_element->addText($var);
13102836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1311a6f13a4aSGreg Roach    }
1312a6f13a4aSGreg Roach
1313a6f13a4aSGreg Roach    /**
1314fab8f067SGreg Roach     * Handle <facts>
131576692c8bSGreg Roach     *
131609482a55SGreg Roach     * @param array<string> $attrs
13178ba2e626SGreg Roach     *
13188ba2e626SGreg Roach     * @return void
1319a6f13a4aSGreg Roach     */
1320b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1321c1010edaSGreg Roach    {
1322a6f13a4aSGreg Roach        $this->process_repeats++;
1323a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1324a6f13a4aSGreg Roach            return;
1325a6f13a4aSGreg Roach        }
1326a6f13a4aSGreg Roach
13279b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
132813abd6f3SGreg Roach        $this->repeats         = [];
1329e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1330a6f13a4aSGreg Roach
13317a6ee1acSGreg Roach        $id    = '';
133213abd6f3SGreg Roach        $match = [];
13337a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1334a6f13a4aSGreg Roach            $id = $match[1];
1335a6f13a4aSGreg Roach        }
13367a6ee1acSGreg Roach        $tag = '';
1337a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1338a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1339a6f13a4aSGreg Roach        }
13407a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1341d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1342a6f13a4aSGreg Roach        }
1343a6f13a4aSGreg Roach
13446b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1345a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1346820b62dfSGreg Roach            $facts = $record->facts([], true);
134713abd6f3SGreg Roach            $this->repeats = [];
1348a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1349195b5e75SGreg Roach            foreach ($facts as $fact) {
1350d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1351d0889c63SGreg Roach
1352d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1353195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1354a6f13a4aSGreg Roach                }
1355a6f13a4aSGreg Roach            }
1356a6f13a4aSGreg Roach        } else {
135730158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1358d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1359138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1360a6f13a4aSGreg Roach                }
1361a6f13a4aSGreg Roach            }
1362a6f13a4aSGreg Roach        }
1363a6f13a4aSGreg Roach    }
1364a6f13a4aSGreg Roach
1365a6f13a4aSGreg Roach    /**
1366fab8f067SGreg Roach     * Handle </facts>
13678ba2e626SGreg Roach     *
13688ba2e626SGreg Roach     * @return void
1369a6f13a4aSGreg Roach     */
1370b702978eSGreg Roach    protected function factsEndHandler(): void
1371c1010edaSGreg Roach    {
1372a6f13a4aSGreg Roach        $this->process_repeats--;
1373a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1374a6f13a4aSGreg Roach            return;
1375a6f13a4aSGreg Roach        }
1376a6f13a4aSGreg Roach
1377a6f13a4aSGreg Roach        // Check if there is anything to repeat
1378a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1379e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1380a6f13a4aSGreg Roach            $lineoffset = 0;
1381a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1382a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1383a6f13a4aSGreg Roach            }
1384a6f13a4aSGreg Roach
1385a6f13a4aSGreg Roach            //-- read the xml from the file
1386299d100dSGreg Roach            $lines = file($this->report);
1387dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1388a6f13a4aSGreg Roach                $lineoffset--;
1389a6f13a4aSGreg Roach            }
1390a6f13a4aSGreg Roach            $lineoffset++;
1391a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1392a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1393a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1394a6f13a4aSGreg Roach            while ($line_nr < $i) {
1395a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1396a6f13a4aSGreg Roach                $line_nr++;
1397a6f13a4aSGreg Roach            }
1398a6f13a4aSGreg Roach            // No need to drag this
1399a6f13a4aSGreg Roach            unset($lines);
1400a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1401a6f13a4aSGreg Roach            // Save original values
14029b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1403a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1404a6f13a4aSGreg Roach            $count                = count($this->repeats);
1405a6f13a4aSGreg Roach            $i                    = 0;
1406a6f13a4aSGreg Roach            while ($i < $count) {
1407a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1408a6f13a4aSGreg Roach                $this->fact   = '';
1409a6f13a4aSGreg Roach                $this->desc   = '';
1410a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1411a6f13a4aSGreg Roach                    $this->fact = $match[1];
1412a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
141313abd6f3SGreg Roach                        $tmatch = [];
1414a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1415a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1416a6f13a4aSGreg Roach                        } else {
1417a6f13a4aSGreg Roach                            $this->type = ' ';
1418a6f13a4aSGreg Roach                        }
1419a6f13a4aSGreg Roach                    }
1420a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
1421b315f3e1SGreg Roach                    $this->desc .= self::getCont(2, $this->gedrec);
1422a6f13a4aSGreg Roach                }
1423a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1424e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1425a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
14261aa04befSGreg Roach
14271aa04befSGreg Roach                xml_set_element_handler(
14281aa04befSGreg Roach                    $repeat_parser,
14299d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14301aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14311aa04befSGreg Roach                    },
14329d454b6bSGreg Roach                    function ($parser, string $name): void {
14331aa04befSGreg Roach                        $this->endElement($parser, $name);
14341aa04befSGreg Roach                    }
14351aa04befSGreg Roach                );
14361aa04befSGreg Roach
14371aa04befSGreg Roach                xml_set_character_data_handler(
14381aa04befSGreg Roach                    $repeat_parser,
14399d454b6bSGreg Roach                    function ($parser, string $data): void {
14401aa04befSGreg Roach                        $this->characterData($parser, $data);
14411aa04befSGreg Roach                    }
14421aa04befSGreg Roach                );
14431aa04befSGreg Roach
1444a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14456ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1446a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1447a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1448a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1449a6f13a4aSGreg Roach                    ));
1450a6f13a4aSGreg Roach                }
1451a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1452a6f13a4aSGreg Roach                $i++;
1453a6f13a4aSGreg Roach            }
1454a6f13a4aSGreg Roach            // Restore original values
1455e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1456a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1457a6f13a4aSGreg Roach        }
145865e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1459a6f13a4aSGreg Roach    }
1460a6f13a4aSGreg Roach
1461a6f13a4aSGreg Roach    /**
1462a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1463d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1464a6f13a4aSGreg Roach     *
146509482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
14668ba2e626SGreg Roach     *
14678ba2e626SGreg Roach     * @return void
1468a6f13a4aSGreg Roach     */
1469b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1470c1010edaSGreg Roach    {
1471a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14726ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1473a6f13a4aSGreg Roach        }
1474a6f13a4aSGreg Roach
1475a6f13a4aSGreg Roach        $name  = $attrs['name'];
1476a6f13a4aSGreg Roach        $value = $attrs['value'];
147713abd6f3SGreg Roach        $match = [];
1478a6f13a4aSGreg Roach        // Current GEDCOM record strings
1479044416d2SGreg Roach        if ($value === '@ID') {
14807a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1481a6f13a4aSGreg Roach                $value = $match[1];
1482a6f13a4aSGreg Roach            }
1483044416d2SGreg Roach        } elseif ($value === '@fact') {
1484a6f13a4aSGreg Roach            $value = $this->fact;
1485044416d2SGreg Roach        } elseif ($value === '@desc') {
1486a6f13a4aSGreg Roach            $value = $this->desc;
1487044416d2SGreg Roach        } elseif ($value === '@generation') {
1488589feda3SGreg Roach            $value = (string) $this->generation;
1489a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
149013abd6f3SGreg Roach            $gmatch = [];
1491a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14927a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1493a6f13a4aSGreg Roach            }
1494a6f13a4aSGreg Roach        }
1495a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1496d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1497a6f13a4aSGreg Roach        }
1498a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1499a6f13a4aSGreg Roach        $i     = 0;
1500a6f13a4aSGreg Roach        while ($i < $count) {
1501d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
15027a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1503a6f13a4aSGreg Roach            $i++;
1504a6f13a4aSGreg Roach        }
1505a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1506da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1507a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1508a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1509a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1510a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1511a6f13a4aSGreg Roach        }
151252868398SGreg Roach
1513a6f13a4aSGreg Roach        // Arithmetic functions
15143cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
151552868398SGreg Roach            // Create an expression language with the functions used by our reports.
151652868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1517c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1518c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
151952868398SGreg Roach
152052868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1521a6f13a4aSGreg Roach        }
152252868398SGreg Roach
1523dec352c1SGreg Roach        if (str_contains($value, '@')) {
15247a6ee1acSGreg Roach            $value = '';
1525a6f13a4aSGreg Roach        }
1526d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1527a6f13a4aSGreg Roach    }
1528a6f13a4aSGreg Roach
1529a6f13a4aSGreg Roach    /**
1530fab8f067SGreg Roach     * Handle <if>
1531a6f13a4aSGreg Roach     *
153209482a55SGreg Roach     * @param array<string> $attrs
15338ba2e626SGreg Roach     *
15348ba2e626SGreg Roach     * @return void
1535a6f13a4aSGreg Roach     */
1536b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1537c1010edaSGreg Roach    {
1538a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1539a6f13a4aSGreg Roach            $this->process_ifs++;
1540a6f13a4aSGreg Roach
1541a6f13a4aSGreg Roach            return;
1542a6f13a4aSGreg Roach        }
1543a6f13a4aSGreg Roach
1544a6f13a4aSGreg Roach        $condition = $attrs['condition'];
154582759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1546c1010edaSGreg Roach        $condition = str_replace([
1547c1010edaSGreg Roach            ' LT ',
1548c1010edaSGreg Roach            ' GT ',
1549c1010edaSGreg Roach        ], [
1550c1010edaSGreg Roach            '<',
1551c1010edaSGreg Roach            '>',
1552c1010edaSGreg Roach        ], $condition);
15533cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15547a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
155513abd6f3SGreg Roach        $match     = [];
15563cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1557a6f13a4aSGreg Roach        $i         = 0;
1558a6f13a4aSGreg Roach        while ($i < $count) {
1559a6f13a4aSGreg Roach            $id    = $match[$i][1];
1560a6f13a4aSGreg Roach            $value = '""';
1561044416d2SGreg Roach            if ($id === 'ID') {
15627a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1563a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1564a6f13a4aSGreg Roach                }
15657a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1566a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15677a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1568a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15697a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1570a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1571a6f13a4aSGreg Roach            } else {
1572b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1573b2448a1bSGreg Roach                if ($level === 0) {
1574a6f13a4aSGreg Roach                    $level++;
1575a6f13a4aSGreg Roach                }
15763d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1577a6f13a4aSGreg Roach                if (empty($value)) {
1578a6f13a4aSGreg Roach                    $level++;
15793d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1580a6f13a4aSGreg Roach                }
15818d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15825e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1583a6f13a4aSGreg Roach            }
1584a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1585a6f13a4aSGreg Roach            $i++;
1586a6f13a4aSGreg Roach        }
15875809450fSGreg Roach
1588cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1589cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1590c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1591c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1592cb63a60eSGreg Roach
1593cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15945809450fSGreg Roach
1595a6f13a4aSGreg Roach        if (!$ret) {
1596a6f13a4aSGreg Roach            $this->process_ifs++;
1597a6f13a4aSGreg Roach        }
1598a6f13a4aSGreg Roach    }
1599a6f13a4aSGreg Roach
1600a6f13a4aSGreg Roach    /**
1601fab8f067SGreg Roach     * Handle </if>
16028ba2e626SGreg Roach     *
16038ba2e626SGreg Roach     * @return void
1604a6f13a4aSGreg Roach     */
1605b702978eSGreg Roach    protected function ifEndHandler(): void
1606c1010edaSGreg Roach    {
1607a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1608a6f13a4aSGreg Roach            $this->process_ifs--;
1609a6f13a4aSGreg Roach        }
1610a6f13a4aSGreg Roach    }
1611a6f13a4aSGreg Roach
1612a6f13a4aSGreg Roach    /**
1613fab8f067SGreg Roach     * Handle <footnote>
1614a6f13a4aSGreg Roach     * Collect the Footnote links
1615fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1616a6f13a4aSGreg Roach     *
161709482a55SGreg Roach     * @param array<string> $attrs
16188ba2e626SGreg Roach     *
16198ba2e626SGreg Roach     * @return void
1620a6f13a4aSGreg Roach     */
1621b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1622c1010edaSGreg Roach    {
16237a6ee1acSGreg Roach        $id = '';
16247a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1625a6f13a4aSGreg Roach            $id = $match[2];
1626a6f13a4aSGreg Roach        }
16276b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1628a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16299b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1630a6f13a4aSGreg Roach            $this->print_data         = true;
16317a6ee1acSGreg Roach            $style                    = '';
1632a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1633a6f13a4aSGreg Roach                $style = $attrs['style'];
1634a6f13a4aSGreg Roach            }
1635a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1636e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1637a6f13a4aSGreg Roach        } else {
1638a6f13a4aSGreg Roach            $this->print_data       = false;
1639a6f13a4aSGreg Roach            $this->process_footnote = false;
1640a6f13a4aSGreg Roach        }
1641a6f13a4aSGreg Roach    }
1642a6f13a4aSGreg Roach
1643a6f13a4aSGreg Roach    /**
1644fab8f067SGreg Roach     * Handle </footnote>
1645a6f13a4aSGreg Roach     * Print the collected Footnote data
16468ba2e626SGreg Roach     *
16478ba2e626SGreg Roach     * @return void
1648a6f13a4aSGreg Roach     */
1649b702978eSGreg Roach    protected function footnoteEndHandler(): void
1650c1010edaSGreg Roach    {
1651a6f13a4aSGreg Roach        if ($this->process_footnote) {
1652a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1653a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1654a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1655e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1656a6f13a4aSGreg Roach            }
1657a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1658a6f13a4aSGreg Roach        } else {
1659a6f13a4aSGreg Roach            $this->process_footnote = true;
1660a6f13a4aSGreg Roach        }
1661a6f13a4aSGreg Roach    }
1662a6f13a4aSGreg Roach
1663a6f13a4aSGreg Roach    /**
1664fab8f067SGreg Roach     * Handle <footnoteTexts />
16658ba2e626SGreg Roach     *
16668ba2e626SGreg Roach     * @return void
1667a6f13a4aSGreg Roach     */
1668b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1669c1010edaSGreg Roach    {
16707a6ee1acSGreg Roach        $temp = 'footnotetexts';
1671e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1672a6f13a4aSGreg Roach    }
1673a6f13a4aSGreg Roach
1674a6f13a4aSGreg Roach    /**
1675a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16768ba2e626SGreg Roach     *
16778ba2e626SGreg Roach     * @return void
1678a6f13a4aSGreg Roach     */
1679b702978eSGreg Roach    protected function brStartHandler(): void
1680c1010edaSGreg Roach    {
1681a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1682a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1683a6f13a4aSGreg Roach        }
1684a6f13a4aSGreg Roach    }
1685a6f13a4aSGreg Roach
1686a6f13a4aSGreg Roach    /**
1687fab8f067SGreg Roach     * Handle <sp />
1688fab8f067SGreg Roach     * Forced space
16898ba2e626SGreg Roach     *
16908ba2e626SGreg Roach     * @return void
1691a6f13a4aSGreg Roach     */
1692b702978eSGreg Roach    protected function spStartHandler(): void
1693c1010edaSGreg Roach    {
1694a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1695a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1696a6f13a4aSGreg Roach        }
1697a6f13a4aSGreg Roach    }
1698a6f13a4aSGreg Roach
1699a6f13a4aSGreg Roach    /**
1700fab8f067SGreg Roach     * Handle <highlightedImage />
170176692c8bSGreg Roach     *
170209482a55SGreg Roach     * @param array<string> $attrs
17038ba2e626SGreg Roach     *
17048ba2e626SGreg Roach     * @return void
1705a6f13a4aSGreg Roach     */
1706b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1707c1010edaSGreg Roach    {
1708a6f13a4aSGreg Roach        $id = '';
17097a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1710a6f13a4aSGreg Roach            $id = $match[1];
1711a6f13a4aSGreg Roach        }
1712a6f13a4aSGreg Roach
1713c21bdddcSGreg Roach        // Position the top corner of this box on the page
1714c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1715a6f13a4aSGreg Roach
1716c21bdddcSGreg Roach        // Position the left corner of this box on the page
1717c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1718a6f13a4aSGreg Roach
171983cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172083cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1721a6f13a4aSGreg Roach
1722a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172383cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1724a6f13a4aSGreg Roach
172583cdc021SGreg Roach        // Width, height (or both).
1726c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1727c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1728a6f13a4aSGreg Roach
17296b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
17304a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
173186a63f51SGreg Roach
1732*9458f20aSGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists()) {
1733*9458f20aSGreg Roach            $image      = imagecreatefromstring($media_file->fileContents());
1734b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
173529518ad2SGreg Roach
1736a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17373c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17383c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1739a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17403c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17413c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1742a6f13a4aSGreg Roach            } else {
1743136a8abaSGreg Roach                $width  = (float) $attributes[0];
1744136a8abaSGreg Roach                $height = (float) $attributes[1];
1745a6f13a4aSGreg Roach            }
1746*9458f20aSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1747e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1748a6f13a4aSGreg Roach        }
1749a6f13a4aSGreg Roach    }
1750a6f13a4aSGreg Roach
1751a6f13a4aSGreg Roach    /**
1752fab8f067SGreg Roach     * Handle <image/>
175376692c8bSGreg Roach     *
175409482a55SGreg Roach     * @param array<string> $attrs
17558ba2e626SGreg Roach     *
17568ba2e626SGreg Roach     * @return void
1757a6f13a4aSGreg Roach     */
1758b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1759c1010edaSGreg Roach    {
176083cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1761c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1762a6f13a4aSGreg Roach
1763a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1764c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1765a6f13a4aSGreg Roach
176683cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
176783cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1768a6f13a4aSGreg Roach
1769a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
177083cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1771a6f13a4aSGreg Roach
177283cdc021SGreg Roach        // Width, height (or both).
1773c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1774c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1775a6f13a4aSGreg Roach
177683cdc021SGreg Roach        $file = $attrs['file'] ?? '';
177783cdc021SGreg Roach
1778044416d2SGreg Roach        if ($file === '@FILE') {
177913abd6f3SGreg Roach            $match = [];
1780a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17816b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17824a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1783cdf416fbSGreg Roach
1784*9458f20aSGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists()) {
1785*9458f20aSGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents());
1786b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
178729518ad2SGreg Roach
1788a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17893c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17903c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1791a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17923c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17933c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1794a6f13a4aSGreg Roach                    } else {
1795136a8abaSGreg Roach                        $width  = (float) $attributes[0];
1796136a8abaSGreg Roach                        $height = (float) $attributes[1];
1797a6f13a4aSGreg Roach                    }
1798*9458f20aSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1799e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1800a6f13a4aSGreg Roach                }
1801a6f13a4aSGreg Roach            }
1802a6f13a4aSGreg Roach        } else {
18037a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1804a6f13a4aSGreg Roach                $size = getimagesize($file);
1805a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1806a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1807a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1808a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1809a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1810a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1811a6f13a4aSGreg Roach                } else {
1812a6f13a4aSGreg Roach                    $width  = $size[0];
1813a6f13a4aSGreg Roach                    $height = $size[1];
1814a6f13a4aSGreg Roach                }
1815e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1816e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1817a6f13a4aSGreg Roach            }
1818a6f13a4aSGreg Roach        }
1819a6f13a4aSGreg Roach    }
1820a6f13a4aSGreg Roach
1821a6f13a4aSGreg Roach    /**
1822fab8f067SGreg Roach     * Handle <line>
1823a6f13a4aSGreg Roach     *
182409482a55SGreg Roach     * @param array<string> $attrs
18258ba2e626SGreg Roach     *
18268ba2e626SGreg Roach     * @return void
1827a6f13a4aSGreg Roach     */
1828b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1829c1010edaSGreg Roach    {
1830a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1831c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1832a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18337a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1834a6f13a4aSGreg Roach                $x1 = 0;
18357a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1836c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1837a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1838c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1839a6f13a4aSGreg Roach            }
1840a6f13a4aSGreg Roach        }
1841a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1842c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1843a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18447a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1845a6f13a4aSGreg Roach                $y1 = 0;
18467a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1847c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1848a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1849c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1850a6f13a4aSGreg Roach            }
1851a6f13a4aSGreg Roach        }
1852a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1853c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1854a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18557a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1856a6f13a4aSGreg Roach                $x2 = 0;
18577a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1858c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1859a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1860c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1861a6f13a4aSGreg Roach            }
1862a6f13a4aSGreg Roach        }
1863a6f13a4aSGreg Roach        // End vertical position
1864c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1865a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18667a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1867a6f13a4aSGreg Roach                $y2 = 0;
18687a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1869c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1870a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1871c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1872a6f13a4aSGreg Roach            }
1873a6f13a4aSGreg Roach        }
1874a6f13a4aSGreg Roach
1875e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1876e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1877a6f13a4aSGreg Roach    }
1878a6f13a4aSGreg Roach
1879a6f13a4aSGreg Roach    /**
1880fab8f067SGreg Roach     * Handle <list>
1881a6f13a4aSGreg Roach     *
188209482a55SGreg Roach     * @param array<string> $attrs
18838ba2e626SGreg Roach     *
18848ba2e626SGreg Roach     * @return void
1885a6f13a4aSGreg Roach     */
1886b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1887c1010edaSGreg Roach    {
1888a6f13a4aSGreg Roach        $this->process_repeats++;
1889a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1890a6f13a4aSGreg Roach            return;
1891a6f13a4aSGreg Roach        }
1892a6f13a4aSGreg Roach
189313abd6f3SGreg Roach        $match = [];
1894a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1895a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1896a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1897d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1898a6f13a4aSGreg Roach                $sortby = trim($sortby);
1899a6f13a4aSGreg Roach            }
1900a6f13a4aSGreg Roach        } else {
19017a6ee1acSGreg Roach            $sortby = 'NAME';
1902a6f13a4aSGreg Roach        }
1903a6f13a4aSGreg Roach
1904e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1905195b5e75SGreg Roach
1906a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1907a6f13a4aSGreg Roach        switch ($listname) {
19087a6ee1acSGreg Roach            case 'pending':
190963b1851dSGreg Roach                $this->list = DB::table('change')
1910195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1911a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1912195b5e75SGreg Roach                            ->from('change')
1913195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1914195b5e75SGreg Roach                            ->where('status', '=', 'pending')
19157f5c2944SGreg Roach                            ->groupBy(['xref']);
1916195b5e75SGreg Roach                    })
191763b1851dSGreg Roach                    ->get()
191863b1851dSGreg Roach                    ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom))
191963b1851dSGreg Roach                    ->filter()
192063b1851dSGreg Roach                    ->all();
1921a6f13a4aSGreg Roach                break;
192263b1851dSGreg Roach
1923a6f13a4aSGreg Roach            case 'individual':
19245985adfbSGreg Roach                $query = DB::table('individuals')
19255985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19265985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19275985adfbSGreg Roach                    ->distinct();
19285985adfbSGreg Roach
1929a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1930dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
193182759250SGreg Roach                        $value = $this->substituteVars($value, false);
1932a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1933a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19340b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19355985adfbSGreg Roach                                $join
19365985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19375985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19385985adfbSGreg Roach                            });
19395985adfbSGreg Roach
19405985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19415985adfbSGreg Roach
1942a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19435985adfbSGreg Roach
1944044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19455985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1946a6f13a4aSGreg Roach                            } else {
19475985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1948a6f13a4aSGreg Roach                            }
19495985adfbSGreg Roach
19505985adfbSGreg Roach                            // This filter has been fully processed
19515985adfbSGreg Roach                            unset($attrs[$attr]);
19527ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19530b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19545985adfbSGreg Roach                                $join
19555985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19565985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19575985adfbSGreg Roach                            });
1958a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19597a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19605d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1961b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1962a6f13a4aSGreg Roach                            }
19635985adfbSGreg Roach
19645985adfbSGreg Roach                            // This filter has been fully processed
19655985adfbSGreg Roach                            unset($attrs[$attr]);
1966a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19675985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1968a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19695985adfbSGreg Roach
1970a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19715985adfbSGreg Roach
19725985adfbSGreg Roach                            // This filter has been fully processed
19735985adfbSGreg Roach                            unset($attrs[$attr]);
19742fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19759dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19765985adfbSGreg Roach                            $query
19770b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19785985adfbSGreg Roach                                    $join
19797ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19807ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19815985adfbSGreg Roach                                })
19820b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19837ee4bfadSGreg Roach                                    $join
19847ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19857ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19867ee4bfadSGreg Roach                                })
19879b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19886e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19895985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19907ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19916e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19926e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
199347a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19946e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19956e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1996e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19977ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1998a6f13a4aSGreg Roach                        }
1999a6f13a4aSGreg Roach                    }
2000a6f13a4aSGreg Roach                }
2001a6f13a4aSGreg Roach
200213abd6f3SGreg Roach                $this->list = [];
2003a6f13a4aSGreg Roach
20045985adfbSGreg Roach                foreach ($query->get() as $row) {
20056b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
2006a6f13a4aSGreg Roach                }
2007a6f13a4aSGreg Roach                break;
2008a6f13a4aSGreg Roach
2009a6f13a4aSGreg Roach            case 'family':
201030fc2b1eSGreg Roach                $query = DB::table('families')
201130fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
201230fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
201330fc2b1eSGreg Roach                    ->distinct();
201430fc2b1eSGreg Roach
2015a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
2016dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
201782759250SGreg Roach                        $value = $this->substituteVars($value, false);
2018a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2019a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20200b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
202130fc2b1eSGreg Roach                                $join
202230fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
202330fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
202430fc2b1eSGreg Roach                            });
202530fc2b1eSGreg Roach
202630fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
202730fc2b1eSGreg Roach
2028a6f13a4aSGreg Roach                            $date = new Date($match[3]);
202930fc2b1eSGreg Roach
2030044416d2SGreg Roach                            if ($match[2] === 'LTE') {
203130fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2032a6f13a4aSGreg Roach                            } else {
203330fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2034a6f13a4aSGreg Roach                            }
203530fc2b1eSGreg Roach
203630fc2b1eSGreg Roach                            // This filter has been fully processed
203730fc2b1eSGreg Roach                            unset($attrs[$attr]);
2038a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
203930fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2040a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
204130fc2b1eSGreg Roach
2042a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
204330fc2b1eSGreg Roach
204430fc2b1eSGreg Roach                            // This filter has been fully processed
204530fc2b1eSGreg Roach                            unset($attrs[$attr]);
204630fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20473b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20480b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
204930fc2b1eSGreg Roach                                    $join
205030fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20513b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
205230fc2b1eSGreg Roach                                            $query
205330fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
205430fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
205530fc2b1eSGreg Roach                                        });
205630fc2b1eSGreg Roach                                });
20575d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20587a6ee1acSGreg Roach                                if ($match[1] != '') {
20597a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20605d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2061b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20625d0bc43dSGreg Roach                                    }
20635d0bc43dSGreg Roach                                }
2064a6f13a4aSGreg Roach                            }
206530fc2b1eSGreg Roach
206630fc2b1eSGreg Roach                            // This filter has been fully processed
206730fc2b1eSGreg Roach                            unset($attrs[$attr]);
20686e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20699dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
207030fc2b1eSGreg Roach                            $query
20710b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
207230fc2b1eSGreg Roach                                    $join
20736e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20746e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
207530fc2b1eSGreg Roach                                })
20760b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20776e5c0963SGreg Roach                                    $join
20786e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20796e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20806e5c0963SGreg Roach                                })
2081b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20826e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
208330fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20846e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20856e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20866e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20876e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20886e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20896e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2090e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20916e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2092a6f13a4aSGreg Roach                        }
2093a6f13a4aSGreg Roach                    }
2094a6f13a4aSGreg Roach                }
2095a6f13a4aSGreg Roach
209613abd6f3SGreg Roach                $this->list = [];
2097a6f13a4aSGreg Roach
209830fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20996b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2100a6f13a4aSGreg Roach                }
2101a6f13a4aSGreg Roach                break;
2102a6f13a4aSGreg Roach
2103a6f13a4aSGreg Roach            default:
21046ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2105a6f13a4aSGreg Roach        }
2106a6f13a4aSGreg Roach
210713abd6f3SGreg Roach        $filters  = [];
210813abd6f3SGreg Roach        $filters2 = [];
2109a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2110a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2111a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2112a6f13a4aSGreg Roach                    $condition = $value;
2113a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2114a6f13a4aSGreg Roach                        $id    = $match[1];
2115a6f13a4aSGreg Roach                        $value = "''";
2116044416d2SGreg Roach                        if ($id === 'ID') {
21177a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2118a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2119a6f13a4aSGreg Roach                            }
2120044416d2SGreg Roach                        } elseif ($id === 'fact') {
2121a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2122044416d2SGreg Roach                        } elseif ($id === 'desc') {
2123a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2124a6f13a4aSGreg Roach                        } else {
2125a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21267a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2127a6f13a4aSGreg Roach                            }
2128a6f13a4aSGreg Roach                        }
2129a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2130a6f13a4aSGreg Roach                    }
2131a6f13a4aSGreg Roach                    //-- handle regular expressions
2132a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2133a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2134a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2135a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2136a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2137d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2138a6f13a4aSGreg Roach                            $val = trim($val);
2139a6f13a4aSGreg Roach                        }
2140a6f13a4aSGreg Roach                        if ($val) {
21417a6ee1acSGreg Roach                            $searchstr = '';
21427a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2143a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2144a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2145a6f13a4aSGreg Roach                                $level = 1;
2146f71a7dedSGreg Roach                                $t = 'XXXX';
2147a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2148a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2149a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2150a6f13a4aSGreg Roach                                    }
2151a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2152044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21537a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2154a6f13a4aSGreg Roach                                    }
21557a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2156a6f13a4aSGreg Roach                                    $level++;
2157a6f13a4aSGreg Roach                                }
2158a6f13a4aSGreg Roach                            } else {
2159044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21607a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2161a6f13a4aSGreg Roach                                }
2162a6f13a4aSGreg Roach                                $t         = $tag;
21637a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2164a6f13a4aSGreg Roach                            }
2165a6f13a4aSGreg Roach                            switch ($expr) {
21667a6ee1acSGreg Roach                                case 'CONTAINS':
2167044416d2SGreg Roach                                    if ($t === 'PLAC') {
2168a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2169a6f13a4aSGreg Roach                                    } else {
2170a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2171a6f13a4aSGreg Roach                                    }
2172a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2173a6f13a4aSGreg Roach                                    break;
2174a6f13a4aSGreg Roach                                default:
2175c1010edaSGreg Roach                                    $filters2[] = [
2176c1010edaSGreg Roach                                        'tag'  => $tag,
2177c1010edaSGreg Roach                                        'expr' => $expr,
2178c1010edaSGreg Roach                                        'val'  => $val,
2179c1010edaSGreg Roach                                    ];
2180a6f13a4aSGreg Roach                                    break;
2181a6f13a4aSGreg Roach                            }
2182a6f13a4aSGreg Roach                        }
2183a6f13a4aSGreg Roach                    }
2184a6f13a4aSGreg Roach                }
2185a6f13a4aSGreg Roach            }
2186a6f13a4aSGreg Roach        }
2187a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2188a6f13a4aSGreg Roach        if ($filters) {
2189a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2190a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2191299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2192a6f13a4aSGreg Roach                        unset($this->list[$key]);
2193a6f13a4aSGreg Roach                        break;
2194a6f13a4aSGreg Roach                    }
2195a6f13a4aSGreg Roach                }
2196a6f13a4aSGreg Roach            }
2197a6f13a4aSGreg Roach        }
2198a6f13a4aSGreg Roach        if ($filters2) {
219913abd6f3SGreg Roach            $mylist = [];
2200a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2201c0935879SGreg Roach                $key  = $indi->xref();
2202299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2203a6f13a4aSGreg Roach                $keep = true;
2204a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2205a6f13a4aSGreg Roach                    if ($keep) {
2206a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2207a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2208a6f13a4aSGreg Roach                        $val  = $filter['val'];
2209b2448a1bSGreg Roach                        if ($val === "''") {
22107a6ee1acSGreg Roach                            $val = '';
2211a6f13a4aSGreg Roach                        }
22127a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2213a6f13a4aSGreg Roach                        $t    = end($tags);
22143d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2215a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2216044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
22177a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
22187a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2219a6f13a4aSGreg Roach                            $t    = end($tags);
2220b315f3e1SGreg Roach                            $v    = self::getSubRecord(1, $tag, $grec);
2221a6f13a4aSGreg Roach                        }
2222a6f13a4aSGreg Roach
2223a6f13a4aSGreg Roach                        switch ($expr) {
22247a6ee1acSGreg Roach                            case 'GTE':
2225044416d2SGreg Roach                                if ($t === 'DATE') {
2226a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2227a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2228a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2229a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2230a6f13a4aSGreg Roach                                    $keep = true;
2231a6f13a4aSGreg Roach                                }
2232a6f13a4aSGreg Roach                                break;
22337a6ee1acSGreg Roach                            case 'LTE':
2234044416d2SGreg Roach                                if ($t === 'DATE') {
2235a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2236a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2237a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2238a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2239a6f13a4aSGreg Roach                                    $keep = true;
2240a6f13a4aSGreg Roach                                }
2241a6f13a4aSGreg Roach                                break;
2242a6f13a4aSGreg Roach                            default:
2243a6f13a4aSGreg Roach                                if ($v == $val) {
2244a6f13a4aSGreg Roach                                    $keep = true;
2245a6f13a4aSGreg Roach                                } else {
2246a6f13a4aSGreg Roach                                    $keep = false;
2247a6f13a4aSGreg Roach                                }
2248a6f13a4aSGreg Roach                                break;
2249a6f13a4aSGreg Roach                        }
2250a6f13a4aSGreg Roach                    }
2251a6f13a4aSGreg Roach                }
2252a6f13a4aSGreg Roach                if ($keep) {
2253a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2254a6f13a4aSGreg Roach                }
2255a6f13a4aSGreg Roach            }
2256a6f13a4aSGreg Roach            $this->list = $mylist;
2257a6f13a4aSGreg Roach        }
2258a6f13a4aSGreg Roach
2259a6f13a4aSGreg Roach        switch ($sortby) {
2260a6f13a4aSGreg Roach            case 'NAME':
2261c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2262a6f13a4aSGreg Roach                break;
2263a6f13a4aSGreg Roach            case 'CHAN':
2264c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2265a6f13a4aSGreg Roach                break;
2266a6f13a4aSGreg Roach            case 'BIRT:DATE':
2267c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2268a6f13a4aSGreg Roach                break;
2269a6f13a4aSGreg Roach            case 'DEAT:DATE':
2270c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2271a6f13a4aSGreg Roach                break;
2272a6f13a4aSGreg Roach            case 'MARR:DATE':
2273c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2274a6f13a4aSGreg Roach                break;
2275a6f13a4aSGreg Roach            default:
2276a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2277a6f13a4aSGreg Roach                break;
2278a6f13a4aSGreg Roach        }
2279a6f13a4aSGreg Roach
22809b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2281e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2282a6f13a4aSGreg Roach    }
2283a6f13a4aSGreg Roach
2284a6f13a4aSGreg Roach    /**
2285fab8f067SGreg Roach     * Handle </list>
22868ba2e626SGreg Roach     *
22878ba2e626SGreg Roach     * @return void
2288a6f13a4aSGreg Roach     */
2289b702978eSGreg Roach    protected function listEndHandler(): void
2290c1010edaSGreg Roach    {
2291a6f13a4aSGreg Roach        $this->process_repeats--;
2292a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2293a6f13a4aSGreg Roach            return;
2294a6f13a4aSGreg Roach        }
2295a6f13a4aSGreg Roach
2296a6f13a4aSGreg Roach        // Check if there is any list
2297a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2298a6f13a4aSGreg Roach            $lineoffset = 0;
2299a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2300a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2301a6f13a4aSGreg Roach            }
2302a6f13a4aSGreg Roach            //-- read the xml from the file
2303299d100dSGreg Roach            $lines = file($this->report);
2304dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2305a6f13a4aSGreg Roach                $lineoffset--;
2306a6f13a4aSGreg Roach            }
2307a6f13a4aSGreg Roach            $lineoffset++;
2308a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2309a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2310a6f13a4aSGreg Roach            // List Level counter
2311a6f13a4aSGreg Roach            $count = 1;
2312a6f13a4aSGreg Roach            while (0 < $count) {
2313dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2314a6f13a4aSGreg Roach                    $count++;
2315dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2316a6f13a4aSGreg Roach                    $count--;
2317a6f13a4aSGreg Roach                }
2318a6f13a4aSGreg Roach                if (0 < $count) {
2319a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2320a6f13a4aSGreg Roach                }
2321a6f13a4aSGreg Roach                $line_nr++;
2322a6f13a4aSGreg Roach            }
2323a6f13a4aSGreg Roach            // No need to drag this
2324a6f13a4aSGreg Roach            unset($lines);
23257a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2326a6f13a4aSGreg Roach            // Save original values
23279b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2328a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2329a6f13a4aSGreg Roach
2330a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2331a6f13a4aSGreg Roach            $this->list_private = 0;
2332a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2333a6f13a4aSGreg Roach                if ($record->canShow()) {
2334f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2335a6f13a4aSGreg Roach                    //-- start the sax parser
2336a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2337e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2338a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23391aa04befSGreg Roach
23401aa04befSGreg Roach                    xml_set_element_handler(
23411aa04befSGreg Roach                        $repeat_parser,
23429d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23431aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23441aa04befSGreg Roach                        },
23459d454b6bSGreg Roach                        function ($parser, string $name): void {
23461aa04befSGreg Roach                            $this->endElement($parser, $name);
23471aa04befSGreg Roach                        }
23481aa04befSGreg Roach                    );
23491aa04befSGreg Roach
23501aa04befSGreg Roach                    xml_set_character_data_handler(
23511aa04befSGreg Roach                        $repeat_parser,
23529d454b6bSGreg Roach                        function ($parser, string $data): void {
23531aa04befSGreg Roach                            $this->characterData($parser, $data);
23541aa04befSGreg Roach                        }
23551aa04befSGreg Roach                    );
23561aa04befSGreg Roach
2357a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23586ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2359a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2360a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2361a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2362a6f13a4aSGreg Roach                        ));
2363a6f13a4aSGreg Roach                    }
2364a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2365a6f13a4aSGreg Roach                } else {
2366a6f13a4aSGreg Roach                    $this->list_private++;
2367a6f13a4aSGreg Roach                }
2368a6f13a4aSGreg Roach            }
236913abd6f3SGreg Roach            $this->list   = [];
2370e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2371a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2372a6f13a4aSGreg Roach        }
237365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2374a6f13a4aSGreg Roach    }
2375a6f13a4aSGreg Roach
2376a6f13a4aSGreg Roach    /**
2377fab8f067SGreg Roach     * Handle <listTotal>
2378a6f13a4aSGreg Roach     * Prints the total number of records in a list
2379fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23808ba2e626SGreg Roach     *
23818ba2e626SGreg Roach     * @return void
2382a6f13a4aSGreg Roach     */
2383b702978eSGreg Roach    protected function listTotalStartHandler(): void
2384c1010edaSGreg Roach    {
2385a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2386589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2387a6f13a4aSGreg Roach        } else {
23887a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2389a6f13a4aSGreg Roach        }
2390a6f13a4aSGreg Roach    }
2391a6f13a4aSGreg Roach
2392a6f13a4aSGreg Roach    /**
2393fab8f067SGreg Roach     * Handle <relatives>
239476692c8bSGreg Roach     *
239509482a55SGreg Roach     * @param array<string> $attrs
23968ba2e626SGreg Roach     *
23978ba2e626SGreg Roach     * @return void
2398a6f13a4aSGreg Roach     */
2399b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2400c1010edaSGreg Roach    {
2401a6f13a4aSGreg Roach        $this->process_repeats++;
2402a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2403a6f13a4aSGreg Roach            return;
2404a6f13a4aSGreg Roach        }
2405a6f13a4aSGreg Roach
2406e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2407e364afe4SGreg Roach
240813abd6f3SGreg Roach        $match = [];
2409a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2410d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2411a6f13a4aSGreg Roach            $sortby = trim($sortby);
2412a6f13a4aSGreg Roach        }
2413a6f13a4aSGreg Roach
2414a6f13a4aSGreg Roach        $maxgen = -1;
2415a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2416c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2417a6f13a4aSGreg Roach        }
2418a6f13a4aSGreg Roach
2419e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2420e364afe4SGreg Roach
2421a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2422d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2423a6f13a4aSGreg Roach            $group = trim($group);
2424a6f13a4aSGreg Roach        }
2425a6f13a4aSGreg Roach
2426e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2427e364afe4SGreg Roach
2428a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2429d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2430a6f13a4aSGreg Roach            $id = trim($id);
2431a6f13a4aSGreg Roach        }
2432a6f13a4aSGreg Roach
243313abd6f3SGreg Roach        $this->list = [];
24346b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2435d965cc1aSGreg Roach        if ($person instanceof Individual) {
2436a6f13a4aSGreg Roach            $this->list[$id] = $person;
2437a6f13a4aSGreg Roach            switch ($group) {
24387a6ee1acSGreg Roach                case 'child-family':
243939ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2440820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2441820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2442a6f13a4aSGreg Roach                        }
2443820b62dfSGreg Roach
2444820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2445c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2446a6f13a4aSGreg Roach                        }
2447a6f13a4aSGreg Roach                    }
2448a6f13a4aSGreg Roach                    break;
24497a6ee1acSGreg Roach                case 'spouse-family':
245039ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2451820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2452820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2453a6f13a4aSGreg Roach                        }
2454820b62dfSGreg Roach
2455820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2456c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2457a6f13a4aSGreg Roach                        }
2458a6f13a4aSGreg Roach                    }
2459a6f13a4aSGreg Roach                    break;
24607a6ee1acSGreg Roach                case 'direct-ancestors':
24613d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2462a6f13a4aSGreg Roach                    break;
24637a6ee1acSGreg Roach                case 'ancestors':
24643d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2465a6f13a4aSGreg Roach                    break;
24667a6ee1acSGreg Roach                case 'descendants':
2467a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24683d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2469a6f13a4aSGreg Roach                    break;
24707a6ee1acSGreg Roach                case 'all':
24713d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24723d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2473a6f13a4aSGreg Roach                    break;
2474a6f13a4aSGreg Roach            }
2475a6f13a4aSGreg Roach        }
2476a6f13a4aSGreg Roach
2477a6f13a4aSGreg Roach        switch ($sortby) {
2478a6f13a4aSGreg Roach            case 'NAME':
2479c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2480a6f13a4aSGreg Roach                break;
2481a6f13a4aSGreg Roach            case 'BIRT:DATE':
2482c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2483a6f13a4aSGreg Roach                break;
2484a6f13a4aSGreg Roach            case 'DEAT:DATE':
2485c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2486a6f13a4aSGreg Roach                break;
2487a6f13a4aSGreg Roach            case 'generation':
248813abd6f3SGreg Roach                $newarray = [];
2489a6f13a4aSGreg Roach                reset($this->list);
2490a6f13a4aSGreg Roach                $genCounter = 1;
2491a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2492a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2493a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2494a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2495f70bcff5SGreg Roach                            $newarray[$key] = (object) ['generation' => $this->generation];
2496a6f13a4aSGreg Roach                        }
2497a6f13a4aSGreg Roach                    }
2498a6f13a4aSGreg Roach                    $genCounter++;
2499a6f13a4aSGreg Roach                }
2500a6f13a4aSGreg Roach                $this->list = $newarray;
2501a6f13a4aSGreg Roach                break;
2502a6f13a4aSGreg Roach            default:
2503a6f13a4aSGreg Roach                // unsorted
2504a6f13a4aSGreg Roach                break;
2505a6f13a4aSGreg Roach        }
25069b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2507e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2508a6f13a4aSGreg Roach    }
2509a6f13a4aSGreg Roach
2510a6f13a4aSGreg Roach    /**
2511fab8f067SGreg Roach     * Handle </relatives>
25128ba2e626SGreg Roach     *
25138ba2e626SGreg Roach     * @return void
2514a6f13a4aSGreg Roach     */
2515b702978eSGreg Roach    protected function relativesEndHandler(): void
2516c1010edaSGreg Roach    {
2517a6f13a4aSGreg Roach        $this->process_repeats--;
2518a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2519a6f13a4aSGreg Roach            return;
2520a6f13a4aSGreg Roach        }
2521a6f13a4aSGreg Roach
2522a6f13a4aSGreg Roach        // Check if there is any relatives
2523a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2524a6f13a4aSGreg Roach            $lineoffset = 0;
2525a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2526a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2527a6f13a4aSGreg Roach            }
2528a6f13a4aSGreg Roach            //-- read the xml from the file
2529299d100dSGreg Roach            $lines = file($this->report);
2530dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2531a6f13a4aSGreg Roach                $lineoffset--;
2532a6f13a4aSGreg Roach            }
2533a6f13a4aSGreg Roach            $lineoffset++;
2534a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2535a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2536a6f13a4aSGreg Roach            // Relatives Level counter
2537a6f13a4aSGreg Roach            $count = 1;
2538a6f13a4aSGreg Roach            while (0 < $count) {
2539dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2540a6f13a4aSGreg Roach                    $count++;
2541dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2542a6f13a4aSGreg Roach                    $count--;
2543a6f13a4aSGreg Roach                }
2544a6f13a4aSGreg Roach                if (0 < $count) {
2545a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2546a6f13a4aSGreg Roach                }
2547a6f13a4aSGreg Roach                $line_nr++;
2548a6f13a4aSGreg Roach            }
2549a6f13a4aSGreg Roach            // No need to drag this
2550a6f13a4aSGreg Roach            unset($lines);
2551a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2552a6f13a4aSGreg Roach            // Save original values
25539b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2554a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2555a6f13a4aSGreg Roach
2556a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2557a6f13a4aSGreg Roach            $this->list_private = 0;
2558b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2559a6f13a4aSGreg Roach                if (isset($value->generation)) {
2560a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2561a6f13a4aSGreg Roach                }
25626b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2563299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2564a6f13a4aSGreg Roach
2565a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2566e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2567a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25681aa04befSGreg Roach
25691aa04befSGreg Roach                xml_set_element_handler(
25701aa04befSGreg Roach                    $repeat_parser,
25719d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25721aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25731aa04befSGreg Roach                    },
25749d454b6bSGreg Roach                    function ($parser, string $name): void {
25751aa04befSGreg Roach                        $this->endElement($parser, $name);
25761aa04befSGreg Roach                    }
25771aa04befSGreg Roach                );
25781aa04befSGreg Roach
25791aa04befSGreg Roach                xml_set_character_data_handler(
25801aa04befSGreg Roach                    $repeat_parser,
25819d454b6bSGreg Roach                    function ($parser, string $data): void {
25821aa04befSGreg Roach                        $this->characterData($parser, $data);
25831aa04befSGreg Roach                    }
25841aa04befSGreg Roach                );
2585a6f13a4aSGreg Roach
2586a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25876ccdf4f0SGreg 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)));
2588a6f13a4aSGreg Roach                }
2589a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2590a6f13a4aSGreg Roach            }
2591a6f13a4aSGreg Roach            // Clean up the list array
259213abd6f3SGreg Roach            $this->list   = [];
2593e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2594a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2595a6f13a4aSGreg Roach        }
259665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2597a6f13a4aSGreg Roach    }
2598a6f13a4aSGreg Roach
2599a6f13a4aSGreg Roach    /**
2600fab8f067SGreg Roach     * Handle <generation />
2601a6f13a4aSGreg Roach     * Prints the number of generations
26028ba2e626SGreg Roach     *
26038ba2e626SGreg Roach     * @return void
2604a6f13a4aSGreg Roach     */
2605b702978eSGreg Roach    protected function generationStartHandler(): void
2606c1010edaSGreg Roach    {
2607589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2608a6f13a4aSGreg Roach    }
2609a6f13a4aSGreg Roach
2610a6f13a4aSGreg Roach    /**
2611fab8f067SGreg Roach     * Handle <newPage />
2612a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
26138ba2e626SGreg Roach     *
26148ba2e626SGreg Roach     * @return void
2615a6f13a4aSGreg Roach     */
2616b702978eSGreg Roach    protected function newPageStartHandler(): void
2617c1010edaSGreg Roach    {
26187a6ee1acSGreg Roach        $temp = 'addpage';
2619e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2620a6f13a4aSGreg Roach    }
2621a6f13a4aSGreg Roach
2622a6f13a4aSGreg Roach    /**
2623fab8f067SGreg Roach     * Handle </title>
26248ba2e626SGreg Roach     *
26258ba2e626SGreg Roach     * @return void
2626a6f13a4aSGreg Roach     */
2627b702978eSGreg Roach    protected function titleEndHandler(): void
2628c1010edaSGreg Roach    {
26292836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2630a6f13a4aSGreg Roach    }
2631a6f13a4aSGreg Roach
2632a6f13a4aSGreg Roach    /**
2633fab8f067SGreg Roach     * Handle </description>
26348ba2e626SGreg Roach     *
26358ba2e626SGreg Roach     * @return void
2636a6f13a4aSGreg Roach     */
2637b702978eSGreg Roach    protected function descriptionEndHandler(): void
2638c1010edaSGreg Roach    {
26392836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2640a6f13a4aSGreg Roach    }
2641729ce104SGreg Roach
2642729ce104SGreg Roach    /**
264376692c8bSGreg Roach     * Create a list of all descendants.
264476692c8bSGreg Roach     *
26451c7df322SGreg Roach     * @param array<Individual> $list
2646729ce104SGreg Roach     * @param string            $pid
2647729ce104SGreg Roach     * @param bool              $parents
2648729ce104SGreg Roach     * @param int               $generations
26498ba2e626SGreg Roach     *
26508ba2e626SGreg Roach     * @return void
2651729ce104SGreg Roach     */
26523b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2653c1010edaSGreg Roach    {
26546b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2655729ce104SGreg Roach        if ($person === null) {
2656729ce104SGreg Roach            return;
2657729ce104SGreg Roach        }
2658729ce104SGreg Roach        if (!isset($list[$pid])) {
2659729ce104SGreg Roach            $list[$pid] = $person;
2660729ce104SGreg Roach        }
2661729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2662729ce104SGreg Roach            $list[$pid]->generation = 0;
2663729ce104SGreg Roach        }
266439ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2665729ce104SGreg Roach            if ($parents) {
266639ca88baSGreg Roach                $husband = $family->husband();
266739ca88baSGreg Roach                $wife    = $family->wife();
2668729ce104SGreg Roach                if ($husband) {
2669c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2670729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2671c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2672729ce104SGreg Roach                    } else {
2673c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2674729ce104SGreg Roach                    }
2675729ce104SGreg Roach                }
2676729ce104SGreg Roach                if ($wife) {
2677c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2678729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2679c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2680729ce104SGreg Roach                    } else {
2681c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2682729ce104SGreg Roach                    }
2683729ce104SGreg Roach                }
2684729ce104SGreg Roach            }
2685820b62dfSGreg Roach
268639ca88baSGreg Roach            $children = $family->children();
2687820b62dfSGreg Roach
2688729ce104SGreg Roach            foreach ($children as $child) {
2689729ce104SGreg Roach                if ($child) {
2690c0935879SGreg Roach                    $list[$child->xref()] = $child;
2691820b62dfSGreg Roach
2692729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2693c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2694729ce104SGreg Roach                    } else {
2695c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2696729ce104SGreg Roach                    }
2697729ce104SGreg Roach                }
2698729ce104SGreg Roach            }
2699729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2700729ce104SGreg Roach                foreach ($children as $child) {
2701c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2702729ce104SGreg Roach                }
2703729ce104SGreg Roach            }
2704729ce104SGreg Roach        }
2705729ce104SGreg Roach    }
2706729ce104SGreg Roach
2707729ce104SGreg Roach    /**
270876692c8bSGreg Roach     * Create a list of all ancestors.
270976692c8bSGreg Roach     *
27101c7df322SGreg Roach     * @param array<Individual> $list
2711729ce104SGreg Roach     * @param string            $pid
2712729ce104SGreg Roach     * @param bool              $children
2713729ce104SGreg Roach     * @param int               $generations
27148ba2e626SGreg Roach     *
27158ba2e626SGreg Roach     * @return void
2716729ce104SGreg Roach     */
27173b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2718c1010edaSGreg Roach    {
271913abd6f3SGreg Roach        $genlist                = [$pid];
2720729ce104SGreg Roach        $list[$pid]->generation = 1;
2721729ce104SGreg Roach        while (count($genlist) > 0) {
2722729ce104SGreg Roach            $id = array_shift($genlist);
2723dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2724729ce104SGreg Roach                continue; // id can be something like “empty7”
2725729ce104SGreg Roach            }
27266b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
272739ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
272839ca88baSGreg Roach                $husband = $family->husband();
272939ca88baSGreg Roach                $wife    = $family->wife();
2730729ce104SGreg Roach                if ($husband) {
2731c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2732c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2733729ce104SGreg Roach                }
2734729ce104SGreg Roach                if ($wife) {
2735c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2736c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2737729ce104SGreg Roach                }
2738729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2739729ce104SGreg Roach                    if ($husband) {
2740c0935879SGreg Roach                        $genlist[] = $husband->xref();
2741729ce104SGreg Roach                    }
2742729ce104SGreg Roach                    if ($wife) {
2743c0935879SGreg Roach                        $genlist[] = $wife->xref();
2744729ce104SGreg Roach                    }
2745729ce104SGreg Roach                }
2746729ce104SGreg Roach                if ($children) {
274739ca88baSGreg Roach                    foreach ($family->children() as $child) {
2748c0935879SGreg Roach                        $list[$child->xref()] = $child;
2749e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2750729ce104SGreg Roach                    }
2751729ce104SGreg Roach                }
2752729ce104SGreg Roach            }
2753729ce104SGreg Roach        }
2754729ce104SGreg Roach    }
2755729ce104SGreg Roach
2756729ce104SGreg Roach    /**
2757729ce104SGreg Roach     * get gedcom tag value
2758729ce104SGreg Roach     *
2759729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2760729ce104SGreg 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
2761729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2762729ce104SGreg Roach     *
2763729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2764729ce104SGreg Roach     */
2765b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2766c1010edaSGreg Roach    {
2767b2448a1bSGreg Roach        if ($gedrec === '') {
2768729ce104SGreg Roach            return '';
2769729ce104SGreg Roach        }
2770729ce104SGreg Roach        $tags      = explode(':', $tag);
2771729ce104SGreg Roach        $origlevel = $level;
2772b2448a1bSGreg Roach        if ($level === 0) {
27733c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2774729ce104SGreg Roach        }
2775729ce104SGreg Roach
2776729ce104SGreg Roach        $subrec = $gedrec;
2777f71a7dedSGreg Roach        $t = 'XXXX';
2778729ce104SGreg Roach        foreach ($tags as $t) {
2779729ce104SGreg Roach            $lastsubrec = $subrec;
2780b315f3e1SGreg Roach            $subrec     = self::getSubRecord($level, "$level $t", $subrec);
2781729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2782729ce104SGreg Roach                $level--;
2783b315f3e1SGreg Roach                $subrec = self::getSubRecord($level, "$level $t", $lastsubrec);
2784729ce104SGreg Roach            }
2785729ce104SGreg Roach            if (empty($subrec)) {
2786044416d2SGreg Roach                if ($t === 'TITL') {
2787b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "$level ABBR", $lastsubrec);
2788729ce104SGreg Roach                    if (!empty($subrec)) {
27897a6ee1acSGreg Roach                        $t = 'ABBR';
2790729ce104SGreg Roach                    }
2791729ce104SGreg Roach                }
2792b2448a1bSGreg Roach                if ($subrec === '') {
2793729ce104SGreg Roach                    if ($level > 0) {
2794729ce104SGreg Roach                        $level--;
2795729ce104SGreg Roach                    }
2796b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "@ $t", $gedrec);
2797b2448a1bSGreg Roach                    if ($subrec === '') {
2798729ce104SGreg Roach                        return '';
2799729ce104SGreg Roach                    }
2800729ce104SGreg Roach                }
2801729ce104SGreg Roach            }
2802729ce104SGreg Roach            $level++;
2803729ce104SGreg Roach        }
2804729ce104SGreg Roach        $level--;
2805729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2806f71a7dedSGreg Roach        if ($ct === 0) {
2807729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2808729ce104SGreg Roach        }
2809f71a7dedSGreg Roach        if ($ct === 0) {
2810729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2811729ce104SGreg Roach        }
2812729ce104SGreg Roach        if ($ct > 0) {
2813729ce104SGreg Roach            $value = trim($match[1]);
2814044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
28156b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2816ff166e64SGreg Roach                if ($note instanceof Note) {
2817729ce104SGreg Roach                    $value = $note->getNote();
2818729ce104SGreg Roach                } else {
2819729ce104SGreg Roach                    //-- set the value to the id without the @
2820729ce104SGreg Roach                    $value = $match[1];
2821729ce104SGreg Roach                }
2822729ce104SGreg Roach            }
2823f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
2824b315f3e1SGreg Roach                $value .= self::getCont($level + 1, $subrec);
2825729ce104SGreg Roach            }
2826729ce104SGreg Roach
2827729ce104SGreg Roach            return $value;
2828729ce104SGreg Roach        }
2829729ce104SGreg Roach
28307a6ee1acSGreg Roach        return '';
2831729ce104SGreg Roach    }
2832d1286247SGreg Roach
2833d1286247SGreg Roach    /**
2834d1286247SGreg Roach     * Replace variable identifiers with their values.
2835d1286247SGreg Roach     *
2836d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
283782759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2838d1286247SGreg Roach     *
2839d1286247SGreg Roach     * @return string
2840d1286247SGreg Roach     */
28418f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2842c1010edaSGreg Roach    {
2843d1286247SGreg Roach        return preg_replace_callback(
2844d1286247SGreg Roach            '/\$(\w+)/',
284518d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28462118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
284782759250SGreg Roach                    if ($quote) {
28482118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2849b2ce94c6SRico Sonntag                    }
2850b2ce94c6SRico Sonntag
28512118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
285282759250SGreg Roach                }
2853b2ce94c6SRico Sonntag
2854d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28553d7a8a4cSGreg Roach
2856d1286247SGreg Roach                return '$' . $matches[1];
2857d1286247SGreg Roach            },
2858d1286247SGreg Roach            $expression
2859d1286247SGreg Roach        );
2860d1286247SGreg Roach    }
2861a6f13a4aSGreg Roach}
2862