xref: /webtrees/app/Report/ReportParserGenerate.php (revision b6ec1ccf23d5adc5a335304551953cfb6f52eef2)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
256f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2697def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
27ab2c9152SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
28a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
299a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
33d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
34a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
35729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
36a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
37d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
38299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
39195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
40a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
415985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
429a9e551aSGreg Roachuse Illuminate\Support\Str;
4331d9777aSGreg Roachuse LogicException;
44c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
455809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
464e69366aSGreg Roachuse XMLParser;
4759597b37SGreg Roach
48b6f35a76SGreg Roachuse function addcslashes;
49b6f35a76SGreg Roachuse function addslashes;
50b6f35a76SGreg Roachuse function array_pop;
51b6f35a76SGreg Roachuse function array_shift;
52b6f35a76SGreg Roachuse function assert;
53b6f35a76SGreg Roachuse function count;
54b6f35a76SGreg Roachuse function end;
55b2448a1bSGreg Roachuse function explode;
56b6f35a76SGreg Roachuse function file;
57b6f35a76SGreg Roachuse function file_exists;
58b6f35a76SGreg Roachuse function getimagesize;
5929518ad2SGreg Roachuse function imagecreatefromstring;
6029518ad2SGreg Roachuse function imagesx;
6129518ad2SGreg Roachuse function imagesy;
62b6f35a76SGreg Roachuse function in_array;
6372d49ceeSGreg Roachuse function ltrim;
64b19e047dSGreg Roachuse function method_exists;
65b6f35a76SGreg Roachuse function preg_match;
66b6f35a76SGreg Roachuse function preg_match_all;
67b6f35a76SGreg Roachuse function preg_replace;
68b6f35a76SGreg Roachuse function preg_replace_callback;
69b6f35a76SGreg Roachuse function preg_split;
70b6f35a76SGreg Roachuse function reset;
71b6f35a76SGreg Roachuse function round;
72b6f35a76SGreg Roachuse function sprintf;
73dec352c1SGreg Roachuse function str_contains;
7472d49ceeSGreg Roachuse function str_ends_with;
75b6f35a76SGreg Roachuse function str_replace;
76dec352c1SGreg Roachuse function str_starts_with;
77b6f35a76SGreg Roachuse function strip_tags;
78b6f35a76SGreg Roachuse function strlen;
7972d49ceeSGreg Roachuse function strpos;
80b6f35a76SGreg Roachuse function strtoupper;
81b6f35a76SGreg Roachuse function substr;
82b2448a1bSGreg Roachuse function trim;
83b6f35a76SGreg Roachuse function uasort;
84b6f35a76SGreg Roachuse function xml_error_string;
85b6f35a76SGreg Roachuse function xml_get_current_line_number;
86b6f35a76SGreg Roachuse function xml_get_error_code;
87b6f35a76SGreg Roachuse function xml_parse;
88b6f35a76SGreg Roachuse function xml_parser_create;
89b6f35a76SGreg Roachuse function xml_parser_free;
90b6f35a76SGreg Roachuse function xml_parser_set_option;
91b6f35a76SGreg Roachuse function xml_set_character_data_handler;
92b6f35a76SGreg Roachuse function xml_set_element_handler;
93b6f35a76SGreg Roach
9472d49ceeSGreg Roachuse const PREG_OFFSET_CAPTURE;
95b6f35a76SGreg Roachuse const PREG_SET_ORDER;
96b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9729518ad2SGreg Roach
98a6f13a4aSGreg Roach/**
99a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
100a6f13a4aSGreg Roach */
101c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
102c1010edaSGreg Roach{
103f315390bSGreg Roach    /** Are we collecting data from <Footnote> elements */
104f315390bSGreg Roach    private bool $process_footnote = true;
105a6f13a4aSGreg Roach
10652f124b0SAlejandro Criado-Pérez    /** Are we currently outputting data? */
107f315390bSGreg Roach    private bool $print_data = false;
108a6f13a4aSGreg Roach
109f315390bSGreg Roach    /** @var array<int,bool> Push-down stack of $print_data */
110f315390bSGreg Roach    private array $print_data_stack = [];
111a6f13a4aSGreg Roach
112f315390bSGreg Roach    /** Are we processing GEDCOM data */
113f315390bSGreg Roach    private int $process_gedcoms = 0;
114a6f13a4aSGreg Roach
115f315390bSGreg Roach    /** Are we processing conditionals */
116f315390bSGreg Roach    private int $process_ifs = 0;
117a6f13a4aSGreg Roach
118f315390bSGreg Roach    /** Are we processing repeats */
119f315390bSGreg Roach    private int $process_repeats = 0;
120a6f13a4aSGreg Roach
121f315390bSGreg Roach    /** Quantity of data to repeat during loops */
122f315390bSGreg Roach    private int $repeat_bytes = 0;
123a6f13a4aSGreg Roach
12409482a55SGreg Roach    /** @var array<string> Repeated data when iterating over loops */
12509482a55SGreg Roach    private array $repeats = [];
126a6f13a4aSGreg Roach
12709482a55SGreg Roach    /** @var array<int,array<int,array<string>|int>> Nested repeating data */
12809482a55SGreg Roach    private array $repeats_stack = [];
129a6f13a4aSGreg Roach
13009482a55SGreg Roach    /** @var array<AbstractRenderer> Nested repeating data */
13109482a55SGreg Roach    private array $wt_report_stack = [];
132e8e7866bSGreg Roach
133b112142eSGreg Roach    // Nested repeating data
134b112142eSGreg Roach    private XMLParser $parser;
135e8e7866bSGreg Roach
1364e69366aSGreg Roach    /** @var XMLParser[] (resource[] before PHP 8.0) Nested repeating data */
1374e69366aSGreg Roach    private array $parser_stack = [];
138e8e7866bSGreg Roach
139f315390bSGreg Roach    /** The current GEDCOM record */
140f315390bSGreg Roach    private string $gedrec = '';
141a6f13a4aSGreg Roach
142f315390bSGreg Roach    /** @var array<int,array<int,string>> Nested GEDCOM records */
1434e69366aSGreg Roach    private array $gedrec_stack = [];
144a6f13a4aSGreg Roach
145a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
146a6f13a4aSGreg Roach    private $current_element;
147a6f13a4aSGreg Roach
148a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
149a6f13a4aSGreg Roach    private $footnote_element;
150a6f13a4aSGreg Roach
151f315390bSGreg Roach    /** The GEDCOM fact currently being processed */
152f315390bSGreg Roach    private string $fact = '';
153a6f13a4aSGreg Roach
154f315390bSGreg Roach    /** The GEDCOM value currently being processed */
155f315390bSGreg Roach    private string $desc = '';
156a6f13a4aSGreg Roach
157f315390bSGreg Roach    /** The GEDCOM type currently being processed */
158f315390bSGreg Roach    private string $type = '';
159a6f13a4aSGreg Roach
160f315390bSGreg Roach    /** The current generational level */
161f315390bSGreg Roach    private int $generation = 1;
162a6f13a4aSGreg Roach
16376d39c55SGreg Roach    /** @var array<static|GedcomRecord> Source data for processing lists */
1644e69366aSGreg Roach    private array $list = [];
165a6f13a4aSGreg Roach
166f315390bSGreg Roach    /** Number of items in lists */
167f315390bSGreg Roach    private int $list_total = 0;
168a6f13a4aSGreg Roach
169f315390bSGreg Roach    /** Number of items filtered from lists */
170f315390bSGreg Roach    private int $list_private = 0;
171a6f13a4aSGreg Roach
172299d100dSGreg Roach    /** @var string The filename of the XML report */
173299d100dSGreg Roach    protected $report;
174299d100dSGreg Roach
175b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
176e8e7866bSGreg Roach    private $report_root;
177e8e7866bSGreg Roach
178b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
179e8e7866bSGreg Roach    private $wt_report;
180e8e7866bSGreg Roach
18109482a55SGreg Roach    /** @var array<array<string>> Variables defined in the report at run-time */
1824e69366aSGreg Roach    private array $vars;
183d1286247SGreg Roach
1844e69366aSGreg Roach    private Tree $tree;
185299d100dSGreg 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
19376692c8bSGreg Roach     */
1949458f20aSGreg Roach    public function __construct(string $report, AbstractRenderer $report_root, array $vars, Tree $tree)
1959458f20aSGreg Roach    {
196299d100dSGreg Roach        $this->report          = $report;
197e8e7866bSGreg Roach        $this->report_root     = $report_root;
198e8e7866bSGreg Roach        $this->wt_report       = $report_root;
19959f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
200d1286247SGreg Roach        $this->vars            = $vars;
201299d100dSGreg Roach        $this->tree            = $tree;
202299d100dSGreg Roach
20376f666f4SGreg Roach        parent::__construct($report);
204a6f13a4aSGreg Roach    }
205a6f13a4aSGreg Roach
206a6f13a4aSGreg Roach    /**
207b315f3e1SGreg Roach     * get a gedcom subrecord
208b315f3e1SGreg Roach     *
209b315f3e1SGreg Roach     * searches a gedcom record and returns a subrecord of it. A subrecord is defined starting at a
210b315f3e1SGreg Roach     * line with level N and all subsequent lines greater than N until the next N level is reached.
211b315f3e1SGreg Roach     * For example, the following is a BIRT subrecord:
212b315f3e1SGreg Roach     * <code>1 BIRT
213b315f3e1SGreg Roach     * 2 DATE 1 JAN 1900
214b315f3e1SGreg Roach     * 2 PLAC Phoenix, Maricopa, Arizona</code>
215b315f3e1SGreg Roach     * The following example is the DATE subrecord of the above BIRT subrecord:
216b315f3e1SGreg Roach     * <code>2 DATE 1 JAN 1900</code>
217b315f3e1SGreg Roach     *
218b315f3e1SGreg Roach     * @param int    $level   the N level of the subrecord to get
219b315f3e1SGreg Roach     * @param string $tag     a gedcom tag or string to search for in the record (ie 1 BIRT or 2 DATE)
220b315f3e1SGreg Roach     * @param string $gedrec  the parent gedcom record to search in
221b315f3e1SGreg Roach     * @param int    $num     this allows you to specify which matching <var>$tag</var> to get. Oftentimes a
222b315f3e1SGreg Roach     *                        gedcom record will have more that 1 of the same type of subrecord. An individual may have
223b315f3e1SGreg Roach     *                        multiple events for example. Passing $num=1 would get the first 1. Passing $num=2 would get the
224b315f3e1SGreg Roach     *                        second one, etc.
225b315f3e1SGreg Roach     *
226b315f3e1SGreg Roach     * @return string the subrecord that was found or an empty string "" if not found.
227b315f3e1SGreg Roach     */
228b315f3e1SGreg Roach    public static function getSubRecord(int $level, string $tag, string $gedrec, int $num = 1): string
229b315f3e1SGreg Roach    {
230b315f3e1SGreg Roach        if ($gedrec === '') {
231b315f3e1SGreg Roach            return '';
232b315f3e1SGreg Roach        }
233b315f3e1SGreg Roach        // -- adding \n before and after gedrec
234b315f3e1SGreg Roach        $gedrec       = "\n" . $gedrec . "\n";
235b315f3e1SGreg Roach        $tag          = trim($tag);
236b315f3e1SGreg Roach        $searchTarget = "~[\n]" . $tag . "[\s]~";
237b315f3e1SGreg Roach        $ct           = preg_match_all($searchTarget, $gedrec, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
238b315f3e1SGreg Roach        if ($ct === 0) {
239b315f3e1SGreg Roach            return '';
240b315f3e1SGreg Roach        }
241b315f3e1SGreg Roach        if ($ct < $num) {
242b315f3e1SGreg Roach            return '';
243b315f3e1SGreg Roach        }
244b315f3e1SGreg Roach        $pos1 = $match[$num - 1][0][1];
245b315f3e1SGreg Roach        $pos2 = strpos($gedrec, "\n$level", $pos1 + 1);
246b315f3e1SGreg Roach        if (!$pos2) {
247b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\n1", $pos1 + 1);
248b315f3e1SGreg Roach        }
249b315f3e1SGreg Roach        if (!$pos2) {
250b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\nWT_", $pos1 + 1); // WT_SPOUSE, WT_FAMILY_ID ...
251b315f3e1SGreg Roach        }
252b315f3e1SGreg Roach        if (!$pos2) {
253b315f3e1SGreg Roach            return ltrim(substr($gedrec, $pos1));
254b315f3e1SGreg Roach        }
255b315f3e1SGreg Roach        $subrec = substr($gedrec, $pos1, $pos2 - $pos1);
256b315f3e1SGreg Roach
257b315f3e1SGreg Roach        return ltrim($subrec);
258b315f3e1SGreg Roach    }
259b315f3e1SGreg Roach
260b315f3e1SGreg Roach    /**
261b315f3e1SGreg Roach     * get CONT lines
262b315f3e1SGreg Roach     *
263b315f3e1SGreg Roach     * get the N+1 CONT or CONC lines of a gedcom subrecord
264b315f3e1SGreg Roach     *
265b315f3e1SGreg Roach     * @param int    $nlevel the level of the CONT lines to get
266b315f3e1SGreg Roach     * @param string $nrec   the gedcom subrecord to search in
267b315f3e1SGreg Roach     *
268b315f3e1SGreg Roach     * @return string a string with all CONT lines merged
269b315f3e1SGreg Roach     */
270b315f3e1SGreg Roach    public static function getCont(int $nlevel, string $nrec): string
271b315f3e1SGreg Roach    {
272b315f3e1SGreg Roach        $text = '';
273b315f3e1SGreg Roach
274b315f3e1SGreg Roach        $subrecords = explode("\n", $nrec);
275b315f3e1SGreg Roach        foreach ($subrecords as $thisSubrecord) {
276b315f3e1SGreg Roach            if (substr($thisSubrecord, 0, 2) !== $nlevel . ' ') {
277b315f3e1SGreg Roach                continue;
278b315f3e1SGreg Roach            }
279b315f3e1SGreg Roach            $subrecordType = substr($thisSubrecord, 2, 4);
280b315f3e1SGreg Roach            if ($subrecordType === 'CONT') {
281b315f3e1SGreg Roach                $text .= "\n" . substr($thisSubrecord, 7);
282b315f3e1SGreg Roach            }
283b315f3e1SGreg Roach        }
284b315f3e1SGreg Roach
285b315f3e1SGreg Roach        return $text;
286b315f3e1SGreg Roach    }
287b315f3e1SGreg Roach
288b315f3e1SGreg Roach    /**
289a6f13a4aSGreg Roach     * XML start element handler
290a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
291a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
292a6f13a4aSGreg Roach     *
293a6f13a4aSGreg Roach     * @param resource      $parser the resource handler for the XML parser
294a6f13a4aSGreg Roach     * @param string        $name   the name of the XML element parsed
29509482a55SGreg Roach     * @param array<string> $attrs  an array of key value pairs for the attributes
29618d7a90dSGreg Roach     *
29718d7a90dSGreg Roach     * @return void
298a6f13a4aSGreg Roach     */
299af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
300c1010edaSGreg Roach    {
30113abd6f3SGreg Roach        $newattrs = [];
302a6f13a4aSGreg Roach
303a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
304a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
305e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
306d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
307a6f13a4aSGreg Roach                }
308a6f13a4aSGreg Roach            }
309a6f13a4aSGreg Roach            $newattrs[$key] = $value;
310a6f13a4aSGreg Roach        }
311a6f13a4aSGreg Roach        $attrs = $newattrs;
3127a6ee1acSGreg 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')) {
313a0e2939aSGreg Roach            $method = $name . 'StartHandler';
314208e9f76SGreg Roach
315a0e2939aSGreg Roach            if (method_exists($this, $method)) {
316f315390bSGreg Roach                $this->{$method}($attrs);
317a6f13a4aSGreg Roach            }
318a6f13a4aSGreg Roach        }
319a6f13a4aSGreg Roach    }
320a6f13a4aSGreg Roach
321a6f13a4aSGreg Roach    /**
322a6f13a4aSGreg Roach     * XML end element handler
323a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
324a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
325a6f13a4aSGreg Roach     *
326a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
327a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
32818d7a90dSGreg Roach     *
32918d7a90dSGreg Roach     * @return void
330a6f13a4aSGreg Roach     */
331af14d238SGreg Roach    protected function endElement($parser, string $name): void
332c1010edaSGreg Roach    {
3337a6ee1acSGreg 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')) {
334a0e2939aSGreg Roach            $method = $name . 'EndHandler';
335a0e2939aSGreg Roach
336a0e2939aSGreg Roach            if (method_exists($this, $method)) {
337f315390bSGreg Roach                $this->{$method}();
338a6f13a4aSGreg Roach            }
339a6f13a4aSGreg Roach        }
340a6f13a4aSGreg Roach    }
341a6f13a4aSGreg Roach
342a6f13a4aSGreg Roach    /**
343a6f13a4aSGreg Roach     * XML character data handler
344a6f13a4aSGreg Roach     *
345a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
346a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
34718d7a90dSGreg Roach     *
34818d7a90dSGreg Roach     * @return void
349a6f13a4aSGreg Roach     */
35024f2a3afSGreg Roach    protected function characterData($parser, string $data): void
351c1010edaSGreg Roach    {
352e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
353a6f13a4aSGreg Roach            $this->current_element->addText($data);
354a6f13a4aSGreg Roach        }
355a6f13a4aSGreg Roach    }
356a6f13a4aSGreg Roach
357a6f13a4aSGreg Roach    /**
358fab8f067SGreg Roach     * Handle <style>
359a6f13a4aSGreg Roach     *
36009482a55SGreg Roach     * @param array<string> $attrs
3618ba2e626SGreg Roach     *
3628ba2e626SGreg Roach     * @return void
363a6f13a4aSGreg Roach     */
364b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
365c1010edaSGreg Roach    {
366a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
3676ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
368a6f13a4aSGreg Roach        }
369a6f13a4aSGreg Roach
370f315390bSGreg Roach        $style = [
371f315390bSGreg Roach            'name'  => $attrs['name'],
372f315390bSGreg Roach            'font'  => $attrs['font'] ?? $this->wt_report->default_font,
373f315390bSGreg Roach            'size'  => (float) ($attrs['size'] ?? $this->wt_report->default_font_size),
374f315390bSGreg Roach            'style' => $attrs['style'] ?? '',
375f315390bSGreg Roach        ];
376a6f13a4aSGreg Roach
377f315390bSGreg Roach        $this->wt_report->addStyle($style);
378a6f13a4aSGreg Roach    }
379a6f13a4aSGreg Roach
380a6f13a4aSGreg Roach    /**
381fab8f067SGreg Roach     * Handle <doc>
382a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
383a6f13a4aSGreg Roach     *
38409482a55SGreg Roach     * @param array<string> $attrs
3858ba2e626SGreg Roach     *
3868ba2e626SGreg Roach     * @return void
387a6f13a4aSGreg Roach     */
388b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
389c1010edaSGreg Roach    {
390e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
391a6f13a4aSGreg Roach
392a6f13a4aSGreg Roach        // Custom page width
393a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
394f315390bSGreg Roach            $this->wt_report->page_width = (float) $attrs['customwidth'];
395f315390bSGreg Roach        }
396a6f13a4aSGreg Roach        // Custom Page height
397a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
398f315390bSGreg Roach            $this->wt_report->page_height = (float) $attrs['customheight'];
399f315390bSGreg Roach        }
400a6f13a4aSGreg Roach
401a6f13a4aSGreg Roach        // Left Margin
402a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
4037a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
404208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
405a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
406f315390bSGreg Roach                $this->wt_report->left_margin = (float) $attrs['leftmargin'];
407a6f13a4aSGreg Roach            }
408a6f13a4aSGreg Roach        }
409a6f13a4aSGreg Roach        // Right Margin
410a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
4117a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
412208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
413a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
414f315390bSGreg Roach                $this->wt_report->right_margin = (float) $attrs['rightmargin'];
415a6f13a4aSGreg Roach            }
416a6f13a4aSGreg Roach        }
417a6f13a4aSGreg Roach        // Top Margin
418a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
4197a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
420208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
421a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
422f315390bSGreg Roach                $this->wt_report->top_margin = (float) $attrs['topmargin'];
423a6f13a4aSGreg Roach            }
424a6f13a4aSGreg Roach        }
425a6f13a4aSGreg Roach        // Bottom Margin
426a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
4277a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
428208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
429a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
430f315390bSGreg Roach                $this->wt_report->bottom_margin = (float) $attrs['bottommargin'];
431a6f13a4aSGreg Roach            }
432a6f13a4aSGreg Roach        }
433a6f13a4aSGreg Roach        // Header Margin
434a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
4357a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
436208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
437a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
438f315390bSGreg Roach                $this->wt_report->header_margin = (float) $attrs['headermargin'];
439a6f13a4aSGreg Roach            }
440a6f13a4aSGreg Roach        }
441a6f13a4aSGreg Roach        // Footer Margin
442a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
4437a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
444208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
445a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
446f315390bSGreg Roach                $this->wt_report->footer_margin = (float) $attrs['footermargin'];
447a6f13a4aSGreg Roach            }
448a6f13a4aSGreg Roach        }
449a6f13a4aSGreg Roach
450a6f13a4aSGreg Roach        // Page Orientation
451a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
452044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4537a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
454044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4557a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
456a6f13a4aSGreg Roach            }
457a6f13a4aSGreg Roach        }
458a6f13a4aSGreg Roach        // Page Size
459a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
460208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
461a6f13a4aSGreg Roach        }
462a6f13a4aSGreg Roach
463a6f13a4aSGreg Roach        // Show Generated By...
464a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4657a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
466208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4677a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
468208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
469a6f13a4aSGreg Roach            }
470a6f13a4aSGreg Roach        }
471a6f13a4aSGreg Roach
472e8e7866bSGreg Roach        $this->wt_report->setup();
473a6f13a4aSGreg Roach    }
474a6f13a4aSGreg Roach
475a6f13a4aSGreg Roach    /**
476fab8f067SGreg Roach     * Handle </doc>
4778ba2e626SGreg Roach     *
4788ba2e626SGreg Roach     * @return void
479a6f13a4aSGreg Roach     */
480b702978eSGreg Roach    protected function docEndHandler(): void
481c1010edaSGreg Roach    {
482e8e7866bSGreg Roach        $this->wt_report->run();
483a6f13a4aSGreg Roach    }
484a6f13a4aSGreg Roach
485a6f13a4aSGreg Roach    /**
486fab8f067SGreg Roach     * Handle <header>
4878ba2e626SGreg Roach     *
4888ba2e626SGreg Roach     * @return void
489a6f13a4aSGreg Roach     */
490b702978eSGreg Roach    protected function headerStartHandler(): void
491c1010edaSGreg Roach    {
492a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
493e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4947a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
495a6f13a4aSGreg Roach    }
496a6f13a4aSGreg Roach
497a6f13a4aSGreg Roach    /**
498fab8f067SGreg Roach     * Handle <body>
4998ba2e626SGreg Roach     *
5008ba2e626SGreg Roach     * @return void
501a6f13a4aSGreg Roach     */
502b702978eSGreg Roach    protected function bodyStartHandler(): void
503c1010edaSGreg Roach    {
5047a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
505a6f13a4aSGreg Roach    }
506a6f13a4aSGreg Roach
507a6f13a4aSGreg Roach    /**
508fab8f067SGreg Roach     * Handle <footer>
5098ba2e626SGreg Roach     *
5108ba2e626SGreg Roach     * @return void
511a6f13a4aSGreg Roach     */
512b702978eSGreg Roach    protected function footerStartHandler(): void
513c1010edaSGreg Roach    {
5147a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
515a6f13a4aSGreg Roach    }
516a6f13a4aSGreg Roach
517a6f13a4aSGreg Roach    /**
518fab8f067SGreg Roach     * Handle <cell>
519a6f13a4aSGreg Roach     *
52077bab461SGreg Roach     * @param array<string,string> $attrs
5218ba2e626SGreg Roach     *
5228ba2e626SGreg Roach     * @return void
523a6f13a4aSGreg Roach     */
524b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
525c1010edaSGreg Roach    {
526a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
52777bab461SGreg Roach        $align = $attrs['align'] ?? '';
528a6f13a4aSGreg Roach        // RTL supported left/right alignment
529044416d2SGreg Roach        if ($align === 'rightrtl') {
530e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5317a6ee1acSGreg Roach                $align = 'left';
532a6f13a4aSGreg Roach            } else {
5337a6ee1acSGreg Roach                $align = 'right';
534a6f13a4aSGreg Roach            }
535044416d2SGreg Roach        } elseif ($align === 'leftrtl') {
536e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5377a6ee1acSGreg Roach                $align = 'right';
538a6f13a4aSGreg Roach            } else {
5397a6ee1acSGreg Roach                $align = 'left';
540a6f13a4aSGreg Roach            }
541a6f13a4aSGreg Roach        }
542a6f13a4aSGreg Roach
54377bab461SGreg Roach        // The color to fill the background of this cell
54477bab461SGreg Roach        $bgcolor = $attrs['bgcolor'] ?? '';
545a6f13a4aSGreg Roach
54677bab461SGreg Roach        // Whether the background should be painted
547f315390bSGreg Roach        $fill = (bool) ($attrs['fill'] ?? '0');
548a6f13a4aSGreg Roach
54977bab461SGreg Roach        // If true reset the last cell height
55077bab461SGreg Roach        $reseth = (bool) ($attrs['reseth'] ?? '1');
551a6f13a4aSGreg Roach
55277bab461SGreg Roach        // Whether a border should be printed around this box
55377bab461SGreg Roach        $border = $attrs['border'] ?? '';
55477bab461SGreg Roach
555a6f13a4aSGreg Roach        // string Border color in HTML code
55677bab461SGreg Roach        $bocolor = $attrs['bocolor'] ?? '';
557a6f13a4aSGreg Roach
55877bab461SGreg Roach        // Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
55977bab461SGreg Roach        $height = (int) ($attrs['height'] ?? '0');
56077bab461SGreg Roach
561a6f13a4aSGreg 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.
56277bab461SGreg Roach        $width = (int) ($attrs['width'] ?? '0');
563a6f13a4aSGreg Roach
56477bab461SGreg Roach        // Stretch character mode
56577bab461SGreg Roach        $stretch = (int) ($attrs['stretch'] ?? '0');
566a6f13a4aSGreg Roach
567a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
568c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
569a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5707a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
571c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
572a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
573f315390bSGreg Roach                $left = (float) $attrs['left'];
5747a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
575f315390bSGreg Roach                $left = 0.0;
576a6f13a4aSGreg Roach            }
577a6f13a4aSGreg Roach        }
578a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
579c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
580a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5817a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
582c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
583a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
584f315390bSGreg Roach                $top = (float) $attrs['top'];
5857a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
586f315390bSGreg Roach                $top = 0.0;
587a6f13a4aSGreg Roach            }
588a6f13a4aSGreg Roach        }
589a6f13a4aSGreg Roach
59077bab461SGreg Roach        // The name of the Style that should be used to render the text.
59177bab461SGreg Roach        $style = $attrs['style'] ?? '';
592a6f13a4aSGreg Roach
593a6f13a4aSGreg Roach        // string Text color in html code
59477bab461SGreg Roach        $tcolor = $attrs['tcolor'] ?? '';
595a6f13a4aSGreg Roach
596a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
597a6f13a4aSGreg Roach        $ln = 0;
598a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
599a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
600a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
6017a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
602a6f13a4aSGreg Roach                $ln = 0;
603a6f13a4aSGreg Roach            }
604a6f13a4aSGreg Roach        }
605a6f13a4aSGreg Roach
606044416d2SGreg Roach        if ($align === 'left') {
6077a6ee1acSGreg Roach            $align = 'L';
608044416d2SGreg Roach        } elseif ($align === 'right') {
6097a6ee1acSGreg Roach            $align = 'R';
610044416d2SGreg Roach        } elseif ($align === 'center') {
6117a6ee1acSGreg Roach            $align = 'C';
612044416d2SGreg Roach        } elseif ($align === 'justify') {
6137a6ee1acSGreg Roach            $align = 'J';
614a6f13a4aSGreg Roach        }
615a6f13a4aSGreg Roach
6169b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
617a6f13a4aSGreg Roach        $this->print_data         = true;
618a6f13a4aSGreg Roach
619e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
62024f2a3afSGreg Roach            (int) $width,
62124f2a3afSGreg Roach            (int) $height,
622a6f13a4aSGreg Roach            $border,
623a6f13a4aSGreg Roach            $align,
624a6f13a4aSGreg Roach            $bgcolor,
625a6f13a4aSGreg Roach            $style,
626a6f13a4aSGreg Roach            $ln,
627a6f13a4aSGreg Roach            $top,
628a6f13a4aSGreg Roach            $left,
629a6f13a4aSGreg Roach            $fill,
630a6f13a4aSGreg Roach            $stretch,
631a6f13a4aSGreg Roach            $bocolor,
632a6f13a4aSGreg Roach            $tcolor,
633a6f13a4aSGreg Roach            $reseth
634a6f13a4aSGreg Roach        );
635a6f13a4aSGreg Roach    }
636a6f13a4aSGreg Roach
637a6f13a4aSGreg Roach    /**
638fab8f067SGreg Roach     * Handle </cell>
6398ba2e626SGreg Roach     *
6408ba2e626SGreg Roach     * @return void
641a6f13a4aSGreg Roach     */
642b702978eSGreg Roach    protected function cellEndHandler(): void
643c1010edaSGreg Roach    {
644a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
645e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
646a6f13a4aSGreg Roach    }
647a6f13a4aSGreg Roach
648a6f13a4aSGreg Roach    /**
649fab8f067SGreg Roach     * Handle <now />
6508ba2e626SGreg Roach     *
6518ba2e626SGreg Roach     * @return void
652a6f13a4aSGreg Roach     */
653b702978eSGreg Roach    protected function nowStartHandler(): void
654c1010edaSGreg Roach    {
655d97083feSGreg Roach        $this->current_element->addText(Registry::timestampFactory()->now()->isoFormat('LLLL'));
656a6f13a4aSGreg Roach    }
657a6f13a4aSGreg Roach
658a6f13a4aSGreg Roach    /**
659fab8f067SGreg Roach     * Handle <pageNum />
6608ba2e626SGreg Roach     *
6618ba2e626SGreg Roach     * @return void
662a6f13a4aSGreg Roach     */
663b702978eSGreg Roach    protected function pageNumStartHandler(): void
664c1010edaSGreg Roach    {
6657a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
666a6f13a4aSGreg Roach    }
667a6f13a4aSGreg Roach
668a6f13a4aSGreg Roach    /**
669fab8f067SGreg Roach     * Handle <totalPages />
6708ba2e626SGreg Roach     *
6718ba2e626SGreg Roach     * @return void
672a6f13a4aSGreg Roach     */
673b702978eSGreg Roach    protected function totalPagesStartHandler(): void
674c1010edaSGreg Roach    {
6757a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
676a6f13a4aSGreg Roach    }
677a6f13a4aSGreg Roach
678a6f13a4aSGreg Roach    /**
679a6f13a4aSGreg Roach     * Called at the start of an element.
680a6f13a4aSGreg Roach     *
68109482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
6828ba2e626SGreg Roach     *
6838ba2e626SGreg Roach     * @return void
684a6f13a4aSGreg Roach     */
685b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
686c1010edaSGreg Roach    {
687a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
688a6f13a4aSGreg Roach            $this->process_gedcoms++;
689a6f13a4aSGreg Roach
690a6f13a4aSGreg Roach            return;
691a6f13a4aSGreg Roach        }
692a6f13a4aSGreg Roach
693a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6947a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6957a6ee1acSGreg Roach        $tags      = explode(':', $tag);
696a6f13a4aSGreg Roach        $newgedrec = '';
697a6f13a4aSGreg Roach        if (count($tags) < 2) {
6986b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
699299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
700a6f13a4aSGreg Roach        }
701a6f13a4aSGreg Roach        if (empty($newgedrec)) {
702a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
703a6f13a4aSGreg Roach            $newgedrec = '';
704a6f13a4aSGreg Roach            foreach ($tags as $tag) {
7057a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
706d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
707d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
708a6f13a4aSGreg Roach                    } else {
7096b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
710299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
711a6f13a4aSGreg Roach                    }
712a6f13a4aSGreg Roach                } else {
7137a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
71413abd6f3SGreg Roach                        $gmatch = [];
715a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7166b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
717299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
718a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
719a6f13a4aSGreg Roach                        } else {
720a6f13a4aSGreg Roach                            $newgedrec = '';
721a6f13a4aSGreg Roach                            break;
722a6f13a4aSGreg Roach                        }
723a6f13a4aSGreg Roach                    } else {
724b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
725b315f3e1SGreg Roach                        $newgedrec = self::getSubRecord($level, "$level $tag", $tgedrec);
726a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
727a6f13a4aSGreg Roach                    }
728a6f13a4aSGreg Roach                }
729a6f13a4aSGreg Roach            }
730a6f13a4aSGreg Roach        }
731a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7329b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
733a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
734a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
735a6f13a4aSGreg Roach                $this->fact = $match[2];
736a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
737a6f13a4aSGreg Roach            }
738a6f13a4aSGreg Roach        } else {
739a6f13a4aSGreg Roach            $this->process_gedcoms++;
740a6f13a4aSGreg Roach        }
741a6f13a4aSGreg Roach    }
742a6f13a4aSGreg Roach
743a6f13a4aSGreg Roach    /**
744a6f13a4aSGreg Roach     * Called at the end of an element.
7458ba2e626SGreg Roach     *
7468ba2e626SGreg Roach     * @return void
747a6f13a4aSGreg Roach     */
748b702978eSGreg Roach    protected function gedcomEndHandler(): void
749c1010edaSGreg Roach    {
750a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
751a6f13a4aSGreg Roach            $this->process_gedcoms--;
752a6f13a4aSGreg Roach        } else {
75365e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
754a6f13a4aSGreg Roach        }
755a6f13a4aSGreg Roach    }
756a6f13a4aSGreg Roach
757a6f13a4aSGreg Roach    /**
758fab8f067SGreg Roach     * Handle <textBox>
759a6f13a4aSGreg Roach     *
76009482a55SGreg Roach     * @param array<string> $attrs
7618ba2e626SGreg Roach     *
7628ba2e626SGreg Roach     * @return void
763a6f13a4aSGreg Roach     */
764b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
765c1010edaSGreg Roach    {
766a6f13a4aSGreg Roach        // string Background color code
7677a6ee1acSGreg Roach        $bgcolor = '';
768a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
769a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
770a6f13a4aSGreg Roach        }
771a6f13a4aSGreg Roach
772a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
773a6f13a4aSGreg Roach        $fill = true;
774a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7757a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
776a6f13a4aSGreg Roach                $fill = false;
7777a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
778a6f13a4aSGreg Roach                $fill = true;
779a6f13a4aSGreg Roach            }
780a6f13a4aSGreg Roach        }
781a6f13a4aSGreg Roach
782a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
783a6f13a4aSGreg Roach        $border = false;
784a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7857a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
786a6f13a4aSGreg Roach                $border = true;
7877a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
788a6f13a4aSGreg Roach                $border = false;
789a6f13a4aSGreg Roach            }
790a6f13a4aSGreg Roach        }
791a6f13a4aSGreg Roach
792a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
793a6f13a4aSGreg Roach        $height = 0;
794a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
795a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
796a6f13a4aSGreg Roach        }
797a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
798a6f13a4aSGreg Roach        $width = 0;
799a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
800a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
801a6f13a4aSGreg Roach        }
802a6f13a4aSGreg Roach
803a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
804c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
805a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
8067a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
807c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
808a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
809a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
8107a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
811a6f13a4aSGreg Roach                $left = 0;
812a6f13a4aSGreg Roach            }
813a6f13a4aSGreg Roach        }
814a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
815c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
816a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8177a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
818c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
819a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
820a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8217a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
822a6f13a4aSGreg Roach                $top = 0;
823a6f13a4aSGreg Roach            }
824a6f13a4aSGreg Roach        }
825a6f13a4aSGreg 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
826a6f13a4aSGreg Roach        $newline = false;
827a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8287a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
829a6f13a4aSGreg Roach                $newline = true;
8307a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
831a6f13a4aSGreg Roach                $newline = false;
832a6f13a4aSGreg Roach            }
833a6f13a4aSGreg Roach        }
834a6f13a4aSGreg Roach        // boolean
835a6f13a4aSGreg Roach        $pagecheck = true;
836a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8377a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
838a6f13a4aSGreg Roach                $pagecheck = false;
8397a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
840a6f13a4aSGreg Roach                $pagecheck = true;
841a6f13a4aSGreg Roach            }
842a6f13a4aSGreg Roach        }
843a6f13a4aSGreg Roach        // boolean Cell padding
844a6f13a4aSGreg Roach        $padding = true;
845a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8467a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
847a6f13a4aSGreg Roach                $padding = false;
8487a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
849a6f13a4aSGreg Roach                $padding = true;
850a6f13a4aSGreg Roach            }
851a6f13a4aSGreg Roach        }
852a6f13a4aSGreg Roach        // boolean Reset this box Height
853a6f13a4aSGreg Roach        $reseth = false;
854a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8557a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
856a6f13a4aSGreg Roach                $reseth = true;
8577a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
858a6f13a4aSGreg Roach                $reseth = false;
859a6f13a4aSGreg Roach            }
860a6f13a4aSGreg Roach        }
861a6f13a4aSGreg Roach
862a6f13a4aSGreg Roach        // string Style of rendering
8637a6ee1acSGreg Roach        $style = '';
864a6f13a4aSGreg Roach
8659b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
866a6f13a4aSGreg Roach        $this->print_data         = false;
867a6f13a4aSGreg Roach
8689b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
869e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
870a6f13a4aSGreg Roach            $width,
871a6f13a4aSGreg Roach            $height,
872a6f13a4aSGreg Roach            $border,
873a6f13a4aSGreg Roach            $bgcolor,
874a6f13a4aSGreg Roach            $newline,
875a6f13a4aSGreg Roach            $left,
876a6f13a4aSGreg Roach            $top,
877a6f13a4aSGreg Roach            $pagecheck,
878a6f13a4aSGreg Roach            $style,
879a6f13a4aSGreg Roach            $fill,
880a6f13a4aSGreg Roach            $padding,
881a6f13a4aSGreg Roach            $reseth
882a6f13a4aSGreg Roach        );
883a6f13a4aSGreg Roach    }
884a6f13a4aSGreg Roach
885a6f13a4aSGreg Roach    /**
886fab8f067SGreg Roach     * Handle <textBox>
8878ba2e626SGreg Roach     *
8888ba2e626SGreg Roach     * @return void
889a6f13a4aSGreg Roach     */
890b702978eSGreg Roach    protected function textBoxEndHandler(): void
891c1010edaSGreg Roach    {
892a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
893e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
89431d9777aSGreg Roach
89531d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
89631d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
89731d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
89831d9777aSGreg Roach
899e8e7866bSGreg Roach        $this->wt_report = array_pop($this->wt_report_stack);
900e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
901a6f13a4aSGreg Roach    }
902a6f13a4aSGreg Roach
903a6f13a4aSGreg Roach    /**
90476692c8bSGreg Roach     * XLM <Text>.
90576692c8bSGreg Roach     *
90609482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9078ba2e626SGreg Roach     *
9088ba2e626SGreg Roach     * @return void
909a6f13a4aSGreg Roach     */
910b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
911c1010edaSGreg Roach    {
9129b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
913a6f13a4aSGreg Roach        $this->print_data         = true;
914a6f13a4aSGreg Roach
915a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9167a6ee1acSGreg Roach        $style = '';
917a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
918a6f13a4aSGreg Roach            $style = $attrs['style'];
919a6f13a4aSGreg Roach        }
920a6f13a4aSGreg Roach
921a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9227a6ee1acSGreg Roach        $color = '';
923a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
924a6f13a4aSGreg Roach            $color = $attrs['color'];
925a6f13a4aSGreg Roach        }
926a6f13a4aSGreg Roach
927e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
928a6f13a4aSGreg Roach    }
929a6f13a4aSGreg Roach
930a6f13a4aSGreg Roach    /**
931fab8f067SGreg Roach     * Handle </text>
9328ba2e626SGreg Roach     *
9338ba2e626SGreg Roach     * @return void
934a6f13a4aSGreg Roach     */
935b702978eSGreg Roach    protected function textEndHandler(): void
936c1010edaSGreg Roach    {
937a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
938e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
939a6f13a4aSGreg Roach    }
940a6f13a4aSGreg Roach
941a6f13a4aSGreg Roach    /**
942fab8f067SGreg Roach     * Handle <getPersonName />
943a6f13a4aSGreg Roach     * Get the name
944a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
945a6f13a4aSGreg Roach     * 2. id is set with a record id
946a6f13a4aSGreg Roach     *
94709482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9488ba2e626SGreg Roach     *
9498ba2e626SGreg Roach     * @return void
950a6f13a4aSGreg Roach     */
951b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
952c1010edaSGreg Roach    {
9537a6ee1acSGreg Roach        $id    = '';
95413abd6f3SGreg Roach        $match = [];
955a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9567a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
957a6f13a4aSGreg Roach                $id = $match[1];
958a6f13a4aSGreg Roach            }
959a6f13a4aSGreg Roach        } else {
9607a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
961d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
962d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
963a6f13a4aSGreg Roach                }
964a6f13a4aSGreg Roach            } else {
9657a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
96613abd6f3SGreg Roach                    $gmatch = [];
967a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
968a6f13a4aSGreg Roach                        $id = $gmatch[1];
969a6f13a4aSGreg Roach                    }
970a6f13a4aSGreg Roach                } else {
971a6f13a4aSGreg Roach                    $id = $attrs['id'];
972a6f13a4aSGreg Roach                }
973a6f13a4aSGreg Roach            }
974a6f13a4aSGreg Roach        }
975a6f13a4aSGreg Roach        if (!empty($id)) {
9766b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9778f038c36SRico Sonntag            if ($record === null) {
978a6f13a4aSGreg Roach                return;
979a6f13a4aSGreg Roach            }
980a6f13a4aSGreg Roach            if (!$record->canShowName()) {
981a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
982a6f13a4aSGreg Roach            } else {
98339ca88baSGreg Roach                $name = $record->fullName();
984a6f13a4aSGreg Roach                $name = strip_tags($name);
985a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
986381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
987a6f13a4aSGreg Roach                } else {
988911124f5SGreg Roach                    $addname = (string) $record->alternateName();
989a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
990a6f13a4aSGreg Roach                    if (!empty($addname)) {
9917a6ee1acSGreg Roach                        $name .= ' ' . $addname;
992a6f13a4aSGreg Roach                    }
993a6f13a4aSGreg Roach                }
994a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
995a6f13a4aSGreg Roach            }
996a6f13a4aSGreg Roach        }
997a6f13a4aSGreg Roach    }
998a6f13a4aSGreg Roach
999a6f13a4aSGreg Roach    /**
1000fab8f067SGreg Roach     * Handle <gedcomValue />
1001a6f13a4aSGreg Roach     *
100209482a55SGreg Roach     * @param array<string> $attrs
10038ba2e626SGreg Roach     *
10048ba2e626SGreg Roach     * @return void
1005a6f13a4aSGreg Roach     */
1006b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1007c1010edaSGreg Roach    {
10087a6ee1acSGreg Roach        $id    = '';
100913abd6f3SGreg Roach        $match = [];
10107a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1011a6f13a4aSGreg Roach            $id = $match[1];
1012a6f13a4aSGreg Roach        }
1013a6f13a4aSGreg Roach
1014044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10157a6ee1acSGreg Roach            $useBreak = '1';
1016a6f13a4aSGreg Roach        } else {
10177a6ee1acSGreg Roach            $useBreak = '0';
1018a6f13a4aSGreg Roach        }
1019a6f13a4aSGreg Roach
1020a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1021a6f13a4aSGreg Roach        if (!empty($tag)) {
1022044416d2SGreg Roach            if ($tag === '@desc') {
1023a6f13a4aSGreg Roach                $value = $this->desc;
1024a6f13a4aSGreg Roach                $value = trim($value);
1025a6f13a4aSGreg Roach                $this->current_element->addText($value);
1026a6f13a4aSGreg Roach            }
1027044416d2SGreg Roach            if ($tag === '@id') {
1028a6f13a4aSGreg Roach                $this->current_element->addText($id);
1029a6f13a4aSGreg Roach            } else {
10307a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1031a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1032b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1033b2448a1bSGreg Roach                    if ($level === 0) {
1034a6f13a4aSGreg Roach                        $level++;
1035a6f13a4aSGreg Roach                    }
1036a6f13a4aSGreg Roach                } else {
1037b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1038a6f13a4aSGreg Roach                }
1039a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10403d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1041a6f13a4aSGreg Roach                switch (end($tags)) {
1042a6f13a4aSGreg Roach                    case 'DATE':
1043a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1044326dd299SGreg Roach                        $value = strip_tags($tmp->display());
1045a6f13a4aSGreg Roach                        break;
1046a6f13a4aSGreg Roach                    case 'PLAC':
1047299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1048392561bbSGreg Roach                        $value = $tmp->shortName();
1049a6f13a4aSGreg Roach                        break;
1050a6f13a4aSGreg Roach                }
1051044416d2SGreg Roach                if ($useBreak === '1') {
1052a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1053a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1054a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1055a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1056a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10573b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10583b3cfeeaSGreg Roach                        $value = substr($value, 4);
1059a6f13a4aSGreg Roach                    }
1060a6f13a4aSGreg Roach                }
1061d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10623cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10634d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
1064ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($value, $this->tree), ['br']);
10654d35caa7SGreg Roach                    } else {
1066ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($value, $this->tree), ['br']);
10674d35caa7SGreg Roach                    }
1068ab2c9152SGreg Roach                    $value = strtr($value, [MarkdownFactory::BREAK => ' ']);
1069a4d703aeSGreg Roach                }
1070a2a485c3SGreg Roach
1071a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1072381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1073a2a485c3SGreg Roach                }
1074a6f13a4aSGreg Roach                $this->current_element->addText($value);
1075a6f13a4aSGreg Roach            }
1076a6f13a4aSGreg Roach        }
1077a6f13a4aSGreg Roach    }
1078a6f13a4aSGreg Roach
1079a6f13a4aSGreg Roach    /**
1080fab8f067SGreg Roach     * Handle <repeatTag>
1081a6f13a4aSGreg Roach     *
108209482a55SGreg Roach     * @param array<string> $attrs
10838ba2e626SGreg Roach     *
10848ba2e626SGreg Roach     * @return void
1085a6f13a4aSGreg Roach     */
1086b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1087c1010edaSGreg Roach    {
1088a6f13a4aSGreg Roach        $this->process_repeats++;
1089a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1090a6f13a4aSGreg Roach            return;
1091a6f13a4aSGreg Roach        }
1092a6f13a4aSGreg Roach
10939b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
109413abd6f3SGreg Roach        $this->repeats         = [];
1095e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1096a6f13a4aSGreg Roach
1097e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1098a6f13a4aSGreg Roach        if (!empty($tag)) {
1099044416d2SGreg Roach            if ($tag === '@desc') {
1100a6f13a4aSGreg Roach                $value = $this->desc;
1101a6f13a4aSGreg Roach                $value = trim($value);
1102a6f13a4aSGreg Roach                $this->current_element->addText($value);
1103a6f13a4aSGreg Roach            } else {
11047a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11057a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1106b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1107b2448a1bSGreg Roach                if ($level === 0) {
1108a6f13a4aSGreg Roach                    $level++;
1109a6f13a4aSGreg Roach                }
1110a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1111a6f13a4aSGreg Roach                $t      = $tag;
1112a6f13a4aSGreg Roach                $count  = count($tags);
1113a6f13a4aSGreg Roach                $i      = 0;
1114a6f13a4aSGreg Roach                while ($i < $count) {
1115a6f13a4aSGreg Roach                    $t = $tags[$i];
1116a6f13a4aSGreg Roach                    if (!empty($t)) {
1117a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
1118b315f3e1SGreg Roach                            $subrec = self::getSubRecord($level, "$level $t", $subrec);
1119a6f13a4aSGreg Roach                            if (empty($subrec)) {
1120a6f13a4aSGreg Roach                                $level--;
1121b315f3e1SGreg Roach                                $subrec = self::getSubRecord($level, "@ $t", $this->gedrec);
1122a6f13a4aSGreg Roach                                if (empty($subrec)) {
1123a6f13a4aSGreg Roach                                    return;
1124a6f13a4aSGreg Roach                                }
1125a6f13a4aSGreg Roach                            }
1126a6f13a4aSGreg Roach                        }
1127a6f13a4aSGreg Roach                        $level++;
1128a6f13a4aSGreg Roach                    }
1129a6f13a4aSGreg Roach                    $i++;
1130a6f13a4aSGreg Roach                }
1131a6f13a4aSGreg Roach                $level--;
1132a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1133a6f13a4aSGreg Roach                $i     = 0;
1134a6f13a4aSGreg Roach                while ($i < $count) {
1135a6f13a4aSGreg Roach                    $i++;
1136a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1137b315f3e1SGreg Roach                    $subrecord = self::getSubRecord($level, "$level $t", $subrec, $i);
11388d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11396b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1140a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1141a9007102SGreg Roach                            continue;
1142a9007102SGreg Roach                        }
1143a9007102SGreg Roach                    }
1144a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1145a6f13a4aSGreg Roach                }
1146a6f13a4aSGreg Roach            }
1147a6f13a4aSGreg Roach        }
1148a6f13a4aSGreg Roach    }
1149a6f13a4aSGreg Roach
1150a6f13a4aSGreg Roach    /**
1151fab8f067SGreg Roach     * Handle </repeatTag>
11528ba2e626SGreg Roach     *
11538ba2e626SGreg Roach     * @return void
1154a6f13a4aSGreg Roach     */
1155b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1156c1010edaSGreg Roach    {
1157a6f13a4aSGreg Roach        $this->process_repeats--;
1158a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1159a6f13a4aSGreg Roach            return;
1160a6f13a4aSGreg Roach        }
1161a6f13a4aSGreg Roach
1162a6f13a4aSGreg Roach        // Check if there is anything to repeat
1163a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1164a6f13a4aSGreg Roach            // No need to load them if not used...
1165a6f13a4aSGreg Roach
1166a6f13a4aSGreg Roach            $lineoffset = 0;
1167a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1168a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1169a6f13a4aSGreg Roach            }
1170a6f13a4aSGreg Roach            //-- read the xml from the file
1171299d100dSGreg Roach            $lines = file($this->report);
1172dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1173a6f13a4aSGreg Roach                $lineoffset--;
1174a6f13a4aSGreg Roach            }
1175a6f13a4aSGreg Roach            $lineoffset++;
1176a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1177a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1178a6f13a4aSGreg Roach            // RepeatTag Level counter
1179a6f13a4aSGreg Roach            $count = 1;
1180a6f13a4aSGreg Roach            while (0 < $count) {
1181dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1182a6f13a4aSGreg Roach                    $count++;
1183dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1184a6f13a4aSGreg Roach                    $count--;
1185a6f13a4aSGreg Roach                }
1186a6f13a4aSGreg Roach                if (0 < $count) {
1187a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1188a6f13a4aSGreg Roach                }
1189a6f13a4aSGreg Roach                $line_nr++;
1190a6f13a4aSGreg Roach            }
1191a6f13a4aSGreg Roach            // No need to drag this
1192a6f13a4aSGreg Roach            unset($lines);
1193a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1194a6f13a4aSGreg Roach            // Save original values
11959b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1196a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1197a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1198a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1199a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1200e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1201f315390bSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
12021aa04befSGreg Roach
12031aa04befSGreg Roach                xml_set_element_handler(
12041aa04befSGreg Roach                    $repeat_parser,
12059d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12061aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12071aa04befSGreg Roach                    },
12089d454b6bSGreg Roach                    function ($parser, string $name): void {
12091aa04befSGreg Roach                        $this->endElement($parser, $name);
12101aa04befSGreg Roach                    }
12111aa04befSGreg Roach                );
12121aa04befSGreg Roach
12131aa04befSGreg Roach                xml_set_character_data_handler(
12141aa04befSGreg Roach                    $repeat_parser,
12159d454b6bSGreg Roach                    function ($parser, string $data): void {
12161aa04befSGreg Roach                        $this->characterData($parser, $data);
12171aa04befSGreg Roach                    }
12181aa04befSGreg Roach                );
12191aa04befSGreg Roach
1220a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12216ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1222a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1223a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1224a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1225a6f13a4aSGreg Roach                    ));
1226a6f13a4aSGreg Roach                }
1227a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1228a6f13a4aSGreg Roach            }
1229a6f13a4aSGreg Roach            // Restore original values
1230a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1231e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1232a6f13a4aSGreg Roach        }
123365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1234a6f13a4aSGreg Roach    }
1235a6f13a4aSGreg Roach
1236a6f13a4aSGreg Roach    /**
1237a6f13a4aSGreg Roach     * Variable lookup
1238a6f13a4aSGreg Roach     * Retrieve predefined variables :
1239a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1240a6f13a4aSGreg Roach     *        1 EVEN This is a description
1241a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1242a6f13a4aSGreg Roach     * $ I18N::translate('....')
1243a6f13a4aSGreg Roach     * $ language_settings[]
1244a6f13a4aSGreg Roach     *
124509482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
12468ba2e626SGreg Roach     *
12478ba2e626SGreg Roach     * @return void
1248a6f13a4aSGreg Roach     */
1249b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1250c1010edaSGreg Roach    {
1251a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12526ccdf4f0SGreg 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));
1253a6f13a4aSGreg Roach        }
1254a6f13a4aSGreg Roach
1255a6f13a4aSGreg Roach        $var = $attrs['var'];
1256a6f13a4aSGreg Roach        // SetVar element preset variables
1257d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1258d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1259a6f13a4aSGreg Roach        } else {
1260a6f13a4aSGreg Roach            $tfact = $this->fact;
12617a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1262a6f13a4aSGreg Roach                // Use :
1263a6f13a4aSGreg Roach                // n TYPE This text if string
1264a6f13a4aSGreg Roach                $tfact = $this->type;
126597def6bcSGreg Roach            } else {
126697def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
126797def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
126897def6bcSGreg Roach
126997def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
127097def6bcSGreg Roach                        $tfact = $element->label();
127197def6bcSGreg Roach                        break;
1272a6f13a4aSGreg Roach                    }
127397def6bcSGreg Roach                }
127497def6bcSGreg Roach            }
127597def6bcSGreg Roach
127697def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
127797def6bcSGreg Roach
1278a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1279da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1280a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1281a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1282a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1283a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1284a6f13a4aSGreg Roach            }
1285a6f13a4aSGreg Roach        }
1286a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1287a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12887a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1289a6f13a4aSGreg Roach                $g   = new Date($var);
1290a6f13a4aSGreg Roach                $var = $g->display();
1291a6f13a4aSGreg Roach            }
1292a6f13a4aSGreg Roach        }
1293a6f13a4aSGreg Roach        $this->current_element->addText($var);
12942836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1295a6f13a4aSGreg Roach    }
1296a6f13a4aSGreg Roach
1297a6f13a4aSGreg Roach    /**
1298fab8f067SGreg Roach     * Handle <facts>
129976692c8bSGreg Roach     *
130009482a55SGreg Roach     * @param array<string> $attrs
13018ba2e626SGreg Roach     *
13028ba2e626SGreg Roach     * @return void
1303a6f13a4aSGreg Roach     */
1304b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1305c1010edaSGreg Roach    {
1306a6f13a4aSGreg Roach        $this->process_repeats++;
1307a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1308a6f13a4aSGreg Roach            return;
1309a6f13a4aSGreg Roach        }
1310a6f13a4aSGreg Roach
13119b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
131213abd6f3SGreg Roach        $this->repeats         = [];
1313e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1314a6f13a4aSGreg Roach
13157a6ee1acSGreg Roach        $id    = '';
131613abd6f3SGreg Roach        $match = [];
13177a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1318a6f13a4aSGreg Roach            $id = $match[1];
1319a6f13a4aSGreg Roach        }
13207a6ee1acSGreg Roach        $tag = '';
1321a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1322a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1323a6f13a4aSGreg Roach        }
13247a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1325d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1326a6f13a4aSGreg Roach        }
1327a6f13a4aSGreg Roach
13286b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1329a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1330820b62dfSGreg Roach            $facts = $record->facts([], true);
133113abd6f3SGreg Roach            $this->repeats = [];
1332a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1333195b5e75SGreg Roach            foreach ($facts as $fact) {
1334d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1335d0889c63SGreg Roach
1336d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1337195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1338a6f13a4aSGreg Roach                }
1339a6f13a4aSGreg Roach            }
1340a6f13a4aSGreg Roach        } else {
134130158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1342d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1343138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1344a6f13a4aSGreg Roach                }
1345a6f13a4aSGreg Roach            }
1346a6f13a4aSGreg Roach        }
1347a6f13a4aSGreg Roach    }
1348a6f13a4aSGreg Roach
1349a6f13a4aSGreg Roach    /**
1350fab8f067SGreg Roach     * Handle </facts>
13518ba2e626SGreg Roach     *
13528ba2e626SGreg Roach     * @return void
1353a6f13a4aSGreg Roach     */
1354b702978eSGreg Roach    protected function factsEndHandler(): void
1355c1010edaSGreg Roach    {
1356a6f13a4aSGreg Roach        $this->process_repeats--;
1357a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1358a6f13a4aSGreg Roach            return;
1359a6f13a4aSGreg Roach        }
1360a6f13a4aSGreg Roach
1361a6f13a4aSGreg Roach        // Check if there is anything to repeat
1362a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1363e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1364a6f13a4aSGreg Roach            $lineoffset = 0;
1365a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1366a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1367a6f13a4aSGreg Roach            }
1368a6f13a4aSGreg Roach
1369a6f13a4aSGreg Roach            //-- read the xml from the file
1370299d100dSGreg Roach            $lines = file($this->report);
1371dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1372a6f13a4aSGreg Roach                $lineoffset--;
1373a6f13a4aSGreg Roach            }
1374a6f13a4aSGreg Roach            $lineoffset++;
1375a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1376a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1377a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1378a6f13a4aSGreg Roach            while ($line_nr < $i) {
1379a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1380a6f13a4aSGreg Roach                $line_nr++;
1381a6f13a4aSGreg Roach            }
1382a6f13a4aSGreg Roach            // No need to drag this
1383a6f13a4aSGreg Roach            unset($lines);
1384a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1385a6f13a4aSGreg Roach            // Save original values
13869b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1387a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1388a6f13a4aSGreg Roach            $count                = count($this->repeats);
1389a6f13a4aSGreg Roach            $i                    = 0;
1390a6f13a4aSGreg Roach            while ($i < $count) {
1391a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1392a6f13a4aSGreg Roach                $this->fact   = '';
1393a6f13a4aSGreg Roach                $this->desc   = '';
1394a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1395a6f13a4aSGreg Roach                    $this->fact = $match[1];
1396a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
139713abd6f3SGreg Roach                        $tmatch = [];
1398a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1399a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1400a6f13a4aSGreg Roach                        } else {
1401a6f13a4aSGreg Roach                            $this->type = ' ';
1402a6f13a4aSGreg Roach                        }
1403a6f13a4aSGreg Roach                    }
1404a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
1405b315f3e1SGreg Roach                    $this->desc .= self::getCont(2, $this->gedrec);
1406a6f13a4aSGreg Roach                }
1407a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1408e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1409f315390bSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
14101aa04befSGreg Roach
14111aa04befSGreg Roach                xml_set_element_handler(
14121aa04befSGreg Roach                    $repeat_parser,
14139d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14141aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14151aa04befSGreg Roach                    },
14169d454b6bSGreg Roach                    function ($parser, string $name): void {
14171aa04befSGreg Roach                        $this->endElement($parser, $name);
14181aa04befSGreg Roach                    }
14191aa04befSGreg Roach                );
14201aa04befSGreg Roach
14211aa04befSGreg Roach                xml_set_character_data_handler(
14221aa04befSGreg Roach                    $repeat_parser,
14239d454b6bSGreg Roach                    function ($parser, string $data): void {
14241aa04befSGreg Roach                        $this->characterData($parser, $data);
14251aa04befSGreg Roach                    }
14261aa04befSGreg Roach                );
14271aa04befSGreg Roach
1428a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14296ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1430a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1431a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1432a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1433a6f13a4aSGreg Roach                    ));
1434a6f13a4aSGreg Roach                }
1435a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1436a6f13a4aSGreg Roach                $i++;
1437a6f13a4aSGreg Roach            }
1438a6f13a4aSGreg Roach            // Restore original values
1439e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1440a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1441a6f13a4aSGreg Roach        }
144265e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1443a6f13a4aSGreg Roach    }
1444a6f13a4aSGreg Roach
1445a6f13a4aSGreg Roach    /**
1446a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1447d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1448a6f13a4aSGreg Roach     *
144909482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
14508ba2e626SGreg Roach     *
14518ba2e626SGreg Roach     * @return void
1452a6f13a4aSGreg Roach     */
1453b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1454c1010edaSGreg Roach    {
1455a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14566ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1457a6f13a4aSGreg Roach        }
1458a6f13a4aSGreg Roach
1459a6f13a4aSGreg Roach        $name  = $attrs['name'];
1460a6f13a4aSGreg Roach        $value = $attrs['value'];
146113abd6f3SGreg Roach        $match = [];
1462a6f13a4aSGreg Roach        // Current GEDCOM record strings
1463044416d2SGreg Roach        if ($value === '@ID') {
14647a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1465a6f13a4aSGreg Roach                $value = $match[1];
1466a6f13a4aSGreg Roach            }
1467044416d2SGreg Roach        } elseif ($value === '@fact') {
1468a6f13a4aSGreg Roach            $value = $this->fact;
1469044416d2SGreg Roach        } elseif ($value === '@desc') {
1470a6f13a4aSGreg Roach            $value = $this->desc;
1471044416d2SGreg Roach        } elseif ($value === '@generation') {
1472589feda3SGreg Roach            $value = (string) $this->generation;
1473a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
147413abd6f3SGreg Roach            $gmatch = [];
1475a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14767a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1477a6f13a4aSGreg Roach            }
1478a6f13a4aSGreg Roach        }
1479a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1480d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1481a6f13a4aSGreg Roach        }
1482a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1483a6f13a4aSGreg Roach        $i     = 0;
1484a6f13a4aSGreg Roach        while ($i < $count) {
1485d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14867a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1487a6f13a4aSGreg Roach            $i++;
1488a6f13a4aSGreg Roach        }
1489a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1490da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1491a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1492a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1493a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1494a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1495a6f13a4aSGreg Roach        }
149652868398SGreg Roach
1497a6f13a4aSGreg Roach        // Arithmetic functions
14983cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
149952868398SGreg Roach            // Create an expression language with the functions used by our reports.
150052868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1501c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1502c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
150352868398SGreg Roach
150452868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1505a6f13a4aSGreg Roach        }
150652868398SGreg Roach
1507dec352c1SGreg Roach        if (str_contains($value, '@')) {
15087a6ee1acSGreg Roach            $value = '';
1509a6f13a4aSGreg Roach        }
1510d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1511a6f13a4aSGreg Roach    }
1512a6f13a4aSGreg Roach
1513a6f13a4aSGreg Roach    /**
1514fab8f067SGreg Roach     * Handle <if>
1515a6f13a4aSGreg Roach     *
151609482a55SGreg Roach     * @param array<string> $attrs
15178ba2e626SGreg Roach     *
15188ba2e626SGreg Roach     * @return void
1519a6f13a4aSGreg Roach     */
1520b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1521c1010edaSGreg Roach    {
1522a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1523a6f13a4aSGreg Roach            $this->process_ifs++;
1524a6f13a4aSGreg Roach
1525a6f13a4aSGreg Roach            return;
1526a6f13a4aSGreg Roach        }
1527a6f13a4aSGreg Roach
1528a6f13a4aSGreg Roach        $condition = $attrs['condition'];
152982759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1530c1010edaSGreg Roach        $condition = str_replace([
1531c1010edaSGreg Roach            ' LT ',
1532c1010edaSGreg Roach            ' GT ',
1533c1010edaSGreg Roach        ], [
1534c1010edaSGreg Roach            '<',
1535c1010edaSGreg Roach            '>',
1536c1010edaSGreg Roach        ], $condition);
15373cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15387a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
153913abd6f3SGreg Roach        $match     = [];
15403cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1541a6f13a4aSGreg Roach        $i         = 0;
1542a6f13a4aSGreg Roach        while ($i < $count) {
1543a6f13a4aSGreg Roach            $id    = $match[$i][1];
1544a6f13a4aSGreg Roach            $value = '""';
1545044416d2SGreg Roach            if ($id === 'ID') {
15467a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1547a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1548a6f13a4aSGreg Roach                }
15497a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1550a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15517a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1552a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15537a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1554a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1555a6f13a4aSGreg Roach            } else {
1556b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1557b2448a1bSGreg Roach                if ($level === 0) {
1558a6f13a4aSGreg Roach                    $level++;
1559a6f13a4aSGreg Roach                }
15603d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1561a6f13a4aSGreg Roach                if (empty($value)) {
1562a6f13a4aSGreg Roach                    $level++;
15633d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1564a6f13a4aSGreg Roach                }
15658d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15665e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1567a6f13a4aSGreg Roach            }
1568a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1569a6f13a4aSGreg Roach            $i++;
1570a6f13a4aSGreg Roach        }
15715809450fSGreg Roach
1572cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1573cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1574c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1575c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1576cb63a60eSGreg Roach
1577cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15785809450fSGreg Roach
1579a6f13a4aSGreg Roach        if (!$ret) {
1580a6f13a4aSGreg Roach            $this->process_ifs++;
1581a6f13a4aSGreg Roach        }
1582a6f13a4aSGreg Roach    }
1583a6f13a4aSGreg Roach
1584a6f13a4aSGreg Roach    /**
1585fab8f067SGreg Roach     * Handle </if>
15868ba2e626SGreg Roach     *
15878ba2e626SGreg Roach     * @return void
1588a6f13a4aSGreg Roach     */
1589b702978eSGreg Roach    protected function ifEndHandler(): void
1590c1010edaSGreg Roach    {
1591a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1592a6f13a4aSGreg Roach            $this->process_ifs--;
1593a6f13a4aSGreg Roach        }
1594a6f13a4aSGreg Roach    }
1595a6f13a4aSGreg Roach
1596a6f13a4aSGreg Roach    /**
1597fab8f067SGreg Roach     * Handle <footnote>
1598a6f13a4aSGreg Roach     * Collect the Footnote links
1599fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1600a6f13a4aSGreg Roach     *
160109482a55SGreg Roach     * @param array<string> $attrs
16028ba2e626SGreg Roach     *
16038ba2e626SGreg Roach     * @return void
1604a6f13a4aSGreg Roach     */
1605b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1606c1010edaSGreg Roach    {
16077a6ee1acSGreg Roach        $id = '';
16087a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1609a6f13a4aSGreg Roach            $id = $match[2];
1610a6f13a4aSGreg Roach        }
16116b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1612a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16139b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1614a6f13a4aSGreg Roach            $this->print_data         = true;
16157a6ee1acSGreg Roach            $style                    = '';
1616a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1617a6f13a4aSGreg Roach                $style = $attrs['style'];
1618a6f13a4aSGreg Roach            }
1619a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1620e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1621a6f13a4aSGreg Roach        } else {
1622a6f13a4aSGreg Roach            $this->print_data       = false;
1623a6f13a4aSGreg Roach            $this->process_footnote = false;
1624a6f13a4aSGreg Roach        }
1625a6f13a4aSGreg Roach    }
1626a6f13a4aSGreg Roach
1627a6f13a4aSGreg Roach    /**
1628fab8f067SGreg Roach     * Handle </footnote>
1629a6f13a4aSGreg Roach     * Print the collected Footnote data
16308ba2e626SGreg Roach     *
16318ba2e626SGreg Roach     * @return void
1632a6f13a4aSGreg Roach     */
1633b702978eSGreg Roach    protected function footnoteEndHandler(): void
1634c1010edaSGreg Roach    {
1635a6f13a4aSGreg Roach        if ($this->process_footnote) {
1636a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1637a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1638a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1639e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1640a6f13a4aSGreg Roach            }
1641a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1642a6f13a4aSGreg Roach        } else {
1643a6f13a4aSGreg Roach            $this->process_footnote = true;
1644a6f13a4aSGreg Roach        }
1645a6f13a4aSGreg Roach    }
1646a6f13a4aSGreg Roach
1647a6f13a4aSGreg Roach    /**
1648fab8f067SGreg Roach     * Handle <footnoteTexts />
16498ba2e626SGreg Roach     *
16508ba2e626SGreg Roach     * @return void
1651a6f13a4aSGreg Roach     */
1652b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1653c1010edaSGreg Roach    {
16547a6ee1acSGreg Roach        $temp = 'footnotetexts';
1655e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1656a6f13a4aSGreg Roach    }
1657a6f13a4aSGreg Roach
1658a6f13a4aSGreg Roach    /**
1659a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16608ba2e626SGreg Roach     *
16618ba2e626SGreg Roach     * @return void
1662a6f13a4aSGreg Roach     */
1663b702978eSGreg Roach    protected function brStartHandler(): void
1664c1010edaSGreg Roach    {
1665a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1666a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1667a6f13a4aSGreg Roach        }
1668a6f13a4aSGreg Roach    }
1669a6f13a4aSGreg Roach
1670a6f13a4aSGreg Roach    /**
1671fab8f067SGreg Roach     * Handle <sp />
1672fab8f067SGreg Roach     * Forced space
16738ba2e626SGreg Roach     *
16748ba2e626SGreg Roach     * @return void
1675a6f13a4aSGreg Roach     */
1676b702978eSGreg Roach    protected function spStartHandler(): void
1677c1010edaSGreg Roach    {
1678a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1679a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1680a6f13a4aSGreg Roach        }
1681a6f13a4aSGreg Roach    }
1682a6f13a4aSGreg Roach
1683a6f13a4aSGreg Roach    /**
1684fab8f067SGreg Roach     * Handle <highlightedImage />
168576692c8bSGreg Roach     *
168609482a55SGreg Roach     * @param array<string> $attrs
16878ba2e626SGreg Roach     *
16888ba2e626SGreg Roach     * @return void
1689a6f13a4aSGreg Roach     */
1690b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1691c1010edaSGreg Roach    {
1692a6f13a4aSGreg Roach        $id = '';
16937a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1694a6f13a4aSGreg Roach            $id = $match[1];
1695a6f13a4aSGreg Roach        }
1696a6f13a4aSGreg Roach
1697c21bdddcSGreg Roach        // Position the top corner of this box on the page
1698c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1699a6f13a4aSGreg Roach
1700c21bdddcSGreg Roach        // Position the left corner of this box on the page
1701c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1702a6f13a4aSGreg Roach
170383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
170483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1705a6f13a4aSGreg Roach
1706a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
170783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1708a6f13a4aSGreg Roach
170983cdc021SGreg Roach        // Width, height (or both).
1710c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1711c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1712a6f13a4aSGreg Roach
17136b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
17144a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
171586a63f51SGreg Roach
17169458f20aSGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists()) {
17179458f20aSGreg Roach            $image      = imagecreatefromstring($media_file->fileContents());
1718b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
171929518ad2SGreg Roach
1720a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17213c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17223c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1723a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17243c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17253c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1726a6f13a4aSGreg Roach            } else {
1727136a8abaSGreg Roach                $width  = (float) $attributes[0];
1728136a8abaSGreg Roach                $height = (float) $attributes[1];
1729a6f13a4aSGreg Roach            }
17309458f20aSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1731e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1732a6f13a4aSGreg Roach        }
1733a6f13a4aSGreg Roach    }
1734a6f13a4aSGreg Roach
1735a6f13a4aSGreg Roach    /**
1736fab8f067SGreg Roach     * Handle <image/>
173776692c8bSGreg Roach     *
173809482a55SGreg Roach     * @param array<string> $attrs
17398ba2e626SGreg Roach     *
17408ba2e626SGreg Roach     * @return void
1741a6f13a4aSGreg Roach     */
1742b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1743c1010edaSGreg Roach    {
174483cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1745c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1746a6f13a4aSGreg Roach
1747a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1748c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1749a6f13a4aSGreg Roach
175083cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
175183cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1752a6f13a4aSGreg Roach
1753a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
175483cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1755a6f13a4aSGreg Roach
175683cdc021SGreg Roach        // Width, height (or both).
1757c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1758c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1759a6f13a4aSGreg Roach
176083cdc021SGreg Roach        $file = $attrs['file'] ?? '';
176183cdc021SGreg Roach
1762044416d2SGreg Roach        if ($file === '@FILE') {
176313abd6f3SGreg Roach            $match = [];
1764a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17656b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17664a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1767cdf416fbSGreg Roach
17689458f20aSGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists()) {
17699458f20aSGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents());
1770b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
177129518ad2SGreg Roach
1772a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17733c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17743c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1775a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17763c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17773c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1778a6f13a4aSGreg Roach                    } else {
1779136a8abaSGreg Roach                        $width  = (float) $attributes[0];
1780136a8abaSGreg Roach                        $height = (float) $attributes[1];
1781a6f13a4aSGreg Roach                    }
17829458f20aSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1783e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1784a6f13a4aSGreg Roach                }
1785a6f13a4aSGreg Roach            }
1786a6f13a4aSGreg Roach        } else {
17877a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1788a6f13a4aSGreg Roach                $size = getimagesize($file);
1789a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1790a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1791a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1792a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1793a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1794a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1795a6f13a4aSGreg Roach                } else {
1796a6f13a4aSGreg Roach                    $width  = $size[0];
1797a6f13a4aSGreg Roach                    $height = $size[1];
1798a6f13a4aSGreg Roach                }
1799e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1800e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1801a6f13a4aSGreg Roach            }
1802a6f13a4aSGreg Roach        }
1803a6f13a4aSGreg Roach    }
1804a6f13a4aSGreg Roach
1805a6f13a4aSGreg Roach    /**
1806fab8f067SGreg Roach     * Handle <line>
1807a6f13a4aSGreg Roach     *
180809482a55SGreg Roach     * @param array<string> $attrs
18098ba2e626SGreg Roach     *
18108ba2e626SGreg Roach     * @return void
1811a6f13a4aSGreg Roach     */
1812b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1813c1010edaSGreg Roach    {
1814a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1815c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1816a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18177a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1818a6f13a4aSGreg Roach                $x1 = 0;
18197a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1820c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1821a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1822c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1823a6f13a4aSGreg Roach            }
1824a6f13a4aSGreg Roach        }
1825a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1826c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1827a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18287a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1829a6f13a4aSGreg Roach                $y1 = 0;
18307a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1831c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1832a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1833c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1834a6f13a4aSGreg Roach            }
1835a6f13a4aSGreg Roach        }
1836a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1837c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1838a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18397a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1840a6f13a4aSGreg Roach                $x2 = 0;
18417a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1842c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1843a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1844c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1845a6f13a4aSGreg Roach            }
1846a6f13a4aSGreg Roach        }
1847a6f13a4aSGreg Roach        // End vertical position
1848c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1849a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18507a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1851a6f13a4aSGreg Roach                $y2 = 0;
18527a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1853c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1854a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1855c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1856a6f13a4aSGreg Roach            }
1857a6f13a4aSGreg Roach        }
1858a6f13a4aSGreg Roach
1859e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1860e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1861a6f13a4aSGreg Roach    }
1862a6f13a4aSGreg Roach
1863a6f13a4aSGreg Roach    /**
1864fab8f067SGreg Roach     * Handle <list>
1865a6f13a4aSGreg Roach     *
186609482a55SGreg Roach     * @param array<string> $attrs
18678ba2e626SGreg Roach     *
18688ba2e626SGreg Roach     * @return void
1869a6f13a4aSGreg Roach     */
1870b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1871c1010edaSGreg Roach    {
1872a6f13a4aSGreg Roach        $this->process_repeats++;
1873a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1874a6f13a4aSGreg Roach            return;
1875a6f13a4aSGreg Roach        }
1876a6f13a4aSGreg Roach
187713abd6f3SGreg Roach        $match = [];
1878a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1879a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1880a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1881d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1882a6f13a4aSGreg Roach                $sortby = trim($sortby);
1883a6f13a4aSGreg Roach            }
1884a6f13a4aSGreg Roach        } else {
18857a6ee1acSGreg Roach            $sortby = 'NAME';
1886a6f13a4aSGreg Roach        }
1887a6f13a4aSGreg Roach
1888e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1889195b5e75SGreg Roach
1890a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1891a6f13a4aSGreg Roach        switch ($listname) {
18927a6ee1acSGreg Roach            case 'pending':
189363b1851dSGreg Roach                $this->list = DB::table('change')
1894195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1895059898c9SGreg Roach                        $query->select([new Expression('MAX(change_id)')])
1896195b5e75SGreg Roach                            ->from('change')
1897195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1898195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18997f5c2944SGreg Roach                            ->groupBy(['xref']);
1900195b5e75SGreg Roach                    })
190163b1851dSGreg Roach                    ->get()
190263b1851dSGreg Roach                    ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom))
190363b1851dSGreg Roach                    ->filter()
190463b1851dSGreg Roach                    ->all();
1905a6f13a4aSGreg Roach                break;
190663b1851dSGreg Roach
1907a6f13a4aSGreg Roach            case 'individual':
19085985adfbSGreg Roach                $query = DB::table('individuals')
19095985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19105985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19115985adfbSGreg Roach                    ->distinct();
19125985adfbSGreg Roach
1913a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1914dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
191582759250SGreg Roach                        $value = $this->substituteVars($value, false);
1916a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1917a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19180b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19195985adfbSGreg Roach                                $join
19205985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19215985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19225985adfbSGreg Roach                            });
19235985adfbSGreg Roach
19245985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19255985adfbSGreg Roach
1926a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19275985adfbSGreg Roach
1928044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19295985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1930a6f13a4aSGreg Roach                            } else {
19315985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1932a6f13a4aSGreg Roach                            }
19335985adfbSGreg Roach
19345985adfbSGreg Roach                            // This filter has been fully processed
19355985adfbSGreg Roach                            unset($attrs[$attr]);
19367ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19370b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19385985adfbSGreg Roach                                $join
19395985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19405985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19415985adfbSGreg Roach                            });
1942a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19437a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
1944c5402244SGreg Roach                            foreach ($names as $name) {
1945b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1946a6f13a4aSGreg Roach                            }
19475985adfbSGreg Roach
19485985adfbSGreg Roach                            // This filter has been fully processed
19495985adfbSGreg Roach                            unset($attrs[$attr]);
1950a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19515985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1952a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19535985adfbSGreg Roach
1954a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19555985adfbSGreg Roach
19565985adfbSGreg Roach                            // This filter has been fully processed
19575985adfbSGreg Roach                            unset($attrs[$attr]);
19582fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19599dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19605985adfbSGreg Roach                            $query
19610b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19625985adfbSGreg Roach                                    $join
19637ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19647ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19655985adfbSGreg Roach                                })
19660b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19677ee4bfadSGreg Roach                                    $join
19687ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19697ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19707ee4bfadSGreg Roach                                })
19719b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19726e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19735985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19747ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19756e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19766e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
197747a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19786e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19796e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1980e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19817ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1982a6f13a4aSGreg Roach                        }
1983a6f13a4aSGreg Roach                    }
1984a6f13a4aSGreg Roach                }
1985a6f13a4aSGreg Roach
198613abd6f3SGreg Roach                $this->list = [];
1987a6f13a4aSGreg Roach
19885985adfbSGreg Roach                foreach ($query->get() as $row) {
19896b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1990a6f13a4aSGreg Roach                }
1991a6f13a4aSGreg Roach                break;
1992a6f13a4aSGreg Roach
1993a6f13a4aSGreg Roach            case 'family':
199430fc2b1eSGreg Roach                $query = DB::table('families')
199530fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
199630fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
199730fc2b1eSGreg Roach                    ->distinct();
199830fc2b1eSGreg Roach
1999a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
2000dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
200182759250SGreg Roach                        $value = $this->substituteVars($value, false);
2002a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2003a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20040b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200530fc2b1eSGreg Roach                                $join
200630fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
200730fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
200830fc2b1eSGreg Roach                            });
200930fc2b1eSGreg Roach
201030fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
201130fc2b1eSGreg Roach
2012a6f13a4aSGreg Roach                            $date = new Date($match[3]);
201330fc2b1eSGreg Roach
2014044416d2SGreg Roach                            if ($match[2] === 'LTE') {
201530fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2016a6f13a4aSGreg Roach                            } else {
201730fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2018a6f13a4aSGreg Roach                            }
201930fc2b1eSGreg Roach
202030fc2b1eSGreg Roach                            // This filter has been fully processed
202130fc2b1eSGreg Roach                            unset($attrs[$attr]);
2022a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
202330fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2024a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
202530fc2b1eSGreg Roach
2026a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
202730fc2b1eSGreg Roach
202830fc2b1eSGreg Roach                            // This filter has been fully processed
202930fc2b1eSGreg Roach                            unset($attrs[$attr]);
203030fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20313b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20320b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
203330fc2b1eSGreg Roach                                    $join
203430fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20353b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
203630fc2b1eSGreg Roach                                            $query
203730fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
203830fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
203930fc2b1eSGreg Roach                                        });
204030fc2b1eSGreg Roach                                });
20415d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20427a6ee1acSGreg Roach                                if ($match[1] != '') {
20437a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
2044c5402244SGreg Roach                                    foreach ($names as $name) {
2045b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20465d0bc43dSGreg Roach                                    }
20475d0bc43dSGreg Roach                                }
2048a6f13a4aSGreg Roach                            }
204930fc2b1eSGreg Roach
205030fc2b1eSGreg Roach                            // This filter has been fully processed
205130fc2b1eSGreg Roach                            unset($attrs[$attr]);
20526e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20539dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
205430fc2b1eSGreg Roach                            $query
20550b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
205630fc2b1eSGreg Roach                                    $join
20576e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20586e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
205930fc2b1eSGreg Roach                                })
20600b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20616e5c0963SGreg Roach                                    $join
20626e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20636e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20646e5c0963SGreg Roach                                })
2065b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20666e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
206730fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20686e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20696e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20706e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20716e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20726e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20736e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2074e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20756e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2076a6f13a4aSGreg Roach                        }
2077a6f13a4aSGreg Roach                    }
2078a6f13a4aSGreg Roach                }
2079a6f13a4aSGreg Roach
208013abd6f3SGreg Roach                $this->list = [];
2081a6f13a4aSGreg Roach
208230fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20836b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2084a6f13a4aSGreg Roach                }
2085a6f13a4aSGreg Roach                break;
2086a6f13a4aSGreg Roach
2087a6f13a4aSGreg Roach            default:
20886ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2089a6f13a4aSGreg Roach        }
2090a6f13a4aSGreg Roach
209113abd6f3SGreg Roach        $filters  = [];
209213abd6f3SGreg Roach        $filters2 = [];
2093a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2094a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2095a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2096a6f13a4aSGreg Roach                    $condition = $value;
2097a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2098a6f13a4aSGreg Roach                        $id    = $match[1];
2099a6f13a4aSGreg Roach                        $value = "''";
2100044416d2SGreg Roach                        if ($id === 'ID') {
21017a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2102a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2103a6f13a4aSGreg Roach                            }
2104044416d2SGreg Roach                        } elseif ($id === 'fact') {
2105a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2106044416d2SGreg Roach                        } elseif ($id === 'desc') {
2107a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2108a6f13a4aSGreg Roach                        } else {
2109a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21107a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2111a6f13a4aSGreg Roach                            }
2112a6f13a4aSGreg Roach                        }
2113a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2114a6f13a4aSGreg Roach                    }
2115a6f13a4aSGreg Roach                    //-- handle regular expressions
2116a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2117a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2118a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2119a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2120a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2121d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2122a6f13a4aSGreg Roach                            $val = trim($val);
2123a6f13a4aSGreg Roach                        }
2124*b6ec1ccfSGreg Roach                        if ($val !== '') {
21257a6ee1acSGreg Roach                            $searchstr = '';
21267a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2127a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2128a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2129a6f13a4aSGreg Roach                                $level = 1;
2130f71a7dedSGreg Roach                                $t = 'XXXX';
2131a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2132a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2133a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2134a6f13a4aSGreg Roach                                    }
2135a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2136044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21377a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2138a6f13a4aSGreg Roach                                    }
21397a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2140a6f13a4aSGreg Roach                                    $level++;
2141a6f13a4aSGreg Roach                                }
2142a6f13a4aSGreg Roach                            } else {
2143044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21447a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2145a6f13a4aSGreg Roach                                }
2146a6f13a4aSGreg Roach                                $t         = $tag;
21477a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2148a6f13a4aSGreg Roach                            }
2149a6f13a4aSGreg Roach                            switch ($expr) {
21507a6ee1acSGreg Roach                                case 'CONTAINS':
2151044416d2SGreg Roach                                    if ($t === 'PLAC') {
2152a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2153a6f13a4aSGreg Roach                                    } else {
2154a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2155a6f13a4aSGreg Roach                                    }
2156a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2157a6f13a4aSGreg Roach                                    break;
2158a6f13a4aSGreg Roach                                default:
2159c1010edaSGreg Roach                                    $filters2[] = [
2160c1010edaSGreg Roach                                        'tag'  => $tag,
2161c1010edaSGreg Roach                                        'expr' => $expr,
2162c1010edaSGreg Roach                                        'val'  => $val,
2163c1010edaSGreg Roach                                    ];
2164a6f13a4aSGreg Roach                                    break;
2165a6f13a4aSGreg Roach                            }
2166a6f13a4aSGreg Roach                        }
2167a6f13a4aSGreg Roach                    }
2168a6f13a4aSGreg Roach                }
2169a6f13a4aSGreg Roach            }
2170a6f13a4aSGreg Roach        }
2171a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2172*b6ec1ccfSGreg Roach        if ($filters !== []) {
2173a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2174a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2175299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2176a6f13a4aSGreg Roach                        unset($this->list[$key]);
2177a6f13a4aSGreg Roach                        break;
2178a6f13a4aSGreg Roach                    }
2179a6f13a4aSGreg Roach                }
2180a6f13a4aSGreg Roach            }
2181a6f13a4aSGreg Roach        }
2182*b6ec1ccfSGreg Roach        if ($filters2 !== []) {
218313abd6f3SGreg Roach            $mylist = [];
2184a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2185c0935879SGreg Roach                $key  = $indi->xref();
2186299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2187a6f13a4aSGreg Roach                $keep = true;
2188a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2189a6f13a4aSGreg Roach                    if ($keep) {
2190a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2191a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2192a6f13a4aSGreg Roach                        $val  = $filter['val'];
2193b2448a1bSGreg Roach                        if ($val === "''") {
21947a6ee1acSGreg Roach                            $val = '';
2195a6f13a4aSGreg Roach                        }
21967a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2197a6f13a4aSGreg Roach                        $t    = end($tags);
21983d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2199a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2200044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
22017a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
22027a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2203a6f13a4aSGreg Roach                            $t    = end($tags);
2204b315f3e1SGreg Roach                            $v    = self::getSubRecord(1, $tag, $grec);
2205a6f13a4aSGreg Roach                        }
2206a6f13a4aSGreg Roach
2207a6f13a4aSGreg Roach                        switch ($expr) {
22087a6ee1acSGreg Roach                            case 'GTE':
2209044416d2SGreg Roach                                if ($t === 'DATE') {
2210a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2211a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2212a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2213a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2214a6f13a4aSGreg Roach                                    $keep = true;
2215a6f13a4aSGreg Roach                                }
2216a6f13a4aSGreg Roach                                break;
22177a6ee1acSGreg Roach                            case 'LTE':
2218044416d2SGreg Roach                                if ($t === 'DATE') {
2219a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2220a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2221a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2222a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2223a6f13a4aSGreg Roach                                    $keep = true;
2224a6f13a4aSGreg Roach                                }
2225a6f13a4aSGreg Roach                                break;
2226a6f13a4aSGreg Roach                            default:
2227a6f13a4aSGreg Roach                                if ($v == $val) {
2228a6f13a4aSGreg Roach                                    $keep = true;
2229a6f13a4aSGreg Roach                                } else {
2230a6f13a4aSGreg Roach                                    $keep = false;
2231a6f13a4aSGreg Roach                                }
2232a6f13a4aSGreg Roach                                break;
2233a6f13a4aSGreg Roach                        }
2234a6f13a4aSGreg Roach                    }
2235a6f13a4aSGreg Roach                }
2236a6f13a4aSGreg Roach                if ($keep) {
2237a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2238a6f13a4aSGreg Roach                }
2239a6f13a4aSGreg Roach            }
2240a6f13a4aSGreg Roach            $this->list = $mylist;
2241a6f13a4aSGreg Roach        }
2242a6f13a4aSGreg Roach
2243a6f13a4aSGreg Roach        switch ($sortby) {
2244a6f13a4aSGreg Roach            case 'NAME':
2245c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2246a6f13a4aSGreg Roach                break;
2247a6f13a4aSGreg Roach            case 'CHAN':
2248c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2249a6f13a4aSGreg Roach                break;
2250a6f13a4aSGreg Roach            case 'BIRT:DATE':
2251c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2252a6f13a4aSGreg Roach                break;
2253a6f13a4aSGreg Roach            case 'DEAT:DATE':
2254c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2255a6f13a4aSGreg Roach                break;
2256a6f13a4aSGreg Roach            case 'MARR:DATE':
2257c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2258a6f13a4aSGreg Roach                break;
2259a6f13a4aSGreg Roach            default:
2260a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2261a6f13a4aSGreg Roach                break;
2262a6f13a4aSGreg Roach        }
2263a6f13a4aSGreg Roach
22649b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2265e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2266a6f13a4aSGreg Roach    }
2267a6f13a4aSGreg Roach
2268a6f13a4aSGreg Roach    /**
2269fab8f067SGreg Roach     * Handle </list>
22708ba2e626SGreg Roach     *
22718ba2e626SGreg Roach     * @return void
2272a6f13a4aSGreg Roach     */
2273b702978eSGreg Roach    protected function listEndHandler(): void
2274c1010edaSGreg Roach    {
2275a6f13a4aSGreg Roach        $this->process_repeats--;
2276a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2277a6f13a4aSGreg Roach            return;
2278a6f13a4aSGreg Roach        }
2279a6f13a4aSGreg Roach
2280a6f13a4aSGreg Roach        // Check if there is any list
2281a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2282a6f13a4aSGreg Roach            $lineoffset = 0;
2283a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2284a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2285a6f13a4aSGreg Roach            }
2286a6f13a4aSGreg Roach            //-- read the xml from the file
2287299d100dSGreg Roach            $lines = file($this->report);
2288dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2289a6f13a4aSGreg Roach                $lineoffset--;
2290a6f13a4aSGreg Roach            }
2291a6f13a4aSGreg Roach            $lineoffset++;
2292a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2293a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2294a6f13a4aSGreg Roach            // List Level counter
2295a6f13a4aSGreg Roach            $count = 1;
2296a6f13a4aSGreg Roach            while (0 < $count) {
2297dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2298a6f13a4aSGreg Roach                    $count++;
2299dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2300a6f13a4aSGreg Roach                    $count--;
2301a6f13a4aSGreg Roach                }
2302a6f13a4aSGreg Roach                if (0 < $count) {
2303a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2304a6f13a4aSGreg Roach                }
2305a6f13a4aSGreg Roach                $line_nr++;
2306a6f13a4aSGreg Roach            }
2307a6f13a4aSGreg Roach            // No need to drag this
2308a6f13a4aSGreg Roach            unset($lines);
23097a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2310a6f13a4aSGreg Roach            // Save original values
23119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2312a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2313a6f13a4aSGreg Roach
2314a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2315a6f13a4aSGreg Roach            $this->list_private = 0;
2316a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2317a6f13a4aSGreg Roach                if ($record->canShow()) {
2318f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2319a6f13a4aSGreg Roach                    //-- start the sax parser
2320a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2321e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2322f315390bSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
23231aa04befSGreg Roach
23241aa04befSGreg Roach                    xml_set_element_handler(
23251aa04befSGreg Roach                        $repeat_parser,
23269d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23271aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23281aa04befSGreg Roach                        },
23299d454b6bSGreg Roach                        function ($parser, string $name): void {
23301aa04befSGreg Roach                            $this->endElement($parser, $name);
23311aa04befSGreg Roach                        }
23321aa04befSGreg Roach                    );
23331aa04befSGreg Roach
23341aa04befSGreg Roach                    xml_set_character_data_handler(
23351aa04befSGreg Roach                        $repeat_parser,
23369d454b6bSGreg Roach                        function ($parser, string $data): void {
23371aa04befSGreg Roach                            $this->characterData($parser, $data);
23381aa04befSGreg Roach                        }
23391aa04befSGreg Roach                    );
23401aa04befSGreg Roach
2341a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23426ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2343a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2344a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2345a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2346a6f13a4aSGreg Roach                        ));
2347a6f13a4aSGreg Roach                    }
2348a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2349a6f13a4aSGreg Roach                } else {
2350a6f13a4aSGreg Roach                    $this->list_private++;
2351a6f13a4aSGreg Roach                }
2352a6f13a4aSGreg Roach            }
235313abd6f3SGreg Roach            $this->list   = [];
2354e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2355a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2356a6f13a4aSGreg Roach        }
235765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2358a6f13a4aSGreg Roach    }
2359a6f13a4aSGreg Roach
2360a6f13a4aSGreg Roach    /**
2361fab8f067SGreg Roach     * Handle <listTotal>
2362a6f13a4aSGreg Roach     * Prints the total number of records in a list
2363fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23648ba2e626SGreg Roach     *
23658ba2e626SGreg Roach     * @return void
2366a6f13a4aSGreg Roach     */
2367b702978eSGreg Roach    protected function listTotalStartHandler(): void
2368c1010edaSGreg Roach    {
2369a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2370589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2371a6f13a4aSGreg Roach        } else {
23727a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2373a6f13a4aSGreg Roach        }
2374a6f13a4aSGreg Roach    }
2375a6f13a4aSGreg Roach
2376a6f13a4aSGreg Roach    /**
2377fab8f067SGreg Roach     * Handle <relatives>
237876692c8bSGreg Roach     *
237909482a55SGreg Roach     * @param array<string> $attrs
23808ba2e626SGreg Roach     *
23818ba2e626SGreg Roach     * @return void
2382a6f13a4aSGreg Roach     */
2383b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2384c1010edaSGreg Roach    {
2385a6f13a4aSGreg Roach        $this->process_repeats++;
2386a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2387a6f13a4aSGreg Roach            return;
2388a6f13a4aSGreg Roach        }
2389a6f13a4aSGreg Roach
2390e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2391e364afe4SGreg Roach
239213abd6f3SGreg Roach        $match = [];
2393a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2394d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2395a6f13a4aSGreg Roach            $sortby = trim($sortby);
2396a6f13a4aSGreg Roach        }
2397a6f13a4aSGreg Roach
2398a6f13a4aSGreg Roach        $maxgen = -1;
2399a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2400c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2401a6f13a4aSGreg Roach        }
2402a6f13a4aSGreg Roach
2403e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2404e364afe4SGreg Roach
2405a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2406d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2407a6f13a4aSGreg Roach            $group = trim($group);
2408a6f13a4aSGreg Roach        }
2409a6f13a4aSGreg Roach
2410e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2411e364afe4SGreg Roach
2412a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2413d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2414a6f13a4aSGreg Roach            $id = trim($id);
2415a6f13a4aSGreg Roach        }
2416a6f13a4aSGreg Roach
241713abd6f3SGreg Roach        $this->list = [];
24186b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2419d965cc1aSGreg Roach        if ($person instanceof Individual) {
2420a6f13a4aSGreg Roach            $this->list[$id] = $person;
2421a6f13a4aSGreg Roach            switch ($group) {
24227a6ee1acSGreg Roach                case 'child-family':
242339ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2424820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2425820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2426a6f13a4aSGreg Roach                        }
2427820b62dfSGreg Roach
2428820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2429c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2430a6f13a4aSGreg Roach                        }
2431a6f13a4aSGreg Roach                    }
2432a6f13a4aSGreg Roach                    break;
24337a6ee1acSGreg Roach                case 'spouse-family':
243439ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2435820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2436820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2437a6f13a4aSGreg Roach                        }
2438820b62dfSGreg Roach
2439820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2440c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2441a6f13a4aSGreg Roach                        }
2442a6f13a4aSGreg Roach                    }
2443a6f13a4aSGreg Roach                    break;
24447a6ee1acSGreg Roach                case 'direct-ancestors':
24453d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2446a6f13a4aSGreg Roach                    break;
24477a6ee1acSGreg Roach                case 'ancestors':
24483d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2449a6f13a4aSGreg Roach                    break;
24507a6ee1acSGreg Roach                case 'descendants':
2451a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24523d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2453a6f13a4aSGreg Roach                    break;
24547a6ee1acSGreg Roach                case 'all':
24553d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24563d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2457a6f13a4aSGreg Roach                    break;
2458a6f13a4aSGreg Roach            }
2459a6f13a4aSGreg Roach        }
2460a6f13a4aSGreg Roach
2461a6f13a4aSGreg Roach        switch ($sortby) {
2462a6f13a4aSGreg Roach            case 'NAME':
2463c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2464a6f13a4aSGreg Roach                break;
2465a6f13a4aSGreg Roach            case 'BIRT:DATE':
2466c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2467a6f13a4aSGreg Roach                break;
2468a6f13a4aSGreg Roach            case 'DEAT:DATE':
2469c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2470a6f13a4aSGreg Roach                break;
2471a6f13a4aSGreg Roach            case 'generation':
247213abd6f3SGreg Roach                $newarray = [];
2473a6f13a4aSGreg Roach                reset($this->list);
2474a6f13a4aSGreg Roach                $genCounter = 1;
2475a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2476a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2477a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2478a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2479f70bcff5SGreg Roach                            $newarray[$key] = (object) ['generation' => $this->generation];
2480a6f13a4aSGreg Roach                        }
2481a6f13a4aSGreg Roach                    }
2482a6f13a4aSGreg Roach                    $genCounter++;
2483a6f13a4aSGreg Roach                }
2484a6f13a4aSGreg Roach                $this->list = $newarray;
2485a6f13a4aSGreg Roach                break;
2486a6f13a4aSGreg Roach            default:
2487a6f13a4aSGreg Roach                // unsorted
2488a6f13a4aSGreg Roach                break;
2489a6f13a4aSGreg Roach        }
24909b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2491e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2492a6f13a4aSGreg Roach    }
2493a6f13a4aSGreg Roach
2494a6f13a4aSGreg Roach    /**
2495fab8f067SGreg Roach     * Handle </relatives>
24968ba2e626SGreg Roach     *
24978ba2e626SGreg Roach     * @return void
2498a6f13a4aSGreg Roach     */
2499b702978eSGreg Roach    protected function relativesEndHandler(): void
2500c1010edaSGreg Roach    {
2501a6f13a4aSGreg Roach        $this->process_repeats--;
2502a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2503a6f13a4aSGreg Roach            return;
2504a6f13a4aSGreg Roach        }
2505a6f13a4aSGreg Roach
2506a6f13a4aSGreg Roach        // Check if there is any relatives
2507a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2508a6f13a4aSGreg Roach            $lineoffset = 0;
2509a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2510a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2511a6f13a4aSGreg Roach            }
2512a6f13a4aSGreg Roach            //-- read the xml from the file
2513299d100dSGreg Roach            $lines = file($this->report);
2514dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2515a6f13a4aSGreg Roach                $lineoffset--;
2516a6f13a4aSGreg Roach            }
2517a6f13a4aSGreg Roach            $lineoffset++;
2518a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2519a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2520a6f13a4aSGreg Roach            // Relatives Level counter
2521a6f13a4aSGreg Roach            $count = 1;
2522a6f13a4aSGreg Roach            while (0 < $count) {
2523dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2524a6f13a4aSGreg Roach                    $count++;
2525dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2526a6f13a4aSGreg Roach                    $count--;
2527a6f13a4aSGreg Roach                }
2528a6f13a4aSGreg Roach                if (0 < $count) {
2529a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2530a6f13a4aSGreg Roach                }
2531a6f13a4aSGreg Roach                $line_nr++;
2532a6f13a4aSGreg Roach            }
2533a6f13a4aSGreg Roach            // No need to drag this
2534a6f13a4aSGreg Roach            unset($lines);
2535a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2536a6f13a4aSGreg Roach            // Save original values
25379b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2538a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2539a6f13a4aSGreg Roach
2540a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2541a6f13a4aSGreg Roach            $this->list_private = 0;
2542b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2543a6f13a4aSGreg Roach                if (isset($value->generation)) {
2544a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2545a6f13a4aSGreg Roach                }
25466b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2547299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2548a6f13a4aSGreg Roach
2549a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2550e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2551f315390bSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
25521aa04befSGreg Roach
25531aa04befSGreg Roach                xml_set_element_handler(
25541aa04befSGreg Roach                    $repeat_parser,
25559d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25561aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25571aa04befSGreg Roach                    },
25589d454b6bSGreg Roach                    function ($parser, string $name): void {
25591aa04befSGreg Roach                        $this->endElement($parser, $name);
25601aa04befSGreg Roach                    }
25611aa04befSGreg Roach                );
25621aa04befSGreg Roach
25631aa04befSGreg Roach                xml_set_character_data_handler(
25641aa04befSGreg Roach                    $repeat_parser,
25659d454b6bSGreg Roach                    function ($parser, string $data): void {
25661aa04befSGreg Roach                        $this->characterData($parser, $data);
25671aa04befSGreg Roach                    }
25681aa04befSGreg Roach                );
2569a6f13a4aSGreg Roach
2570a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25716ccdf4f0SGreg 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)));
2572a6f13a4aSGreg Roach                }
2573a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2574a6f13a4aSGreg Roach            }
2575a6f13a4aSGreg Roach            // Clean up the list array
257613abd6f3SGreg Roach            $this->list   = [];
2577e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2578a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2579a6f13a4aSGreg Roach        }
258065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2581a6f13a4aSGreg Roach    }
2582a6f13a4aSGreg Roach
2583a6f13a4aSGreg Roach    /**
2584fab8f067SGreg Roach     * Handle <generation />
2585a6f13a4aSGreg Roach     * Prints the number of generations
25868ba2e626SGreg Roach     *
25878ba2e626SGreg Roach     * @return void
2588a6f13a4aSGreg Roach     */
2589b702978eSGreg Roach    protected function generationStartHandler(): void
2590c1010edaSGreg Roach    {
2591589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2592a6f13a4aSGreg Roach    }
2593a6f13a4aSGreg Roach
2594a6f13a4aSGreg Roach    /**
2595fab8f067SGreg Roach     * Handle <newPage />
2596a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
25978ba2e626SGreg Roach     *
25988ba2e626SGreg Roach     * @return void
2599a6f13a4aSGreg Roach     */
2600b702978eSGreg Roach    protected function newPageStartHandler(): void
2601c1010edaSGreg Roach    {
26027a6ee1acSGreg Roach        $temp = 'addpage';
2603e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2604a6f13a4aSGreg Roach    }
2605a6f13a4aSGreg Roach
2606a6f13a4aSGreg Roach    /**
2607fab8f067SGreg Roach     * Handle </title>
26088ba2e626SGreg Roach     *
26098ba2e626SGreg Roach     * @return void
2610a6f13a4aSGreg Roach     */
2611b702978eSGreg Roach    protected function titleEndHandler(): void
2612c1010edaSGreg Roach    {
26132836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2614a6f13a4aSGreg Roach    }
2615a6f13a4aSGreg Roach
2616a6f13a4aSGreg Roach    /**
2617fab8f067SGreg Roach     * Handle </description>
26188ba2e626SGreg Roach     *
26198ba2e626SGreg Roach     * @return void
2620a6f13a4aSGreg Roach     */
2621b702978eSGreg Roach    protected function descriptionEndHandler(): void
2622c1010edaSGreg Roach    {
26232836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2624a6f13a4aSGreg Roach    }
2625729ce104SGreg Roach
2626729ce104SGreg Roach    /**
262776692c8bSGreg Roach     * Create a list of all descendants.
262876692c8bSGreg Roach     *
26291c7df322SGreg Roach     * @param array<Individual> $list
2630729ce104SGreg Roach     * @param string            $pid
2631729ce104SGreg Roach     * @param bool              $parents
2632729ce104SGreg Roach     * @param int               $generations
26338ba2e626SGreg Roach     *
26348ba2e626SGreg Roach     * @return void
2635729ce104SGreg Roach     */
26363b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2637c1010edaSGreg Roach    {
26386b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2639729ce104SGreg Roach        if ($person === null) {
2640729ce104SGreg Roach            return;
2641729ce104SGreg Roach        }
2642729ce104SGreg Roach        if (!isset($list[$pid])) {
2643729ce104SGreg Roach            $list[$pid] = $person;
2644729ce104SGreg Roach        }
2645729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2646729ce104SGreg Roach            $list[$pid]->generation = 0;
2647729ce104SGreg Roach        }
264839ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2649729ce104SGreg Roach            if ($parents) {
265039ca88baSGreg Roach                $husband = $family->husband();
265139ca88baSGreg Roach                $wife    = $family->wife();
2652729ce104SGreg Roach                if ($husband) {
2653c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2654729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2655c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2656729ce104SGreg Roach                    } else {
2657c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2658729ce104SGreg Roach                    }
2659729ce104SGreg Roach                }
2660729ce104SGreg Roach                if ($wife) {
2661c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2662729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2663c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2664729ce104SGreg Roach                    } else {
2665c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2666729ce104SGreg Roach                    }
2667729ce104SGreg Roach                }
2668729ce104SGreg Roach            }
2669820b62dfSGreg Roach
267039ca88baSGreg Roach            $children = $family->children();
2671820b62dfSGreg Roach
2672729ce104SGreg Roach            foreach ($children as $child) {
2673729ce104SGreg Roach                if ($child) {
2674c0935879SGreg Roach                    $list[$child->xref()] = $child;
2675820b62dfSGreg Roach
2676729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2677c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2678729ce104SGreg Roach                    } else {
2679c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2680729ce104SGreg Roach                    }
2681729ce104SGreg Roach                }
2682729ce104SGreg Roach            }
2683729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2684729ce104SGreg Roach                foreach ($children as $child) {
2685c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2686729ce104SGreg Roach                }
2687729ce104SGreg Roach            }
2688729ce104SGreg Roach        }
2689729ce104SGreg Roach    }
2690729ce104SGreg Roach
2691729ce104SGreg Roach    /**
269276692c8bSGreg Roach     * Create a list of all ancestors.
269376692c8bSGreg Roach     *
26941c7df322SGreg Roach     * @param array<Individual> $list
2695729ce104SGreg Roach     * @param string            $pid
2696729ce104SGreg Roach     * @param bool              $children
2697729ce104SGreg Roach     * @param int               $generations
26988ba2e626SGreg Roach     *
26998ba2e626SGreg Roach     * @return void
2700729ce104SGreg Roach     */
27013b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2702c1010edaSGreg Roach    {
270313abd6f3SGreg Roach        $genlist                = [$pid];
2704729ce104SGreg Roach        $list[$pid]->generation = 1;
2705729ce104SGreg Roach        while (count($genlist) > 0) {
2706729ce104SGreg Roach            $id = array_shift($genlist);
2707dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2708729ce104SGreg Roach                continue; // id can be something like “empty7”
2709729ce104SGreg Roach            }
27106b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
271139ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
271239ca88baSGreg Roach                $husband = $family->husband();
271339ca88baSGreg Roach                $wife    = $family->wife();
2714729ce104SGreg Roach                if ($husband) {
2715c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2716c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2717729ce104SGreg Roach                }
2718729ce104SGreg Roach                if ($wife) {
2719c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2720c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2721729ce104SGreg Roach                }
2722729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2723729ce104SGreg Roach                    if ($husband) {
2724c0935879SGreg Roach                        $genlist[] = $husband->xref();
2725729ce104SGreg Roach                    }
2726729ce104SGreg Roach                    if ($wife) {
2727c0935879SGreg Roach                        $genlist[] = $wife->xref();
2728729ce104SGreg Roach                    }
2729729ce104SGreg Roach                }
2730729ce104SGreg Roach                if ($children) {
273139ca88baSGreg Roach                    foreach ($family->children() as $child) {
2732c0935879SGreg Roach                        $list[$child->xref()] = $child;
2733e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2734729ce104SGreg Roach                    }
2735729ce104SGreg Roach                }
2736729ce104SGreg Roach            }
2737729ce104SGreg Roach        }
2738729ce104SGreg Roach    }
2739729ce104SGreg Roach
2740729ce104SGreg Roach    /**
2741729ce104SGreg Roach     * get gedcom tag value
2742729ce104SGreg Roach     *
2743729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2744729ce104SGreg 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
2745729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2746729ce104SGreg Roach     *
2747729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2748729ce104SGreg Roach     */
2749b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2750c1010edaSGreg Roach    {
2751b2448a1bSGreg Roach        if ($gedrec === '') {
2752729ce104SGreg Roach            return '';
2753729ce104SGreg Roach        }
2754729ce104SGreg Roach        $tags      = explode(':', $tag);
2755729ce104SGreg Roach        $origlevel = $level;
2756b2448a1bSGreg Roach        if ($level === 0) {
27573c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2758729ce104SGreg Roach        }
2759729ce104SGreg Roach
2760729ce104SGreg Roach        $subrec = $gedrec;
2761f71a7dedSGreg Roach        $t = 'XXXX';
2762729ce104SGreg Roach        foreach ($tags as $t) {
2763729ce104SGreg Roach            $lastsubrec = $subrec;
2764b315f3e1SGreg Roach            $subrec     = self::getSubRecord($level, "$level $t", $subrec);
2765729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2766729ce104SGreg Roach                $level--;
2767b315f3e1SGreg Roach                $subrec = self::getSubRecord($level, "$level $t", $lastsubrec);
2768729ce104SGreg Roach            }
2769729ce104SGreg Roach            if (empty($subrec)) {
2770044416d2SGreg Roach                if ($t === 'TITL') {
2771b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "$level ABBR", $lastsubrec);
2772729ce104SGreg Roach                    if (!empty($subrec)) {
27737a6ee1acSGreg Roach                        $t = 'ABBR';
2774729ce104SGreg Roach                    }
2775729ce104SGreg Roach                }
2776b2448a1bSGreg Roach                if ($subrec === '') {
2777729ce104SGreg Roach                    if ($level > 0) {
2778729ce104SGreg Roach                        $level--;
2779729ce104SGreg Roach                    }
2780b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "@ $t", $gedrec);
2781b2448a1bSGreg Roach                    if ($subrec === '') {
2782729ce104SGreg Roach                        return '';
2783729ce104SGreg Roach                    }
2784729ce104SGreg Roach                }
2785729ce104SGreg Roach            }
2786729ce104SGreg Roach            $level++;
2787729ce104SGreg Roach        }
2788729ce104SGreg Roach        $level--;
2789729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2790f71a7dedSGreg Roach        if ($ct === 0) {
2791729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2792729ce104SGreg Roach        }
2793f71a7dedSGreg Roach        if ($ct === 0) {
2794729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2795729ce104SGreg Roach        }
2796729ce104SGreg Roach        if ($ct > 0) {
2797729ce104SGreg Roach            $value = trim($match[1]);
2798044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
27996b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2800ff166e64SGreg Roach                if ($note instanceof Note) {
2801729ce104SGreg Roach                    $value = $note->getNote();
2802729ce104SGreg Roach                } else {
2803729ce104SGreg Roach                    //-- set the value to the id without the @
2804729ce104SGreg Roach                    $value = $match[1];
2805729ce104SGreg Roach                }
2806729ce104SGreg Roach            }
2807f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
2808b315f3e1SGreg Roach                $value .= self::getCont($level + 1, $subrec);
2809729ce104SGreg Roach            }
2810729ce104SGreg Roach
281172d49ceeSGreg Roach            if ($tag === 'NAME' || $tag === '_MARNM' || $tag === '_AKA') {
281272d49ceeSGreg Roach                return strtr($value, ['/' => '']);
281372d49ceeSGreg Roach            }
281472d49ceeSGreg Roach
2815729ce104SGreg Roach            return $value;
2816729ce104SGreg Roach        }
2817729ce104SGreg Roach
28187a6ee1acSGreg Roach        return '';
2819729ce104SGreg Roach    }
2820d1286247SGreg Roach
2821d1286247SGreg Roach    /**
2822d1286247SGreg Roach     * Replace variable identifiers with their values.
2823d1286247SGreg Roach     *
2824d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
282582759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2826d1286247SGreg Roach     *
2827d1286247SGreg Roach     * @return string
2828d1286247SGreg Roach     */
28298f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2830c1010edaSGreg Roach    {
2831d1286247SGreg Roach        return preg_replace_callback(
2832d1286247SGreg Roach            '/\$(\w+)/',
283318d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28342118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
283582759250SGreg Roach                    if ($quote) {
28362118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2837b2ce94c6SRico Sonntag                    }
2838b2ce94c6SRico Sonntag
28392118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
284082759250SGreg Roach                }
2841b2ce94c6SRico Sonntag
2842d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28433d7a8a4cSGreg Roach
2844d1286247SGreg Roach                return '$' . $matches[1];
2845d1286247SGreg Roach            },
2846d1286247SGreg Roach            $expression
2847d1286247SGreg Roach        );
2848d1286247SGreg Roach    }
2849a6f13a4aSGreg Roach}
2850