xref: /webtrees/app/Report/ReportParserGenerate.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6a6f13a4aSGreg Roach * This program is free software: you can redistribute it and/or modify
7a6f13a4aSGreg Roach * it under the terms of the GNU General Public License as published by
8a6f13a4aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a6f13a4aSGreg Roach * (at your option) any later version.
10a6f13a4aSGreg Roach * This program is distributed in the hope that it will be useful,
11a6f13a4aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a6f13a4aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a6f13a4aSGreg Roach * GNU General Public License for more details.
14a6f13a4aSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a6f13a4aSGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
226ccdf4f0SGreg Roachuse DomainException;
23a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
24a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
2597def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
26a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
279a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
28a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
31d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
32a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
33729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
34a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
35d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
36299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
37195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
38195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
39a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
405985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
419a9e551aSGreg Roachuse Illuminate\Support\Str;
42f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
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;
53b19e047dSGreg Roachuse function call_user_func;
54b6f35a76SGreg Roachuse function count;
55b6f35a76SGreg Roachuse function end;
56b2448a1bSGreg Roachuse function explode;
57b6f35a76SGreg Roachuse function file;
58b6f35a76SGreg Roachuse function file_exists;
59b6f35a76SGreg Roachuse function getimagesize;
6029518ad2SGreg Roachuse function imagecreatefromstring;
6129518ad2SGreg Roachuse function imagesx;
6229518ad2SGreg Roachuse function imagesy;
63b6f35a76SGreg Roachuse function in_array;
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;
74b6f35a76SGreg Roachuse function str_replace;
75dec352c1SGreg Roachuse function str_starts_with;
76b6f35a76SGreg Roachuse function strip_tags;
77b6f35a76SGreg Roachuse function strlen;
78b6f35a76SGreg Roachuse function strtoupper;
79b6f35a76SGreg Roachuse function substr;
80b2448a1bSGreg Roachuse function trim;
81b6f35a76SGreg Roachuse function uasort;
82b6f35a76SGreg Roachuse function xml_error_string;
83b6f35a76SGreg Roachuse function xml_get_current_line_number;
84b6f35a76SGreg Roachuse function xml_get_error_code;
85b6f35a76SGreg Roachuse function xml_parse;
86b6f35a76SGreg Roachuse function xml_parser_create;
87b6f35a76SGreg Roachuse function xml_parser_free;
88b6f35a76SGreg Roachuse function xml_parser_set_option;
89b6f35a76SGreg Roachuse function xml_set_character_data_handler;
90b6f35a76SGreg Roachuse function xml_set_element_handler;
91b6f35a76SGreg Roach
92b6f35a76SGreg Roachuse const PREG_SET_ORDER;
93b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9429518ad2SGreg Roach
95a6f13a4aSGreg Roach/**
96a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
97a6f13a4aSGreg Roach */
98c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
99c1010edaSGreg Roach{
100a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
101a6f13a4aSGreg Roach    private $process_footnote = true;
102a6f13a4aSGreg Roach
103a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
104a6f13a4aSGreg Roach    private $print_data = false;
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
10713abd6f3SGreg Roach    private $print_data_stack = [];
108a6f13a4aSGreg Roach
10976692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
110a6f13a4aSGreg Roach    private $process_gedcoms = 0;
111a6f13a4aSGreg Roach
11276692c8bSGreg Roach    /** @var int Are we processing conditionals */
113a6f13a4aSGreg Roach    private $process_ifs = 0;
114a6f13a4aSGreg Roach
11576692c8bSGreg Roach    /** @var int Are we processing repeats */
116a6f13a4aSGreg Roach    private $process_repeats = 0;
117a6f13a4aSGreg Roach
118a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
119a6f13a4aSGreg Roach    private $repeat_bytes = 0;
120a6f13a4aSGreg Roach
12109482a55SGreg Roach    /** @var array<string> Repeated data when iterating over loops */
12209482a55SGreg Roach    private array $repeats = [];
123a6f13a4aSGreg Roach
12409482a55SGreg Roach    /** @var array<int,array<int,array<string>|int>> Nested repeating data */
12509482a55SGreg Roach    private array $repeats_stack = [];
126a6f13a4aSGreg Roach
12709482a55SGreg Roach    /** @var array<AbstractRenderer> Nested repeating data */
12809482a55SGreg Roach    private array $wt_report_stack = [];
129e8e7866bSGreg Roach
1304e69366aSGreg Roach    /** @var XMLParser (resource before PHP 8.0) Nested repeating data */
131e8e7866bSGreg Roach    private $parser;
132e8e7866bSGreg Roach
1334e69366aSGreg Roach    /** @var XMLParser[] (resource[] before PHP 8.0) Nested repeating data */
1344e69366aSGreg Roach    private array $parser_stack = [];
135e8e7866bSGreg Roach
136a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
137a6f13a4aSGreg Roach    private $gedrec = '';
138a6f13a4aSGreg Roach
13909482a55SGreg Roach    /** @var array<string> Nested GEDCOM records */
1404e69366aSGreg Roach    private array $gedrec_stack = [];
141a6f13a4aSGreg Roach
142a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
143a6f13a4aSGreg Roach    private $current_element;
144a6f13a4aSGreg Roach
145a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
146a6f13a4aSGreg Roach    private $footnote_element;
147a6f13a4aSGreg Roach
148a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
149a6f13a4aSGreg Roach    private $fact = '';
150a6f13a4aSGreg Roach
151a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
152a6f13a4aSGreg Roach    private $desc = '';
153a6f13a4aSGreg Roach
154a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
155a6f13a4aSGreg Roach    private $type = '';
156a6f13a4aSGreg Roach
157a6f13a4aSGreg Roach    /** @var int The current generational level */
158a6f13a4aSGreg Roach    private $generation = 1;
159a6f13a4aSGreg Roach
16076d39c55SGreg Roach    /** @var array<static|GedcomRecord> Source data for processing lists */
1614e69366aSGreg Roach    private array $list = [];
162a6f13a4aSGreg Roach
163a6f13a4aSGreg Roach    /** @var int Number of items in lists */
164a6f13a4aSGreg Roach    private $list_total = 0;
165a6f13a4aSGreg Roach
166a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
167a6f13a4aSGreg Roach    private $list_private = 0;
168a6f13a4aSGreg Roach
169299d100dSGreg Roach    /** @var string The filename of the XML report */
170299d100dSGreg Roach    protected $report;
171299d100dSGreg Roach
172b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
173e8e7866bSGreg Roach    private $report_root;
174e8e7866bSGreg Roach
175b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
176e8e7866bSGreg Roach    private $wt_report;
177e8e7866bSGreg Roach
17809482a55SGreg Roach    /** @var array<array<string>> Variables defined in the report at run-time */
1794e69366aSGreg Roach    private array $vars;
180d1286247SGreg Roach
1814e69366aSGreg Roach    private Tree $tree;
182299d100dSGreg Roach
1834e69366aSGreg Roach    private FilesystemOperator $data_filesystem;
184a04bb9a2SGreg Roach
18576692c8bSGreg Roach    /**
18676692c8bSGreg Roach     * Create a parser for a report
18776692c8bSGreg Roach     *
18876692c8bSGreg Roach     * @param string               $report The XML filename
189b6f35a76SGreg Roach     * @param AbstractRenderer     $report_root
19009482a55SGreg Roach     * @param array<array<string>> $vars
191299d100dSGreg Roach     * @param Tree                 $tree
192f7cf8a15SGreg Roach     * @param FilesystemOperator   $data_filesystem
19376692c8bSGreg Roach     */
194a04bb9a2SGreg Roach    public function __construct(
195a04bb9a2SGreg Roach        string $report,
196b6f35a76SGreg Roach        AbstractRenderer $report_root,
197a04bb9a2SGreg Roach        array $vars,
198a04bb9a2SGreg Roach        Tree $tree,
199f7cf8a15SGreg Roach        FilesystemOperator $data_filesystem
200a04bb9a2SGreg Roach    ) {
201299d100dSGreg Roach        $this->report          = $report;
202e8e7866bSGreg Roach        $this->report_root     = $report_root;
203e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20459f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
205d1286247SGreg Roach        $this->vars            = $vars;
206299d100dSGreg Roach        $this->tree            = $tree;
207a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
208299d100dSGreg Roach
20976f666f4SGreg Roach        parent::__construct($report);
210a6f13a4aSGreg Roach    }
211a6f13a4aSGreg Roach
212a6f13a4aSGreg Roach    /**
213b315f3e1SGreg Roach     * get a gedcom subrecord
214b315f3e1SGreg Roach     *
215b315f3e1SGreg Roach     * searches a gedcom record and returns a subrecord of it. A subrecord is defined starting at a
216b315f3e1SGreg Roach     * line with level N and all subsequent lines greater than N until the next N level is reached.
217b315f3e1SGreg Roach     * For example, the following is a BIRT subrecord:
218b315f3e1SGreg Roach     * <code>1 BIRT
219b315f3e1SGreg Roach     * 2 DATE 1 JAN 1900
220b315f3e1SGreg Roach     * 2 PLAC Phoenix, Maricopa, Arizona</code>
221b315f3e1SGreg Roach     * The following example is the DATE subrecord of the above BIRT subrecord:
222b315f3e1SGreg Roach     * <code>2 DATE 1 JAN 1900</code>
223b315f3e1SGreg Roach     *
224b315f3e1SGreg Roach     * @param int    $level   the N level of the subrecord to get
225b315f3e1SGreg Roach     * @param string $tag     a gedcom tag or string to search for in the record (ie 1 BIRT or 2 DATE)
226b315f3e1SGreg Roach     * @param string $gedrec  the parent gedcom record to search in
227b315f3e1SGreg Roach     * @param int    $num     this allows you to specify which matching <var>$tag</var> to get. Oftentimes a
228b315f3e1SGreg Roach     *                        gedcom record will have more that 1 of the same type of subrecord. An individual may have
229b315f3e1SGreg Roach     *                        multiple events for example. Passing $num=1 would get the first 1. Passing $num=2 would get the
230b315f3e1SGreg Roach     *                        second one, etc.
231b315f3e1SGreg Roach     *
232b315f3e1SGreg Roach     * @return string the subrecord that was found or an empty string "" if not found.
233b315f3e1SGreg Roach     */
234b315f3e1SGreg Roach    public static function getSubRecord(int $level, string $tag, string $gedrec, int $num = 1): string
235b315f3e1SGreg Roach    {
236b315f3e1SGreg Roach        if ($gedrec === '') {
237b315f3e1SGreg Roach            return '';
238b315f3e1SGreg Roach        }
239b315f3e1SGreg Roach        // -- adding \n before and after gedrec
240b315f3e1SGreg Roach        $gedrec       = "\n" . $gedrec . "\n";
241b315f3e1SGreg Roach        $tag          = trim($tag);
242b315f3e1SGreg Roach        $searchTarget = "~[\n]" . $tag . "[\s]~";
243b315f3e1SGreg Roach        $ct           = preg_match_all($searchTarget, $gedrec, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
244b315f3e1SGreg Roach        if ($ct === 0) {
245b315f3e1SGreg Roach            return '';
246b315f3e1SGreg Roach        }
247b315f3e1SGreg Roach        if ($ct < $num) {
248b315f3e1SGreg Roach            return '';
249b315f3e1SGreg Roach        }
250b315f3e1SGreg Roach        $pos1 = $match[$num - 1][0][1];
251b315f3e1SGreg Roach        $pos2 = strpos($gedrec, "\n$level", $pos1 + 1);
252b315f3e1SGreg Roach        if (!$pos2) {
253b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\n1", $pos1 + 1);
254b315f3e1SGreg Roach        }
255b315f3e1SGreg Roach        if (!$pos2) {
256b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\nWT_", $pos1 + 1); // WT_SPOUSE, WT_FAMILY_ID ...
257b315f3e1SGreg Roach        }
258b315f3e1SGreg Roach        if (!$pos2) {
259b315f3e1SGreg Roach            return ltrim(substr($gedrec, $pos1));
260b315f3e1SGreg Roach        }
261b315f3e1SGreg Roach        $subrec = substr($gedrec, $pos1, $pos2 - $pos1);
262b315f3e1SGreg Roach
263b315f3e1SGreg Roach        return ltrim($subrec);
264b315f3e1SGreg Roach    }
265b315f3e1SGreg Roach
266b315f3e1SGreg Roach    /**
267b315f3e1SGreg Roach     * get CONT lines
268b315f3e1SGreg Roach     *
269b315f3e1SGreg Roach     * get the N+1 CONT or CONC lines of a gedcom subrecord
270b315f3e1SGreg Roach     *
271b315f3e1SGreg Roach     * @param int    $nlevel the level of the CONT lines to get
272b315f3e1SGreg Roach     * @param string $nrec   the gedcom subrecord to search in
273b315f3e1SGreg Roach     *
274b315f3e1SGreg Roach     * @return string a string with all CONT lines merged
275b315f3e1SGreg Roach     */
276b315f3e1SGreg Roach    public static function getCont(int $nlevel, string $nrec): string
277b315f3e1SGreg Roach    {
278b315f3e1SGreg Roach        $text = '';
279b315f3e1SGreg Roach
280b315f3e1SGreg Roach        $subrecords = explode("\n", $nrec);
281b315f3e1SGreg Roach        foreach ($subrecords as $thisSubrecord) {
282b315f3e1SGreg Roach            if (substr($thisSubrecord, 0, 2) !== $nlevel . ' ') {
283b315f3e1SGreg Roach                continue;
284b315f3e1SGreg Roach            }
285b315f3e1SGreg Roach            $subrecordType = substr($thisSubrecord, 2, 4);
286b315f3e1SGreg Roach            if ($subrecordType === 'CONT') {
287b315f3e1SGreg Roach                $text .= "\n" . substr($thisSubrecord, 7);
288b315f3e1SGreg Roach            }
289b315f3e1SGreg Roach        }
290b315f3e1SGreg Roach
291b315f3e1SGreg Roach        return $text;
292b315f3e1SGreg Roach    }
293b315f3e1SGreg Roach
294b315f3e1SGreg Roach    /**
295a6f13a4aSGreg Roach     * XML start element handler
296a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
297a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
298a6f13a4aSGreg Roach     *
299a6f13a4aSGreg Roach     * @param resource      $parser the resource handler for the XML parser
300a6f13a4aSGreg Roach     * @param string        $name   the name of the XML element parsed
30109482a55SGreg Roach     * @param array<string> $attrs  an array of key value pairs for the attributes
30218d7a90dSGreg Roach     *
30318d7a90dSGreg Roach     * @return void
304a6f13a4aSGreg Roach     */
305af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
306c1010edaSGreg Roach    {
30713abd6f3SGreg Roach        $newattrs = [];
308a6f13a4aSGreg Roach
309a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
310a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
311e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
312d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
313a6f13a4aSGreg Roach                }
314a6f13a4aSGreg Roach            }
315a6f13a4aSGreg Roach            $newattrs[$key] = $value;
316a6f13a4aSGreg Roach        }
317a6f13a4aSGreg Roach        $attrs = $newattrs;
3187a6ee1acSGreg 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')) {
319a0e2939aSGreg Roach            $method = $name . 'StartHandler';
320208e9f76SGreg Roach
321a0e2939aSGreg Roach            if (method_exists($this, $method)) {
322b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
323a6f13a4aSGreg Roach            }
324a6f13a4aSGreg Roach        }
325a6f13a4aSGreg Roach    }
326a6f13a4aSGreg Roach
327a6f13a4aSGreg Roach    /**
328a6f13a4aSGreg Roach     * XML end element handler
329a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
330a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
331a6f13a4aSGreg Roach     *
332a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
333a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
33418d7a90dSGreg Roach     *
33518d7a90dSGreg Roach     * @return void
336a6f13a4aSGreg Roach     */
337af14d238SGreg Roach    protected function endElement($parser, string $name): void
338c1010edaSGreg Roach    {
3397a6ee1acSGreg 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')) {
340a0e2939aSGreg Roach            $method = $name . 'EndHandler';
341a0e2939aSGreg Roach
342a0e2939aSGreg Roach            if (method_exists($this, $method)) {
343b19e047dSGreg Roach                call_user_func([$this, $method]);
344a6f13a4aSGreg Roach            }
345a6f13a4aSGreg Roach        }
346a6f13a4aSGreg Roach    }
347a6f13a4aSGreg Roach
348a6f13a4aSGreg Roach    /**
349a6f13a4aSGreg Roach     * XML character data handler
350a6f13a4aSGreg Roach     *
351a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
352a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
35318d7a90dSGreg Roach     *
35418d7a90dSGreg Roach     * @return void
355a6f13a4aSGreg Roach     */
35624f2a3afSGreg Roach    protected function characterData($parser, string $data): void
357c1010edaSGreg Roach    {
358e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
359a6f13a4aSGreg Roach            $this->current_element->addText($data);
360a6f13a4aSGreg Roach        }
361a6f13a4aSGreg Roach    }
362a6f13a4aSGreg Roach
363a6f13a4aSGreg Roach    /**
364fab8f067SGreg Roach     * Handle <style>
365a6f13a4aSGreg Roach     *
36609482a55SGreg Roach     * @param array<string> $attrs
3678ba2e626SGreg Roach     *
3688ba2e626SGreg Roach     * @return void
369a6f13a4aSGreg Roach     */
370b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
371c1010edaSGreg Roach    {
372a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
3736ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
374a6f13a4aSGreg Roach        }
375a6f13a4aSGreg Roach
376a6f13a4aSGreg Roach        // array Style that will be passed on
37713abd6f3SGreg Roach        $s = [];
378a6f13a4aSGreg Roach
379a6f13a4aSGreg Roach        // string Name af the style
380a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
381a6f13a4aSGreg Roach
382a6f13a4aSGreg Roach        // string Name of the DEFAULT font
383208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
384a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
385a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
386a6f13a4aSGreg Roach        }
387a6f13a4aSGreg Roach
388a6f13a4aSGreg Roach        // int The size of the font in points
389208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
390a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
39159597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
39259597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
39359597b37SGreg Roach        }
394a6f13a4aSGreg Roach
395a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3967a6ee1acSGreg Roach        $s['style'] = '';
397a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
398a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
399a6f13a4aSGreg Roach        }
400a6f13a4aSGreg Roach
401e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
402a6f13a4aSGreg Roach    }
403a6f13a4aSGreg Roach
404a6f13a4aSGreg Roach    /**
405fab8f067SGreg Roach     * Handle <doc>
406a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
407a6f13a4aSGreg Roach     *
40809482a55SGreg Roach     * @param array<string> $attrs
4098ba2e626SGreg Roach     *
4108ba2e626SGreg Roach     * @return void
411a6f13a4aSGreg Roach     */
412b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
413c1010edaSGreg Roach    {
414e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
415a6f13a4aSGreg Roach
416a6f13a4aSGreg Roach        // Custom page width
417a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
418208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
419a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
420a6f13a4aSGreg Roach        // Custom Page height
421a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
422208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
423a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
424a6f13a4aSGreg Roach
425a6f13a4aSGreg Roach        // Left Margin
426a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
4277a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
428208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
429a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
430208e9f76SGreg Roach                $this->wt_report->left_margin = (int) $attrs['leftmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
431a6f13a4aSGreg Roach            }
432a6f13a4aSGreg Roach        }
433a6f13a4aSGreg Roach        // Right Margin
434a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
4357a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
436208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
437a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
438208e9f76SGreg Roach                $this->wt_report->right_margin = (int) $attrs['rightmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
439a6f13a4aSGreg Roach            }
440a6f13a4aSGreg Roach        }
441a6f13a4aSGreg Roach        // Top Margin
442a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
4437a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
444208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
445a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
446208e9f76SGreg Roach                $this->wt_report->top_margin = (int) $attrs['topmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
447a6f13a4aSGreg Roach            }
448a6f13a4aSGreg Roach        }
449a6f13a4aSGreg Roach        // Bottom Margin
450a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
4517a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
452208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
453a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
454208e9f76SGreg Roach                $this->wt_report->bottom_margin = (int) $attrs['bottommargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
455a6f13a4aSGreg Roach            }
456a6f13a4aSGreg Roach        }
457a6f13a4aSGreg Roach        // Header Margin
458a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
4597a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
460208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
461a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
462208e9f76SGreg Roach                $this->wt_report->header_margin = (int) $attrs['headermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
463a6f13a4aSGreg Roach            }
464a6f13a4aSGreg Roach        }
465a6f13a4aSGreg Roach        // Footer Margin
466a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
4677a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
468208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
469a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
470208e9f76SGreg Roach                $this->wt_report->footer_margin = (int) $attrs['footermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
471a6f13a4aSGreg Roach            }
472a6f13a4aSGreg Roach        }
473a6f13a4aSGreg Roach
474a6f13a4aSGreg Roach        // Page Orientation
475a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
476044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4777a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
478044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4797a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
480a6f13a4aSGreg Roach            }
481a6f13a4aSGreg Roach        }
482a6f13a4aSGreg Roach        // Page Size
483a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
484208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
485a6f13a4aSGreg Roach        }
486a6f13a4aSGreg Roach
487a6f13a4aSGreg Roach        // Show Generated By...
488a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4897a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
490208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4917a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
492208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
493a6f13a4aSGreg Roach            }
494a6f13a4aSGreg Roach        }
495a6f13a4aSGreg Roach
496e8e7866bSGreg Roach        $this->wt_report->setup();
497a6f13a4aSGreg Roach    }
498a6f13a4aSGreg Roach
499a6f13a4aSGreg Roach    /**
500fab8f067SGreg Roach     * Handle </doc>
5018ba2e626SGreg Roach     *
5028ba2e626SGreg Roach     * @return void
503a6f13a4aSGreg Roach     */
504b702978eSGreg Roach    protected function docEndHandler(): void
505c1010edaSGreg Roach    {
506e8e7866bSGreg Roach        $this->wt_report->run();
507a6f13a4aSGreg Roach    }
508a6f13a4aSGreg Roach
509a6f13a4aSGreg Roach    /**
510fab8f067SGreg Roach     * Handle <header>
5118ba2e626SGreg Roach     *
5128ba2e626SGreg Roach     * @return void
513a6f13a4aSGreg Roach     */
514b702978eSGreg Roach    protected function headerStartHandler(): void
515c1010edaSGreg Roach    {
516a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
517e8e7866bSGreg Roach        $this->wt_report->clearHeader();
5187a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
519a6f13a4aSGreg Roach    }
520a6f13a4aSGreg Roach
521a6f13a4aSGreg Roach    /**
522fab8f067SGreg Roach     * Handle <body>
5238ba2e626SGreg Roach     *
5248ba2e626SGreg Roach     * @return void
525a6f13a4aSGreg Roach     */
526b702978eSGreg Roach    protected function bodyStartHandler(): void
527c1010edaSGreg Roach    {
5287a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
529a6f13a4aSGreg Roach    }
530a6f13a4aSGreg Roach
531a6f13a4aSGreg Roach    /**
532fab8f067SGreg Roach     * Handle <footer>
5338ba2e626SGreg Roach     *
5348ba2e626SGreg Roach     * @return void
535a6f13a4aSGreg Roach     */
536b702978eSGreg Roach    protected function footerStartHandler(): void
537c1010edaSGreg Roach    {
5387a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
539a6f13a4aSGreg Roach    }
540a6f13a4aSGreg Roach
541a6f13a4aSGreg Roach    /**
542fab8f067SGreg Roach     * Handle <cell>
543a6f13a4aSGreg Roach     *
54477bab461SGreg Roach     * @param array<string,string> $attrs
5458ba2e626SGreg Roach     *
5468ba2e626SGreg Roach     * @return void
547a6f13a4aSGreg Roach     */
548b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
549c1010edaSGreg Roach    {
550a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
55177bab461SGreg Roach        $align = $attrs['align'] ?? '';
552a6f13a4aSGreg Roach        // RTL supported left/right alignment
553044416d2SGreg Roach        if ($align === 'rightrtl') {
554e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5557a6ee1acSGreg Roach                $align = 'left';
556a6f13a4aSGreg Roach            } else {
5577a6ee1acSGreg Roach                $align = 'right';
558a6f13a4aSGreg Roach            }
559044416d2SGreg Roach        } elseif ($align === 'leftrtl') {
560e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5617a6ee1acSGreg Roach                $align = 'right';
562a6f13a4aSGreg Roach            } else {
5637a6ee1acSGreg Roach                $align = 'left';
564a6f13a4aSGreg Roach            }
565a6f13a4aSGreg Roach        }
566a6f13a4aSGreg Roach
56777bab461SGreg Roach        // The color to fill the background of this cell
56877bab461SGreg Roach        $bgcolor = $attrs['bgcolor'] ?? '';
569a6f13a4aSGreg Roach
57077bab461SGreg Roach        // Whether the background should be painted
57177bab461SGreg Roach        $fill = (int) ($attrs['fill'] ?? '0');
572a6f13a4aSGreg Roach
57377bab461SGreg Roach        // If true reset the last cell height
57477bab461SGreg Roach        $reseth = (bool) ($attrs['reseth'] ?? '1');
575a6f13a4aSGreg Roach
57677bab461SGreg Roach        // Whether a border should be printed around this box
57777bab461SGreg Roach        $border = $attrs['border'] ?? '';
57877bab461SGreg Roach
579a6f13a4aSGreg Roach        // string Border color in HTML code
58077bab461SGreg Roach        $bocolor = $attrs['bocolor'] ?? '';
581a6f13a4aSGreg Roach
58277bab461SGreg Roach        // Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
58377bab461SGreg Roach        $height = (int) ($attrs['height'] ?? '0');
58477bab461SGreg Roach
585a6f13a4aSGreg 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.
58677bab461SGreg Roach        $width = (int) ($attrs['width'] ?? '0');
587a6f13a4aSGreg Roach
58877bab461SGreg Roach        // Stretch character mode
58977bab461SGreg Roach        $stretch = (int) ($attrs['stretch'] ?? '0');
590a6f13a4aSGreg Roach
591a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
592c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
593a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5947a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
595c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
596a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
597a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5987a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
599a6f13a4aSGreg Roach                $left = 0;
600a6f13a4aSGreg Roach            }
601a6f13a4aSGreg Roach        }
602a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
603c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
604a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
6057a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
606c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
607a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
608a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
6097a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
610a6f13a4aSGreg Roach                $top = 0;
611a6f13a4aSGreg Roach            }
612a6f13a4aSGreg Roach        }
613a6f13a4aSGreg Roach
61477bab461SGreg Roach        // The name of the Style that should be used to render the text.
61577bab461SGreg Roach        $style = $attrs['style'] ?? '';
616a6f13a4aSGreg Roach
617a6f13a4aSGreg Roach        // string Text color in html code
61877bab461SGreg Roach        $tcolor = $attrs['tcolor'] ?? '';
619a6f13a4aSGreg Roach
620a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
621a6f13a4aSGreg Roach        $ln = 0;
622a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
623a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
624a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
6257a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
626a6f13a4aSGreg Roach                $ln = 0;
627a6f13a4aSGreg Roach            }
628a6f13a4aSGreg Roach        }
629a6f13a4aSGreg Roach
630044416d2SGreg Roach        if ($align === 'left') {
6317a6ee1acSGreg Roach            $align = 'L';
632044416d2SGreg Roach        } elseif ($align === 'right') {
6337a6ee1acSGreg Roach            $align = 'R';
634044416d2SGreg Roach        } elseif ($align === 'center') {
6357a6ee1acSGreg Roach            $align = 'C';
636044416d2SGreg Roach        } elseif ($align === 'justify') {
6377a6ee1acSGreg Roach            $align = 'J';
638a6f13a4aSGreg Roach        }
639a6f13a4aSGreg Roach
6409b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
641a6f13a4aSGreg Roach        $this->print_data         = true;
642a6f13a4aSGreg Roach
643e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
64424f2a3afSGreg Roach            (int) $width,
64524f2a3afSGreg Roach            (int) $height,
646a6f13a4aSGreg Roach            $border,
647a6f13a4aSGreg Roach            $align,
648a6f13a4aSGreg Roach            $bgcolor,
649a6f13a4aSGreg Roach            $style,
650a6f13a4aSGreg Roach            $ln,
651a6f13a4aSGreg Roach            $top,
652a6f13a4aSGreg Roach            $left,
653a6f13a4aSGreg Roach            $fill,
654a6f13a4aSGreg Roach            $stretch,
655a6f13a4aSGreg Roach            $bocolor,
656a6f13a4aSGreg Roach            $tcolor,
657a6f13a4aSGreg Roach            $reseth
658a6f13a4aSGreg Roach        );
659a6f13a4aSGreg Roach    }
660a6f13a4aSGreg Roach
661a6f13a4aSGreg Roach    /**
662fab8f067SGreg Roach     * Handle </cell>
6638ba2e626SGreg Roach     *
6648ba2e626SGreg Roach     * @return void
665a6f13a4aSGreg Roach     */
666b702978eSGreg Roach    protected function cellEndHandler(): void
667c1010edaSGreg Roach    {
668a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
669e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
670a6f13a4aSGreg Roach    }
671a6f13a4aSGreg Roach
672a6f13a4aSGreg Roach    /**
673fab8f067SGreg Roach     * Handle <now />
6748ba2e626SGreg Roach     *
6758ba2e626SGreg Roach     * @return void
676a6f13a4aSGreg Roach     */
677b702978eSGreg Roach    protected function nowStartHandler(): void
678c1010edaSGreg Roach    {
679d97083feSGreg Roach        $this->current_element->addText(Registry::timestampFactory()->now()->isoFormat('LLLL'));
680a6f13a4aSGreg Roach    }
681a6f13a4aSGreg Roach
682a6f13a4aSGreg Roach    /**
683fab8f067SGreg Roach     * Handle <pageNum />
6848ba2e626SGreg Roach     *
6858ba2e626SGreg Roach     * @return void
686a6f13a4aSGreg Roach     */
687b702978eSGreg Roach    protected function pageNumStartHandler(): void
688c1010edaSGreg Roach    {
6897a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
690a6f13a4aSGreg Roach    }
691a6f13a4aSGreg Roach
692a6f13a4aSGreg Roach    /**
693fab8f067SGreg Roach     * Handle <totalPages />
6948ba2e626SGreg Roach     *
6958ba2e626SGreg Roach     * @return void
696a6f13a4aSGreg Roach     */
697b702978eSGreg Roach    protected function totalPagesStartHandler(): void
698c1010edaSGreg Roach    {
6997a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
700a6f13a4aSGreg Roach    }
701a6f13a4aSGreg Roach
702a6f13a4aSGreg Roach    /**
703a6f13a4aSGreg Roach     * Called at the start of an element.
704a6f13a4aSGreg Roach     *
70509482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
7068ba2e626SGreg Roach     *
7078ba2e626SGreg Roach     * @return void
708a6f13a4aSGreg Roach     */
709b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
710c1010edaSGreg Roach    {
711a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
712a6f13a4aSGreg Roach            $this->process_gedcoms++;
713a6f13a4aSGreg Roach
714a6f13a4aSGreg Roach            return;
715a6f13a4aSGreg Roach        }
716a6f13a4aSGreg Roach
717a6f13a4aSGreg Roach        $tag       = $attrs['id'];
7187a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
7197a6ee1acSGreg Roach        $tags      = explode(':', $tag);
720a6f13a4aSGreg Roach        $newgedrec = '';
721a6f13a4aSGreg Roach        if (count($tags) < 2) {
7226b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
723299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
724a6f13a4aSGreg Roach        }
725a6f13a4aSGreg Roach        if (empty($newgedrec)) {
726a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
727a6f13a4aSGreg Roach            $newgedrec = '';
728a6f13a4aSGreg Roach            foreach ($tags as $tag) {
7297a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
730d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
731d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
732a6f13a4aSGreg Roach                    } else {
7336b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
734299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
735a6f13a4aSGreg Roach                    }
736a6f13a4aSGreg Roach                } else {
7377a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
73813abd6f3SGreg Roach                        $gmatch = [];
739a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7406b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
741299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
742a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
743a6f13a4aSGreg Roach                        } else {
744a6f13a4aSGreg Roach                            $newgedrec = '';
745a6f13a4aSGreg Roach                            break;
746a6f13a4aSGreg Roach                        }
747a6f13a4aSGreg Roach                    } else {
748b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
749b315f3e1SGreg Roach                        $newgedrec = self::getSubRecord($level, "$level $tag", $tgedrec);
750a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
751a6f13a4aSGreg Roach                    }
752a6f13a4aSGreg Roach                }
753a6f13a4aSGreg Roach            }
754a6f13a4aSGreg Roach        }
755a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7569b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
757a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
758a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
759a6f13a4aSGreg Roach                $this->fact = $match[2];
760a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
761a6f13a4aSGreg Roach            }
762a6f13a4aSGreg Roach        } else {
763a6f13a4aSGreg Roach            $this->process_gedcoms++;
764a6f13a4aSGreg Roach        }
765a6f13a4aSGreg Roach    }
766a6f13a4aSGreg Roach
767a6f13a4aSGreg Roach    /**
768a6f13a4aSGreg Roach     * Called at the end of an element.
7698ba2e626SGreg Roach     *
7708ba2e626SGreg Roach     * @return void
771a6f13a4aSGreg Roach     */
772b702978eSGreg Roach    protected function gedcomEndHandler(): void
773c1010edaSGreg Roach    {
774a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
775a6f13a4aSGreg Roach            $this->process_gedcoms--;
776a6f13a4aSGreg Roach        } else {
77765e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
778a6f13a4aSGreg Roach        }
779a6f13a4aSGreg Roach    }
780a6f13a4aSGreg Roach
781a6f13a4aSGreg Roach    /**
782fab8f067SGreg Roach     * Handle <textBox>
783a6f13a4aSGreg Roach     *
78409482a55SGreg Roach     * @param array<string> $attrs
7858ba2e626SGreg Roach     *
7868ba2e626SGreg Roach     * @return void
787a6f13a4aSGreg Roach     */
788b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
789c1010edaSGreg Roach    {
790a6f13a4aSGreg Roach        // string Background color code
7917a6ee1acSGreg Roach        $bgcolor = '';
792a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
793a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
794a6f13a4aSGreg Roach        }
795a6f13a4aSGreg Roach
796a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
797a6f13a4aSGreg Roach        $fill = true;
798a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7997a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
800a6f13a4aSGreg Roach                $fill = false;
8017a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
802a6f13a4aSGreg Roach                $fill = true;
803a6f13a4aSGreg Roach            }
804a6f13a4aSGreg Roach        }
805a6f13a4aSGreg Roach
806a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
807a6f13a4aSGreg Roach        $border = false;
808a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
8097a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
810a6f13a4aSGreg Roach                $border = true;
8117a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
812a6f13a4aSGreg Roach                $border = false;
813a6f13a4aSGreg Roach            }
814a6f13a4aSGreg Roach        }
815a6f13a4aSGreg Roach
816a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
817a6f13a4aSGreg Roach        $height = 0;
818a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
819a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
820a6f13a4aSGreg Roach        }
821a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
822a6f13a4aSGreg Roach        $width = 0;
823a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
824a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
825a6f13a4aSGreg Roach        }
826a6f13a4aSGreg Roach
827a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
828c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
829a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
8307a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
831c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
832a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
833a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
8347a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
835a6f13a4aSGreg Roach                $left = 0;
836a6f13a4aSGreg Roach            }
837a6f13a4aSGreg Roach        }
838a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
839c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
840a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8417a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
842c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
843a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
844a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8457a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
846a6f13a4aSGreg Roach                $top = 0;
847a6f13a4aSGreg Roach            }
848a6f13a4aSGreg Roach        }
849a6f13a4aSGreg 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
850a6f13a4aSGreg Roach        $newline = false;
851a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8527a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
853a6f13a4aSGreg Roach                $newline = true;
8547a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
855a6f13a4aSGreg Roach                $newline = false;
856a6f13a4aSGreg Roach            }
857a6f13a4aSGreg Roach        }
858a6f13a4aSGreg Roach        // boolean
859a6f13a4aSGreg Roach        $pagecheck = true;
860a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8617a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
862a6f13a4aSGreg Roach                $pagecheck = false;
8637a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
864a6f13a4aSGreg Roach                $pagecheck = true;
865a6f13a4aSGreg Roach            }
866a6f13a4aSGreg Roach        }
867a6f13a4aSGreg Roach        // boolean Cell padding
868a6f13a4aSGreg Roach        $padding = true;
869a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8707a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
871a6f13a4aSGreg Roach                $padding = false;
8727a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
873a6f13a4aSGreg Roach                $padding = true;
874a6f13a4aSGreg Roach            }
875a6f13a4aSGreg Roach        }
876a6f13a4aSGreg Roach        // boolean Reset this box Height
877a6f13a4aSGreg Roach        $reseth = false;
878a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8797a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
880a6f13a4aSGreg Roach                $reseth = true;
8817a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
882a6f13a4aSGreg Roach                $reseth = false;
883a6f13a4aSGreg Roach            }
884a6f13a4aSGreg Roach        }
885a6f13a4aSGreg Roach
886a6f13a4aSGreg Roach        // string Style of rendering
8877a6ee1acSGreg Roach        $style = '';
888a6f13a4aSGreg Roach
8899b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
890a6f13a4aSGreg Roach        $this->print_data         = false;
891a6f13a4aSGreg Roach
8929b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
893e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
894a6f13a4aSGreg Roach            $width,
895a6f13a4aSGreg Roach            $height,
896a6f13a4aSGreg Roach            $border,
897a6f13a4aSGreg Roach            $bgcolor,
898a6f13a4aSGreg Roach            $newline,
899a6f13a4aSGreg Roach            $left,
900a6f13a4aSGreg Roach            $top,
901a6f13a4aSGreg Roach            $pagecheck,
902a6f13a4aSGreg Roach            $style,
903a6f13a4aSGreg Roach            $fill,
904a6f13a4aSGreg Roach            $padding,
905a6f13a4aSGreg Roach            $reseth
906a6f13a4aSGreg Roach        );
907a6f13a4aSGreg Roach    }
908a6f13a4aSGreg Roach
909a6f13a4aSGreg Roach    /**
910fab8f067SGreg Roach     * Handle <textBox>
9118ba2e626SGreg Roach     *
9128ba2e626SGreg Roach     * @return void
913a6f13a4aSGreg Roach     */
914b702978eSGreg Roach    protected function textBoxEndHandler(): void
915c1010edaSGreg Roach    {
916a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
917e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
91831d9777aSGreg Roach
91931d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
92031d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
92131d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
92231d9777aSGreg Roach
923e8e7866bSGreg Roach        $this->wt_report = array_pop($this->wt_report_stack);
924e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
925a6f13a4aSGreg Roach    }
926a6f13a4aSGreg Roach
927a6f13a4aSGreg Roach    /**
92876692c8bSGreg Roach     * XLM <Text>.
92976692c8bSGreg Roach     *
93009482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9318ba2e626SGreg Roach     *
9328ba2e626SGreg Roach     * @return void
933a6f13a4aSGreg Roach     */
934b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
935c1010edaSGreg Roach    {
9369b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
937a6f13a4aSGreg Roach        $this->print_data         = true;
938a6f13a4aSGreg Roach
939a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9407a6ee1acSGreg Roach        $style = '';
941a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
942a6f13a4aSGreg Roach            $style = $attrs['style'];
943a6f13a4aSGreg Roach        }
944a6f13a4aSGreg Roach
945a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9467a6ee1acSGreg Roach        $color = '';
947a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
948a6f13a4aSGreg Roach            $color = $attrs['color'];
949a6f13a4aSGreg Roach        }
950a6f13a4aSGreg Roach
951e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
952a6f13a4aSGreg Roach    }
953a6f13a4aSGreg Roach
954a6f13a4aSGreg Roach    /**
955fab8f067SGreg Roach     * Handle </text>
9568ba2e626SGreg Roach     *
9578ba2e626SGreg Roach     * @return void
958a6f13a4aSGreg Roach     */
959b702978eSGreg Roach    protected function textEndHandler(): void
960c1010edaSGreg Roach    {
961a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
962e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
963a6f13a4aSGreg Roach    }
964a6f13a4aSGreg Roach
965a6f13a4aSGreg Roach    /**
966fab8f067SGreg Roach     * Handle <getPersonName />
967a6f13a4aSGreg Roach     * Get the name
968a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
969a6f13a4aSGreg Roach     * 2. id is set with a record id
970a6f13a4aSGreg Roach     *
97109482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9728ba2e626SGreg Roach     *
9738ba2e626SGreg Roach     * @return void
974a6f13a4aSGreg Roach     */
975b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
976c1010edaSGreg Roach    {
9777a6ee1acSGreg Roach        $id    = '';
97813abd6f3SGreg Roach        $match = [];
979a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9807a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
981a6f13a4aSGreg Roach                $id = $match[1];
982a6f13a4aSGreg Roach            }
983a6f13a4aSGreg Roach        } else {
9847a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
985d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
986d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
987a6f13a4aSGreg Roach                }
988a6f13a4aSGreg Roach            } else {
9897a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
99013abd6f3SGreg Roach                    $gmatch = [];
991a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
992a6f13a4aSGreg Roach                        $id = $gmatch[1];
993a6f13a4aSGreg Roach                    }
994a6f13a4aSGreg Roach                } else {
995a6f13a4aSGreg Roach                    $id = $attrs['id'];
996a6f13a4aSGreg Roach                }
997a6f13a4aSGreg Roach            }
998a6f13a4aSGreg Roach        }
999a6f13a4aSGreg Roach        if (!empty($id)) {
10006b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
10018f038c36SRico Sonntag            if ($record === null) {
1002a6f13a4aSGreg Roach                return;
1003a6f13a4aSGreg Roach            }
1004a6f13a4aSGreg Roach            if (!$record->canShowName()) {
1005a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
1006a6f13a4aSGreg Roach            } else {
100739ca88baSGreg Roach                $name = $record->fullName();
1008a6f13a4aSGreg Roach                $name = strip_tags($name);
1009a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
1010381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
1011a6f13a4aSGreg Roach                } else {
1012911124f5SGreg Roach                    $addname = (string) $record->alternateName();
1013a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
1014a6f13a4aSGreg Roach                    if (!empty($addname)) {
10157a6ee1acSGreg Roach                        $name .= ' ' . $addname;
1016a6f13a4aSGreg Roach                    }
1017a6f13a4aSGreg Roach                }
1018a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
1019a6f13a4aSGreg Roach            }
1020a6f13a4aSGreg Roach        }
1021a6f13a4aSGreg Roach    }
1022a6f13a4aSGreg Roach
1023a6f13a4aSGreg Roach    /**
1024fab8f067SGreg Roach     * Handle <gedcomValue />
1025a6f13a4aSGreg Roach     *
102609482a55SGreg Roach     * @param array<string> $attrs
10278ba2e626SGreg Roach     *
10288ba2e626SGreg Roach     * @return void
1029a6f13a4aSGreg Roach     */
1030b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1031c1010edaSGreg Roach    {
10327a6ee1acSGreg Roach        $id    = '';
103313abd6f3SGreg Roach        $match = [];
10347a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1035a6f13a4aSGreg Roach            $id = $match[1];
1036a6f13a4aSGreg Roach        }
1037a6f13a4aSGreg Roach
1038044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10397a6ee1acSGreg Roach            $useBreak = '1';
1040a6f13a4aSGreg Roach        } else {
10417a6ee1acSGreg Roach            $useBreak = '0';
1042a6f13a4aSGreg Roach        }
1043a6f13a4aSGreg Roach
1044a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1045a6f13a4aSGreg Roach        if (!empty($tag)) {
1046044416d2SGreg Roach            if ($tag === '@desc') {
1047a6f13a4aSGreg Roach                $value = $this->desc;
1048a6f13a4aSGreg Roach                $value = trim($value);
1049a6f13a4aSGreg Roach                $this->current_element->addText($value);
1050a6f13a4aSGreg Roach            }
1051044416d2SGreg Roach            if ($tag === '@id') {
1052a6f13a4aSGreg Roach                $this->current_element->addText($id);
1053a6f13a4aSGreg Roach            } else {
10547a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1055a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1056b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1057b2448a1bSGreg Roach                    if ($level === 0) {
1058a6f13a4aSGreg Roach                        $level++;
1059a6f13a4aSGreg Roach                    }
1060a6f13a4aSGreg Roach                } else {
1061b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1062a6f13a4aSGreg Roach                }
1063a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10643d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1065a6f13a4aSGreg Roach                switch (end($tags)) {
1066a6f13a4aSGreg Roach                    case 'DATE':
1067a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1068326dd299SGreg Roach                        $value = strip_tags($tmp->display());
1069a6f13a4aSGreg Roach                        break;
1070a6f13a4aSGreg Roach                    case 'PLAC':
1071299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1072392561bbSGreg Roach                        $value = $tmp->shortName();
1073a6f13a4aSGreg Roach                        break;
1074a6f13a4aSGreg Roach                }
1075044416d2SGreg Roach                if ($useBreak === '1') {
1076a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1077a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1078a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1079a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1080a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10813b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10823b3cfeeaSGreg Roach                        $value = substr($value, 4);
1083a6f13a4aSGreg Roach                    }
1084a6f13a4aSGreg Roach                }
1085d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10863cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10874d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
10886f595250SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($value, $this->tree));
10894d35caa7SGreg Roach                    } else {
10906f595250SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($value, $this->tree));
10914d35caa7SGreg Roach                    }
1092a4d703aeSGreg Roach                }
1093a2a485c3SGreg Roach
1094a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1095381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1096a2a485c3SGreg Roach                }
1097a6f13a4aSGreg Roach                $this->current_element->addText($value);
1098a6f13a4aSGreg Roach            }
1099a6f13a4aSGreg Roach        }
1100a6f13a4aSGreg Roach    }
1101a6f13a4aSGreg Roach
1102a6f13a4aSGreg Roach    /**
1103fab8f067SGreg Roach     * Handle <repeatTag>
1104a6f13a4aSGreg Roach     *
110509482a55SGreg Roach     * @param array<string> $attrs
11068ba2e626SGreg Roach     *
11078ba2e626SGreg Roach     * @return void
1108a6f13a4aSGreg Roach     */
1109b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1110c1010edaSGreg Roach    {
1111a6f13a4aSGreg Roach        $this->process_repeats++;
1112a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1113a6f13a4aSGreg Roach            return;
1114a6f13a4aSGreg Roach        }
1115a6f13a4aSGreg Roach
11169b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
111713abd6f3SGreg Roach        $this->repeats         = [];
1118e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1119a6f13a4aSGreg Roach
1120e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1121a6f13a4aSGreg Roach        if (!empty($tag)) {
1122044416d2SGreg Roach            if ($tag === '@desc') {
1123a6f13a4aSGreg Roach                $value = $this->desc;
1124a6f13a4aSGreg Roach                $value = trim($value);
1125a6f13a4aSGreg Roach                $this->current_element->addText($value);
1126a6f13a4aSGreg Roach            } else {
11277a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11287a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1129b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1130b2448a1bSGreg Roach                if ($level === 0) {
1131a6f13a4aSGreg Roach                    $level++;
1132a6f13a4aSGreg Roach                }
1133a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1134a6f13a4aSGreg Roach                $t      = $tag;
1135a6f13a4aSGreg Roach                $count  = count($tags);
1136a6f13a4aSGreg Roach                $i      = 0;
1137a6f13a4aSGreg Roach                while ($i < $count) {
1138a6f13a4aSGreg Roach                    $t = $tags[$i];
1139a6f13a4aSGreg Roach                    if (!empty($t)) {
1140a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
1141b315f3e1SGreg Roach                            $subrec = self::getSubRecord($level, "$level $t", $subrec);
1142a6f13a4aSGreg Roach                            if (empty($subrec)) {
1143a6f13a4aSGreg Roach                                $level--;
1144b315f3e1SGreg Roach                                $subrec = self::getSubRecord($level, "@ $t", $this->gedrec);
1145a6f13a4aSGreg Roach                                if (empty($subrec)) {
1146a6f13a4aSGreg Roach                                    return;
1147a6f13a4aSGreg Roach                                }
1148a6f13a4aSGreg Roach                            }
1149a6f13a4aSGreg Roach                        }
1150a6f13a4aSGreg Roach                        $level++;
1151a6f13a4aSGreg Roach                    }
1152a6f13a4aSGreg Roach                    $i++;
1153a6f13a4aSGreg Roach                }
1154a6f13a4aSGreg Roach                $level--;
1155a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1156a6f13a4aSGreg Roach                $i     = 0;
1157a6f13a4aSGreg Roach                while ($i < $count) {
1158a6f13a4aSGreg Roach                    $i++;
1159a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1160b315f3e1SGreg Roach                    $subrecord = self::getSubRecord($level, "$level $t", $subrec, $i);
11618d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11626b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1163a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1164a9007102SGreg Roach                            continue;
1165a9007102SGreg Roach                        }
1166a9007102SGreg Roach                    }
1167a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1168a6f13a4aSGreg Roach                }
1169a6f13a4aSGreg Roach            }
1170a6f13a4aSGreg Roach        }
1171a6f13a4aSGreg Roach    }
1172a6f13a4aSGreg Roach
1173a6f13a4aSGreg Roach    /**
1174fab8f067SGreg Roach     * Handle </repeatTag>
11758ba2e626SGreg Roach     *
11768ba2e626SGreg Roach     * @return void
1177a6f13a4aSGreg Roach     */
1178b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1179c1010edaSGreg Roach    {
1180a6f13a4aSGreg Roach        $this->process_repeats--;
1181a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1182a6f13a4aSGreg Roach            return;
1183a6f13a4aSGreg Roach        }
1184a6f13a4aSGreg Roach
1185a6f13a4aSGreg Roach        // Check if there is anything to repeat
1186a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1187a6f13a4aSGreg Roach            // No need to load them if not used...
1188a6f13a4aSGreg Roach
1189a6f13a4aSGreg Roach            $lineoffset = 0;
1190a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1191a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1192a6f13a4aSGreg Roach            }
1193a6f13a4aSGreg Roach            //-- read the xml from the file
1194299d100dSGreg Roach            $lines = file($this->report);
1195dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1196a6f13a4aSGreg Roach                $lineoffset--;
1197a6f13a4aSGreg Roach            }
1198a6f13a4aSGreg Roach            $lineoffset++;
1199a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1200a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1201a6f13a4aSGreg Roach            // RepeatTag Level counter
1202a6f13a4aSGreg Roach            $count = 1;
1203a6f13a4aSGreg Roach            while (0 < $count) {
1204dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1205a6f13a4aSGreg Roach                    $count++;
1206dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1207a6f13a4aSGreg Roach                    $count--;
1208a6f13a4aSGreg Roach                }
1209a6f13a4aSGreg Roach                if (0 < $count) {
1210a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1211a6f13a4aSGreg Roach                }
1212a6f13a4aSGreg Roach                $line_nr++;
1213a6f13a4aSGreg Roach            }
1214a6f13a4aSGreg Roach            // No need to drag this
1215a6f13a4aSGreg Roach            unset($lines);
1216a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1217a6f13a4aSGreg Roach            // Save original values
12189b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1219a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1220a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1221a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1222a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1223e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1224a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
12251aa04befSGreg Roach
12261aa04befSGreg Roach                xml_set_element_handler(
12271aa04befSGreg Roach                    $repeat_parser,
12289d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12291aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12301aa04befSGreg Roach                    },
12319d454b6bSGreg Roach                    function ($parser, string $name): void {
12321aa04befSGreg Roach                        $this->endElement($parser, $name);
12331aa04befSGreg Roach                    }
12341aa04befSGreg Roach                );
12351aa04befSGreg Roach
12361aa04befSGreg Roach                xml_set_character_data_handler(
12371aa04befSGreg Roach                    $repeat_parser,
12389d454b6bSGreg Roach                    function ($parser, string $data): void {
12391aa04befSGreg Roach                        $this->characterData($parser, $data);
12401aa04befSGreg Roach                    }
12411aa04befSGreg Roach                );
12421aa04befSGreg Roach
1243a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12446ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1245a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1246a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1247a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1248a6f13a4aSGreg Roach                    ));
1249a6f13a4aSGreg Roach                }
1250a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1251a6f13a4aSGreg Roach            }
1252a6f13a4aSGreg Roach            // Restore original values
1253a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1254e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1255a6f13a4aSGreg Roach        }
125665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1257a6f13a4aSGreg Roach    }
1258a6f13a4aSGreg Roach
1259a6f13a4aSGreg Roach    /**
1260a6f13a4aSGreg Roach     * Variable lookup
1261a6f13a4aSGreg Roach     * Retrieve predefined variables :
1262a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1263a6f13a4aSGreg Roach     *        1 EVEN This is a description
1264a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1265a6f13a4aSGreg Roach     * $ I18N::translate('....')
1266a6f13a4aSGreg Roach     * $ language_settings[]
1267a6f13a4aSGreg Roach     *
126809482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
12698ba2e626SGreg Roach     *
12708ba2e626SGreg Roach     * @return void
1271a6f13a4aSGreg Roach     */
1272b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1273c1010edaSGreg Roach    {
1274a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12756ccdf4f0SGreg 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));
1276a6f13a4aSGreg Roach        }
1277a6f13a4aSGreg Roach
1278a6f13a4aSGreg Roach        $var = $attrs['var'];
1279a6f13a4aSGreg Roach        // SetVar element preset variables
1280d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1281d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1282a6f13a4aSGreg Roach        } else {
1283a6f13a4aSGreg Roach            $tfact = $this->fact;
12847a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1285a6f13a4aSGreg Roach                // Use :
1286a6f13a4aSGreg Roach                // n TYPE This text if string
1287a6f13a4aSGreg Roach                $tfact = $this->type;
128897def6bcSGreg Roach            } else {
128997def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
129097def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
129197def6bcSGreg Roach
129297def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
129397def6bcSGreg Roach                        $tfact = $element->label();
129497def6bcSGreg Roach                        break;
1295a6f13a4aSGreg Roach                    }
129697def6bcSGreg Roach                }
129797def6bcSGreg Roach            }
129897def6bcSGreg Roach
129997def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
130097def6bcSGreg Roach
1301a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1302da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1303a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1304a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1305a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1306a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1307a6f13a4aSGreg Roach            }
1308a6f13a4aSGreg Roach        }
1309a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1310a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
13117a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1312a6f13a4aSGreg Roach                $g   = new Date($var);
1313a6f13a4aSGreg Roach                $var = $g->display();
1314a6f13a4aSGreg Roach            }
1315a6f13a4aSGreg Roach        }
1316a6f13a4aSGreg Roach        $this->current_element->addText($var);
13172836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1318a6f13a4aSGreg Roach    }
1319a6f13a4aSGreg Roach
1320a6f13a4aSGreg Roach    /**
1321fab8f067SGreg Roach     * Handle <facts>
132276692c8bSGreg Roach     *
132309482a55SGreg Roach     * @param array<string> $attrs
13248ba2e626SGreg Roach     *
13258ba2e626SGreg Roach     * @return void
1326a6f13a4aSGreg Roach     */
1327b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1328c1010edaSGreg Roach    {
1329a6f13a4aSGreg Roach        $this->process_repeats++;
1330a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1331a6f13a4aSGreg Roach            return;
1332a6f13a4aSGreg Roach        }
1333a6f13a4aSGreg Roach
13349b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
133513abd6f3SGreg Roach        $this->repeats         = [];
1336e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1337a6f13a4aSGreg Roach
13387a6ee1acSGreg Roach        $id    = '';
133913abd6f3SGreg Roach        $match = [];
13407a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1341a6f13a4aSGreg Roach            $id = $match[1];
1342a6f13a4aSGreg Roach        }
13437a6ee1acSGreg Roach        $tag = '';
1344a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1345a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1346a6f13a4aSGreg Roach        }
13477a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1348d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1349a6f13a4aSGreg Roach        }
1350a6f13a4aSGreg Roach
13516b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1352a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1353820b62dfSGreg Roach            $facts = $record->facts([], true);
135413abd6f3SGreg Roach            $this->repeats = [];
1355a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1356195b5e75SGreg Roach            foreach ($facts as $fact) {
1357d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1358d0889c63SGreg Roach
1359d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1360195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1361a6f13a4aSGreg Roach                }
1362a6f13a4aSGreg Roach            }
1363a6f13a4aSGreg Roach        } else {
136430158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1365d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1366138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1367a6f13a4aSGreg Roach                }
1368a6f13a4aSGreg Roach            }
1369a6f13a4aSGreg Roach        }
1370a6f13a4aSGreg Roach    }
1371a6f13a4aSGreg Roach
1372a6f13a4aSGreg Roach    /**
1373fab8f067SGreg Roach     * Handle </facts>
13748ba2e626SGreg Roach     *
13758ba2e626SGreg Roach     * @return void
1376a6f13a4aSGreg Roach     */
1377b702978eSGreg Roach    protected function factsEndHandler(): void
1378c1010edaSGreg Roach    {
1379a6f13a4aSGreg Roach        $this->process_repeats--;
1380a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1381a6f13a4aSGreg Roach            return;
1382a6f13a4aSGreg Roach        }
1383a6f13a4aSGreg Roach
1384a6f13a4aSGreg Roach        // Check if there is anything to repeat
1385a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1386e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1387a6f13a4aSGreg Roach            $lineoffset = 0;
1388a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1389a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1390a6f13a4aSGreg Roach            }
1391a6f13a4aSGreg Roach
1392a6f13a4aSGreg Roach            //-- read the xml from the file
1393299d100dSGreg Roach            $lines = file($this->report);
1394dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1395a6f13a4aSGreg Roach                $lineoffset--;
1396a6f13a4aSGreg Roach            }
1397a6f13a4aSGreg Roach            $lineoffset++;
1398a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1399a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1400a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1401a6f13a4aSGreg Roach            while ($line_nr < $i) {
1402a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1403a6f13a4aSGreg Roach                $line_nr++;
1404a6f13a4aSGreg Roach            }
1405a6f13a4aSGreg Roach            // No need to drag this
1406a6f13a4aSGreg Roach            unset($lines);
1407a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1408a6f13a4aSGreg Roach            // Save original values
14099b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1410a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1411a6f13a4aSGreg Roach            $count                = count($this->repeats);
1412a6f13a4aSGreg Roach            $i                    = 0;
1413a6f13a4aSGreg Roach            while ($i < $count) {
1414a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1415a6f13a4aSGreg Roach                $this->fact   = '';
1416a6f13a4aSGreg Roach                $this->desc   = '';
1417a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1418a6f13a4aSGreg Roach                    $this->fact = $match[1];
1419a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
142013abd6f3SGreg Roach                        $tmatch = [];
1421a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1422a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1423a6f13a4aSGreg Roach                        } else {
1424a6f13a4aSGreg Roach                            $this->type = ' ';
1425a6f13a4aSGreg Roach                        }
1426a6f13a4aSGreg Roach                    }
1427a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
1428b315f3e1SGreg Roach                    $this->desc .= self::getCont(2, $this->gedrec);
1429a6f13a4aSGreg Roach                }
1430a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1431e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1432a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
14331aa04befSGreg Roach
14341aa04befSGreg Roach                xml_set_element_handler(
14351aa04befSGreg Roach                    $repeat_parser,
14369d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14371aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14381aa04befSGreg Roach                    },
14399d454b6bSGreg Roach                    function ($parser, string $name): void {
14401aa04befSGreg Roach                        $this->endElement($parser, $name);
14411aa04befSGreg Roach                    }
14421aa04befSGreg Roach                );
14431aa04befSGreg Roach
14441aa04befSGreg Roach                xml_set_character_data_handler(
14451aa04befSGreg Roach                    $repeat_parser,
14469d454b6bSGreg Roach                    function ($parser, string $data): void {
14471aa04befSGreg Roach                        $this->characterData($parser, $data);
14481aa04befSGreg Roach                    }
14491aa04befSGreg Roach                );
14501aa04befSGreg Roach
1451a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14526ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1453a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1454a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1455a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1456a6f13a4aSGreg Roach                    ));
1457a6f13a4aSGreg Roach                }
1458a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1459a6f13a4aSGreg Roach                $i++;
1460a6f13a4aSGreg Roach            }
1461a6f13a4aSGreg Roach            // Restore original values
1462e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1463a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1464a6f13a4aSGreg Roach        }
146565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1466a6f13a4aSGreg Roach    }
1467a6f13a4aSGreg Roach
1468a6f13a4aSGreg Roach    /**
1469a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1470d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1471a6f13a4aSGreg Roach     *
147209482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
14738ba2e626SGreg Roach     *
14748ba2e626SGreg Roach     * @return void
1475a6f13a4aSGreg Roach     */
1476b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1477c1010edaSGreg Roach    {
1478a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14796ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1480a6f13a4aSGreg Roach        }
1481a6f13a4aSGreg Roach
1482a6f13a4aSGreg Roach        $name  = $attrs['name'];
1483a6f13a4aSGreg Roach        $value = $attrs['value'];
148413abd6f3SGreg Roach        $match = [];
1485a6f13a4aSGreg Roach        // Current GEDCOM record strings
1486044416d2SGreg Roach        if ($value === '@ID') {
14877a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1488a6f13a4aSGreg Roach                $value = $match[1];
1489a6f13a4aSGreg Roach            }
1490044416d2SGreg Roach        } elseif ($value === '@fact') {
1491a6f13a4aSGreg Roach            $value = $this->fact;
1492044416d2SGreg Roach        } elseif ($value === '@desc') {
1493a6f13a4aSGreg Roach            $value = $this->desc;
1494044416d2SGreg Roach        } elseif ($value === '@generation') {
1495589feda3SGreg Roach            $value = (string) $this->generation;
1496a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
149713abd6f3SGreg Roach            $gmatch = [];
1498a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14997a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1500a6f13a4aSGreg Roach            }
1501a6f13a4aSGreg Roach        }
1502a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1503d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1504a6f13a4aSGreg Roach        }
1505a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1506a6f13a4aSGreg Roach        $i     = 0;
1507a6f13a4aSGreg Roach        while ($i < $count) {
1508d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
15097a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1510a6f13a4aSGreg Roach            $i++;
1511a6f13a4aSGreg Roach        }
1512a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1513da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1514a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1515a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1516a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1517a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1518a6f13a4aSGreg Roach        }
151952868398SGreg Roach
1520a6f13a4aSGreg Roach        // Arithmetic functions
15213cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
152252868398SGreg Roach            // Create an expression language with the functions used by our reports.
152352868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1524c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1525c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
152652868398SGreg Roach
152752868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1528a6f13a4aSGreg Roach        }
152952868398SGreg Roach
1530dec352c1SGreg Roach        if (str_contains($value, '@')) {
15317a6ee1acSGreg Roach            $value = '';
1532a6f13a4aSGreg Roach        }
1533d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1534a6f13a4aSGreg Roach    }
1535a6f13a4aSGreg Roach
1536a6f13a4aSGreg Roach    /**
1537fab8f067SGreg Roach     * Handle <if>
1538a6f13a4aSGreg Roach     *
153909482a55SGreg Roach     * @param array<string> $attrs
15408ba2e626SGreg Roach     *
15418ba2e626SGreg Roach     * @return void
1542a6f13a4aSGreg Roach     */
1543b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1544c1010edaSGreg Roach    {
1545a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1546a6f13a4aSGreg Roach            $this->process_ifs++;
1547a6f13a4aSGreg Roach
1548a6f13a4aSGreg Roach            return;
1549a6f13a4aSGreg Roach        }
1550a6f13a4aSGreg Roach
1551a6f13a4aSGreg Roach        $condition = $attrs['condition'];
155282759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1553c1010edaSGreg Roach        $condition = str_replace([
1554c1010edaSGreg Roach            ' LT ',
1555c1010edaSGreg Roach            ' GT ',
1556c1010edaSGreg Roach        ], [
1557c1010edaSGreg Roach            '<',
1558c1010edaSGreg Roach            '>',
1559c1010edaSGreg Roach        ], $condition);
15603cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15617a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
156213abd6f3SGreg Roach        $match     = [];
15633cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1564a6f13a4aSGreg Roach        $i         = 0;
1565a6f13a4aSGreg Roach        while ($i < $count) {
1566a6f13a4aSGreg Roach            $id    = $match[$i][1];
1567a6f13a4aSGreg Roach            $value = '""';
1568044416d2SGreg Roach            if ($id === 'ID') {
15697a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1570a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1571a6f13a4aSGreg Roach                }
15727a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1573a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15747a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1575a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15767a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1577a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1578a6f13a4aSGreg Roach            } else {
1579b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1580b2448a1bSGreg Roach                if ($level === 0) {
1581a6f13a4aSGreg Roach                    $level++;
1582a6f13a4aSGreg Roach                }
15833d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1584a6f13a4aSGreg Roach                if (empty($value)) {
1585a6f13a4aSGreg Roach                    $level++;
15863d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1587a6f13a4aSGreg Roach                }
15888d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15895e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1590a6f13a4aSGreg Roach            }
1591a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1592a6f13a4aSGreg Roach            $i++;
1593a6f13a4aSGreg Roach        }
15945809450fSGreg Roach
1595cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1596cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1597c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1598c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1599cb63a60eSGreg Roach
1600cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
16015809450fSGreg Roach
1602a6f13a4aSGreg Roach        if (!$ret) {
1603a6f13a4aSGreg Roach            $this->process_ifs++;
1604a6f13a4aSGreg Roach        }
1605a6f13a4aSGreg Roach    }
1606a6f13a4aSGreg Roach
1607a6f13a4aSGreg Roach    /**
1608fab8f067SGreg Roach     * Handle </if>
16098ba2e626SGreg Roach     *
16108ba2e626SGreg Roach     * @return void
1611a6f13a4aSGreg Roach     */
1612b702978eSGreg Roach    protected function ifEndHandler(): void
1613c1010edaSGreg Roach    {
1614a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1615a6f13a4aSGreg Roach            $this->process_ifs--;
1616a6f13a4aSGreg Roach        }
1617a6f13a4aSGreg Roach    }
1618a6f13a4aSGreg Roach
1619a6f13a4aSGreg Roach    /**
1620fab8f067SGreg Roach     * Handle <footnote>
1621a6f13a4aSGreg Roach     * Collect the Footnote links
1622fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1623a6f13a4aSGreg Roach     *
162409482a55SGreg Roach     * @param array<string> $attrs
16258ba2e626SGreg Roach     *
16268ba2e626SGreg Roach     * @return void
1627a6f13a4aSGreg Roach     */
1628b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1629c1010edaSGreg Roach    {
16307a6ee1acSGreg Roach        $id = '';
16317a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1632a6f13a4aSGreg Roach            $id = $match[2];
1633a6f13a4aSGreg Roach        }
16346b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1635a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16369b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1637a6f13a4aSGreg Roach            $this->print_data         = true;
16387a6ee1acSGreg Roach            $style                    = '';
1639a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1640a6f13a4aSGreg Roach                $style = $attrs['style'];
1641a6f13a4aSGreg Roach            }
1642a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1643e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1644a6f13a4aSGreg Roach        } else {
1645a6f13a4aSGreg Roach            $this->print_data       = false;
1646a6f13a4aSGreg Roach            $this->process_footnote = false;
1647a6f13a4aSGreg Roach        }
1648a6f13a4aSGreg Roach    }
1649a6f13a4aSGreg Roach
1650a6f13a4aSGreg Roach    /**
1651fab8f067SGreg Roach     * Handle </footnote>
1652a6f13a4aSGreg Roach     * Print the collected Footnote data
16538ba2e626SGreg Roach     *
16548ba2e626SGreg Roach     * @return void
1655a6f13a4aSGreg Roach     */
1656b702978eSGreg Roach    protected function footnoteEndHandler(): void
1657c1010edaSGreg Roach    {
1658a6f13a4aSGreg Roach        if ($this->process_footnote) {
1659a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1660a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1661a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1662e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1663a6f13a4aSGreg Roach            }
1664a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1665a6f13a4aSGreg Roach        } else {
1666a6f13a4aSGreg Roach            $this->process_footnote = true;
1667a6f13a4aSGreg Roach        }
1668a6f13a4aSGreg Roach    }
1669a6f13a4aSGreg Roach
1670a6f13a4aSGreg Roach    /**
1671fab8f067SGreg Roach     * Handle <footnoteTexts />
16728ba2e626SGreg Roach     *
16738ba2e626SGreg Roach     * @return void
1674a6f13a4aSGreg Roach     */
1675b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1676c1010edaSGreg Roach    {
16777a6ee1acSGreg Roach        $temp = 'footnotetexts';
1678e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1679a6f13a4aSGreg Roach    }
1680a6f13a4aSGreg Roach
1681a6f13a4aSGreg Roach    /**
1682a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16838ba2e626SGreg Roach     *
16848ba2e626SGreg Roach     * @return void
1685a6f13a4aSGreg Roach     */
1686b702978eSGreg Roach    protected function brStartHandler(): void
1687c1010edaSGreg Roach    {
1688a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1689a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1690a6f13a4aSGreg Roach        }
1691a6f13a4aSGreg Roach    }
1692a6f13a4aSGreg Roach
1693a6f13a4aSGreg Roach    /**
1694fab8f067SGreg Roach     * Handle <sp />
1695fab8f067SGreg Roach     * Forced space
16968ba2e626SGreg Roach     *
16978ba2e626SGreg Roach     * @return void
1698a6f13a4aSGreg Roach     */
1699b702978eSGreg Roach    protected function spStartHandler(): void
1700c1010edaSGreg Roach    {
1701a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1702a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1703a6f13a4aSGreg Roach        }
1704a6f13a4aSGreg Roach    }
1705a6f13a4aSGreg Roach
1706a6f13a4aSGreg Roach    /**
1707fab8f067SGreg Roach     * Handle <highlightedImage />
170876692c8bSGreg Roach     *
170909482a55SGreg Roach     * @param array<string> $attrs
17108ba2e626SGreg Roach     *
17118ba2e626SGreg Roach     * @return void
1712a6f13a4aSGreg Roach     */
1713b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1714c1010edaSGreg Roach    {
1715a6f13a4aSGreg Roach        $id = '';
17167a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1717a6f13a4aSGreg Roach            $id = $match[1];
1718a6f13a4aSGreg Roach        }
1719a6f13a4aSGreg Roach
1720c21bdddcSGreg Roach        // Position the top corner of this box on the page
1721c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1722a6f13a4aSGreg Roach
1723c21bdddcSGreg Roach        // Position the left corner of this box on the page
1724c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1725a6f13a4aSGreg Roach
172683cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172783cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1728a6f13a4aSGreg Roach
1729a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
173083cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1731a6f13a4aSGreg Roach
173283cdc021SGreg Roach        // Width, height (or both).
1733c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1734c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1735a6f13a4aSGreg Roach
17366b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
17374a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
173886a63f51SGreg Roach
1739a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1740a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1741b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
174229518ad2SGreg Roach
1743a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17443c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17453c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1746a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17473c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17483c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1749a6f13a4aSGreg Roach            } else {
17503c3b90deSGreg Roach                $width  = $attributes[0];
17513c3b90deSGreg Roach                $height = $attributes[1];
1752a6f13a4aSGreg Roach            }
1753a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1754e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1755a6f13a4aSGreg Roach        }
1756a6f13a4aSGreg Roach    }
1757a6f13a4aSGreg Roach
1758a6f13a4aSGreg Roach    /**
1759fab8f067SGreg Roach     * Handle <image/>
176076692c8bSGreg Roach     *
176109482a55SGreg Roach     * @param array<string> $attrs
17628ba2e626SGreg Roach     *
17638ba2e626SGreg Roach     * @return void
1764a6f13a4aSGreg Roach     */
1765b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1766c1010edaSGreg Roach    {
176783cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1768c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1769a6f13a4aSGreg Roach
1770a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1771c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1772a6f13a4aSGreg Roach
177383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
177483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1775a6f13a4aSGreg Roach
1776a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
177783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1778a6f13a4aSGreg Roach
177983cdc021SGreg Roach        // Width, height (or both).
1780c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1781c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1782a6f13a4aSGreg Roach
178383cdc021SGreg Roach        $file = $attrs['file'] ?? '';
178483cdc021SGreg Roach
1785044416d2SGreg Roach        if ($file === '@FILE') {
178613abd6f3SGreg Roach            $match = [];
1787a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17886b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17894a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1790cdf416fbSGreg Roach
1791a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1792a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1793b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
179429518ad2SGreg Roach
1795a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17963c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17973c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1798a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17993c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
18003c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1801a6f13a4aSGreg Roach                    } else {
18023c3b90deSGreg Roach                        $width  = $attributes[0];
18033c3b90deSGreg Roach                        $height = $attributes[1];
1804a6f13a4aSGreg Roach                    }
1805a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1806e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1807a6f13a4aSGreg Roach                }
1808a6f13a4aSGreg Roach            }
1809a6f13a4aSGreg Roach        } else {
18107a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1811a6f13a4aSGreg Roach                $size = getimagesize($file);
1812a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1813a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1814a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1815a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1816a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1817a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1818a6f13a4aSGreg Roach                } else {
1819a6f13a4aSGreg Roach                    $width  = $size[0];
1820a6f13a4aSGreg Roach                    $height = $size[1];
1821a6f13a4aSGreg Roach                }
1822e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1823e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1824a6f13a4aSGreg Roach            }
1825a6f13a4aSGreg Roach        }
1826a6f13a4aSGreg Roach    }
1827a6f13a4aSGreg Roach
1828a6f13a4aSGreg Roach    /**
1829fab8f067SGreg Roach     * Handle <line>
1830a6f13a4aSGreg Roach     *
183109482a55SGreg Roach     * @param array<string> $attrs
18328ba2e626SGreg Roach     *
18338ba2e626SGreg Roach     * @return void
1834a6f13a4aSGreg Roach     */
1835b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1836c1010edaSGreg Roach    {
1837a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1838c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1839a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18407a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1841a6f13a4aSGreg Roach                $x1 = 0;
18427a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1843c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1844a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1845c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1846a6f13a4aSGreg Roach            }
1847a6f13a4aSGreg Roach        }
1848a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1849c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1850a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18517a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1852a6f13a4aSGreg Roach                $y1 = 0;
18537a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1854c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1855a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1856c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1857a6f13a4aSGreg Roach            }
1858a6f13a4aSGreg Roach        }
1859a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1860c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1861a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18627a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1863a6f13a4aSGreg Roach                $x2 = 0;
18647a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1865c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1866a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1867c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1868a6f13a4aSGreg Roach            }
1869a6f13a4aSGreg Roach        }
1870a6f13a4aSGreg Roach        // End vertical position
1871c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1872a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18737a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1874a6f13a4aSGreg Roach                $y2 = 0;
18757a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1876c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1877a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1878c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1879a6f13a4aSGreg Roach            }
1880a6f13a4aSGreg Roach        }
1881a6f13a4aSGreg Roach
1882e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1883e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1884a6f13a4aSGreg Roach    }
1885a6f13a4aSGreg Roach
1886a6f13a4aSGreg Roach    /**
1887fab8f067SGreg Roach     * Handle <list>
1888a6f13a4aSGreg Roach     *
188909482a55SGreg Roach     * @param array<string> $attrs
18908ba2e626SGreg Roach     *
18918ba2e626SGreg Roach     * @return void
1892a6f13a4aSGreg Roach     */
1893b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1894c1010edaSGreg Roach    {
1895a6f13a4aSGreg Roach        $this->process_repeats++;
1896a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1897a6f13a4aSGreg Roach            return;
1898a6f13a4aSGreg Roach        }
1899a6f13a4aSGreg Roach
190013abd6f3SGreg Roach        $match = [];
1901a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1902a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1903a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1904d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1905a6f13a4aSGreg Roach                $sortby = trim($sortby);
1906a6f13a4aSGreg Roach            }
1907a6f13a4aSGreg Roach        } else {
19087a6ee1acSGreg Roach            $sortby = 'NAME';
1909a6f13a4aSGreg Roach        }
1910a6f13a4aSGreg Roach
1911e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1912195b5e75SGreg Roach
1913a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1914a6f13a4aSGreg Roach        switch ($listname) {
19157a6ee1acSGreg Roach            case 'pending':
191663b1851dSGreg Roach                $this->list = DB::table('change')
1917195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1918a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1919195b5e75SGreg Roach                            ->from('change')
1920195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1921195b5e75SGreg Roach                            ->where('status', '=', 'pending')
19227f5c2944SGreg Roach                            ->groupBy(['xref']);
1923195b5e75SGreg Roach                    })
192463b1851dSGreg Roach                    ->get()
192563b1851dSGreg Roach                    ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom))
192663b1851dSGreg Roach                    ->filter()
192763b1851dSGreg Roach                    ->all();
1928a6f13a4aSGreg Roach                break;
192963b1851dSGreg Roach
1930a6f13a4aSGreg Roach            case 'individual':
19315985adfbSGreg Roach                $query = DB::table('individuals')
19325985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19335985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19345985adfbSGreg Roach                    ->distinct();
19355985adfbSGreg Roach
1936a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1937dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
193882759250SGreg Roach                        $value = $this->substituteVars($value, false);
1939a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1940a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19410b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19425985adfbSGreg Roach                                $join
19435985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19445985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19455985adfbSGreg Roach                            });
19465985adfbSGreg Roach
19475985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19485985adfbSGreg Roach
1949a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19505985adfbSGreg Roach
1951044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19525985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1953a6f13a4aSGreg Roach                            } else {
19545985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1955a6f13a4aSGreg Roach                            }
19565985adfbSGreg Roach
19575985adfbSGreg Roach                            // This filter has been fully processed
19585985adfbSGreg Roach                            unset($attrs[$attr]);
19597ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19600b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19615985adfbSGreg Roach                                $join
19625985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19635985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19645985adfbSGreg Roach                            });
1965a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19667a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19675d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1968b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1969a6f13a4aSGreg Roach                            }
19705985adfbSGreg Roach
19715985adfbSGreg Roach                            // This filter has been fully processed
19725985adfbSGreg Roach                            unset($attrs[$attr]);
1973a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19745985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1975a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19765985adfbSGreg Roach
1977a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19785985adfbSGreg Roach
19795985adfbSGreg Roach                            // This filter has been fully processed
19805985adfbSGreg Roach                            unset($attrs[$attr]);
19812fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19829dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19835985adfbSGreg Roach                            $query
19840b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19855985adfbSGreg Roach                                    $join
19867ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19877ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19885985adfbSGreg Roach                                })
19890b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19907ee4bfadSGreg Roach                                    $join
19917ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19927ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19937ee4bfadSGreg Roach                                })
19949b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19956e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19965985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19977ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19986e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19996e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
200047a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
20016e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20026e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2003e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20047ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
2005a6f13a4aSGreg Roach                        }
2006a6f13a4aSGreg Roach                    }
2007a6f13a4aSGreg Roach                }
2008a6f13a4aSGreg Roach
200913abd6f3SGreg Roach                $this->list = [];
2010a6f13a4aSGreg Roach
20115985adfbSGreg Roach                foreach ($query->get() as $row) {
20126b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
2013a6f13a4aSGreg Roach                }
2014a6f13a4aSGreg Roach                break;
2015a6f13a4aSGreg Roach
2016a6f13a4aSGreg Roach            case 'family':
201730fc2b1eSGreg Roach                $query = DB::table('families')
201830fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
201930fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
202030fc2b1eSGreg Roach                    ->distinct();
202130fc2b1eSGreg Roach
2022a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
2023dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
202482759250SGreg Roach                        $value = $this->substituteVars($value, false);
2025a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2026a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20270b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
202830fc2b1eSGreg Roach                                $join
202930fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
203030fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
203130fc2b1eSGreg Roach                            });
203230fc2b1eSGreg Roach
203330fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
203430fc2b1eSGreg Roach
2035a6f13a4aSGreg Roach                            $date = new Date($match[3]);
203630fc2b1eSGreg Roach
2037044416d2SGreg Roach                            if ($match[2] === 'LTE') {
203830fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2039a6f13a4aSGreg Roach                            } else {
204030fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2041a6f13a4aSGreg Roach                            }
204230fc2b1eSGreg Roach
204330fc2b1eSGreg Roach                            // This filter has been fully processed
204430fc2b1eSGreg Roach                            unset($attrs[$attr]);
2045a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
204630fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2047a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
204830fc2b1eSGreg Roach
2049a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
205030fc2b1eSGreg Roach
205130fc2b1eSGreg Roach                            // This filter has been fully processed
205230fc2b1eSGreg Roach                            unset($attrs[$attr]);
205330fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20543b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20550b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
205630fc2b1eSGreg Roach                                    $join
205730fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20583b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
205930fc2b1eSGreg Roach                                            $query
206030fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
206130fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
206230fc2b1eSGreg Roach                                        });
206330fc2b1eSGreg Roach                                });
20645d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20657a6ee1acSGreg Roach                                if ($match[1] != '') {
20667a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20675d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2068b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20695d0bc43dSGreg Roach                                    }
20705d0bc43dSGreg Roach                                }
2071a6f13a4aSGreg Roach                            }
207230fc2b1eSGreg Roach
207330fc2b1eSGreg Roach                            // This filter has been fully processed
207430fc2b1eSGreg Roach                            unset($attrs[$attr]);
20756e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20769dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
207730fc2b1eSGreg Roach                            $query
20780b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
207930fc2b1eSGreg Roach                                    $join
20806e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20816e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
208230fc2b1eSGreg Roach                                })
20830b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20846e5c0963SGreg Roach                                    $join
20856e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20866e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20876e5c0963SGreg Roach                                })
2088b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20896e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
209030fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20916e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20926e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20936e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20946e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20956e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20966e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2097e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20986e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2099a6f13a4aSGreg Roach                        }
2100a6f13a4aSGreg Roach                    }
2101a6f13a4aSGreg Roach                }
2102a6f13a4aSGreg Roach
210313abd6f3SGreg Roach                $this->list = [];
2104a6f13a4aSGreg Roach
210530fc2b1eSGreg Roach                foreach ($query->get() as $row) {
21066b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2107a6f13a4aSGreg Roach                }
2108a6f13a4aSGreg Roach                break;
2109a6f13a4aSGreg Roach
2110a6f13a4aSGreg Roach            default:
21116ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2112a6f13a4aSGreg Roach        }
2113a6f13a4aSGreg Roach
211413abd6f3SGreg Roach        $filters  = [];
211513abd6f3SGreg Roach        $filters2 = [];
2116a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2117a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2118a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2119a6f13a4aSGreg Roach                    $condition = $value;
2120a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2121a6f13a4aSGreg Roach                        $id    = $match[1];
2122a6f13a4aSGreg Roach                        $value = "''";
2123044416d2SGreg Roach                        if ($id === 'ID') {
21247a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2125a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2126a6f13a4aSGreg Roach                            }
2127044416d2SGreg Roach                        } elseif ($id === 'fact') {
2128a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2129044416d2SGreg Roach                        } elseif ($id === 'desc') {
2130a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2131a6f13a4aSGreg Roach                        } else {
2132a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21337a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2134a6f13a4aSGreg Roach                            }
2135a6f13a4aSGreg Roach                        }
2136a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2137a6f13a4aSGreg Roach                    }
2138a6f13a4aSGreg Roach                    //-- handle regular expressions
2139a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2140a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2141a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2142a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2143a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2144d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2145a6f13a4aSGreg Roach                            $val = trim($val);
2146a6f13a4aSGreg Roach                        }
2147a6f13a4aSGreg Roach                        if ($val) {
21487a6ee1acSGreg Roach                            $searchstr = '';
21497a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2150a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2151a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2152a6f13a4aSGreg Roach                                $level = 1;
2153f71a7dedSGreg Roach                                $t = 'XXXX';
2154a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2155a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2156a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2157a6f13a4aSGreg Roach                                    }
2158a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2159044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21607a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2161a6f13a4aSGreg Roach                                    }
21627a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2163a6f13a4aSGreg Roach                                    $level++;
2164a6f13a4aSGreg Roach                                }
2165a6f13a4aSGreg Roach                            } else {
2166044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21677a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2168a6f13a4aSGreg Roach                                }
2169a6f13a4aSGreg Roach                                $t         = $tag;
21707a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2171a6f13a4aSGreg Roach                            }
2172a6f13a4aSGreg Roach                            switch ($expr) {
21737a6ee1acSGreg Roach                                case 'CONTAINS':
2174044416d2SGreg Roach                                    if ($t === 'PLAC') {
2175a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2176a6f13a4aSGreg Roach                                    } else {
2177a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2178a6f13a4aSGreg Roach                                    }
2179a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2180a6f13a4aSGreg Roach                                    break;
2181a6f13a4aSGreg Roach                                default:
2182c1010edaSGreg Roach                                    $filters2[] = [
2183c1010edaSGreg Roach                                        'tag'  => $tag,
2184c1010edaSGreg Roach                                        'expr' => $expr,
2185c1010edaSGreg Roach                                        'val'  => $val,
2186c1010edaSGreg Roach                                    ];
2187a6f13a4aSGreg Roach                                    break;
2188a6f13a4aSGreg Roach                            }
2189a6f13a4aSGreg Roach                        }
2190a6f13a4aSGreg Roach                    }
2191a6f13a4aSGreg Roach                }
2192a6f13a4aSGreg Roach            }
2193a6f13a4aSGreg Roach        }
2194a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2195a6f13a4aSGreg Roach        if ($filters) {
2196a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2197a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2198299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2199a6f13a4aSGreg Roach                        unset($this->list[$key]);
2200a6f13a4aSGreg Roach                        break;
2201a6f13a4aSGreg Roach                    }
2202a6f13a4aSGreg Roach                }
2203a6f13a4aSGreg Roach            }
2204a6f13a4aSGreg Roach        }
2205a6f13a4aSGreg Roach        if ($filters2) {
220613abd6f3SGreg Roach            $mylist = [];
2207a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2208c0935879SGreg Roach                $key  = $indi->xref();
2209299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2210a6f13a4aSGreg Roach                $keep = true;
2211a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2212a6f13a4aSGreg Roach                    if ($keep) {
2213a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2214a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2215a6f13a4aSGreg Roach                        $val  = $filter['val'];
2216b2448a1bSGreg Roach                        if ($val === "''") {
22177a6ee1acSGreg Roach                            $val = '';
2218a6f13a4aSGreg Roach                        }
22197a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2220a6f13a4aSGreg Roach                        $t    = end($tags);
22213d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2222a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2223044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
22247a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
22257a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2226a6f13a4aSGreg Roach                            $t    = end($tags);
2227b315f3e1SGreg Roach                            $v    = self::getSubRecord(1, $tag, $grec);
2228a6f13a4aSGreg Roach                        }
2229a6f13a4aSGreg Roach
2230a6f13a4aSGreg Roach                        switch ($expr) {
22317a6ee1acSGreg Roach                            case 'GTE':
2232044416d2SGreg Roach                                if ($t === 'DATE') {
2233a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2234a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2235a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2236a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2237a6f13a4aSGreg Roach                                    $keep = true;
2238a6f13a4aSGreg Roach                                }
2239a6f13a4aSGreg Roach                                break;
22407a6ee1acSGreg Roach                            case 'LTE':
2241044416d2SGreg Roach                                if ($t === 'DATE') {
2242a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2243a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2244a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2245a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2246a6f13a4aSGreg Roach                                    $keep = true;
2247a6f13a4aSGreg Roach                                }
2248a6f13a4aSGreg Roach                                break;
2249a6f13a4aSGreg Roach                            default:
2250a6f13a4aSGreg Roach                                if ($v == $val) {
2251a6f13a4aSGreg Roach                                    $keep = true;
2252a6f13a4aSGreg Roach                                } else {
2253a6f13a4aSGreg Roach                                    $keep = false;
2254a6f13a4aSGreg Roach                                }
2255a6f13a4aSGreg Roach                                break;
2256a6f13a4aSGreg Roach                        }
2257a6f13a4aSGreg Roach                    }
2258a6f13a4aSGreg Roach                }
2259a6f13a4aSGreg Roach                if ($keep) {
2260a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2261a6f13a4aSGreg Roach                }
2262a6f13a4aSGreg Roach            }
2263a6f13a4aSGreg Roach            $this->list = $mylist;
2264a6f13a4aSGreg Roach        }
2265a6f13a4aSGreg Roach
2266a6f13a4aSGreg Roach        switch ($sortby) {
2267a6f13a4aSGreg Roach            case 'NAME':
2268c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2269a6f13a4aSGreg Roach                break;
2270a6f13a4aSGreg Roach            case 'CHAN':
2271c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2272a6f13a4aSGreg Roach                break;
2273a6f13a4aSGreg Roach            case 'BIRT:DATE':
2274c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2275a6f13a4aSGreg Roach                break;
2276a6f13a4aSGreg Roach            case 'DEAT:DATE':
2277c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2278a6f13a4aSGreg Roach                break;
2279a6f13a4aSGreg Roach            case 'MARR:DATE':
2280c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2281a6f13a4aSGreg Roach                break;
2282a6f13a4aSGreg Roach            default:
2283a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2284a6f13a4aSGreg Roach                break;
2285a6f13a4aSGreg Roach        }
2286a6f13a4aSGreg Roach
22879b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2288e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2289a6f13a4aSGreg Roach    }
2290a6f13a4aSGreg Roach
2291a6f13a4aSGreg Roach    /**
2292fab8f067SGreg Roach     * Handle </list>
22938ba2e626SGreg Roach     *
22948ba2e626SGreg Roach     * @return void
2295a6f13a4aSGreg Roach     */
2296b702978eSGreg Roach    protected function listEndHandler(): void
2297c1010edaSGreg Roach    {
2298a6f13a4aSGreg Roach        $this->process_repeats--;
2299a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2300a6f13a4aSGreg Roach            return;
2301a6f13a4aSGreg Roach        }
2302a6f13a4aSGreg Roach
2303a6f13a4aSGreg Roach        // Check if there is any list
2304a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2305a6f13a4aSGreg Roach            $lineoffset = 0;
2306a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2307a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2308a6f13a4aSGreg Roach            }
2309a6f13a4aSGreg Roach            //-- read the xml from the file
2310299d100dSGreg Roach            $lines = file($this->report);
2311dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2312a6f13a4aSGreg Roach                $lineoffset--;
2313a6f13a4aSGreg Roach            }
2314a6f13a4aSGreg Roach            $lineoffset++;
2315a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2316a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2317a6f13a4aSGreg Roach            // List Level counter
2318a6f13a4aSGreg Roach            $count = 1;
2319a6f13a4aSGreg Roach            while (0 < $count) {
2320dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2321a6f13a4aSGreg Roach                    $count++;
2322dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2323a6f13a4aSGreg Roach                    $count--;
2324a6f13a4aSGreg Roach                }
2325a6f13a4aSGreg Roach                if (0 < $count) {
2326a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2327a6f13a4aSGreg Roach                }
2328a6f13a4aSGreg Roach                $line_nr++;
2329a6f13a4aSGreg Roach            }
2330a6f13a4aSGreg Roach            // No need to drag this
2331a6f13a4aSGreg Roach            unset($lines);
23327a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2333a6f13a4aSGreg Roach            // Save original values
23349b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2335a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2336a6f13a4aSGreg Roach
2337a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2338a6f13a4aSGreg Roach            $this->list_private = 0;
2339a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2340a6f13a4aSGreg Roach                if ($record->canShow()) {
2341f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2342a6f13a4aSGreg Roach                    //-- start the sax parser
2343a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2344e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2345a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23461aa04befSGreg Roach
23471aa04befSGreg Roach                    xml_set_element_handler(
23481aa04befSGreg Roach                        $repeat_parser,
23499d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23501aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23511aa04befSGreg Roach                        },
23529d454b6bSGreg Roach                        function ($parser, string $name): void {
23531aa04befSGreg Roach                            $this->endElement($parser, $name);
23541aa04befSGreg Roach                        }
23551aa04befSGreg Roach                    );
23561aa04befSGreg Roach
23571aa04befSGreg Roach                    xml_set_character_data_handler(
23581aa04befSGreg Roach                        $repeat_parser,
23599d454b6bSGreg Roach                        function ($parser, string $data): void {
23601aa04befSGreg Roach                            $this->characterData($parser, $data);
23611aa04befSGreg Roach                        }
23621aa04befSGreg Roach                    );
23631aa04befSGreg Roach
2364a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23656ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2366a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2367a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2368a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2369a6f13a4aSGreg Roach                        ));
2370a6f13a4aSGreg Roach                    }
2371a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2372a6f13a4aSGreg Roach                } else {
2373a6f13a4aSGreg Roach                    $this->list_private++;
2374a6f13a4aSGreg Roach                }
2375a6f13a4aSGreg Roach            }
237613abd6f3SGreg Roach            $this->list   = [];
2377e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2378a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2379a6f13a4aSGreg Roach        }
238065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2381a6f13a4aSGreg Roach    }
2382a6f13a4aSGreg Roach
2383a6f13a4aSGreg Roach    /**
2384fab8f067SGreg Roach     * Handle <listTotal>
2385a6f13a4aSGreg Roach     * Prints the total number of records in a list
2386fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23878ba2e626SGreg Roach     *
23888ba2e626SGreg Roach     * @return void
2389a6f13a4aSGreg Roach     */
2390b702978eSGreg Roach    protected function listTotalStartHandler(): void
2391c1010edaSGreg Roach    {
2392a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2393589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2394a6f13a4aSGreg Roach        } else {
23957a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2396a6f13a4aSGreg Roach        }
2397a6f13a4aSGreg Roach    }
2398a6f13a4aSGreg Roach
2399a6f13a4aSGreg Roach    /**
2400fab8f067SGreg Roach     * Handle <relatives>
240176692c8bSGreg Roach     *
240209482a55SGreg Roach     * @param array<string> $attrs
24038ba2e626SGreg Roach     *
24048ba2e626SGreg Roach     * @return void
2405a6f13a4aSGreg Roach     */
2406b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2407c1010edaSGreg Roach    {
2408a6f13a4aSGreg Roach        $this->process_repeats++;
2409a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2410a6f13a4aSGreg Roach            return;
2411a6f13a4aSGreg Roach        }
2412a6f13a4aSGreg Roach
2413e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2414e364afe4SGreg Roach
241513abd6f3SGreg Roach        $match = [];
2416a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2417d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2418a6f13a4aSGreg Roach            $sortby = trim($sortby);
2419a6f13a4aSGreg Roach        }
2420a6f13a4aSGreg Roach
2421a6f13a4aSGreg Roach        $maxgen = -1;
2422a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2423c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2424a6f13a4aSGreg Roach        }
2425a6f13a4aSGreg Roach
2426e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2427e364afe4SGreg Roach
2428a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2429d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2430a6f13a4aSGreg Roach            $group = trim($group);
2431a6f13a4aSGreg Roach        }
2432a6f13a4aSGreg Roach
2433e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2434e364afe4SGreg Roach
2435a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2436d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2437a6f13a4aSGreg Roach            $id = trim($id);
2438a6f13a4aSGreg Roach        }
2439a6f13a4aSGreg Roach
244013abd6f3SGreg Roach        $this->list = [];
24416b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2442d965cc1aSGreg Roach        if ($person instanceof Individual) {
2443a6f13a4aSGreg Roach            $this->list[$id] = $person;
2444a6f13a4aSGreg Roach            switch ($group) {
24457a6ee1acSGreg Roach                case 'child-family':
244639ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2447820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2448820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2449a6f13a4aSGreg Roach                        }
2450820b62dfSGreg Roach
2451820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2452c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2453a6f13a4aSGreg Roach                        }
2454a6f13a4aSGreg Roach                    }
2455a6f13a4aSGreg Roach                    break;
24567a6ee1acSGreg Roach                case 'spouse-family':
245739ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2458820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2459820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2460a6f13a4aSGreg Roach                        }
2461820b62dfSGreg Roach
2462820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2463c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2464a6f13a4aSGreg Roach                        }
2465a6f13a4aSGreg Roach                    }
2466a6f13a4aSGreg Roach                    break;
24677a6ee1acSGreg Roach                case 'direct-ancestors':
24683d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2469a6f13a4aSGreg Roach                    break;
24707a6ee1acSGreg Roach                case 'ancestors':
24713d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2472a6f13a4aSGreg Roach                    break;
24737a6ee1acSGreg Roach                case 'descendants':
2474a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24753d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2476a6f13a4aSGreg Roach                    break;
24777a6ee1acSGreg Roach                case 'all':
24783d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24793d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2480a6f13a4aSGreg Roach                    break;
2481a6f13a4aSGreg Roach            }
2482a6f13a4aSGreg Roach        }
2483a6f13a4aSGreg Roach
2484a6f13a4aSGreg Roach        switch ($sortby) {
2485a6f13a4aSGreg Roach            case 'NAME':
2486c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2487a6f13a4aSGreg Roach                break;
2488a6f13a4aSGreg Roach            case 'BIRT:DATE':
2489c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2490a6f13a4aSGreg Roach                break;
2491a6f13a4aSGreg Roach            case 'DEAT:DATE':
2492c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2493a6f13a4aSGreg Roach                break;
2494a6f13a4aSGreg Roach            case 'generation':
249513abd6f3SGreg Roach                $newarray = [];
2496a6f13a4aSGreg Roach                reset($this->list);
2497a6f13a4aSGreg Roach                $genCounter = 1;
2498a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2499a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2500a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2501a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2502f70bcff5SGreg Roach                            $newarray[$key] = (object) ['generation' => $this->generation];
2503a6f13a4aSGreg Roach                        }
2504a6f13a4aSGreg Roach                    }
2505a6f13a4aSGreg Roach                    $genCounter++;
2506a6f13a4aSGreg Roach                }
2507a6f13a4aSGreg Roach                $this->list = $newarray;
2508a6f13a4aSGreg Roach                break;
2509a6f13a4aSGreg Roach            default:
2510a6f13a4aSGreg Roach                // unsorted
2511a6f13a4aSGreg Roach                break;
2512a6f13a4aSGreg Roach        }
25139b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2514e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2515a6f13a4aSGreg Roach    }
2516a6f13a4aSGreg Roach
2517a6f13a4aSGreg Roach    /**
2518fab8f067SGreg Roach     * Handle </relatives>
25198ba2e626SGreg Roach     *
25208ba2e626SGreg Roach     * @return void
2521a6f13a4aSGreg Roach     */
2522b702978eSGreg Roach    protected function relativesEndHandler(): void
2523c1010edaSGreg Roach    {
2524a6f13a4aSGreg Roach        $this->process_repeats--;
2525a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2526a6f13a4aSGreg Roach            return;
2527a6f13a4aSGreg Roach        }
2528a6f13a4aSGreg Roach
2529a6f13a4aSGreg Roach        // Check if there is any relatives
2530a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2531a6f13a4aSGreg Roach            $lineoffset = 0;
2532a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2533a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2534a6f13a4aSGreg Roach            }
2535a6f13a4aSGreg Roach            //-- read the xml from the file
2536299d100dSGreg Roach            $lines = file($this->report);
2537dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2538a6f13a4aSGreg Roach                $lineoffset--;
2539a6f13a4aSGreg Roach            }
2540a6f13a4aSGreg Roach            $lineoffset++;
2541a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2542a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2543a6f13a4aSGreg Roach            // Relatives Level counter
2544a6f13a4aSGreg Roach            $count = 1;
2545a6f13a4aSGreg Roach            while (0 < $count) {
2546dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2547a6f13a4aSGreg Roach                    $count++;
2548dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2549a6f13a4aSGreg Roach                    $count--;
2550a6f13a4aSGreg Roach                }
2551a6f13a4aSGreg Roach                if (0 < $count) {
2552a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2553a6f13a4aSGreg Roach                }
2554a6f13a4aSGreg Roach                $line_nr++;
2555a6f13a4aSGreg Roach            }
2556a6f13a4aSGreg Roach            // No need to drag this
2557a6f13a4aSGreg Roach            unset($lines);
2558a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2559a6f13a4aSGreg Roach            // Save original values
25609b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2561a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2562a6f13a4aSGreg Roach
2563a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2564a6f13a4aSGreg Roach            $this->list_private = 0;
2565b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2566a6f13a4aSGreg Roach                if (isset($value->generation)) {
2567a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2568a6f13a4aSGreg Roach                }
25696b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2570299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2571a6f13a4aSGreg Roach
2572a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2573e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2574a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25751aa04befSGreg Roach
25761aa04befSGreg Roach                xml_set_element_handler(
25771aa04befSGreg Roach                    $repeat_parser,
25789d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25791aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25801aa04befSGreg Roach                    },
25819d454b6bSGreg Roach                    function ($parser, string $name): void {
25821aa04befSGreg Roach                        $this->endElement($parser, $name);
25831aa04befSGreg Roach                    }
25841aa04befSGreg Roach                );
25851aa04befSGreg Roach
25861aa04befSGreg Roach                xml_set_character_data_handler(
25871aa04befSGreg Roach                    $repeat_parser,
25889d454b6bSGreg Roach                    function ($parser, string $data): void {
25891aa04befSGreg Roach                        $this->characterData($parser, $data);
25901aa04befSGreg Roach                    }
25911aa04befSGreg Roach                );
2592a6f13a4aSGreg Roach
2593a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25946ccdf4f0SGreg 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)));
2595a6f13a4aSGreg Roach                }
2596a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2597a6f13a4aSGreg Roach            }
2598a6f13a4aSGreg Roach            // Clean up the list array
259913abd6f3SGreg Roach            $this->list   = [];
2600e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2601a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2602a6f13a4aSGreg Roach        }
260365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2604a6f13a4aSGreg Roach    }
2605a6f13a4aSGreg Roach
2606a6f13a4aSGreg Roach    /**
2607fab8f067SGreg Roach     * Handle <generation />
2608a6f13a4aSGreg Roach     * Prints the number of generations
26098ba2e626SGreg Roach     *
26108ba2e626SGreg Roach     * @return void
2611a6f13a4aSGreg Roach     */
2612b702978eSGreg Roach    protected function generationStartHandler(): void
2613c1010edaSGreg Roach    {
2614589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2615a6f13a4aSGreg Roach    }
2616a6f13a4aSGreg Roach
2617a6f13a4aSGreg Roach    /**
2618fab8f067SGreg Roach     * Handle <newPage />
2619a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
26208ba2e626SGreg Roach     *
26218ba2e626SGreg Roach     * @return void
2622a6f13a4aSGreg Roach     */
2623b702978eSGreg Roach    protected function newPageStartHandler(): void
2624c1010edaSGreg Roach    {
26257a6ee1acSGreg Roach        $temp = 'addpage';
2626e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2627a6f13a4aSGreg Roach    }
2628a6f13a4aSGreg Roach
2629a6f13a4aSGreg Roach    /**
2630fab8f067SGreg Roach     * Handle </title>
26318ba2e626SGreg Roach     *
26328ba2e626SGreg Roach     * @return void
2633a6f13a4aSGreg Roach     */
2634b702978eSGreg Roach    protected function titleEndHandler(): void
2635c1010edaSGreg Roach    {
26362836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2637a6f13a4aSGreg Roach    }
2638a6f13a4aSGreg Roach
2639a6f13a4aSGreg Roach    /**
2640fab8f067SGreg Roach     * Handle </description>
26418ba2e626SGreg Roach     *
26428ba2e626SGreg Roach     * @return void
2643a6f13a4aSGreg Roach     */
2644b702978eSGreg Roach    protected function descriptionEndHandler(): void
2645c1010edaSGreg Roach    {
26462836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2647a6f13a4aSGreg Roach    }
2648729ce104SGreg Roach
2649729ce104SGreg Roach    /**
265076692c8bSGreg Roach     * Create a list of all descendants.
265176692c8bSGreg Roach     *
26521c7df322SGreg Roach     * @param array<Individual> $list
2653729ce104SGreg Roach     * @param string            $pid
2654729ce104SGreg Roach     * @param bool              $parents
2655729ce104SGreg Roach     * @param int               $generations
26568ba2e626SGreg Roach     *
26578ba2e626SGreg Roach     * @return void
2658729ce104SGreg Roach     */
26593b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2660c1010edaSGreg Roach    {
26616b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2662729ce104SGreg Roach        if ($person === null) {
2663729ce104SGreg Roach            return;
2664729ce104SGreg Roach        }
2665729ce104SGreg Roach        if (!isset($list[$pid])) {
2666729ce104SGreg Roach            $list[$pid] = $person;
2667729ce104SGreg Roach        }
2668729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2669729ce104SGreg Roach            $list[$pid]->generation = 0;
2670729ce104SGreg Roach        }
267139ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2672729ce104SGreg Roach            if ($parents) {
267339ca88baSGreg Roach                $husband = $family->husband();
267439ca88baSGreg Roach                $wife    = $family->wife();
2675729ce104SGreg Roach                if ($husband) {
2676c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2677729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2678c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2679729ce104SGreg Roach                    } else {
2680c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2681729ce104SGreg Roach                    }
2682729ce104SGreg Roach                }
2683729ce104SGreg Roach                if ($wife) {
2684c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2685729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2686c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2687729ce104SGreg Roach                    } else {
2688c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2689729ce104SGreg Roach                    }
2690729ce104SGreg Roach                }
2691729ce104SGreg Roach            }
2692820b62dfSGreg Roach
269339ca88baSGreg Roach            $children = $family->children();
2694820b62dfSGreg Roach
2695729ce104SGreg Roach            foreach ($children as $child) {
2696729ce104SGreg Roach                if ($child) {
2697c0935879SGreg Roach                    $list[$child->xref()] = $child;
2698820b62dfSGreg Roach
2699729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2700c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2701729ce104SGreg Roach                    } else {
2702c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2703729ce104SGreg Roach                    }
2704729ce104SGreg Roach                }
2705729ce104SGreg Roach            }
2706729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2707729ce104SGreg Roach                foreach ($children as $child) {
2708c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2709729ce104SGreg Roach                }
2710729ce104SGreg Roach            }
2711729ce104SGreg Roach        }
2712729ce104SGreg Roach    }
2713729ce104SGreg Roach
2714729ce104SGreg Roach    /**
271576692c8bSGreg Roach     * Create a list of all ancestors.
271676692c8bSGreg Roach     *
27171c7df322SGreg Roach     * @param array<Individual> $list
2718729ce104SGreg Roach     * @param string            $pid
2719729ce104SGreg Roach     * @param bool              $children
2720729ce104SGreg Roach     * @param int               $generations
27218ba2e626SGreg Roach     *
27228ba2e626SGreg Roach     * @return void
2723729ce104SGreg Roach     */
27243b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2725c1010edaSGreg Roach    {
272613abd6f3SGreg Roach        $genlist                = [$pid];
2727729ce104SGreg Roach        $list[$pid]->generation = 1;
2728729ce104SGreg Roach        while (count($genlist) > 0) {
2729729ce104SGreg Roach            $id = array_shift($genlist);
2730dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2731729ce104SGreg Roach                continue; // id can be something like “empty7”
2732729ce104SGreg Roach            }
27336b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
273439ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
273539ca88baSGreg Roach                $husband = $family->husband();
273639ca88baSGreg Roach                $wife    = $family->wife();
2737729ce104SGreg Roach                if ($husband) {
2738c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2739c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2740729ce104SGreg Roach                }
2741729ce104SGreg Roach                if ($wife) {
2742c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2743c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2744729ce104SGreg Roach                }
2745729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2746729ce104SGreg Roach                    if ($husband) {
2747c0935879SGreg Roach                        $genlist[] = $husband->xref();
2748729ce104SGreg Roach                    }
2749729ce104SGreg Roach                    if ($wife) {
2750c0935879SGreg Roach                        $genlist[] = $wife->xref();
2751729ce104SGreg Roach                    }
2752729ce104SGreg Roach                }
2753729ce104SGreg Roach                if ($children) {
275439ca88baSGreg Roach                    foreach ($family->children() as $child) {
2755c0935879SGreg Roach                        $list[$child->xref()] = $child;
2756e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2757729ce104SGreg Roach                    }
2758729ce104SGreg Roach                }
2759729ce104SGreg Roach            }
2760729ce104SGreg Roach        }
2761729ce104SGreg Roach    }
2762729ce104SGreg Roach
2763729ce104SGreg Roach    /**
2764729ce104SGreg Roach     * get gedcom tag value
2765729ce104SGreg Roach     *
2766729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2767729ce104SGreg 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
2768729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2769729ce104SGreg Roach     *
2770729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2771729ce104SGreg Roach     */
2772b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2773c1010edaSGreg Roach    {
2774b2448a1bSGreg Roach        if ($gedrec === '') {
2775729ce104SGreg Roach            return '';
2776729ce104SGreg Roach        }
2777729ce104SGreg Roach        $tags      = explode(':', $tag);
2778729ce104SGreg Roach        $origlevel = $level;
2779b2448a1bSGreg Roach        if ($level === 0) {
27803c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2781729ce104SGreg Roach        }
2782729ce104SGreg Roach
2783729ce104SGreg Roach        $subrec = $gedrec;
2784f71a7dedSGreg Roach        $t = 'XXXX';
2785729ce104SGreg Roach        foreach ($tags as $t) {
2786729ce104SGreg Roach            $lastsubrec = $subrec;
2787b315f3e1SGreg Roach            $subrec     = self::getSubRecord($level, "$level $t", $subrec);
2788729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2789729ce104SGreg Roach                $level--;
2790b315f3e1SGreg Roach                $subrec = self::getSubRecord($level, "$level $t", $lastsubrec);
2791729ce104SGreg Roach            }
2792729ce104SGreg Roach            if (empty($subrec)) {
2793044416d2SGreg Roach                if ($t === 'TITL') {
2794b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "$level ABBR", $lastsubrec);
2795729ce104SGreg Roach                    if (!empty($subrec)) {
27967a6ee1acSGreg Roach                        $t = 'ABBR';
2797729ce104SGreg Roach                    }
2798729ce104SGreg Roach                }
2799b2448a1bSGreg Roach                if ($subrec === '') {
2800729ce104SGreg Roach                    if ($level > 0) {
2801729ce104SGreg Roach                        $level--;
2802729ce104SGreg Roach                    }
2803b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "@ $t", $gedrec);
2804b2448a1bSGreg Roach                    if ($subrec === '') {
2805729ce104SGreg Roach                        return '';
2806729ce104SGreg Roach                    }
2807729ce104SGreg Roach                }
2808729ce104SGreg Roach            }
2809729ce104SGreg Roach            $level++;
2810729ce104SGreg Roach        }
2811729ce104SGreg Roach        $level--;
2812729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2813f71a7dedSGreg Roach        if ($ct === 0) {
2814729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2815729ce104SGreg Roach        }
2816f71a7dedSGreg Roach        if ($ct === 0) {
2817729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2818729ce104SGreg Roach        }
2819729ce104SGreg Roach        if ($ct > 0) {
2820729ce104SGreg Roach            $value = trim($match[1]);
2821044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
28226b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2823ff166e64SGreg Roach                if ($note instanceof Note) {
2824729ce104SGreg Roach                    $value = $note->getNote();
2825729ce104SGreg Roach                } else {
2826729ce104SGreg Roach                    //-- set the value to the id without the @
2827729ce104SGreg Roach                    $value = $match[1];
2828729ce104SGreg Roach                }
2829729ce104SGreg Roach            }
2830f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
2831b315f3e1SGreg Roach                $value .= self::getCont($level + 1, $subrec);
2832729ce104SGreg Roach            }
2833729ce104SGreg Roach
2834729ce104SGreg Roach            return $value;
2835729ce104SGreg Roach        }
2836729ce104SGreg Roach
28377a6ee1acSGreg Roach        return '';
2838729ce104SGreg Roach    }
2839d1286247SGreg Roach
2840d1286247SGreg Roach    /**
2841d1286247SGreg Roach     * Replace variable identifiers with their values.
2842d1286247SGreg Roach     *
2843d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
284482759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2845d1286247SGreg Roach     *
2846d1286247SGreg Roach     * @return string
2847d1286247SGreg Roach     */
28488f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2849c1010edaSGreg Roach    {
2850d1286247SGreg Roach        return preg_replace_callback(
2851d1286247SGreg Roach            '/\$(\w+)/',
285218d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28532118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
285482759250SGreg Roach                    if ($quote) {
28552118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2856b2ce94c6SRico Sonntag                    }
2857b2ce94c6SRico Sonntag
28582118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
285982759250SGreg Roach                }
2860b2ce94c6SRico Sonntag
2861d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28623d7a8a4cSGreg Roach
2863d1286247SGreg Roach                return '$' . $matches[1];
2864d1286247SGreg Roach            },
2865d1286247SGreg Roach            $expression
2866d1286247SGreg Roach        );
2867d1286247SGreg Roach    }
2868a6f13a4aSGreg Roach}
2869