xref: /webtrees/app/Report/ReportParserGenerate.php (revision 136a8aba35984c6e731bd992888a61cc7b2ca756)
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
1844e69366aSGreg Roach    private FilesystemOperator $data_filesystem;
185a04bb9a2SGreg Roach
18676692c8bSGreg Roach    /**
18776692c8bSGreg Roach     * Create a parser for a report
18876692c8bSGreg Roach     *
18976692c8bSGreg Roach     * @param string               $report The XML filename
190b6f35a76SGreg Roach     * @param AbstractRenderer     $report_root
19109482a55SGreg Roach     * @param array<array<string>> $vars
192299d100dSGreg Roach     * @param Tree                 $tree
193f7cf8a15SGreg Roach     * @param FilesystemOperator   $data_filesystem
19476692c8bSGreg Roach     */
195a04bb9a2SGreg Roach    public function __construct(
196a04bb9a2SGreg Roach        string $report,
197b6f35a76SGreg Roach        AbstractRenderer $report_root,
198a04bb9a2SGreg Roach        array $vars,
199a04bb9a2SGreg Roach        Tree $tree,
200f7cf8a15SGreg Roach        FilesystemOperator $data_filesystem
201a04bb9a2SGreg Roach    ) {
202299d100dSGreg Roach        $this->report          = $report;
203e8e7866bSGreg Roach        $this->report_root     = $report_root;
204e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20559f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
206d1286247SGreg Roach        $this->vars            = $vars;
207299d100dSGreg Roach        $this->tree            = $tree;
208a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
209299d100dSGreg Roach
21076f666f4SGreg Roach        parent::__construct($report);
211a6f13a4aSGreg Roach    }
212a6f13a4aSGreg Roach
213a6f13a4aSGreg Roach    /**
214b315f3e1SGreg Roach     * get a gedcom subrecord
215b315f3e1SGreg Roach     *
216b315f3e1SGreg Roach     * searches a gedcom record and returns a subrecord of it. A subrecord is defined starting at a
217b315f3e1SGreg Roach     * line with level N and all subsequent lines greater than N until the next N level is reached.
218b315f3e1SGreg Roach     * For example, the following is a BIRT subrecord:
219b315f3e1SGreg Roach     * <code>1 BIRT
220b315f3e1SGreg Roach     * 2 DATE 1 JAN 1900
221b315f3e1SGreg Roach     * 2 PLAC Phoenix, Maricopa, Arizona</code>
222b315f3e1SGreg Roach     * The following example is the DATE subrecord of the above BIRT subrecord:
223b315f3e1SGreg Roach     * <code>2 DATE 1 JAN 1900</code>
224b315f3e1SGreg Roach     *
225b315f3e1SGreg Roach     * @param int    $level   the N level of the subrecord to get
226b315f3e1SGreg Roach     * @param string $tag     a gedcom tag or string to search for in the record (ie 1 BIRT or 2 DATE)
227b315f3e1SGreg Roach     * @param string $gedrec  the parent gedcom record to search in
228b315f3e1SGreg Roach     * @param int    $num     this allows you to specify which matching <var>$tag</var> to get. Oftentimes a
229b315f3e1SGreg Roach     *                        gedcom record will have more that 1 of the same type of subrecord. An individual may have
230b315f3e1SGreg Roach     *                        multiple events for example. Passing $num=1 would get the first 1. Passing $num=2 would get the
231b315f3e1SGreg Roach     *                        second one, etc.
232b315f3e1SGreg Roach     *
233b315f3e1SGreg Roach     * @return string the subrecord that was found or an empty string "" if not found.
234b315f3e1SGreg Roach     */
235b315f3e1SGreg Roach    public static function getSubRecord(int $level, string $tag, string $gedrec, int $num = 1): string
236b315f3e1SGreg Roach    {
237b315f3e1SGreg Roach        if ($gedrec === '') {
238b315f3e1SGreg Roach            return '';
239b315f3e1SGreg Roach        }
240b315f3e1SGreg Roach        // -- adding \n before and after gedrec
241b315f3e1SGreg Roach        $gedrec       = "\n" . $gedrec . "\n";
242b315f3e1SGreg Roach        $tag          = trim($tag);
243b315f3e1SGreg Roach        $searchTarget = "~[\n]" . $tag . "[\s]~";
244b315f3e1SGreg Roach        $ct           = preg_match_all($searchTarget, $gedrec, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
245b315f3e1SGreg Roach        if ($ct === 0) {
246b315f3e1SGreg Roach            return '';
247b315f3e1SGreg Roach        }
248b315f3e1SGreg Roach        if ($ct < $num) {
249b315f3e1SGreg Roach            return '';
250b315f3e1SGreg Roach        }
251b315f3e1SGreg Roach        $pos1 = $match[$num - 1][0][1];
252b315f3e1SGreg Roach        $pos2 = strpos($gedrec, "\n$level", $pos1 + 1);
253b315f3e1SGreg Roach        if (!$pos2) {
254b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\n1", $pos1 + 1);
255b315f3e1SGreg Roach        }
256b315f3e1SGreg Roach        if (!$pos2) {
257b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\nWT_", $pos1 + 1); // WT_SPOUSE, WT_FAMILY_ID ...
258b315f3e1SGreg Roach        }
259b315f3e1SGreg Roach        if (!$pos2) {
260b315f3e1SGreg Roach            return ltrim(substr($gedrec, $pos1));
261b315f3e1SGreg Roach        }
262b315f3e1SGreg Roach        $subrec = substr($gedrec, $pos1, $pos2 - $pos1);
263b315f3e1SGreg Roach
264b315f3e1SGreg Roach        return ltrim($subrec);
265b315f3e1SGreg Roach    }
266b315f3e1SGreg Roach
267b315f3e1SGreg Roach    /**
268b315f3e1SGreg Roach     * get CONT lines
269b315f3e1SGreg Roach     *
270b315f3e1SGreg Roach     * get the N+1 CONT or CONC lines of a gedcom subrecord
271b315f3e1SGreg Roach     *
272b315f3e1SGreg Roach     * @param int    $nlevel the level of the CONT lines to get
273b315f3e1SGreg Roach     * @param string $nrec   the gedcom subrecord to search in
274b315f3e1SGreg Roach     *
275b315f3e1SGreg Roach     * @return string a string with all CONT lines merged
276b315f3e1SGreg Roach     */
277b315f3e1SGreg Roach    public static function getCont(int $nlevel, string $nrec): string
278b315f3e1SGreg Roach    {
279b315f3e1SGreg Roach        $text = '';
280b315f3e1SGreg Roach
281b315f3e1SGreg Roach        $subrecords = explode("\n", $nrec);
282b315f3e1SGreg Roach        foreach ($subrecords as $thisSubrecord) {
283b315f3e1SGreg Roach            if (substr($thisSubrecord, 0, 2) !== $nlevel . ' ') {
284b315f3e1SGreg Roach                continue;
285b315f3e1SGreg Roach            }
286b315f3e1SGreg Roach            $subrecordType = substr($thisSubrecord, 2, 4);
287b315f3e1SGreg Roach            if ($subrecordType === 'CONT') {
288b315f3e1SGreg Roach                $text .= "\n" . substr($thisSubrecord, 7);
289b315f3e1SGreg Roach            }
290b315f3e1SGreg Roach        }
291b315f3e1SGreg Roach
292b315f3e1SGreg Roach        return $text;
293b315f3e1SGreg Roach    }
294b315f3e1SGreg Roach
295b315f3e1SGreg Roach    /**
296a6f13a4aSGreg Roach     * XML start element handler
297a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
298a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
299a6f13a4aSGreg Roach     *
300a6f13a4aSGreg Roach     * @param resource      $parser the resource handler for the XML parser
301a6f13a4aSGreg Roach     * @param string        $name   the name of the XML element parsed
30209482a55SGreg Roach     * @param array<string> $attrs  an array of key value pairs for the attributes
30318d7a90dSGreg Roach     *
30418d7a90dSGreg Roach     * @return void
305a6f13a4aSGreg Roach     */
306af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
307c1010edaSGreg Roach    {
30813abd6f3SGreg Roach        $newattrs = [];
309a6f13a4aSGreg Roach
310a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
311a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
312e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
313d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
314a6f13a4aSGreg Roach                }
315a6f13a4aSGreg Roach            }
316a6f13a4aSGreg Roach            $newattrs[$key] = $value;
317a6f13a4aSGreg Roach        }
318a6f13a4aSGreg Roach        $attrs = $newattrs;
3197a6ee1acSGreg 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')) {
320a0e2939aSGreg Roach            $method = $name . 'StartHandler';
321208e9f76SGreg Roach
322a0e2939aSGreg Roach            if (method_exists($this, $method)) {
323b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
324a6f13a4aSGreg Roach            }
325a6f13a4aSGreg Roach        }
326a6f13a4aSGreg Roach    }
327a6f13a4aSGreg Roach
328a6f13a4aSGreg Roach    /**
329a6f13a4aSGreg Roach     * XML end element handler
330a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
331a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
332a6f13a4aSGreg Roach     *
333a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
334a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
33518d7a90dSGreg Roach     *
33618d7a90dSGreg Roach     * @return void
337a6f13a4aSGreg Roach     */
338af14d238SGreg Roach    protected function endElement($parser, string $name): void
339c1010edaSGreg Roach    {
3407a6ee1acSGreg 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')) {
341a0e2939aSGreg Roach            $method = $name . 'EndHandler';
342a0e2939aSGreg Roach
343a0e2939aSGreg Roach            if (method_exists($this, $method)) {
344b19e047dSGreg Roach                call_user_func([$this, $method]);
345a6f13a4aSGreg Roach            }
346a6f13a4aSGreg Roach        }
347a6f13a4aSGreg Roach    }
348a6f13a4aSGreg Roach
349a6f13a4aSGreg Roach    /**
350a6f13a4aSGreg Roach     * XML character data handler
351a6f13a4aSGreg Roach     *
352a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
353a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
35418d7a90dSGreg Roach     *
35518d7a90dSGreg Roach     * @return void
356a6f13a4aSGreg Roach     */
35724f2a3afSGreg Roach    protected function characterData($parser, string $data): void
358c1010edaSGreg Roach    {
359e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
360a6f13a4aSGreg Roach            $this->current_element->addText($data);
361a6f13a4aSGreg Roach        }
362a6f13a4aSGreg Roach    }
363a6f13a4aSGreg Roach
364a6f13a4aSGreg Roach    /**
365fab8f067SGreg Roach     * Handle <style>
366a6f13a4aSGreg Roach     *
36709482a55SGreg Roach     * @param array<string> $attrs
3688ba2e626SGreg Roach     *
3698ba2e626SGreg Roach     * @return void
370a6f13a4aSGreg Roach     */
371b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
372c1010edaSGreg Roach    {
373a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
3746ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
375a6f13a4aSGreg Roach        }
376a6f13a4aSGreg Roach
377a6f13a4aSGreg Roach        // array Style that will be passed on
37813abd6f3SGreg Roach        $s = [];
379a6f13a4aSGreg Roach
380a6f13a4aSGreg Roach        // string Name af the style
381a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
382a6f13a4aSGreg Roach
383a6f13a4aSGreg Roach        // string Name of the DEFAULT font
384208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
385a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
386a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
387a6f13a4aSGreg Roach        }
388a6f13a4aSGreg Roach
389a6f13a4aSGreg Roach        // int The size of the font in points
390208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
391a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
39259597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
39359597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
39459597b37SGreg Roach        }
395a6f13a4aSGreg Roach
396a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3977a6ee1acSGreg Roach        $s['style'] = '';
398a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
399a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
400a6f13a4aSGreg Roach        }
401a6f13a4aSGreg Roach
402e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
403a6f13a4aSGreg Roach    }
404a6f13a4aSGreg Roach
405a6f13a4aSGreg Roach    /**
406fab8f067SGreg Roach     * Handle <doc>
407a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
408a6f13a4aSGreg Roach     *
40909482a55SGreg Roach     * @param array<string> $attrs
4108ba2e626SGreg Roach     *
4118ba2e626SGreg Roach     * @return void
412a6f13a4aSGreg Roach     */
413b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
414c1010edaSGreg Roach    {
415e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
416a6f13a4aSGreg Roach
417a6f13a4aSGreg Roach        // Custom page width
418a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
419208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
420a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
421a6f13a4aSGreg Roach        // Custom Page height
422a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
423208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
424a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
425a6f13a4aSGreg Roach
426a6f13a4aSGreg Roach        // Left Margin
427a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
4287a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
429208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
430a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
431208e9f76SGreg 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))
432a6f13a4aSGreg Roach            }
433a6f13a4aSGreg Roach        }
434a6f13a4aSGreg Roach        // Right Margin
435a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
4367a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
437208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
438a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
439208e9f76SGreg 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))
440a6f13a4aSGreg Roach            }
441a6f13a4aSGreg Roach        }
442a6f13a4aSGreg Roach        // Top Margin
443a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
4447a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
445208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
446a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
447208e9f76SGreg 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))
448a6f13a4aSGreg Roach            }
449a6f13a4aSGreg Roach        }
450a6f13a4aSGreg Roach        // Bottom Margin
451a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
4527a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
453208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
454a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
455208e9f76SGreg 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))
456a6f13a4aSGreg Roach            }
457a6f13a4aSGreg Roach        }
458a6f13a4aSGreg Roach        // Header Margin
459a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
4607a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
461208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
462a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
463208e9f76SGreg 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))
464a6f13a4aSGreg Roach            }
465a6f13a4aSGreg Roach        }
466a6f13a4aSGreg Roach        // Footer Margin
467a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
4687a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
469208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
470a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
471208e9f76SGreg 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))
472a6f13a4aSGreg Roach            }
473a6f13a4aSGreg Roach        }
474a6f13a4aSGreg Roach
475a6f13a4aSGreg Roach        // Page Orientation
476a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
477044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4787a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
479044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4807a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
481a6f13a4aSGreg Roach            }
482a6f13a4aSGreg Roach        }
483a6f13a4aSGreg Roach        // Page Size
484a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
485208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
486a6f13a4aSGreg Roach        }
487a6f13a4aSGreg Roach
488a6f13a4aSGreg Roach        // Show Generated By...
489a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4907a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
491208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4927a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
493208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
494a6f13a4aSGreg Roach            }
495a6f13a4aSGreg Roach        }
496a6f13a4aSGreg Roach
497e8e7866bSGreg Roach        $this->wt_report->setup();
498a6f13a4aSGreg Roach    }
499a6f13a4aSGreg Roach
500a6f13a4aSGreg Roach    /**
501fab8f067SGreg Roach     * Handle </doc>
5028ba2e626SGreg Roach     *
5038ba2e626SGreg Roach     * @return void
504a6f13a4aSGreg Roach     */
505b702978eSGreg Roach    protected function docEndHandler(): void
506c1010edaSGreg Roach    {
507e8e7866bSGreg Roach        $this->wt_report->run();
508a6f13a4aSGreg Roach    }
509a6f13a4aSGreg Roach
510a6f13a4aSGreg Roach    /**
511fab8f067SGreg Roach     * Handle <header>
5128ba2e626SGreg Roach     *
5138ba2e626SGreg Roach     * @return void
514a6f13a4aSGreg Roach     */
515b702978eSGreg Roach    protected function headerStartHandler(): void
516c1010edaSGreg Roach    {
517a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
518e8e7866bSGreg Roach        $this->wt_report->clearHeader();
5197a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
520a6f13a4aSGreg Roach    }
521a6f13a4aSGreg Roach
522a6f13a4aSGreg Roach    /**
523fab8f067SGreg Roach     * Handle <body>
5248ba2e626SGreg Roach     *
5258ba2e626SGreg Roach     * @return void
526a6f13a4aSGreg Roach     */
527b702978eSGreg Roach    protected function bodyStartHandler(): void
528c1010edaSGreg Roach    {
5297a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
530a6f13a4aSGreg Roach    }
531a6f13a4aSGreg Roach
532a6f13a4aSGreg Roach    /**
533fab8f067SGreg Roach     * Handle <footer>
5348ba2e626SGreg Roach     *
5358ba2e626SGreg Roach     * @return void
536a6f13a4aSGreg Roach     */
537b702978eSGreg Roach    protected function footerStartHandler(): void
538c1010edaSGreg Roach    {
5397a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
540a6f13a4aSGreg Roach    }
541a6f13a4aSGreg Roach
542a6f13a4aSGreg Roach    /**
543fab8f067SGreg Roach     * Handle <cell>
544a6f13a4aSGreg Roach     *
54577bab461SGreg Roach     * @param array<string,string> $attrs
5468ba2e626SGreg Roach     *
5478ba2e626SGreg Roach     * @return void
548a6f13a4aSGreg Roach     */
549b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
550c1010edaSGreg Roach    {
551a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
55277bab461SGreg Roach        $align = $attrs['align'] ?? '';
553a6f13a4aSGreg Roach        // RTL supported left/right alignment
554044416d2SGreg Roach        if ($align === 'rightrtl') {
555e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5567a6ee1acSGreg Roach                $align = 'left';
557a6f13a4aSGreg Roach            } else {
5587a6ee1acSGreg Roach                $align = 'right';
559a6f13a4aSGreg Roach            }
560044416d2SGreg Roach        } elseif ($align === 'leftrtl') {
561e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5627a6ee1acSGreg Roach                $align = 'right';
563a6f13a4aSGreg Roach            } else {
5647a6ee1acSGreg Roach                $align = 'left';
565a6f13a4aSGreg Roach            }
566a6f13a4aSGreg Roach        }
567a6f13a4aSGreg Roach
56877bab461SGreg Roach        // The color to fill the background of this cell
56977bab461SGreg Roach        $bgcolor = $attrs['bgcolor'] ?? '';
570a6f13a4aSGreg Roach
57177bab461SGreg Roach        // Whether the background should be painted
57277bab461SGreg Roach        $fill = (int) ($attrs['fill'] ?? '0');
573a6f13a4aSGreg Roach
57477bab461SGreg Roach        // If true reset the last cell height
57577bab461SGreg Roach        $reseth = (bool) ($attrs['reseth'] ?? '1');
576a6f13a4aSGreg Roach
57777bab461SGreg Roach        // Whether a border should be printed around this box
57877bab461SGreg Roach        $border = $attrs['border'] ?? '';
57977bab461SGreg Roach
580a6f13a4aSGreg Roach        // string Border color in HTML code
58177bab461SGreg Roach        $bocolor = $attrs['bocolor'] ?? '';
582a6f13a4aSGreg Roach
58377bab461SGreg Roach        // Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
58477bab461SGreg Roach        $height = (int) ($attrs['height'] ?? '0');
58577bab461SGreg Roach
586a6f13a4aSGreg 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.
58777bab461SGreg Roach        $width = (int) ($attrs['width'] ?? '0');
588a6f13a4aSGreg Roach
58977bab461SGreg Roach        // Stretch character mode
59077bab461SGreg Roach        $stretch = (int) ($attrs['stretch'] ?? '0');
591a6f13a4aSGreg Roach
592a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
593c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
594a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5957a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
596c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
597a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
598a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5997a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
600a6f13a4aSGreg Roach                $left = 0;
601a6f13a4aSGreg Roach            }
602a6f13a4aSGreg Roach        }
603a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
604c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
605a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
6067a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
607c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
608a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
609a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
6107a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
611a6f13a4aSGreg Roach                $top = 0;
612a6f13a4aSGreg Roach            }
613a6f13a4aSGreg Roach        }
614a6f13a4aSGreg Roach
61577bab461SGreg Roach        // The name of the Style that should be used to render the text.
61677bab461SGreg Roach        $style = $attrs['style'] ?? '';
617a6f13a4aSGreg Roach
618a6f13a4aSGreg Roach        // string Text color in html code
61977bab461SGreg Roach        $tcolor = $attrs['tcolor'] ?? '';
620a6f13a4aSGreg Roach
621a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
622a6f13a4aSGreg Roach        $ln = 0;
623a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
624a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
625a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
6267a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
627a6f13a4aSGreg Roach                $ln = 0;
628a6f13a4aSGreg Roach            }
629a6f13a4aSGreg Roach        }
630a6f13a4aSGreg Roach
631044416d2SGreg Roach        if ($align === 'left') {
6327a6ee1acSGreg Roach            $align = 'L';
633044416d2SGreg Roach        } elseif ($align === 'right') {
6347a6ee1acSGreg Roach            $align = 'R';
635044416d2SGreg Roach        } elseif ($align === 'center') {
6367a6ee1acSGreg Roach            $align = 'C';
637044416d2SGreg Roach        } elseif ($align === 'justify') {
6387a6ee1acSGreg Roach            $align = 'J';
639a6f13a4aSGreg Roach        }
640a6f13a4aSGreg Roach
6419b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
642a6f13a4aSGreg Roach        $this->print_data         = true;
643a6f13a4aSGreg Roach
644e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
64524f2a3afSGreg Roach            (int) $width,
64624f2a3afSGreg Roach            (int) $height,
647a6f13a4aSGreg Roach            $border,
648a6f13a4aSGreg Roach            $align,
649a6f13a4aSGreg Roach            $bgcolor,
650a6f13a4aSGreg Roach            $style,
651a6f13a4aSGreg Roach            $ln,
652a6f13a4aSGreg Roach            $top,
653a6f13a4aSGreg Roach            $left,
654a6f13a4aSGreg Roach            $fill,
655a6f13a4aSGreg Roach            $stretch,
656a6f13a4aSGreg Roach            $bocolor,
657a6f13a4aSGreg Roach            $tcolor,
658a6f13a4aSGreg Roach            $reseth
659a6f13a4aSGreg Roach        );
660a6f13a4aSGreg Roach    }
661a6f13a4aSGreg Roach
662a6f13a4aSGreg Roach    /**
663fab8f067SGreg Roach     * Handle </cell>
6648ba2e626SGreg Roach     *
6658ba2e626SGreg Roach     * @return void
666a6f13a4aSGreg Roach     */
667b702978eSGreg Roach    protected function cellEndHandler(): void
668c1010edaSGreg Roach    {
669a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
670e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
671a6f13a4aSGreg Roach    }
672a6f13a4aSGreg Roach
673a6f13a4aSGreg Roach    /**
674fab8f067SGreg Roach     * Handle <now />
6758ba2e626SGreg Roach     *
6768ba2e626SGreg Roach     * @return void
677a6f13a4aSGreg Roach     */
678b702978eSGreg Roach    protected function nowStartHandler(): void
679c1010edaSGreg Roach    {
680d97083feSGreg Roach        $this->current_element->addText(Registry::timestampFactory()->now()->isoFormat('LLLL'));
681a6f13a4aSGreg Roach    }
682a6f13a4aSGreg Roach
683a6f13a4aSGreg Roach    /**
684fab8f067SGreg Roach     * Handle <pageNum />
6858ba2e626SGreg Roach     *
6868ba2e626SGreg Roach     * @return void
687a6f13a4aSGreg Roach     */
688b702978eSGreg Roach    protected function pageNumStartHandler(): void
689c1010edaSGreg Roach    {
6907a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
691a6f13a4aSGreg Roach    }
692a6f13a4aSGreg Roach
693a6f13a4aSGreg Roach    /**
694fab8f067SGreg Roach     * Handle <totalPages />
6958ba2e626SGreg Roach     *
6968ba2e626SGreg Roach     * @return void
697a6f13a4aSGreg Roach     */
698b702978eSGreg Roach    protected function totalPagesStartHandler(): void
699c1010edaSGreg Roach    {
7007a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
701a6f13a4aSGreg Roach    }
702a6f13a4aSGreg Roach
703a6f13a4aSGreg Roach    /**
704a6f13a4aSGreg Roach     * Called at the start of an element.
705a6f13a4aSGreg Roach     *
70609482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
7078ba2e626SGreg Roach     *
7088ba2e626SGreg Roach     * @return void
709a6f13a4aSGreg Roach     */
710b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
711c1010edaSGreg Roach    {
712a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
713a6f13a4aSGreg Roach            $this->process_gedcoms++;
714a6f13a4aSGreg Roach
715a6f13a4aSGreg Roach            return;
716a6f13a4aSGreg Roach        }
717a6f13a4aSGreg Roach
718a6f13a4aSGreg Roach        $tag       = $attrs['id'];
7197a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
7207a6ee1acSGreg Roach        $tags      = explode(':', $tag);
721a6f13a4aSGreg Roach        $newgedrec = '';
722a6f13a4aSGreg Roach        if (count($tags) < 2) {
7236b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
724299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
725a6f13a4aSGreg Roach        }
726a6f13a4aSGreg Roach        if (empty($newgedrec)) {
727a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
728a6f13a4aSGreg Roach            $newgedrec = '';
729a6f13a4aSGreg Roach            foreach ($tags as $tag) {
7307a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
731d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
732d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
733a6f13a4aSGreg Roach                    } else {
7346b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
735299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
736a6f13a4aSGreg Roach                    }
737a6f13a4aSGreg Roach                } else {
7387a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
73913abd6f3SGreg Roach                        $gmatch = [];
740a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7416b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
742299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
743a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
744a6f13a4aSGreg Roach                        } else {
745a6f13a4aSGreg Roach                            $newgedrec = '';
746a6f13a4aSGreg Roach                            break;
747a6f13a4aSGreg Roach                        }
748a6f13a4aSGreg Roach                    } else {
749b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
750b315f3e1SGreg Roach                        $newgedrec = self::getSubRecord($level, "$level $tag", $tgedrec);
751a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
752a6f13a4aSGreg Roach                    }
753a6f13a4aSGreg Roach                }
754a6f13a4aSGreg Roach            }
755a6f13a4aSGreg Roach        }
756a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7579b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
758a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
759a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
760a6f13a4aSGreg Roach                $this->fact = $match[2];
761a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
762a6f13a4aSGreg Roach            }
763a6f13a4aSGreg Roach        } else {
764a6f13a4aSGreg Roach            $this->process_gedcoms++;
765a6f13a4aSGreg Roach        }
766a6f13a4aSGreg Roach    }
767a6f13a4aSGreg Roach
768a6f13a4aSGreg Roach    /**
769a6f13a4aSGreg Roach     * Called at the end of an element.
7708ba2e626SGreg Roach     *
7718ba2e626SGreg Roach     * @return void
772a6f13a4aSGreg Roach     */
773b702978eSGreg Roach    protected function gedcomEndHandler(): void
774c1010edaSGreg Roach    {
775a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
776a6f13a4aSGreg Roach            $this->process_gedcoms--;
777a6f13a4aSGreg Roach        } else {
77865e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
779a6f13a4aSGreg Roach        }
780a6f13a4aSGreg Roach    }
781a6f13a4aSGreg Roach
782a6f13a4aSGreg Roach    /**
783fab8f067SGreg Roach     * Handle <textBox>
784a6f13a4aSGreg Roach     *
78509482a55SGreg Roach     * @param array<string> $attrs
7868ba2e626SGreg Roach     *
7878ba2e626SGreg Roach     * @return void
788a6f13a4aSGreg Roach     */
789b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
790c1010edaSGreg Roach    {
791a6f13a4aSGreg Roach        // string Background color code
7927a6ee1acSGreg Roach        $bgcolor = '';
793a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
794a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
795a6f13a4aSGreg Roach        }
796a6f13a4aSGreg Roach
797a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
798a6f13a4aSGreg Roach        $fill = true;
799a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
8007a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
801a6f13a4aSGreg Roach                $fill = false;
8027a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
803a6f13a4aSGreg Roach                $fill = true;
804a6f13a4aSGreg Roach            }
805a6f13a4aSGreg Roach        }
806a6f13a4aSGreg Roach
807a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
808a6f13a4aSGreg Roach        $border = false;
809a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
8107a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
811a6f13a4aSGreg Roach                $border = true;
8127a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
813a6f13a4aSGreg Roach                $border = false;
814a6f13a4aSGreg Roach            }
815a6f13a4aSGreg Roach        }
816a6f13a4aSGreg Roach
817a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
818a6f13a4aSGreg Roach        $height = 0;
819a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
820a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
821a6f13a4aSGreg Roach        }
822a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
823a6f13a4aSGreg Roach        $width = 0;
824a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
825a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
826a6f13a4aSGreg Roach        }
827a6f13a4aSGreg Roach
828a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
829c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
830a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
8317a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
832c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
833a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
834a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
8357a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
836a6f13a4aSGreg Roach                $left = 0;
837a6f13a4aSGreg Roach            }
838a6f13a4aSGreg Roach        }
839a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
840c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
841a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8427a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
843c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
844a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
845a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8467a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
847a6f13a4aSGreg Roach                $top = 0;
848a6f13a4aSGreg Roach            }
849a6f13a4aSGreg Roach        }
850a6f13a4aSGreg 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
851a6f13a4aSGreg Roach        $newline = false;
852a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8537a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
854a6f13a4aSGreg Roach                $newline = true;
8557a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
856a6f13a4aSGreg Roach                $newline = false;
857a6f13a4aSGreg Roach            }
858a6f13a4aSGreg Roach        }
859a6f13a4aSGreg Roach        // boolean
860a6f13a4aSGreg Roach        $pagecheck = true;
861a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8627a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
863a6f13a4aSGreg Roach                $pagecheck = false;
8647a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
865a6f13a4aSGreg Roach                $pagecheck = true;
866a6f13a4aSGreg Roach            }
867a6f13a4aSGreg Roach        }
868a6f13a4aSGreg Roach        // boolean Cell padding
869a6f13a4aSGreg Roach        $padding = true;
870a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8717a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
872a6f13a4aSGreg Roach                $padding = false;
8737a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
874a6f13a4aSGreg Roach                $padding = true;
875a6f13a4aSGreg Roach            }
876a6f13a4aSGreg Roach        }
877a6f13a4aSGreg Roach        // boolean Reset this box Height
878a6f13a4aSGreg Roach        $reseth = false;
879a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8807a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
881a6f13a4aSGreg Roach                $reseth = true;
8827a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
883a6f13a4aSGreg Roach                $reseth = false;
884a6f13a4aSGreg Roach            }
885a6f13a4aSGreg Roach        }
886a6f13a4aSGreg Roach
887a6f13a4aSGreg Roach        // string Style of rendering
8887a6ee1acSGreg Roach        $style = '';
889a6f13a4aSGreg Roach
8909b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
891a6f13a4aSGreg Roach        $this->print_data         = false;
892a6f13a4aSGreg Roach
8939b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
894e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
895a6f13a4aSGreg Roach            $width,
896a6f13a4aSGreg Roach            $height,
897a6f13a4aSGreg Roach            $border,
898a6f13a4aSGreg Roach            $bgcolor,
899a6f13a4aSGreg Roach            $newline,
900a6f13a4aSGreg Roach            $left,
901a6f13a4aSGreg Roach            $top,
902a6f13a4aSGreg Roach            $pagecheck,
903a6f13a4aSGreg Roach            $style,
904a6f13a4aSGreg Roach            $fill,
905a6f13a4aSGreg Roach            $padding,
906a6f13a4aSGreg Roach            $reseth
907a6f13a4aSGreg Roach        );
908a6f13a4aSGreg Roach    }
909a6f13a4aSGreg Roach
910a6f13a4aSGreg Roach    /**
911fab8f067SGreg Roach     * Handle <textBox>
9128ba2e626SGreg Roach     *
9138ba2e626SGreg Roach     * @return void
914a6f13a4aSGreg Roach     */
915b702978eSGreg Roach    protected function textBoxEndHandler(): void
916c1010edaSGreg Roach    {
917a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
918e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
91931d9777aSGreg Roach
92031d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
92131d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
92231d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
92331d9777aSGreg Roach
924e8e7866bSGreg Roach        $this->wt_report = array_pop($this->wt_report_stack);
925e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
926a6f13a4aSGreg Roach    }
927a6f13a4aSGreg Roach
928a6f13a4aSGreg Roach    /**
92976692c8bSGreg Roach     * XLM <Text>.
93076692c8bSGreg Roach     *
93109482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9328ba2e626SGreg Roach     *
9338ba2e626SGreg Roach     * @return void
934a6f13a4aSGreg Roach     */
935b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
936c1010edaSGreg Roach    {
9379b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
938a6f13a4aSGreg Roach        $this->print_data         = true;
939a6f13a4aSGreg Roach
940a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9417a6ee1acSGreg Roach        $style = '';
942a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
943a6f13a4aSGreg Roach            $style = $attrs['style'];
944a6f13a4aSGreg Roach        }
945a6f13a4aSGreg Roach
946a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9477a6ee1acSGreg Roach        $color = '';
948a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
949a6f13a4aSGreg Roach            $color = $attrs['color'];
950a6f13a4aSGreg Roach        }
951a6f13a4aSGreg Roach
952e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
953a6f13a4aSGreg Roach    }
954a6f13a4aSGreg Roach
955a6f13a4aSGreg Roach    /**
956fab8f067SGreg Roach     * Handle </text>
9578ba2e626SGreg Roach     *
9588ba2e626SGreg Roach     * @return void
959a6f13a4aSGreg Roach     */
960b702978eSGreg Roach    protected function textEndHandler(): void
961c1010edaSGreg Roach    {
962a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
963e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
964a6f13a4aSGreg Roach    }
965a6f13a4aSGreg Roach
966a6f13a4aSGreg Roach    /**
967fab8f067SGreg Roach     * Handle <getPersonName />
968a6f13a4aSGreg Roach     * Get the name
969a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
970a6f13a4aSGreg Roach     * 2. id is set with a record id
971a6f13a4aSGreg Roach     *
97209482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9738ba2e626SGreg Roach     *
9748ba2e626SGreg Roach     * @return void
975a6f13a4aSGreg Roach     */
976b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
977c1010edaSGreg Roach    {
9787a6ee1acSGreg Roach        $id    = '';
97913abd6f3SGreg Roach        $match = [];
980a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9817a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
982a6f13a4aSGreg Roach                $id = $match[1];
983a6f13a4aSGreg Roach            }
984a6f13a4aSGreg Roach        } else {
9857a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
986d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
987d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
988a6f13a4aSGreg Roach                }
989a6f13a4aSGreg Roach            } else {
9907a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
99113abd6f3SGreg Roach                    $gmatch = [];
992a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
993a6f13a4aSGreg Roach                        $id = $gmatch[1];
994a6f13a4aSGreg Roach                    }
995a6f13a4aSGreg Roach                } else {
996a6f13a4aSGreg Roach                    $id = $attrs['id'];
997a6f13a4aSGreg Roach                }
998a6f13a4aSGreg Roach            }
999a6f13a4aSGreg Roach        }
1000a6f13a4aSGreg Roach        if (!empty($id)) {
10016b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
10028f038c36SRico Sonntag            if ($record === null) {
1003a6f13a4aSGreg Roach                return;
1004a6f13a4aSGreg Roach            }
1005a6f13a4aSGreg Roach            if (!$record->canShowName()) {
1006a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
1007a6f13a4aSGreg Roach            } else {
100839ca88baSGreg Roach                $name = $record->fullName();
1009a6f13a4aSGreg Roach                $name = strip_tags($name);
1010a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
1011381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
1012a6f13a4aSGreg Roach                } else {
1013911124f5SGreg Roach                    $addname = (string) $record->alternateName();
1014a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
1015a6f13a4aSGreg Roach                    if (!empty($addname)) {
10167a6ee1acSGreg Roach                        $name .= ' ' . $addname;
1017a6f13a4aSGreg Roach                    }
1018a6f13a4aSGreg Roach                }
1019a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
1020a6f13a4aSGreg Roach            }
1021a6f13a4aSGreg Roach        }
1022a6f13a4aSGreg Roach    }
1023a6f13a4aSGreg Roach
1024a6f13a4aSGreg Roach    /**
1025fab8f067SGreg Roach     * Handle <gedcomValue />
1026a6f13a4aSGreg Roach     *
102709482a55SGreg Roach     * @param array<string> $attrs
10288ba2e626SGreg Roach     *
10298ba2e626SGreg Roach     * @return void
1030a6f13a4aSGreg Roach     */
1031b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1032c1010edaSGreg Roach    {
10337a6ee1acSGreg Roach        $id    = '';
103413abd6f3SGreg Roach        $match = [];
10357a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1036a6f13a4aSGreg Roach            $id = $match[1];
1037a6f13a4aSGreg Roach        }
1038a6f13a4aSGreg Roach
1039044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10407a6ee1acSGreg Roach            $useBreak = '1';
1041a6f13a4aSGreg Roach        } else {
10427a6ee1acSGreg Roach            $useBreak = '0';
1043a6f13a4aSGreg Roach        }
1044a6f13a4aSGreg Roach
1045a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1046a6f13a4aSGreg Roach        if (!empty($tag)) {
1047044416d2SGreg Roach            if ($tag === '@desc') {
1048a6f13a4aSGreg Roach                $value = $this->desc;
1049a6f13a4aSGreg Roach                $value = trim($value);
1050a6f13a4aSGreg Roach                $this->current_element->addText($value);
1051a6f13a4aSGreg Roach            }
1052044416d2SGreg Roach            if ($tag === '@id') {
1053a6f13a4aSGreg Roach                $this->current_element->addText($id);
1054a6f13a4aSGreg Roach            } else {
10557a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1056a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1057b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1058b2448a1bSGreg Roach                    if ($level === 0) {
1059a6f13a4aSGreg Roach                        $level++;
1060a6f13a4aSGreg Roach                    }
1061a6f13a4aSGreg Roach                } else {
1062b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1063a6f13a4aSGreg Roach                }
1064a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10653d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1066a6f13a4aSGreg Roach                switch (end($tags)) {
1067a6f13a4aSGreg Roach                    case 'DATE':
1068a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1069326dd299SGreg Roach                        $value = strip_tags($tmp->display());
1070a6f13a4aSGreg Roach                        break;
1071a6f13a4aSGreg Roach                    case 'PLAC':
1072299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1073392561bbSGreg Roach                        $value = $tmp->shortName();
1074a6f13a4aSGreg Roach                        break;
1075a6f13a4aSGreg Roach                }
1076044416d2SGreg Roach                if ($useBreak === '1') {
1077a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1078a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1079a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1080a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1081a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10823b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10833b3cfeeaSGreg Roach                        $value = substr($value, 4);
1084a6f13a4aSGreg Roach                    }
1085a6f13a4aSGreg Roach                }
1086d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10873cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10884d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
1089ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($value, $this->tree), ['br']);
10904d35caa7SGreg Roach                    } else {
1091ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($value, $this->tree), ['br']);
10924d35caa7SGreg Roach                    }
1093ab2c9152SGreg Roach                    $value = strtr($value, [MarkdownFactory::BREAK => ' ']);
1094a4d703aeSGreg Roach                }
1095a2a485c3SGreg Roach
1096a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1097381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1098a2a485c3SGreg Roach                }
1099a6f13a4aSGreg Roach                $this->current_element->addText($value);
1100a6f13a4aSGreg Roach            }
1101a6f13a4aSGreg Roach        }
1102a6f13a4aSGreg Roach    }
1103a6f13a4aSGreg Roach
1104a6f13a4aSGreg Roach    /**
1105fab8f067SGreg Roach     * Handle <repeatTag>
1106a6f13a4aSGreg Roach     *
110709482a55SGreg Roach     * @param array<string> $attrs
11088ba2e626SGreg Roach     *
11098ba2e626SGreg Roach     * @return void
1110a6f13a4aSGreg Roach     */
1111b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1112c1010edaSGreg Roach    {
1113a6f13a4aSGreg Roach        $this->process_repeats++;
1114a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1115a6f13a4aSGreg Roach            return;
1116a6f13a4aSGreg Roach        }
1117a6f13a4aSGreg Roach
11189b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
111913abd6f3SGreg Roach        $this->repeats         = [];
1120e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1121a6f13a4aSGreg Roach
1122e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1123a6f13a4aSGreg Roach        if (!empty($tag)) {
1124044416d2SGreg Roach            if ($tag === '@desc') {
1125a6f13a4aSGreg Roach                $value = $this->desc;
1126a6f13a4aSGreg Roach                $value = trim($value);
1127a6f13a4aSGreg Roach                $this->current_element->addText($value);
1128a6f13a4aSGreg Roach            } else {
11297a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11307a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1131b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1132b2448a1bSGreg Roach                if ($level === 0) {
1133a6f13a4aSGreg Roach                    $level++;
1134a6f13a4aSGreg Roach                }
1135a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1136a6f13a4aSGreg Roach                $t      = $tag;
1137a6f13a4aSGreg Roach                $count  = count($tags);
1138a6f13a4aSGreg Roach                $i      = 0;
1139a6f13a4aSGreg Roach                while ($i < $count) {
1140a6f13a4aSGreg Roach                    $t = $tags[$i];
1141a6f13a4aSGreg Roach                    if (!empty($t)) {
1142a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
1143b315f3e1SGreg Roach                            $subrec = self::getSubRecord($level, "$level $t", $subrec);
1144a6f13a4aSGreg Roach                            if (empty($subrec)) {
1145a6f13a4aSGreg Roach                                $level--;
1146b315f3e1SGreg Roach                                $subrec = self::getSubRecord($level, "@ $t", $this->gedrec);
1147a6f13a4aSGreg Roach                                if (empty($subrec)) {
1148a6f13a4aSGreg Roach                                    return;
1149a6f13a4aSGreg Roach                                }
1150a6f13a4aSGreg Roach                            }
1151a6f13a4aSGreg Roach                        }
1152a6f13a4aSGreg Roach                        $level++;
1153a6f13a4aSGreg Roach                    }
1154a6f13a4aSGreg Roach                    $i++;
1155a6f13a4aSGreg Roach                }
1156a6f13a4aSGreg Roach                $level--;
1157a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1158a6f13a4aSGreg Roach                $i     = 0;
1159a6f13a4aSGreg Roach                while ($i < $count) {
1160a6f13a4aSGreg Roach                    $i++;
1161a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1162b315f3e1SGreg Roach                    $subrecord = self::getSubRecord($level, "$level $t", $subrec, $i);
11638d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11646b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1165a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1166a9007102SGreg Roach                            continue;
1167a9007102SGreg Roach                        }
1168a9007102SGreg Roach                    }
1169a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1170a6f13a4aSGreg Roach                }
1171a6f13a4aSGreg Roach            }
1172a6f13a4aSGreg Roach        }
1173a6f13a4aSGreg Roach    }
1174a6f13a4aSGreg Roach
1175a6f13a4aSGreg Roach    /**
1176fab8f067SGreg Roach     * Handle </repeatTag>
11778ba2e626SGreg Roach     *
11788ba2e626SGreg Roach     * @return void
1179a6f13a4aSGreg Roach     */
1180b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1181c1010edaSGreg Roach    {
1182a6f13a4aSGreg Roach        $this->process_repeats--;
1183a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1184a6f13a4aSGreg Roach            return;
1185a6f13a4aSGreg Roach        }
1186a6f13a4aSGreg Roach
1187a6f13a4aSGreg Roach        // Check if there is anything to repeat
1188a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1189a6f13a4aSGreg Roach            // No need to load them if not used...
1190a6f13a4aSGreg Roach
1191a6f13a4aSGreg Roach            $lineoffset = 0;
1192a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1193a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1194a6f13a4aSGreg Roach            }
1195a6f13a4aSGreg Roach            //-- read the xml from the file
1196299d100dSGreg Roach            $lines = file($this->report);
1197dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1198a6f13a4aSGreg Roach                $lineoffset--;
1199a6f13a4aSGreg Roach            }
1200a6f13a4aSGreg Roach            $lineoffset++;
1201a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1202a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1203a6f13a4aSGreg Roach            // RepeatTag Level counter
1204a6f13a4aSGreg Roach            $count = 1;
1205a6f13a4aSGreg Roach            while (0 < $count) {
1206dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1207a6f13a4aSGreg Roach                    $count++;
1208dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1209a6f13a4aSGreg Roach                    $count--;
1210a6f13a4aSGreg Roach                }
1211a6f13a4aSGreg Roach                if (0 < $count) {
1212a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1213a6f13a4aSGreg Roach                }
1214a6f13a4aSGreg Roach                $line_nr++;
1215a6f13a4aSGreg Roach            }
1216a6f13a4aSGreg Roach            // No need to drag this
1217a6f13a4aSGreg Roach            unset($lines);
1218a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1219a6f13a4aSGreg Roach            // Save original values
12209b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1221a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1222a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1223a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1224a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1225e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1226a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
12271aa04befSGreg Roach
12281aa04befSGreg Roach                xml_set_element_handler(
12291aa04befSGreg Roach                    $repeat_parser,
12309d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12311aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12321aa04befSGreg Roach                    },
12339d454b6bSGreg Roach                    function ($parser, string $name): void {
12341aa04befSGreg Roach                        $this->endElement($parser, $name);
12351aa04befSGreg Roach                    }
12361aa04befSGreg Roach                );
12371aa04befSGreg Roach
12381aa04befSGreg Roach                xml_set_character_data_handler(
12391aa04befSGreg Roach                    $repeat_parser,
12409d454b6bSGreg Roach                    function ($parser, string $data): void {
12411aa04befSGreg Roach                        $this->characterData($parser, $data);
12421aa04befSGreg Roach                    }
12431aa04befSGreg Roach                );
12441aa04befSGreg Roach
1245a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12466ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1247a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1248a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1249a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1250a6f13a4aSGreg Roach                    ));
1251a6f13a4aSGreg Roach                }
1252a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1253a6f13a4aSGreg Roach            }
1254a6f13a4aSGreg Roach            // Restore original values
1255a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1256e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1257a6f13a4aSGreg Roach        }
125865e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1259a6f13a4aSGreg Roach    }
1260a6f13a4aSGreg Roach
1261a6f13a4aSGreg Roach    /**
1262a6f13a4aSGreg Roach     * Variable lookup
1263a6f13a4aSGreg Roach     * Retrieve predefined variables :
1264a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1265a6f13a4aSGreg Roach     *        1 EVEN This is a description
1266a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1267a6f13a4aSGreg Roach     * $ I18N::translate('....')
1268a6f13a4aSGreg Roach     * $ language_settings[]
1269a6f13a4aSGreg Roach     *
127009482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
12718ba2e626SGreg Roach     *
12728ba2e626SGreg Roach     * @return void
1273a6f13a4aSGreg Roach     */
1274b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1275c1010edaSGreg Roach    {
1276a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12776ccdf4f0SGreg 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));
1278a6f13a4aSGreg Roach        }
1279a6f13a4aSGreg Roach
1280a6f13a4aSGreg Roach        $var = $attrs['var'];
1281a6f13a4aSGreg Roach        // SetVar element preset variables
1282d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1283d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1284a6f13a4aSGreg Roach        } else {
1285a6f13a4aSGreg Roach            $tfact = $this->fact;
12867a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1287a6f13a4aSGreg Roach                // Use :
1288a6f13a4aSGreg Roach                // n TYPE This text if string
1289a6f13a4aSGreg Roach                $tfact = $this->type;
129097def6bcSGreg Roach            } else {
129197def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
129297def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
129397def6bcSGreg Roach
129497def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
129597def6bcSGreg Roach                        $tfact = $element->label();
129697def6bcSGreg Roach                        break;
1297a6f13a4aSGreg Roach                    }
129897def6bcSGreg Roach                }
129997def6bcSGreg Roach            }
130097def6bcSGreg Roach
130197def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
130297def6bcSGreg Roach
1303a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1304da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1305a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1306a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1307a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1308a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1309a6f13a4aSGreg Roach            }
1310a6f13a4aSGreg Roach        }
1311a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1312a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
13137a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1314a6f13a4aSGreg Roach                $g   = new Date($var);
1315a6f13a4aSGreg Roach                $var = $g->display();
1316a6f13a4aSGreg Roach            }
1317a6f13a4aSGreg Roach        }
1318a6f13a4aSGreg Roach        $this->current_element->addText($var);
13192836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1320a6f13a4aSGreg Roach    }
1321a6f13a4aSGreg Roach
1322a6f13a4aSGreg Roach    /**
1323fab8f067SGreg Roach     * Handle <facts>
132476692c8bSGreg Roach     *
132509482a55SGreg Roach     * @param array<string> $attrs
13268ba2e626SGreg Roach     *
13278ba2e626SGreg Roach     * @return void
1328a6f13a4aSGreg Roach     */
1329b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1330c1010edaSGreg Roach    {
1331a6f13a4aSGreg Roach        $this->process_repeats++;
1332a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1333a6f13a4aSGreg Roach            return;
1334a6f13a4aSGreg Roach        }
1335a6f13a4aSGreg Roach
13369b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
133713abd6f3SGreg Roach        $this->repeats         = [];
1338e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1339a6f13a4aSGreg Roach
13407a6ee1acSGreg Roach        $id    = '';
134113abd6f3SGreg Roach        $match = [];
13427a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1343a6f13a4aSGreg Roach            $id = $match[1];
1344a6f13a4aSGreg Roach        }
13457a6ee1acSGreg Roach        $tag = '';
1346a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1347a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1348a6f13a4aSGreg Roach        }
13497a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1350d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1351a6f13a4aSGreg Roach        }
1352a6f13a4aSGreg Roach
13536b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1354a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1355820b62dfSGreg Roach            $facts = $record->facts([], true);
135613abd6f3SGreg Roach            $this->repeats = [];
1357a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1358195b5e75SGreg Roach            foreach ($facts as $fact) {
1359d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1360d0889c63SGreg Roach
1361d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1362195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1363a6f13a4aSGreg Roach                }
1364a6f13a4aSGreg Roach            }
1365a6f13a4aSGreg Roach        } else {
136630158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1367d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1368138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1369a6f13a4aSGreg Roach                }
1370a6f13a4aSGreg Roach            }
1371a6f13a4aSGreg Roach        }
1372a6f13a4aSGreg Roach    }
1373a6f13a4aSGreg Roach
1374a6f13a4aSGreg Roach    /**
1375fab8f067SGreg Roach     * Handle </facts>
13768ba2e626SGreg Roach     *
13778ba2e626SGreg Roach     * @return void
1378a6f13a4aSGreg Roach     */
1379b702978eSGreg Roach    protected function factsEndHandler(): void
1380c1010edaSGreg Roach    {
1381a6f13a4aSGreg Roach        $this->process_repeats--;
1382a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1383a6f13a4aSGreg Roach            return;
1384a6f13a4aSGreg Roach        }
1385a6f13a4aSGreg Roach
1386a6f13a4aSGreg Roach        // Check if there is anything to repeat
1387a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1388e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1389a6f13a4aSGreg Roach            $lineoffset = 0;
1390a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1391a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1392a6f13a4aSGreg Roach            }
1393a6f13a4aSGreg Roach
1394a6f13a4aSGreg Roach            //-- read the xml from the file
1395299d100dSGreg Roach            $lines = file($this->report);
1396dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1397a6f13a4aSGreg Roach                $lineoffset--;
1398a6f13a4aSGreg Roach            }
1399a6f13a4aSGreg Roach            $lineoffset++;
1400a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1401a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1402a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1403a6f13a4aSGreg Roach            while ($line_nr < $i) {
1404a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1405a6f13a4aSGreg Roach                $line_nr++;
1406a6f13a4aSGreg Roach            }
1407a6f13a4aSGreg Roach            // No need to drag this
1408a6f13a4aSGreg Roach            unset($lines);
1409a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1410a6f13a4aSGreg Roach            // Save original values
14119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1412a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1413a6f13a4aSGreg Roach            $count                = count($this->repeats);
1414a6f13a4aSGreg Roach            $i                    = 0;
1415a6f13a4aSGreg Roach            while ($i < $count) {
1416a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1417a6f13a4aSGreg Roach                $this->fact   = '';
1418a6f13a4aSGreg Roach                $this->desc   = '';
1419a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1420a6f13a4aSGreg Roach                    $this->fact = $match[1];
1421a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
142213abd6f3SGreg Roach                        $tmatch = [];
1423a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1424a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1425a6f13a4aSGreg Roach                        } else {
1426a6f13a4aSGreg Roach                            $this->type = ' ';
1427a6f13a4aSGreg Roach                        }
1428a6f13a4aSGreg Roach                    }
1429a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
1430b315f3e1SGreg Roach                    $this->desc .= self::getCont(2, $this->gedrec);
1431a6f13a4aSGreg Roach                }
1432a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1433e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1434a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
14351aa04befSGreg Roach
14361aa04befSGreg Roach                xml_set_element_handler(
14371aa04befSGreg Roach                    $repeat_parser,
14389d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14391aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14401aa04befSGreg Roach                    },
14419d454b6bSGreg Roach                    function ($parser, string $name): void {
14421aa04befSGreg Roach                        $this->endElement($parser, $name);
14431aa04befSGreg Roach                    }
14441aa04befSGreg Roach                );
14451aa04befSGreg Roach
14461aa04befSGreg Roach                xml_set_character_data_handler(
14471aa04befSGreg Roach                    $repeat_parser,
14489d454b6bSGreg Roach                    function ($parser, string $data): void {
14491aa04befSGreg Roach                        $this->characterData($parser, $data);
14501aa04befSGreg Roach                    }
14511aa04befSGreg Roach                );
14521aa04befSGreg Roach
1453a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14546ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1455a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1456a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1457a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1458a6f13a4aSGreg Roach                    ));
1459a6f13a4aSGreg Roach                }
1460a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1461a6f13a4aSGreg Roach                $i++;
1462a6f13a4aSGreg Roach            }
1463a6f13a4aSGreg Roach            // Restore original values
1464e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1465a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1466a6f13a4aSGreg Roach        }
146765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1468a6f13a4aSGreg Roach    }
1469a6f13a4aSGreg Roach
1470a6f13a4aSGreg Roach    /**
1471a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1472d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1473a6f13a4aSGreg Roach     *
147409482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
14758ba2e626SGreg Roach     *
14768ba2e626SGreg Roach     * @return void
1477a6f13a4aSGreg Roach     */
1478b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1479c1010edaSGreg Roach    {
1480a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14816ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1482a6f13a4aSGreg Roach        }
1483a6f13a4aSGreg Roach
1484a6f13a4aSGreg Roach        $name  = $attrs['name'];
1485a6f13a4aSGreg Roach        $value = $attrs['value'];
148613abd6f3SGreg Roach        $match = [];
1487a6f13a4aSGreg Roach        // Current GEDCOM record strings
1488044416d2SGreg Roach        if ($value === '@ID') {
14897a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1490a6f13a4aSGreg Roach                $value = $match[1];
1491a6f13a4aSGreg Roach            }
1492044416d2SGreg Roach        } elseif ($value === '@fact') {
1493a6f13a4aSGreg Roach            $value = $this->fact;
1494044416d2SGreg Roach        } elseif ($value === '@desc') {
1495a6f13a4aSGreg Roach            $value = $this->desc;
1496044416d2SGreg Roach        } elseif ($value === '@generation') {
1497589feda3SGreg Roach            $value = (string) $this->generation;
1498a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
149913abd6f3SGreg Roach            $gmatch = [];
1500a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
15017a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1502a6f13a4aSGreg Roach            }
1503a6f13a4aSGreg Roach        }
1504a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1505d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1506a6f13a4aSGreg Roach        }
1507a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1508a6f13a4aSGreg Roach        $i     = 0;
1509a6f13a4aSGreg Roach        while ($i < $count) {
1510d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
15117a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1512a6f13a4aSGreg Roach            $i++;
1513a6f13a4aSGreg Roach        }
1514a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1515da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1516a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1517a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1518a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1519a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1520a6f13a4aSGreg Roach        }
152152868398SGreg Roach
1522a6f13a4aSGreg Roach        // Arithmetic functions
15233cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
152452868398SGreg Roach            // Create an expression language with the functions used by our reports.
152552868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1526c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1527c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
152852868398SGreg Roach
152952868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1530a6f13a4aSGreg Roach        }
153152868398SGreg Roach
1532dec352c1SGreg Roach        if (str_contains($value, '@')) {
15337a6ee1acSGreg Roach            $value = '';
1534a6f13a4aSGreg Roach        }
1535d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1536a6f13a4aSGreg Roach    }
1537a6f13a4aSGreg Roach
1538a6f13a4aSGreg Roach    /**
1539fab8f067SGreg Roach     * Handle <if>
1540a6f13a4aSGreg Roach     *
154109482a55SGreg Roach     * @param array<string> $attrs
15428ba2e626SGreg Roach     *
15438ba2e626SGreg Roach     * @return void
1544a6f13a4aSGreg Roach     */
1545b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1546c1010edaSGreg Roach    {
1547a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1548a6f13a4aSGreg Roach            $this->process_ifs++;
1549a6f13a4aSGreg Roach
1550a6f13a4aSGreg Roach            return;
1551a6f13a4aSGreg Roach        }
1552a6f13a4aSGreg Roach
1553a6f13a4aSGreg Roach        $condition = $attrs['condition'];
155482759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1555c1010edaSGreg Roach        $condition = str_replace([
1556c1010edaSGreg Roach            ' LT ',
1557c1010edaSGreg Roach            ' GT ',
1558c1010edaSGreg Roach        ], [
1559c1010edaSGreg Roach            '<',
1560c1010edaSGreg Roach            '>',
1561c1010edaSGreg Roach        ], $condition);
15623cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15637a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
156413abd6f3SGreg Roach        $match     = [];
15653cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1566a6f13a4aSGreg Roach        $i         = 0;
1567a6f13a4aSGreg Roach        while ($i < $count) {
1568a6f13a4aSGreg Roach            $id    = $match[$i][1];
1569a6f13a4aSGreg Roach            $value = '""';
1570044416d2SGreg Roach            if ($id === 'ID') {
15717a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1572a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1573a6f13a4aSGreg Roach                }
15747a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1575a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15767a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1577a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15787a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1579a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1580a6f13a4aSGreg Roach            } else {
1581b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1582b2448a1bSGreg Roach                if ($level === 0) {
1583a6f13a4aSGreg Roach                    $level++;
1584a6f13a4aSGreg Roach                }
15853d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1586a6f13a4aSGreg Roach                if (empty($value)) {
1587a6f13a4aSGreg Roach                    $level++;
15883d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1589a6f13a4aSGreg Roach                }
15908d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15915e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1592a6f13a4aSGreg Roach            }
1593a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1594a6f13a4aSGreg Roach            $i++;
1595a6f13a4aSGreg Roach        }
15965809450fSGreg Roach
1597cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1598cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1599c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1600c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1601cb63a60eSGreg Roach
1602cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
16035809450fSGreg Roach
1604a6f13a4aSGreg Roach        if (!$ret) {
1605a6f13a4aSGreg Roach            $this->process_ifs++;
1606a6f13a4aSGreg Roach        }
1607a6f13a4aSGreg Roach    }
1608a6f13a4aSGreg Roach
1609a6f13a4aSGreg Roach    /**
1610fab8f067SGreg Roach     * Handle </if>
16118ba2e626SGreg Roach     *
16128ba2e626SGreg Roach     * @return void
1613a6f13a4aSGreg Roach     */
1614b702978eSGreg Roach    protected function ifEndHandler(): void
1615c1010edaSGreg Roach    {
1616a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1617a6f13a4aSGreg Roach            $this->process_ifs--;
1618a6f13a4aSGreg Roach        }
1619a6f13a4aSGreg Roach    }
1620a6f13a4aSGreg Roach
1621a6f13a4aSGreg Roach    /**
1622fab8f067SGreg Roach     * Handle <footnote>
1623a6f13a4aSGreg Roach     * Collect the Footnote links
1624fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1625a6f13a4aSGreg Roach     *
162609482a55SGreg Roach     * @param array<string> $attrs
16278ba2e626SGreg Roach     *
16288ba2e626SGreg Roach     * @return void
1629a6f13a4aSGreg Roach     */
1630b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1631c1010edaSGreg Roach    {
16327a6ee1acSGreg Roach        $id = '';
16337a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1634a6f13a4aSGreg Roach            $id = $match[2];
1635a6f13a4aSGreg Roach        }
16366b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1637a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16389b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1639a6f13a4aSGreg Roach            $this->print_data         = true;
16407a6ee1acSGreg Roach            $style                    = '';
1641a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1642a6f13a4aSGreg Roach                $style = $attrs['style'];
1643a6f13a4aSGreg Roach            }
1644a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1645e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1646a6f13a4aSGreg Roach        } else {
1647a6f13a4aSGreg Roach            $this->print_data       = false;
1648a6f13a4aSGreg Roach            $this->process_footnote = false;
1649a6f13a4aSGreg Roach        }
1650a6f13a4aSGreg Roach    }
1651a6f13a4aSGreg Roach
1652a6f13a4aSGreg Roach    /**
1653fab8f067SGreg Roach     * Handle </footnote>
1654a6f13a4aSGreg Roach     * Print the collected Footnote data
16558ba2e626SGreg Roach     *
16568ba2e626SGreg Roach     * @return void
1657a6f13a4aSGreg Roach     */
1658b702978eSGreg Roach    protected function footnoteEndHandler(): void
1659c1010edaSGreg Roach    {
1660a6f13a4aSGreg Roach        if ($this->process_footnote) {
1661a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1662a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1663a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1664e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1665a6f13a4aSGreg Roach            }
1666a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1667a6f13a4aSGreg Roach        } else {
1668a6f13a4aSGreg Roach            $this->process_footnote = true;
1669a6f13a4aSGreg Roach        }
1670a6f13a4aSGreg Roach    }
1671a6f13a4aSGreg Roach
1672a6f13a4aSGreg Roach    /**
1673fab8f067SGreg Roach     * Handle <footnoteTexts />
16748ba2e626SGreg Roach     *
16758ba2e626SGreg Roach     * @return void
1676a6f13a4aSGreg Roach     */
1677b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1678c1010edaSGreg Roach    {
16797a6ee1acSGreg Roach        $temp = 'footnotetexts';
1680e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1681a6f13a4aSGreg Roach    }
1682a6f13a4aSGreg Roach
1683a6f13a4aSGreg Roach    /**
1684a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16858ba2e626SGreg Roach     *
16868ba2e626SGreg Roach     * @return void
1687a6f13a4aSGreg Roach     */
1688b702978eSGreg Roach    protected function brStartHandler(): void
1689c1010edaSGreg Roach    {
1690a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1691a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1692a6f13a4aSGreg Roach        }
1693a6f13a4aSGreg Roach    }
1694a6f13a4aSGreg Roach
1695a6f13a4aSGreg Roach    /**
1696fab8f067SGreg Roach     * Handle <sp />
1697fab8f067SGreg Roach     * Forced space
16988ba2e626SGreg Roach     *
16998ba2e626SGreg Roach     * @return void
1700a6f13a4aSGreg Roach     */
1701b702978eSGreg Roach    protected function spStartHandler(): void
1702c1010edaSGreg Roach    {
1703a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1704a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1705a6f13a4aSGreg Roach        }
1706a6f13a4aSGreg Roach    }
1707a6f13a4aSGreg Roach
1708a6f13a4aSGreg Roach    /**
1709fab8f067SGreg Roach     * Handle <highlightedImage />
171076692c8bSGreg Roach     *
171109482a55SGreg Roach     * @param array<string> $attrs
17128ba2e626SGreg Roach     *
17138ba2e626SGreg Roach     * @return void
1714a6f13a4aSGreg Roach     */
1715b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1716c1010edaSGreg Roach    {
1717a6f13a4aSGreg Roach        $id = '';
17187a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1719a6f13a4aSGreg Roach            $id = $match[1];
1720a6f13a4aSGreg Roach        }
1721a6f13a4aSGreg Roach
1722c21bdddcSGreg Roach        // Position the top corner of this box on the page
1723c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1724a6f13a4aSGreg Roach
1725c21bdddcSGreg Roach        // Position the left corner of this box on the page
1726c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1727a6f13a4aSGreg Roach
172883cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172983cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1730a6f13a4aSGreg Roach
1731a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
173283cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1733a6f13a4aSGreg Roach
173483cdc021SGreg Roach        // Width, height (or both).
1735c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1736c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1737a6f13a4aSGreg Roach
17386b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
17394a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
174086a63f51SGreg Roach
1741a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1742a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1743b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
174429518ad2SGreg Roach
1745a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17463c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17473c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1748a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17493c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17503c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1751a6f13a4aSGreg Roach            } else {
1752*136a8abaSGreg Roach                $width  = (float) $attributes[0];
1753*136a8abaSGreg Roach                $height = (float) $attributes[1];
1754a6f13a4aSGreg Roach            }
1755a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1756e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1757a6f13a4aSGreg Roach        }
1758a6f13a4aSGreg Roach    }
1759a6f13a4aSGreg Roach
1760a6f13a4aSGreg Roach    /**
1761fab8f067SGreg Roach     * Handle <image/>
176276692c8bSGreg Roach     *
176309482a55SGreg Roach     * @param array<string> $attrs
17648ba2e626SGreg Roach     *
17658ba2e626SGreg Roach     * @return void
1766a6f13a4aSGreg Roach     */
1767b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1768c1010edaSGreg Roach    {
176983cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1770c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1771a6f13a4aSGreg Roach
1772a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1773c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1774a6f13a4aSGreg Roach
177583cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
177683cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1777a6f13a4aSGreg Roach
1778a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
177983cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1780a6f13a4aSGreg Roach
178183cdc021SGreg Roach        // Width, height (or both).
1782c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1783c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1784a6f13a4aSGreg Roach
178583cdc021SGreg Roach        $file = $attrs['file'] ?? '';
178683cdc021SGreg Roach
1787044416d2SGreg Roach        if ($file === '@FILE') {
178813abd6f3SGreg Roach            $match = [];
1789a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17906b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17914a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1792cdf416fbSGreg Roach
1793a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1794a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1795b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
179629518ad2SGreg Roach
1797a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17983c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17993c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1800a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
18013c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
18023c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1803a6f13a4aSGreg Roach                    } else {
1804*136a8abaSGreg Roach                        $width  = (float) $attributes[0];
1805*136a8abaSGreg Roach                        $height = (float) $attributes[1];
1806a6f13a4aSGreg Roach                    }
1807a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1808e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1809a6f13a4aSGreg Roach                }
1810a6f13a4aSGreg Roach            }
1811a6f13a4aSGreg Roach        } else {
18127a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1813a6f13a4aSGreg Roach                $size = getimagesize($file);
1814a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1815a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1816a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1817a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1818a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1819a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1820a6f13a4aSGreg Roach                } else {
1821a6f13a4aSGreg Roach                    $width  = $size[0];
1822a6f13a4aSGreg Roach                    $height = $size[1];
1823a6f13a4aSGreg Roach                }
1824e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1825e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1826a6f13a4aSGreg Roach            }
1827a6f13a4aSGreg Roach        }
1828a6f13a4aSGreg Roach    }
1829a6f13a4aSGreg Roach
1830a6f13a4aSGreg Roach    /**
1831fab8f067SGreg Roach     * Handle <line>
1832a6f13a4aSGreg Roach     *
183309482a55SGreg Roach     * @param array<string> $attrs
18348ba2e626SGreg Roach     *
18358ba2e626SGreg Roach     * @return void
1836a6f13a4aSGreg Roach     */
1837b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1838c1010edaSGreg Roach    {
1839a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1840c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1841a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18427a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1843a6f13a4aSGreg Roach                $x1 = 0;
18447a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1845c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1846a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1847c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1848a6f13a4aSGreg Roach            }
1849a6f13a4aSGreg Roach        }
1850a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1851c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1852a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18537a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1854a6f13a4aSGreg Roach                $y1 = 0;
18557a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1856c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1857a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1858c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1859a6f13a4aSGreg Roach            }
1860a6f13a4aSGreg Roach        }
1861a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1862c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1863a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18647a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1865a6f13a4aSGreg Roach                $x2 = 0;
18667a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1867c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1868a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1869c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1870a6f13a4aSGreg Roach            }
1871a6f13a4aSGreg Roach        }
1872a6f13a4aSGreg Roach        // End vertical position
1873c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1874a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18757a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1876a6f13a4aSGreg Roach                $y2 = 0;
18777a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1878c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1879a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1880c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1881a6f13a4aSGreg Roach            }
1882a6f13a4aSGreg Roach        }
1883a6f13a4aSGreg Roach
1884e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1885e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1886a6f13a4aSGreg Roach    }
1887a6f13a4aSGreg Roach
1888a6f13a4aSGreg Roach    /**
1889fab8f067SGreg Roach     * Handle <list>
1890a6f13a4aSGreg Roach     *
189109482a55SGreg Roach     * @param array<string> $attrs
18928ba2e626SGreg Roach     *
18938ba2e626SGreg Roach     * @return void
1894a6f13a4aSGreg Roach     */
1895b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1896c1010edaSGreg Roach    {
1897a6f13a4aSGreg Roach        $this->process_repeats++;
1898a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1899a6f13a4aSGreg Roach            return;
1900a6f13a4aSGreg Roach        }
1901a6f13a4aSGreg Roach
190213abd6f3SGreg Roach        $match = [];
1903a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1904a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1905a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1906d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1907a6f13a4aSGreg Roach                $sortby = trim($sortby);
1908a6f13a4aSGreg Roach            }
1909a6f13a4aSGreg Roach        } else {
19107a6ee1acSGreg Roach            $sortby = 'NAME';
1911a6f13a4aSGreg Roach        }
1912a6f13a4aSGreg Roach
1913e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1914195b5e75SGreg Roach
1915a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1916a6f13a4aSGreg Roach        switch ($listname) {
19177a6ee1acSGreg Roach            case 'pending':
191863b1851dSGreg Roach                $this->list = DB::table('change')
1919195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1920a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1921195b5e75SGreg Roach                            ->from('change')
1922195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1923195b5e75SGreg Roach                            ->where('status', '=', 'pending')
19247f5c2944SGreg Roach                            ->groupBy(['xref']);
1925195b5e75SGreg Roach                    })
192663b1851dSGreg Roach                    ->get()
192763b1851dSGreg Roach                    ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom))
192863b1851dSGreg Roach                    ->filter()
192963b1851dSGreg Roach                    ->all();
1930a6f13a4aSGreg Roach                break;
193163b1851dSGreg Roach
1932a6f13a4aSGreg Roach            case 'individual':
19335985adfbSGreg Roach                $query = DB::table('individuals')
19345985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19355985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19365985adfbSGreg Roach                    ->distinct();
19375985adfbSGreg Roach
1938a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1939dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
194082759250SGreg Roach                        $value = $this->substituteVars($value, false);
1941a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1942a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19430b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19445985adfbSGreg Roach                                $join
19455985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19465985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19475985adfbSGreg Roach                            });
19485985adfbSGreg Roach
19495985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19505985adfbSGreg Roach
1951a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19525985adfbSGreg Roach
1953044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19545985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1955a6f13a4aSGreg Roach                            } else {
19565985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1957a6f13a4aSGreg Roach                            }
19585985adfbSGreg Roach
19595985adfbSGreg Roach                            // This filter has been fully processed
19605985adfbSGreg Roach                            unset($attrs[$attr]);
19617ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19620b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19635985adfbSGreg Roach                                $join
19645985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19655985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19665985adfbSGreg Roach                            });
1967a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19687a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19695d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1970b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1971a6f13a4aSGreg Roach                            }
19725985adfbSGreg Roach
19735985adfbSGreg Roach                            // This filter has been fully processed
19745985adfbSGreg Roach                            unset($attrs[$attr]);
1975a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19765985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1977a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19785985adfbSGreg Roach
1979a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19805985adfbSGreg Roach
19815985adfbSGreg Roach                            // This filter has been fully processed
19825985adfbSGreg Roach                            unset($attrs[$attr]);
19832fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19849dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19855985adfbSGreg Roach                            $query
19860b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19875985adfbSGreg Roach                                    $join
19887ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19897ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19905985adfbSGreg Roach                                })
19910b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19927ee4bfadSGreg Roach                                    $join
19937ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19947ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19957ee4bfadSGreg Roach                                })
19969b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19976e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19985985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19997ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20006e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20016e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
200247a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
20036e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20046e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2005e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20067ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
2007a6f13a4aSGreg Roach                        }
2008a6f13a4aSGreg Roach                    }
2009a6f13a4aSGreg Roach                }
2010a6f13a4aSGreg Roach
201113abd6f3SGreg Roach                $this->list = [];
2012a6f13a4aSGreg Roach
20135985adfbSGreg Roach                foreach ($query->get() as $row) {
20146b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
2015a6f13a4aSGreg Roach                }
2016a6f13a4aSGreg Roach                break;
2017a6f13a4aSGreg Roach
2018a6f13a4aSGreg Roach            case 'family':
201930fc2b1eSGreg Roach                $query = DB::table('families')
202030fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
202130fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
202230fc2b1eSGreg Roach                    ->distinct();
202330fc2b1eSGreg Roach
2024a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
2025dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
202682759250SGreg Roach                        $value = $this->substituteVars($value, false);
2027a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2028a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20290b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
203030fc2b1eSGreg Roach                                $join
203130fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
203230fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
203330fc2b1eSGreg Roach                            });
203430fc2b1eSGreg Roach
203530fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
203630fc2b1eSGreg Roach
2037a6f13a4aSGreg Roach                            $date = new Date($match[3]);
203830fc2b1eSGreg Roach
2039044416d2SGreg Roach                            if ($match[2] === 'LTE') {
204030fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2041a6f13a4aSGreg Roach                            } else {
204230fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2043a6f13a4aSGreg Roach                            }
204430fc2b1eSGreg Roach
204530fc2b1eSGreg Roach                            // This filter has been fully processed
204630fc2b1eSGreg Roach                            unset($attrs[$attr]);
2047a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
204830fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2049a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
205030fc2b1eSGreg Roach
2051a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
205230fc2b1eSGreg Roach
205330fc2b1eSGreg Roach                            // This filter has been fully processed
205430fc2b1eSGreg Roach                            unset($attrs[$attr]);
205530fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20563b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20570b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
205830fc2b1eSGreg Roach                                    $join
205930fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20603b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
206130fc2b1eSGreg Roach                                            $query
206230fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
206330fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
206430fc2b1eSGreg Roach                                        });
206530fc2b1eSGreg Roach                                });
20665d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20677a6ee1acSGreg Roach                                if ($match[1] != '') {
20687a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20695d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2070b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20715d0bc43dSGreg Roach                                    }
20725d0bc43dSGreg Roach                                }
2073a6f13a4aSGreg Roach                            }
207430fc2b1eSGreg Roach
207530fc2b1eSGreg Roach                            // This filter has been fully processed
207630fc2b1eSGreg Roach                            unset($attrs[$attr]);
20776e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20789dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
207930fc2b1eSGreg Roach                            $query
20800b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
208130fc2b1eSGreg Roach                                    $join
20826e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20836e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
208430fc2b1eSGreg Roach                                })
20850b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20866e5c0963SGreg Roach                                    $join
20876e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20886e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20896e5c0963SGreg Roach                                })
2090b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20916e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
209230fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20936e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20946e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20956e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20966e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20976e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20986e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2099e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
21006e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2101a6f13a4aSGreg Roach                        }
2102a6f13a4aSGreg Roach                    }
2103a6f13a4aSGreg Roach                }
2104a6f13a4aSGreg Roach
210513abd6f3SGreg Roach                $this->list = [];
2106a6f13a4aSGreg Roach
210730fc2b1eSGreg Roach                foreach ($query->get() as $row) {
21086b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2109a6f13a4aSGreg Roach                }
2110a6f13a4aSGreg Roach                break;
2111a6f13a4aSGreg Roach
2112a6f13a4aSGreg Roach            default:
21136ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2114a6f13a4aSGreg Roach        }
2115a6f13a4aSGreg Roach
211613abd6f3SGreg Roach        $filters  = [];
211713abd6f3SGreg Roach        $filters2 = [];
2118a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2119a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2120a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2121a6f13a4aSGreg Roach                    $condition = $value;
2122a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2123a6f13a4aSGreg Roach                        $id    = $match[1];
2124a6f13a4aSGreg Roach                        $value = "''";
2125044416d2SGreg Roach                        if ($id === 'ID') {
21267a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2127a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2128a6f13a4aSGreg Roach                            }
2129044416d2SGreg Roach                        } elseif ($id === 'fact') {
2130a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2131044416d2SGreg Roach                        } elseif ($id === 'desc') {
2132a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2133a6f13a4aSGreg Roach                        } else {
2134a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21357a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2136a6f13a4aSGreg Roach                            }
2137a6f13a4aSGreg Roach                        }
2138a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2139a6f13a4aSGreg Roach                    }
2140a6f13a4aSGreg Roach                    //-- handle regular expressions
2141a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2142a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2143a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2144a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2145a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2146d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2147a6f13a4aSGreg Roach                            $val = trim($val);
2148a6f13a4aSGreg Roach                        }
2149a6f13a4aSGreg Roach                        if ($val) {
21507a6ee1acSGreg Roach                            $searchstr = '';
21517a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2152a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2153a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2154a6f13a4aSGreg Roach                                $level = 1;
2155f71a7dedSGreg Roach                                $t = 'XXXX';
2156a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2157a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2158a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2159a6f13a4aSGreg Roach                                    }
2160a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2161044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21627a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2163a6f13a4aSGreg Roach                                    }
21647a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2165a6f13a4aSGreg Roach                                    $level++;
2166a6f13a4aSGreg Roach                                }
2167a6f13a4aSGreg Roach                            } else {
2168044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21697a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2170a6f13a4aSGreg Roach                                }
2171a6f13a4aSGreg Roach                                $t         = $tag;
21727a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2173a6f13a4aSGreg Roach                            }
2174a6f13a4aSGreg Roach                            switch ($expr) {
21757a6ee1acSGreg Roach                                case 'CONTAINS':
2176044416d2SGreg Roach                                    if ($t === 'PLAC') {
2177a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2178a6f13a4aSGreg Roach                                    } else {
2179a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2180a6f13a4aSGreg Roach                                    }
2181a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2182a6f13a4aSGreg Roach                                    break;
2183a6f13a4aSGreg Roach                                default:
2184c1010edaSGreg Roach                                    $filters2[] = [
2185c1010edaSGreg Roach                                        'tag'  => $tag,
2186c1010edaSGreg Roach                                        'expr' => $expr,
2187c1010edaSGreg Roach                                        'val'  => $val,
2188c1010edaSGreg Roach                                    ];
2189a6f13a4aSGreg Roach                                    break;
2190a6f13a4aSGreg Roach                            }
2191a6f13a4aSGreg Roach                        }
2192a6f13a4aSGreg Roach                    }
2193a6f13a4aSGreg Roach                }
2194a6f13a4aSGreg Roach            }
2195a6f13a4aSGreg Roach        }
2196a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2197a6f13a4aSGreg Roach        if ($filters) {
2198a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2199a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2200299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2201a6f13a4aSGreg Roach                        unset($this->list[$key]);
2202a6f13a4aSGreg Roach                        break;
2203a6f13a4aSGreg Roach                    }
2204a6f13a4aSGreg Roach                }
2205a6f13a4aSGreg Roach            }
2206a6f13a4aSGreg Roach        }
2207a6f13a4aSGreg Roach        if ($filters2) {
220813abd6f3SGreg Roach            $mylist = [];
2209a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2210c0935879SGreg Roach                $key  = $indi->xref();
2211299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2212a6f13a4aSGreg Roach                $keep = true;
2213a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2214a6f13a4aSGreg Roach                    if ($keep) {
2215a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2216a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2217a6f13a4aSGreg Roach                        $val  = $filter['val'];
2218b2448a1bSGreg Roach                        if ($val === "''") {
22197a6ee1acSGreg Roach                            $val = '';
2220a6f13a4aSGreg Roach                        }
22217a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2222a6f13a4aSGreg Roach                        $t    = end($tags);
22233d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2224a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2225044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
22267a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
22277a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2228a6f13a4aSGreg Roach                            $t    = end($tags);
2229b315f3e1SGreg Roach                            $v    = self::getSubRecord(1, $tag, $grec);
2230a6f13a4aSGreg Roach                        }
2231a6f13a4aSGreg Roach
2232a6f13a4aSGreg Roach                        switch ($expr) {
22337a6ee1acSGreg Roach                            case 'GTE':
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;
22427a6ee1acSGreg Roach                            case 'LTE':
2243044416d2SGreg Roach                                if ($t === 'DATE') {
2244a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2245a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2246a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2247a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2248a6f13a4aSGreg Roach                                    $keep = true;
2249a6f13a4aSGreg Roach                                }
2250a6f13a4aSGreg Roach                                break;
2251a6f13a4aSGreg Roach                            default:
2252a6f13a4aSGreg Roach                                if ($v == $val) {
2253a6f13a4aSGreg Roach                                    $keep = true;
2254a6f13a4aSGreg Roach                                } else {
2255a6f13a4aSGreg Roach                                    $keep = false;
2256a6f13a4aSGreg Roach                                }
2257a6f13a4aSGreg Roach                                break;
2258a6f13a4aSGreg Roach                        }
2259a6f13a4aSGreg Roach                    }
2260a6f13a4aSGreg Roach                }
2261a6f13a4aSGreg Roach                if ($keep) {
2262a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2263a6f13a4aSGreg Roach                }
2264a6f13a4aSGreg Roach            }
2265a6f13a4aSGreg Roach            $this->list = $mylist;
2266a6f13a4aSGreg Roach        }
2267a6f13a4aSGreg Roach
2268a6f13a4aSGreg Roach        switch ($sortby) {
2269a6f13a4aSGreg Roach            case 'NAME':
2270c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2271a6f13a4aSGreg Roach                break;
2272a6f13a4aSGreg Roach            case 'CHAN':
2273c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2274a6f13a4aSGreg Roach                break;
2275a6f13a4aSGreg Roach            case 'BIRT:DATE':
2276c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2277a6f13a4aSGreg Roach                break;
2278a6f13a4aSGreg Roach            case 'DEAT:DATE':
2279c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2280a6f13a4aSGreg Roach                break;
2281a6f13a4aSGreg Roach            case 'MARR:DATE':
2282c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2283a6f13a4aSGreg Roach                break;
2284a6f13a4aSGreg Roach            default:
2285a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2286a6f13a4aSGreg Roach                break;
2287a6f13a4aSGreg Roach        }
2288a6f13a4aSGreg Roach
22899b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2290e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2291a6f13a4aSGreg Roach    }
2292a6f13a4aSGreg Roach
2293a6f13a4aSGreg Roach    /**
2294fab8f067SGreg Roach     * Handle </list>
22958ba2e626SGreg Roach     *
22968ba2e626SGreg Roach     * @return void
2297a6f13a4aSGreg Roach     */
2298b702978eSGreg Roach    protected function listEndHandler(): void
2299c1010edaSGreg Roach    {
2300a6f13a4aSGreg Roach        $this->process_repeats--;
2301a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2302a6f13a4aSGreg Roach            return;
2303a6f13a4aSGreg Roach        }
2304a6f13a4aSGreg Roach
2305a6f13a4aSGreg Roach        // Check if there is any list
2306a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2307a6f13a4aSGreg Roach            $lineoffset = 0;
2308a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2309a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2310a6f13a4aSGreg Roach            }
2311a6f13a4aSGreg Roach            //-- read the xml from the file
2312299d100dSGreg Roach            $lines = file($this->report);
2313dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2314a6f13a4aSGreg Roach                $lineoffset--;
2315a6f13a4aSGreg Roach            }
2316a6f13a4aSGreg Roach            $lineoffset++;
2317a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2318a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2319a6f13a4aSGreg Roach            // List Level counter
2320a6f13a4aSGreg Roach            $count = 1;
2321a6f13a4aSGreg Roach            while (0 < $count) {
2322dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2323a6f13a4aSGreg Roach                    $count++;
2324dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2325a6f13a4aSGreg Roach                    $count--;
2326a6f13a4aSGreg Roach                }
2327a6f13a4aSGreg Roach                if (0 < $count) {
2328a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2329a6f13a4aSGreg Roach                }
2330a6f13a4aSGreg Roach                $line_nr++;
2331a6f13a4aSGreg Roach            }
2332a6f13a4aSGreg Roach            // No need to drag this
2333a6f13a4aSGreg Roach            unset($lines);
23347a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2335a6f13a4aSGreg Roach            // Save original values
23369b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2337a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2338a6f13a4aSGreg Roach
2339a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2340a6f13a4aSGreg Roach            $this->list_private = 0;
2341a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2342a6f13a4aSGreg Roach                if ($record->canShow()) {
2343f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2344a6f13a4aSGreg Roach                    //-- start the sax parser
2345a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2346e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2347a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23481aa04befSGreg Roach
23491aa04befSGreg Roach                    xml_set_element_handler(
23501aa04befSGreg Roach                        $repeat_parser,
23519d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23521aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23531aa04befSGreg Roach                        },
23549d454b6bSGreg Roach                        function ($parser, string $name): void {
23551aa04befSGreg Roach                            $this->endElement($parser, $name);
23561aa04befSGreg Roach                        }
23571aa04befSGreg Roach                    );
23581aa04befSGreg Roach
23591aa04befSGreg Roach                    xml_set_character_data_handler(
23601aa04befSGreg Roach                        $repeat_parser,
23619d454b6bSGreg Roach                        function ($parser, string $data): void {
23621aa04befSGreg Roach                            $this->characterData($parser, $data);
23631aa04befSGreg Roach                        }
23641aa04befSGreg Roach                    );
23651aa04befSGreg Roach
2366a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23676ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2368a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2369a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2370a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2371a6f13a4aSGreg Roach                        ));
2372a6f13a4aSGreg Roach                    }
2373a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2374a6f13a4aSGreg Roach                } else {
2375a6f13a4aSGreg Roach                    $this->list_private++;
2376a6f13a4aSGreg Roach                }
2377a6f13a4aSGreg Roach            }
237813abd6f3SGreg Roach            $this->list   = [];
2379e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2380a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2381a6f13a4aSGreg Roach        }
238265e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2383a6f13a4aSGreg Roach    }
2384a6f13a4aSGreg Roach
2385a6f13a4aSGreg Roach    /**
2386fab8f067SGreg Roach     * Handle <listTotal>
2387a6f13a4aSGreg Roach     * Prints the total number of records in a list
2388fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23898ba2e626SGreg Roach     *
23908ba2e626SGreg Roach     * @return void
2391a6f13a4aSGreg Roach     */
2392b702978eSGreg Roach    protected function listTotalStartHandler(): void
2393c1010edaSGreg Roach    {
2394a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2395589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2396a6f13a4aSGreg Roach        } else {
23977a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2398a6f13a4aSGreg Roach        }
2399a6f13a4aSGreg Roach    }
2400a6f13a4aSGreg Roach
2401a6f13a4aSGreg Roach    /**
2402fab8f067SGreg Roach     * Handle <relatives>
240376692c8bSGreg Roach     *
240409482a55SGreg Roach     * @param array<string> $attrs
24058ba2e626SGreg Roach     *
24068ba2e626SGreg Roach     * @return void
2407a6f13a4aSGreg Roach     */
2408b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2409c1010edaSGreg Roach    {
2410a6f13a4aSGreg Roach        $this->process_repeats++;
2411a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2412a6f13a4aSGreg Roach            return;
2413a6f13a4aSGreg Roach        }
2414a6f13a4aSGreg Roach
2415e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2416e364afe4SGreg Roach
241713abd6f3SGreg Roach        $match = [];
2418a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2419d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2420a6f13a4aSGreg Roach            $sortby = trim($sortby);
2421a6f13a4aSGreg Roach        }
2422a6f13a4aSGreg Roach
2423a6f13a4aSGreg Roach        $maxgen = -1;
2424a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2425c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2426a6f13a4aSGreg Roach        }
2427a6f13a4aSGreg Roach
2428e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2429e364afe4SGreg Roach
2430a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2431d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2432a6f13a4aSGreg Roach            $group = trim($group);
2433a6f13a4aSGreg Roach        }
2434a6f13a4aSGreg Roach
2435e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2436e364afe4SGreg Roach
2437a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2438d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2439a6f13a4aSGreg Roach            $id = trim($id);
2440a6f13a4aSGreg Roach        }
2441a6f13a4aSGreg Roach
244213abd6f3SGreg Roach        $this->list = [];
24436b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2444d965cc1aSGreg Roach        if ($person instanceof Individual) {
2445a6f13a4aSGreg Roach            $this->list[$id] = $person;
2446a6f13a4aSGreg Roach            switch ($group) {
24477a6ee1acSGreg Roach                case 'child-family':
244839ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2449820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2450820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2451a6f13a4aSGreg Roach                        }
2452820b62dfSGreg Roach
2453820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2454c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2455a6f13a4aSGreg Roach                        }
2456a6f13a4aSGreg Roach                    }
2457a6f13a4aSGreg Roach                    break;
24587a6ee1acSGreg Roach                case 'spouse-family':
245939ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2460820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2461820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2462a6f13a4aSGreg Roach                        }
2463820b62dfSGreg Roach
2464820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2465c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2466a6f13a4aSGreg Roach                        }
2467a6f13a4aSGreg Roach                    }
2468a6f13a4aSGreg Roach                    break;
24697a6ee1acSGreg Roach                case 'direct-ancestors':
24703d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2471a6f13a4aSGreg Roach                    break;
24727a6ee1acSGreg Roach                case 'ancestors':
24733d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2474a6f13a4aSGreg Roach                    break;
24757a6ee1acSGreg Roach                case 'descendants':
2476a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24773d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2478a6f13a4aSGreg Roach                    break;
24797a6ee1acSGreg Roach                case 'all':
24803d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24813d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2482a6f13a4aSGreg Roach                    break;
2483a6f13a4aSGreg Roach            }
2484a6f13a4aSGreg Roach        }
2485a6f13a4aSGreg Roach
2486a6f13a4aSGreg Roach        switch ($sortby) {
2487a6f13a4aSGreg Roach            case 'NAME':
2488c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2489a6f13a4aSGreg Roach                break;
2490a6f13a4aSGreg Roach            case 'BIRT:DATE':
2491c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2492a6f13a4aSGreg Roach                break;
2493a6f13a4aSGreg Roach            case 'DEAT:DATE':
2494c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2495a6f13a4aSGreg Roach                break;
2496a6f13a4aSGreg Roach            case 'generation':
249713abd6f3SGreg Roach                $newarray = [];
2498a6f13a4aSGreg Roach                reset($this->list);
2499a6f13a4aSGreg Roach                $genCounter = 1;
2500a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2501a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2502a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2503a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2504f70bcff5SGreg Roach                            $newarray[$key] = (object) ['generation' => $this->generation];
2505a6f13a4aSGreg Roach                        }
2506a6f13a4aSGreg Roach                    }
2507a6f13a4aSGreg Roach                    $genCounter++;
2508a6f13a4aSGreg Roach                }
2509a6f13a4aSGreg Roach                $this->list = $newarray;
2510a6f13a4aSGreg Roach                break;
2511a6f13a4aSGreg Roach            default:
2512a6f13a4aSGreg Roach                // unsorted
2513a6f13a4aSGreg Roach                break;
2514a6f13a4aSGreg Roach        }
25159b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2516e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2517a6f13a4aSGreg Roach    }
2518a6f13a4aSGreg Roach
2519a6f13a4aSGreg Roach    /**
2520fab8f067SGreg Roach     * Handle </relatives>
25218ba2e626SGreg Roach     *
25228ba2e626SGreg Roach     * @return void
2523a6f13a4aSGreg Roach     */
2524b702978eSGreg Roach    protected function relativesEndHandler(): void
2525c1010edaSGreg Roach    {
2526a6f13a4aSGreg Roach        $this->process_repeats--;
2527a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2528a6f13a4aSGreg Roach            return;
2529a6f13a4aSGreg Roach        }
2530a6f13a4aSGreg Roach
2531a6f13a4aSGreg Roach        // Check if there is any relatives
2532a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2533a6f13a4aSGreg Roach            $lineoffset = 0;
2534a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2535a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2536a6f13a4aSGreg Roach            }
2537a6f13a4aSGreg Roach            //-- read the xml from the file
2538299d100dSGreg Roach            $lines = file($this->report);
2539dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2540a6f13a4aSGreg Roach                $lineoffset--;
2541a6f13a4aSGreg Roach            }
2542a6f13a4aSGreg Roach            $lineoffset++;
2543a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2544a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2545a6f13a4aSGreg Roach            // Relatives Level counter
2546a6f13a4aSGreg Roach            $count = 1;
2547a6f13a4aSGreg Roach            while (0 < $count) {
2548dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2549a6f13a4aSGreg Roach                    $count++;
2550dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2551a6f13a4aSGreg Roach                    $count--;
2552a6f13a4aSGreg Roach                }
2553a6f13a4aSGreg Roach                if (0 < $count) {
2554a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2555a6f13a4aSGreg Roach                }
2556a6f13a4aSGreg Roach                $line_nr++;
2557a6f13a4aSGreg Roach            }
2558a6f13a4aSGreg Roach            // No need to drag this
2559a6f13a4aSGreg Roach            unset($lines);
2560a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2561a6f13a4aSGreg Roach            // Save original values
25629b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2563a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2564a6f13a4aSGreg Roach
2565a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2566a6f13a4aSGreg Roach            $this->list_private = 0;
2567b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2568a6f13a4aSGreg Roach                if (isset($value->generation)) {
2569a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2570a6f13a4aSGreg Roach                }
25716b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2572299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2573a6f13a4aSGreg Roach
2574a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2575e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2576a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25771aa04befSGreg Roach
25781aa04befSGreg Roach                xml_set_element_handler(
25791aa04befSGreg Roach                    $repeat_parser,
25809d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25811aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25821aa04befSGreg Roach                    },
25839d454b6bSGreg Roach                    function ($parser, string $name): void {
25841aa04befSGreg Roach                        $this->endElement($parser, $name);
25851aa04befSGreg Roach                    }
25861aa04befSGreg Roach                );
25871aa04befSGreg Roach
25881aa04befSGreg Roach                xml_set_character_data_handler(
25891aa04befSGreg Roach                    $repeat_parser,
25909d454b6bSGreg Roach                    function ($parser, string $data): void {
25911aa04befSGreg Roach                        $this->characterData($parser, $data);
25921aa04befSGreg Roach                    }
25931aa04befSGreg Roach                );
2594a6f13a4aSGreg Roach
2595a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25966ccdf4f0SGreg 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)));
2597a6f13a4aSGreg Roach                }
2598a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2599a6f13a4aSGreg Roach            }
2600a6f13a4aSGreg Roach            // Clean up the list array
260113abd6f3SGreg Roach            $this->list   = [];
2602e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2603a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2604a6f13a4aSGreg Roach        }
260565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2606a6f13a4aSGreg Roach    }
2607a6f13a4aSGreg Roach
2608a6f13a4aSGreg Roach    /**
2609fab8f067SGreg Roach     * Handle <generation />
2610a6f13a4aSGreg Roach     * Prints the number of generations
26118ba2e626SGreg Roach     *
26128ba2e626SGreg Roach     * @return void
2613a6f13a4aSGreg Roach     */
2614b702978eSGreg Roach    protected function generationStartHandler(): void
2615c1010edaSGreg Roach    {
2616589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2617a6f13a4aSGreg Roach    }
2618a6f13a4aSGreg Roach
2619a6f13a4aSGreg Roach    /**
2620fab8f067SGreg Roach     * Handle <newPage />
2621a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
26228ba2e626SGreg Roach     *
26238ba2e626SGreg Roach     * @return void
2624a6f13a4aSGreg Roach     */
2625b702978eSGreg Roach    protected function newPageStartHandler(): void
2626c1010edaSGreg Roach    {
26277a6ee1acSGreg Roach        $temp = 'addpage';
2628e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2629a6f13a4aSGreg Roach    }
2630a6f13a4aSGreg Roach
2631a6f13a4aSGreg Roach    /**
2632fab8f067SGreg Roach     * Handle </title>
26338ba2e626SGreg Roach     *
26348ba2e626SGreg Roach     * @return void
2635a6f13a4aSGreg Roach     */
2636b702978eSGreg Roach    protected function titleEndHandler(): void
2637c1010edaSGreg Roach    {
26382836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2639a6f13a4aSGreg Roach    }
2640a6f13a4aSGreg Roach
2641a6f13a4aSGreg Roach    /**
2642fab8f067SGreg Roach     * Handle </description>
26438ba2e626SGreg Roach     *
26448ba2e626SGreg Roach     * @return void
2645a6f13a4aSGreg Roach     */
2646b702978eSGreg Roach    protected function descriptionEndHandler(): void
2647c1010edaSGreg Roach    {
26482836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2649a6f13a4aSGreg Roach    }
2650729ce104SGreg Roach
2651729ce104SGreg Roach    /**
265276692c8bSGreg Roach     * Create a list of all descendants.
265376692c8bSGreg Roach     *
26541c7df322SGreg Roach     * @param array<Individual> $list
2655729ce104SGreg Roach     * @param string            $pid
2656729ce104SGreg Roach     * @param bool              $parents
2657729ce104SGreg Roach     * @param int               $generations
26588ba2e626SGreg Roach     *
26598ba2e626SGreg Roach     * @return void
2660729ce104SGreg Roach     */
26613b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2662c1010edaSGreg Roach    {
26636b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2664729ce104SGreg Roach        if ($person === null) {
2665729ce104SGreg Roach            return;
2666729ce104SGreg Roach        }
2667729ce104SGreg Roach        if (!isset($list[$pid])) {
2668729ce104SGreg Roach            $list[$pid] = $person;
2669729ce104SGreg Roach        }
2670729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2671729ce104SGreg Roach            $list[$pid]->generation = 0;
2672729ce104SGreg Roach        }
267339ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2674729ce104SGreg Roach            if ($parents) {
267539ca88baSGreg Roach                $husband = $family->husband();
267639ca88baSGreg Roach                $wife    = $family->wife();
2677729ce104SGreg Roach                if ($husband) {
2678c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2679729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2680c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2681729ce104SGreg Roach                    } else {
2682c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2683729ce104SGreg Roach                    }
2684729ce104SGreg Roach                }
2685729ce104SGreg Roach                if ($wife) {
2686c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2687729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2688c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2689729ce104SGreg Roach                    } else {
2690c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2691729ce104SGreg Roach                    }
2692729ce104SGreg Roach                }
2693729ce104SGreg Roach            }
2694820b62dfSGreg Roach
269539ca88baSGreg Roach            $children = $family->children();
2696820b62dfSGreg Roach
2697729ce104SGreg Roach            foreach ($children as $child) {
2698729ce104SGreg Roach                if ($child) {
2699c0935879SGreg Roach                    $list[$child->xref()] = $child;
2700820b62dfSGreg Roach
2701729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2702c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2703729ce104SGreg Roach                    } else {
2704c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2705729ce104SGreg Roach                    }
2706729ce104SGreg Roach                }
2707729ce104SGreg Roach            }
2708729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2709729ce104SGreg Roach                foreach ($children as $child) {
2710c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2711729ce104SGreg Roach                }
2712729ce104SGreg Roach            }
2713729ce104SGreg Roach        }
2714729ce104SGreg Roach    }
2715729ce104SGreg Roach
2716729ce104SGreg Roach    /**
271776692c8bSGreg Roach     * Create a list of all ancestors.
271876692c8bSGreg Roach     *
27191c7df322SGreg Roach     * @param array<Individual> $list
2720729ce104SGreg Roach     * @param string            $pid
2721729ce104SGreg Roach     * @param bool              $children
2722729ce104SGreg Roach     * @param int               $generations
27238ba2e626SGreg Roach     *
27248ba2e626SGreg Roach     * @return void
2725729ce104SGreg Roach     */
27263b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2727c1010edaSGreg Roach    {
272813abd6f3SGreg Roach        $genlist                = [$pid];
2729729ce104SGreg Roach        $list[$pid]->generation = 1;
2730729ce104SGreg Roach        while (count($genlist) > 0) {
2731729ce104SGreg Roach            $id = array_shift($genlist);
2732dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2733729ce104SGreg Roach                continue; // id can be something like “empty7”
2734729ce104SGreg Roach            }
27356b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
273639ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
273739ca88baSGreg Roach                $husband = $family->husband();
273839ca88baSGreg Roach                $wife    = $family->wife();
2739729ce104SGreg Roach                if ($husband) {
2740c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2741c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2742729ce104SGreg Roach                }
2743729ce104SGreg Roach                if ($wife) {
2744c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2745c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2746729ce104SGreg Roach                }
2747729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2748729ce104SGreg Roach                    if ($husband) {
2749c0935879SGreg Roach                        $genlist[] = $husband->xref();
2750729ce104SGreg Roach                    }
2751729ce104SGreg Roach                    if ($wife) {
2752c0935879SGreg Roach                        $genlist[] = $wife->xref();
2753729ce104SGreg Roach                    }
2754729ce104SGreg Roach                }
2755729ce104SGreg Roach                if ($children) {
275639ca88baSGreg Roach                    foreach ($family->children() as $child) {
2757c0935879SGreg Roach                        $list[$child->xref()] = $child;
2758e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2759729ce104SGreg Roach                    }
2760729ce104SGreg Roach                }
2761729ce104SGreg Roach            }
2762729ce104SGreg Roach        }
2763729ce104SGreg Roach    }
2764729ce104SGreg Roach
2765729ce104SGreg Roach    /**
2766729ce104SGreg Roach     * get gedcom tag value
2767729ce104SGreg Roach     *
2768729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2769729ce104SGreg 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
2770729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2771729ce104SGreg Roach     *
2772729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2773729ce104SGreg Roach     */
2774b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2775c1010edaSGreg Roach    {
2776b2448a1bSGreg Roach        if ($gedrec === '') {
2777729ce104SGreg Roach            return '';
2778729ce104SGreg Roach        }
2779729ce104SGreg Roach        $tags      = explode(':', $tag);
2780729ce104SGreg Roach        $origlevel = $level;
2781b2448a1bSGreg Roach        if ($level === 0) {
27823c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2783729ce104SGreg Roach        }
2784729ce104SGreg Roach
2785729ce104SGreg Roach        $subrec = $gedrec;
2786f71a7dedSGreg Roach        $t = 'XXXX';
2787729ce104SGreg Roach        foreach ($tags as $t) {
2788729ce104SGreg Roach            $lastsubrec = $subrec;
2789b315f3e1SGreg Roach            $subrec     = self::getSubRecord($level, "$level $t", $subrec);
2790729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2791729ce104SGreg Roach                $level--;
2792b315f3e1SGreg Roach                $subrec = self::getSubRecord($level, "$level $t", $lastsubrec);
2793729ce104SGreg Roach            }
2794729ce104SGreg Roach            if (empty($subrec)) {
2795044416d2SGreg Roach                if ($t === 'TITL') {
2796b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "$level ABBR", $lastsubrec);
2797729ce104SGreg Roach                    if (!empty($subrec)) {
27987a6ee1acSGreg Roach                        $t = 'ABBR';
2799729ce104SGreg Roach                    }
2800729ce104SGreg Roach                }
2801b2448a1bSGreg Roach                if ($subrec === '') {
2802729ce104SGreg Roach                    if ($level > 0) {
2803729ce104SGreg Roach                        $level--;
2804729ce104SGreg Roach                    }
2805b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "@ $t", $gedrec);
2806b2448a1bSGreg Roach                    if ($subrec === '') {
2807729ce104SGreg Roach                        return '';
2808729ce104SGreg Roach                    }
2809729ce104SGreg Roach                }
2810729ce104SGreg Roach            }
2811729ce104SGreg Roach            $level++;
2812729ce104SGreg Roach        }
2813729ce104SGreg Roach        $level--;
2814729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2815f71a7dedSGreg Roach        if ($ct === 0) {
2816729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2817729ce104SGreg Roach        }
2818f71a7dedSGreg Roach        if ($ct === 0) {
2819729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2820729ce104SGreg Roach        }
2821729ce104SGreg Roach        if ($ct > 0) {
2822729ce104SGreg Roach            $value = trim($match[1]);
2823044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
28246b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2825ff166e64SGreg Roach                if ($note instanceof Note) {
2826729ce104SGreg Roach                    $value = $note->getNote();
2827729ce104SGreg Roach                } else {
2828729ce104SGreg Roach                    //-- set the value to the id without the @
2829729ce104SGreg Roach                    $value = $match[1];
2830729ce104SGreg Roach                }
2831729ce104SGreg Roach            }
2832f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
2833b315f3e1SGreg Roach                $value .= self::getCont($level + 1, $subrec);
2834729ce104SGreg Roach            }
2835729ce104SGreg Roach
2836729ce104SGreg Roach            return $value;
2837729ce104SGreg Roach        }
2838729ce104SGreg Roach
28397a6ee1acSGreg Roach        return '';
2840729ce104SGreg Roach    }
2841d1286247SGreg Roach
2842d1286247SGreg Roach    /**
2843d1286247SGreg Roach     * Replace variable identifiers with their values.
2844d1286247SGreg Roach     *
2845d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
284682759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2847d1286247SGreg Roach     *
2848d1286247SGreg Roach     * @return string
2849d1286247SGreg Roach     */
28508f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2851c1010edaSGreg Roach    {
2852d1286247SGreg Roach        return preg_replace_callback(
2853d1286247SGreg Roach            '/\$(\w+)/',
285418d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28552118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
285682759250SGreg Roach                    if ($quote) {
28572118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2858b2ce94c6SRico Sonntag                    }
2859b2ce94c6SRico Sonntag
28602118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
286182759250SGreg Roach                }
2862b2ce94c6SRico Sonntag
2863d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28643d7a8a4cSGreg Roach
2865d1286247SGreg Roach                return '$' . $matches[1];
2866d1286247SGreg Roach            },
2867d1286247SGreg Roach            $expression
2868d1286247SGreg Roach        );
2869d1286247SGreg Roach    }
2870a6f13a4aSGreg Roach}
2871