xref: /webtrees/app/Report/ReportParserGenerate.php (revision 870ec5a3f6f8023085b468a88a94b244578a1408)
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;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
28a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
293d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
309a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
33a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
34a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
35d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
36a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
37729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
38a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
39299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
40195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
41195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
42a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
435985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
449a9e551aSGreg Roachuse Illuminate\Support\Str;
45f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
4631d9777aSGreg Roachuse LogicException;
4779529c87SGreg Roachuse stdClass;
48c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
495809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
5059597b37SGreg Roach
51b6f35a76SGreg Roachuse function addcslashes;
52b6f35a76SGreg Roachuse function addslashes;
53b6f35a76SGreg Roachuse function array_pop;
54b6f35a76SGreg Roachuse function array_shift;
55b6f35a76SGreg Roachuse function assert;
56b19e047dSGreg Roachuse function call_user_func;
57b6f35a76SGreg Roachuse function count;
58b6f35a76SGreg Roachuse function end;
59b2448a1bSGreg Roachuse function explode;
60b6f35a76SGreg Roachuse function file;
61b6f35a76SGreg Roachuse function file_exists;
62b6f35a76SGreg Roachuse function getimagesize;
6329518ad2SGreg Roachuse function imagecreatefromstring;
6429518ad2SGreg Roachuse function imagesx;
6529518ad2SGreg Roachuse function imagesy;
66b6f35a76SGreg Roachuse function in_array;
67b19e047dSGreg Roachuse function method_exists;
68b6f35a76SGreg Roachuse function preg_match;
69b6f35a76SGreg Roachuse function preg_match_all;
70b6f35a76SGreg Roachuse function preg_replace;
71b6f35a76SGreg Roachuse function preg_replace_callback;
72b6f35a76SGreg Roachuse function preg_split;
73b6f35a76SGreg Roachuse function reset;
74b6f35a76SGreg Roachuse function round;
75b6f35a76SGreg Roachuse function sprintf;
76dec352c1SGreg Roachuse function str_contains;
77b6f35a76SGreg Roachuse function str_replace;
78dec352c1SGreg Roachuse function str_starts_with;
79b6f35a76SGreg Roachuse function strip_tags;
80b6f35a76SGreg Roachuse function strlen;
81b6f35a76SGreg Roachuse function strtoupper;
82b6f35a76SGreg Roachuse function substr;
83b2448a1bSGreg Roachuse function trim;
84b6f35a76SGreg Roachuse function uasort;
85b6f35a76SGreg Roachuse function xml_error_string;
86b6f35a76SGreg Roachuse function xml_get_current_line_number;
87b6f35a76SGreg Roachuse function xml_get_error_code;
88b6f35a76SGreg Roachuse function xml_parse;
89b6f35a76SGreg Roachuse function xml_parser_create;
90b6f35a76SGreg Roachuse function xml_parser_free;
91b6f35a76SGreg Roachuse function xml_parser_set_option;
92b6f35a76SGreg Roachuse function xml_set_character_data_handler;
93b6f35a76SGreg Roachuse function xml_set_element_handler;
94b6f35a76SGreg Roach
95b6f35a76SGreg Roachuse const PREG_SET_ORDER;
96b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9729518ad2SGreg Roach
98a6f13a4aSGreg Roach/**
99a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
100a6f13a4aSGreg Roach */
101c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
102c1010edaSGreg Roach{
103a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
104a6f13a4aSGreg Roach    private $process_footnote = true;
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
107a6f13a4aSGreg Roach    private $print_data = false;
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
11013abd6f3SGreg Roach    private $print_data_stack = [];
111a6f13a4aSGreg Roach
11276692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
113a6f13a4aSGreg Roach    private $process_gedcoms = 0;
114a6f13a4aSGreg Roach
11576692c8bSGreg Roach    /** @var int Are we processing conditionals */
116a6f13a4aSGreg Roach    private $process_ifs = 0;
117a6f13a4aSGreg Roach
11876692c8bSGreg Roach    /** @var int Are we processing repeats */
119a6f13a4aSGreg Roach    private $process_repeats = 0;
120a6f13a4aSGreg Roach
121a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
122a6f13a4aSGreg Roach    private $repeat_bytes = 0;
123a6f13a4aSGreg Roach
1245b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
12513abd6f3SGreg Roach    private $repeats = [];
126a6f13a4aSGreg Roach
127a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
12813abd6f3SGreg Roach    private $repeats_stack = [];
129a6f13a4aSGreg Roach
130b6f35a76SGreg Roach    /** @var AbstractRenderer[] Nested repeating data */
13113abd6f3SGreg Roach    private $wt_report_stack = [];
132e8e7866bSGreg Roach
133e8e7866bSGreg Roach    /** @var resource Nested repeating data */
134e8e7866bSGreg Roach    private $parser;
135e8e7866bSGreg Roach
136e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
13713abd6f3SGreg Roach    private $parser_stack = [];
138e8e7866bSGreg Roach
139a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
140a6f13a4aSGreg Roach    private $gedrec = '';
141a6f13a4aSGreg Roach
142a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
14313abd6f3SGreg Roach    private $gedrec_stack = [];
144a6f13a4aSGreg Roach
145a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
146a6f13a4aSGreg Roach    private $current_element;
147a6f13a4aSGreg Roach
148a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
149a6f13a4aSGreg Roach    private $footnote_element;
150a6f13a4aSGreg Roach
151a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
152a6f13a4aSGreg Roach    private $fact = '';
153a6f13a4aSGreg Roach
154a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
155a6f13a4aSGreg Roach    private $desc = '';
156a6f13a4aSGreg Roach
157a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
158a6f13a4aSGreg Roach    private $type = '';
159a6f13a4aSGreg Roach
160a6f13a4aSGreg Roach    /** @var int The current generational level */
161a6f13a4aSGreg Roach    private $generation = 1;
162a6f13a4aSGreg Roach
163a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
16413abd6f3SGreg Roach    private $list = [];
165a6f13a4aSGreg Roach
166a6f13a4aSGreg Roach    /** @var int Number of items in lists */
167a6f13a4aSGreg Roach    private $list_total = 0;
168a6f13a4aSGreg Roach
169a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
170a6f13a4aSGreg Roach    private $list_private = 0;
171a6f13a4aSGreg Roach
172299d100dSGreg Roach    /** @var string The filename of the XML report */
173299d100dSGreg Roach    protected $report;
174299d100dSGreg Roach
175b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
176e8e7866bSGreg Roach    private $report_root;
177e8e7866bSGreg Roach
178b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
179e8e7866bSGreg Roach    private $wt_report;
180e8e7866bSGreg Roach
181d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1822118c0e3SGreg Roach    private $vars;
183d1286247SGreg Roach
184299d100dSGreg Roach    /** @var Tree The current tree */
185299d100dSGreg Roach    private $tree;
186299d100dSGreg Roach
187f7cf8a15SGreg Roach    /** @var FilesystemOperator */
188a04bb9a2SGreg Roach    private $data_filesystem;
189a04bb9a2SGreg Roach
19076692c8bSGreg Roach    /**
19176692c8bSGreg Roach     * Create a parser for a report
19276692c8bSGreg Roach     *
19376692c8bSGreg Roach     * @param string             $report The XML filename
194b6f35a76SGreg Roach     * @param AbstractRenderer   $report_root
19576692c8bSGreg Roach     * @param string[][]         $vars
196299d100dSGreg Roach     * @param Tree               $tree
197f7cf8a15SGreg Roach     * @param FilesystemOperator $data_filesystem
19876692c8bSGreg Roach     */
199a04bb9a2SGreg Roach    public function __construct(
200a04bb9a2SGreg Roach        string $report,
201b6f35a76SGreg Roach        AbstractRenderer $report_root,
202a04bb9a2SGreg Roach        array $vars,
203a04bb9a2SGreg Roach        Tree $tree,
204f7cf8a15SGreg Roach        FilesystemOperator $data_filesystem
205a04bb9a2SGreg Roach    ) {
206299d100dSGreg Roach        $this->report          = $report;
207e8e7866bSGreg Roach        $this->report_root     = $report_root;
208e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20959f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
210d1286247SGreg Roach        $this->vars            = $vars;
211299d100dSGreg Roach        $this->tree            = $tree;
212a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
213299d100dSGreg Roach
21476f666f4SGreg Roach        parent::__construct($report);
215a6f13a4aSGreg Roach    }
216a6f13a4aSGreg Roach
217a6f13a4aSGreg Roach    /**
218a6f13a4aSGreg Roach     * XML start element handler
219a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
220a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
221a6f13a4aSGreg Roach     *
222a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
223a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
2248a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
22518d7a90dSGreg Roach     *
22618d7a90dSGreg Roach     * @return void
227a6f13a4aSGreg Roach     */
228af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
229c1010edaSGreg Roach    {
23013abd6f3SGreg Roach        $newattrs = [];
231a6f13a4aSGreg Roach
232a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
233a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
234e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
235d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
236a6f13a4aSGreg Roach                }
237a6f13a4aSGreg Roach            }
238a6f13a4aSGreg Roach            $newattrs[$key] = $value;
239a6f13a4aSGreg Roach        }
240a6f13a4aSGreg Roach        $attrs = $newattrs;
2417a6ee1acSGreg 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')) {
242a0e2939aSGreg Roach            $method = $name . 'StartHandler';
243208e9f76SGreg Roach
244a0e2939aSGreg Roach            if (method_exists($this, $method)) {
245b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
246a6f13a4aSGreg Roach            }
247a6f13a4aSGreg Roach        }
248a6f13a4aSGreg Roach    }
249a6f13a4aSGreg Roach
250a6f13a4aSGreg Roach    /**
251a6f13a4aSGreg Roach     * XML end element handler
252a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
253a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
254a6f13a4aSGreg Roach     *
255a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
256a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
25718d7a90dSGreg Roach     *
25818d7a90dSGreg Roach     * @return void
259a6f13a4aSGreg Roach     */
260af14d238SGreg Roach    protected function endElement($parser, string $name): void
261c1010edaSGreg Roach    {
2627a6ee1acSGreg 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')) {
263a0e2939aSGreg Roach            $method = $name . 'EndHandler';
264a0e2939aSGreg Roach
265a0e2939aSGreg Roach            if (method_exists($this, $method)) {
266b19e047dSGreg Roach                call_user_func([$this, $method]);
267a6f13a4aSGreg Roach            }
268a6f13a4aSGreg Roach        }
269a6f13a4aSGreg Roach    }
270a6f13a4aSGreg Roach
271a6f13a4aSGreg Roach    /**
272a6f13a4aSGreg Roach     * XML character data handler
273a6f13a4aSGreg Roach     *
274a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
275a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
27618d7a90dSGreg Roach     *
27718d7a90dSGreg Roach     * @return void
278a6f13a4aSGreg Roach     */
27924f2a3afSGreg Roach    protected function characterData($parser, string $data): void
280c1010edaSGreg Roach    {
281e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
282a6f13a4aSGreg Roach            $this->current_element->addText($data);
283a6f13a4aSGreg Roach        }
284a6f13a4aSGreg Roach    }
285a6f13a4aSGreg Roach
286a6f13a4aSGreg Roach    /**
287fab8f067SGreg Roach     * Handle <style>
288a6f13a4aSGreg Roach     *
289fab8f067SGreg Roach     * @param string[] $attrs
2908ba2e626SGreg Roach     *
2918ba2e626SGreg Roach     * @return void
292a6f13a4aSGreg Roach     */
293b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
294c1010edaSGreg Roach    {
295a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2966ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
297a6f13a4aSGreg Roach        }
298a6f13a4aSGreg Roach
299a6f13a4aSGreg Roach        // array Style that will be passed on
30013abd6f3SGreg Roach        $s = [];
301a6f13a4aSGreg Roach
302a6f13a4aSGreg Roach        // string Name af the style
303a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
304a6f13a4aSGreg Roach
305a6f13a4aSGreg Roach        // string Name of the DEFAULT font
306208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
307a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
308a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
309a6f13a4aSGreg Roach        }
310a6f13a4aSGreg Roach
311a6f13a4aSGreg Roach        // int The size of the font in points
312208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
313a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
31459597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
31559597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
31659597b37SGreg Roach        }
317a6f13a4aSGreg Roach
318a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3197a6ee1acSGreg Roach        $s['style'] = '';
320a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
321a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
322a6f13a4aSGreg Roach        }
323a6f13a4aSGreg Roach
324e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
325a6f13a4aSGreg Roach    }
326a6f13a4aSGreg Roach
327a6f13a4aSGreg Roach    /**
328fab8f067SGreg Roach     * Handle <doc>
329a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
330a6f13a4aSGreg Roach     *
331fab8f067SGreg Roach     * @param string[] $attrs
3328ba2e626SGreg Roach     *
3338ba2e626SGreg Roach     * @return void
334a6f13a4aSGreg Roach     */
335b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
336c1010edaSGreg Roach    {
337e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
338a6f13a4aSGreg Roach
339a6f13a4aSGreg Roach        // Custom page width
340a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
341208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
342a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
343a6f13a4aSGreg Roach        // Custom Page height
344a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
345208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
346a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
347a6f13a4aSGreg Roach
348a6f13a4aSGreg Roach        // Left Margin
349a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
3507a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
351208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
352a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
353208e9f76SGreg 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))
354a6f13a4aSGreg Roach            }
355a6f13a4aSGreg Roach        }
356a6f13a4aSGreg Roach        // Right Margin
357a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3587a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
359208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
360a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
361208e9f76SGreg 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))
362a6f13a4aSGreg Roach            }
363a6f13a4aSGreg Roach        }
364a6f13a4aSGreg Roach        // Top Margin
365a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3667a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
367208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
368a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
369208e9f76SGreg 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))
370a6f13a4aSGreg Roach            }
371a6f13a4aSGreg Roach        }
372a6f13a4aSGreg Roach        // Bottom Margin
373a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3747a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
375208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
376a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
377208e9f76SGreg 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))
378a6f13a4aSGreg Roach            }
379a6f13a4aSGreg Roach        }
380a6f13a4aSGreg Roach        // Header Margin
381a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3827a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
383208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
384a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
385208e9f76SGreg 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))
386a6f13a4aSGreg Roach            }
387a6f13a4aSGreg Roach        }
388a6f13a4aSGreg Roach        // Footer Margin
389a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3907a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
391208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
392a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
393208e9f76SGreg 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))
394a6f13a4aSGreg Roach            }
395a6f13a4aSGreg Roach        }
396a6f13a4aSGreg Roach
397a6f13a4aSGreg Roach        // Page Orientation
398a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
399044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4007a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
401044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4027a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
403a6f13a4aSGreg Roach            }
404a6f13a4aSGreg Roach        }
405a6f13a4aSGreg Roach        // Page Size
406a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
407208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
408a6f13a4aSGreg Roach        }
409a6f13a4aSGreg Roach
410a6f13a4aSGreg Roach        // Show Generated By...
411a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4127a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
413208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4147a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
415208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
416a6f13a4aSGreg Roach            }
417a6f13a4aSGreg Roach        }
418a6f13a4aSGreg Roach
419e8e7866bSGreg Roach        $this->wt_report->setup();
420a6f13a4aSGreg Roach    }
421a6f13a4aSGreg Roach
422a6f13a4aSGreg Roach    /**
423fab8f067SGreg Roach     * Handle </doc>
4248ba2e626SGreg Roach     *
4258ba2e626SGreg Roach     * @return void
426a6f13a4aSGreg Roach     */
427b702978eSGreg Roach    protected function docEndHandler(): void
428c1010edaSGreg Roach    {
429e8e7866bSGreg Roach        $this->wt_report->run();
430a6f13a4aSGreg Roach    }
431a6f13a4aSGreg Roach
432a6f13a4aSGreg Roach    /**
433fab8f067SGreg Roach     * Handle <header>
4348ba2e626SGreg Roach     *
4358ba2e626SGreg Roach     * @return void
436a6f13a4aSGreg Roach     */
437b702978eSGreg Roach    protected function headerStartHandler(): void
438c1010edaSGreg Roach    {
439a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
440e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4417a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
442a6f13a4aSGreg Roach    }
443a6f13a4aSGreg Roach
444a6f13a4aSGreg Roach    /**
445fab8f067SGreg Roach     * Handle <body>
4468ba2e626SGreg Roach     *
4478ba2e626SGreg Roach     * @return void
448a6f13a4aSGreg Roach     */
449b702978eSGreg Roach    protected function bodyStartHandler(): void
450c1010edaSGreg Roach    {
4517a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
452a6f13a4aSGreg Roach    }
453a6f13a4aSGreg Roach
454a6f13a4aSGreg Roach    /**
455fab8f067SGreg Roach     * Handle <footer>
4568ba2e626SGreg Roach     *
4578ba2e626SGreg Roach     * @return void
458a6f13a4aSGreg Roach     */
459b702978eSGreg Roach    protected function footerStartHandler(): void
460c1010edaSGreg Roach    {
4617a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
462a6f13a4aSGreg Roach    }
463a6f13a4aSGreg Roach
464a6f13a4aSGreg Roach    /**
465fab8f067SGreg Roach     * Handle <cell>
466a6f13a4aSGreg Roach     *
467fab8f067SGreg Roach     * @param string[] $attrs
4688ba2e626SGreg Roach     *
4698ba2e626SGreg Roach     * @return void
470a6f13a4aSGreg Roach     */
471b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
472c1010edaSGreg Roach    {
473a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4747a6ee1acSGreg Roach        $align = '';
475a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
476a6f13a4aSGreg Roach            $align = $attrs['align'];
477a6f13a4aSGreg Roach            // RTL supported left/right alignment
478044416d2SGreg Roach            if ($align === 'rightrtl') {
479e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4807a6ee1acSGreg Roach                    $align = 'left';
481a6f13a4aSGreg Roach                } else {
4827a6ee1acSGreg Roach                    $align = 'right';
483a6f13a4aSGreg Roach                }
484044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
485e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4867a6ee1acSGreg Roach                    $align = 'right';
487a6f13a4aSGreg Roach                } else {
4887a6ee1acSGreg Roach                    $align = 'left';
489a6f13a4aSGreg Roach                }
490a6f13a4aSGreg Roach            }
491a6f13a4aSGreg Roach        }
492a6f13a4aSGreg Roach
493a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4947a6ee1acSGreg Roach        $bgcolor = '';
495a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
496a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
497a6f13a4aSGreg Roach        }
498a6f13a4aSGreg Roach
499a6f13a4aSGreg Roach        // int Whether or not the background should be painted
500a6f13a4aSGreg Roach        $fill = 1;
501a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
5027a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
503a6f13a4aSGreg Roach                $fill = 0;
5047a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
505a6f13a4aSGreg Roach                $fill = 1;
506a6f13a4aSGreg Roach            }
507a6f13a4aSGreg Roach        }
508a6f13a4aSGreg Roach
509a6f13a4aSGreg Roach        $reseth = true;
510a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
511a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
5127a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
513a6f13a4aSGreg Roach                $reseth = false;
5147a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
515a6f13a4aSGreg Roach                $reseth = true;
516a6f13a4aSGreg Roach            }
517a6f13a4aSGreg Roach        }
518a6f13a4aSGreg Roach
519a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
520a6f13a4aSGreg Roach        $border = 0;
521a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
522a6f13a4aSGreg Roach            $border = $attrs['border'];
523a6f13a4aSGreg Roach        }
524a6f13a4aSGreg Roach        // string Border color in HTML code
5257a6ee1acSGreg Roach        $bocolor = '';
526a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
527a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
528a6f13a4aSGreg Roach        }
529a6f13a4aSGreg Roach
530a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
531a6f13a4aSGreg Roach        $height = 0;
532a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
533589feda3SGreg Roach            $height = $attrs['height'];
534a6f13a4aSGreg Roach        }
535a6f13a4aSGreg 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.
536a6f13a4aSGreg Roach        $width = 0;
537a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
538589feda3SGreg Roach            $width = $attrs['width'];
539a6f13a4aSGreg Roach        }
540a6f13a4aSGreg Roach
541a6f13a4aSGreg Roach        // int Stretch carachter mode
542a6f13a4aSGreg Roach        $stretch = 0;
543a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
544a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
545a6f13a4aSGreg Roach        }
546a6f13a4aSGreg Roach
547a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
548c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
549a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5507a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
551c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
552a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
553a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5547a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
555a6f13a4aSGreg Roach                $left = 0;
556a6f13a4aSGreg Roach            }
557a6f13a4aSGreg Roach        }
558a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
559c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
560a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5617a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
562c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
563a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
564a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5657a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
566a6f13a4aSGreg Roach                $top = 0;
567a6f13a4aSGreg Roach            }
568a6f13a4aSGreg Roach        }
569a6f13a4aSGreg Roach
570a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5717a6ee1acSGreg Roach        $style = '';
572a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
573a6f13a4aSGreg Roach            $style = $attrs['style'];
574a6f13a4aSGreg Roach        }
575a6f13a4aSGreg Roach
576a6f13a4aSGreg Roach        // string Text color in html code
5777a6ee1acSGreg Roach        $tcolor = '';
578a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
579a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
580a6f13a4aSGreg Roach        }
581a6f13a4aSGreg Roach
582a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
583a6f13a4aSGreg Roach        $ln = 0;
584a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
585a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
586a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5877a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
588a6f13a4aSGreg Roach                $ln = 0;
589a6f13a4aSGreg Roach            }
590a6f13a4aSGreg Roach        }
591a6f13a4aSGreg Roach
592044416d2SGreg Roach        if ($align === 'left') {
5937a6ee1acSGreg Roach            $align = 'L';
594044416d2SGreg Roach        } elseif ($align === 'right') {
5957a6ee1acSGreg Roach            $align = 'R';
596044416d2SGreg Roach        } elseif ($align === 'center') {
5977a6ee1acSGreg Roach            $align = 'C';
598044416d2SGreg Roach        } elseif ($align === 'justify') {
5997a6ee1acSGreg Roach            $align = 'J';
600a6f13a4aSGreg Roach        }
601a6f13a4aSGreg Roach
6029b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
603a6f13a4aSGreg Roach        $this->print_data         = true;
604a6f13a4aSGreg Roach
605e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
60624f2a3afSGreg Roach            (int) $width,
60724f2a3afSGreg Roach            (int) $height,
608a6f13a4aSGreg Roach            $border,
609a6f13a4aSGreg Roach            $align,
610a6f13a4aSGreg Roach            $bgcolor,
611a6f13a4aSGreg Roach            $style,
612a6f13a4aSGreg Roach            $ln,
613a6f13a4aSGreg Roach            $top,
614a6f13a4aSGreg Roach            $left,
615a6f13a4aSGreg Roach            $fill,
616a6f13a4aSGreg Roach            $stretch,
617a6f13a4aSGreg Roach            $bocolor,
618a6f13a4aSGreg Roach            $tcolor,
619a6f13a4aSGreg Roach            $reseth
620a6f13a4aSGreg Roach        );
621a6f13a4aSGreg Roach    }
622a6f13a4aSGreg Roach
623a6f13a4aSGreg Roach    /**
624fab8f067SGreg Roach     * Handle </cell>
6258ba2e626SGreg Roach     *
6268ba2e626SGreg Roach     * @return void
627a6f13a4aSGreg Roach     */
628b702978eSGreg Roach    protected function cellEndHandler(): void
629c1010edaSGreg Roach    {
630a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
631e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
632a6f13a4aSGreg Roach    }
633a6f13a4aSGreg Roach
634a6f13a4aSGreg Roach    /**
635fab8f067SGreg Roach     * Handle <now />
6368ba2e626SGreg Roach     *
6378ba2e626SGreg Roach     * @return void
638a6f13a4aSGreg Roach     */
639b702978eSGreg Roach    protected function nowStartHandler(): void
640c1010edaSGreg Roach    {
6414459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
642a6f13a4aSGreg Roach    }
643a6f13a4aSGreg Roach
644a6f13a4aSGreg Roach    /**
645fab8f067SGreg Roach     * Handle <pageNum />
6468ba2e626SGreg Roach     *
6478ba2e626SGreg Roach     * @return void
648a6f13a4aSGreg Roach     */
649b702978eSGreg Roach    protected function pageNumStartHandler(): void
650c1010edaSGreg Roach    {
6517a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
652a6f13a4aSGreg Roach    }
653a6f13a4aSGreg Roach
654a6f13a4aSGreg Roach    /**
655fab8f067SGreg Roach     * Handle <totalPages />
6568ba2e626SGreg Roach     *
6578ba2e626SGreg Roach     * @return void
658a6f13a4aSGreg Roach     */
659b702978eSGreg Roach    protected function totalPagesStartHandler(): void
660c1010edaSGreg Roach    {
6617a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
662a6f13a4aSGreg Roach    }
663a6f13a4aSGreg Roach
664a6f13a4aSGreg Roach    /**
665a6f13a4aSGreg Roach     * Called at the start of an element.
666a6f13a4aSGreg Roach     *
667c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6688ba2e626SGreg Roach     *
6698ba2e626SGreg Roach     * @return void
670a6f13a4aSGreg Roach     */
671b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
672c1010edaSGreg Roach    {
673a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
674a6f13a4aSGreg Roach            $this->process_gedcoms++;
675a6f13a4aSGreg Roach
676a6f13a4aSGreg Roach            return;
677a6f13a4aSGreg Roach        }
678a6f13a4aSGreg Roach
679a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6807a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6817a6ee1acSGreg Roach        $tags      = explode(':', $tag);
682a6f13a4aSGreg Roach        $newgedrec = '';
683a6f13a4aSGreg Roach        if (count($tags) < 2) {
6846b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
685299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
686a6f13a4aSGreg Roach        }
687a6f13a4aSGreg Roach        if (empty($newgedrec)) {
688a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
689a6f13a4aSGreg Roach            $newgedrec = '';
690a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6917a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
692d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
693d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
694a6f13a4aSGreg Roach                    } else {
6956b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
696299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
697a6f13a4aSGreg Roach                    }
698a6f13a4aSGreg Roach                } else {
6997a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
70013abd6f3SGreg Roach                        $gmatch = [];
701a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7026b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
703299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
704a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
705a6f13a4aSGreg Roach                        } else {
706a6f13a4aSGreg Roach                            $newgedrec = '';
707a6f13a4aSGreg Roach                            break;
708a6f13a4aSGreg Roach                        }
709a6f13a4aSGreg Roach                    } else {
710b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
7113d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
712a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
713a6f13a4aSGreg Roach                    }
714a6f13a4aSGreg Roach                }
715a6f13a4aSGreg Roach            }
716a6f13a4aSGreg Roach        }
717a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7189b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
719a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
720a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
721a6f13a4aSGreg Roach                $this->fact = $match[2];
722a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
723a6f13a4aSGreg Roach            }
724a6f13a4aSGreg Roach        } else {
725a6f13a4aSGreg Roach            $this->process_gedcoms++;
726a6f13a4aSGreg Roach        }
727a6f13a4aSGreg Roach    }
728a6f13a4aSGreg Roach
729a6f13a4aSGreg Roach    /**
730a6f13a4aSGreg Roach     * Called at the end of an element.
7318ba2e626SGreg Roach     *
7328ba2e626SGreg Roach     * @return void
733a6f13a4aSGreg Roach     */
734b702978eSGreg Roach    protected function gedcomEndHandler(): void
735c1010edaSGreg Roach    {
736a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
737a6f13a4aSGreg Roach            $this->process_gedcoms--;
738a6f13a4aSGreg Roach        } else {
73965e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
740a6f13a4aSGreg Roach        }
741a6f13a4aSGreg Roach    }
742a6f13a4aSGreg Roach
743a6f13a4aSGreg Roach    /**
744fab8f067SGreg Roach     * Handle <textBox>
745a6f13a4aSGreg Roach     *
746fab8f067SGreg Roach     * @param string[] $attrs
7478ba2e626SGreg Roach     *
7488ba2e626SGreg Roach     * @return void
749a6f13a4aSGreg Roach     */
750b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
751c1010edaSGreg Roach    {
752a6f13a4aSGreg Roach        // string Background color code
7537a6ee1acSGreg Roach        $bgcolor = '';
754a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
755a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
756a6f13a4aSGreg Roach        }
757a6f13a4aSGreg Roach
758a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
759a6f13a4aSGreg Roach        $fill = true;
760a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7617a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
762a6f13a4aSGreg Roach                $fill = false;
7637a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
764a6f13a4aSGreg Roach                $fill = true;
765a6f13a4aSGreg Roach            }
766a6f13a4aSGreg Roach        }
767a6f13a4aSGreg Roach
768a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
769a6f13a4aSGreg Roach        $border = false;
770a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7717a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
772a6f13a4aSGreg Roach                $border = true;
7737a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
774a6f13a4aSGreg Roach                $border = false;
775a6f13a4aSGreg Roach            }
776a6f13a4aSGreg Roach        }
777a6f13a4aSGreg Roach
778a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
779a6f13a4aSGreg Roach        $height = 0;
780a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
781a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
782a6f13a4aSGreg Roach        }
783a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
784a6f13a4aSGreg Roach        $width = 0;
785a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
786a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
787a6f13a4aSGreg Roach        }
788a6f13a4aSGreg Roach
789a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
790c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
791a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7927a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
793c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
794a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
795a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7967a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
797a6f13a4aSGreg Roach                $left = 0;
798a6f13a4aSGreg Roach            }
799a6f13a4aSGreg Roach        }
800a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
801c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
802a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8037a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
804c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
805a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
806a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8077a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
808a6f13a4aSGreg Roach                $top = 0;
809a6f13a4aSGreg Roach            }
810a6f13a4aSGreg Roach        }
811a6f13a4aSGreg 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
812a6f13a4aSGreg Roach        $newline = false;
813a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8147a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
815a6f13a4aSGreg Roach                $newline = true;
8167a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
817a6f13a4aSGreg Roach                $newline = false;
818a6f13a4aSGreg Roach            }
819a6f13a4aSGreg Roach        }
820a6f13a4aSGreg Roach        // boolean
821a6f13a4aSGreg Roach        $pagecheck = true;
822a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8237a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
824a6f13a4aSGreg Roach                $pagecheck = false;
8257a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
826a6f13a4aSGreg Roach                $pagecheck = true;
827a6f13a4aSGreg Roach            }
828a6f13a4aSGreg Roach        }
829a6f13a4aSGreg Roach        // boolean Cell padding
830a6f13a4aSGreg Roach        $padding = true;
831a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8327a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
833a6f13a4aSGreg Roach                $padding = false;
8347a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
835a6f13a4aSGreg Roach                $padding = true;
836a6f13a4aSGreg Roach            }
837a6f13a4aSGreg Roach        }
838a6f13a4aSGreg Roach        // boolean Reset this box Height
839a6f13a4aSGreg Roach        $reseth = false;
840a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8417a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
842a6f13a4aSGreg Roach                $reseth = true;
8437a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
844a6f13a4aSGreg Roach                $reseth = false;
845a6f13a4aSGreg Roach            }
846a6f13a4aSGreg Roach        }
847a6f13a4aSGreg Roach
848a6f13a4aSGreg Roach        // string Style of rendering
8497a6ee1acSGreg Roach        $style = '';
850a6f13a4aSGreg Roach
8519b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
852a6f13a4aSGreg Roach        $this->print_data         = false;
853a6f13a4aSGreg Roach
8549b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
855e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
856a6f13a4aSGreg Roach            $width,
857a6f13a4aSGreg Roach            $height,
858a6f13a4aSGreg Roach            $border,
859a6f13a4aSGreg Roach            $bgcolor,
860a6f13a4aSGreg Roach            $newline,
861a6f13a4aSGreg Roach            $left,
862a6f13a4aSGreg Roach            $top,
863a6f13a4aSGreg Roach            $pagecheck,
864a6f13a4aSGreg Roach            $style,
865a6f13a4aSGreg Roach            $fill,
866a6f13a4aSGreg Roach            $padding,
867a6f13a4aSGreg Roach            $reseth
868a6f13a4aSGreg Roach        );
869a6f13a4aSGreg Roach    }
870a6f13a4aSGreg Roach
871a6f13a4aSGreg Roach    /**
872fab8f067SGreg Roach     * Handle <textBox>
8738ba2e626SGreg Roach     *
8748ba2e626SGreg Roach     * @return void
875a6f13a4aSGreg Roach     */
876b702978eSGreg Roach    protected function textBoxEndHandler(): void
877c1010edaSGreg Roach    {
878a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
879e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
88031d9777aSGreg Roach
88131d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
88231d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
88331d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
88431d9777aSGreg Roach
885e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
886e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
887a6f13a4aSGreg Roach    }
888a6f13a4aSGreg Roach
889a6f13a4aSGreg Roach    /**
89076692c8bSGreg Roach     * XLM <Text>.
89176692c8bSGreg Roach     *
892c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8938ba2e626SGreg Roach     *
8948ba2e626SGreg Roach     * @return void
895a6f13a4aSGreg Roach     */
896b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
897c1010edaSGreg Roach    {
8989b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
899a6f13a4aSGreg Roach        $this->print_data         = true;
900a6f13a4aSGreg Roach
901a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9027a6ee1acSGreg Roach        $style = '';
903a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
904a6f13a4aSGreg Roach            $style = $attrs['style'];
905a6f13a4aSGreg Roach        }
906a6f13a4aSGreg Roach
907a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9087a6ee1acSGreg Roach        $color = '';
909a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
910a6f13a4aSGreg Roach            $color = $attrs['color'];
911a6f13a4aSGreg Roach        }
912a6f13a4aSGreg Roach
913e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
914a6f13a4aSGreg Roach    }
915a6f13a4aSGreg Roach
916a6f13a4aSGreg Roach    /**
917fab8f067SGreg Roach     * Handle </text>
9188ba2e626SGreg Roach     *
9198ba2e626SGreg Roach     * @return void
920a6f13a4aSGreg Roach     */
921b702978eSGreg Roach    protected function textEndHandler(): void
922c1010edaSGreg Roach    {
923a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
924e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
925a6f13a4aSGreg Roach    }
926a6f13a4aSGreg Roach
927a6f13a4aSGreg Roach    /**
928fab8f067SGreg Roach     * Handle <getPersonName />
929a6f13a4aSGreg Roach     * Get the name
930a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
931a6f13a4aSGreg Roach     * 2. id is set with a record id
932a6f13a4aSGreg Roach     *
933c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9348ba2e626SGreg Roach     *
9358ba2e626SGreg Roach     * @return void
936a6f13a4aSGreg Roach     */
937b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
938c1010edaSGreg Roach    {
9397a6ee1acSGreg Roach        $id    = '';
94013abd6f3SGreg Roach        $match = [];
941a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9427a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
943a6f13a4aSGreg Roach                $id = $match[1];
944a6f13a4aSGreg Roach            }
945a6f13a4aSGreg Roach        } else {
9467a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
947d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
948d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
949a6f13a4aSGreg Roach                }
950a6f13a4aSGreg Roach            } else {
9517a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
95213abd6f3SGreg Roach                    $gmatch = [];
953a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
954a6f13a4aSGreg Roach                        $id = $gmatch[1];
955a6f13a4aSGreg Roach                    }
956a6f13a4aSGreg Roach                } else {
957a6f13a4aSGreg Roach                    $id = $attrs['id'];
958a6f13a4aSGreg Roach                }
959a6f13a4aSGreg Roach            }
960a6f13a4aSGreg Roach        }
961a6f13a4aSGreg Roach        if (!empty($id)) {
9626b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9638f038c36SRico Sonntag            if ($record === null) {
964a6f13a4aSGreg Roach                return;
965a6f13a4aSGreg Roach            }
966a6f13a4aSGreg Roach            if (!$record->canShowName()) {
967a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
968a6f13a4aSGreg Roach            } else {
96939ca88baSGreg Roach                $name = $record->fullName();
970a6f13a4aSGreg Roach                $name = strip_tags($name);
971a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
972381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
973a6f13a4aSGreg Roach                } else {
974911124f5SGreg Roach                    $addname = (string) $record->alternateName();
975a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
976a6f13a4aSGreg Roach                    if (!empty($addname)) {
9777a6ee1acSGreg Roach                        $name .= ' ' . $addname;
978a6f13a4aSGreg Roach                    }
979a6f13a4aSGreg Roach                }
980a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
981a6f13a4aSGreg Roach            }
982a6f13a4aSGreg Roach        }
983a6f13a4aSGreg Roach    }
984a6f13a4aSGreg Roach
985a6f13a4aSGreg Roach    /**
986fab8f067SGreg Roach     * Handle <gedcomValue />
987a6f13a4aSGreg Roach     *
988fab8f067SGreg Roach     * @param string[] $attrs
9898ba2e626SGreg Roach     *
9908ba2e626SGreg Roach     * @return void
991a6f13a4aSGreg Roach     */
992b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
993c1010edaSGreg Roach    {
9947a6ee1acSGreg Roach        $id    = '';
99513abd6f3SGreg Roach        $match = [];
9967a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
997a6f13a4aSGreg Roach            $id = $match[1];
998a6f13a4aSGreg Roach        }
999a6f13a4aSGreg Roach
1000044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10017a6ee1acSGreg Roach            $useBreak = '1';
1002a6f13a4aSGreg Roach        } else {
10037a6ee1acSGreg Roach            $useBreak = '0';
1004a6f13a4aSGreg Roach        }
1005a6f13a4aSGreg Roach
1006a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1007a6f13a4aSGreg Roach        if (!empty($tag)) {
1008044416d2SGreg Roach            if ($tag === '@desc') {
1009a6f13a4aSGreg Roach                $value = $this->desc;
1010a6f13a4aSGreg Roach                $value = trim($value);
1011a6f13a4aSGreg Roach                $this->current_element->addText($value);
1012a6f13a4aSGreg Roach            }
1013044416d2SGreg Roach            if ($tag === '@id') {
1014a6f13a4aSGreg Roach                $this->current_element->addText($id);
1015a6f13a4aSGreg Roach            } else {
10167a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1017a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1018b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1019b2448a1bSGreg Roach                    if ($level === 0) {
1020a6f13a4aSGreg Roach                        $level++;
1021a6f13a4aSGreg Roach                    }
1022a6f13a4aSGreg Roach                } else {
1023b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1024a6f13a4aSGreg Roach                }
1025a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10263d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1027a6f13a4aSGreg Roach                switch (end($tags)) {
1028a6f13a4aSGreg Roach                    case 'DATE':
1029a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1030a6f13a4aSGreg Roach                        $value = $tmp->display();
1031a6f13a4aSGreg Roach                        break;
1032a6f13a4aSGreg Roach                    case 'PLAC':
1033299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1034392561bbSGreg Roach                        $value = $tmp->shortName();
1035a6f13a4aSGreg Roach                        break;
1036a6f13a4aSGreg Roach                }
1037044416d2SGreg Roach                if ($useBreak === '1') {
1038a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1039a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1040a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1041a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1042a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10433b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10443b3cfeeaSGreg Roach                        $value = substr($value, 4);
1045a6f13a4aSGreg Roach                    }
1046a6f13a4aSGreg Roach                }
1047d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10483cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
1049299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1050a4d703aeSGreg Roach                }
1051a2a485c3SGreg Roach
1052a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
10539c32db08SGreg Roach                    $value = strip_tags($value);
1054381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1055a2a485c3SGreg Roach                }
1056a6f13a4aSGreg Roach                $this->current_element->addText($value);
1057a6f13a4aSGreg Roach            }
1058a6f13a4aSGreg Roach        }
1059a6f13a4aSGreg Roach    }
1060a6f13a4aSGreg Roach
1061a6f13a4aSGreg Roach    /**
1062fab8f067SGreg Roach     * Handle <repeatTag>
1063a6f13a4aSGreg Roach     *
1064fab8f067SGreg Roach     * @param string[] $attrs
10658ba2e626SGreg Roach     *
10668ba2e626SGreg Roach     * @return void
1067a6f13a4aSGreg Roach     */
1068b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1069c1010edaSGreg Roach    {
1070a6f13a4aSGreg Roach        $this->process_repeats++;
1071a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1072a6f13a4aSGreg Roach            return;
1073a6f13a4aSGreg Roach        }
1074a6f13a4aSGreg Roach
10759b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
107613abd6f3SGreg Roach        $this->repeats         = [];
1077e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1078a6f13a4aSGreg Roach
1079e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1080a6f13a4aSGreg Roach        if (!empty($tag)) {
1081044416d2SGreg Roach            if ($tag === '@desc') {
1082a6f13a4aSGreg Roach                $value = $this->desc;
1083a6f13a4aSGreg Roach                $value = trim($value);
1084a6f13a4aSGreg Roach                $this->current_element->addText($value);
1085a6f13a4aSGreg Roach            } else {
10867a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10877a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1088b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1089b2448a1bSGreg Roach                if ($level === 0) {
1090a6f13a4aSGreg Roach                    $level++;
1091a6f13a4aSGreg Roach                }
1092a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1093a6f13a4aSGreg Roach                $t      = $tag;
1094a6f13a4aSGreg Roach                $count  = count($tags);
1095a6f13a4aSGreg Roach                $i      = 0;
1096a6f13a4aSGreg Roach                while ($i < $count) {
1097a6f13a4aSGreg Roach                    $t = $tags[$i];
1098a6f13a4aSGreg Roach                    if (!empty($t)) {
1099a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
11003d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1101a6f13a4aSGreg Roach                            if (empty($subrec)) {
1102a6f13a4aSGreg Roach                                $level--;
11033d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1104a6f13a4aSGreg Roach                                if (empty($subrec)) {
1105a6f13a4aSGreg Roach                                    return;
1106a6f13a4aSGreg Roach                                }
1107a6f13a4aSGreg Roach                            }
1108a6f13a4aSGreg Roach                        }
1109a6f13a4aSGreg Roach                        $level++;
1110a6f13a4aSGreg Roach                    }
1111a6f13a4aSGreg Roach                    $i++;
1112a6f13a4aSGreg Roach                }
1113a6f13a4aSGreg Roach                $level--;
1114a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1115a6f13a4aSGreg Roach                $i     = 0;
1116a6f13a4aSGreg Roach                while ($i < $count) {
1117a6f13a4aSGreg Roach                    $i++;
1118a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1119a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11208d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11216b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1122a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1123a9007102SGreg Roach                            continue;
1124a9007102SGreg Roach                        }
1125a9007102SGreg Roach                    }
1126a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1127a6f13a4aSGreg Roach                }
1128a6f13a4aSGreg Roach            }
1129a6f13a4aSGreg Roach        }
1130a6f13a4aSGreg Roach    }
1131a6f13a4aSGreg Roach
1132a6f13a4aSGreg Roach    /**
1133fab8f067SGreg Roach     * Handle </repeatTag>
11348ba2e626SGreg Roach     *
11358ba2e626SGreg Roach     * @return void
1136a6f13a4aSGreg Roach     */
1137b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1138c1010edaSGreg Roach    {
1139a6f13a4aSGreg Roach        $this->process_repeats--;
1140a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1141a6f13a4aSGreg Roach            return;
1142a6f13a4aSGreg Roach        }
1143a6f13a4aSGreg Roach
1144a6f13a4aSGreg Roach        // Check if there is anything to repeat
1145a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1146a6f13a4aSGreg Roach            // No need to load them if not used...
1147a6f13a4aSGreg Roach
1148a6f13a4aSGreg Roach            $lineoffset = 0;
1149a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1150a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1151a6f13a4aSGreg Roach            }
1152a6f13a4aSGreg Roach            //-- read the xml from the file
1153299d100dSGreg Roach            $lines = file($this->report);
1154dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1155a6f13a4aSGreg Roach                $lineoffset--;
1156a6f13a4aSGreg Roach            }
1157a6f13a4aSGreg Roach            $lineoffset++;
1158a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1159a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1160a6f13a4aSGreg Roach            // RepeatTag Level counter
1161a6f13a4aSGreg Roach            $count = 1;
1162a6f13a4aSGreg Roach            while (0 < $count) {
1163dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1164a6f13a4aSGreg Roach                    $count++;
1165dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1166a6f13a4aSGreg Roach                    $count--;
1167a6f13a4aSGreg Roach                }
1168a6f13a4aSGreg Roach                if (0 < $count) {
1169a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1170a6f13a4aSGreg Roach                }
1171a6f13a4aSGreg Roach                $line_nr++;
1172a6f13a4aSGreg Roach            }
1173a6f13a4aSGreg Roach            // No need to drag this
1174a6f13a4aSGreg Roach            unset($lines);
1175a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1176a6f13a4aSGreg Roach            // Save original values
11779b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1178a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1179a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1180a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1181a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1182e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1183a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11841aa04befSGreg Roach
11851aa04befSGreg Roach                xml_set_element_handler(
11861aa04befSGreg Roach                    $repeat_parser,
11879d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11881aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11891aa04befSGreg Roach                    },
11909d454b6bSGreg Roach                    function ($parser, string $name): void {
11911aa04befSGreg Roach                        $this->endElement($parser, $name);
11921aa04befSGreg Roach                    }
11931aa04befSGreg Roach                );
11941aa04befSGreg Roach
11951aa04befSGreg Roach                xml_set_character_data_handler(
11961aa04befSGreg Roach                    $repeat_parser,
11979d454b6bSGreg Roach                    function ($parser, string $data): void {
11981aa04befSGreg Roach                        $this->characterData($parser, $data);
11991aa04befSGreg Roach                    }
12001aa04befSGreg Roach                );
12011aa04befSGreg Roach
1202a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12036ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1204a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1205a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1206a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1207a6f13a4aSGreg Roach                    ));
1208a6f13a4aSGreg Roach                }
1209a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1210a6f13a4aSGreg Roach            }
1211a6f13a4aSGreg Roach            // Restore original values
1212a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1213e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1214a6f13a4aSGreg Roach        }
121565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1216a6f13a4aSGreg Roach    }
1217a6f13a4aSGreg Roach
1218a6f13a4aSGreg Roach    /**
1219a6f13a4aSGreg Roach     * Variable lookup
1220a6f13a4aSGreg Roach     * Retrieve predefined variables :
1221a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1222a6f13a4aSGreg Roach     *        1 EVEN This is a description
1223a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1224a6f13a4aSGreg Roach     * $ I18N::translate('....')
1225a6f13a4aSGreg Roach     * $ language_settings[]
1226a6f13a4aSGreg Roach     *
1227c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12288ba2e626SGreg Roach     *
12298ba2e626SGreg Roach     * @return void
1230a6f13a4aSGreg Roach     */
1231b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1232c1010edaSGreg Roach    {
1233a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12346ccdf4f0SGreg 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));
1235a6f13a4aSGreg Roach        }
1236a6f13a4aSGreg Roach
1237a6f13a4aSGreg Roach        $var = $attrs['var'];
1238a6f13a4aSGreg Roach        // SetVar element preset variables
1239d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1240d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1241a6f13a4aSGreg Roach        } else {
1242a6f13a4aSGreg Roach            $tfact = $this->fact;
12437a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1244a6f13a4aSGreg Roach                // Use :
1245a6f13a4aSGreg Roach                // n TYPE This text if string
1246a6f13a4aSGreg Roach                $tfact = $this->type;
1247a6f13a4aSGreg Roach            }
1248c1010edaSGreg Roach            $var = str_replace([
1249c1010edaSGreg Roach                '@fact',
1250c1010edaSGreg Roach                '@desc',
1251c1010edaSGreg Roach            ], [
1252c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1253c1010edaSGreg Roach                $this->desc,
1254c1010edaSGreg Roach            ], $var);
1255a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1256da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1257a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1258a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1259a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1260a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1261a6f13a4aSGreg Roach            }
1262a6f13a4aSGreg Roach        }
1263a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1264a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12657a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1266a6f13a4aSGreg Roach                $g   = new Date($var);
1267a6f13a4aSGreg Roach                $var = $g->display();
1268a6f13a4aSGreg Roach            }
1269a6f13a4aSGreg Roach        }
1270a6f13a4aSGreg Roach        $this->current_element->addText($var);
12712836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1272a6f13a4aSGreg Roach    }
1273a6f13a4aSGreg Roach
1274a6f13a4aSGreg Roach    /**
1275fab8f067SGreg Roach     * Handle <facts>
127676692c8bSGreg Roach     *
1277fab8f067SGreg Roach     * @param string[] $attrs
12788ba2e626SGreg Roach     *
12798ba2e626SGreg Roach     * @return void
1280a6f13a4aSGreg Roach     */
1281b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1282c1010edaSGreg Roach    {
1283a6f13a4aSGreg Roach        $this->process_repeats++;
1284a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1285a6f13a4aSGreg Roach            return;
1286a6f13a4aSGreg Roach        }
1287a6f13a4aSGreg Roach
12889b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
128913abd6f3SGreg Roach        $this->repeats         = [];
1290e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1291a6f13a4aSGreg Roach
12927a6ee1acSGreg Roach        $id    = '';
129313abd6f3SGreg Roach        $match = [];
12947a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1295a6f13a4aSGreg Roach            $id = $match[1];
1296a6f13a4aSGreg Roach        }
12977a6ee1acSGreg Roach        $tag = '';
1298a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1299a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1300a6f13a4aSGreg Roach        }
13017a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1302d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1303a6f13a4aSGreg Roach        }
1304a6f13a4aSGreg Roach
13056b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1306a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1307820b62dfSGreg Roach            $facts = $record->facts([], true);
130813abd6f3SGreg Roach            $this->repeats = [];
1309a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1310195b5e75SGreg Roach            foreach ($facts as $fact) {
13117fe676e5SGreg Roach                if (!in_array($fact->getTag(), $nonfacts, true)) {
1312195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1313a6f13a4aSGreg Roach                }
1314a6f13a4aSGreg Roach            }
1315a6f13a4aSGreg Roach        } else {
131630158ae7SGreg Roach            foreach ($record->facts() as $fact) {
13177fe676e5SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1318138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1319a6f13a4aSGreg Roach                }
1320a6f13a4aSGreg Roach            }
1321a6f13a4aSGreg Roach        }
1322a6f13a4aSGreg Roach    }
1323a6f13a4aSGreg Roach
1324a6f13a4aSGreg Roach    /**
1325fab8f067SGreg Roach     * Handle </facts>
13268ba2e626SGreg Roach     *
13278ba2e626SGreg Roach     * @return void
1328a6f13a4aSGreg Roach     */
1329b702978eSGreg Roach    protected function factsEndHandler(): void
1330c1010edaSGreg Roach    {
1331a6f13a4aSGreg Roach        $this->process_repeats--;
1332a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1333a6f13a4aSGreg Roach            return;
1334a6f13a4aSGreg Roach        }
1335a6f13a4aSGreg Roach
1336a6f13a4aSGreg Roach        // Check if there is anything to repeat
1337a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1338e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1339a6f13a4aSGreg Roach            $lineoffset = 0;
1340a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1341a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1342a6f13a4aSGreg Roach            }
1343a6f13a4aSGreg Roach
1344a6f13a4aSGreg Roach            //-- read the xml from the file
1345299d100dSGreg Roach            $lines = file($this->report);
1346dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1347a6f13a4aSGreg Roach                $lineoffset--;
1348a6f13a4aSGreg Roach            }
1349a6f13a4aSGreg Roach            $lineoffset++;
1350a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1351a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1352a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1353a6f13a4aSGreg Roach            while ($line_nr < $i) {
1354a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1355a6f13a4aSGreg Roach                $line_nr++;
1356a6f13a4aSGreg Roach            }
1357a6f13a4aSGreg Roach            // No need to drag this
1358a6f13a4aSGreg Roach            unset($lines);
1359a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1360a6f13a4aSGreg Roach            // Save original values
13619b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1362a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1363a6f13a4aSGreg Roach            $count                = count($this->repeats);
1364a6f13a4aSGreg Roach            $i                    = 0;
1365a6f13a4aSGreg Roach            while ($i < $count) {
1366a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1367a6f13a4aSGreg Roach                $this->fact   = '';
1368a6f13a4aSGreg Roach                $this->desc   = '';
1369a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1370a6f13a4aSGreg Roach                    $this->fact = $match[1];
1371a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
137213abd6f3SGreg Roach                        $tmatch = [];
1373a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1374a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1375a6f13a4aSGreg Roach                        } else {
1376a6f13a4aSGreg Roach                            $this->type = ' ';
1377a6f13a4aSGreg Roach                        }
1378a6f13a4aSGreg Roach                    }
1379a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13803d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1381a6f13a4aSGreg Roach                }
1382a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1383e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1384a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13851aa04befSGreg Roach
13861aa04befSGreg Roach                xml_set_element_handler(
13871aa04befSGreg Roach                    $repeat_parser,
13889d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13891aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13901aa04befSGreg Roach                    },
13919d454b6bSGreg Roach                    function ($parser, string $name): void {
13921aa04befSGreg Roach                        $this->endElement($parser, $name);
13931aa04befSGreg Roach                    }
13941aa04befSGreg Roach                );
13951aa04befSGreg Roach
13961aa04befSGreg Roach                xml_set_character_data_handler(
13971aa04befSGreg Roach                    $repeat_parser,
13989d454b6bSGreg Roach                    function ($parser, string $data): void {
13991aa04befSGreg Roach                        $this->characterData($parser, $data);
14001aa04befSGreg Roach                    }
14011aa04befSGreg Roach                );
14021aa04befSGreg Roach
1403a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14046ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1405a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1406a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1407a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1408a6f13a4aSGreg Roach                    ));
1409a6f13a4aSGreg Roach                }
1410a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1411a6f13a4aSGreg Roach                $i++;
1412a6f13a4aSGreg Roach            }
1413a6f13a4aSGreg Roach            // Restore original values
1414e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1415a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1416a6f13a4aSGreg Roach        }
141765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1418a6f13a4aSGreg Roach    }
1419a6f13a4aSGreg Roach
1420a6f13a4aSGreg Roach    /**
1421a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1422d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1423a6f13a4aSGreg Roach     *
1424c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14258ba2e626SGreg Roach     *
14268ba2e626SGreg Roach     * @return void
1427a6f13a4aSGreg Roach     */
1428b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1429c1010edaSGreg Roach    {
1430a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14316ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1432a6f13a4aSGreg Roach        }
1433a6f13a4aSGreg Roach
1434a6f13a4aSGreg Roach        $name  = $attrs['name'];
1435a6f13a4aSGreg Roach        $value = $attrs['value'];
143613abd6f3SGreg Roach        $match = [];
1437a6f13a4aSGreg Roach        // Current GEDCOM record strings
1438044416d2SGreg Roach        if ($value === '@ID') {
14397a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1440a6f13a4aSGreg Roach                $value = $match[1];
1441a6f13a4aSGreg Roach            }
1442044416d2SGreg Roach        } elseif ($value === '@fact') {
1443a6f13a4aSGreg Roach            $value = $this->fact;
1444044416d2SGreg Roach        } elseif ($value === '@desc') {
1445a6f13a4aSGreg Roach            $value = $this->desc;
1446044416d2SGreg Roach        } elseif ($value === '@generation') {
1447589feda3SGreg Roach            $value = (string) $this->generation;
1448a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144913abd6f3SGreg Roach            $gmatch = [];
1450a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14517a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1452a6f13a4aSGreg Roach            }
1453a6f13a4aSGreg Roach        }
1454a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1455d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1456a6f13a4aSGreg Roach        }
1457a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1458a6f13a4aSGreg Roach        $i     = 0;
1459a6f13a4aSGreg Roach        while ($i < $count) {
1460d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14617a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1462a6f13a4aSGreg Roach            $i++;
1463a6f13a4aSGreg Roach        }
1464a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1465da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1466a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1467a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1468a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1469a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1470a6f13a4aSGreg Roach        }
147152868398SGreg Roach
1472a6f13a4aSGreg Roach        // Arithmetic functions
14733cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
147452868398SGreg Roach            // Create an expression language with the functions used by our reports.
147552868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1476c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1477c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
147852868398SGreg Roach
147952868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1480a6f13a4aSGreg Roach        }
148152868398SGreg Roach
1482dec352c1SGreg Roach        if (str_contains($value, '@')) {
14837a6ee1acSGreg Roach            $value = '';
1484a6f13a4aSGreg Roach        }
1485d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1486a6f13a4aSGreg Roach    }
1487a6f13a4aSGreg Roach
1488a6f13a4aSGreg Roach    /**
1489fab8f067SGreg Roach     * Handle <if>
1490a6f13a4aSGreg Roach     *
1491fab8f067SGreg Roach     * @param string[] $attrs
14928ba2e626SGreg Roach     *
14938ba2e626SGreg Roach     * @return void
1494a6f13a4aSGreg Roach     */
1495b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1496c1010edaSGreg Roach    {
1497a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1498a6f13a4aSGreg Roach            $this->process_ifs++;
1499a6f13a4aSGreg Roach
1500a6f13a4aSGreg Roach            return;
1501a6f13a4aSGreg Roach        }
1502a6f13a4aSGreg Roach
1503a6f13a4aSGreg Roach        $condition = $attrs['condition'];
150482759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1505c1010edaSGreg Roach        $condition = str_replace([
1506c1010edaSGreg Roach            ' LT ',
1507c1010edaSGreg Roach            ' GT ',
1508c1010edaSGreg Roach        ], [
1509c1010edaSGreg Roach            '<',
1510c1010edaSGreg Roach            '>',
1511c1010edaSGreg Roach        ], $condition);
15123cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15137a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
151413abd6f3SGreg Roach        $match     = [];
15153cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1516a6f13a4aSGreg Roach        $i         = 0;
1517a6f13a4aSGreg Roach        while ($i < $count) {
1518a6f13a4aSGreg Roach            $id    = $match[$i][1];
1519a6f13a4aSGreg Roach            $value = '""';
1520044416d2SGreg Roach            if ($id === 'ID') {
15217a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1522a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1523a6f13a4aSGreg Roach                }
15247a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1525a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15267a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1527a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15287a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1529a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1530a6f13a4aSGreg Roach            } else {
1531b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1532b2448a1bSGreg Roach                if ($level === 0) {
1533a6f13a4aSGreg Roach                    $level++;
1534a6f13a4aSGreg Roach                }
15353d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1536a6f13a4aSGreg Roach                if (empty($value)) {
1537a6f13a4aSGreg Roach                    $level++;
15383d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1539a6f13a4aSGreg Roach                }
15408d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15415e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1542a6f13a4aSGreg Roach            }
1543a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1544a6f13a4aSGreg Roach            $i++;
1545a6f13a4aSGreg Roach        }
15465809450fSGreg Roach
1547cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1548cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1549c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1550c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1551cb63a60eSGreg Roach
1552cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15535809450fSGreg Roach
1554a6f13a4aSGreg Roach        if (!$ret) {
1555a6f13a4aSGreg Roach            $this->process_ifs++;
1556a6f13a4aSGreg Roach        }
1557a6f13a4aSGreg Roach    }
1558a6f13a4aSGreg Roach
1559a6f13a4aSGreg Roach    /**
1560fab8f067SGreg Roach     * Handle </if>
15618ba2e626SGreg Roach     *
15628ba2e626SGreg Roach     * @return void
1563a6f13a4aSGreg Roach     */
1564b702978eSGreg Roach    protected function ifEndHandler(): void
1565c1010edaSGreg Roach    {
1566a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1567a6f13a4aSGreg Roach            $this->process_ifs--;
1568a6f13a4aSGreg Roach        }
1569a6f13a4aSGreg Roach    }
1570a6f13a4aSGreg Roach
1571a6f13a4aSGreg Roach    /**
1572fab8f067SGreg Roach     * Handle <footnote>
1573a6f13a4aSGreg Roach     * Collect the Footnote links
1574fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1575a6f13a4aSGreg Roach     *
1576fab8f067SGreg Roach     * @param string[] $attrs
15778ba2e626SGreg Roach     *
15788ba2e626SGreg Roach     * @return void
1579a6f13a4aSGreg Roach     */
1580b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1581c1010edaSGreg Roach    {
15827a6ee1acSGreg Roach        $id = '';
15837a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1584a6f13a4aSGreg Roach            $id = $match[2];
1585a6f13a4aSGreg Roach        }
15866b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1587a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15889b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1589a6f13a4aSGreg Roach            $this->print_data         = true;
15907a6ee1acSGreg Roach            $style                    = '';
1591a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1592a6f13a4aSGreg Roach                $style = $attrs['style'];
1593a6f13a4aSGreg Roach            }
1594a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1595e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1596a6f13a4aSGreg Roach        } else {
1597a6f13a4aSGreg Roach            $this->print_data       = false;
1598a6f13a4aSGreg Roach            $this->process_footnote = false;
1599a6f13a4aSGreg Roach        }
1600a6f13a4aSGreg Roach    }
1601a6f13a4aSGreg Roach
1602a6f13a4aSGreg Roach    /**
1603fab8f067SGreg Roach     * Handle </footnote>
1604a6f13a4aSGreg Roach     * Print the collected Footnote data
16058ba2e626SGreg Roach     *
16068ba2e626SGreg Roach     * @return void
1607a6f13a4aSGreg Roach     */
1608b702978eSGreg Roach    protected function footnoteEndHandler(): void
1609c1010edaSGreg Roach    {
1610a6f13a4aSGreg Roach        if ($this->process_footnote) {
1611a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1612a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1613a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1614e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1615a6f13a4aSGreg Roach            }
1616a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1617a6f13a4aSGreg Roach        } else {
1618a6f13a4aSGreg Roach            $this->process_footnote = true;
1619a6f13a4aSGreg Roach        }
1620a6f13a4aSGreg Roach    }
1621a6f13a4aSGreg Roach
1622a6f13a4aSGreg Roach    /**
1623fab8f067SGreg Roach     * Handle <footnoteTexts />
16248ba2e626SGreg Roach     *
16258ba2e626SGreg Roach     * @return void
1626a6f13a4aSGreg Roach     */
1627b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1628c1010edaSGreg Roach    {
16297a6ee1acSGreg Roach        $temp = 'footnotetexts';
1630e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1631a6f13a4aSGreg Roach    }
1632a6f13a4aSGreg Roach
1633a6f13a4aSGreg Roach    /**
1634a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16358ba2e626SGreg Roach     *
16368ba2e626SGreg Roach     * @return void
1637a6f13a4aSGreg Roach     */
1638b702978eSGreg Roach    protected function brStartHandler(): void
1639c1010edaSGreg Roach    {
1640a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1641a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1642a6f13a4aSGreg Roach        }
1643a6f13a4aSGreg Roach    }
1644a6f13a4aSGreg Roach
1645a6f13a4aSGreg Roach    /**
1646fab8f067SGreg Roach     * Handle <sp />
1647fab8f067SGreg Roach     * Forced space
16488ba2e626SGreg Roach     *
16498ba2e626SGreg Roach     * @return void
1650a6f13a4aSGreg Roach     */
1651b702978eSGreg Roach    protected function spStartHandler(): void
1652c1010edaSGreg Roach    {
1653a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1654a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1655a6f13a4aSGreg Roach        }
1656a6f13a4aSGreg Roach    }
1657a6f13a4aSGreg Roach
1658a6f13a4aSGreg Roach    /**
1659fab8f067SGreg Roach     * Handle <highlightedImage />
166076692c8bSGreg Roach     *
1661fab8f067SGreg Roach     * @param string[] $attrs
16628ba2e626SGreg Roach     *
16638ba2e626SGreg Roach     * @return void
1664a6f13a4aSGreg Roach     */
1665b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1666c1010edaSGreg Roach    {
1667a6f13a4aSGreg Roach        $id = '';
16687a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1669a6f13a4aSGreg Roach            $id = $match[1];
1670a6f13a4aSGreg Roach        }
1671a6f13a4aSGreg Roach
1672c21bdddcSGreg Roach        // Position the top corner of this box on the page
1673c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1674a6f13a4aSGreg Roach
1675c21bdddcSGreg Roach        // Position the left corner of this box on the page
1676c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1677a6f13a4aSGreg Roach
167883cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167983cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1680a6f13a4aSGreg Roach
1681a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
168283cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1683a6f13a4aSGreg Roach
168483cdc021SGreg Roach        // Width, height (or both).
1685c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1686c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1687a6f13a4aSGreg Roach
16886b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
16894a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
169086a63f51SGreg Roach
1691a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1692a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1693b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
169429518ad2SGreg Roach
1695a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16963c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16973c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1698a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16993c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17003c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1701a6f13a4aSGreg Roach            } else {
17023c3b90deSGreg Roach                $width  = $attributes[0];
17033c3b90deSGreg Roach                $height = $attributes[1];
1704a6f13a4aSGreg Roach            }
1705a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1706e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1707a6f13a4aSGreg Roach        }
1708a6f13a4aSGreg Roach    }
1709a6f13a4aSGreg Roach
1710a6f13a4aSGreg Roach    /**
1711fab8f067SGreg Roach     * Handle <image/>
171276692c8bSGreg Roach     *
1713fab8f067SGreg Roach     * @param string[] $attrs
17148ba2e626SGreg Roach     *
17158ba2e626SGreg Roach     * @return void
1716a6f13a4aSGreg Roach     */
1717b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1718c1010edaSGreg Roach    {
171983cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1720c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1721a6f13a4aSGreg Roach
1722a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1723c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1724a6f13a4aSGreg Roach
172583cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172683cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1727a6f13a4aSGreg Roach
1728a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172983cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1730a6f13a4aSGreg Roach
173183cdc021SGreg Roach        // Width, height (or both).
1732c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1733c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1734a6f13a4aSGreg Roach
173583cdc021SGreg Roach        $file = $attrs['file'] ?? '';
173683cdc021SGreg Roach
1737044416d2SGreg Roach        if ($file === '@FILE') {
173813abd6f3SGreg Roach            $match = [];
1739a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17406b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17414a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1742cdf416fbSGreg Roach
1743a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1744a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1745b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
174629518ad2SGreg Roach
1747a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17483c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17493c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1750a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17513c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17523c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1753a6f13a4aSGreg Roach                    } else {
17543c3b90deSGreg Roach                        $width  = $attributes[0];
17553c3b90deSGreg Roach                        $height = $attributes[1];
1756a6f13a4aSGreg Roach                    }
1757a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1758e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1759a6f13a4aSGreg Roach                }
1760a6f13a4aSGreg Roach            }
1761a6f13a4aSGreg Roach        } else {
17627a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1763a6f13a4aSGreg Roach                $size = getimagesize($file);
1764a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1765a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1766a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1767a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1768a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1769a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1770a6f13a4aSGreg Roach                } else {
1771a6f13a4aSGreg Roach                    $width  = $size[0];
1772a6f13a4aSGreg Roach                    $height = $size[1];
1773a6f13a4aSGreg Roach                }
1774e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1775e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1776a6f13a4aSGreg Roach            }
1777a6f13a4aSGreg Roach        }
1778a6f13a4aSGreg Roach    }
1779a6f13a4aSGreg Roach
1780a6f13a4aSGreg Roach    /**
1781fab8f067SGreg Roach     * Handle <line>
1782a6f13a4aSGreg Roach     *
1783fab8f067SGreg Roach     * @param string[] $attrs
17848ba2e626SGreg Roach     *
17858ba2e626SGreg Roach     * @return void
1786a6f13a4aSGreg Roach     */
1787b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1788c1010edaSGreg Roach    {
1789a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1790c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1791a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17927a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1793a6f13a4aSGreg Roach                $x1 = 0;
17947a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1795c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1796a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1797c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1798a6f13a4aSGreg Roach            }
1799a6f13a4aSGreg Roach        }
1800a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1801c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1802a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18037a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1804a6f13a4aSGreg Roach                $y1 = 0;
18057a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1806c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1807a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1808c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1809a6f13a4aSGreg Roach            }
1810a6f13a4aSGreg Roach        }
1811a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1812c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1813a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18147a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1815a6f13a4aSGreg Roach                $x2 = 0;
18167a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1817c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1818a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1819c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1820a6f13a4aSGreg Roach            }
1821a6f13a4aSGreg Roach        }
1822a6f13a4aSGreg Roach        // End vertical position
1823c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1824a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18257a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1826a6f13a4aSGreg Roach                $y2 = 0;
18277a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1828c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1829a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1830c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1831a6f13a4aSGreg Roach            }
1832a6f13a4aSGreg Roach        }
1833a6f13a4aSGreg Roach
1834e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1835e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1836a6f13a4aSGreg Roach    }
1837a6f13a4aSGreg Roach
1838a6f13a4aSGreg Roach    /**
1839fab8f067SGreg Roach     * Handle <list>
1840a6f13a4aSGreg Roach     *
1841fab8f067SGreg Roach     * @param string[] $attrs
18428ba2e626SGreg Roach     *
18438ba2e626SGreg Roach     * @return void
1844a6f13a4aSGreg Roach     */
1845b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1846c1010edaSGreg Roach    {
1847a6f13a4aSGreg Roach        $this->process_repeats++;
1848a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1849a6f13a4aSGreg Roach            return;
1850a6f13a4aSGreg Roach        }
1851a6f13a4aSGreg Roach
185213abd6f3SGreg Roach        $match = [];
1853a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1854a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1855a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1856d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1857a6f13a4aSGreg Roach                $sortby = trim($sortby);
1858a6f13a4aSGreg Roach            }
1859a6f13a4aSGreg Roach        } else {
18607a6ee1acSGreg Roach            $sortby = 'NAME';
1861a6f13a4aSGreg Roach        }
1862a6f13a4aSGreg Roach
1863e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1864195b5e75SGreg Roach
1865a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1866a6f13a4aSGreg Roach        switch ($listname) {
18677a6ee1acSGreg Roach            case 'pending':
18686c0bef3aSGreg Roach                $xrefs = DB::table('change')
1869195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1870a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1871195b5e75SGreg Roach                            ->from('change')
1872195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1873195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18747f5c2944SGreg Roach                            ->groupBy(['xref']);
1875195b5e75SGreg Roach                    })
18766c0bef3aSGreg Roach                    ->pluck('xref');
1877195b5e75SGreg Roach
187813abd6f3SGreg Roach                $this->list = [];
18796c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18806b9cb339SGreg Roach                    $this->list[] = Registry::gedcomRecordFactory()->make($xref, $this->tree);
1881a6f13a4aSGreg Roach                }
1882a6f13a4aSGreg Roach                break;
1883a6f13a4aSGreg Roach            case 'individual':
18845985adfbSGreg Roach                $query = DB::table('individuals')
18855985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18865985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18875985adfbSGreg Roach                    ->distinct();
18885985adfbSGreg Roach
1889a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1890dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
189182759250SGreg Roach                        $value = $this->substituteVars($value, false);
1892a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1893a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
18940b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
18955985adfbSGreg Roach                                $join
18965985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
18975985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
18985985adfbSGreg Roach                            });
18995985adfbSGreg Roach
19005985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19015985adfbSGreg Roach
1902a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19035985adfbSGreg Roach
1904044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19055985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1906a6f13a4aSGreg Roach                            } else {
19075985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1908a6f13a4aSGreg Roach                            }
19095985adfbSGreg Roach
19105985adfbSGreg Roach                            // This filter has been fully processed
19115985adfbSGreg Roach                            unset($attrs[$attr]);
19127ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19130b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19145985adfbSGreg Roach                                $join
19155985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19165985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19175985adfbSGreg Roach                            });
1918a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19197a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19205d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1921b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1922a6f13a4aSGreg Roach                            }
19235985adfbSGreg Roach
19245985adfbSGreg Roach                            // This filter has been fully processed
19255985adfbSGreg Roach                            unset($attrs[$attr]);
1926a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19275985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1928a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19295985adfbSGreg Roach
1930a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19315985adfbSGreg Roach
19325985adfbSGreg Roach                            // This filter has been fully processed
19335985adfbSGreg Roach                            unset($attrs[$attr]);
19342fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19359dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19365985adfbSGreg Roach                            $query
19370b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19385985adfbSGreg Roach                                    $join
19397ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19407ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19415985adfbSGreg Roach                                })
19420b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19437ee4bfadSGreg Roach                                    $join
19447ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19457ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19467ee4bfadSGreg Roach                                })
19479b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19486e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19495985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19507ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19516e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19526e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
195347a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19546e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19556e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1956e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19577ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1958a6f13a4aSGreg Roach                        }
1959a6f13a4aSGreg Roach                    }
1960a6f13a4aSGreg Roach                }
1961a6f13a4aSGreg Roach
196213abd6f3SGreg Roach                $this->list = [];
1963a6f13a4aSGreg Roach
19645985adfbSGreg Roach                foreach ($query->get() as $row) {
19656b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1966a6f13a4aSGreg Roach                }
1967a6f13a4aSGreg Roach                break;
1968a6f13a4aSGreg Roach
1969a6f13a4aSGreg Roach            case 'family':
197030fc2b1eSGreg Roach                $query = DB::table('families')
197130fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
197230fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
197330fc2b1eSGreg Roach                    ->distinct();
197430fc2b1eSGreg Roach
1975a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1976dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
197782759250SGreg Roach                        $value = $this->substituteVars($value, false);
1978a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1979a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19800b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
198130fc2b1eSGreg Roach                                $join
198230fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
198330fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
198430fc2b1eSGreg Roach                            });
198530fc2b1eSGreg Roach
198630fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
198730fc2b1eSGreg Roach
1988a6f13a4aSGreg Roach                            $date = new Date($match[3]);
198930fc2b1eSGreg Roach
1990044416d2SGreg Roach                            if ($match[2] === 'LTE') {
199130fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1992a6f13a4aSGreg Roach                            } else {
199330fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1994a6f13a4aSGreg Roach                            }
199530fc2b1eSGreg Roach
199630fc2b1eSGreg Roach                            // This filter has been fully processed
199730fc2b1eSGreg Roach                            unset($attrs[$attr]);
1998a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
199930fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2000a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
200130fc2b1eSGreg Roach
2002a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
200330fc2b1eSGreg Roach
200430fc2b1eSGreg Roach                            // This filter has been fully processed
200530fc2b1eSGreg Roach                            unset($attrs[$attr]);
200630fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20073b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20080b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200930fc2b1eSGreg Roach                                    $join
201030fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20113b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
201230fc2b1eSGreg Roach                                            $query
201330fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
201430fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
201530fc2b1eSGreg Roach                                        });
201630fc2b1eSGreg Roach                                });
20175d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20187a6ee1acSGreg Roach                                if ($match[1] != '') {
20197a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20205d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2021b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20225d0bc43dSGreg Roach                                    }
20235d0bc43dSGreg Roach                                }
2024a6f13a4aSGreg Roach                            }
202530fc2b1eSGreg Roach
202630fc2b1eSGreg Roach                            // This filter has been fully processed
202730fc2b1eSGreg Roach                            unset($attrs[$attr]);
20286e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20299dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
203030fc2b1eSGreg Roach                            $query
20310b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
203230fc2b1eSGreg Roach                                    $join
20336e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20346e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
203530fc2b1eSGreg Roach                                })
20360b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20376e5c0963SGreg Roach                                    $join
20386e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20396e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20406e5c0963SGreg Roach                                })
2041b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20426e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
204330fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20446e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20456e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20466e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20476e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20486e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20496e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2050e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20516e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2052a6f13a4aSGreg Roach                        }
2053a6f13a4aSGreg Roach                    }
2054a6f13a4aSGreg Roach                }
2055a6f13a4aSGreg Roach
205613abd6f3SGreg Roach                $this->list = [];
2057a6f13a4aSGreg Roach
205830fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20596b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2060a6f13a4aSGreg Roach                }
2061a6f13a4aSGreg Roach                break;
2062a6f13a4aSGreg Roach
2063a6f13a4aSGreg Roach            default:
20646ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2065a6f13a4aSGreg Roach        }
2066a6f13a4aSGreg Roach
206713abd6f3SGreg Roach        $filters  = [];
206813abd6f3SGreg Roach        $filters2 = [];
2069a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2070a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2071a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2072a6f13a4aSGreg Roach                    $condition = $value;
2073a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2074a6f13a4aSGreg Roach                        $id    = $match[1];
2075a6f13a4aSGreg Roach                        $value = "''";
2076044416d2SGreg Roach                        if ($id === 'ID') {
20777a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2078a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2079a6f13a4aSGreg Roach                            }
2080044416d2SGreg Roach                        } elseif ($id === 'fact') {
2081a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2082044416d2SGreg Roach                        } elseif ($id === 'desc') {
2083a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2084a6f13a4aSGreg Roach                        } else {
2085a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20867a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2087a6f13a4aSGreg Roach                            }
2088a6f13a4aSGreg Roach                        }
2089a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2090a6f13a4aSGreg Roach                    }
2091a6f13a4aSGreg Roach                    //-- handle regular expressions
2092a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2093a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2094a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2095a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2096a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2097d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2098a6f13a4aSGreg Roach                            $val = trim($val);
2099a6f13a4aSGreg Roach                        }
2100a6f13a4aSGreg Roach                        if ($val) {
21017a6ee1acSGreg Roach                            $searchstr = '';
21027a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2103a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2104a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2105a6f13a4aSGreg Roach                                $level = 1;
2106f71a7dedSGreg Roach                                $t = 'XXXX';
2107a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2108a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2109a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2110a6f13a4aSGreg Roach                                    }
2111a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2112044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21137a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2114a6f13a4aSGreg Roach                                    }
21157a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2116a6f13a4aSGreg Roach                                    $level++;
2117a6f13a4aSGreg Roach                                }
2118a6f13a4aSGreg Roach                            } else {
2119044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21207a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2121a6f13a4aSGreg Roach                                }
2122a6f13a4aSGreg Roach                                $t         = $tag;
21237a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2124a6f13a4aSGreg Roach                            }
2125a6f13a4aSGreg Roach                            switch ($expr) {
21267a6ee1acSGreg Roach                                case 'CONTAINS':
2127044416d2SGreg Roach                                    if ($t === 'PLAC') {
2128a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2129a6f13a4aSGreg Roach                                    } else {
2130a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2131a6f13a4aSGreg Roach                                    }
2132a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2133a6f13a4aSGreg Roach                                    break;
2134a6f13a4aSGreg Roach                                default:
2135c1010edaSGreg Roach                                    $filters2[] = [
2136c1010edaSGreg Roach                                        'tag'  => $tag,
2137c1010edaSGreg Roach                                        'expr' => $expr,
2138c1010edaSGreg Roach                                        'val'  => $val,
2139c1010edaSGreg Roach                                    ];
2140a6f13a4aSGreg Roach                                    break;
2141a6f13a4aSGreg Roach                            }
2142a6f13a4aSGreg Roach                        }
2143a6f13a4aSGreg Roach                    }
2144a6f13a4aSGreg Roach                }
2145a6f13a4aSGreg Roach            }
2146a6f13a4aSGreg Roach        }
2147a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2148a6f13a4aSGreg Roach        if ($filters) {
2149a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2150a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2151299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2152a6f13a4aSGreg Roach                        unset($this->list[$key]);
2153a6f13a4aSGreg Roach                        break;
2154a6f13a4aSGreg Roach                    }
2155a6f13a4aSGreg Roach                }
2156a6f13a4aSGreg Roach            }
2157a6f13a4aSGreg Roach        }
2158a6f13a4aSGreg Roach        if ($filters2) {
215913abd6f3SGreg Roach            $mylist = [];
2160a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2161c0935879SGreg Roach                $key  = $indi->xref();
2162299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2163a6f13a4aSGreg Roach                $keep = true;
2164a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2165a6f13a4aSGreg Roach                    if ($keep) {
2166a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2167a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2168a6f13a4aSGreg Roach                        $val  = $filter['val'];
2169b2448a1bSGreg Roach                        if ($val === "''") {
21707a6ee1acSGreg Roach                            $val = '';
2171a6f13a4aSGreg Roach                        }
21727a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2173a6f13a4aSGreg Roach                        $t    = end($tags);
21743d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2175a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2176044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21777a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21787a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2179a6f13a4aSGreg Roach                            $t    = end($tags);
21803d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2181a6f13a4aSGreg Roach                        }
2182a6f13a4aSGreg Roach
2183a6f13a4aSGreg Roach                        switch ($expr) {
21847a6ee1acSGreg Roach                            case 'GTE':
2185044416d2SGreg Roach                                if ($t === 'DATE') {
2186a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2187a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2188a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2189a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2190a6f13a4aSGreg Roach                                    $keep = true;
2191a6f13a4aSGreg Roach                                }
2192a6f13a4aSGreg Roach                                break;
21937a6ee1acSGreg Roach                            case 'LTE':
2194044416d2SGreg Roach                                if ($t === 'DATE') {
2195a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2196a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2197a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2198a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2199a6f13a4aSGreg Roach                                    $keep = true;
2200a6f13a4aSGreg Roach                                }
2201a6f13a4aSGreg Roach                                break;
2202a6f13a4aSGreg Roach                            default:
2203a6f13a4aSGreg Roach                                if ($v == $val) {
2204a6f13a4aSGreg Roach                                    $keep = true;
2205a6f13a4aSGreg Roach                                } else {
2206a6f13a4aSGreg Roach                                    $keep = false;
2207a6f13a4aSGreg Roach                                }
2208a6f13a4aSGreg Roach                                break;
2209a6f13a4aSGreg Roach                        }
2210a6f13a4aSGreg Roach                    }
2211a6f13a4aSGreg Roach                }
2212a6f13a4aSGreg Roach                if ($keep) {
2213a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2214a6f13a4aSGreg Roach                }
2215a6f13a4aSGreg Roach            }
2216a6f13a4aSGreg Roach            $this->list = $mylist;
2217a6f13a4aSGreg Roach        }
2218a6f13a4aSGreg Roach
2219a6f13a4aSGreg Roach        switch ($sortby) {
2220a6f13a4aSGreg Roach            case 'NAME':
2221c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2222a6f13a4aSGreg Roach                break;
2223a6f13a4aSGreg Roach            case 'CHAN':
2224c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2225a6f13a4aSGreg Roach                break;
2226a6f13a4aSGreg Roach            case 'BIRT:DATE':
2227c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2228a6f13a4aSGreg Roach                break;
2229a6f13a4aSGreg Roach            case 'DEAT:DATE':
2230c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2231a6f13a4aSGreg Roach                break;
2232a6f13a4aSGreg Roach            case 'MARR:DATE':
2233c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2234a6f13a4aSGreg Roach                break;
2235a6f13a4aSGreg Roach            default:
2236a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2237a6f13a4aSGreg Roach                break;
2238a6f13a4aSGreg Roach        }
2239a6f13a4aSGreg Roach
22409b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2241e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2242a6f13a4aSGreg Roach    }
2243a6f13a4aSGreg Roach
2244a6f13a4aSGreg Roach    /**
2245fab8f067SGreg Roach     * Handle </list>
22468ba2e626SGreg Roach     *
22478ba2e626SGreg Roach     * @return void
2248a6f13a4aSGreg Roach     */
2249b702978eSGreg Roach    protected function listEndHandler(): void
2250c1010edaSGreg Roach    {
2251a6f13a4aSGreg Roach        $this->process_repeats--;
2252a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2253a6f13a4aSGreg Roach            return;
2254a6f13a4aSGreg Roach        }
2255a6f13a4aSGreg Roach
2256a6f13a4aSGreg Roach        // Check if there is any list
2257a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2258a6f13a4aSGreg Roach            $lineoffset = 0;
2259a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2260a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2261a6f13a4aSGreg Roach            }
2262a6f13a4aSGreg Roach            //-- read the xml from the file
2263299d100dSGreg Roach            $lines = file($this->report);
2264dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2265a6f13a4aSGreg Roach                $lineoffset--;
2266a6f13a4aSGreg Roach            }
2267a6f13a4aSGreg Roach            $lineoffset++;
2268a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2269a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2270a6f13a4aSGreg Roach            // List Level counter
2271a6f13a4aSGreg Roach            $count = 1;
2272a6f13a4aSGreg Roach            while (0 < $count) {
2273dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2274a6f13a4aSGreg Roach                    $count++;
2275dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2276a6f13a4aSGreg Roach                    $count--;
2277a6f13a4aSGreg Roach                }
2278a6f13a4aSGreg Roach                if (0 < $count) {
2279a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2280a6f13a4aSGreg Roach                }
2281a6f13a4aSGreg Roach                $line_nr++;
2282a6f13a4aSGreg Roach            }
2283a6f13a4aSGreg Roach            // No need to drag this
2284a6f13a4aSGreg Roach            unset($lines);
22857a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2286a6f13a4aSGreg Roach            // Save original values
22879b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2288a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2289a6f13a4aSGreg Roach
2290a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2291a6f13a4aSGreg Roach            $this->list_private = 0;
2292a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2293a6f13a4aSGreg Roach                if ($record->canShow()) {
2294f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2295a6f13a4aSGreg Roach                    //-- start the sax parser
2296a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2297e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2298a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22991aa04befSGreg Roach
23001aa04befSGreg Roach                    xml_set_element_handler(
23011aa04befSGreg Roach                        $repeat_parser,
23029d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23031aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23041aa04befSGreg Roach                        },
23059d454b6bSGreg Roach                        function ($parser, string $name): void {
23061aa04befSGreg Roach                            $this->endElement($parser, $name);
23071aa04befSGreg Roach                        }
23081aa04befSGreg Roach                    );
23091aa04befSGreg Roach
23101aa04befSGreg Roach                    xml_set_character_data_handler(
23111aa04befSGreg Roach                        $repeat_parser,
23129d454b6bSGreg Roach                        function ($parser, string $data): void {
23131aa04befSGreg Roach                            $this->characterData($parser, $data);
23141aa04befSGreg Roach                        }
23151aa04befSGreg Roach                    );
23161aa04befSGreg Roach
2317a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23186ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2319a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2320a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2321a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2322a6f13a4aSGreg Roach                        ));
2323a6f13a4aSGreg Roach                    }
2324a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2325a6f13a4aSGreg Roach                } else {
2326a6f13a4aSGreg Roach                    $this->list_private++;
2327a6f13a4aSGreg Roach                }
2328a6f13a4aSGreg Roach            }
232913abd6f3SGreg Roach            $this->list   = [];
2330e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2331a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2332a6f13a4aSGreg Roach        }
233365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2334a6f13a4aSGreg Roach    }
2335a6f13a4aSGreg Roach
2336a6f13a4aSGreg Roach    /**
2337fab8f067SGreg Roach     * Handle <listTotal>
2338a6f13a4aSGreg Roach     * Prints the total number of records in a list
2339fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23408ba2e626SGreg Roach     *
23418ba2e626SGreg Roach     * @return void
2342a6f13a4aSGreg Roach     */
2343b702978eSGreg Roach    protected function listTotalStartHandler(): void
2344c1010edaSGreg Roach    {
2345a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2346589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2347a6f13a4aSGreg Roach        } else {
23487a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2349a6f13a4aSGreg Roach        }
2350a6f13a4aSGreg Roach    }
2351a6f13a4aSGreg Roach
2352a6f13a4aSGreg Roach    /**
2353fab8f067SGreg Roach     * Handle <relatives>
235476692c8bSGreg Roach     *
2355fab8f067SGreg Roach     * @param string[] $attrs
23568ba2e626SGreg Roach     *
23578ba2e626SGreg Roach     * @return void
2358a6f13a4aSGreg Roach     */
2359b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2360c1010edaSGreg Roach    {
2361a6f13a4aSGreg Roach        $this->process_repeats++;
2362a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2363a6f13a4aSGreg Roach            return;
2364a6f13a4aSGreg Roach        }
2365a6f13a4aSGreg Roach
2366e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2367e364afe4SGreg Roach
236813abd6f3SGreg Roach        $match = [];
2369a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2370d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2371a6f13a4aSGreg Roach            $sortby = trim($sortby);
2372a6f13a4aSGreg Roach        }
2373a6f13a4aSGreg Roach
2374a6f13a4aSGreg Roach        $maxgen = -1;
2375a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2376c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2377a6f13a4aSGreg Roach        }
2378a6f13a4aSGreg Roach
2379e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2380e364afe4SGreg Roach
2381a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2382d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2383a6f13a4aSGreg Roach            $group = trim($group);
2384a6f13a4aSGreg Roach        }
2385a6f13a4aSGreg Roach
2386e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2387e364afe4SGreg Roach
2388a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2389d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2390a6f13a4aSGreg Roach            $id = trim($id);
2391a6f13a4aSGreg Roach        }
2392a6f13a4aSGreg Roach
239313abd6f3SGreg Roach        $this->list = [];
23946b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2395d965cc1aSGreg Roach        if ($person instanceof Individual) {
2396a6f13a4aSGreg Roach            $this->list[$id] = $person;
2397a6f13a4aSGreg Roach            switch ($group) {
23987a6ee1acSGreg Roach                case 'child-family':
239939ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2400820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2401820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2402a6f13a4aSGreg Roach                        }
2403820b62dfSGreg Roach
2404820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2405c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2406a6f13a4aSGreg Roach                        }
2407a6f13a4aSGreg Roach                    }
2408a6f13a4aSGreg Roach                    break;
24097a6ee1acSGreg Roach                case 'spouse-family':
241039ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2411820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2412820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2413a6f13a4aSGreg Roach                        }
2414820b62dfSGreg Roach
2415820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2416c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2417a6f13a4aSGreg Roach                        }
2418a6f13a4aSGreg Roach                    }
2419a6f13a4aSGreg Roach                    break;
24207a6ee1acSGreg Roach                case 'direct-ancestors':
24213d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2422a6f13a4aSGreg Roach                    break;
24237a6ee1acSGreg Roach                case 'ancestors':
24243d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2425a6f13a4aSGreg Roach                    break;
24267a6ee1acSGreg Roach                case 'descendants':
2427a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24283d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2429a6f13a4aSGreg Roach                    break;
24307a6ee1acSGreg Roach                case 'all':
24313d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24323d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2433a6f13a4aSGreg Roach                    break;
2434a6f13a4aSGreg Roach            }
2435a6f13a4aSGreg Roach        }
2436a6f13a4aSGreg Roach
2437a6f13a4aSGreg Roach        switch ($sortby) {
2438a6f13a4aSGreg Roach            case 'NAME':
2439c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2440a6f13a4aSGreg Roach                break;
2441a6f13a4aSGreg Roach            case 'BIRT:DATE':
2442c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2443a6f13a4aSGreg Roach                break;
2444a6f13a4aSGreg Roach            case 'DEAT:DATE':
2445c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2446a6f13a4aSGreg Roach                break;
2447a6f13a4aSGreg Roach            case 'generation':
244813abd6f3SGreg Roach                $newarray = [];
2449a6f13a4aSGreg Roach                reset($this->list);
2450a6f13a4aSGreg Roach                $genCounter = 1;
2451a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2452a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2453a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2454a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
245579529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2456a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2457a6f13a4aSGreg Roach                        }
2458a6f13a4aSGreg Roach                    }
2459a6f13a4aSGreg Roach                    $genCounter++;
2460a6f13a4aSGreg Roach                }
2461a6f13a4aSGreg Roach                $this->list = $newarray;
2462a6f13a4aSGreg Roach                break;
2463a6f13a4aSGreg Roach            default:
2464a6f13a4aSGreg Roach                // unsorted
2465a6f13a4aSGreg Roach                break;
2466a6f13a4aSGreg Roach        }
24679b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2468e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2469a6f13a4aSGreg Roach    }
2470a6f13a4aSGreg Roach
2471a6f13a4aSGreg Roach    /**
2472fab8f067SGreg Roach     * Handle </relatives>
24738ba2e626SGreg Roach     *
24748ba2e626SGreg Roach     * @return void
2475a6f13a4aSGreg Roach     */
2476b702978eSGreg Roach    protected function relativesEndHandler(): void
2477c1010edaSGreg Roach    {
2478a6f13a4aSGreg Roach        $this->process_repeats--;
2479a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2480a6f13a4aSGreg Roach            return;
2481a6f13a4aSGreg Roach        }
2482a6f13a4aSGreg Roach
2483a6f13a4aSGreg Roach        // Check if there is any relatives
2484a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2485a6f13a4aSGreg Roach            $lineoffset = 0;
2486a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2487a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2488a6f13a4aSGreg Roach            }
2489a6f13a4aSGreg Roach            //-- read the xml from the file
2490299d100dSGreg Roach            $lines = file($this->report);
2491dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2492a6f13a4aSGreg Roach                $lineoffset--;
2493a6f13a4aSGreg Roach            }
2494a6f13a4aSGreg Roach            $lineoffset++;
2495a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2496a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2497a6f13a4aSGreg Roach            // Relatives Level counter
2498a6f13a4aSGreg Roach            $count = 1;
2499a6f13a4aSGreg Roach            while (0 < $count) {
2500dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2501a6f13a4aSGreg Roach                    $count++;
2502dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2503a6f13a4aSGreg Roach                    $count--;
2504a6f13a4aSGreg Roach                }
2505a6f13a4aSGreg Roach                if (0 < $count) {
2506a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2507a6f13a4aSGreg Roach                }
2508a6f13a4aSGreg Roach                $line_nr++;
2509a6f13a4aSGreg Roach            }
2510a6f13a4aSGreg Roach            // No need to drag this
2511a6f13a4aSGreg Roach            unset($lines);
2512a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2513a6f13a4aSGreg Roach            // Save original values
25149b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2515a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2516a6f13a4aSGreg Roach
2517a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2518a6f13a4aSGreg Roach            $this->list_private = 0;
2519b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2520a6f13a4aSGreg Roach                if (isset($value->generation)) {
2521a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2522a6f13a4aSGreg Roach                }
25236b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2524299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2525a6f13a4aSGreg Roach
2526a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2527e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2528a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25291aa04befSGreg Roach
25301aa04befSGreg Roach                xml_set_element_handler(
25311aa04befSGreg Roach                    $repeat_parser,
25329d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25331aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25341aa04befSGreg Roach                    },
25359d454b6bSGreg Roach                    function ($parser, string $name): void {
25361aa04befSGreg Roach                        $this->endElement($parser, $name);
25371aa04befSGreg Roach                    }
25381aa04befSGreg Roach                );
25391aa04befSGreg Roach
25401aa04befSGreg Roach                xml_set_character_data_handler(
25411aa04befSGreg Roach                    $repeat_parser,
25429d454b6bSGreg Roach                    function ($parser, string $data): void {
25431aa04befSGreg Roach                        $this->characterData($parser, $data);
25441aa04befSGreg Roach                    }
25451aa04befSGreg Roach                );
2546a6f13a4aSGreg Roach
2547a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25486ccdf4f0SGreg 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)));
2549a6f13a4aSGreg Roach                }
2550a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2551a6f13a4aSGreg Roach            }
2552a6f13a4aSGreg Roach            // Clean up the list array
255313abd6f3SGreg Roach            $this->list   = [];
2554e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2555a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2556a6f13a4aSGreg Roach        }
255765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2558a6f13a4aSGreg Roach    }
2559a6f13a4aSGreg Roach
2560a6f13a4aSGreg Roach    /**
2561fab8f067SGreg Roach     * Handle <generation />
2562a6f13a4aSGreg Roach     * Prints the number of generations
25638ba2e626SGreg Roach     *
25648ba2e626SGreg Roach     * @return void
2565a6f13a4aSGreg Roach     */
2566b702978eSGreg Roach    protected function generationStartHandler(): void
2567c1010edaSGreg Roach    {
2568589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2569a6f13a4aSGreg Roach    }
2570a6f13a4aSGreg Roach
2571a6f13a4aSGreg Roach    /**
2572fab8f067SGreg Roach     * Handle <newPage />
2573a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
25748ba2e626SGreg Roach     *
25758ba2e626SGreg Roach     * @return void
2576a6f13a4aSGreg Roach     */
2577b702978eSGreg Roach    protected function newPageStartHandler(): void
2578c1010edaSGreg Roach    {
25797a6ee1acSGreg Roach        $temp = 'addpage';
2580e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2581a6f13a4aSGreg Roach    }
2582a6f13a4aSGreg Roach
2583a6f13a4aSGreg Roach    /**
2584fab8f067SGreg Roach     * Handle </title>
25858ba2e626SGreg Roach     *
25868ba2e626SGreg Roach     * @return void
2587a6f13a4aSGreg Roach     */
2588b702978eSGreg Roach    protected function titleEndHandler(): void
2589c1010edaSGreg Roach    {
25902836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2591a6f13a4aSGreg Roach    }
2592a6f13a4aSGreg Roach
2593a6f13a4aSGreg Roach    /**
2594fab8f067SGreg Roach     * Handle </description>
25958ba2e626SGreg Roach     *
25968ba2e626SGreg Roach     * @return void
2597a6f13a4aSGreg Roach     */
2598b702978eSGreg Roach    protected function descriptionEndHandler(): void
2599c1010edaSGreg Roach    {
26002836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2601a6f13a4aSGreg Roach    }
2602729ce104SGreg Roach
2603729ce104SGreg Roach    /**
260476692c8bSGreg Roach     * Create a list of all descendants.
260576692c8bSGreg Roach     *
2606729ce104SGreg Roach     * @param string[] $list
2607729ce104SGreg Roach     * @param string   $pid
2608729ce104SGreg Roach     * @param bool     $parents
2609729ce104SGreg Roach     * @param int      $generations
26108ba2e626SGreg Roach     *
26118ba2e626SGreg Roach     * @return void
2612729ce104SGreg Roach     */
26133b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2614c1010edaSGreg Roach    {
26156b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2616729ce104SGreg Roach        if ($person === null) {
2617729ce104SGreg Roach            return;
2618729ce104SGreg Roach        }
2619729ce104SGreg Roach        if (!isset($list[$pid])) {
2620729ce104SGreg Roach            $list[$pid] = $person;
2621729ce104SGreg Roach        }
2622729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2623729ce104SGreg Roach            $list[$pid]->generation = 0;
2624729ce104SGreg Roach        }
262539ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2626729ce104SGreg Roach            if ($parents) {
262739ca88baSGreg Roach                $husband = $family->husband();
262839ca88baSGreg Roach                $wife    = $family->wife();
2629729ce104SGreg Roach                if ($husband) {
2630c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2631729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2632c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2633729ce104SGreg Roach                    } else {
2634c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2635729ce104SGreg Roach                    }
2636729ce104SGreg Roach                }
2637729ce104SGreg Roach                if ($wife) {
2638c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2639729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2640c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2641729ce104SGreg Roach                    } else {
2642c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2643729ce104SGreg Roach                    }
2644729ce104SGreg Roach                }
2645729ce104SGreg Roach            }
2646820b62dfSGreg Roach
264739ca88baSGreg Roach            $children = $family->children();
2648820b62dfSGreg Roach
2649729ce104SGreg Roach            foreach ($children as $child) {
2650729ce104SGreg Roach                if ($child) {
2651c0935879SGreg Roach                    $list[$child->xref()] = $child;
2652820b62dfSGreg Roach
2653729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2654c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2655729ce104SGreg Roach                    } else {
2656c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2657729ce104SGreg Roach                    }
2658729ce104SGreg Roach                }
2659729ce104SGreg Roach            }
2660729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2661729ce104SGreg Roach                foreach ($children as $child) {
2662c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2663729ce104SGreg Roach                }
2664729ce104SGreg Roach            }
2665729ce104SGreg Roach        }
2666729ce104SGreg Roach    }
2667729ce104SGreg Roach
2668729ce104SGreg Roach    /**
266976692c8bSGreg Roach     * Create a list of all ancestors.
267076692c8bSGreg Roach     *
2671*870ec5a3SGreg Roach     * @param array<stdClass> $list
2672729ce104SGreg Roach     * @param string          $pid
2673729ce104SGreg Roach     * @param bool            $children
2674729ce104SGreg Roach     * @param int             $generations
26758ba2e626SGreg Roach     *
26768ba2e626SGreg Roach     * @return void
2677729ce104SGreg Roach     */
26783b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2679c1010edaSGreg Roach    {
268013abd6f3SGreg Roach        $genlist                = [$pid];
2681729ce104SGreg Roach        $list[$pid]->generation = 1;
2682729ce104SGreg Roach        while (count($genlist) > 0) {
2683729ce104SGreg Roach            $id = array_shift($genlist);
2684dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2685729ce104SGreg Roach                continue; // id can be something like “empty7”
2686729ce104SGreg Roach            }
26876b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
268839ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
268939ca88baSGreg Roach                $husband = $family->husband();
269039ca88baSGreg Roach                $wife    = $family->wife();
2691729ce104SGreg Roach                if ($husband) {
2692c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2693c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2694729ce104SGreg Roach                }
2695729ce104SGreg Roach                if ($wife) {
2696c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2697c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2698729ce104SGreg Roach                }
2699729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2700729ce104SGreg Roach                    if ($husband) {
2701c0935879SGreg Roach                        $genlist[] = $husband->xref();
2702729ce104SGreg Roach                    }
2703729ce104SGreg Roach                    if ($wife) {
2704c0935879SGreg Roach                        $genlist[] = $wife->xref();
2705729ce104SGreg Roach                    }
2706729ce104SGreg Roach                }
2707729ce104SGreg Roach                if ($children) {
270839ca88baSGreg Roach                    foreach ($family->children() as $child) {
2709c0935879SGreg Roach                        $list[$child->xref()] = $child;
2710e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2711729ce104SGreg Roach                    }
2712729ce104SGreg Roach                }
2713729ce104SGreg Roach            }
2714729ce104SGreg Roach        }
2715729ce104SGreg Roach    }
2716729ce104SGreg Roach
2717729ce104SGreg Roach    /**
2718729ce104SGreg Roach     * get gedcom tag value
2719729ce104SGreg Roach     *
2720729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2721729ce104SGreg 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
2722729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2723729ce104SGreg Roach     *
2724729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2725729ce104SGreg Roach     */
2726b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2727c1010edaSGreg Roach    {
2728b2448a1bSGreg Roach        if ($gedrec === '') {
2729729ce104SGreg Roach            return '';
2730729ce104SGreg Roach        }
2731729ce104SGreg Roach        $tags      = explode(':', $tag);
2732729ce104SGreg Roach        $origlevel = $level;
2733b2448a1bSGreg Roach        if ($level === 0) {
27343c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2735729ce104SGreg Roach        }
2736729ce104SGreg Roach
2737729ce104SGreg Roach        $subrec = $gedrec;
2738f71a7dedSGreg Roach        $t = 'XXXX';
2739729ce104SGreg Roach        foreach ($tags as $t) {
2740729ce104SGreg Roach            $lastsubrec = $subrec;
27413d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2742729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2743729ce104SGreg Roach                $level--;
27443d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2745729ce104SGreg Roach            }
2746729ce104SGreg Roach            if (empty($subrec)) {
2747044416d2SGreg Roach                if ($t === 'TITL') {
27483d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2749729ce104SGreg Roach                    if (!empty($subrec)) {
27507a6ee1acSGreg Roach                        $t = 'ABBR';
2751729ce104SGreg Roach                    }
2752729ce104SGreg Roach                }
2753b2448a1bSGreg Roach                if ($subrec === '') {
2754729ce104SGreg Roach                    if ($level > 0) {
2755729ce104SGreg Roach                        $level--;
2756729ce104SGreg Roach                    }
27573d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2758b2448a1bSGreg Roach                    if ($subrec === '') {
2759729ce104SGreg Roach                        return '';
2760729ce104SGreg Roach                    }
2761729ce104SGreg Roach                }
2762729ce104SGreg Roach            }
2763729ce104SGreg Roach            $level++;
2764729ce104SGreg Roach        }
2765729ce104SGreg Roach        $level--;
2766729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2767f71a7dedSGreg Roach        if ($ct === 0) {
2768729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2769729ce104SGreg Roach        }
2770f71a7dedSGreg Roach        if ($ct === 0) {
2771729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2772729ce104SGreg Roach        }
2773729ce104SGreg Roach        if ($ct > 0) {
2774729ce104SGreg Roach            $value = trim($match[1]);
2775044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
27766b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2777ff166e64SGreg Roach                if ($note instanceof Note) {
2778729ce104SGreg Roach                    $value = $note->getNote();
2779729ce104SGreg Roach                } else {
2780729ce104SGreg Roach                    //-- set the value to the id without the @
2781729ce104SGreg Roach                    $value = $match[1];
2782729ce104SGreg Roach                }
2783729ce104SGreg Roach            }
2784f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
27853d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2786729ce104SGreg Roach            }
2787729ce104SGreg Roach
2788729ce104SGreg Roach            return $value;
2789729ce104SGreg Roach        }
2790729ce104SGreg Roach
27917a6ee1acSGreg Roach        return '';
2792729ce104SGreg Roach    }
2793d1286247SGreg Roach
2794d1286247SGreg Roach    /**
2795d1286247SGreg Roach     * Replace variable identifiers with their values.
2796d1286247SGreg Roach     *
2797d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
279882759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2799d1286247SGreg Roach     *
2800d1286247SGreg Roach     * @return string
2801d1286247SGreg Roach     */
28028f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2803c1010edaSGreg Roach    {
2804d1286247SGreg Roach        return preg_replace_callback(
2805d1286247SGreg Roach            '/\$(\w+)/',
280618d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28072118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
280882759250SGreg Roach                    if ($quote) {
28092118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2810b2ce94c6SRico Sonntag                    }
2811b2ce94c6SRico Sonntag
28122118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
281382759250SGreg Roach                }
2814b2ce94c6SRico Sonntag
2815d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28163d7a8a4cSGreg Roach
2817d1286247SGreg Roach                return '$' . $matches[1];
2818d1286247SGreg Roach            },
2819d1286247SGreg Roach            $expression
2820d1286247SGreg Roach        );
2821d1286247SGreg Roach    }
2822a6f13a4aSGreg Roach}
2823