xref: /webtrees/app/Report/ReportParserGenerate.php (revision 059898c926a4478bc49a5a58f1ac1c58d0ea796d)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a6f13a4aSGreg Roach * This program is free software: you can redistribute it and/or modify
7a6f13a4aSGreg Roach * it under the terms of the GNU General Public License as published by
8a6f13a4aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a6f13a4aSGreg Roach * (at your option) any later version.
10a6f13a4aSGreg Roach * This program is distributed in the hope that it will be useful,
11a6f13a4aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a6f13a4aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a6f13a4aSGreg Roach * GNU General Public License for more details.
14a6f13a4aSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a6f13a4aSGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
226ccdf4f0SGreg Roachuse DomainException;
23a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
24a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
2597def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
26ab2c9152SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
289a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
32d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
33a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
34729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
35a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
36d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
37299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
38195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
39195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
40a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
415985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
429a9e551aSGreg Roachuse Illuminate\Support\Str;
43f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
4431d9777aSGreg Roachuse LogicException;
45c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
465809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
474e69366aSGreg Roachuse XMLParser;
4859597b37SGreg Roach
49b6f35a76SGreg Roachuse function addcslashes;
50b6f35a76SGreg Roachuse function addslashes;
51b6f35a76SGreg Roachuse function array_pop;
52b6f35a76SGreg Roachuse function array_shift;
53b6f35a76SGreg Roachuse function assert;
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{
100f315390bSGreg Roach    /** Are we collecting data from <Footnote> elements */
101f315390bSGreg Roach    private bool $process_footnote = true;
102a6f13a4aSGreg Roach
103f315390bSGreg Roach    /** Are we currently outputing data? */
104f315390bSGreg Roach    private bool $print_data = false;
105a6f13a4aSGreg Roach
106f315390bSGreg Roach    /** @var array<int,bool> Push-down stack of $print_data */
107f315390bSGreg Roach    private array $print_data_stack = [];
108a6f13a4aSGreg Roach
109f315390bSGreg Roach    /** Are we processing GEDCOM data */
110f315390bSGreg Roach    private int $process_gedcoms = 0;
111a6f13a4aSGreg Roach
112f315390bSGreg Roach    /** Are we processing conditionals */
113f315390bSGreg Roach    private int $process_ifs = 0;
114a6f13a4aSGreg Roach
115f315390bSGreg Roach    /** Are we processing repeats */
116f315390bSGreg Roach    private int $process_repeats = 0;
117a6f13a4aSGreg Roach
118f315390bSGreg Roach    /** Quantity of data to repeat during loops */
119f315390bSGreg Roach    private int $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
130b112142eSGreg Roach    // Nested repeating data
131b112142eSGreg Roach    private XMLParser $parser;
132e8e7866bSGreg Roach
1334e69366aSGreg Roach    /** @var XMLParser[] (resource[] before PHP 8.0) Nested repeating data */
1344e69366aSGreg Roach    private array $parser_stack = [];
135e8e7866bSGreg Roach
136f315390bSGreg Roach    /** The current GEDCOM record */
137f315390bSGreg Roach    private string $gedrec = '';
138a6f13a4aSGreg Roach
139f315390bSGreg Roach    /** @var array<int,array<int,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
148f315390bSGreg Roach    /** The GEDCOM fact currently being processed */
149f315390bSGreg Roach    private string $fact = '';
150a6f13a4aSGreg Roach
151f315390bSGreg Roach    /** The GEDCOM value currently being processed */
152f315390bSGreg Roach    private string $desc = '';
153a6f13a4aSGreg Roach
154f315390bSGreg Roach    /** The GEDCOM type currently being processed */
155f315390bSGreg Roach    private string $type = '';
156a6f13a4aSGreg Roach
157f315390bSGreg Roach    /** The current generational level */
158f315390bSGreg Roach    private int $generation = 1;
159a6f13a4aSGreg Roach
16076d39c55SGreg Roach    /** @var array<static|GedcomRecord> Source data for processing lists */
1614e69366aSGreg Roach    private array $list = [];
162a6f13a4aSGreg Roach
163f315390bSGreg Roach    /** Number of items in lists */
164f315390bSGreg Roach    private int $list_total = 0;
165a6f13a4aSGreg Roach
166f315390bSGreg Roach    /** Number of items filtered from lists */
167f315390bSGreg Roach    private int $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
18376692c8bSGreg Roach    /**
18476692c8bSGreg Roach     * Create a parser for a report
18576692c8bSGreg Roach     *
18676692c8bSGreg Roach     * @param string               $report The XML filename
187b6f35a76SGreg Roach     * @param AbstractRenderer     $report_root
18809482a55SGreg Roach     * @param array<array<string>> $vars
189299d100dSGreg Roach     * @param Tree                 $tree
19076692c8bSGreg Roach     */
1919458f20aSGreg Roach    public function __construct(string $report, AbstractRenderer $report_root, array $vars, Tree $tree)
1929458f20aSGreg Roach    {
193299d100dSGreg Roach        $this->report          = $report;
194e8e7866bSGreg Roach        $this->report_root     = $report_root;
195e8e7866bSGreg Roach        $this->wt_report       = $report_root;
19659f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
197d1286247SGreg Roach        $this->vars            = $vars;
198299d100dSGreg Roach        $this->tree            = $tree;
199299d100dSGreg Roach
20076f666f4SGreg Roach        parent::__construct($report);
201a6f13a4aSGreg Roach    }
202a6f13a4aSGreg Roach
203a6f13a4aSGreg Roach    /**
204b315f3e1SGreg Roach     * get a gedcom subrecord
205b315f3e1SGreg Roach     *
206b315f3e1SGreg Roach     * searches a gedcom record and returns a subrecord of it. A subrecord is defined starting at a
207b315f3e1SGreg Roach     * line with level N and all subsequent lines greater than N until the next N level is reached.
208b315f3e1SGreg Roach     * For example, the following is a BIRT subrecord:
209b315f3e1SGreg Roach     * <code>1 BIRT
210b315f3e1SGreg Roach     * 2 DATE 1 JAN 1900
211b315f3e1SGreg Roach     * 2 PLAC Phoenix, Maricopa, Arizona</code>
212b315f3e1SGreg Roach     * The following example is the DATE subrecord of the above BIRT subrecord:
213b315f3e1SGreg Roach     * <code>2 DATE 1 JAN 1900</code>
214b315f3e1SGreg Roach     *
215b315f3e1SGreg Roach     * @param int    $level   the N level of the subrecord to get
216b315f3e1SGreg Roach     * @param string $tag     a gedcom tag or string to search for in the record (ie 1 BIRT or 2 DATE)
217b315f3e1SGreg Roach     * @param string $gedrec  the parent gedcom record to search in
218b315f3e1SGreg Roach     * @param int    $num     this allows you to specify which matching <var>$tag</var> to get. Oftentimes a
219b315f3e1SGreg Roach     *                        gedcom record will have more that 1 of the same type of subrecord. An individual may have
220b315f3e1SGreg Roach     *                        multiple events for example. Passing $num=1 would get the first 1. Passing $num=2 would get the
221b315f3e1SGreg Roach     *                        second one, etc.
222b315f3e1SGreg Roach     *
223b315f3e1SGreg Roach     * @return string the subrecord that was found or an empty string "" if not found.
224b315f3e1SGreg Roach     */
225b315f3e1SGreg Roach    public static function getSubRecord(int $level, string $tag, string $gedrec, int $num = 1): string
226b315f3e1SGreg Roach    {
227b315f3e1SGreg Roach        if ($gedrec === '') {
228b315f3e1SGreg Roach            return '';
229b315f3e1SGreg Roach        }
230b315f3e1SGreg Roach        // -- adding \n before and after gedrec
231b315f3e1SGreg Roach        $gedrec       = "\n" . $gedrec . "\n";
232b315f3e1SGreg Roach        $tag          = trim($tag);
233b315f3e1SGreg Roach        $searchTarget = "~[\n]" . $tag . "[\s]~";
234b315f3e1SGreg Roach        $ct           = preg_match_all($searchTarget, $gedrec, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
235b315f3e1SGreg Roach        if ($ct === 0) {
236b315f3e1SGreg Roach            return '';
237b315f3e1SGreg Roach        }
238b315f3e1SGreg Roach        if ($ct < $num) {
239b315f3e1SGreg Roach            return '';
240b315f3e1SGreg Roach        }
241b315f3e1SGreg Roach        $pos1 = $match[$num - 1][0][1];
242b315f3e1SGreg Roach        $pos2 = strpos($gedrec, "\n$level", $pos1 + 1);
243b315f3e1SGreg Roach        if (!$pos2) {
244b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\n1", $pos1 + 1);
245b315f3e1SGreg Roach        }
246b315f3e1SGreg Roach        if (!$pos2) {
247b315f3e1SGreg Roach            $pos2 = strpos($gedrec, "\nWT_", $pos1 + 1); // WT_SPOUSE, WT_FAMILY_ID ...
248b315f3e1SGreg Roach        }
249b315f3e1SGreg Roach        if (!$pos2) {
250b315f3e1SGreg Roach            return ltrim(substr($gedrec, $pos1));
251b315f3e1SGreg Roach        }
252b315f3e1SGreg Roach        $subrec = substr($gedrec, $pos1, $pos2 - $pos1);
253b315f3e1SGreg Roach
254b315f3e1SGreg Roach        return ltrim($subrec);
255b315f3e1SGreg Roach    }
256b315f3e1SGreg Roach
257b315f3e1SGreg Roach    /**
258b315f3e1SGreg Roach     * get CONT lines
259b315f3e1SGreg Roach     *
260b315f3e1SGreg Roach     * get the N+1 CONT or CONC lines of a gedcom subrecord
261b315f3e1SGreg Roach     *
262b315f3e1SGreg Roach     * @param int    $nlevel the level of the CONT lines to get
263b315f3e1SGreg Roach     * @param string $nrec   the gedcom subrecord to search in
264b315f3e1SGreg Roach     *
265b315f3e1SGreg Roach     * @return string a string with all CONT lines merged
266b315f3e1SGreg Roach     */
267b315f3e1SGreg Roach    public static function getCont(int $nlevel, string $nrec): string
268b315f3e1SGreg Roach    {
269b315f3e1SGreg Roach        $text = '';
270b315f3e1SGreg Roach
271b315f3e1SGreg Roach        $subrecords = explode("\n", $nrec);
272b315f3e1SGreg Roach        foreach ($subrecords as $thisSubrecord) {
273b315f3e1SGreg Roach            if (substr($thisSubrecord, 0, 2) !== $nlevel . ' ') {
274b315f3e1SGreg Roach                continue;
275b315f3e1SGreg Roach            }
276b315f3e1SGreg Roach            $subrecordType = substr($thisSubrecord, 2, 4);
277b315f3e1SGreg Roach            if ($subrecordType === 'CONT') {
278b315f3e1SGreg Roach                $text .= "\n" . substr($thisSubrecord, 7);
279b315f3e1SGreg Roach            }
280b315f3e1SGreg Roach        }
281b315f3e1SGreg Roach
282b315f3e1SGreg Roach        return $text;
283b315f3e1SGreg Roach    }
284b315f3e1SGreg Roach
285b315f3e1SGreg Roach    /**
286a6f13a4aSGreg Roach     * XML start element handler
287a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
288a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
289a6f13a4aSGreg Roach     *
290a6f13a4aSGreg Roach     * @param resource      $parser the resource handler for the XML parser
291a6f13a4aSGreg Roach     * @param string        $name   the name of the XML element parsed
29209482a55SGreg Roach     * @param array<string> $attrs  an array of key value pairs for the attributes
29318d7a90dSGreg Roach     *
29418d7a90dSGreg Roach     * @return void
295a6f13a4aSGreg Roach     */
296af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
297c1010edaSGreg Roach    {
29813abd6f3SGreg Roach        $newattrs = [];
299a6f13a4aSGreg Roach
300a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
301a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
302e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
303d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
304a6f13a4aSGreg Roach                }
305a6f13a4aSGreg Roach            }
306a6f13a4aSGreg Roach            $newattrs[$key] = $value;
307a6f13a4aSGreg Roach        }
308a6f13a4aSGreg Roach        $attrs = $newattrs;
3097a6ee1acSGreg 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')) {
310a0e2939aSGreg Roach            $method = $name . 'StartHandler';
311208e9f76SGreg Roach
312a0e2939aSGreg Roach            if (method_exists($this, $method)) {
313f315390bSGreg Roach                $this->{$method}($attrs);
314a6f13a4aSGreg Roach            }
315a6f13a4aSGreg Roach        }
316a6f13a4aSGreg Roach    }
317a6f13a4aSGreg Roach
318a6f13a4aSGreg Roach    /**
319a6f13a4aSGreg Roach     * XML end element handler
320a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
321a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
322a6f13a4aSGreg Roach     *
323a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
324a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
32518d7a90dSGreg Roach     *
32618d7a90dSGreg Roach     * @return void
327a6f13a4aSGreg Roach     */
328af14d238SGreg Roach    protected function endElement($parser, string $name): void
329c1010edaSGreg Roach    {
3307a6ee1acSGreg 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')) {
331a0e2939aSGreg Roach            $method = $name . 'EndHandler';
332a0e2939aSGreg Roach
333a0e2939aSGreg Roach            if (method_exists($this, $method)) {
334f315390bSGreg Roach                $this->{$method}();
335a6f13a4aSGreg Roach            }
336a6f13a4aSGreg Roach        }
337a6f13a4aSGreg Roach    }
338a6f13a4aSGreg Roach
339a6f13a4aSGreg Roach    /**
340a6f13a4aSGreg Roach     * XML character data handler
341a6f13a4aSGreg Roach     *
342a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
343a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
34418d7a90dSGreg Roach     *
34518d7a90dSGreg Roach     * @return void
346a6f13a4aSGreg Roach     */
34724f2a3afSGreg Roach    protected function characterData($parser, string $data): void
348c1010edaSGreg Roach    {
349e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
350a6f13a4aSGreg Roach            $this->current_element->addText($data);
351a6f13a4aSGreg Roach        }
352a6f13a4aSGreg Roach    }
353a6f13a4aSGreg Roach
354a6f13a4aSGreg Roach    /**
355fab8f067SGreg Roach     * Handle <style>
356a6f13a4aSGreg Roach     *
35709482a55SGreg Roach     * @param array<string> $attrs
3588ba2e626SGreg Roach     *
3598ba2e626SGreg Roach     * @return void
360a6f13a4aSGreg Roach     */
361b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
362c1010edaSGreg Roach    {
363a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
3646ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
365a6f13a4aSGreg Roach        }
366a6f13a4aSGreg Roach
367f315390bSGreg Roach        $style = [
368f315390bSGreg Roach            'name'  => $attrs['name'],
369f315390bSGreg Roach            'font'  => $attrs['font'] ?? $this->wt_report->default_font,
370f315390bSGreg Roach            'size'  => (float) ($attrs['size'] ?? $this->wt_report->default_font_size),
371f315390bSGreg Roach            'style' => $attrs['style'] ?? '',
372f315390bSGreg Roach        ];
373a6f13a4aSGreg Roach
374f315390bSGreg Roach        $this->wt_report->addStyle($style);
375a6f13a4aSGreg Roach    }
376a6f13a4aSGreg Roach
377a6f13a4aSGreg Roach    /**
378fab8f067SGreg Roach     * Handle <doc>
379a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
380a6f13a4aSGreg Roach     *
38109482a55SGreg Roach     * @param array<string> $attrs
3828ba2e626SGreg Roach     *
3838ba2e626SGreg Roach     * @return void
384a6f13a4aSGreg Roach     */
385b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
386c1010edaSGreg Roach    {
387e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
388a6f13a4aSGreg Roach
389a6f13a4aSGreg Roach        // Custom page width
390a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
391f315390bSGreg Roach            $this->wt_report->page_width = (float) $attrs['customwidth'];
392f315390bSGreg Roach        }
393a6f13a4aSGreg Roach        // Custom Page height
394a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
395f315390bSGreg Roach            $this->wt_report->page_height = (float) $attrs['customheight'];
396f315390bSGreg Roach        }
397a6f13a4aSGreg Roach
398a6f13a4aSGreg Roach        // Left Margin
399a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
4007a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
401208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
402a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
403f315390bSGreg Roach                $this->wt_report->left_margin = (float) $attrs['leftmargin'];
404a6f13a4aSGreg Roach            }
405a6f13a4aSGreg Roach        }
406a6f13a4aSGreg Roach        // Right Margin
407a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
4087a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
409208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
410a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
411f315390bSGreg Roach                $this->wt_report->right_margin = (float) $attrs['rightmargin'];
412a6f13a4aSGreg Roach            }
413a6f13a4aSGreg Roach        }
414a6f13a4aSGreg Roach        // Top Margin
415a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
4167a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
417208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
418a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
419f315390bSGreg Roach                $this->wt_report->top_margin = (float) $attrs['topmargin'];
420a6f13a4aSGreg Roach            }
421a6f13a4aSGreg Roach        }
422a6f13a4aSGreg Roach        // Bottom Margin
423a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
4247a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
425208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
426a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
427f315390bSGreg Roach                $this->wt_report->bottom_margin = (float) $attrs['bottommargin'];
428a6f13a4aSGreg Roach            }
429a6f13a4aSGreg Roach        }
430a6f13a4aSGreg Roach        // Header Margin
431a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
4327a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
433208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
434a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
435f315390bSGreg Roach                $this->wt_report->header_margin = (float) $attrs['headermargin'];
436a6f13a4aSGreg Roach            }
437a6f13a4aSGreg Roach        }
438a6f13a4aSGreg Roach        // Footer Margin
439a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
4407a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
441208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
442a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
443f315390bSGreg Roach                $this->wt_report->footer_margin = (float) $attrs['footermargin'];
444a6f13a4aSGreg Roach            }
445a6f13a4aSGreg Roach        }
446a6f13a4aSGreg Roach
447a6f13a4aSGreg Roach        // Page Orientation
448a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
449044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4507a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
451044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4527a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
453a6f13a4aSGreg Roach            }
454a6f13a4aSGreg Roach        }
455a6f13a4aSGreg Roach        // Page Size
456a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
457208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
458a6f13a4aSGreg Roach        }
459a6f13a4aSGreg Roach
460a6f13a4aSGreg Roach        // Show Generated By...
461a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4627a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
463208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4647a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
465208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
466a6f13a4aSGreg Roach            }
467a6f13a4aSGreg Roach        }
468a6f13a4aSGreg Roach
469e8e7866bSGreg Roach        $this->wt_report->setup();
470a6f13a4aSGreg Roach    }
471a6f13a4aSGreg Roach
472a6f13a4aSGreg Roach    /**
473fab8f067SGreg Roach     * Handle </doc>
4748ba2e626SGreg Roach     *
4758ba2e626SGreg Roach     * @return void
476a6f13a4aSGreg Roach     */
477b702978eSGreg Roach    protected function docEndHandler(): void
478c1010edaSGreg Roach    {
479e8e7866bSGreg Roach        $this->wt_report->run();
480a6f13a4aSGreg Roach    }
481a6f13a4aSGreg Roach
482a6f13a4aSGreg Roach    /**
483fab8f067SGreg Roach     * Handle <header>
4848ba2e626SGreg Roach     *
4858ba2e626SGreg Roach     * @return void
486a6f13a4aSGreg Roach     */
487b702978eSGreg Roach    protected function headerStartHandler(): void
488c1010edaSGreg Roach    {
489a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
490e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4917a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
492a6f13a4aSGreg Roach    }
493a6f13a4aSGreg Roach
494a6f13a4aSGreg Roach    /**
495fab8f067SGreg Roach     * Handle <body>
4968ba2e626SGreg Roach     *
4978ba2e626SGreg Roach     * @return void
498a6f13a4aSGreg Roach     */
499b702978eSGreg Roach    protected function bodyStartHandler(): void
500c1010edaSGreg Roach    {
5017a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
502a6f13a4aSGreg Roach    }
503a6f13a4aSGreg Roach
504a6f13a4aSGreg Roach    /**
505fab8f067SGreg Roach     * Handle <footer>
5068ba2e626SGreg Roach     *
5078ba2e626SGreg Roach     * @return void
508a6f13a4aSGreg Roach     */
509b702978eSGreg Roach    protected function footerStartHandler(): void
510c1010edaSGreg Roach    {
5117a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
512a6f13a4aSGreg Roach    }
513a6f13a4aSGreg Roach
514a6f13a4aSGreg Roach    /**
515fab8f067SGreg Roach     * Handle <cell>
516a6f13a4aSGreg Roach     *
51777bab461SGreg Roach     * @param array<string,string> $attrs
5188ba2e626SGreg Roach     *
5198ba2e626SGreg Roach     * @return void
520a6f13a4aSGreg Roach     */
521b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
522c1010edaSGreg Roach    {
523a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
52477bab461SGreg Roach        $align = $attrs['align'] ?? '';
525a6f13a4aSGreg Roach        // RTL supported left/right alignment
526044416d2SGreg Roach        if ($align === 'rightrtl') {
527e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5287a6ee1acSGreg Roach                $align = 'left';
529a6f13a4aSGreg Roach            } else {
5307a6ee1acSGreg Roach                $align = 'right';
531a6f13a4aSGreg Roach            }
532044416d2SGreg Roach        } elseif ($align === 'leftrtl') {
533e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
5347a6ee1acSGreg Roach                $align = 'right';
535a6f13a4aSGreg Roach            } else {
5367a6ee1acSGreg Roach                $align = 'left';
537a6f13a4aSGreg Roach            }
538a6f13a4aSGreg Roach        }
539a6f13a4aSGreg Roach
54077bab461SGreg Roach        // The color to fill the background of this cell
54177bab461SGreg Roach        $bgcolor = $attrs['bgcolor'] ?? '';
542a6f13a4aSGreg Roach
54377bab461SGreg Roach        // Whether the background should be painted
544f315390bSGreg Roach        $fill = (bool) ($attrs['fill'] ?? '0');
545a6f13a4aSGreg Roach
54677bab461SGreg Roach        // If true reset the last cell height
54777bab461SGreg Roach        $reseth = (bool) ($attrs['reseth'] ?? '1');
548a6f13a4aSGreg Roach
54977bab461SGreg Roach        // Whether a border should be printed around this box
55077bab461SGreg Roach        $border = $attrs['border'] ?? '';
55177bab461SGreg Roach
552a6f13a4aSGreg Roach        // string Border color in HTML code
55377bab461SGreg Roach        $bocolor = $attrs['bocolor'] ?? '';
554a6f13a4aSGreg Roach
55577bab461SGreg Roach        // Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
55677bab461SGreg Roach        $height = (int) ($attrs['height'] ?? '0');
55777bab461SGreg Roach
558a6f13a4aSGreg 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.
55977bab461SGreg Roach        $width = (int) ($attrs['width'] ?? '0');
560a6f13a4aSGreg Roach
56177bab461SGreg Roach        // Stretch character mode
56277bab461SGreg Roach        $stretch = (int) ($attrs['stretch'] ?? '0');
563a6f13a4aSGreg Roach
564a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
565c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
566a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5677a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
568c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
569a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
570f315390bSGreg Roach                $left = (float) $attrs['left'];
5717a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
572f315390bSGreg Roach                $left = 0.0;
573a6f13a4aSGreg Roach            }
574a6f13a4aSGreg Roach        }
575a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
576c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
577a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5787a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
579c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
580a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
581f315390bSGreg Roach                $top = (float) $attrs['top'];
5827a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
583f315390bSGreg Roach                $top = 0.0;
584a6f13a4aSGreg Roach            }
585a6f13a4aSGreg Roach        }
586a6f13a4aSGreg Roach
58777bab461SGreg Roach        // The name of the Style that should be used to render the text.
58877bab461SGreg Roach        $style = $attrs['style'] ?? '';
589a6f13a4aSGreg Roach
590a6f13a4aSGreg Roach        // string Text color in html code
59177bab461SGreg Roach        $tcolor = $attrs['tcolor'] ?? '';
592a6f13a4aSGreg Roach
593a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
594a6f13a4aSGreg Roach        $ln = 0;
595a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
596a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
597a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5987a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
599a6f13a4aSGreg Roach                $ln = 0;
600a6f13a4aSGreg Roach            }
601a6f13a4aSGreg Roach        }
602a6f13a4aSGreg Roach
603044416d2SGreg Roach        if ($align === 'left') {
6047a6ee1acSGreg Roach            $align = 'L';
605044416d2SGreg Roach        } elseif ($align === 'right') {
6067a6ee1acSGreg Roach            $align = 'R';
607044416d2SGreg Roach        } elseif ($align === 'center') {
6087a6ee1acSGreg Roach            $align = 'C';
609044416d2SGreg Roach        } elseif ($align === 'justify') {
6107a6ee1acSGreg Roach            $align = 'J';
611a6f13a4aSGreg Roach        }
612a6f13a4aSGreg Roach
6139b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
614a6f13a4aSGreg Roach        $this->print_data         = true;
615a6f13a4aSGreg Roach
616e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
61724f2a3afSGreg Roach            (int) $width,
61824f2a3afSGreg Roach            (int) $height,
619a6f13a4aSGreg Roach            $border,
620a6f13a4aSGreg Roach            $align,
621a6f13a4aSGreg Roach            $bgcolor,
622a6f13a4aSGreg Roach            $style,
623a6f13a4aSGreg Roach            $ln,
624a6f13a4aSGreg Roach            $top,
625a6f13a4aSGreg Roach            $left,
626a6f13a4aSGreg Roach            $fill,
627a6f13a4aSGreg Roach            $stretch,
628a6f13a4aSGreg Roach            $bocolor,
629a6f13a4aSGreg Roach            $tcolor,
630a6f13a4aSGreg Roach            $reseth
631a6f13a4aSGreg Roach        );
632a6f13a4aSGreg Roach    }
633a6f13a4aSGreg Roach
634a6f13a4aSGreg Roach    /**
635fab8f067SGreg Roach     * Handle </cell>
6368ba2e626SGreg Roach     *
6378ba2e626SGreg Roach     * @return void
638a6f13a4aSGreg Roach     */
639b702978eSGreg Roach    protected function cellEndHandler(): void
640c1010edaSGreg Roach    {
641a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
642e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
643a6f13a4aSGreg Roach    }
644a6f13a4aSGreg Roach
645a6f13a4aSGreg Roach    /**
646fab8f067SGreg Roach     * Handle <now />
6478ba2e626SGreg Roach     *
6488ba2e626SGreg Roach     * @return void
649a6f13a4aSGreg Roach     */
650b702978eSGreg Roach    protected function nowStartHandler(): void
651c1010edaSGreg Roach    {
652d97083feSGreg Roach        $this->current_element->addText(Registry::timestampFactory()->now()->isoFormat('LLLL'));
653a6f13a4aSGreg Roach    }
654a6f13a4aSGreg Roach
655a6f13a4aSGreg Roach    /**
656fab8f067SGreg Roach     * Handle <pageNum />
6578ba2e626SGreg Roach     *
6588ba2e626SGreg Roach     * @return void
659a6f13a4aSGreg Roach     */
660b702978eSGreg Roach    protected function pageNumStartHandler(): void
661c1010edaSGreg Roach    {
6627a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
663a6f13a4aSGreg Roach    }
664a6f13a4aSGreg Roach
665a6f13a4aSGreg Roach    /**
666fab8f067SGreg Roach     * Handle <totalPages />
6678ba2e626SGreg Roach     *
6688ba2e626SGreg Roach     * @return void
669a6f13a4aSGreg Roach     */
670b702978eSGreg Roach    protected function totalPagesStartHandler(): void
671c1010edaSGreg Roach    {
6727a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
673a6f13a4aSGreg Roach    }
674a6f13a4aSGreg Roach
675a6f13a4aSGreg Roach    /**
676a6f13a4aSGreg Roach     * Called at the start of an element.
677a6f13a4aSGreg Roach     *
67809482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
6798ba2e626SGreg Roach     *
6808ba2e626SGreg Roach     * @return void
681a6f13a4aSGreg Roach     */
682b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
683c1010edaSGreg Roach    {
684a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
685a6f13a4aSGreg Roach            $this->process_gedcoms++;
686a6f13a4aSGreg Roach
687a6f13a4aSGreg Roach            return;
688a6f13a4aSGreg Roach        }
689a6f13a4aSGreg Roach
690a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6917a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6927a6ee1acSGreg Roach        $tags      = explode(':', $tag);
693a6f13a4aSGreg Roach        $newgedrec = '';
694a6f13a4aSGreg Roach        if (count($tags) < 2) {
6956b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
696299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
697a6f13a4aSGreg Roach        }
698a6f13a4aSGreg Roach        if (empty($newgedrec)) {
699a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
700a6f13a4aSGreg Roach            $newgedrec = '';
701a6f13a4aSGreg Roach            foreach ($tags as $tag) {
7027a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
703d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
704d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
705a6f13a4aSGreg Roach                    } else {
7066b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
707299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
708a6f13a4aSGreg Roach                    }
709a6f13a4aSGreg Roach                } else {
7107a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
71113abd6f3SGreg Roach                        $gmatch = [];
712a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7136b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
714299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
715a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
716a6f13a4aSGreg Roach                        } else {
717a6f13a4aSGreg Roach                            $newgedrec = '';
718a6f13a4aSGreg Roach                            break;
719a6f13a4aSGreg Roach                        }
720a6f13a4aSGreg Roach                    } else {
721b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
722b315f3e1SGreg Roach                        $newgedrec = self::getSubRecord($level, "$level $tag", $tgedrec);
723a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
724a6f13a4aSGreg Roach                    }
725a6f13a4aSGreg Roach                }
726a6f13a4aSGreg Roach            }
727a6f13a4aSGreg Roach        }
728a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7299b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
730a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
731a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
732a6f13a4aSGreg Roach                $this->fact = $match[2];
733a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
734a6f13a4aSGreg Roach            }
735a6f13a4aSGreg Roach        } else {
736a6f13a4aSGreg Roach            $this->process_gedcoms++;
737a6f13a4aSGreg Roach        }
738a6f13a4aSGreg Roach    }
739a6f13a4aSGreg Roach
740a6f13a4aSGreg Roach    /**
741a6f13a4aSGreg Roach     * Called at the end of an element.
7428ba2e626SGreg Roach     *
7438ba2e626SGreg Roach     * @return void
744a6f13a4aSGreg Roach     */
745b702978eSGreg Roach    protected function gedcomEndHandler(): void
746c1010edaSGreg Roach    {
747a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
748a6f13a4aSGreg Roach            $this->process_gedcoms--;
749a6f13a4aSGreg Roach        } else {
75065e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
751a6f13a4aSGreg Roach        }
752a6f13a4aSGreg Roach    }
753a6f13a4aSGreg Roach
754a6f13a4aSGreg Roach    /**
755fab8f067SGreg Roach     * Handle <textBox>
756a6f13a4aSGreg Roach     *
75709482a55SGreg Roach     * @param array<string> $attrs
7588ba2e626SGreg Roach     *
7598ba2e626SGreg Roach     * @return void
760a6f13a4aSGreg Roach     */
761b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
762c1010edaSGreg Roach    {
763a6f13a4aSGreg Roach        // string Background color code
7647a6ee1acSGreg Roach        $bgcolor = '';
765a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
766a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
767a6f13a4aSGreg Roach        }
768a6f13a4aSGreg Roach
769a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
770a6f13a4aSGreg Roach        $fill = true;
771a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7727a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
773a6f13a4aSGreg Roach                $fill = false;
7747a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
775a6f13a4aSGreg Roach                $fill = true;
776a6f13a4aSGreg Roach            }
777a6f13a4aSGreg Roach        }
778a6f13a4aSGreg Roach
779a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
780a6f13a4aSGreg Roach        $border = false;
781a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7827a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
783a6f13a4aSGreg Roach                $border = true;
7847a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
785a6f13a4aSGreg Roach                $border = false;
786a6f13a4aSGreg Roach            }
787a6f13a4aSGreg Roach        }
788a6f13a4aSGreg Roach
789a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
790a6f13a4aSGreg Roach        $height = 0;
791a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
792a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
793a6f13a4aSGreg Roach        }
794a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
795a6f13a4aSGreg Roach        $width = 0;
796a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
797a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
798a6f13a4aSGreg Roach        }
799a6f13a4aSGreg Roach
800a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
801c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
802a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
8037a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
804c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
805a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
806a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
8077a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
808a6f13a4aSGreg Roach                $left = 0;
809a6f13a4aSGreg Roach            }
810a6f13a4aSGreg Roach        }
811a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
812c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
813a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8147a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
815c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
816a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
817a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8187a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
819a6f13a4aSGreg Roach                $top = 0;
820a6f13a4aSGreg Roach            }
821a6f13a4aSGreg Roach        }
822a6f13a4aSGreg 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
823a6f13a4aSGreg Roach        $newline = false;
824a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8257a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
826a6f13a4aSGreg Roach                $newline = true;
8277a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
828a6f13a4aSGreg Roach                $newline = false;
829a6f13a4aSGreg Roach            }
830a6f13a4aSGreg Roach        }
831a6f13a4aSGreg Roach        // boolean
832a6f13a4aSGreg Roach        $pagecheck = true;
833a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8347a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
835a6f13a4aSGreg Roach                $pagecheck = false;
8367a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
837a6f13a4aSGreg Roach                $pagecheck = true;
838a6f13a4aSGreg Roach            }
839a6f13a4aSGreg Roach        }
840a6f13a4aSGreg Roach        // boolean Cell padding
841a6f13a4aSGreg Roach        $padding = true;
842a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8437a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
844a6f13a4aSGreg Roach                $padding = false;
8457a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
846a6f13a4aSGreg Roach                $padding = true;
847a6f13a4aSGreg Roach            }
848a6f13a4aSGreg Roach        }
849a6f13a4aSGreg Roach        // boolean Reset this box Height
850a6f13a4aSGreg Roach        $reseth = false;
851a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8527a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
853a6f13a4aSGreg Roach                $reseth = true;
8547a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
855a6f13a4aSGreg Roach                $reseth = false;
856a6f13a4aSGreg Roach            }
857a6f13a4aSGreg Roach        }
858a6f13a4aSGreg Roach
859a6f13a4aSGreg Roach        // string Style of rendering
8607a6ee1acSGreg Roach        $style = '';
861a6f13a4aSGreg Roach
8629b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
863a6f13a4aSGreg Roach        $this->print_data         = false;
864a6f13a4aSGreg Roach
8659b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
866e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
867a6f13a4aSGreg Roach            $width,
868a6f13a4aSGreg Roach            $height,
869a6f13a4aSGreg Roach            $border,
870a6f13a4aSGreg Roach            $bgcolor,
871a6f13a4aSGreg Roach            $newline,
872a6f13a4aSGreg Roach            $left,
873a6f13a4aSGreg Roach            $top,
874a6f13a4aSGreg Roach            $pagecheck,
875a6f13a4aSGreg Roach            $style,
876a6f13a4aSGreg Roach            $fill,
877a6f13a4aSGreg Roach            $padding,
878a6f13a4aSGreg Roach            $reseth
879a6f13a4aSGreg Roach        );
880a6f13a4aSGreg Roach    }
881a6f13a4aSGreg Roach
882a6f13a4aSGreg Roach    /**
883fab8f067SGreg Roach     * Handle <textBox>
8848ba2e626SGreg Roach     *
8858ba2e626SGreg Roach     * @return void
886a6f13a4aSGreg Roach     */
887b702978eSGreg Roach    protected function textBoxEndHandler(): void
888c1010edaSGreg Roach    {
889a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
890e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
89131d9777aSGreg Roach
89231d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
89331d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
89431d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
89531d9777aSGreg Roach
896e8e7866bSGreg Roach        $this->wt_report = array_pop($this->wt_report_stack);
897e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
898a6f13a4aSGreg Roach    }
899a6f13a4aSGreg Roach
900a6f13a4aSGreg Roach    /**
90176692c8bSGreg Roach     * XLM <Text>.
90276692c8bSGreg Roach     *
90309482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9048ba2e626SGreg Roach     *
9058ba2e626SGreg Roach     * @return void
906a6f13a4aSGreg Roach     */
907b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
908c1010edaSGreg Roach    {
9099b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
910a6f13a4aSGreg Roach        $this->print_data         = true;
911a6f13a4aSGreg Roach
912a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9137a6ee1acSGreg Roach        $style = '';
914a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
915a6f13a4aSGreg Roach            $style = $attrs['style'];
916a6f13a4aSGreg Roach        }
917a6f13a4aSGreg Roach
918a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9197a6ee1acSGreg Roach        $color = '';
920a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
921a6f13a4aSGreg Roach            $color = $attrs['color'];
922a6f13a4aSGreg Roach        }
923a6f13a4aSGreg Roach
924e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
925a6f13a4aSGreg Roach    }
926a6f13a4aSGreg Roach
927a6f13a4aSGreg Roach    /**
928fab8f067SGreg Roach     * Handle </text>
9298ba2e626SGreg Roach     *
9308ba2e626SGreg Roach     * @return void
931a6f13a4aSGreg Roach     */
932b702978eSGreg Roach    protected function textEndHandler(): void
933c1010edaSGreg Roach    {
934a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
935e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
936a6f13a4aSGreg Roach    }
937a6f13a4aSGreg Roach
938a6f13a4aSGreg Roach    /**
939fab8f067SGreg Roach     * Handle <getPersonName />
940a6f13a4aSGreg Roach     * Get the name
941a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
942a6f13a4aSGreg Roach     * 2. id is set with a record id
943a6f13a4aSGreg Roach     *
94409482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
9458ba2e626SGreg Roach     *
9468ba2e626SGreg Roach     * @return void
947a6f13a4aSGreg Roach     */
948b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
949c1010edaSGreg Roach    {
9507a6ee1acSGreg Roach        $id    = '';
95113abd6f3SGreg Roach        $match = [];
952a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9537a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
954a6f13a4aSGreg Roach                $id = $match[1];
955a6f13a4aSGreg Roach            }
956a6f13a4aSGreg Roach        } else {
9577a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
958d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
959d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
960a6f13a4aSGreg Roach                }
961a6f13a4aSGreg Roach            } else {
9627a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
96313abd6f3SGreg Roach                    $gmatch = [];
964a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
965a6f13a4aSGreg Roach                        $id = $gmatch[1];
966a6f13a4aSGreg Roach                    }
967a6f13a4aSGreg Roach                } else {
968a6f13a4aSGreg Roach                    $id = $attrs['id'];
969a6f13a4aSGreg Roach                }
970a6f13a4aSGreg Roach            }
971a6f13a4aSGreg Roach        }
972a6f13a4aSGreg Roach        if (!empty($id)) {
9736b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9748f038c36SRico Sonntag            if ($record === null) {
975a6f13a4aSGreg Roach                return;
976a6f13a4aSGreg Roach            }
977a6f13a4aSGreg Roach            if (!$record->canShowName()) {
978a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
979a6f13a4aSGreg Roach            } else {
98039ca88baSGreg Roach                $name = $record->fullName();
981a6f13a4aSGreg Roach                $name = strip_tags($name);
982a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
983381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
984a6f13a4aSGreg Roach                } else {
985911124f5SGreg Roach                    $addname = (string) $record->alternateName();
986a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
987a6f13a4aSGreg Roach                    if (!empty($addname)) {
9887a6ee1acSGreg Roach                        $name .= ' ' . $addname;
989a6f13a4aSGreg Roach                    }
990a6f13a4aSGreg Roach                }
991a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
992a6f13a4aSGreg Roach            }
993a6f13a4aSGreg Roach        }
994a6f13a4aSGreg Roach    }
995a6f13a4aSGreg Roach
996a6f13a4aSGreg Roach    /**
997fab8f067SGreg Roach     * Handle <gedcomValue />
998a6f13a4aSGreg Roach     *
99909482a55SGreg Roach     * @param array<string> $attrs
10008ba2e626SGreg Roach     *
10018ba2e626SGreg Roach     * @return void
1002a6f13a4aSGreg Roach     */
1003b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1004c1010edaSGreg Roach    {
10057a6ee1acSGreg Roach        $id    = '';
100613abd6f3SGreg Roach        $match = [];
10077a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1008a6f13a4aSGreg Roach            $id = $match[1];
1009a6f13a4aSGreg Roach        }
1010a6f13a4aSGreg Roach
1011044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10127a6ee1acSGreg Roach            $useBreak = '1';
1013a6f13a4aSGreg Roach        } else {
10147a6ee1acSGreg Roach            $useBreak = '0';
1015a6f13a4aSGreg Roach        }
1016a6f13a4aSGreg Roach
1017a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1018a6f13a4aSGreg Roach        if (!empty($tag)) {
1019044416d2SGreg Roach            if ($tag === '@desc') {
1020a6f13a4aSGreg Roach                $value = $this->desc;
1021a6f13a4aSGreg Roach                $value = trim($value);
1022a6f13a4aSGreg Roach                $this->current_element->addText($value);
1023a6f13a4aSGreg Roach            }
1024044416d2SGreg Roach            if ($tag === '@id') {
1025a6f13a4aSGreg Roach                $this->current_element->addText($id);
1026a6f13a4aSGreg Roach            } else {
10277a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1028a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1029b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1030b2448a1bSGreg Roach                    if ($level === 0) {
1031a6f13a4aSGreg Roach                        $level++;
1032a6f13a4aSGreg Roach                    }
1033a6f13a4aSGreg Roach                } else {
1034b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1035a6f13a4aSGreg Roach                }
1036a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10373d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1038a6f13a4aSGreg Roach                switch (end($tags)) {
1039a6f13a4aSGreg Roach                    case 'DATE':
1040a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1041326dd299SGreg Roach                        $value = strip_tags($tmp->display());
1042a6f13a4aSGreg Roach                        break;
1043a6f13a4aSGreg Roach                    case 'PLAC':
1044299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1045392561bbSGreg Roach                        $value = $tmp->shortName();
1046a6f13a4aSGreg Roach                        break;
1047a6f13a4aSGreg Roach                }
1048044416d2SGreg Roach                if ($useBreak === '1') {
1049a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1050a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1051a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1052a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1053a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10543b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10553b3cfeeaSGreg Roach                        $value = substr($value, 4);
1056a6f13a4aSGreg Roach                    }
1057a6f13a4aSGreg Roach                }
1058d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10593cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10604d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
1061ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($value, $this->tree), ['br']);
10624d35caa7SGreg Roach                    } else {
1063ab2c9152SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($value, $this->tree), ['br']);
10644d35caa7SGreg Roach                    }
1065ab2c9152SGreg Roach                    $value = strtr($value, [MarkdownFactory::BREAK => ' ']);
1066a4d703aeSGreg Roach                }
1067a2a485c3SGreg Roach
1068a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1069381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1070a2a485c3SGreg Roach                }
1071a6f13a4aSGreg Roach                $this->current_element->addText($value);
1072a6f13a4aSGreg Roach            }
1073a6f13a4aSGreg Roach        }
1074a6f13a4aSGreg Roach    }
1075a6f13a4aSGreg Roach
1076a6f13a4aSGreg Roach    /**
1077fab8f067SGreg Roach     * Handle <repeatTag>
1078a6f13a4aSGreg Roach     *
107909482a55SGreg Roach     * @param array<string> $attrs
10808ba2e626SGreg Roach     *
10818ba2e626SGreg Roach     * @return void
1082a6f13a4aSGreg Roach     */
1083b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1084c1010edaSGreg Roach    {
1085a6f13a4aSGreg Roach        $this->process_repeats++;
1086a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1087a6f13a4aSGreg Roach            return;
1088a6f13a4aSGreg Roach        }
1089a6f13a4aSGreg Roach
10909b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
109113abd6f3SGreg Roach        $this->repeats         = [];
1092e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1093a6f13a4aSGreg Roach
1094e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1095a6f13a4aSGreg Roach        if (!empty($tag)) {
1096044416d2SGreg Roach            if ($tag === '@desc') {
1097a6f13a4aSGreg Roach                $value = $this->desc;
1098a6f13a4aSGreg Roach                $value = trim($value);
1099a6f13a4aSGreg Roach                $this->current_element->addText($value);
1100a6f13a4aSGreg Roach            } else {
11017a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11027a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1103b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1104b2448a1bSGreg Roach                if ($level === 0) {
1105a6f13a4aSGreg Roach                    $level++;
1106a6f13a4aSGreg Roach                }
1107a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1108a6f13a4aSGreg Roach                $t      = $tag;
1109a6f13a4aSGreg Roach                $count  = count($tags);
1110a6f13a4aSGreg Roach                $i      = 0;
1111a6f13a4aSGreg Roach                while ($i < $count) {
1112a6f13a4aSGreg Roach                    $t = $tags[$i];
1113a6f13a4aSGreg Roach                    if (!empty($t)) {
1114a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
1115b315f3e1SGreg Roach                            $subrec = self::getSubRecord($level, "$level $t", $subrec);
1116a6f13a4aSGreg Roach                            if (empty($subrec)) {
1117a6f13a4aSGreg Roach                                $level--;
1118b315f3e1SGreg Roach                                $subrec = self::getSubRecord($level, "@ $t", $this->gedrec);
1119a6f13a4aSGreg Roach                                if (empty($subrec)) {
1120a6f13a4aSGreg Roach                                    return;
1121a6f13a4aSGreg Roach                                }
1122a6f13a4aSGreg Roach                            }
1123a6f13a4aSGreg Roach                        }
1124a6f13a4aSGreg Roach                        $level++;
1125a6f13a4aSGreg Roach                    }
1126a6f13a4aSGreg Roach                    $i++;
1127a6f13a4aSGreg Roach                }
1128a6f13a4aSGreg Roach                $level--;
1129a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1130a6f13a4aSGreg Roach                $i     = 0;
1131a6f13a4aSGreg Roach                while ($i < $count) {
1132a6f13a4aSGreg Roach                    $i++;
1133a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1134b315f3e1SGreg Roach                    $subrecord = self::getSubRecord($level, "$level $t", $subrec, $i);
11358d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11366b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1137a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1138a9007102SGreg Roach                            continue;
1139a9007102SGreg Roach                        }
1140a9007102SGreg Roach                    }
1141a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1142a6f13a4aSGreg Roach                }
1143a6f13a4aSGreg Roach            }
1144a6f13a4aSGreg Roach        }
1145a6f13a4aSGreg Roach    }
1146a6f13a4aSGreg Roach
1147a6f13a4aSGreg Roach    /**
1148fab8f067SGreg Roach     * Handle </repeatTag>
11498ba2e626SGreg Roach     *
11508ba2e626SGreg Roach     * @return void
1151a6f13a4aSGreg Roach     */
1152b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1153c1010edaSGreg Roach    {
1154a6f13a4aSGreg Roach        $this->process_repeats--;
1155a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1156a6f13a4aSGreg Roach            return;
1157a6f13a4aSGreg Roach        }
1158a6f13a4aSGreg Roach
1159a6f13a4aSGreg Roach        // Check if there is anything to repeat
1160a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1161a6f13a4aSGreg Roach            // No need to load them if not used...
1162a6f13a4aSGreg Roach
1163a6f13a4aSGreg Roach            $lineoffset = 0;
1164a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1165a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1166a6f13a4aSGreg Roach            }
1167a6f13a4aSGreg Roach            //-- read the xml from the file
1168299d100dSGreg Roach            $lines = file($this->report);
1169dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1170a6f13a4aSGreg Roach                $lineoffset--;
1171a6f13a4aSGreg Roach            }
1172a6f13a4aSGreg Roach            $lineoffset++;
1173a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1174a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1175a6f13a4aSGreg Roach            // RepeatTag Level counter
1176a6f13a4aSGreg Roach            $count = 1;
1177a6f13a4aSGreg Roach            while (0 < $count) {
1178dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1179a6f13a4aSGreg Roach                    $count++;
1180dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1181a6f13a4aSGreg Roach                    $count--;
1182a6f13a4aSGreg Roach                }
1183a6f13a4aSGreg Roach                if (0 < $count) {
1184a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1185a6f13a4aSGreg Roach                }
1186a6f13a4aSGreg Roach                $line_nr++;
1187a6f13a4aSGreg Roach            }
1188a6f13a4aSGreg Roach            // No need to drag this
1189a6f13a4aSGreg Roach            unset($lines);
1190a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1191a6f13a4aSGreg Roach            // Save original values
11929b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1193a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1194a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1195a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1196a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1197e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1198f315390bSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
11991aa04befSGreg Roach
12001aa04befSGreg Roach                xml_set_element_handler(
12011aa04befSGreg Roach                    $repeat_parser,
12029d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12031aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12041aa04befSGreg Roach                    },
12059d454b6bSGreg Roach                    function ($parser, string $name): void {
12061aa04befSGreg Roach                        $this->endElement($parser, $name);
12071aa04befSGreg Roach                    }
12081aa04befSGreg Roach                );
12091aa04befSGreg Roach
12101aa04befSGreg Roach                xml_set_character_data_handler(
12111aa04befSGreg Roach                    $repeat_parser,
12129d454b6bSGreg Roach                    function ($parser, string $data): void {
12131aa04befSGreg Roach                        $this->characterData($parser, $data);
12141aa04befSGreg Roach                    }
12151aa04befSGreg Roach                );
12161aa04befSGreg Roach
1217a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12186ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1219a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1220a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1221a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1222a6f13a4aSGreg Roach                    ));
1223a6f13a4aSGreg Roach                }
1224a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1225a6f13a4aSGreg Roach            }
1226a6f13a4aSGreg Roach            // Restore original values
1227a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1228e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1229a6f13a4aSGreg Roach        }
123065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1231a6f13a4aSGreg Roach    }
1232a6f13a4aSGreg Roach
1233a6f13a4aSGreg Roach    /**
1234a6f13a4aSGreg Roach     * Variable lookup
1235a6f13a4aSGreg Roach     * Retrieve predefined variables :
1236a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1237a6f13a4aSGreg Roach     *        1 EVEN This is a description
1238a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1239a6f13a4aSGreg Roach     * $ I18N::translate('....')
1240a6f13a4aSGreg Roach     * $ language_settings[]
1241a6f13a4aSGreg Roach     *
124209482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
12438ba2e626SGreg Roach     *
12448ba2e626SGreg Roach     * @return void
1245a6f13a4aSGreg Roach     */
1246b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1247c1010edaSGreg Roach    {
1248a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12496ccdf4f0SGreg 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));
1250a6f13a4aSGreg Roach        }
1251a6f13a4aSGreg Roach
1252a6f13a4aSGreg Roach        $var = $attrs['var'];
1253a6f13a4aSGreg Roach        // SetVar element preset variables
1254d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1255d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1256a6f13a4aSGreg Roach        } else {
1257a6f13a4aSGreg Roach            $tfact = $this->fact;
12587a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1259a6f13a4aSGreg Roach                // Use :
1260a6f13a4aSGreg Roach                // n TYPE This text if string
1261a6f13a4aSGreg Roach                $tfact = $this->type;
126297def6bcSGreg Roach            } else {
126397def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
126497def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
126597def6bcSGreg Roach
126697def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
126797def6bcSGreg Roach                        $tfact = $element->label();
126897def6bcSGreg Roach                        break;
1269a6f13a4aSGreg Roach                    }
127097def6bcSGreg Roach                }
127197def6bcSGreg Roach            }
127297def6bcSGreg Roach
127397def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
127497def6bcSGreg Roach
1275a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1276da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1277a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1278a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1279a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1280a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1281a6f13a4aSGreg Roach            }
1282a6f13a4aSGreg Roach        }
1283a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1284a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12857a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1286a6f13a4aSGreg Roach                $g   = new Date($var);
1287a6f13a4aSGreg Roach                $var = $g->display();
1288a6f13a4aSGreg Roach            }
1289a6f13a4aSGreg Roach        }
1290a6f13a4aSGreg Roach        $this->current_element->addText($var);
12912836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1292a6f13a4aSGreg Roach    }
1293a6f13a4aSGreg Roach
1294a6f13a4aSGreg Roach    /**
1295fab8f067SGreg Roach     * Handle <facts>
129676692c8bSGreg Roach     *
129709482a55SGreg Roach     * @param array<string> $attrs
12988ba2e626SGreg Roach     *
12998ba2e626SGreg Roach     * @return void
1300a6f13a4aSGreg Roach     */
1301b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1302c1010edaSGreg Roach    {
1303a6f13a4aSGreg Roach        $this->process_repeats++;
1304a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1305a6f13a4aSGreg Roach            return;
1306a6f13a4aSGreg Roach        }
1307a6f13a4aSGreg Roach
13089b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
130913abd6f3SGreg Roach        $this->repeats         = [];
1310e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1311a6f13a4aSGreg Roach
13127a6ee1acSGreg Roach        $id    = '';
131313abd6f3SGreg Roach        $match = [];
13147a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1315a6f13a4aSGreg Roach            $id = $match[1];
1316a6f13a4aSGreg Roach        }
13177a6ee1acSGreg Roach        $tag = '';
1318a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1319a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1320a6f13a4aSGreg Roach        }
13217a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1322d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1323a6f13a4aSGreg Roach        }
1324a6f13a4aSGreg Roach
13256b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1326a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1327820b62dfSGreg Roach            $facts = $record->facts([], true);
132813abd6f3SGreg Roach            $this->repeats = [];
1329a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1330195b5e75SGreg Roach            foreach ($facts as $fact) {
1331d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1332d0889c63SGreg Roach
1333d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1334195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1335a6f13a4aSGreg Roach                }
1336a6f13a4aSGreg Roach            }
1337a6f13a4aSGreg Roach        } else {
133830158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1339d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1340138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1341a6f13a4aSGreg Roach                }
1342a6f13a4aSGreg Roach            }
1343a6f13a4aSGreg Roach        }
1344a6f13a4aSGreg Roach    }
1345a6f13a4aSGreg Roach
1346a6f13a4aSGreg Roach    /**
1347fab8f067SGreg Roach     * Handle </facts>
13488ba2e626SGreg Roach     *
13498ba2e626SGreg Roach     * @return void
1350a6f13a4aSGreg Roach     */
1351b702978eSGreg Roach    protected function factsEndHandler(): void
1352c1010edaSGreg Roach    {
1353a6f13a4aSGreg Roach        $this->process_repeats--;
1354a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1355a6f13a4aSGreg Roach            return;
1356a6f13a4aSGreg Roach        }
1357a6f13a4aSGreg Roach
1358a6f13a4aSGreg Roach        // Check if there is anything to repeat
1359a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1360e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1361a6f13a4aSGreg Roach            $lineoffset = 0;
1362a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1363a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1364a6f13a4aSGreg Roach            }
1365a6f13a4aSGreg Roach
1366a6f13a4aSGreg Roach            //-- read the xml from the file
1367299d100dSGreg Roach            $lines = file($this->report);
1368dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1369a6f13a4aSGreg Roach                $lineoffset--;
1370a6f13a4aSGreg Roach            }
1371a6f13a4aSGreg Roach            $lineoffset++;
1372a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1373a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1374a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1375a6f13a4aSGreg Roach            while ($line_nr < $i) {
1376a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1377a6f13a4aSGreg Roach                $line_nr++;
1378a6f13a4aSGreg Roach            }
1379a6f13a4aSGreg Roach            // No need to drag this
1380a6f13a4aSGreg Roach            unset($lines);
1381a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1382a6f13a4aSGreg Roach            // Save original values
13839b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1384a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1385a6f13a4aSGreg Roach            $count                = count($this->repeats);
1386a6f13a4aSGreg Roach            $i                    = 0;
1387a6f13a4aSGreg Roach            while ($i < $count) {
1388a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1389a6f13a4aSGreg Roach                $this->fact   = '';
1390a6f13a4aSGreg Roach                $this->desc   = '';
1391a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1392a6f13a4aSGreg Roach                    $this->fact = $match[1];
1393a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
139413abd6f3SGreg Roach                        $tmatch = [];
1395a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1396a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1397a6f13a4aSGreg Roach                        } else {
1398a6f13a4aSGreg Roach                            $this->type = ' ';
1399a6f13a4aSGreg Roach                        }
1400a6f13a4aSGreg Roach                    }
1401a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
1402b315f3e1SGreg Roach                    $this->desc .= self::getCont(2, $this->gedrec);
1403a6f13a4aSGreg Roach                }
1404a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1405e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1406f315390bSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
14071aa04befSGreg Roach
14081aa04befSGreg Roach                xml_set_element_handler(
14091aa04befSGreg Roach                    $repeat_parser,
14109d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14111aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14121aa04befSGreg Roach                    },
14139d454b6bSGreg Roach                    function ($parser, string $name): void {
14141aa04befSGreg Roach                        $this->endElement($parser, $name);
14151aa04befSGreg Roach                    }
14161aa04befSGreg Roach                );
14171aa04befSGreg Roach
14181aa04befSGreg Roach                xml_set_character_data_handler(
14191aa04befSGreg Roach                    $repeat_parser,
14209d454b6bSGreg Roach                    function ($parser, string $data): void {
14211aa04befSGreg Roach                        $this->characterData($parser, $data);
14221aa04befSGreg Roach                    }
14231aa04befSGreg Roach                );
14241aa04befSGreg Roach
1425a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14266ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1427a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1428a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1429a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1430a6f13a4aSGreg Roach                    ));
1431a6f13a4aSGreg Roach                }
1432a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1433a6f13a4aSGreg Roach                $i++;
1434a6f13a4aSGreg Roach            }
1435a6f13a4aSGreg Roach            // Restore original values
1436e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1437a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1438a6f13a4aSGreg Roach        }
143965e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1440a6f13a4aSGreg Roach    }
1441a6f13a4aSGreg Roach
1442a6f13a4aSGreg Roach    /**
1443a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1444d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1445a6f13a4aSGreg Roach     *
144609482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
14478ba2e626SGreg Roach     *
14488ba2e626SGreg Roach     * @return void
1449a6f13a4aSGreg Roach     */
1450b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1451c1010edaSGreg Roach    {
1452a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14536ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1454a6f13a4aSGreg Roach        }
1455a6f13a4aSGreg Roach
1456a6f13a4aSGreg Roach        $name  = $attrs['name'];
1457a6f13a4aSGreg Roach        $value = $attrs['value'];
145813abd6f3SGreg Roach        $match = [];
1459a6f13a4aSGreg Roach        // Current GEDCOM record strings
1460044416d2SGreg Roach        if ($value === '@ID') {
14617a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1462a6f13a4aSGreg Roach                $value = $match[1];
1463a6f13a4aSGreg Roach            }
1464044416d2SGreg Roach        } elseif ($value === '@fact') {
1465a6f13a4aSGreg Roach            $value = $this->fact;
1466044416d2SGreg Roach        } elseif ($value === '@desc') {
1467a6f13a4aSGreg Roach            $value = $this->desc;
1468044416d2SGreg Roach        } elseif ($value === '@generation') {
1469589feda3SGreg Roach            $value = (string) $this->generation;
1470a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
147113abd6f3SGreg Roach            $gmatch = [];
1472a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14737a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1474a6f13a4aSGreg Roach            }
1475a6f13a4aSGreg Roach        }
1476a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1477d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1478a6f13a4aSGreg Roach        }
1479a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1480a6f13a4aSGreg Roach        $i     = 0;
1481a6f13a4aSGreg Roach        while ($i < $count) {
1482d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14837a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1484a6f13a4aSGreg Roach            $i++;
1485a6f13a4aSGreg Roach        }
1486a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1487da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1488a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1489a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1490a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1491a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1492a6f13a4aSGreg Roach        }
149352868398SGreg Roach
1494a6f13a4aSGreg Roach        // Arithmetic functions
14953cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
149652868398SGreg Roach            // Create an expression language with the functions used by our reports.
149752868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1498c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1499c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
150052868398SGreg Roach
150152868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1502a6f13a4aSGreg Roach        }
150352868398SGreg Roach
1504dec352c1SGreg Roach        if (str_contains($value, '@')) {
15057a6ee1acSGreg Roach            $value = '';
1506a6f13a4aSGreg Roach        }
1507d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1508a6f13a4aSGreg Roach    }
1509a6f13a4aSGreg Roach
1510a6f13a4aSGreg Roach    /**
1511fab8f067SGreg Roach     * Handle <if>
1512a6f13a4aSGreg Roach     *
151309482a55SGreg Roach     * @param array<string> $attrs
15148ba2e626SGreg Roach     *
15158ba2e626SGreg Roach     * @return void
1516a6f13a4aSGreg Roach     */
1517b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1518c1010edaSGreg Roach    {
1519a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1520a6f13a4aSGreg Roach            $this->process_ifs++;
1521a6f13a4aSGreg Roach
1522a6f13a4aSGreg Roach            return;
1523a6f13a4aSGreg Roach        }
1524a6f13a4aSGreg Roach
1525a6f13a4aSGreg Roach        $condition = $attrs['condition'];
152682759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1527c1010edaSGreg Roach        $condition = str_replace([
1528c1010edaSGreg Roach            ' LT ',
1529c1010edaSGreg Roach            ' GT ',
1530c1010edaSGreg Roach        ], [
1531c1010edaSGreg Roach            '<',
1532c1010edaSGreg Roach            '>',
1533c1010edaSGreg Roach        ], $condition);
15343cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15357a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
153613abd6f3SGreg Roach        $match     = [];
15373cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1538a6f13a4aSGreg Roach        $i         = 0;
1539a6f13a4aSGreg Roach        while ($i < $count) {
1540a6f13a4aSGreg Roach            $id    = $match[$i][1];
1541a6f13a4aSGreg Roach            $value = '""';
1542044416d2SGreg Roach            if ($id === 'ID') {
15437a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1544a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1545a6f13a4aSGreg Roach                }
15467a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1547a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15487a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1549a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15507a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1551a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1552a6f13a4aSGreg Roach            } else {
1553b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1554b2448a1bSGreg Roach                if ($level === 0) {
1555a6f13a4aSGreg Roach                    $level++;
1556a6f13a4aSGreg Roach                }
15573d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1558a6f13a4aSGreg Roach                if (empty($value)) {
1559a6f13a4aSGreg Roach                    $level++;
15603d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1561a6f13a4aSGreg Roach                }
15628d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15635e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1564a6f13a4aSGreg Roach            }
1565a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1566a6f13a4aSGreg Roach            $i++;
1567a6f13a4aSGreg Roach        }
15685809450fSGreg Roach
1569cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1570cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1571c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1572c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1573cb63a60eSGreg Roach
1574cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15755809450fSGreg Roach
1576a6f13a4aSGreg Roach        if (!$ret) {
1577a6f13a4aSGreg Roach            $this->process_ifs++;
1578a6f13a4aSGreg Roach        }
1579a6f13a4aSGreg Roach    }
1580a6f13a4aSGreg Roach
1581a6f13a4aSGreg Roach    /**
1582fab8f067SGreg Roach     * Handle </if>
15838ba2e626SGreg Roach     *
15848ba2e626SGreg Roach     * @return void
1585a6f13a4aSGreg Roach     */
1586b702978eSGreg Roach    protected function ifEndHandler(): void
1587c1010edaSGreg Roach    {
1588a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1589a6f13a4aSGreg Roach            $this->process_ifs--;
1590a6f13a4aSGreg Roach        }
1591a6f13a4aSGreg Roach    }
1592a6f13a4aSGreg Roach
1593a6f13a4aSGreg Roach    /**
1594fab8f067SGreg Roach     * Handle <footnote>
1595a6f13a4aSGreg Roach     * Collect the Footnote links
1596fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1597a6f13a4aSGreg Roach     *
159809482a55SGreg Roach     * @param array<string> $attrs
15998ba2e626SGreg Roach     *
16008ba2e626SGreg Roach     * @return void
1601a6f13a4aSGreg Roach     */
1602b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1603c1010edaSGreg Roach    {
16047a6ee1acSGreg Roach        $id = '';
16057a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1606a6f13a4aSGreg Roach            $id = $match[2];
1607a6f13a4aSGreg Roach        }
16086b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1609a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16109b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1611a6f13a4aSGreg Roach            $this->print_data         = true;
16127a6ee1acSGreg Roach            $style                    = '';
1613a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1614a6f13a4aSGreg Roach                $style = $attrs['style'];
1615a6f13a4aSGreg Roach            }
1616a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1617e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1618a6f13a4aSGreg Roach        } else {
1619a6f13a4aSGreg Roach            $this->print_data       = false;
1620a6f13a4aSGreg Roach            $this->process_footnote = false;
1621a6f13a4aSGreg Roach        }
1622a6f13a4aSGreg Roach    }
1623a6f13a4aSGreg Roach
1624a6f13a4aSGreg Roach    /**
1625fab8f067SGreg Roach     * Handle </footnote>
1626a6f13a4aSGreg Roach     * Print the collected Footnote data
16278ba2e626SGreg Roach     *
16288ba2e626SGreg Roach     * @return void
1629a6f13a4aSGreg Roach     */
1630b702978eSGreg Roach    protected function footnoteEndHandler(): void
1631c1010edaSGreg Roach    {
1632a6f13a4aSGreg Roach        if ($this->process_footnote) {
1633a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1634a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1635a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1636e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1637a6f13a4aSGreg Roach            }
1638a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1639a6f13a4aSGreg Roach        } else {
1640a6f13a4aSGreg Roach            $this->process_footnote = true;
1641a6f13a4aSGreg Roach        }
1642a6f13a4aSGreg Roach    }
1643a6f13a4aSGreg Roach
1644a6f13a4aSGreg Roach    /**
1645fab8f067SGreg Roach     * Handle <footnoteTexts />
16468ba2e626SGreg Roach     *
16478ba2e626SGreg Roach     * @return void
1648a6f13a4aSGreg Roach     */
1649b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1650c1010edaSGreg Roach    {
16517a6ee1acSGreg Roach        $temp = 'footnotetexts';
1652e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1653a6f13a4aSGreg Roach    }
1654a6f13a4aSGreg Roach
1655a6f13a4aSGreg Roach    /**
1656a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16578ba2e626SGreg Roach     *
16588ba2e626SGreg Roach     * @return void
1659a6f13a4aSGreg Roach     */
1660b702978eSGreg Roach    protected function brStartHandler(): void
1661c1010edaSGreg Roach    {
1662a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1663a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1664a6f13a4aSGreg Roach        }
1665a6f13a4aSGreg Roach    }
1666a6f13a4aSGreg Roach
1667a6f13a4aSGreg Roach    /**
1668fab8f067SGreg Roach     * Handle <sp />
1669fab8f067SGreg Roach     * Forced space
16708ba2e626SGreg Roach     *
16718ba2e626SGreg Roach     * @return void
1672a6f13a4aSGreg Roach     */
1673b702978eSGreg Roach    protected function spStartHandler(): void
1674c1010edaSGreg Roach    {
1675a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1676a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1677a6f13a4aSGreg Roach        }
1678a6f13a4aSGreg Roach    }
1679a6f13a4aSGreg Roach
1680a6f13a4aSGreg Roach    /**
1681fab8f067SGreg Roach     * Handle <highlightedImage />
168276692c8bSGreg Roach     *
168309482a55SGreg Roach     * @param array<string> $attrs
16848ba2e626SGreg Roach     *
16858ba2e626SGreg Roach     * @return void
1686a6f13a4aSGreg Roach     */
1687b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1688c1010edaSGreg Roach    {
1689a6f13a4aSGreg Roach        $id = '';
16907a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1691a6f13a4aSGreg Roach            $id = $match[1];
1692a6f13a4aSGreg Roach        }
1693a6f13a4aSGreg Roach
1694c21bdddcSGreg Roach        // Position the top corner of this box on the page
1695c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1696a6f13a4aSGreg Roach
1697c21bdddcSGreg Roach        // Position the left corner of this box on the page
1698c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1699a6f13a4aSGreg Roach
170083cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
170183cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1702a6f13a4aSGreg Roach
1703a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
170483cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1705a6f13a4aSGreg Roach
170683cdc021SGreg Roach        // Width, height (or both).
1707c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1708c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1709a6f13a4aSGreg Roach
17106b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
17114a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
171286a63f51SGreg Roach
17139458f20aSGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists()) {
17149458f20aSGreg Roach            $image      = imagecreatefromstring($media_file->fileContents());
1715b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
171629518ad2SGreg Roach
1717a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17183c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17193c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1720a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17213c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17223c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1723a6f13a4aSGreg Roach            } else {
1724136a8abaSGreg Roach                $width  = (float) $attributes[0];
1725136a8abaSGreg Roach                $height = (float) $attributes[1];
1726a6f13a4aSGreg Roach            }
17279458f20aSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1728e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1729a6f13a4aSGreg Roach        }
1730a6f13a4aSGreg Roach    }
1731a6f13a4aSGreg Roach
1732a6f13a4aSGreg Roach    /**
1733fab8f067SGreg Roach     * Handle <image/>
173476692c8bSGreg Roach     *
173509482a55SGreg Roach     * @param array<string> $attrs
17368ba2e626SGreg Roach     *
17378ba2e626SGreg Roach     * @return void
1738a6f13a4aSGreg Roach     */
1739b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1740c1010edaSGreg Roach    {
174183cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1742c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1743a6f13a4aSGreg Roach
1744a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1745c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1746a6f13a4aSGreg Roach
174783cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
174883cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1749a6f13a4aSGreg Roach
1750a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
175183cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1752a6f13a4aSGreg Roach
175383cdc021SGreg Roach        // Width, height (or both).
1754c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1755c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1756a6f13a4aSGreg Roach
175783cdc021SGreg Roach        $file = $attrs['file'] ?? '';
175883cdc021SGreg Roach
1759044416d2SGreg Roach        if ($file === '@FILE') {
176013abd6f3SGreg Roach            $match = [];
1761a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17626b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17634a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1764cdf416fbSGreg Roach
17659458f20aSGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists()) {
17669458f20aSGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents());
1767b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
176829518ad2SGreg Roach
1769a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17703c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17713c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1772a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17733c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17743c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1775a6f13a4aSGreg Roach                    } else {
1776136a8abaSGreg Roach                        $width  = (float) $attributes[0];
1777136a8abaSGreg Roach                        $height = (float) $attributes[1];
1778a6f13a4aSGreg Roach                    }
17799458f20aSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1780e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1781a6f13a4aSGreg Roach                }
1782a6f13a4aSGreg Roach            }
1783a6f13a4aSGreg Roach        } else {
17847a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1785a6f13a4aSGreg Roach                $size = getimagesize($file);
1786a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1787a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1788a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1789a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1790a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1791a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1792a6f13a4aSGreg Roach                } else {
1793a6f13a4aSGreg Roach                    $width  = $size[0];
1794a6f13a4aSGreg Roach                    $height = $size[1];
1795a6f13a4aSGreg Roach                }
1796e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1797e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1798a6f13a4aSGreg Roach            }
1799a6f13a4aSGreg Roach        }
1800a6f13a4aSGreg Roach    }
1801a6f13a4aSGreg Roach
1802a6f13a4aSGreg Roach    /**
1803fab8f067SGreg Roach     * Handle <line>
1804a6f13a4aSGreg Roach     *
180509482a55SGreg Roach     * @param array<string> $attrs
18068ba2e626SGreg Roach     *
18078ba2e626SGreg Roach     * @return void
1808a6f13a4aSGreg Roach     */
1809b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1810c1010edaSGreg Roach    {
1811a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1812c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1813a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18147a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1815a6f13a4aSGreg Roach                $x1 = 0;
18167a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1817c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1818a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1819c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1820a6f13a4aSGreg Roach            }
1821a6f13a4aSGreg Roach        }
1822a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1823c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1824a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18257a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1826a6f13a4aSGreg Roach                $y1 = 0;
18277a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1828c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1829a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1830c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1831a6f13a4aSGreg Roach            }
1832a6f13a4aSGreg Roach        }
1833a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1834c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1835a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18367a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1837a6f13a4aSGreg Roach                $x2 = 0;
18387a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1839c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1840a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1841c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1842a6f13a4aSGreg Roach            }
1843a6f13a4aSGreg Roach        }
1844a6f13a4aSGreg Roach        // End vertical position
1845c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1846a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18477a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1848a6f13a4aSGreg Roach                $y2 = 0;
18497a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1850c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1851a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1852c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1853a6f13a4aSGreg Roach            }
1854a6f13a4aSGreg Roach        }
1855a6f13a4aSGreg Roach
1856e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1857e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1858a6f13a4aSGreg Roach    }
1859a6f13a4aSGreg Roach
1860a6f13a4aSGreg Roach    /**
1861fab8f067SGreg Roach     * Handle <list>
1862a6f13a4aSGreg Roach     *
186309482a55SGreg Roach     * @param array<string> $attrs
18648ba2e626SGreg Roach     *
18658ba2e626SGreg Roach     * @return void
1866a6f13a4aSGreg Roach     */
1867b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1868c1010edaSGreg Roach    {
1869a6f13a4aSGreg Roach        $this->process_repeats++;
1870a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1871a6f13a4aSGreg Roach            return;
1872a6f13a4aSGreg Roach        }
1873a6f13a4aSGreg Roach
187413abd6f3SGreg Roach        $match = [];
1875a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1876a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1877a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1878d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1879a6f13a4aSGreg Roach                $sortby = trim($sortby);
1880a6f13a4aSGreg Roach            }
1881a6f13a4aSGreg Roach        } else {
18827a6ee1acSGreg Roach            $sortby = 'NAME';
1883a6f13a4aSGreg Roach        }
1884a6f13a4aSGreg Roach
1885e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1886195b5e75SGreg Roach
1887a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1888a6f13a4aSGreg Roach        switch ($listname) {
18897a6ee1acSGreg Roach            case 'pending':
189063b1851dSGreg Roach                $this->list = DB::table('change')
1891195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1892*059898c9SGreg Roach                        $query->select([new Expression('MAX(change_id)')])
1893195b5e75SGreg Roach                            ->from('change')
1894195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1895195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18967f5c2944SGreg Roach                            ->groupBy(['xref']);
1897195b5e75SGreg Roach                    })
189863b1851dSGreg Roach                    ->get()
189963b1851dSGreg Roach                    ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom))
190063b1851dSGreg Roach                    ->filter()
190163b1851dSGreg Roach                    ->all();
1902a6f13a4aSGreg Roach                break;
190363b1851dSGreg Roach
1904a6f13a4aSGreg Roach            case 'individual':
19055985adfbSGreg Roach                $query = DB::table('individuals')
19065985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19075985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19085985adfbSGreg Roach                    ->distinct();
19095985adfbSGreg Roach
1910a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1911dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
191282759250SGreg Roach                        $value = $this->substituteVars($value, false);
1913a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1914a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19150b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19165985adfbSGreg Roach                                $join
19175985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19185985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19195985adfbSGreg Roach                            });
19205985adfbSGreg Roach
19215985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19225985adfbSGreg Roach
1923a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19245985adfbSGreg Roach
1925044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19265985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1927a6f13a4aSGreg Roach                            } else {
19285985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1929a6f13a4aSGreg Roach                            }
19305985adfbSGreg Roach
19315985adfbSGreg Roach                            // This filter has been fully processed
19325985adfbSGreg Roach                            unset($attrs[$attr]);
19337ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19340b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19355985adfbSGreg Roach                                $join
19365985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19375985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19385985adfbSGreg Roach                            });
1939a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19407a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19415d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1942b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1943a6f13a4aSGreg Roach                            }
19445985adfbSGreg Roach
19455985adfbSGreg Roach                            // This filter has been fully processed
19465985adfbSGreg Roach                            unset($attrs[$attr]);
1947a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19485985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1949a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19505985adfbSGreg Roach
1951a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19525985adfbSGreg Roach
19535985adfbSGreg Roach                            // This filter has been fully processed
19545985adfbSGreg Roach                            unset($attrs[$attr]);
19552fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19569dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19575985adfbSGreg Roach                            $query
19580b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19595985adfbSGreg Roach                                    $join
19607ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19617ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19625985adfbSGreg Roach                                })
19630b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19647ee4bfadSGreg Roach                                    $join
19657ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19667ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19677ee4bfadSGreg Roach                                })
19689b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19696e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19705985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19717ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19726e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19736e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
197447a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19756e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19766e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1977e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19787ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1979a6f13a4aSGreg Roach                        }
1980a6f13a4aSGreg Roach                    }
1981a6f13a4aSGreg Roach                }
1982a6f13a4aSGreg Roach
198313abd6f3SGreg Roach                $this->list = [];
1984a6f13a4aSGreg Roach
19855985adfbSGreg Roach                foreach ($query->get() as $row) {
19866b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1987a6f13a4aSGreg Roach                }
1988a6f13a4aSGreg Roach                break;
1989a6f13a4aSGreg Roach
1990a6f13a4aSGreg Roach            case 'family':
199130fc2b1eSGreg Roach                $query = DB::table('families')
199230fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
199330fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
199430fc2b1eSGreg Roach                    ->distinct();
199530fc2b1eSGreg Roach
1996a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1997dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
199882759250SGreg Roach                        $value = $this->substituteVars($value, false);
1999a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2000a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20010b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200230fc2b1eSGreg Roach                                $join
200330fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
200430fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
200530fc2b1eSGreg Roach                            });
200630fc2b1eSGreg Roach
200730fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
200830fc2b1eSGreg Roach
2009a6f13a4aSGreg Roach                            $date = new Date($match[3]);
201030fc2b1eSGreg Roach
2011044416d2SGreg Roach                            if ($match[2] === 'LTE') {
201230fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2013a6f13a4aSGreg Roach                            } else {
201430fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2015a6f13a4aSGreg Roach                            }
201630fc2b1eSGreg Roach
201730fc2b1eSGreg Roach                            // This filter has been fully processed
201830fc2b1eSGreg Roach                            unset($attrs[$attr]);
2019a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
202030fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2021a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
202230fc2b1eSGreg Roach
2023a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
202430fc2b1eSGreg Roach
202530fc2b1eSGreg Roach                            // This filter has been fully processed
202630fc2b1eSGreg Roach                            unset($attrs[$attr]);
202730fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20283b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20290b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
203030fc2b1eSGreg Roach                                    $join
203130fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20323b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
203330fc2b1eSGreg Roach                                            $query
203430fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
203530fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
203630fc2b1eSGreg Roach                                        });
203730fc2b1eSGreg Roach                                });
20385d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20397a6ee1acSGreg Roach                                if ($match[1] != '') {
20407a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20415d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2042b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20435d0bc43dSGreg Roach                                    }
20445d0bc43dSGreg Roach                                }
2045a6f13a4aSGreg Roach                            }
204630fc2b1eSGreg Roach
204730fc2b1eSGreg Roach                            // This filter has been fully processed
204830fc2b1eSGreg Roach                            unset($attrs[$attr]);
20496e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20509dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
205130fc2b1eSGreg Roach                            $query
20520b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
205330fc2b1eSGreg Roach                                    $join
20546e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20556e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
205630fc2b1eSGreg Roach                                })
20570b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20586e5c0963SGreg Roach                                    $join
20596e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20606e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20616e5c0963SGreg Roach                                })
2062b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20636e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
206430fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20656e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20666e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20676e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20686e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20696e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20706e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2071e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20726e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2073a6f13a4aSGreg Roach                        }
2074a6f13a4aSGreg Roach                    }
2075a6f13a4aSGreg Roach                }
2076a6f13a4aSGreg Roach
207713abd6f3SGreg Roach                $this->list = [];
2078a6f13a4aSGreg Roach
207930fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20806b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2081a6f13a4aSGreg Roach                }
2082a6f13a4aSGreg Roach                break;
2083a6f13a4aSGreg Roach
2084a6f13a4aSGreg Roach            default:
20856ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2086a6f13a4aSGreg Roach        }
2087a6f13a4aSGreg Roach
208813abd6f3SGreg Roach        $filters  = [];
208913abd6f3SGreg Roach        $filters2 = [];
2090a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2091a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2092a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2093a6f13a4aSGreg Roach                    $condition = $value;
2094a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2095a6f13a4aSGreg Roach                        $id    = $match[1];
2096a6f13a4aSGreg Roach                        $value = "''";
2097044416d2SGreg Roach                        if ($id === 'ID') {
20987a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2099a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2100a6f13a4aSGreg Roach                            }
2101044416d2SGreg Roach                        } elseif ($id === 'fact') {
2102a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2103044416d2SGreg Roach                        } elseif ($id === 'desc') {
2104a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2105a6f13a4aSGreg Roach                        } else {
2106a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21077a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2108a6f13a4aSGreg Roach                            }
2109a6f13a4aSGreg Roach                        }
2110a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2111a6f13a4aSGreg Roach                    }
2112a6f13a4aSGreg Roach                    //-- handle regular expressions
2113a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2114a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2115a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2116a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2117a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2118d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2119a6f13a4aSGreg Roach                            $val = trim($val);
2120a6f13a4aSGreg Roach                        }
2121a6f13a4aSGreg Roach                        if ($val) {
21227a6ee1acSGreg Roach                            $searchstr = '';
21237a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2124a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2125a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2126a6f13a4aSGreg Roach                                $level = 1;
2127f71a7dedSGreg Roach                                $t = 'XXXX';
2128a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2129a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2130a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2131a6f13a4aSGreg Roach                                    }
2132a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2133044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21347a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2135a6f13a4aSGreg Roach                                    }
21367a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2137a6f13a4aSGreg Roach                                    $level++;
2138a6f13a4aSGreg Roach                                }
2139a6f13a4aSGreg Roach                            } else {
2140044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21417a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2142a6f13a4aSGreg Roach                                }
2143a6f13a4aSGreg Roach                                $t         = $tag;
21447a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2145a6f13a4aSGreg Roach                            }
2146a6f13a4aSGreg Roach                            switch ($expr) {
21477a6ee1acSGreg Roach                                case 'CONTAINS':
2148044416d2SGreg Roach                                    if ($t === 'PLAC') {
2149a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2150a6f13a4aSGreg Roach                                    } else {
2151a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2152a6f13a4aSGreg Roach                                    }
2153a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2154a6f13a4aSGreg Roach                                    break;
2155a6f13a4aSGreg Roach                                default:
2156c1010edaSGreg Roach                                    $filters2[] = [
2157c1010edaSGreg Roach                                        'tag'  => $tag,
2158c1010edaSGreg Roach                                        'expr' => $expr,
2159c1010edaSGreg Roach                                        'val'  => $val,
2160c1010edaSGreg Roach                                    ];
2161a6f13a4aSGreg Roach                                    break;
2162a6f13a4aSGreg Roach                            }
2163a6f13a4aSGreg Roach                        }
2164a6f13a4aSGreg Roach                    }
2165a6f13a4aSGreg Roach                }
2166a6f13a4aSGreg Roach            }
2167a6f13a4aSGreg Roach        }
2168a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2169a6f13a4aSGreg Roach        if ($filters) {
2170a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2171a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2172299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2173a6f13a4aSGreg Roach                        unset($this->list[$key]);
2174a6f13a4aSGreg Roach                        break;
2175a6f13a4aSGreg Roach                    }
2176a6f13a4aSGreg Roach                }
2177a6f13a4aSGreg Roach            }
2178a6f13a4aSGreg Roach        }
2179a6f13a4aSGreg Roach        if ($filters2) {
218013abd6f3SGreg Roach            $mylist = [];
2181a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2182c0935879SGreg Roach                $key  = $indi->xref();
2183299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2184a6f13a4aSGreg Roach                $keep = true;
2185a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2186a6f13a4aSGreg Roach                    if ($keep) {
2187a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2188a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2189a6f13a4aSGreg Roach                        $val  = $filter['val'];
2190b2448a1bSGreg Roach                        if ($val === "''") {
21917a6ee1acSGreg Roach                            $val = '';
2192a6f13a4aSGreg Roach                        }
21937a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2194a6f13a4aSGreg Roach                        $t    = end($tags);
21953d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2196a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2197044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21987a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21997a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2200a6f13a4aSGreg Roach                            $t    = end($tags);
2201b315f3e1SGreg Roach                            $v    = self::getSubRecord(1, $tag, $grec);
2202a6f13a4aSGreg Roach                        }
2203a6f13a4aSGreg Roach
2204a6f13a4aSGreg Roach                        switch ($expr) {
22057a6ee1acSGreg Roach                            case 'GTE':
2206044416d2SGreg Roach                                if ($t === 'DATE') {
2207a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2208a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2209a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2210a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2211a6f13a4aSGreg Roach                                    $keep = true;
2212a6f13a4aSGreg Roach                                }
2213a6f13a4aSGreg Roach                                break;
22147a6ee1acSGreg Roach                            case 'LTE':
2215044416d2SGreg Roach                                if ($t === 'DATE') {
2216a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2217a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2218a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2219a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2220a6f13a4aSGreg Roach                                    $keep = true;
2221a6f13a4aSGreg Roach                                }
2222a6f13a4aSGreg Roach                                break;
2223a6f13a4aSGreg Roach                            default:
2224a6f13a4aSGreg Roach                                if ($v == $val) {
2225a6f13a4aSGreg Roach                                    $keep = true;
2226a6f13a4aSGreg Roach                                } else {
2227a6f13a4aSGreg Roach                                    $keep = false;
2228a6f13a4aSGreg Roach                                }
2229a6f13a4aSGreg Roach                                break;
2230a6f13a4aSGreg Roach                        }
2231a6f13a4aSGreg Roach                    }
2232a6f13a4aSGreg Roach                }
2233a6f13a4aSGreg Roach                if ($keep) {
2234a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2235a6f13a4aSGreg Roach                }
2236a6f13a4aSGreg Roach            }
2237a6f13a4aSGreg Roach            $this->list = $mylist;
2238a6f13a4aSGreg Roach        }
2239a6f13a4aSGreg Roach
2240a6f13a4aSGreg Roach        switch ($sortby) {
2241a6f13a4aSGreg Roach            case 'NAME':
2242c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2243a6f13a4aSGreg Roach                break;
2244a6f13a4aSGreg Roach            case 'CHAN':
2245c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2246a6f13a4aSGreg Roach                break;
2247a6f13a4aSGreg Roach            case 'BIRT:DATE':
2248c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2249a6f13a4aSGreg Roach                break;
2250a6f13a4aSGreg Roach            case 'DEAT:DATE':
2251c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2252a6f13a4aSGreg Roach                break;
2253a6f13a4aSGreg Roach            case 'MARR:DATE':
2254c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2255a6f13a4aSGreg Roach                break;
2256a6f13a4aSGreg Roach            default:
2257a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2258a6f13a4aSGreg Roach                break;
2259a6f13a4aSGreg Roach        }
2260a6f13a4aSGreg Roach
22619b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2262e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2263a6f13a4aSGreg Roach    }
2264a6f13a4aSGreg Roach
2265a6f13a4aSGreg Roach    /**
2266fab8f067SGreg Roach     * Handle </list>
22678ba2e626SGreg Roach     *
22688ba2e626SGreg Roach     * @return void
2269a6f13a4aSGreg Roach     */
2270b702978eSGreg Roach    protected function listEndHandler(): void
2271c1010edaSGreg Roach    {
2272a6f13a4aSGreg Roach        $this->process_repeats--;
2273a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2274a6f13a4aSGreg Roach            return;
2275a6f13a4aSGreg Roach        }
2276a6f13a4aSGreg Roach
2277a6f13a4aSGreg Roach        // Check if there is any list
2278a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2279a6f13a4aSGreg Roach            $lineoffset = 0;
2280a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2281a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2282a6f13a4aSGreg Roach            }
2283a6f13a4aSGreg Roach            //-- read the xml from the file
2284299d100dSGreg Roach            $lines = file($this->report);
2285dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2286a6f13a4aSGreg Roach                $lineoffset--;
2287a6f13a4aSGreg Roach            }
2288a6f13a4aSGreg Roach            $lineoffset++;
2289a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2290a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2291a6f13a4aSGreg Roach            // List Level counter
2292a6f13a4aSGreg Roach            $count = 1;
2293a6f13a4aSGreg Roach            while (0 < $count) {
2294dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2295a6f13a4aSGreg Roach                    $count++;
2296dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2297a6f13a4aSGreg Roach                    $count--;
2298a6f13a4aSGreg Roach                }
2299a6f13a4aSGreg Roach                if (0 < $count) {
2300a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2301a6f13a4aSGreg Roach                }
2302a6f13a4aSGreg Roach                $line_nr++;
2303a6f13a4aSGreg Roach            }
2304a6f13a4aSGreg Roach            // No need to drag this
2305a6f13a4aSGreg Roach            unset($lines);
23067a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2307a6f13a4aSGreg Roach            // Save original values
23089b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2309a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2310a6f13a4aSGreg Roach
2311a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2312a6f13a4aSGreg Roach            $this->list_private = 0;
2313a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2314a6f13a4aSGreg Roach                if ($record->canShow()) {
2315f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2316a6f13a4aSGreg Roach                    //-- start the sax parser
2317a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2318e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2319f315390bSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
23201aa04befSGreg Roach
23211aa04befSGreg Roach                    xml_set_element_handler(
23221aa04befSGreg Roach                        $repeat_parser,
23239d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23241aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23251aa04befSGreg Roach                        },
23269d454b6bSGreg Roach                        function ($parser, string $name): void {
23271aa04befSGreg Roach                            $this->endElement($parser, $name);
23281aa04befSGreg Roach                        }
23291aa04befSGreg Roach                    );
23301aa04befSGreg Roach
23311aa04befSGreg Roach                    xml_set_character_data_handler(
23321aa04befSGreg Roach                        $repeat_parser,
23339d454b6bSGreg Roach                        function ($parser, string $data): void {
23341aa04befSGreg Roach                            $this->characterData($parser, $data);
23351aa04befSGreg Roach                        }
23361aa04befSGreg Roach                    );
23371aa04befSGreg Roach
2338a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23396ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2340a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2341a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2342a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2343a6f13a4aSGreg Roach                        ));
2344a6f13a4aSGreg Roach                    }
2345a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2346a6f13a4aSGreg Roach                } else {
2347a6f13a4aSGreg Roach                    $this->list_private++;
2348a6f13a4aSGreg Roach                }
2349a6f13a4aSGreg Roach            }
235013abd6f3SGreg Roach            $this->list   = [];
2351e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2352a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2353a6f13a4aSGreg Roach        }
235465e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2355a6f13a4aSGreg Roach    }
2356a6f13a4aSGreg Roach
2357a6f13a4aSGreg Roach    /**
2358fab8f067SGreg Roach     * Handle <listTotal>
2359a6f13a4aSGreg Roach     * Prints the total number of records in a list
2360fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23618ba2e626SGreg Roach     *
23628ba2e626SGreg Roach     * @return void
2363a6f13a4aSGreg Roach     */
2364b702978eSGreg Roach    protected function listTotalStartHandler(): void
2365c1010edaSGreg Roach    {
2366a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2367589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2368a6f13a4aSGreg Roach        } else {
23697a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2370a6f13a4aSGreg Roach        }
2371a6f13a4aSGreg Roach    }
2372a6f13a4aSGreg Roach
2373a6f13a4aSGreg Roach    /**
2374fab8f067SGreg Roach     * Handle <relatives>
237576692c8bSGreg Roach     *
237609482a55SGreg Roach     * @param array<string> $attrs
23778ba2e626SGreg Roach     *
23788ba2e626SGreg Roach     * @return void
2379a6f13a4aSGreg Roach     */
2380b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2381c1010edaSGreg Roach    {
2382a6f13a4aSGreg Roach        $this->process_repeats++;
2383a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2384a6f13a4aSGreg Roach            return;
2385a6f13a4aSGreg Roach        }
2386a6f13a4aSGreg Roach
2387e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2388e364afe4SGreg Roach
238913abd6f3SGreg Roach        $match = [];
2390a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2391d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2392a6f13a4aSGreg Roach            $sortby = trim($sortby);
2393a6f13a4aSGreg Roach        }
2394a6f13a4aSGreg Roach
2395a6f13a4aSGreg Roach        $maxgen = -1;
2396a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2397c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2398a6f13a4aSGreg Roach        }
2399a6f13a4aSGreg Roach
2400e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2401e364afe4SGreg Roach
2402a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2403d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2404a6f13a4aSGreg Roach            $group = trim($group);
2405a6f13a4aSGreg Roach        }
2406a6f13a4aSGreg Roach
2407e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2408e364afe4SGreg Roach
2409a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2410d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2411a6f13a4aSGreg Roach            $id = trim($id);
2412a6f13a4aSGreg Roach        }
2413a6f13a4aSGreg Roach
241413abd6f3SGreg Roach        $this->list = [];
24156b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2416d965cc1aSGreg Roach        if ($person instanceof Individual) {
2417a6f13a4aSGreg Roach            $this->list[$id] = $person;
2418a6f13a4aSGreg Roach            switch ($group) {
24197a6ee1acSGreg Roach                case 'child-family':
242039ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2421820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2422820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2423a6f13a4aSGreg Roach                        }
2424820b62dfSGreg Roach
2425820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2426c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2427a6f13a4aSGreg Roach                        }
2428a6f13a4aSGreg Roach                    }
2429a6f13a4aSGreg Roach                    break;
24307a6ee1acSGreg Roach                case 'spouse-family':
243139ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2432820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2433820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2434a6f13a4aSGreg Roach                        }
2435820b62dfSGreg Roach
2436820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2437c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2438a6f13a4aSGreg Roach                        }
2439a6f13a4aSGreg Roach                    }
2440a6f13a4aSGreg Roach                    break;
24417a6ee1acSGreg Roach                case 'direct-ancestors':
24423d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2443a6f13a4aSGreg Roach                    break;
24447a6ee1acSGreg Roach                case 'ancestors':
24453d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2446a6f13a4aSGreg Roach                    break;
24477a6ee1acSGreg Roach                case 'descendants':
2448a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24493d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2450a6f13a4aSGreg Roach                    break;
24517a6ee1acSGreg Roach                case 'all':
24523d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24533d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2454a6f13a4aSGreg Roach                    break;
2455a6f13a4aSGreg Roach            }
2456a6f13a4aSGreg Roach        }
2457a6f13a4aSGreg Roach
2458a6f13a4aSGreg Roach        switch ($sortby) {
2459a6f13a4aSGreg Roach            case 'NAME':
2460c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2461a6f13a4aSGreg Roach                break;
2462a6f13a4aSGreg Roach            case 'BIRT:DATE':
2463c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2464a6f13a4aSGreg Roach                break;
2465a6f13a4aSGreg Roach            case 'DEAT:DATE':
2466c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2467a6f13a4aSGreg Roach                break;
2468a6f13a4aSGreg Roach            case 'generation':
246913abd6f3SGreg Roach                $newarray = [];
2470a6f13a4aSGreg Roach                reset($this->list);
2471a6f13a4aSGreg Roach                $genCounter = 1;
2472a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2473a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2474a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2475a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2476f70bcff5SGreg Roach                            $newarray[$key] = (object) ['generation' => $this->generation];
2477a6f13a4aSGreg Roach                        }
2478a6f13a4aSGreg Roach                    }
2479a6f13a4aSGreg Roach                    $genCounter++;
2480a6f13a4aSGreg Roach                }
2481a6f13a4aSGreg Roach                $this->list = $newarray;
2482a6f13a4aSGreg Roach                break;
2483a6f13a4aSGreg Roach            default:
2484a6f13a4aSGreg Roach                // unsorted
2485a6f13a4aSGreg Roach                break;
2486a6f13a4aSGreg Roach        }
24879b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2488e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2489a6f13a4aSGreg Roach    }
2490a6f13a4aSGreg Roach
2491a6f13a4aSGreg Roach    /**
2492fab8f067SGreg Roach     * Handle </relatives>
24938ba2e626SGreg Roach     *
24948ba2e626SGreg Roach     * @return void
2495a6f13a4aSGreg Roach     */
2496b702978eSGreg Roach    protected function relativesEndHandler(): void
2497c1010edaSGreg Roach    {
2498a6f13a4aSGreg Roach        $this->process_repeats--;
2499a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2500a6f13a4aSGreg Roach            return;
2501a6f13a4aSGreg Roach        }
2502a6f13a4aSGreg Roach
2503a6f13a4aSGreg Roach        // Check if there is any relatives
2504a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2505a6f13a4aSGreg Roach            $lineoffset = 0;
2506a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2507a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2508a6f13a4aSGreg Roach            }
2509a6f13a4aSGreg Roach            //-- read the xml from the file
2510299d100dSGreg Roach            $lines = file($this->report);
2511dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2512a6f13a4aSGreg Roach                $lineoffset--;
2513a6f13a4aSGreg Roach            }
2514a6f13a4aSGreg Roach            $lineoffset++;
2515a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2516a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2517a6f13a4aSGreg Roach            // Relatives Level counter
2518a6f13a4aSGreg Roach            $count = 1;
2519a6f13a4aSGreg Roach            while (0 < $count) {
2520dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2521a6f13a4aSGreg Roach                    $count++;
2522dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2523a6f13a4aSGreg Roach                    $count--;
2524a6f13a4aSGreg Roach                }
2525a6f13a4aSGreg Roach                if (0 < $count) {
2526a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2527a6f13a4aSGreg Roach                }
2528a6f13a4aSGreg Roach                $line_nr++;
2529a6f13a4aSGreg Roach            }
2530a6f13a4aSGreg Roach            // No need to drag this
2531a6f13a4aSGreg Roach            unset($lines);
2532a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2533a6f13a4aSGreg Roach            // Save original values
25349b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2535a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2536a6f13a4aSGreg Roach
2537a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2538a6f13a4aSGreg Roach            $this->list_private = 0;
2539b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2540a6f13a4aSGreg Roach                if (isset($value->generation)) {
2541a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2542a6f13a4aSGreg Roach                }
25436b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2544299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2545a6f13a4aSGreg Roach
2546a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2547e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2548f315390bSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0);
25491aa04befSGreg Roach
25501aa04befSGreg Roach                xml_set_element_handler(
25511aa04befSGreg Roach                    $repeat_parser,
25529d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25531aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25541aa04befSGreg Roach                    },
25559d454b6bSGreg Roach                    function ($parser, string $name): void {
25561aa04befSGreg Roach                        $this->endElement($parser, $name);
25571aa04befSGreg Roach                    }
25581aa04befSGreg Roach                );
25591aa04befSGreg Roach
25601aa04befSGreg Roach                xml_set_character_data_handler(
25611aa04befSGreg Roach                    $repeat_parser,
25629d454b6bSGreg Roach                    function ($parser, string $data): void {
25631aa04befSGreg Roach                        $this->characterData($parser, $data);
25641aa04befSGreg Roach                    }
25651aa04befSGreg Roach                );
2566a6f13a4aSGreg Roach
2567a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25686ccdf4f0SGreg 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)));
2569a6f13a4aSGreg Roach                }
2570a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2571a6f13a4aSGreg Roach            }
2572a6f13a4aSGreg Roach            // Clean up the list array
257313abd6f3SGreg Roach            $this->list   = [];
2574e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2575a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2576a6f13a4aSGreg Roach        }
257765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2578a6f13a4aSGreg Roach    }
2579a6f13a4aSGreg Roach
2580a6f13a4aSGreg Roach    /**
2581fab8f067SGreg Roach     * Handle <generation />
2582a6f13a4aSGreg Roach     * Prints the number of generations
25838ba2e626SGreg Roach     *
25848ba2e626SGreg Roach     * @return void
2585a6f13a4aSGreg Roach     */
2586b702978eSGreg Roach    protected function generationStartHandler(): void
2587c1010edaSGreg Roach    {
2588589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2589a6f13a4aSGreg Roach    }
2590a6f13a4aSGreg Roach
2591a6f13a4aSGreg Roach    /**
2592fab8f067SGreg Roach     * Handle <newPage />
2593a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
25948ba2e626SGreg Roach     *
25958ba2e626SGreg Roach     * @return void
2596a6f13a4aSGreg Roach     */
2597b702978eSGreg Roach    protected function newPageStartHandler(): void
2598c1010edaSGreg Roach    {
25997a6ee1acSGreg Roach        $temp = 'addpage';
2600e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2601a6f13a4aSGreg Roach    }
2602a6f13a4aSGreg Roach
2603a6f13a4aSGreg Roach    /**
2604fab8f067SGreg Roach     * Handle </title>
26058ba2e626SGreg Roach     *
26068ba2e626SGreg Roach     * @return void
2607a6f13a4aSGreg Roach     */
2608b702978eSGreg Roach    protected function titleEndHandler(): void
2609c1010edaSGreg Roach    {
26102836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2611a6f13a4aSGreg Roach    }
2612a6f13a4aSGreg Roach
2613a6f13a4aSGreg Roach    /**
2614fab8f067SGreg Roach     * Handle </description>
26158ba2e626SGreg Roach     *
26168ba2e626SGreg Roach     * @return void
2617a6f13a4aSGreg Roach     */
2618b702978eSGreg Roach    protected function descriptionEndHandler(): void
2619c1010edaSGreg Roach    {
26202836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2621a6f13a4aSGreg Roach    }
2622729ce104SGreg Roach
2623729ce104SGreg Roach    /**
262476692c8bSGreg Roach     * Create a list of all descendants.
262576692c8bSGreg Roach     *
26261c7df322SGreg Roach     * @param array<Individual> $list
2627729ce104SGreg Roach     * @param string            $pid
2628729ce104SGreg Roach     * @param bool              $parents
2629729ce104SGreg Roach     * @param int               $generations
26308ba2e626SGreg Roach     *
26318ba2e626SGreg Roach     * @return void
2632729ce104SGreg Roach     */
26333b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2634c1010edaSGreg Roach    {
26356b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2636729ce104SGreg Roach        if ($person === null) {
2637729ce104SGreg Roach            return;
2638729ce104SGreg Roach        }
2639729ce104SGreg Roach        if (!isset($list[$pid])) {
2640729ce104SGreg Roach            $list[$pid] = $person;
2641729ce104SGreg Roach        }
2642729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2643729ce104SGreg Roach            $list[$pid]->generation = 0;
2644729ce104SGreg Roach        }
264539ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2646729ce104SGreg Roach            if ($parents) {
264739ca88baSGreg Roach                $husband = $family->husband();
264839ca88baSGreg Roach                $wife    = $family->wife();
2649729ce104SGreg Roach                if ($husband) {
2650c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2651729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2652c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2653729ce104SGreg Roach                    } else {
2654c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2655729ce104SGreg Roach                    }
2656729ce104SGreg Roach                }
2657729ce104SGreg Roach                if ($wife) {
2658c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2659729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2660c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2661729ce104SGreg Roach                    } else {
2662c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2663729ce104SGreg Roach                    }
2664729ce104SGreg Roach                }
2665729ce104SGreg Roach            }
2666820b62dfSGreg Roach
266739ca88baSGreg Roach            $children = $family->children();
2668820b62dfSGreg Roach
2669729ce104SGreg Roach            foreach ($children as $child) {
2670729ce104SGreg Roach                if ($child) {
2671c0935879SGreg Roach                    $list[$child->xref()] = $child;
2672820b62dfSGreg Roach
2673729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2674c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2675729ce104SGreg Roach                    } else {
2676c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2677729ce104SGreg Roach                    }
2678729ce104SGreg Roach                }
2679729ce104SGreg Roach            }
2680729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2681729ce104SGreg Roach                foreach ($children as $child) {
2682c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2683729ce104SGreg Roach                }
2684729ce104SGreg Roach            }
2685729ce104SGreg Roach        }
2686729ce104SGreg Roach    }
2687729ce104SGreg Roach
2688729ce104SGreg Roach    /**
268976692c8bSGreg Roach     * Create a list of all ancestors.
269076692c8bSGreg Roach     *
26911c7df322SGreg Roach     * @param array<Individual> $list
2692729ce104SGreg Roach     * @param string            $pid
2693729ce104SGreg Roach     * @param bool              $children
2694729ce104SGreg Roach     * @param int               $generations
26958ba2e626SGreg Roach     *
26968ba2e626SGreg Roach     * @return void
2697729ce104SGreg Roach     */
26983b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2699c1010edaSGreg Roach    {
270013abd6f3SGreg Roach        $genlist                = [$pid];
2701729ce104SGreg Roach        $list[$pid]->generation = 1;
2702729ce104SGreg Roach        while (count($genlist) > 0) {
2703729ce104SGreg Roach            $id = array_shift($genlist);
2704dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2705729ce104SGreg Roach                continue; // id can be something like “empty7”
2706729ce104SGreg Roach            }
27076b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
270839ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
270939ca88baSGreg Roach                $husband = $family->husband();
271039ca88baSGreg Roach                $wife    = $family->wife();
2711729ce104SGreg Roach                if ($husband) {
2712c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2713c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2714729ce104SGreg Roach                }
2715729ce104SGreg Roach                if ($wife) {
2716c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2717c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2718729ce104SGreg Roach                }
2719729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2720729ce104SGreg Roach                    if ($husband) {
2721c0935879SGreg Roach                        $genlist[] = $husband->xref();
2722729ce104SGreg Roach                    }
2723729ce104SGreg Roach                    if ($wife) {
2724c0935879SGreg Roach                        $genlist[] = $wife->xref();
2725729ce104SGreg Roach                    }
2726729ce104SGreg Roach                }
2727729ce104SGreg Roach                if ($children) {
272839ca88baSGreg Roach                    foreach ($family->children() as $child) {
2729c0935879SGreg Roach                        $list[$child->xref()] = $child;
2730e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2731729ce104SGreg Roach                    }
2732729ce104SGreg Roach                }
2733729ce104SGreg Roach            }
2734729ce104SGreg Roach        }
2735729ce104SGreg Roach    }
2736729ce104SGreg Roach
2737729ce104SGreg Roach    /**
2738729ce104SGreg Roach     * get gedcom tag value
2739729ce104SGreg Roach     *
2740729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2741729ce104SGreg 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
2742729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2743729ce104SGreg Roach     *
2744729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2745729ce104SGreg Roach     */
2746b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2747c1010edaSGreg Roach    {
2748b2448a1bSGreg Roach        if ($gedrec === '') {
2749729ce104SGreg Roach            return '';
2750729ce104SGreg Roach        }
2751729ce104SGreg Roach        $tags      = explode(':', $tag);
2752729ce104SGreg Roach        $origlevel = $level;
2753b2448a1bSGreg Roach        if ($level === 0) {
27543c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2755729ce104SGreg Roach        }
2756729ce104SGreg Roach
2757729ce104SGreg Roach        $subrec = $gedrec;
2758f71a7dedSGreg Roach        $t = 'XXXX';
2759729ce104SGreg Roach        foreach ($tags as $t) {
2760729ce104SGreg Roach            $lastsubrec = $subrec;
2761b315f3e1SGreg Roach            $subrec     = self::getSubRecord($level, "$level $t", $subrec);
2762729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2763729ce104SGreg Roach                $level--;
2764b315f3e1SGreg Roach                $subrec = self::getSubRecord($level, "$level $t", $lastsubrec);
2765729ce104SGreg Roach            }
2766729ce104SGreg Roach            if (empty($subrec)) {
2767044416d2SGreg Roach                if ($t === 'TITL') {
2768b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "$level ABBR", $lastsubrec);
2769729ce104SGreg Roach                    if (!empty($subrec)) {
27707a6ee1acSGreg Roach                        $t = 'ABBR';
2771729ce104SGreg Roach                    }
2772729ce104SGreg Roach                }
2773b2448a1bSGreg Roach                if ($subrec === '') {
2774729ce104SGreg Roach                    if ($level > 0) {
2775729ce104SGreg Roach                        $level--;
2776729ce104SGreg Roach                    }
2777b315f3e1SGreg Roach                    $subrec = self::getSubRecord($level, "@ $t", $gedrec);
2778b2448a1bSGreg Roach                    if ($subrec === '') {
2779729ce104SGreg Roach                        return '';
2780729ce104SGreg Roach                    }
2781729ce104SGreg Roach                }
2782729ce104SGreg Roach            }
2783729ce104SGreg Roach            $level++;
2784729ce104SGreg Roach        }
2785729ce104SGreg Roach        $level--;
2786729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2787f71a7dedSGreg Roach        if ($ct === 0) {
2788729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2789729ce104SGreg Roach        }
2790f71a7dedSGreg Roach        if ($ct === 0) {
2791729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2792729ce104SGreg Roach        }
2793729ce104SGreg Roach        if ($ct > 0) {
2794729ce104SGreg Roach            $value = trim($match[1]);
2795044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
27966b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2797ff166e64SGreg Roach                if ($note instanceof Note) {
2798729ce104SGreg Roach                    $value = $note->getNote();
2799729ce104SGreg Roach                } else {
2800729ce104SGreg Roach                    //-- set the value to the id without the @
2801729ce104SGreg Roach                    $value = $match[1];
2802729ce104SGreg Roach                }
2803729ce104SGreg Roach            }
2804f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
2805b315f3e1SGreg Roach                $value .= self::getCont($level + 1, $subrec);
2806729ce104SGreg Roach            }
2807729ce104SGreg Roach
2808729ce104SGreg Roach            return $value;
2809729ce104SGreg Roach        }
2810729ce104SGreg Roach
28117a6ee1acSGreg Roach        return '';
2812729ce104SGreg Roach    }
2813d1286247SGreg Roach
2814d1286247SGreg Roach    /**
2815d1286247SGreg Roach     * Replace variable identifiers with their values.
2816d1286247SGreg Roach     *
2817d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
281882759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2819d1286247SGreg Roach     *
2820d1286247SGreg Roach     * @return string
2821d1286247SGreg Roach     */
28228f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2823c1010edaSGreg Roach    {
2824d1286247SGreg Roach        return preg_replace_callback(
2825d1286247SGreg Roach            '/\$(\w+)/',
282618d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28272118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
282882759250SGreg Roach                    if ($quote) {
28292118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2830b2ce94c6SRico Sonntag                    }
2831b2ce94c6SRico Sonntag
28322118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
283382759250SGreg Roach                }
2834b2ce94c6SRico Sonntag
2835d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28363d7a8a4cSGreg Roach
2837d1286247SGreg Roach                return '$' . $matches[1];
2838d1286247SGreg Roach            },
2839d1286247SGreg Roach            $expression
2840d1286247SGreg Roach        );
2841d1286247SGreg Roach    }
2842a6f13a4aSGreg Roach}
2843