xref: /webtrees/app/Report/ReportParserGenerate.php (revision 63b1851d18dc5df8dcb1a599116a3831998ab96c)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
244459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
25a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
2697def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
283d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
299a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
33d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
34a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
35729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
36a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
37d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
38299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
39195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
40195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
41a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
425985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
439a9e551aSGreg Roachuse Illuminate\Support\Str;
44f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
4531d9777aSGreg Roachuse LogicException;
46c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
475809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
484e69366aSGreg Roachuse XMLParser;
4959597b37SGreg Roach
50b6f35a76SGreg Roachuse function addcslashes;
51b6f35a76SGreg Roachuse function addslashes;
52b6f35a76SGreg Roachuse function array_pop;
53b6f35a76SGreg Roachuse function array_shift;
54b6f35a76SGreg Roachuse function assert;
55b19e047dSGreg Roachuse function call_user_func;
56b6f35a76SGreg Roachuse function count;
57b6f35a76SGreg Roachuse function end;
58b2448a1bSGreg Roachuse function explode;
59b6f35a76SGreg Roachuse function file;
60b6f35a76SGreg Roachuse function file_exists;
61b6f35a76SGreg Roachuse function getimagesize;
6229518ad2SGreg Roachuse function imagecreatefromstring;
6329518ad2SGreg Roachuse function imagesx;
6429518ad2SGreg Roachuse function imagesy;
65b6f35a76SGreg Roachuse function in_array;
66b19e047dSGreg Roachuse function method_exists;
67b6f35a76SGreg Roachuse function preg_match;
68b6f35a76SGreg Roachuse function preg_match_all;
69b6f35a76SGreg Roachuse function preg_replace;
70b6f35a76SGreg Roachuse function preg_replace_callback;
71b6f35a76SGreg Roachuse function preg_split;
72b6f35a76SGreg Roachuse function reset;
73b6f35a76SGreg Roachuse function round;
74b6f35a76SGreg Roachuse function sprintf;
75dec352c1SGreg Roachuse function str_contains;
76b6f35a76SGreg Roachuse function str_replace;
77dec352c1SGreg Roachuse function str_starts_with;
78b6f35a76SGreg Roachuse function strip_tags;
79b6f35a76SGreg Roachuse function strlen;
80b6f35a76SGreg Roachuse function strtoupper;
81b6f35a76SGreg Roachuse function substr;
82b2448a1bSGreg Roachuse function trim;
83b6f35a76SGreg Roachuse function uasort;
84b6f35a76SGreg Roachuse function xml_error_string;
85b6f35a76SGreg Roachuse function xml_get_current_line_number;
86b6f35a76SGreg Roachuse function xml_get_error_code;
87b6f35a76SGreg Roachuse function xml_parse;
88b6f35a76SGreg Roachuse function xml_parser_create;
89b6f35a76SGreg Roachuse function xml_parser_free;
90b6f35a76SGreg Roachuse function xml_parser_set_option;
91b6f35a76SGreg Roachuse function xml_set_character_data_handler;
92b6f35a76SGreg Roachuse function xml_set_element_handler;
93b6f35a76SGreg Roach
94b6f35a76SGreg Roachuse const PREG_SET_ORDER;
95b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9629518ad2SGreg Roach
97a6f13a4aSGreg Roach/**
98a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
99a6f13a4aSGreg Roach */
100c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
101c1010edaSGreg Roach{
102a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
103a6f13a4aSGreg Roach    private $process_footnote = true;
104a6f13a4aSGreg Roach
105a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
106a6f13a4aSGreg Roach    private $print_data = false;
107a6f13a4aSGreg Roach
108a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
10913abd6f3SGreg Roach    private $print_data_stack = [];
110a6f13a4aSGreg Roach
11176692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
112a6f13a4aSGreg Roach    private $process_gedcoms = 0;
113a6f13a4aSGreg Roach
11476692c8bSGreg Roach    /** @var int Are we processing conditionals */
115a6f13a4aSGreg Roach    private $process_ifs = 0;
116a6f13a4aSGreg Roach
11776692c8bSGreg Roach    /** @var int Are we processing repeats */
118a6f13a4aSGreg Roach    private $process_repeats = 0;
119a6f13a4aSGreg Roach
120a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
121a6f13a4aSGreg Roach    private $repeat_bytes = 0;
122a6f13a4aSGreg Roach
12309482a55SGreg Roach    /** @var array<string> Repeated data when iterating over loops */
12409482a55SGreg Roach    private array $repeats = [];
125a6f13a4aSGreg Roach
12609482a55SGreg Roach    /** @var array<int,array<int,array<string>|int>> Nested repeating data */
12709482a55SGreg Roach    private array $repeats_stack = [];
128a6f13a4aSGreg Roach
12909482a55SGreg Roach    /** @var array<AbstractRenderer> Nested repeating data */
13009482a55SGreg Roach    private array $wt_report_stack = [];
131e8e7866bSGreg Roach
1324e69366aSGreg Roach    /** @var XMLParser (resource before PHP 8.0) Nested repeating data */
133e8e7866bSGreg Roach    private $parser;
134e8e7866bSGreg Roach
1354e69366aSGreg Roach    /** @var XMLParser[] (resource[] before PHP 8.0) Nested repeating data */
1364e69366aSGreg Roach    private array $parser_stack = [];
137e8e7866bSGreg Roach
138a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
139a6f13a4aSGreg Roach    private $gedrec = '';
140a6f13a4aSGreg Roach
14109482a55SGreg Roach    /** @var array<string> Nested GEDCOM records */
1424e69366aSGreg Roach    private array $gedrec_stack = [];
143a6f13a4aSGreg Roach
144a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
145a6f13a4aSGreg Roach    private $current_element;
146a6f13a4aSGreg Roach
147a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
148a6f13a4aSGreg Roach    private $footnote_element;
149a6f13a4aSGreg Roach
150a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
151a6f13a4aSGreg Roach    private $fact = '';
152a6f13a4aSGreg Roach
153a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
154a6f13a4aSGreg Roach    private $desc = '';
155a6f13a4aSGreg Roach
156a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
157a6f13a4aSGreg Roach    private $type = '';
158a6f13a4aSGreg Roach
159a6f13a4aSGreg Roach    /** @var int The current generational level */
160a6f13a4aSGreg Roach    private $generation = 1;
161a6f13a4aSGreg Roach
162a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
1634e69366aSGreg Roach    private array $list = [];
164a6f13a4aSGreg Roach
165a6f13a4aSGreg Roach    /** @var int Number of items in lists */
166a6f13a4aSGreg Roach    private $list_total = 0;
167a6f13a4aSGreg Roach
168a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
169a6f13a4aSGreg Roach    private $list_private = 0;
170a6f13a4aSGreg Roach
171299d100dSGreg Roach    /** @var string The filename of the XML report */
172299d100dSGreg Roach    protected $report;
173299d100dSGreg Roach
174b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
175e8e7866bSGreg Roach    private $report_root;
176e8e7866bSGreg Roach
177b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
178e8e7866bSGreg Roach    private $wt_report;
179e8e7866bSGreg Roach
18009482a55SGreg Roach    /** @var array<array<string>> Variables defined in the report at run-time */
1814e69366aSGreg Roach    private array $vars;
182d1286247SGreg Roach
1834e69366aSGreg Roach    private Tree $tree;
184299d100dSGreg Roach
1854e69366aSGreg Roach    private FilesystemOperator $data_filesystem;
186a04bb9a2SGreg Roach
18776692c8bSGreg Roach    /**
18876692c8bSGreg Roach     * Create a parser for a report
18976692c8bSGreg Roach     *
19076692c8bSGreg Roach     * @param string               $report The XML filename
191b6f35a76SGreg Roach     * @param AbstractRenderer     $report_root
19209482a55SGreg Roach     * @param array<array<string>> $vars
193299d100dSGreg Roach     * @param Tree                 $tree
194f7cf8a15SGreg Roach     * @param FilesystemOperator   $data_filesystem
19576692c8bSGreg Roach     */
196a04bb9a2SGreg Roach    public function __construct(
197a04bb9a2SGreg Roach        string $report,
198b6f35a76SGreg Roach        AbstractRenderer $report_root,
199a04bb9a2SGreg Roach        array $vars,
200a04bb9a2SGreg Roach        Tree $tree,
201f7cf8a15SGreg Roach        FilesystemOperator $data_filesystem
202a04bb9a2SGreg Roach    ) {
203299d100dSGreg Roach        $this->report          = $report;
204e8e7866bSGreg Roach        $this->report_root     = $report_root;
205e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20659f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
207d1286247SGreg Roach        $this->vars            = $vars;
208299d100dSGreg Roach        $this->tree            = $tree;
209a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
210299d100dSGreg Roach
21176f666f4SGreg Roach        parent::__construct($report);
212a6f13a4aSGreg Roach    }
213a6f13a4aSGreg Roach
214a6f13a4aSGreg Roach    /**
215a6f13a4aSGreg Roach     * XML start element handler
216a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
217a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
218a6f13a4aSGreg Roach     *
219a6f13a4aSGreg Roach     * @param resource      $parser the resource handler for the XML parser
220a6f13a4aSGreg Roach     * @param string        $name   the name of the XML element parsed
22109482a55SGreg Roach     * @param array<string> $attrs  an array of key value pairs for the attributes
22218d7a90dSGreg Roach     *
22318d7a90dSGreg Roach     * @return void
224a6f13a4aSGreg Roach     */
225af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
226c1010edaSGreg Roach    {
22713abd6f3SGreg Roach        $newattrs = [];
228a6f13a4aSGreg Roach
229a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
230a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
231e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
232d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
233a6f13a4aSGreg Roach                }
234a6f13a4aSGreg Roach            }
235a6f13a4aSGreg Roach            $newattrs[$key] = $value;
236a6f13a4aSGreg Roach        }
237a6f13a4aSGreg Roach        $attrs = $newattrs;
2387a6ee1acSGreg 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')) {
239a0e2939aSGreg Roach            $method = $name . 'StartHandler';
240208e9f76SGreg Roach
241a0e2939aSGreg Roach            if (method_exists($this, $method)) {
242b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
243a6f13a4aSGreg Roach            }
244a6f13a4aSGreg Roach        }
245a6f13a4aSGreg Roach    }
246a6f13a4aSGreg Roach
247a6f13a4aSGreg Roach    /**
248a6f13a4aSGreg Roach     * XML end element handler
249a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
250a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
251a6f13a4aSGreg Roach     *
252a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
253a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
25418d7a90dSGreg Roach     *
25518d7a90dSGreg Roach     * @return void
256a6f13a4aSGreg Roach     */
257af14d238SGreg Roach    protected function endElement($parser, string $name): void
258c1010edaSGreg Roach    {
2597a6ee1acSGreg 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')) {
260a0e2939aSGreg Roach            $method = $name . 'EndHandler';
261a0e2939aSGreg Roach
262a0e2939aSGreg Roach            if (method_exists($this, $method)) {
263b19e047dSGreg Roach                call_user_func([$this, $method]);
264a6f13a4aSGreg Roach            }
265a6f13a4aSGreg Roach        }
266a6f13a4aSGreg Roach    }
267a6f13a4aSGreg Roach
268a6f13a4aSGreg Roach    /**
269a6f13a4aSGreg Roach     * XML character data handler
270a6f13a4aSGreg Roach     *
271a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
272a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
27318d7a90dSGreg Roach     *
27418d7a90dSGreg Roach     * @return void
275a6f13a4aSGreg Roach     */
27624f2a3afSGreg Roach    protected function characterData($parser, string $data): void
277c1010edaSGreg Roach    {
278e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
279a6f13a4aSGreg Roach            $this->current_element->addText($data);
280a6f13a4aSGreg Roach        }
281a6f13a4aSGreg Roach    }
282a6f13a4aSGreg Roach
283a6f13a4aSGreg Roach    /**
284fab8f067SGreg Roach     * Handle <style>
285a6f13a4aSGreg Roach     *
28609482a55SGreg Roach     * @param array<string> $attrs
2878ba2e626SGreg Roach     *
2888ba2e626SGreg Roach     * @return void
289a6f13a4aSGreg Roach     */
290b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
291c1010edaSGreg Roach    {
292a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2936ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
294a6f13a4aSGreg Roach        }
295a6f13a4aSGreg Roach
296a6f13a4aSGreg Roach        // array Style that will be passed on
29713abd6f3SGreg Roach        $s = [];
298a6f13a4aSGreg Roach
299a6f13a4aSGreg Roach        // string Name af the style
300a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
301a6f13a4aSGreg Roach
302a6f13a4aSGreg Roach        // string Name of the DEFAULT font
303208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
304a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
305a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
306a6f13a4aSGreg Roach        }
307a6f13a4aSGreg Roach
308a6f13a4aSGreg Roach        // int The size of the font in points
309208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
310a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
31159597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
31259597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
31359597b37SGreg Roach        }
314a6f13a4aSGreg Roach
315a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3167a6ee1acSGreg Roach        $s['style'] = '';
317a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
318a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
319a6f13a4aSGreg Roach        }
320a6f13a4aSGreg Roach
321e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
322a6f13a4aSGreg Roach    }
323a6f13a4aSGreg Roach
324a6f13a4aSGreg Roach    /**
325fab8f067SGreg Roach     * Handle <doc>
326a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
327a6f13a4aSGreg Roach     *
32809482a55SGreg Roach     * @param array<string> $attrs
3298ba2e626SGreg Roach     *
3308ba2e626SGreg Roach     * @return void
331a6f13a4aSGreg Roach     */
332b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
333c1010edaSGreg Roach    {
334e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
335a6f13a4aSGreg Roach
336a6f13a4aSGreg Roach        // Custom page width
337a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
338208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
339a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
340a6f13a4aSGreg Roach        // Custom Page height
341a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
342208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
343a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
344a6f13a4aSGreg Roach
345a6f13a4aSGreg Roach        // Left Margin
346a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
3477a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
348208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
349a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
350208e9f76SGreg Roach                $this->wt_report->left_margin = (int) $attrs['leftmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
351a6f13a4aSGreg Roach            }
352a6f13a4aSGreg Roach        }
353a6f13a4aSGreg Roach        // Right Margin
354a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3557a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
356208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
357a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
358208e9f76SGreg Roach                $this->wt_report->right_margin = (int) $attrs['rightmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
359a6f13a4aSGreg Roach            }
360a6f13a4aSGreg Roach        }
361a6f13a4aSGreg Roach        // Top Margin
362a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3637a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
364208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
365a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
366208e9f76SGreg Roach                $this->wt_report->top_margin = (int) $attrs['topmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
367a6f13a4aSGreg Roach            }
368a6f13a4aSGreg Roach        }
369a6f13a4aSGreg Roach        // Bottom Margin
370a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3717a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
372208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
373a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
374208e9f76SGreg Roach                $this->wt_report->bottom_margin = (int) $attrs['bottommargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
375a6f13a4aSGreg Roach            }
376a6f13a4aSGreg Roach        }
377a6f13a4aSGreg Roach        // Header Margin
378a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3797a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
380208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
381a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
382208e9f76SGreg Roach                $this->wt_report->header_margin = (int) $attrs['headermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
383a6f13a4aSGreg Roach            }
384a6f13a4aSGreg Roach        }
385a6f13a4aSGreg Roach        // Footer Margin
386a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3877a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
388208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
389a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
390208e9f76SGreg Roach                $this->wt_report->footer_margin = (int) $attrs['footermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
391a6f13a4aSGreg Roach            }
392a6f13a4aSGreg Roach        }
393a6f13a4aSGreg Roach
394a6f13a4aSGreg Roach        // Page Orientation
395a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
396044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3977a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
398044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3997a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
400a6f13a4aSGreg Roach            }
401a6f13a4aSGreg Roach        }
402a6f13a4aSGreg Roach        // Page Size
403a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
404208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
405a6f13a4aSGreg Roach        }
406a6f13a4aSGreg Roach
407a6f13a4aSGreg Roach        // Show Generated By...
408a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4097a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
410208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4117a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
412208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
413a6f13a4aSGreg Roach            }
414a6f13a4aSGreg Roach        }
415a6f13a4aSGreg Roach
416e8e7866bSGreg Roach        $this->wt_report->setup();
417a6f13a4aSGreg Roach    }
418a6f13a4aSGreg Roach
419a6f13a4aSGreg Roach    /**
420fab8f067SGreg Roach     * Handle </doc>
4218ba2e626SGreg Roach     *
4228ba2e626SGreg Roach     * @return void
423a6f13a4aSGreg Roach     */
424b702978eSGreg Roach    protected function docEndHandler(): void
425c1010edaSGreg Roach    {
426e8e7866bSGreg Roach        $this->wt_report->run();
427a6f13a4aSGreg Roach    }
428a6f13a4aSGreg Roach
429a6f13a4aSGreg Roach    /**
430fab8f067SGreg Roach     * Handle <header>
4318ba2e626SGreg Roach     *
4328ba2e626SGreg Roach     * @return void
433a6f13a4aSGreg Roach     */
434b702978eSGreg Roach    protected function headerStartHandler(): void
435c1010edaSGreg Roach    {
436a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
437e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4387a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
439a6f13a4aSGreg Roach    }
440a6f13a4aSGreg Roach
441a6f13a4aSGreg Roach    /**
442fab8f067SGreg Roach     * Handle <body>
4438ba2e626SGreg Roach     *
4448ba2e626SGreg Roach     * @return void
445a6f13a4aSGreg Roach     */
446b702978eSGreg Roach    protected function bodyStartHandler(): void
447c1010edaSGreg Roach    {
4487a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
449a6f13a4aSGreg Roach    }
450a6f13a4aSGreg Roach
451a6f13a4aSGreg Roach    /**
452fab8f067SGreg Roach     * Handle <footer>
4538ba2e626SGreg Roach     *
4548ba2e626SGreg Roach     * @return void
455a6f13a4aSGreg Roach     */
456b702978eSGreg Roach    protected function footerStartHandler(): void
457c1010edaSGreg Roach    {
4587a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
459a6f13a4aSGreg Roach    }
460a6f13a4aSGreg Roach
461a6f13a4aSGreg Roach    /**
462fab8f067SGreg Roach     * Handle <cell>
463a6f13a4aSGreg Roach     *
46477bab461SGreg Roach     * @param array<string,string> $attrs
4658ba2e626SGreg Roach     *
4668ba2e626SGreg Roach     * @return void
467a6f13a4aSGreg Roach     */
468b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
469c1010edaSGreg Roach    {
470a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
47177bab461SGreg Roach        $align = $attrs['align'] ?? '';
472a6f13a4aSGreg Roach        // RTL supported left/right alignment
473044416d2SGreg Roach        if ($align === 'rightrtl') {
474e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
4757a6ee1acSGreg Roach                $align = 'left';
476a6f13a4aSGreg Roach            } else {
4777a6ee1acSGreg Roach                $align = 'right';
478a6f13a4aSGreg Roach            }
479044416d2SGreg Roach        } elseif ($align === 'leftrtl') {
480e8e7866bSGreg Roach            if ($this->wt_report->rtl) {
4817a6ee1acSGreg Roach                $align = 'right';
482a6f13a4aSGreg Roach            } else {
4837a6ee1acSGreg Roach                $align = 'left';
484a6f13a4aSGreg Roach            }
485a6f13a4aSGreg Roach        }
486a6f13a4aSGreg Roach
48777bab461SGreg Roach        // The color to fill the background of this cell
48877bab461SGreg Roach        $bgcolor = $attrs['bgcolor'] ?? '';
489a6f13a4aSGreg Roach
49077bab461SGreg Roach        // Whether the background should be painted
49177bab461SGreg Roach        $fill = (int) ($attrs['fill'] ?? '0');
492a6f13a4aSGreg Roach
49377bab461SGreg Roach        // If true reset the last cell height
49477bab461SGreg Roach        $reseth = (bool) ($attrs['reseth'] ?? '1');
495a6f13a4aSGreg Roach
49677bab461SGreg Roach        // Whether a border should be printed around this box
49777bab461SGreg Roach        $border = $attrs['border'] ?? '';
49877bab461SGreg Roach
499a6f13a4aSGreg Roach        // string Border color in HTML code
50077bab461SGreg Roach        $bocolor = $attrs['bocolor'] ?? '';
501a6f13a4aSGreg Roach
50277bab461SGreg Roach        // Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
50377bab461SGreg Roach        $height = (int) ($attrs['height'] ?? '0');
50477bab461SGreg Roach
505a6f13a4aSGreg 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.
50677bab461SGreg Roach        $width = (int) ($attrs['width'] ?? '0');
507a6f13a4aSGreg Roach
50877bab461SGreg Roach        // Stretch character mode
50977bab461SGreg Roach        $stretch = (int) ($attrs['stretch'] ?? '0');
510a6f13a4aSGreg Roach
511a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
512c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
513a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5147a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
515c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
516a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
517a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5187a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
519a6f13a4aSGreg Roach                $left = 0;
520a6f13a4aSGreg Roach            }
521a6f13a4aSGreg Roach        }
522a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
523c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
524a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5257a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
526c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
527a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
528a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5297a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
530a6f13a4aSGreg Roach                $top = 0;
531a6f13a4aSGreg Roach            }
532a6f13a4aSGreg Roach        }
533a6f13a4aSGreg Roach
53477bab461SGreg Roach        // The name of the Style that should be used to render the text.
53577bab461SGreg Roach        $style = $attrs['style'] ?? '';
536a6f13a4aSGreg Roach
537a6f13a4aSGreg Roach        // string Text color in html code
53877bab461SGreg Roach        $tcolor = $attrs['tcolor'] ?? '';
539a6f13a4aSGreg Roach
540a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
541a6f13a4aSGreg Roach        $ln = 0;
542a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
543a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
544a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5457a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
546a6f13a4aSGreg Roach                $ln = 0;
547a6f13a4aSGreg Roach            }
548a6f13a4aSGreg Roach        }
549a6f13a4aSGreg Roach
550044416d2SGreg Roach        if ($align === 'left') {
5517a6ee1acSGreg Roach            $align = 'L';
552044416d2SGreg Roach        } elseif ($align === 'right') {
5537a6ee1acSGreg Roach            $align = 'R';
554044416d2SGreg Roach        } elseif ($align === 'center') {
5557a6ee1acSGreg Roach            $align = 'C';
556044416d2SGreg Roach        } elseif ($align === 'justify') {
5577a6ee1acSGreg Roach            $align = 'J';
558a6f13a4aSGreg Roach        }
559a6f13a4aSGreg Roach
5609b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
561a6f13a4aSGreg Roach        $this->print_data         = true;
562a6f13a4aSGreg Roach
563e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
56424f2a3afSGreg Roach            (int) $width,
56524f2a3afSGreg Roach            (int) $height,
566a6f13a4aSGreg Roach            $border,
567a6f13a4aSGreg Roach            $align,
568a6f13a4aSGreg Roach            $bgcolor,
569a6f13a4aSGreg Roach            $style,
570a6f13a4aSGreg Roach            $ln,
571a6f13a4aSGreg Roach            $top,
572a6f13a4aSGreg Roach            $left,
573a6f13a4aSGreg Roach            $fill,
574a6f13a4aSGreg Roach            $stretch,
575a6f13a4aSGreg Roach            $bocolor,
576a6f13a4aSGreg Roach            $tcolor,
577a6f13a4aSGreg Roach            $reseth
578a6f13a4aSGreg Roach        );
579a6f13a4aSGreg Roach    }
580a6f13a4aSGreg Roach
581a6f13a4aSGreg Roach    /**
582fab8f067SGreg Roach     * Handle </cell>
5838ba2e626SGreg Roach     *
5848ba2e626SGreg Roach     * @return void
585a6f13a4aSGreg Roach     */
586b702978eSGreg Roach    protected function cellEndHandler(): void
587c1010edaSGreg Roach    {
588a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
589e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
590a6f13a4aSGreg Roach    }
591a6f13a4aSGreg Roach
592a6f13a4aSGreg Roach    /**
593fab8f067SGreg Roach     * Handle <now />
5948ba2e626SGreg Roach     *
5958ba2e626SGreg Roach     * @return void
596a6f13a4aSGreg Roach     */
597b702978eSGreg Roach    protected function nowStartHandler(): void
598c1010edaSGreg Roach    {
5994459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
600a6f13a4aSGreg Roach    }
601a6f13a4aSGreg Roach
602a6f13a4aSGreg Roach    /**
603fab8f067SGreg Roach     * Handle <pageNum />
6048ba2e626SGreg Roach     *
6058ba2e626SGreg Roach     * @return void
606a6f13a4aSGreg Roach     */
607b702978eSGreg Roach    protected function pageNumStartHandler(): void
608c1010edaSGreg Roach    {
6097a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
610a6f13a4aSGreg Roach    }
611a6f13a4aSGreg Roach
612a6f13a4aSGreg Roach    /**
613fab8f067SGreg Roach     * Handle <totalPages />
6148ba2e626SGreg Roach     *
6158ba2e626SGreg Roach     * @return void
616a6f13a4aSGreg Roach     */
617b702978eSGreg Roach    protected function totalPagesStartHandler(): void
618c1010edaSGreg Roach    {
6197a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
620a6f13a4aSGreg Roach    }
621a6f13a4aSGreg Roach
622a6f13a4aSGreg Roach    /**
623a6f13a4aSGreg Roach     * Called at the start of an element.
624a6f13a4aSGreg Roach     *
62509482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
6268ba2e626SGreg Roach     *
6278ba2e626SGreg Roach     * @return void
628a6f13a4aSGreg Roach     */
629b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
630c1010edaSGreg Roach    {
631a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
632a6f13a4aSGreg Roach            $this->process_gedcoms++;
633a6f13a4aSGreg Roach
634a6f13a4aSGreg Roach            return;
635a6f13a4aSGreg Roach        }
636a6f13a4aSGreg Roach
637a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6387a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6397a6ee1acSGreg Roach        $tags      = explode(':', $tag);
640a6f13a4aSGreg Roach        $newgedrec = '';
641a6f13a4aSGreg Roach        if (count($tags) < 2) {
6426b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
643299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
644a6f13a4aSGreg Roach        }
645a6f13a4aSGreg Roach        if (empty($newgedrec)) {
646a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
647a6f13a4aSGreg Roach            $newgedrec = '';
648a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6497a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
650d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
651d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
652a6f13a4aSGreg Roach                    } else {
6536b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
654299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
655a6f13a4aSGreg Roach                    }
656a6f13a4aSGreg Roach                } else {
6577a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
65813abd6f3SGreg Roach                        $gmatch = [];
659a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
6606b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
661299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
662a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
663a6f13a4aSGreg Roach                        } else {
664a6f13a4aSGreg Roach                            $newgedrec = '';
665a6f13a4aSGreg Roach                            break;
666a6f13a4aSGreg Roach                        }
667a6f13a4aSGreg Roach                    } else {
668b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
6693d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
670a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
671a6f13a4aSGreg Roach                    }
672a6f13a4aSGreg Roach                }
673a6f13a4aSGreg Roach            }
674a6f13a4aSGreg Roach        }
675a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6769b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
677a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
678a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
679a6f13a4aSGreg Roach                $this->fact = $match[2];
680a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
681a6f13a4aSGreg Roach            }
682a6f13a4aSGreg Roach        } else {
683a6f13a4aSGreg Roach            $this->process_gedcoms++;
684a6f13a4aSGreg Roach        }
685a6f13a4aSGreg Roach    }
686a6f13a4aSGreg Roach
687a6f13a4aSGreg Roach    /**
688a6f13a4aSGreg Roach     * Called at the end of an element.
6898ba2e626SGreg Roach     *
6908ba2e626SGreg Roach     * @return void
691a6f13a4aSGreg Roach     */
692b702978eSGreg Roach    protected function gedcomEndHandler(): void
693c1010edaSGreg Roach    {
694a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
695a6f13a4aSGreg Roach            $this->process_gedcoms--;
696a6f13a4aSGreg Roach        } else {
69765e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
698a6f13a4aSGreg Roach        }
699a6f13a4aSGreg Roach    }
700a6f13a4aSGreg Roach
701a6f13a4aSGreg Roach    /**
702fab8f067SGreg Roach     * Handle <textBox>
703a6f13a4aSGreg Roach     *
70409482a55SGreg Roach     * @param array<string> $attrs
7058ba2e626SGreg Roach     *
7068ba2e626SGreg Roach     * @return void
707a6f13a4aSGreg Roach     */
708b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
709c1010edaSGreg Roach    {
710a6f13a4aSGreg Roach        // string Background color code
7117a6ee1acSGreg Roach        $bgcolor = '';
712a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
713a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
714a6f13a4aSGreg Roach        }
715a6f13a4aSGreg Roach
716a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
717a6f13a4aSGreg Roach        $fill = true;
718a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7197a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
720a6f13a4aSGreg Roach                $fill = false;
7217a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
722a6f13a4aSGreg Roach                $fill = true;
723a6f13a4aSGreg Roach            }
724a6f13a4aSGreg Roach        }
725a6f13a4aSGreg Roach
726a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
727a6f13a4aSGreg Roach        $border = false;
728a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7297a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
730a6f13a4aSGreg Roach                $border = true;
7317a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
732a6f13a4aSGreg Roach                $border = false;
733a6f13a4aSGreg Roach            }
734a6f13a4aSGreg Roach        }
735a6f13a4aSGreg Roach
736a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
737a6f13a4aSGreg Roach        $height = 0;
738a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
739a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
740a6f13a4aSGreg Roach        }
741a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
742a6f13a4aSGreg Roach        $width = 0;
743a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
744a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
745a6f13a4aSGreg Roach        }
746a6f13a4aSGreg Roach
747a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
748c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
749a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7507a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
751c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
752a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
753a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7547a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
755a6f13a4aSGreg Roach                $left = 0;
756a6f13a4aSGreg Roach            }
757a6f13a4aSGreg Roach        }
758a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
759c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
760a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7617a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
762c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
763a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
764a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7657a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
766a6f13a4aSGreg Roach                $top = 0;
767a6f13a4aSGreg Roach            }
768a6f13a4aSGreg Roach        }
769a6f13a4aSGreg 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
770a6f13a4aSGreg Roach        $newline = false;
771a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7727a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
773a6f13a4aSGreg Roach                $newline = true;
7747a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
775a6f13a4aSGreg Roach                $newline = false;
776a6f13a4aSGreg Roach            }
777a6f13a4aSGreg Roach        }
778a6f13a4aSGreg Roach        // boolean
779a6f13a4aSGreg Roach        $pagecheck = true;
780a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7817a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
782a6f13a4aSGreg Roach                $pagecheck = false;
7837a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
784a6f13a4aSGreg Roach                $pagecheck = true;
785a6f13a4aSGreg Roach            }
786a6f13a4aSGreg Roach        }
787a6f13a4aSGreg Roach        // boolean Cell padding
788a6f13a4aSGreg Roach        $padding = true;
789a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
7907a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
791a6f13a4aSGreg Roach                $padding = false;
7927a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
793a6f13a4aSGreg Roach                $padding = true;
794a6f13a4aSGreg Roach            }
795a6f13a4aSGreg Roach        }
796a6f13a4aSGreg Roach        // boolean Reset this box Height
797a6f13a4aSGreg Roach        $reseth = false;
798a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
7997a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
800a6f13a4aSGreg Roach                $reseth = true;
8017a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
802a6f13a4aSGreg Roach                $reseth = false;
803a6f13a4aSGreg Roach            }
804a6f13a4aSGreg Roach        }
805a6f13a4aSGreg Roach
806a6f13a4aSGreg Roach        // string Style of rendering
8077a6ee1acSGreg Roach        $style = '';
808a6f13a4aSGreg Roach
8099b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
810a6f13a4aSGreg Roach        $this->print_data         = false;
811a6f13a4aSGreg Roach
8129b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
813e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
814a6f13a4aSGreg Roach            $width,
815a6f13a4aSGreg Roach            $height,
816a6f13a4aSGreg Roach            $border,
817a6f13a4aSGreg Roach            $bgcolor,
818a6f13a4aSGreg Roach            $newline,
819a6f13a4aSGreg Roach            $left,
820a6f13a4aSGreg Roach            $top,
821a6f13a4aSGreg Roach            $pagecheck,
822a6f13a4aSGreg Roach            $style,
823a6f13a4aSGreg Roach            $fill,
824a6f13a4aSGreg Roach            $padding,
825a6f13a4aSGreg Roach            $reseth
826a6f13a4aSGreg Roach        );
827a6f13a4aSGreg Roach    }
828a6f13a4aSGreg Roach
829a6f13a4aSGreg Roach    /**
830fab8f067SGreg Roach     * Handle <textBox>
8318ba2e626SGreg Roach     *
8328ba2e626SGreg Roach     * @return void
833a6f13a4aSGreg Roach     */
834b702978eSGreg Roach    protected function textBoxEndHandler(): void
835c1010edaSGreg Roach    {
836a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
837e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
83831d9777aSGreg Roach
83931d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
84031d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
84131d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
84231d9777aSGreg Roach
843e8e7866bSGreg Roach        $this->wt_report = array_pop($this->wt_report_stack);
844e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
845a6f13a4aSGreg Roach    }
846a6f13a4aSGreg Roach
847a6f13a4aSGreg Roach    /**
84876692c8bSGreg Roach     * XLM <Text>.
84976692c8bSGreg Roach     *
85009482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
8518ba2e626SGreg Roach     *
8528ba2e626SGreg Roach     * @return void
853a6f13a4aSGreg Roach     */
854b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
855c1010edaSGreg Roach    {
8569b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
857a6f13a4aSGreg Roach        $this->print_data         = true;
858a6f13a4aSGreg Roach
859a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8607a6ee1acSGreg Roach        $style = '';
861a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
862a6f13a4aSGreg Roach            $style = $attrs['style'];
863a6f13a4aSGreg Roach        }
864a6f13a4aSGreg Roach
865a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8667a6ee1acSGreg Roach        $color = '';
867a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
868a6f13a4aSGreg Roach            $color = $attrs['color'];
869a6f13a4aSGreg Roach        }
870a6f13a4aSGreg Roach
871e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
872a6f13a4aSGreg Roach    }
873a6f13a4aSGreg Roach
874a6f13a4aSGreg Roach    /**
875fab8f067SGreg Roach     * Handle </text>
8768ba2e626SGreg Roach     *
8778ba2e626SGreg Roach     * @return void
878a6f13a4aSGreg Roach     */
879b702978eSGreg Roach    protected function textEndHandler(): void
880c1010edaSGreg Roach    {
881a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
882e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
883a6f13a4aSGreg Roach    }
884a6f13a4aSGreg Roach
885a6f13a4aSGreg Roach    /**
886fab8f067SGreg Roach     * Handle <getPersonName />
887a6f13a4aSGreg Roach     * Get the name
888a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
889a6f13a4aSGreg Roach     * 2. id is set with a record id
890a6f13a4aSGreg Roach     *
89109482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
8928ba2e626SGreg Roach     *
8938ba2e626SGreg Roach     * @return void
894a6f13a4aSGreg Roach     */
895b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
896c1010edaSGreg Roach    {
8977a6ee1acSGreg Roach        $id    = '';
89813abd6f3SGreg Roach        $match = [];
899a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9007a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
901a6f13a4aSGreg Roach                $id = $match[1];
902a6f13a4aSGreg Roach            }
903a6f13a4aSGreg Roach        } else {
9047a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
905d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
906d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
907a6f13a4aSGreg Roach                }
908a6f13a4aSGreg Roach            } else {
9097a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91013abd6f3SGreg Roach                    $gmatch = [];
911a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
912a6f13a4aSGreg Roach                        $id = $gmatch[1];
913a6f13a4aSGreg Roach                    }
914a6f13a4aSGreg Roach                } else {
915a6f13a4aSGreg Roach                    $id = $attrs['id'];
916a6f13a4aSGreg Roach                }
917a6f13a4aSGreg Roach            }
918a6f13a4aSGreg Roach        }
919a6f13a4aSGreg Roach        if (!empty($id)) {
9206b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9218f038c36SRico Sonntag            if ($record === null) {
922a6f13a4aSGreg Roach                return;
923a6f13a4aSGreg Roach            }
924a6f13a4aSGreg Roach            if (!$record->canShowName()) {
925a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
926a6f13a4aSGreg Roach            } else {
92739ca88baSGreg Roach                $name = $record->fullName();
928a6f13a4aSGreg Roach                $name = strip_tags($name);
929a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
930381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
931a6f13a4aSGreg Roach                } else {
932911124f5SGreg Roach                    $addname = (string) $record->alternateName();
933a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
934a6f13a4aSGreg Roach                    if (!empty($addname)) {
9357a6ee1acSGreg Roach                        $name .= ' ' . $addname;
936a6f13a4aSGreg Roach                    }
937a6f13a4aSGreg Roach                }
938a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
939a6f13a4aSGreg Roach            }
940a6f13a4aSGreg Roach        }
941a6f13a4aSGreg Roach    }
942a6f13a4aSGreg Roach
943a6f13a4aSGreg Roach    /**
944fab8f067SGreg Roach     * Handle <gedcomValue />
945a6f13a4aSGreg Roach     *
94609482a55SGreg Roach     * @param array<string> $attrs
9478ba2e626SGreg Roach     *
9488ba2e626SGreg Roach     * @return void
949a6f13a4aSGreg Roach     */
950b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
951c1010edaSGreg Roach    {
9527a6ee1acSGreg Roach        $id    = '';
95313abd6f3SGreg Roach        $match = [];
9547a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
955a6f13a4aSGreg Roach            $id = $match[1];
956a6f13a4aSGreg Roach        }
957a6f13a4aSGreg Roach
958044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9597a6ee1acSGreg Roach            $useBreak = '1';
960a6f13a4aSGreg Roach        } else {
9617a6ee1acSGreg Roach            $useBreak = '0';
962a6f13a4aSGreg Roach        }
963a6f13a4aSGreg Roach
964a6f13a4aSGreg Roach        $tag = $attrs['tag'];
965a6f13a4aSGreg Roach        if (!empty($tag)) {
966044416d2SGreg Roach            if ($tag === '@desc') {
967a6f13a4aSGreg Roach                $value = $this->desc;
968a6f13a4aSGreg Roach                $value = trim($value);
969a6f13a4aSGreg Roach                $this->current_element->addText($value);
970a6f13a4aSGreg Roach            }
971044416d2SGreg Roach            if ($tag === '@id') {
972a6f13a4aSGreg Roach                $this->current_element->addText($id);
973a6f13a4aSGreg Roach            } else {
9747a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
975a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
976b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
977b2448a1bSGreg Roach                    if ($level === 0) {
978a6f13a4aSGreg Roach                        $level++;
979a6f13a4aSGreg Roach                    }
980a6f13a4aSGreg Roach                } else {
981b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
982a6f13a4aSGreg Roach                }
983a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
9843d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
985a6f13a4aSGreg Roach                switch (end($tags)) {
986a6f13a4aSGreg Roach                    case 'DATE':
987a6f13a4aSGreg Roach                        $tmp   = new Date($value);
988326dd299SGreg Roach                        $value = strip_tags($tmp->display());
989a6f13a4aSGreg Roach                        break;
990a6f13a4aSGreg Roach                    case 'PLAC':
991299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
992392561bbSGreg Roach                        $value = $tmp->shortName();
993a6f13a4aSGreg Roach                        break;
994a6f13a4aSGreg Roach                }
995044416d2SGreg Roach                if ($useBreak === '1') {
996a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
997a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
998a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
999a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1000a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10013b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10023b3cfeeaSGreg Roach                        $value = substr($value, 4);
1003a6f13a4aSGreg Roach                    }
1004a6f13a4aSGreg Roach                }
1005d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10063cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10074d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
100808b2f88aSGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($this->tree)->convertToHtml($value));
10094d35caa7SGreg Roach                    } else {
101008b2f88aSGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($this->tree)->convertToHtml($value));
10114d35caa7SGreg Roach                    }
1012a4d703aeSGreg Roach                }
1013a2a485c3SGreg Roach
1014a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1015381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1016a2a485c3SGreg Roach                }
1017a6f13a4aSGreg Roach                $this->current_element->addText($value);
1018a6f13a4aSGreg Roach            }
1019a6f13a4aSGreg Roach        }
1020a6f13a4aSGreg Roach    }
1021a6f13a4aSGreg Roach
1022a6f13a4aSGreg Roach    /**
1023fab8f067SGreg Roach     * Handle <repeatTag>
1024a6f13a4aSGreg Roach     *
102509482a55SGreg Roach     * @param array<string> $attrs
10268ba2e626SGreg Roach     *
10278ba2e626SGreg Roach     * @return void
1028a6f13a4aSGreg Roach     */
1029b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1030c1010edaSGreg Roach    {
1031a6f13a4aSGreg Roach        $this->process_repeats++;
1032a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1033a6f13a4aSGreg Roach            return;
1034a6f13a4aSGreg Roach        }
1035a6f13a4aSGreg Roach
10369b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
103713abd6f3SGreg Roach        $this->repeats         = [];
1038e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1039a6f13a4aSGreg Roach
1040e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1041a6f13a4aSGreg Roach        if (!empty($tag)) {
1042044416d2SGreg Roach            if ($tag === '@desc') {
1043a6f13a4aSGreg Roach                $value = $this->desc;
1044a6f13a4aSGreg Roach                $value = trim($value);
1045a6f13a4aSGreg Roach                $this->current_element->addText($value);
1046a6f13a4aSGreg Roach            } else {
10477a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10487a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1049b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1050b2448a1bSGreg Roach                if ($level === 0) {
1051a6f13a4aSGreg Roach                    $level++;
1052a6f13a4aSGreg Roach                }
1053a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1054a6f13a4aSGreg Roach                $t      = $tag;
1055a6f13a4aSGreg Roach                $count  = count($tags);
1056a6f13a4aSGreg Roach                $i      = 0;
1057a6f13a4aSGreg Roach                while ($i < $count) {
1058a6f13a4aSGreg Roach                    $t = $tags[$i];
1059a6f13a4aSGreg Roach                    if (!empty($t)) {
1060a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10613d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1062a6f13a4aSGreg Roach                            if (empty($subrec)) {
1063a6f13a4aSGreg Roach                                $level--;
10643d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1065a6f13a4aSGreg Roach                                if (empty($subrec)) {
1066a6f13a4aSGreg Roach                                    return;
1067a6f13a4aSGreg Roach                                }
1068a6f13a4aSGreg Roach                            }
1069a6f13a4aSGreg Roach                        }
1070a6f13a4aSGreg Roach                        $level++;
1071a6f13a4aSGreg Roach                    }
1072a6f13a4aSGreg Roach                    $i++;
1073a6f13a4aSGreg Roach                }
1074a6f13a4aSGreg Roach                $level--;
1075a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1076a6f13a4aSGreg Roach                $i     = 0;
1077a6f13a4aSGreg Roach                while ($i < $count) {
1078a6f13a4aSGreg Roach                    $i++;
1079a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1080a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
10818d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
10826b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1083a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1084a9007102SGreg Roach                            continue;
1085a9007102SGreg Roach                        }
1086a9007102SGreg Roach                    }
1087a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1088a6f13a4aSGreg Roach                }
1089a6f13a4aSGreg Roach            }
1090a6f13a4aSGreg Roach        }
1091a6f13a4aSGreg Roach    }
1092a6f13a4aSGreg Roach
1093a6f13a4aSGreg Roach    /**
1094fab8f067SGreg Roach     * Handle </repeatTag>
10958ba2e626SGreg Roach     *
10968ba2e626SGreg Roach     * @return void
1097a6f13a4aSGreg Roach     */
1098b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1099c1010edaSGreg Roach    {
1100a6f13a4aSGreg Roach        $this->process_repeats--;
1101a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1102a6f13a4aSGreg Roach            return;
1103a6f13a4aSGreg Roach        }
1104a6f13a4aSGreg Roach
1105a6f13a4aSGreg Roach        // Check if there is anything to repeat
1106a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1107a6f13a4aSGreg Roach            // No need to load them if not used...
1108a6f13a4aSGreg Roach
1109a6f13a4aSGreg Roach            $lineoffset = 0;
1110a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1111a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1112a6f13a4aSGreg Roach            }
1113a6f13a4aSGreg Roach            //-- read the xml from the file
1114299d100dSGreg Roach            $lines = file($this->report);
1115dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1116a6f13a4aSGreg Roach                $lineoffset--;
1117a6f13a4aSGreg Roach            }
1118a6f13a4aSGreg Roach            $lineoffset++;
1119a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1120a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1121a6f13a4aSGreg Roach            // RepeatTag Level counter
1122a6f13a4aSGreg Roach            $count = 1;
1123a6f13a4aSGreg Roach            while (0 < $count) {
1124dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1125a6f13a4aSGreg Roach                    $count++;
1126dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1127a6f13a4aSGreg Roach                    $count--;
1128a6f13a4aSGreg Roach                }
1129a6f13a4aSGreg Roach                if (0 < $count) {
1130a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1131a6f13a4aSGreg Roach                }
1132a6f13a4aSGreg Roach                $line_nr++;
1133a6f13a4aSGreg Roach            }
1134a6f13a4aSGreg Roach            // No need to drag this
1135a6f13a4aSGreg Roach            unset($lines);
1136a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1137a6f13a4aSGreg Roach            // Save original values
11389b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1139a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1140a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1141a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1142a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1143e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1144a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11451aa04befSGreg Roach
11461aa04befSGreg Roach                xml_set_element_handler(
11471aa04befSGreg Roach                    $repeat_parser,
11489d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11491aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11501aa04befSGreg Roach                    },
11519d454b6bSGreg Roach                    function ($parser, string $name): void {
11521aa04befSGreg Roach                        $this->endElement($parser, $name);
11531aa04befSGreg Roach                    }
11541aa04befSGreg Roach                );
11551aa04befSGreg Roach
11561aa04befSGreg Roach                xml_set_character_data_handler(
11571aa04befSGreg Roach                    $repeat_parser,
11589d454b6bSGreg Roach                    function ($parser, string $data): void {
11591aa04befSGreg Roach                        $this->characterData($parser, $data);
11601aa04befSGreg Roach                    }
11611aa04befSGreg Roach                );
11621aa04befSGreg Roach
1163a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
11646ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1165a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1166a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1167a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1168a6f13a4aSGreg Roach                    ));
1169a6f13a4aSGreg Roach                }
1170a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1171a6f13a4aSGreg Roach            }
1172a6f13a4aSGreg Roach            // Restore original values
1173a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1174e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1175a6f13a4aSGreg Roach        }
117665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1177a6f13a4aSGreg Roach    }
1178a6f13a4aSGreg Roach
1179a6f13a4aSGreg Roach    /**
1180a6f13a4aSGreg Roach     * Variable lookup
1181a6f13a4aSGreg Roach     * Retrieve predefined variables :
1182a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1183a6f13a4aSGreg Roach     *        1 EVEN This is a description
1184a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1185a6f13a4aSGreg Roach     * $ I18N::translate('....')
1186a6f13a4aSGreg Roach     * $ language_settings[]
1187a6f13a4aSGreg Roach     *
118809482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
11898ba2e626SGreg Roach     *
11908ba2e626SGreg Roach     * @return void
1191a6f13a4aSGreg Roach     */
1192b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1193c1010edaSGreg Roach    {
1194a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
11956ccdf4f0SGreg 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));
1196a6f13a4aSGreg Roach        }
1197a6f13a4aSGreg Roach
1198a6f13a4aSGreg Roach        $var = $attrs['var'];
1199a6f13a4aSGreg Roach        // SetVar element preset variables
1200d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1201d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1202a6f13a4aSGreg Roach        } else {
1203a6f13a4aSGreg Roach            $tfact = $this->fact;
12047a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1205a6f13a4aSGreg Roach                // Use :
1206a6f13a4aSGreg Roach                // n TYPE This text if string
1207a6f13a4aSGreg Roach                $tfact = $this->type;
120897def6bcSGreg Roach            } else {
120997def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
121097def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
121197def6bcSGreg Roach
121297def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
121397def6bcSGreg Roach                        $tfact = $element->label();
121497def6bcSGreg Roach                        break;
1215a6f13a4aSGreg Roach                    }
121697def6bcSGreg Roach                }
121797def6bcSGreg Roach            }
121897def6bcSGreg Roach
121997def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
122097def6bcSGreg Roach
1221a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1222da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1223a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1224a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1225a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1226a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1227a6f13a4aSGreg Roach            }
1228a6f13a4aSGreg Roach        }
1229a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1230a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12317a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1232a6f13a4aSGreg Roach                $g   = new Date($var);
1233a6f13a4aSGreg Roach                $var = $g->display();
1234a6f13a4aSGreg Roach            }
1235a6f13a4aSGreg Roach        }
1236a6f13a4aSGreg Roach        $this->current_element->addText($var);
12372836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1238a6f13a4aSGreg Roach    }
1239a6f13a4aSGreg Roach
1240a6f13a4aSGreg Roach    /**
1241fab8f067SGreg Roach     * Handle <facts>
124276692c8bSGreg Roach     *
124309482a55SGreg Roach     * @param array<string> $attrs
12448ba2e626SGreg Roach     *
12458ba2e626SGreg Roach     * @return void
1246a6f13a4aSGreg Roach     */
1247b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1248c1010edaSGreg Roach    {
1249a6f13a4aSGreg Roach        $this->process_repeats++;
1250a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1251a6f13a4aSGreg Roach            return;
1252a6f13a4aSGreg Roach        }
1253a6f13a4aSGreg Roach
12549b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
125513abd6f3SGreg Roach        $this->repeats         = [];
1256e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1257a6f13a4aSGreg Roach
12587a6ee1acSGreg Roach        $id    = '';
125913abd6f3SGreg Roach        $match = [];
12607a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1261a6f13a4aSGreg Roach            $id = $match[1];
1262a6f13a4aSGreg Roach        }
12637a6ee1acSGreg Roach        $tag = '';
1264a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1265a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1266a6f13a4aSGreg Roach        }
12677a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1268d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1269a6f13a4aSGreg Roach        }
1270a6f13a4aSGreg Roach
12716b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1272a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1273820b62dfSGreg Roach            $facts = $record->facts([], true);
127413abd6f3SGreg Roach            $this->repeats = [];
1275a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1276195b5e75SGreg Roach            foreach ($facts as $fact) {
1277d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1278d0889c63SGreg Roach
1279d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1280195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1281a6f13a4aSGreg Roach                }
1282a6f13a4aSGreg Roach            }
1283a6f13a4aSGreg Roach        } else {
128430158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1285d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1286138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1287a6f13a4aSGreg Roach                }
1288a6f13a4aSGreg Roach            }
1289a6f13a4aSGreg Roach        }
1290a6f13a4aSGreg Roach    }
1291a6f13a4aSGreg Roach
1292a6f13a4aSGreg Roach    /**
1293fab8f067SGreg Roach     * Handle </facts>
12948ba2e626SGreg Roach     *
12958ba2e626SGreg Roach     * @return void
1296a6f13a4aSGreg Roach     */
1297b702978eSGreg Roach    protected function factsEndHandler(): void
1298c1010edaSGreg Roach    {
1299a6f13a4aSGreg Roach        $this->process_repeats--;
1300a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1301a6f13a4aSGreg Roach            return;
1302a6f13a4aSGreg Roach        }
1303a6f13a4aSGreg Roach
1304a6f13a4aSGreg Roach        // Check if there is anything to repeat
1305a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1306e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1307a6f13a4aSGreg Roach            $lineoffset = 0;
1308a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1309a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1310a6f13a4aSGreg Roach            }
1311a6f13a4aSGreg Roach
1312a6f13a4aSGreg Roach            //-- read the xml from the file
1313299d100dSGreg Roach            $lines = file($this->report);
1314dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1315a6f13a4aSGreg Roach                $lineoffset--;
1316a6f13a4aSGreg Roach            }
1317a6f13a4aSGreg Roach            $lineoffset++;
1318a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1319a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1320a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1321a6f13a4aSGreg Roach            while ($line_nr < $i) {
1322a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1323a6f13a4aSGreg Roach                $line_nr++;
1324a6f13a4aSGreg Roach            }
1325a6f13a4aSGreg Roach            // No need to drag this
1326a6f13a4aSGreg Roach            unset($lines);
1327a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1328a6f13a4aSGreg Roach            // Save original values
13299b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1330a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1331a6f13a4aSGreg Roach            $count                = count($this->repeats);
1332a6f13a4aSGreg Roach            $i                    = 0;
1333a6f13a4aSGreg Roach            while ($i < $count) {
1334a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1335a6f13a4aSGreg Roach                $this->fact   = '';
1336a6f13a4aSGreg Roach                $this->desc   = '';
1337a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1338a6f13a4aSGreg Roach                    $this->fact = $match[1];
1339a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
134013abd6f3SGreg Roach                        $tmatch = [];
1341a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1342a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1343a6f13a4aSGreg Roach                        } else {
1344a6f13a4aSGreg Roach                            $this->type = ' ';
1345a6f13a4aSGreg Roach                        }
1346a6f13a4aSGreg Roach                    }
1347a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13483d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1349a6f13a4aSGreg Roach                }
1350a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1351e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1352a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13531aa04befSGreg Roach
13541aa04befSGreg Roach                xml_set_element_handler(
13551aa04befSGreg Roach                    $repeat_parser,
13569d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13571aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13581aa04befSGreg Roach                    },
13599d454b6bSGreg Roach                    function ($parser, string $name): void {
13601aa04befSGreg Roach                        $this->endElement($parser, $name);
13611aa04befSGreg Roach                    }
13621aa04befSGreg Roach                );
13631aa04befSGreg Roach
13641aa04befSGreg Roach                xml_set_character_data_handler(
13651aa04befSGreg Roach                    $repeat_parser,
13669d454b6bSGreg Roach                    function ($parser, string $data): void {
13671aa04befSGreg Roach                        $this->characterData($parser, $data);
13681aa04befSGreg Roach                    }
13691aa04befSGreg Roach                );
13701aa04befSGreg Roach
1371a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
13726ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1373a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1374a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1375a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1376a6f13a4aSGreg Roach                    ));
1377a6f13a4aSGreg Roach                }
1378a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1379a6f13a4aSGreg Roach                $i++;
1380a6f13a4aSGreg Roach            }
1381a6f13a4aSGreg Roach            // Restore original values
1382e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1383a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1384a6f13a4aSGreg Roach        }
138565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1386a6f13a4aSGreg Roach    }
1387a6f13a4aSGreg Roach
1388a6f13a4aSGreg Roach    /**
1389a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1390d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1391a6f13a4aSGreg Roach     *
139209482a55SGreg Roach     * @param array<string> $attrs an array of key value pairs for the attributes
13938ba2e626SGreg Roach     *
13948ba2e626SGreg Roach     * @return void
1395a6f13a4aSGreg Roach     */
1396b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1397c1010edaSGreg Roach    {
1398a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
13996ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1400a6f13a4aSGreg Roach        }
1401a6f13a4aSGreg Roach
1402a6f13a4aSGreg Roach        $name  = $attrs['name'];
1403a6f13a4aSGreg Roach        $value = $attrs['value'];
140413abd6f3SGreg Roach        $match = [];
1405a6f13a4aSGreg Roach        // Current GEDCOM record strings
1406044416d2SGreg Roach        if ($value === '@ID') {
14077a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1408a6f13a4aSGreg Roach                $value = $match[1];
1409a6f13a4aSGreg Roach            }
1410044416d2SGreg Roach        } elseif ($value === '@fact') {
1411a6f13a4aSGreg Roach            $value = $this->fact;
1412044416d2SGreg Roach        } elseif ($value === '@desc') {
1413a6f13a4aSGreg Roach            $value = $this->desc;
1414044416d2SGreg Roach        } elseif ($value === '@generation') {
1415589feda3SGreg Roach            $value = (string) $this->generation;
1416a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
141713abd6f3SGreg Roach            $gmatch = [];
1418a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14197a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1420a6f13a4aSGreg Roach            }
1421a6f13a4aSGreg Roach        }
1422a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1423d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1424a6f13a4aSGreg Roach        }
1425a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1426a6f13a4aSGreg Roach        $i     = 0;
1427a6f13a4aSGreg Roach        while ($i < $count) {
1428d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14297a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1430a6f13a4aSGreg Roach            $i++;
1431a6f13a4aSGreg Roach        }
1432a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1433da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1434a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1435a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1436a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1437a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1438a6f13a4aSGreg Roach        }
143952868398SGreg Roach
1440a6f13a4aSGreg Roach        // Arithmetic functions
14413cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
144252868398SGreg Roach            // Create an expression language with the functions used by our reports.
144352868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1444c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1445c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
144652868398SGreg Roach
144752868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1448a6f13a4aSGreg Roach        }
144952868398SGreg Roach
1450dec352c1SGreg Roach        if (str_contains($value, '@')) {
14517a6ee1acSGreg Roach            $value = '';
1452a6f13a4aSGreg Roach        }
1453d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1454a6f13a4aSGreg Roach    }
1455a6f13a4aSGreg Roach
1456a6f13a4aSGreg Roach    /**
1457fab8f067SGreg Roach     * Handle <if>
1458a6f13a4aSGreg Roach     *
145909482a55SGreg Roach     * @param array<string> $attrs
14608ba2e626SGreg Roach     *
14618ba2e626SGreg Roach     * @return void
1462a6f13a4aSGreg Roach     */
1463b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1464c1010edaSGreg Roach    {
1465a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1466a6f13a4aSGreg Roach            $this->process_ifs++;
1467a6f13a4aSGreg Roach
1468a6f13a4aSGreg Roach            return;
1469a6f13a4aSGreg Roach        }
1470a6f13a4aSGreg Roach
1471a6f13a4aSGreg Roach        $condition = $attrs['condition'];
147282759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1473c1010edaSGreg Roach        $condition = str_replace([
1474c1010edaSGreg Roach            ' LT ',
1475c1010edaSGreg Roach            ' GT ',
1476c1010edaSGreg Roach        ], [
1477c1010edaSGreg Roach            '<',
1478c1010edaSGreg Roach            '>',
1479c1010edaSGreg Roach        ], $condition);
14803cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
14817a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
148213abd6f3SGreg Roach        $match     = [];
14833cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1484a6f13a4aSGreg Roach        $i         = 0;
1485a6f13a4aSGreg Roach        while ($i < $count) {
1486a6f13a4aSGreg Roach            $id    = $match[$i][1];
1487a6f13a4aSGreg Roach            $value = '""';
1488044416d2SGreg Roach            if ($id === 'ID') {
14897a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1490a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1491a6f13a4aSGreg Roach                }
14927a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1493a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
14947a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1495a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
14967a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1497a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1498a6f13a4aSGreg Roach            } else {
1499b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1500b2448a1bSGreg Roach                if ($level === 0) {
1501a6f13a4aSGreg Roach                    $level++;
1502a6f13a4aSGreg Roach                }
15033d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1504a6f13a4aSGreg Roach                if (empty($value)) {
1505a6f13a4aSGreg Roach                    $level++;
15063d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1507a6f13a4aSGreg Roach                }
15088d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15095e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1510a6f13a4aSGreg Roach            }
1511a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1512a6f13a4aSGreg Roach            $i++;
1513a6f13a4aSGreg Roach        }
15145809450fSGreg Roach
1515cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1516cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1517c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1518c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1519cb63a60eSGreg Roach
1520cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15215809450fSGreg Roach
1522a6f13a4aSGreg Roach        if (!$ret) {
1523a6f13a4aSGreg Roach            $this->process_ifs++;
1524a6f13a4aSGreg Roach        }
1525a6f13a4aSGreg Roach    }
1526a6f13a4aSGreg Roach
1527a6f13a4aSGreg Roach    /**
1528fab8f067SGreg Roach     * Handle </if>
15298ba2e626SGreg Roach     *
15308ba2e626SGreg Roach     * @return void
1531a6f13a4aSGreg Roach     */
1532b702978eSGreg Roach    protected function ifEndHandler(): void
1533c1010edaSGreg Roach    {
1534a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1535a6f13a4aSGreg Roach            $this->process_ifs--;
1536a6f13a4aSGreg Roach        }
1537a6f13a4aSGreg Roach    }
1538a6f13a4aSGreg Roach
1539a6f13a4aSGreg Roach    /**
1540fab8f067SGreg Roach     * Handle <footnote>
1541a6f13a4aSGreg Roach     * Collect the Footnote links
1542fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1543a6f13a4aSGreg Roach     *
154409482a55SGreg Roach     * @param array<string> $attrs
15458ba2e626SGreg Roach     *
15468ba2e626SGreg Roach     * @return void
1547a6f13a4aSGreg Roach     */
1548b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1549c1010edaSGreg Roach    {
15507a6ee1acSGreg Roach        $id = '';
15517a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1552a6f13a4aSGreg Roach            $id = $match[2];
1553a6f13a4aSGreg Roach        }
15546b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1555a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15569b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1557a6f13a4aSGreg Roach            $this->print_data         = true;
15587a6ee1acSGreg Roach            $style                    = '';
1559a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1560a6f13a4aSGreg Roach                $style = $attrs['style'];
1561a6f13a4aSGreg Roach            }
1562a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1563e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1564a6f13a4aSGreg Roach        } else {
1565a6f13a4aSGreg Roach            $this->print_data       = false;
1566a6f13a4aSGreg Roach            $this->process_footnote = false;
1567a6f13a4aSGreg Roach        }
1568a6f13a4aSGreg Roach    }
1569a6f13a4aSGreg Roach
1570a6f13a4aSGreg Roach    /**
1571fab8f067SGreg Roach     * Handle </footnote>
1572a6f13a4aSGreg Roach     * Print the collected Footnote data
15738ba2e626SGreg Roach     *
15748ba2e626SGreg Roach     * @return void
1575a6f13a4aSGreg Roach     */
1576b702978eSGreg Roach    protected function footnoteEndHandler(): void
1577c1010edaSGreg Roach    {
1578a6f13a4aSGreg Roach        if ($this->process_footnote) {
1579a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1580a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1581a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1582e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1583a6f13a4aSGreg Roach            }
1584a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1585a6f13a4aSGreg Roach        } else {
1586a6f13a4aSGreg Roach            $this->process_footnote = true;
1587a6f13a4aSGreg Roach        }
1588a6f13a4aSGreg Roach    }
1589a6f13a4aSGreg Roach
1590a6f13a4aSGreg Roach    /**
1591fab8f067SGreg Roach     * Handle <footnoteTexts />
15928ba2e626SGreg Roach     *
15938ba2e626SGreg Roach     * @return void
1594a6f13a4aSGreg Roach     */
1595b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1596c1010edaSGreg Roach    {
15977a6ee1acSGreg Roach        $temp = 'footnotetexts';
1598e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1599a6f13a4aSGreg Roach    }
1600a6f13a4aSGreg Roach
1601a6f13a4aSGreg Roach    /**
1602a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16038ba2e626SGreg Roach     *
16048ba2e626SGreg Roach     * @return void
1605a6f13a4aSGreg Roach     */
1606b702978eSGreg Roach    protected function brStartHandler(): void
1607c1010edaSGreg Roach    {
1608a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1609a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1610a6f13a4aSGreg Roach        }
1611a6f13a4aSGreg Roach    }
1612a6f13a4aSGreg Roach
1613a6f13a4aSGreg Roach    /**
1614fab8f067SGreg Roach     * Handle <sp />
1615fab8f067SGreg Roach     * Forced space
16168ba2e626SGreg Roach     *
16178ba2e626SGreg Roach     * @return void
1618a6f13a4aSGreg Roach     */
1619b702978eSGreg Roach    protected function spStartHandler(): void
1620c1010edaSGreg Roach    {
1621a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1622a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1623a6f13a4aSGreg Roach        }
1624a6f13a4aSGreg Roach    }
1625a6f13a4aSGreg Roach
1626a6f13a4aSGreg Roach    /**
1627fab8f067SGreg Roach     * Handle <highlightedImage />
162876692c8bSGreg Roach     *
162909482a55SGreg Roach     * @param array<string> $attrs
16308ba2e626SGreg Roach     *
16318ba2e626SGreg Roach     * @return void
1632a6f13a4aSGreg Roach     */
1633b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1634c1010edaSGreg Roach    {
1635a6f13a4aSGreg Roach        $id = '';
16367a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1637a6f13a4aSGreg Roach            $id = $match[1];
1638a6f13a4aSGreg Roach        }
1639a6f13a4aSGreg Roach
1640c21bdddcSGreg Roach        // Position the top corner of this box on the page
1641c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1642a6f13a4aSGreg Roach
1643c21bdddcSGreg Roach        // Position the left corner of this box on the page
1644c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1645a6f13a4aSGreg Roach
164683cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
164783cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1648a6f13a4aSGreg Roach
1649a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
165083cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1651a6f13a4aSGreg Roach
165283cdc021SGreg Roach        // Width, height (or both).
1653c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1654c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1655a6f13a4aSGreg Roach
16566b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
16574a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
165886a63f51SGreg Roach
1659a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1660a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1661b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
166229518ad2SGreg Roach
1663a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16643c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16653c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1666a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16673c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16683c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1669a6f13a4aSGreg Roach            } else {
16703c3b90deSGreg Roach                $width  = $attributes[0];
16713c3b90deSGreg Roach                $height = $attributes[1];
1672a6f13a4aSGreg Roach            }
1673a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1674e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1675a6f13a4aSGreg Roach        }
1676a6f13a4aSGreg Roach    }
1677a6f13a4aSGreg Roach
1678a6f13a4aSGreg Roach    /**
1679fab8f067SGreg Roach     * Handle <image/>
168076692c8bSGreg Roach     *
168109482a55SGreg Roach     * @param array<string> $attrs
16828ba2e626SGreg Roach     *
16838ba2e626SGreg Roach     * @return void
1684a6f13a4aSGreg Roach     */
1685b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1686c1010edaSGreg Roach    {
168783cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1688c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1689a6f13a4aSGreg Roach
1690a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1691c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1692a6f13a4aSGreg Roach
169383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
169483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1695a6f13a4aSGreg Roach
1696a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
169783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1698a6f13a4aSGreg Roach
169983cdc021SGreg Roach        // Width, height (or both).
1700c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1701c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1702a6f13a4aSGreg Roach
170383cdc021SGreg Roach        $file = $attrs['file'] ?? '';
170483cdc021SGreg Roach
1705044416d2SGreg Roach        if ($file === '@FILE') {
170613abd6f3SGreg Roach            $match = [];
1707a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17086b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17094a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1710cdf416fbSGreg Roach
1711a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1712a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1713b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
171429518ad2SGreg Roach
1715a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17163c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17173c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1718a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17193c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17203c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1721a6f13a4aSGreg Roach                    } else {
17223c3b90deSGreg Roach                        $width  = $attributes[0];
17233c3b90deSGreg Roach                        $height = $attributes[1];
1724a6f13a4aSGreg Roach                    }
1725a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1726e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1727a6f13a4aSGreg Roach                }
1728a6f13a4aSGreg Roach            }
1729a6f13a4aSGreg Roach        } else {
17307a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1731a6f13a4aSGreg Roach                $size = getimagesize($file);
1732a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1733a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1734a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1735a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1736a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1737a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1738a6f13a4aSGreg Roach                } else {
1739a6f13a4aSGreg Roach                    $width  = $size[0];
1740a6f13a4aSGreg Roach                    $height = $size[1];
1741a6f13a4aSGreg Roach                }
1742e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1743e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1744a6f13a4aSGreg Roach            }
1745a6f13a4aSGreg Roach        }
1746a6f13a4aSGreg Roach    }
1747a6f13a4aSGreg Roach
1748a6f13a4aSGreg Roach    /**
1749fab8f067SGreg Roach     * Handle <line>
1750a6f13a4aSGreg Roach     *
175109482a55SGreg Roach     * @param array<string> $attrs
17528ba2e626SGreg Roach     *
17538ba2e626SGreg Roach     * @return void
1754a6f13a4aSGreg Roach     */
1755b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1756c1010edaSGreg Roach    {
1757a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1758c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1759a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17607a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1761a6f13a4aSGreg Roach                $x1 = 0;
17627a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1763c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1764a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1765c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1766a6f13a4aSGreg Roach            }
1767a6f13a4aSGreg Roach        }
1768a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1769c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1770a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17717a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1772a6f13a4aSGreg Roach                $y1 = 0;
17737a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1774c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1775a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1776c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1777a6f13a4aSGreg Roach            }
1778a6f13a4aSGreg Roach        }
1779a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1780c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1781a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
17827a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1783a6f13a4aSGreg Roach                $x2 = 0;
17847a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1785c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1786a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1787c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1788a6f13a4aSGreg Roach            }
1789a6f13a4aSGreg Roach        }
1790a6f13a4aSGreg Roach        // End vertical position
1791c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1792a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
17937a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1794a6f13a4aSGreg Roach                $y2 = 0;
17957a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1796c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1797a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1798c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1799a6f13a4aSGreg Roach            }
1800a6f13a4aSGreg Roach        }
1801a6f13a4aSGreg Roach
1802e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1803e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1804a6f13a4aSGreg Roach    }
1805a6f13a4aSGreg Roach
1806a6f13a4aSGreg Roach    /**
1807fab8f067SGreg Roach     * Handle <list>
1808a6f13a4aSGreg Roach     *
180909482a55SGreg Roach     * @param array<string> $attrs
18108ba2e626SGreg Roach     *
18118ba2e626SGreg Roach     * @return void
1812a6f13a4aSGreg Roach     */
1813b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1814c1010edaSGreg Roach    {
1815a6f13a4aSGreg Roach        $this->process_repeats++;
1816a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1817a6f13a4aSGreg Roach            return;
1818a6f13a4aSGreg Roach        }
1819a6f13a4aSGreg Roach
182013abd6f3SGreg Roach        $match = [];
1821a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1822a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1823a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1824d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1825a6f13a4aSGreg Roach                $sortby = trim($sortby);
1826a6f13a4aSGreg Roach            }
1827a6f13a4aSGreg Roach        } else {
18287a6ee1acSGreg Roach            $sortby = 'NAME';
1829a6f13a4aSGreg Roach        }
1830a6f13a4aSGreg Roach
1831e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1832195b5e75SGreg Roach
1833a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1834a6f13a4aSGreg Roach        switch ($listname) {
18357a6ee1acSGreg Roach            case 'pending':
1836*63b1851dSGreg Roach                $this->list = DB::table('change')
1837195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1838a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1839195b5e75SGreg Roach                            ->from('change')
1840195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1841195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18427f5c2944SGreg Roach                            ->groupBy(['xref']);
1843195b5e75SGreg Roach                    })
1844*63b1851dSGreg Roach                    ->get()
1845*63b1851dSGreg Roach                    ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom))
1846*63b1851dSGreg Roach                    ->filter()
1847*63b1851dSGreg Roach                    ->all();
1848a6f13a4aSGreg Roach                break;
1849*63b1851dSGreg Roach
1850a6f13a4aSGreg Roach            case 'individual':
18515985adfbSGreg Roach                $query = DB::table('individuals')
18525985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18535985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18545985adfbSGreg Roach                    ->distinct();
18555985adfbSGreg Roach
1856a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1857dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
185882759250SGreg Roach                        $value = $this->substituteVars($value, false);
1859a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1860a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
18610b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
18625985adfbSGreg Roach                                $join
18635985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
18645985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
18655985adfbSGreg Roach                            });
18665985adfbSGreg Roach
18675985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
18685985adfbSGreg Roach
1869a6f13a4aSGreg Roach                            $date = new Date($match[3]);
18705985adfbSGreg Roach
1871044416d2SGreg Roach                            if ($match[2] === 'LTE') {
18725985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1873a6f13a4aSGreg Roach                            } else {
18745985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1875a6f13a4aSGreg Roach                            }
18765985adfbSGreg Roach
18775985adfbSGreg Roach                            // This filter has been fully processed
18785985adfbSGreg Roach                            unset($attrs[$attr]);
18797ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
18800b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
18815985adfbSGreg Roach                                $join
18825985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
18835985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
18845985adfbSGreg Roach                            });
1885a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
18867a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
18875d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1888b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1889a6f13a4aSGreg Roach                            }
18905985adfbSGreg Roach
18915985adfbSGreg Roach                            // This filter has been fully processed
18925985adfbSGreg Roach                            unset($attrs[$attr]);
1893a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
18945985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1895a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
18965985adfbSGreg Roach
1897a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
18985985adfbSGreg Roach
18995985adfbSGreg Roach                            // This filter has been fully processed
19005985adfbSGreg Roach                            unset($attrs[$attr]);
19012fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19029dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19035985adfbSGreg Roach                            $query
19040b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19055985adfbSGreg Roach                                    $join
19067ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19077ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19085985adfbSGreg Roach                                })
19090b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19107ee4bfadSGreg Roach                                    $join
19117ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19127ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19137ee4bfadSGreg Roach                                })
19149b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19156e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19165985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19177ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19186e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19196e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
192047a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19216e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19226e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1923e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19247ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1925a6f13a4aSGreg Roach                        }
1926a6f13a4aSGreg Roach                    }
1927a6f13a4aSGreg Roach                }
1928a6f13a4aSGreg Roach
192913abd6f3SGreg Roach                $this->list = [];
1930a6f13a4aSGreg Roach
19315985adfbSGreg Roach                foreach ($query->get() as $row) {
19326b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1933a6f13a4aSGreg Roach                }
1934a6f13a4aSGreg Roach                break;
1935a6f13a4aSGreg Roach
1936a6f13a4aSGreg Roach            case 'family':
193730fc2b1eSGreg Roach                $query = DB::table('families')
193830fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
193930fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
194030fc2b1eSGreg Roach                    ->distinct();
194130fc2b1eSGreg Roach
1942a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1943dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
194482759250SGreg Roach                        $value = $this->substituteVars($value, false);
1945a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1946a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19470b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
194830fc2b1eSGreg Roach                                $join
194930fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
195030fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
195130fc2b1eSGreg Roach                            });
195230fc2b1eSGreg Roach
195330fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
195430fc2b1eSGreg Roach
1955a6f13a4aSGreg Roach                            $date = new Date($match[3]);
195630fc2b1eSGreg Roach
1957044416d2SGreg Roach                            if ($match[2] === 'LTE') {
195830fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1959a6f13a4aSGreg Roach                            } else {
196030fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1961a6f13a4aSGreg Roach                            }
196230fc2b1eSGreg Roach
196330fc2b1eSGreg Roach                            // This filter has been fully processed
196430fc2b1eSGreg Roach                            unset($attrs[$attr]);
1965a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
196630fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
1967a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
196830fc2b1eSGreg Roach
1969a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
197030fc2b1eSGreg Roach
197130fc2b1eSGreg Roach                            // This filter has been fully processed
197230fc2b1eSGreg Roach                            unset($attrs[$attr]);
197330fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
19743b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
19750b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
197630fc2b1eSGreg Roach                                    $join
197730fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
19783b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
197930fc2b1eSGreg Roach                                            $query
198030fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
198130fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
198230fc2b1eSGreg Roach                                        });
198330fc2b1eSGreg Roach                                });
19845d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
19857a6ee1acSGreg Roach                                if ($match[1] != '') {
19867a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19875d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
1988b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
19895d0bc43dSGreg Roach                                    }
19905d0bc43dSGreg Roach                                }
1991a6f13a4aSGreg Roach                            }
199230fc2b1eSGreg Roach
199330fc2b1eSGreg Roach                            // This filter has been fully processed
199430fc2b1eSGreg Roach                            unset($attrs[$attr]);
19956e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19969dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
199730fc2b1eSGreg Roach                            $query
19980b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
199930fc2b1eSGreg Roach                                    $join
20006e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20016e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
200230fc2b1eSGreg Roach                                })
20030b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20046e5c0963SGreg Roach                                    $join
20056e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20066e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20076e5c0963SGreg Roach                                })
2008b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20096e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
201030fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20116e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20126e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20136e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20146e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20156e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20166e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2017e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20186e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2019a6f13a4aSGreg Roach                        }
2020a6f13a4aSGreg Roach                    }
2021a6f13a4aSGreg Roach                }
2022a6f13a4aSGreg Roach
202313abd6f3SGreg Roach                $this->list = [];
2024a6f13a4aSGreg Roach
202530fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20266b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2027a6f13a4aSGreg Roach                }
2028a6f13a4aSGreg Roach                break;
2029a6f13a4aSGreg Roach
2030a6f13a4aSGreg Roach            default:
20316ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2032a6f13a4aSGreg Roach        }
2033a6f13a4aSGreg Roach
203413abd6f3SGreg Roach        $filters  = [];
203513abd6f3SGreg Roach        $filters2 = [];
2036a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2037a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2038a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2039a6f13a4aSGreg Roach                    $condition = $value;
2040a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2041a6f13a4aSGreg Roach                        $id    = $match[1];
2042a6f13a4aSGreg Roach                        $value = "''";
2043044416d2SGreg Roach                        if ($id === 'ID') {
20447a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2045a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2046a6f13a4aSGreg Roach                            }
2047044416d2SGreg Roach                        } elseif ($id === 'fact') {
2048a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2049044416d2SGreg Roach                        } elseif ($id === 'desc') {
2050a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2051a6f13a4aSGreg Roach                        } else {
2052a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20537a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2054a6f13a4aSGreg Roach                            }
2055a6f13a4aSGreg Roach                        }
2056a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2057a6f13a4aSGreg Roach                    }
2058a6f13a4aSGreg Roach                    //-- handle regular expressions
2059a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2060a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2061a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2062a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2063a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2064d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2065a6f13a4aSGreg Roach                            $val = trim($val);
2066a6f13a4aSGreg Roach                        }
2067a6f13a4aSGreg Roach                        if ($val) {
20687a6ee1acSGreg Roach                            $searchstr = '';
20697a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2070a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2071a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2072a6f13a4aSGreg Roach                                $level = 1;
2073f71a7dedSGreg Roach                                $t = 'XXXX';
2074a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2075a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2076a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2077a6f13a4aSGreg Roach                                    }
2078a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2079044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
20807a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2081a6f13a4aSGreg Roach                                    }
20827a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2083a6f13a4aSGreg Roach                                    $level++;
2084a6f13a4aSGreg Roach                                }
2085a6f13a4aSGreg Roach                            } else {
2086044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
20877a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2088a6f13a4aSGreg Roach                                }
2089a6f13a4aSGreg Roach                                $t         = $tag;
20907a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2091a6f13a4aSGreg Roach                            }
2092a6f13a4aSGreg Roach                            switch ($expr) {
20937a6ee1acSGreg Roach                                case 'CONTAINS':
2094044416d2SGreg Roach                                    if ($t === 'PLAC') {
2095a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2096a6f13a4aSGreg Roach                                    } else {
2097a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2098a6f13a4aSGreg Roach                                    }
2099a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2100a6f13a4aSGreg Roach                                    break;
2101a6f13a4aSGreg Roach                                default:
2102c1010edaSGreg Roach                                    $filters2[] = [
2103c1010edaSGreg Roach                                        'tag'  => $tag,
2104c1010edaSGreg Roach                                        'expr' => $expr,
2105c1010edaSGreg Roach                                        'val'  => $val,
2106c1010edaSGreg Roach                                    ];
2107a6f13a4aSGreg Roach                                    break;
2108a6f13a4aSGreg Roach                            }
2109a6f13a4aSGreg Roach                        }
2110a6f13a4aSGreg Roach                    }
2111a6f13a4aSGreg Roach                }
2112a6f13a4aSGreg Roach            }
2113a6f13a4aSGreg Roach        }
2114a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2115a6f13a4aSGreg Roach        if ($filters) {
2116a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2117a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2118299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2119a6f13a4aSGreg Roach                        unset($this->list[$key]);
2120a6f13a4aSGreg Roach                        break;
2121a6f13a4aSGreg Roach                    }
2122a6f13a4aSGreg Roach                }
2123a6f13a4aSGreg Roach            }
2124a6f13a4aSGreg Roach        }
2125a6f13a4aSGreg Roach        if ($filters2) {
212613abd6f3SGreg Roach            $mylist = [];
2127a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2128c0935879SGreg Roach                $key  = $indi->xref();
2129299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2130a6f13a4aSGreg Roach                $keep = true;
2131a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2132a6f13a4aSGreg Roach                    if ($keep) {
2133a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2134a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2135a6f13a4aSGreg Roach                        $val  = $filter['val'];
2136b2448a1bSGreg Roach                        if ($val === "''") {
21377a6ee1acSGreg Roach                            $val = '';
2138a6f13a4aSGreg Roach                        }
21397a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2140a6f13a4aSGreg Roach                        $t    = end($tags);
21413d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2142a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2143044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21447a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21457a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2146a6f13a4aSGreg Roach                            $t    = end($tags);
21473d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2148a6f13a4aSGreg Roach                        }
2149a6f13a4aSGreg Roach
2150a6f13a4aSGreg Roach                        switch ($expr) {
21517a6ee1acSGreg Roach                            case 'GTE':
2152044416d2SGreg Roach                                if ($t === 'DATE') {
2153a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2154a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2155a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2156a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2157a6f13a4aSGreg Roach                                    $keep = true;
2158a6f13a4aSGreg Roach                                }
2159a6f13a4aSGreg Roach                                break;
21607a6ee1acSGreg Roach                            case 'LTE':
2161044416d2SGreg Roach                                if ($t === 'DATE') {
2162a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2163a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2164a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2165a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2166a6f13a4aSGreg Roach                                    $keep = true;
2167a6f13a4aSGreg Roach                                }
2168a6f13a4aSGreg Roach                                break;
2169a6f13a4aSGreg Roach                            default:
2170a6f13a4aSGreg Roach                                if ($v == $val) {
2171a6f13a4aSGreg Roach                                    $keep = true;
2172a6f13a4aSGreg Roach                                } else {
2173a6f13a4aSGreg Roach                                    $keep = false;
2174a6f13a4aSGreg Roach                                }
2175a6f13a4aSGreg Roach                                break;
2176a6f13a4aSGreg Roach                        }
2177a6f13a4aSGreg Roach                    }
2178a6f13a4aSGreg Roach                }
2179a6f13a4aSGreg Roach                if ($keep) {
2180a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2181a6f13a4aSGreg Roach                }
2182a6f13a4aSGreg Roach            }
2183a6f13a4aSGreg Roach            $this->list = $mylist;
2184a6f13a4aSGreg Roach        }
2185a6f13a4aSGreg Roach
2186a6f13a4aSGreg Roach        switch ($sortby) {
2187a6f13a4aSGreg Roach            case 'NAME':
2188c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2189a6f13a4aSGreg Roach                break;
2190a6f13a4aSGreg Roach            case 'CHAN':
2191c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2192a6f13a4aSGreg Roach                break;
2193a6f13a4aSGreg Roach            case 'BIRT:DATE':
2194c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2195a6f13a4aSGreg Roach                break;
2196a6f13a4aSGreg Roach            case 'DEAT:DATE':
2197c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2198a6f13a4aSGreg Roach                break;
2199a6f13a4aSGreg Roach            case 'MARR:DATE':
2200c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2201a6f13a4aSGreg Roach                break;
2202a6f13a4aSGreg Roach            default:
2203a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2204a6f13a4aSGreg Roach                break;
2205a6f13a4aSGreg Roach        }
2206a6f13a4aSGreg Roach
22079b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2208e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2209a6f13a4aSGreg Roach    }
2210a6f13a4aSGreg Roach
2211a6f13a4aSGreg Roach    /**
2212fab8f067SGreg Roach     * Handle </list>
22138ba2e626SGreg Roach     *
22148ba2e626SGreg Roach     * @return void
2215a6f13a4aSGreg Roach     */
2216b702978eSGreg Roach    protected function listEndHandler(): void
2217c1010edaSGreg Roach    {
2218a6f13a4aSGreg Roach        $this->process_repeats--;
2219a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2220a6f13a4aSGreg Roach            return;
2221a6f13a4aSGreg Roach        }
2222a6f13a4aSGreg Roach
2223a6f13a4aSGreg Roach        // Check if there is any list
2224a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2225a6f13a4aSGreg Roach            $lineoffset = 0;
2226a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2227a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2228a6f13a4aSGreg Roach            }
2229a6f13a4aSGreg Roach            //-- read the xml from the file
2230299d100dSGreg Roach            $lines = file($this->report);
2231dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2232a6f13a4aSGreg Roach                $lineoffset--;
2233a6f13a4aSGreg Roach            }
2234a6f13a4aSGreg Roach            $lineoffset++;
2235a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2236a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2237a6f13a4aSGreg Roach            // List Level counter
2238a6f13a4aSGreg Roach            $count = 1;
2239a6f13a4aSGreg Roach            while (0 < $count) {
2240dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2241a6f13a4aSGreg Roach                    $count++;
2242dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2243a6f13a4aSGreg Roach                    $count--;
2244a6f13a4aSGreg Roach                }
2245a6f13a4aSGreg Roach                if (0 < $count) {
2246a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2247a6f13a4aSGreg Roach                }
2248a6f13a4aSGreg Roach                $line_nr++;
2249a6f13a4aSGreg Roach            }
2250a6f13a4aSGreg Roach            // No need to drag this
2251a6f13a4aSGreg Roach            unset($lines);
22527a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2253a6f13a4aSGreg Roach            // Save original values
22549b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2255a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2256a6f13a4aSGreg Roach
2257a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2258a6f13a4aSGreg Roach            $this->list_private = 0;
2259a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2260a6f13a4aSGreg Roach                if ($record->canShow()) {
2261f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2262a6f13a4aSGreg Roach                    //-- start the sax parser
2263a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2264e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2265a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22661aa04befSGreg Roach
22671aa04befSGreg Roach                    xml_set_element_handler(
22681aa04befSGreg Roach                        $repeat_parser,
22699d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
22701aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
22711aa04befSGreg Roach                        },
22729d454b6bSGreg Roach                        function ($parser, string $name): void {
22731aa04befSGreg Roach                            $this->endElement($parser, $name);
22741aa04befSGreg Roach                        }
22751aa04befSGreg Roach                    );
22761aa04befSGreg Roach
22771aa04befSGreg Roach                    xml_set_character_data_handler(
22781aa04befSGreg Roach                        $repeat_parser,
22799d454b6bSGreg Roach                        function ($parser, string $data): void {
22801aa04befSGreg Roach                            $this->characterData($parser, $data);
22811aa04befSGreg Roach                        }
22821aa04befSGreg Roach                    );
22831aa04befSGreg Roach
2284a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
22856ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2286a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2287a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2288a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2289a6f13a4aSGreg Roach                        ));
2290a6f13a4aSGreg Roach                    }
2291a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2292a6f13a4aSGreg Roach                } else {
2293a6f13a4aSGreg Roach                    $this->list_private++;
2294a6f13a4aSGreg Roach                }
2295a6f13a4aSGreg Roach            }
229613abd6f3SGreg Roach            $this->list   = [];
2297e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2298a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2299a6f13a4aSGreg Roach        }
230065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2301a6f13a4aSGreg Roach    }
2302a6f13a4aSGreg Roach
2303a6f13a4aSGreg Roach    /**
2304fab8f067SGreg Roach     * Handle <listTotal>
2305a6f13a4aSGreg Roach     * Prints the total number of records in a list
2306fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23078ba2e626SGreg Roach     *
23088ba2e626SGreg Roach     * @return void
2309a6f13a4aSGreg Roach     */
2310b702978eSGreg Roach    protected function listTotalStartHandler(): void
2311c1010edaSGreg Roach    {
2312a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2313589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2314a6f13a4aSGreg Roach        } else {
23157a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2316a6f13a4aSGreg Roach        }
2317a6f13a4aSGreg Roach    }
2318a6f13a4aSGreg Roach
2319a6f13a4aSGreg Roach    /**
2320fab8f067SGreg Roach     * Handle <relatives>
232176692c8bSGreg Roach     *
232209482a55SGreg Roach     * @param array<string> $attrs
23238ba2e626SGreg Roach     *
23248ba2e626SGreg Roach     * @return void
2325a6f13a4aSGreg Roach     */
2326b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2327c1010edaSGreg Roach    {
2328a6f13a4aSGreg Roach        $this->process_repeats++;
2329a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2330a6f13a4aSGreg Roach            return;
2331a6f13a4aSGreg Roach        }
2332a6f13a4aSGreg Roach
2333e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2334e364afe4SGreg Roach
233513abd6f3SGreg Roach        $match = [];
2336a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2337d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2338a6f13a4aSGreg Roach            $sortby = trim($sortby);
2339a6f13a4aSGreg Roach        }
2340a6f13a4aSGreg Roach
2341a6f13a4aSGreg Roach        $maxgen = -1;
2342a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2343c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2344a6f13a4aSGreg Roach        }
2345a6f13a4aSGreg Roach
2346e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2347e364afe4SGreg Roach
2348a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2349d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2350a6f13a4aSGreg Roach            $group = trim($group);
2351a6f13a4aSGreg Roach        }
2352a6f13a4aSGreg Roach
2353e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2354e364afe4SGreg Roach
2355a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2356d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2357a6f13a4aSGreg Roach            $id = trim($id);
2358a6f13a4aSGreg Roach        }
2359a6f13a4aSGreg Roach
236013abd6f3SGreg Roach        $this->list = [];
23616b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2362d965cc1aSGreg Roach        if ($person instanceof Individual) {
2363a6f13a4aSGreg Roach            $this->list[$id] = $person;
2364a6f13a4aSGreg Roach            switch ($group) {
23657a6ee1acSGreg Roach                case 'child-family':
236639ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2367820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2368820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2369a6f13a4aSGreg Roach                        }
2370820b62dfSGreg Roach
2371820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2372c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2373a6f13a4aSGreg Roach                        }
2374a6f13a4aSGreg Roach                    }
2375a6f13a4aSGreg Roach                    break;
23767a6ee1acSGreg Roach                case 'spouse-family':
237739ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2378820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2379820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2380a6f13a4aSGreg Roach                        }
2381820b62dfSGreg Roach
2382820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2383c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2384a6f13a4aSGreg Roach                        }
2385a6f13a4aSGreg Roach                    }
2386a6f13a4aSGreg Roach                    break;
23877a6ee1acSGreg Roach                case 'direct-ancestors':
23883d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2389a6f13a4aSGreg Roach                    break;
23907a6ee1acSGreg Roach                case 'ancestors':
23913d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2392a6f13a4aSGreg Roach                    break;
23937a6ee1acSGreg Roach                case 'descendants':
2394a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
23953d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2396a6f13a4aSGreg Roach                    break;
23977a6ee1acSGreg Roach                case 'all':
23983d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
23993d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2400a6f13a4aSGreg Roach                    break;
2401a6f13a4aSGreg Roach            }
2402a6f13a4aSGreg Roach        }
2403a6f13a4aSGreg Roach
2404a6f13a4aSGreg Roach        switch ($sortby) {
2405a6f13a4aSGreg Roach            case 'NAME':
2406c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2407a6f13a4aSGreg Roach                break;
2408a6f13a4aSGreg Roach            case 'BIRT:DATE':
2409c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2410a6f13a4aSGreg Roach                break;
2411a6f13a4aSGreg Roach            case 'DEAT:DATE':
2412c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2413a6f13a4aSGreg Roach                break;
2414a6f13a4aSGreg Roach            case 'generation':
241513abd6f3SGreg Roach                $newarray = [];
2416a6f13a4aSGreg Roach                reset($this->list);
2417a6f13a4aSGreg Roach                $genCounter = 1;
2418a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2419a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2420a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2421a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
2422f70bcff5SGreg Roach                            $newarray[$key] = (object) ['generation' => $this->generation];
2423a6f13a4aSGreg Roach                        }
2424a6f13a4aSGreg Roach                    }
2425a6f13a4aSGreg Roach                    $genCounter++;
2426a6f13a4aSGreg Roach                }
2427a6f13a4aSGreg Roach                $this->list = $newarray;
2428a6f13a4aSGreg Roach                break;
2429a6f13a4aSGreg Roach            default:
2430a6f13a4aSGreg Roach                // unsorted
2431a6f13a4aSGreg Roach                break;
2432a6f13a4aSGreg Roach        }
24339b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2434e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2435a6f13a4aSGreg Roach    }
2436a6f13a4aSGreg Roach
2437a6f13a4aSGreg Roach    /**
2438fab8f067SGreg Roach     * Handle </relatives>
24398ba2e626SGreg Roach     *
24408ba2e626SGreg Roach     * @return void
2441a6f13a4aSGreg Roach     */
2442b702978eSGreg Roach    protected function relativesEndHandler(): void
2443c1010edaSGreg Roach    {
2444a6f13a4aSGreg Roach        $this->process_repeats--;
2445a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2446a6f13a4aSGreg Roach            return;
2447a6f13a4aSGreg Roach        }
2448a6f13a4aSGreg Roach
2449a6f13a4aSGreg Roach        // Check if there is any relatives
2450a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2451a6f13a4aSGreg Roach            $lineoffset = 0;
2452a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2453a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2454a6f13a4aSGreg Roach            }
2455a6f13a4aSGreg Roach            //-- read the xml from the file
2456299d100dSGreg Roach            $lines = file($this->report);
2457dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2458a6f13a4aSGreg Roach                $lineoffset--;
2459a6f13a4aSGreg Roach            }
2460a6f13a4aSGreg Roach            $lineoffset++;
2461a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2462a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2463a6f13a4aSGreg Roach            // Relatives Level counter
2464a6f13a4aSGreg Roach            $count = 1;
2465a6f13a4aSGreg Roach            while (0 < $count) {
2466dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2467a6f13a4aSGreg Roach                    $count++;
2468dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2469a6f13a4aSGreg Roach                    $count--;
2470a6f13a4aSGreg Roach                }
2471a6f13a4aSGreg Roach                if (0 < $count) {
2472a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2473a6f13a4aSGreg Roach                }
2474a6f13a4aSGreg Roach                $line_nr++;
2475a6f13a4aSGreg Roach            }
2476a6f13a4aSGreg Roach            // No need to drag this
2477a6f13a4aSGreg Roach            unset($lines);
2478a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2479a6f13a4aSGreg Roach            // Save original values
24809b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2481a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2482a6f13a4aSGreg Roach
2483a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2484a6f13a4aSGreg Roach            $this->list_private = 0;
2485b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2486a6f13a4aSGreg Roach                if (isset($value->generation)) {
2487a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2488a6f13a4aSGreg Roach                }
24896b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2490299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2491a6f13a4aSGreg Roach
2492a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2493e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2494a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
24951aa04befSGreg Roach
24961aa04befSGreg Roach                xml_set_element_handler(
24971aa04befSGreg Roach                    $repeat_parser,
24989d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
24991aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25001aa04befSGreg Roach                    },
25019d454b6bSGreg Roach                    function ($parser, string $name): void {
25021aa04befSGreg Roach                        $this->endElement($parser, $name);
25031aa04befSGreg Roach                    }
25041aa04befSGreg Roach                );
25051aa04befSGreg Roach
25061aa04befSGreg Roach                xml_set_character_data_handler(
25071aa04befSGreg Roach                    $repeat_parser,
25089d454b6bSGreg Roach                    function ($parser, string $data): void {
25091aa04befSGreg Roach                        $this->characterData($parser, $data);
25101aa04befSGreg Roach                    }
25111aa04befSGreg Roach                );
2512a6f13a4aSGreg Roach
2513a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25146ccdf4f0SGreg 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)));
2515a6f13a4aSGreg Roach                }
2516a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2517a6f13a4aSGreg Roach            }
2518a6f13a4aSGreg Roach            // Clean up the list array
251913abd6f3SGreg Roach            $this->list   = [];
2520e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2521a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2522a6f13a4aSGreg Roach        }
252365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2524a6f13a4aSGreg Roach    }
2525a6f13a4aSGreg Roach
2526a6f13a4aSGreg Roach    /**
2527fab8f067SGreg Roach     * Handle <generation />
2528a6f13a4aSGreg Roach     * Prints the number of generations
25298ba2e626SGreg Roach     *
25308ba2e626SGreg Roach     * @return void
2531a6f13a4aSGreg Roach     */
2532b702978eSGreg Roach    protected function generationStartHandler(): void
2533c1010edaSGreg Roach    {
2534589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2535a6f13a4aSGreg Roach    }
2536a6f13a4aSGreg Roach
2537a6f13a4aSGreg Roach    /**
2538fab8f067SGreg Roach     * Handle <newPage />
2539a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
25408ba2e626SGreg Roach     *
25418ba2e626SGreg Roach     * @return void
2542a6f13a4aSGreg Roach     */
2543b702978eSGreg Roach    protected function newPageStartHandler(): void
2544c1010edaSGreg Roach    {
25457a6ee1acSGreg Roach        $temp = 'addpage';
2546e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2547a6f13a4aSGreg Roach    }
2548a6f13a4aSGreg Roach
2549a6f13a4aSGreg Roach    /**
2550fab8f067SGreg Roach     * Handle </title>
25518ba2e626SGreg Roach     *
25528ba2e626SGreg Roach     * @return void
2553a6f13a4aSGreg Roach     */
2554b702978eSGreg Roach    protected function titleEndHandler(): void
2555c1010edaSGreg Roach    {
25562836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2557a6f13a4aSGreg Roach    }
2558a6f13a4aSGreg Roach
2559a6f13a4aSGreg Roach    /**
2560fab8f067SGreg Roach     * Handle </description>
25618ba2e626SGreg Roach     *
25628ba2e626SGreg Roach     * @return void
2563a6f13a4aSGreg Roach     */
2564b702978eSGreg Roach    protected function descriptionEndHandler(): void
2565c1010edaSGreg Roach    {
25662836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2567a6f13a4aSGreg Roach    }
2568729ce104SGreg Roach
2569729ce104SGreg Roach    /**
257076692c8bSGreg Roach     * Create a list of all descendants.
257176692c8bSGreg Roach     *
25721c7df322SGreg Roach     * @param array<Individual> $list
2573729ce104SGreg Roach     * @param string            $pid
2574729ce104SGreg Roach     * @param bool              $parents
2575729ce104SGreg Roach     * @param int               $generations
25768ba2e626SGreg Roach     *
25778ba2e626SGreg Roach     * @return void
2578729ce104SGreg Roach     */
25793b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2580c1010edaSGreg Roach    {
25816b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2582729ce104SGreg Roach        if ($person === null) {
2583729ce104SGreg Roach            return;
2584729ce104SGreg Roach        }
2585729ce104SGreg Roach        if (!isset($list[$pid])) {
2586729ce104SGreg Roach            $list[$pid] = $person;
2587729ce104SGreg Roach        }
2588729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2589729ce104SGreg Roach            $list[$pid]->generation = 0;
2590729ce104SGreg Roach        }
259139ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2592729ce104SGreg Roach            if ($parents) {
259339ca88baSGreg Roach                $husband = $family->husband();
259439ca88baSGreg Roach                $wife    = $family->wife();
2595729ce104SGreg Roach                if ($husband) {
2596c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2597729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2598c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2599729ce104SGreg Roach                    } else {
2600c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2601729ce104SGreg Roach                    }
2602729ce104SGreg Roach                }
2603729ce104SGreg Roach                if ($wife) {
2604c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2605729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2606c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2607729ce104SGreg Roach                    } else {
2608c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2609729ce104SGreg Roach                    }
2610729ce104SGreg Roach                }
2611729ce104SGreg Roach            }
2612820b62dfSGreg Roach
261339ca88baSGreg Roach            $children = $family->children();
2614820b62dfSGreg Roach
2615729ce104SGreg Roach            foreach ($children as $child) {
2616729ce104SGreg Roach                if ($child) {
2617c0935879SGreg Roach                    $list[$child->xref()] = $child;
2618820b62dfSGreg Roach
2619729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2620c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2621729ce104SGreg Roach                    } else {
2622c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2623729ce104SGreg Roach                    }
2624729ce104SGreg Roach                }
2625729ce104SGreg Roach            }
2626729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2627729ce104SGreg Roach                foreach ($children as $child) {
2628c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2629729ce104SGreg Roach                }
2630729ce104SGreg Roach            }
2631729ce104SGreg Roach        }
2632729ce104SGreg Roach    }
2633729ce104SGreg Roach
2634729ce104SGreg Roach    /**
263576692c8bSGreg Roach     * Create a list of all ancestors.
263676692c8bSGreg Roach     *
26371c7df322SGreg Roach     * @param array<Individual> $list
2638729ce104SGreg Roach     * @param string            $pid
2639729ce104SGreg Roach     * @param bool              $children
2640729ce104SGreg Roach     * @param int               $generations
26418ba2e626SGreg Roach     *
26428ba2e626SGreg Roach     * @return void
2643729ce104SGreg Roach     */
26443b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2645c1010edaSGreg Roach    {
264613abd6f3SGreg Roach        $genlist                = [$pid];
2647729ce104SGreg Roach        $list[$pid]->generation = 1;
2648729ce104SGreg Roach        while (count($genlist) > 0) {
2649729ce104SGreg Roach            $id = array_shift($genlist);
2650dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2651729ce104SGreg Roach                continue; // id can be something like “empty7”
2652729ce104SGreg Roach            }
26536b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
265439ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
265539ca88baSGreg Roach                $husband = $family->husband();
265639ca88baSGreg Roach                $wife    = $family->wife();
2657729ce104SGreg Roach                if ($husband) {
2658c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2659c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2660729ce104SGreg Roach                }
2661729ce104SGreg Roach                if ($wife) {
2662c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2663c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2664729ce104SGreg Roach                }
2665729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2666729ce104SGreg Roach                    if ($husband) {
2667c0935879SGreg Roach                        $genlist[] = $husband->xref();
2668729ce104SGreg Roach                    }
2669729ce104SGreg Roach                    if ($wife) {
2670c0935879SGreg Roach                        $genlist[] = $wife->xref();
2671729ce104SGreg Roach                    }
2672729ce104SGreg Roach                }
2673729ce104SGreg Roach                if ($children) {
267439ca88baSGreg Roach                    foreach ($family->children() as $child) {
2675c0935879SGreg Roach                        $list[$child->xref()] = $child;
2676e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2677729ce104SGreg Roach                    }
2678729ce104SGreg Roach                }
2679729ce104SGreg Roach            }
2680729ce104SGreg Roach        }
2681729ce104SGreg Roach    }
2682729ce104SGreg Roach
2683729ce104SGreg Roach    /**
2684729ce104SGreg Roach     * get gedcom tag value
2685729ce104SGreg Roach     *
2686729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2687729ce104SGreg 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
2688729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2689729ce104SGreg Roach     *
2690729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2691729ce104SGreg Roach     */
2692b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2693c1010edaSGreg Roach    {
2694b2448a1bSGreg Roach        if ($gedrec === '') {
2695729ce104SGreg Roach            return '';
2696729ce104SGreg Roach        }
2697729ce104SGreg Roach        $tags      = explode(':', $tag);
2698729ce104SGreg Roach        $origlevel = $level;
2699b2448a1bSGreg Roach        if ($level === 0) {
27003c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2701729ce104SGreg Roach        }
2702729ce104SGreg Roach
2703729ce104SGreg Roach        $subrec = $gedrec;
2704f71a7dedSGreg Roach        $t = 'XXXX';
2705729ce104SGreg Roach        foreach ($tags as $t) {
2706729ce104SGreg Roach            $lastsubrec = $subrec;
27073d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2708729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2709729ce104SGreg Roach                $level--;
27103d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2711729ce104SGreg Roach            }
2712729ce104SGreg Roach            if (empty($subrec)) {
2713044416d2SGreg Roach                if ($t === 'TITL') {
27143d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2715729ce104SGreg Roach                    if (!empty($subrec)) {
27167a6ee1acSGreg Roach                        $t = 'ABBR';
2717729ce104SGreg Roach                    }
2718729ce104SGreg Roach                }
2719b2448a1bSGreg Roach                if ($subrec === '') {
2720729ce104SGreg Roach                    if ($level > 0) {
2721729ce104SGreg Roach                        $level--;
2722729ce104SGreg Roach                    }
27233d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2724b2448a1bSGreg Roach                    if ($subrec === '') {
2725729ce104SGreg Roach                        return '';
2726729ce104SGreg Roach                    }
2727729ce104SGreg Roach                }
2728729ce104SGreg Roach            }
2729729ce104SGreg Roach            $level++;
2730729ce104SGreg Roach        }
2731729ce104SGreg Roach        $level--;
2732729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2733f71a7dedSGreg Roach        if ($ct === 0) {
2734729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2735729ce104SGreg Roach        }
2736f71a7dedSGreg Roach        if ($ct === 0) {
2737729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2738729ce104SGreg Roach        }
2739729ce104SGreg Roach        if ($ct > 0) {
2740729ce104SGreg Roach            $value = trim($match[1]);
2741044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
27426b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2743ff166e64SGreg Roach                if ($note instanceof Note) {
2744729ce104SGreg Roach                    $value = $note->getNote();
2745729ce104SGreg Roach                } else {
2746729ce104SGreg Roach                    //-- set the value to the id without the @
2747729ce104SGreg Roach                    $value = $match[1];
2748729ce104SGreg Roach                }
2749729ce104SGreg Roach            }
2750f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
27513d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2752729ce104SGreg Roach            }
2753729ce104SGreg Roach
2754729ce104SGreg Roach            return $value;
2755729ce104SGreg Roach        }
2756729ce104SGreg Roach
27577a6ee1acSGreg Roach        return '';
2758729ce104SGreg Roach    }
2759d1286247SGreg Roach
2760d1286247SGreg Roach    /**
2761d1286247SGreg Roach     * Replace variable identifiers with their values.
2762d1286247SGreg Roach     *
2763d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
276482759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2765d1286247SGreg Roach     *
2766d1286247SGreg Roach     * @return string
2767d1286247SGreg Roach     */
27688f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2769c1010edaSGreg Roach    {
2770d1286247SGreg Roach        return preg_replace_callback(
2771d1286247SGreg Roach            '/\$(\w+)/',
277218d7a90dSGreg Roach            function (array $matches) use ($quote): string {
27732118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
277482759250SGreg Roach                    if ($quote) {
27752118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2776b2ce94c6SRico Sonntag                    }
2777b2ce94c6SRico Sonntag
27782118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
277982759250SGreg Roach                }
2780b2ce94c6SRico Sonntag
2781d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
27823d7a8a4cSGreg Roach
2783d1286247SGreg Roach                return '$' . $matches[1];
2784d1286247SGreg Roach            },
2785d1286247SGreg Roach            $expression
2786d1286247SGreg Roach        );
2787d1286247SGreg Roach    }
2788a6f13a4aSGreg Roach}
2789