xref: /webtrees/app/Report/ReportParserGenerate.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
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;
45a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface;
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
187a04bb9a2SGreg Roach    /** @var FilesystemInterface */
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
197a04bb9a2SGreg Roach     * @param FilesystemInterface $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,
204a04bb9a2SGreg Roach        FilesystemInterface $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     */
279*24f2a3afSGreg 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(
606*24f2a3afSGreg Roach            (int) $width,
607*24f2a3afSGreg 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 = preg_replace(
971c1010edaSGreg Roach                    [
972c1010edaSGreg Roach                        '/<span class="starredname">/',
973c1010edaSGreg Roach                        '/<\/span><\/span>/',
974c1010edaSGreg Roach                        '/<\/span>/',
975c1010edaSGreg Roach                    ],
976c1010edaSGreg Roach                    [
977c1010edaSGreg Roach                        '«',
978c1010edaSGreg Roach                        '',
979c1010edaSGreg Roach                        '»',
980c1010edaSGreg Roach                    ],
981a6f13a4aSGreg Roach                    $name
982a6f13a4aSGreg Roach                );
983a6f13a4aSGreg Roach                $name = strip_tags($name);
984a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
985381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
986a6f13a4aSGreg Roach                } else {
987911124f5SGreg Roach                    $addname = (string) $record->alternateName();
988a6f13a4aSGreg Roach                    $addname = preg_replace(
989c1010edaSGreg Roach                        [
990c1010edaSGreg Roach                            '/<span class="starredname">/',
991c1010edaSGreg Roach                            '/<\/span><\/span>/',
992c1010edaSGreg Roach                            '/<\/span>/',
993c1010edaSGreg Roach                        ],
994c1010edaSGreg Roach                        [
995c1010edaSGreg Roach                            '«',
996c1010edaSGreg Roach                            '',
997c1010edaSGreg Roach                            '»',
998c1010edaSGreg Roach                        ],
999a6f13a4aSGreg Roach                        $addname
1000a6f13a4aSGreg Roach                    );
1001a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
1002a6f13a4aSGreg Roach                    if (!empty($addname)) {
10037a6ee1acSGreg Roach                        $name .= ' ' . $addname;
1004a6f13a4aSGreg Roach                    }
1005a6f13a4aSGreg Roach                }
1006a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
1007a6f13a4aSGreg Roach            }
1008a6f13a4aSGreg Roach        }
1009a6f13a4aSGreg Roach    }
1010a6f13a4aSGreg Roach
1011a6f13a4aSGreg Roach    /**
1012fab8f067SGreg Roach     * Handle <gedcomValue />
1013a6f13a4aSGreg Roach     *
1014fab8f067SGreg Roach     * @param string[] $attrs
10158ba2e626SGreg Roach     *
10168ba2e626SGreg Roach     * @return void
1017a6f13a4aSGreg Roach     */
1018b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1019c1010edaSGreg Roach    {
10207a6ee1acSGreg Roach        $id    = '';
102113abd6f3SGreg Roach        $match = [];
10227a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1023a6f13a4aSGreg Roach            $id = $match[1];
1024a6f13a4aSGreg Roach        }
1025a6f13a4aSGreg Roach
1026044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10277a6ee1acSGreg Roach            $useBreak = '1';
1028a6f13a4aSGreg Roach        } else {
10297a6ee1acSGreg Roach            $useBreak = '0';
1030a6f13a4aSGreg Roach        }
1031a6f13a4aSGreg Roach
1032a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1033a6f13a4aSGreg Roach        if (!empty($tag)) {
1034044416d2SGreg Roach            if ($tag === '@desc') {
1035a6f13a4aSGreg Roach                $value = $this->desc;
1036a6f13a4aSGreg Roach                $value = trim($value);
1037a6f13a4aSGreg Roach                $this->current_element->addText($value);
1038a6f13a4aSGreg Roach            }
1039044416d2SGreg Roach            if ($tag === '@id') {
1040a6f13a4aSGreg Roach                $this->current_element->addText($id);
1041a6f13a4aSGreg Roach            } else {
10427a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1043a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1044b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1045b2448a1bSGreg Roach                    if ($level === 0) {
1046a6f13a4aSGreg Roach                        $level++;
1047a6f13a4aSGreg Roach                    }
1048a6f13a4aSGreg Roach                } else {
1049b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1050a6f13a4aSGreg Roach                }
1051a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10523d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1053a6f13a4aSGreg Roach                switch (end($tags)) {
1054a6f13a4aSGreg Roach                    case 'DATE':
1055a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1056a6f13a4aSGreg Roach                        $value = $tmp->display();
1057a6f13a4aSGreg Roach                        break;
1058a6f13a4aSGreg Roach                    case 'PLAC':
1059299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1060392561bbSGreg Roach                        $value = $tmp->shortName();
1061a6f13a4aSGreg Roach                        break;
1062a6f13a4aSGreg Roach                }
1063044416d2SGreg Roach                if ($useBreak === '1') {
1064a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1065a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1066a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1067a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1068a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10693b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10703b3cfeeaSGreg Roach                        $value = substr($value, 4);
1071a6f13a4aSGreg Roach                    }
1072a6f13a4aSGreg Roach                }
1073d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10743cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
1075299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1076a4d703aeSGreg Roach                }
1077a2a485c3SGreg Roach
1078a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
10799c32db08SGreg Roach                    $value = strip_tags($value);
1080381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1081a2a485c3SGreg Roach                }
1082a6f13a4aSGreg Roach                $this->current_element->addText($value);
1083a6f13a4aSGreg Roach            }
1084a6f13a4aSGreg Roach        }
1085a6f13a4aSGreg Roach    }
1086a6f13a4aSGreg Roach
1087a6f13a4aSGreg Roach    /**
1088fab8f067SGreg Roach     * Handle <repeatTag>
1089a6f13a4aSGreg Roach     *
1090fab8f067SGreg Roach     * @param string[] $attrs
10918ba2e626SGreg Roach     *
10928ba2e626SGreg Roach     * @return void
1093a6f13a4aSGreg Roach     */
1094b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1095c1010edaSGreg Roach    {
1096a6f13a4aSGreg Roach        $this->process_repeats++;
1097a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1098a6f13a4aSGreg Roach            return;
1099a6f13a4aSGreg Roach        }
1100a6f13a4aSGreg Roach
11019b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
110213abd6f3SGreg Roach        $this->repeats         = [];
1103e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1104a6f13a4aSGreg Roach
1105e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1106a6f13a4aSGreg Roach        if (!empty($tag)) {
1107044416d2SGreg Roach            if ($tag === '@desc') {
1108a6f13a4aSGreg Roach                $value = $this->desc;
1109a6f13a4aSGreg Roach                $value = trim($value);
1110a6f13a4aSGreg Roach                $this->current_element->addText($value);
1111a6f13a4aSGreg Roach            } else {
11127a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11137a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1114b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1115b2448a1bSGreg Roach                if ($level === 0) {
1116a6f13a4aSGreg Roach                    $level++;
1117a6f13a4aSGreg Roach                }
1118a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1119a6f13a4aSGreg Roach                $t      = $tag;
1120a6f13a4aSGreg Roach                $count  = count($tags);
1121a6f13a4aSGreg Roach                $i      = 0;
1122a6f13a4aSGreg Roach                while ($i < $count) {
1123a6f13a4aSGreg Roach                    $t = $tags[$i];
1124a6f13a4aSGreg Roach                    if (!empty($t)) {
1125a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
11263d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1127a6f13a4aSGreg Roach                            if (empty($subrec)) {
1128a6f13a4aSGreg Roach                                $level--;
11293d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1130a6f13a4aSGreg Roach                                if (empty($subrec)) {
1131a6f13a4aSGreg Roach                                    return;
1132a6f13a4aSGreg Roach                                }
1133a6f13a4aSGreg Roach                            }
1134a6f13a4aSGreg Roach                        }
1135a6f13a4aSGreg Roach                        $level++;
1136a6f13a4aSGreg Roach                    }
1137a6f13a4aSGreg Roach                    $i++;
1138a6f13a4aSGreg Roach                }
1139a6f13a4aSGreg Roach                $level--;
1140a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1141a6f13a4aSGreg Roach                $i     = 0;
1142a6f13a4aSGreg Roach                while ($i < $count) {
1143a6f13a4aSGreg Roach                    $i++;
1144a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1145a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11468d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11476b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1148a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1149a9007102SGreg Roach                            continue;
1150a9007102SGreg Roach                        }
1151a9007102SGreg Roach                    }
1152a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1153a6f13a4aSGreg Roach                }
1154a6f13a4aSGreg Roach            }
1155a6f13a4aSGreg Roach        }
1156a6f13a4aSGreg Roach    }
1157a6f13a4aSGreg Roach
1158a6f13a4aSGreg Roach    /**
1159fab8f067SGreg Roach     * Handle </repeatTag>
11608ba2e626SGreg Roach     *
11618ba2e626SGreg Roach     * @return void
1162a6f13a4aSGreg Roach     */
1163b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1164c1010edaSGreg Roach    {
1165a6f13a4aSGreg Roach        $this->process_repeats--;
1166a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1167a6f13a4aSGreg Roach            return;
1168a6f13a4aSGreg Roach        }
1169a6f13a4aSGreg Roach
1170a6f13a4aSGreg Roach        // Check if there is anything to repeat
1171a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1172a6f13a4aSGreg Roach            // No need to load them if not used...
1173a6f13a4aSGreg Roach
1174a6f13a4aSGreg Roach            $lineoffset = 0;
1175a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1176a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1177a6f13a4aSGreg Roach            }
1178a6f13a4aSGreg Roach            //-- read the xml from the file
1179299d100dSGreg Roach            $lines = file($this->report);
1180dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1181a6f13a4aSGreg Roach                $lineoffset--;
1182a6f13a4aSGreg Roach            }
1183a6f13a4aSGreg Roach            $lineoffset++;
1184a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1185a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1186a6f13a4aSGreg Roach            // RepeatTag Level counter
1187a6f13a4aSGreg Roach            $count = 1;
1188a6f13a4aSGreg Roach            while (0 < $count) {
1189dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1190a6f13a4aSGreg Roach                    $count++;
1191dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1192a6f13a4aSGreg Roach                    $count--;
1193a6f13a4aSGreg Roach                }
1194a6f13a4aSGreg Roach                if (0 < $count) {
1195a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1196a6f13a4aSGreg Roach                }
1197a6f13a4aSGreg Roach                $line_nr++;
1198a6f13a4aSGreg Roach            }
1199a6f13a4aSGreg Roach            // No need to drag this
1200a6f13a4aSGreg Roach            unset($lines);
1201a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1202a6f13a4aSGreg Roach            // Save original values
12039b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1204a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1205a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1206a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1207a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1208e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1209a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
12101aa04befSGreg Roach
12111aa04befSGreg Roach                xml_set_element_handler(
12121aa04befSGreg Roach                    $repeat_parser,
12139d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12141aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12151aa04befSGreg Roach                    },
12169d454b6bSGreg Roach                    function ($parser, string $name): void {
12171aa04befSGreg Roach                        $this->endElement($parser, $name);
12181aa04befSGreg Roach                    }
12191aa04befSGreg Roach                );
12201aa04befSGreg Roach
12211aa04befSGreg Roach                xml_set_character_data_handler(
12221aa04befSGreg Roach                    $repeat_parser,
12239d454b6bSGreg Roach                    function ($parser, string $data): void {
12241aa04befSGreg Roach                        $this->characterData($parser, $data);
12251aa04befSGreg Roach                    }
12261aa04befSGreg Roach                );
12271aa04befSGreg Roach
1228a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12296ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1230a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1231a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1232a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1233a6f13a4aSGreg Roach                    ));
1234a6f13a4aSGreg Roach                }
1235a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1236a6f13a4aSGreg Roach            }
1237a6f13a4aSGreg Roach            // Restore original values
1238a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1239e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1240a6f13a4aSGreg Roach        }
124165e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1242a6f13a4aSGreg Roach    }
1243a6f13a4aSGreg Roach
1244a6f13a4aSGreg Roach    /**
1245a6f13a4aSGreg Roach     * Variable lookup
1246a6f13a4aSGreg Roach     * Retrieve predefined variables :
1247a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1248a6f13a4aSGreg Roach     *        1 EVEN This is a description
1249a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1250a6f13a4aSGreg Roach     * $ I18N::translate('....')
1251a6f13a4aSGreg Roach     * $ language_settings[]
1252a6f13a4aSGreg Roach     *
1253c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12548ba2e626SGreg Roach     *
12558ba2e626SGreg Roach     * @return void
1256a6f13a4aSGreg Roach     */
1257b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1258c1010edaSGreg Roach    {
1259a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12606ccdf4f0SGreg 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));
1261a6f13a4aSGreg Roach        }
1262a6f13a4aSGreg Roach
1263a6f13a4aSGreg Roach        $var = $attrs['var'];
1264a6f13a4aSGreg Roach        // SetVar element preset variables
1265d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1266d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1267a6f13a4aSGreg Roach        } else {
1268a6f13a4aSGreg Roach            $tfact = $this->fact;
12697a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1270a6f13a4aSGreg Roach                // Use :
1271a6f13a4aSGreg Roach                // n TYPE This text if string
1272a6f13a4aSGreg Roach                $tfact = $this->type;
1273a6f13a4aSGreg Roach            }
1274c1010edaSGreg Roach            $var = str_replace([
1275c1010edaSGreg Roach                '@fact',
1276c1010edaSGreg Roach                '@desc',
1277c1010edaSGreg Roach            ], [
1278c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1279c1010edaSGreg Roach                $this->desc,
1280c1010edaSGreg Roach            ], $var);
1281a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1282da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1283a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1284a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1285a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1286a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1287a6f13a4aSGreg Roach            }
1288a6f13a4aSGreg Roach        }
1289a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1290a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12917a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1292a6f13a4aSGreg Roach                $g   = new Date($var);
1293a6f13a4aSGreg Roach                $var = $g->display();
1294a6f13a4aSGreg Roach            }
1295a6f13a4aSGreg Roach        }
1296a6f13a4aSGreg Roach        $this->current_element->addText($var);
12972836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1298a6f13a4aSGreg Roach    }
1299a6f13a4aSGreg Roach
1300a6f13a4aSGreg Roach    /**
1301fab8f067SGreg Roach     * Handle <facts>
130276692c8bSGreg Roach     *
1303fab8f067SGreg Roach     * @param string[] $attrs
13048ba2e626SGreg Roach     *
13058ba2e626SGreg Roach     * @return void
1306a6f13a4aSGreg Roach     */
1307b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1308c1010edaSGreg Roach    {
1309a6f13a4aSGreg Roach        $this->process_repeats++;
1310a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1311a6f13a4aSGreg Roach            return;
1312a6f13a4aSGreg Roach        }
1313a6f13a4aSGreg Roach
13149b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
131513abd6f3SGreg Roach        $this->repeats         = [];
1316e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1317a6f13a4aSGreg Roach
13187a6ee1acSGreg Roach        $id    = '';
131913abd6f3SGreg Roach        $match = [];
13207a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1321a6f13a4aSGreg Roach            $id = $match[1];
1322a6f13a4aSGreg Roach        }
13237a6ee1acSGreg Roach        $tag = '';
1324a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1325a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1326a6f13a4aSGreg Roach        }
13277a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1328d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1329a6f13a4aSGreg Roach        }
1330a6f13a4aSGreg Roach
13316b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1332a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1333820b62dfSGreg Roach            $facts = $record->facts([], true);
133413abd6f3SGreg Roach            $this->repeats = [];
1335a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1336195b5e75SGreg Roach            foreach ($facts as $fact) {
13377fe676e5SGreg Roach                if (!in_array($fact->getTag(), $nonfacts, true)) {
1338195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1339a6f13a4aSGreg Roach                }
1340a6f13a4aSGreg Roach            }
1341a6f13a4aSGreg Roach        } else {
134230158ae7SGreg Roach            foreach ($record->facts() as $fact) {
13437fe676e5SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1344138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1345a6f13a4aSGreg Roach                }
1346a6f13a4aSGreg Roach            }
1347a6f13a4aSGreg Roach        }
1348a6f13a4aSGreg Roach    }
1349a6f13a4aSGreg Roach
1350a6f13a4aSGreg Roach    /**
1351fab8f067SGreg Roach     * Handle </facts>
13528ba2e626SGreg Roach     *
13538ba2e626SGreg Roach     * @return void
1354a6f13a4aSGreg Roach     */
1355b702978eSGreg Roach    protected function factsEndHandler(): void
1356c1010edaSGreg Roach    {
1357a6f13a4aSGreg Roach        $this->process_repeats--;
1358a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1359a6f13a4aSGreg Roach            return;
1360a6f13a4aSGreg Roach        }
1361a6f13a4aSGreg Roach
1362a6f13a4aSGreg Roach        // Check if there is anything to repeat
1363a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1364e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1365a6f13a4aSGreg Roach            $lineoffset = 0;
1366a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1367a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1368a6f13a4aSGreg Roach            }
1369a6f13a4aSGreg Roach
1370a6f13a4aSGreg Roach            //-- read the xml from the file
1371299d100dSGreg Roach            $lines = file($this->report);
1372dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1373a6f13a4aSGreg Roach                $lineoffset--;
1374a6f13a4aSGreg Roach            }
1375a6f13a4aSGreg Roach            $lineoffset++;
1376a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1377a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1378a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1379a6f13a4aSGreg Roach            while ($line_nr < $i) {
1380a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1381a6f13a4aSGreg Roach                $line_nr++;
1382a6f13a4aSGreg Roach            }
1383a6f13a4aSGreg Roach            // No need to drag this
1384a6f13a4aSGreg Roach            unset($lines);
1385a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1386a6f13a4aSGreg Roach            // Save original values
13879b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1388a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1389a6f13a4aSGreg Roach            $count                = count($this->repeats);
1390a6f13a4aSGreg Roach            $i                    = 0;
1391a6f13a4aSGreg Roach            while ($i < $count) {
1392a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1393a6f13a4aSGreg Roach                $this->fact   = '';
1394a6f13a4aSGreg Roach                $this->desc   = '';
1395a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1396a6f13a4aSGreg Roach                    $this->fact = $match[1];
1397a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
139813abd6f3SGreg Roach                        $tmatch = [];
1399a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1400a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1401a6f13a4aSGreg Roach                        } else {
1402a6f13a4aSGreg Roach                            $this->type = ' ';
1403a6f13a4aSGreg Roach                        }
1404a6f13a4aSGreg Roach                    }
1405a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
14063d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1407a6f13a4aSGreg Roach                }
1408a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1409e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1410a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
14111aa04befSGreg Roach
14121aa04befSGreg Roach                xml_set_element_handler(
14131aa04befSGreg Roach                    $repeat_parser,
14149d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14151aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14161aa04befSGreg Roach                    },
14179d454b6bSGreg Roach                    function ($parser, string $name): void {
14181aa04befSGreg Roach                        $this->endElement($parser, $name);
14191aa04befSGreg Roach                    }
14201aa04befSGreg Roach                );
14211aa04befSGreg Roach
14221aa04befSGreg Roach                xml_set_character_data_handler(
14231aa04befSGreg Roach                    $repeat_parser,
14249d454b6bSGreg Roach                    function ($parser, string $data): void {
14251aa04befSGreg Roach                        $this->characterData($parser, $data);
14261aa04befSGreg Roach                    }
14271aa04befSGreg Roach                );
14281aa04befSGreg Roach
1429a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14306ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1431a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1432a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1433a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1434a6f13a4aSGreg Roach                    ));
1435a6f13a4aSGreg Roach                }
1436a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1437a6f13a4aSGreg Roach                $i++;
1438a6f13a4aSGreg Roach            }
1439a6f13a4aSGreg Roach            // Restore original values
1440e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1441a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1442a6f13a4aSGreg Roach        }
144365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1444a6f13a4aSGreg Roach    }
1445a6f13a4aSGreg Roach
1446a6f13a4aSGreg Roach    /**
1447a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1448d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1449a6f13a4aSGreg Roach     *
1450c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14518ba2e626SGreg Roach     *
14528ba2e626SGreg Roach     * @return void
1453a6f13a4aSGreg Roach     */
1454b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1455c1010edaSGreg Roach    {
1456a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14576ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1458a6f13a4aSGreg Roach        }
1459a6f13a4aSGreg Roach
1460a6f13a4aSGreg Roach        $name  = $attrs['name'];
1461a6f13a4aSGreg Roach        $value = $attrs['value'];
146213abd6f3SGreg Roach        $match = [];
1463a6f13a4aSGreg Roach        // Current GEDCOM record strings
1464044416d2SGreg Roach        if ($value === '@ID') {
14657a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1466a6f13a4aSGreg Roach                $value = $match[1];
1467a6f13a4aSGreg Roach            }
1468044416d2SGreg Roach        } elseif ($value === '@fact') {
1469a6f13a4aSGreg Roach            $value = $this->fact;
1470044416d2SGreg Roach        } elseif ($value === '@desc') {
1471a6f13a4aSGreg Roach            $value = $this->desc;
1472044416d2SGreg Roach        } elseif ($value === '@generation') {
1473589feda3SGreg Roach            $value = (string) $this->generation;
1474a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
147513abd6f3SGreg Roach            $gmatch = [];
1476a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14777a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1478a6f13a4aSGreg Roach            }
1479a6f13a4aSGreg Roach        }
1480a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1481d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1482a6f13a4aSGreg Roach        }
1483a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1484a6f13a4aSGreg Roach        $i     = 0;
1485a6f13a4aSGreg Roach        while ($i < $count) {
1486d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14877a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1488a6f13a4aSGreg Roach            $i++;
1489a6f13a4aSGreg Roach        }
1490a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1491da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1492a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1493a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1494a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1495a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1496a6f13a4aSGreg Roach        }
149752868398SGreg Roach
1498a6f13a4aSGreg Roach        // Arithmetic functions
14993cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
150052868398SGreg Roach            // Create an expression language with the functions used by our reports.
150152868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1502c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1503c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
150452868398SGreg Roach
150552868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1506a6f13a4aSGreg Roach        }
150752868398SGreg Roach
1508dec352c1SGreg Roach        if (str_contains($value, '@')) {
15097a6ee1acSGreg Roach            $value = '';
1510a6f13a4aSGreg Roach        }
1511d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1512a6f13a4aSGreg Roach    }
1513a6f13a4aSGreg Roach
1514a6f13a4aSGreg Roach    /**
1515fab8f067SGreg Roach     * Handle <if>
1516a6f13a4aSGreg Roach     *
1517fab8f067SGreg Roach     * @param string[] $attrs
15188ba2e626SGreg Roach     *
15198ba2e626SGreg Roach     * @return void
1520a6f13a4aSGreg Roach     */
1521b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1522c1010edaSGreg Roach    {
1523a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1524a6f13a4aSGreg Roach            $this->process_ifs++;
1525a6f13a4aSGreg Roach
1526a6f13a4aSGreg Roach            return;
1527a6f13a4aSGreg Roach        }
1528a6f13a4aSGreg Roach
1529a6f13a4aSGreg Roach        $condition = $attrs['condition'];
153082759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1531c1010edaSGreg Roach        $condition = str_replace([
1532c1010edaSGreg Roach            ' LT ',
1533c1010edaSGreg Roach            ' GT ',
1534c1010edaSGreg Roach        ], [
1535c1010edaSGreg Roach            '<',
1536c1010edaSGreg Roach            '>',
1537c1010edaSGreg Roach        ], $condition);
15383cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15397a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
154013abd6f3SGreg Roach        $match     = [];
15413cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1542a6f13a4aSGreg Roach        $i         = 0;
1543a6f13a4aSGreg Roach        while ($i < $count) {
1544a6f13a4aSGreg Roach            $id    = $match[$i][1];
1545a6f13a4aSGreg Roach            $value = '""';
1546044416d2SGreg Roach            if ($id === 'ID') {
15477a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1548a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1549a6f13a4aSGreg Roach                }
15507a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1551a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15527a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1553a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15547a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1555a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1556a6f13a4aSGreg Roach            } else {
1557b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1558b2448a1bSGreg Roach                if ($level === 0) {
1559a6f13a4aSGreg Roach                    $level++;
1560a6f13a4aSGreg Roach                }
15613d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1562a6f13a4aSGreg Roach                if (empty($value)) {
1563a6f13a4aSGreg Roach                    $level++;
15643d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1565a6f13a4aSGreg Roach                }
15668d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15675e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1568a6f13a4aSGreg Roach            }
1569a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1570a6f13a4aSGreg Roach            $i++;
1571a6f13a4aSGreg Roach        }
15725809450fSGreg Roach
1573cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1574cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1575c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1576c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1577cb63a60eSGreg Roach
1578cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15795809450fSGreg Roach
1580a6f13a4aSGreg Roach        if (!$ret) {
1581a6f13a4aSGreg Roach            $this->process_ifs++;
1582a6f13a4aSGreg Roach        }
1583a6f13a4aSGreg Roach    }
1584a6f13a4aSGreg Roach
1585a6f13a4aSGreg Roach    /**
1586fab8f067SGreg Roach     * Handle </if>
15878ba2e626SGreg Roach     *
15888ba2e626SGreg Roach     * @return void
1589a6f13a4aSGreg Roach     */
1590b702978eSGreg Roach    protected function ifEndHandler(): void
1591c1010edaSGreg Roach    {
1592a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1593a6f13a4aSGreg Roach            $this->process_ifs--;
1594a6f13a4aSGreg Roach        }
1595a6f13a4aSGreg Roach    }
1596a6f13a4aSGreg Roach
1597a6f13a4aSGreg Roach    /**
1598fab8f067SGreg Roach     * Handle <footnote>
1599a6f13a4aSGreg Roach     * Collect the Footnote links
1600fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1601a6f13a4aSGreg Roach     *
1602fab8f067SGreg Roach     * @param string[] $attrs
16038ba2e626SGreg Roach     *
16048ba2e626SGreg Roach     * @return void
1605a6f13a4aSGreg Roach     */
1606b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1607c1010edaSGreg Roach    {
16087a6ee1acSGreg Roach        $id = '';
16097a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1610a6f13a4aSGreg Roach            $id = $match[2];
1611a6f13a4aSGreg Roach        }
16126b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1613a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16149b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1615a6f13a4aSGreg Roach            $this->print_data         = true;
16167a6ee1acSGreg Roach            $style                    = '';
1617a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1618a6f13a4aSGreg Roach                $style = $attrs['style'];
1619a6f13a4aSGreg Roach            }
1620a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1621e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1622a6f13a4aSGreg Roach        } else {
1623a6f13a4aSGreg Roach            $this->print_data       = false;
1624a6f13a4aSGreg Roach            $this->process_footnote = false;
1625a6f13a4aSGreg Roach        }
1626a6f13a4aSGreg Roach    }
1627a6f13a4aSGreg Roach
1628a6f13a4aSGreg Roach    /**
1629fab8f067SGreg Roach     * Handle </footnote>
1630a6f13a4aSGreg Roach     * Print the collected Footnote data
16318ba2e626SGreg Roach     *
16328ba2e626SGreg Roach     * @return void
1633a6f13a4aSGreg Roach     */
1634b702978eSGreg Roach    protected function footnoteEndHandler(): void
1635c1010edaSGreg Roach    {
1636a6f13a4aSGreg Roach        if ($this->process_footnote) {
1637a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1638a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1639a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1640e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1641a6f13a4aSGreg Roach            }
1642a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1643a6f13a4aSGreg Roach        } else {
1644a6f13a4aSGreg Roach            $this->process_footnote = true;
1645a6f13a4aSGreg Roach        }
1646a6f13a4aSGreg Roach    }
1647a6f13a4aSGreg Roach
1648a6f13a4aSGreg Roach    /**
1649fab8f067SGreg Roach     * Handle <footnoteTexts />
16508ba2e626SGreg Roach     *
16518ba2e626SGreg Roach     * @return void
1652a6f13a4aSGreg Roach     */
1653b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1654c1010edaSGreg Roach    {
16557a6ee1acSGreg Roach        $temp = 'footnotetexts';
1656e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1657a6f13a4aSGreg Roach    }
1658a6f13a4aSGreg Roach
1659a6f13a4aSGreg Roach    /**
1660a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16618ba2e626SGreg Roach     *
16628ba2e626SGreg Roach     * @return void
1663a6f13a4aSGreg Roach     */
1664b702978eSGreg Roach    protected function brStartHandler(): void
1665c1010edaSGreg Roach    {
1666a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1667a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1668a6f13a4aSGreg Roach        }
1669a6f13a4aSGreg Roach    }
1670a6f13a4aSGreg Roach
1671a6f13a4aSGreg Roach    /**
1672fab8f067SGreg Roach     * Handle <sp />
1673fab8f067SGreg Roach     * Forced space
16748ba2e626SGreg Roach     *
16758ba2e626SGreg Roach     * @return void
1676a6f13a4aSGreg Roach     */
1677b702978eSGreg Roach    protected function spStartHandler(): void
1678c1010edaSGreg Roach    {
1679a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1680a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1681a6f13a4aSGreg Roach        }
1682a6f13a4aSGreg Roach    }
1683a6f13a4aSGreg Roach
1684a6f13a4aSGreg Roach    /**
1685fab8f067SGreg Roach     * Handle <highlightedImage />
168676692c8bSGreg Roach     *
1687fab8f067SGreg Roach     * @param string[] $attrs
16888ba2e626SGreg Roach     *
16898ba2e626SGreg Roach     * @return void
1690a6f13a4aSGreg Roach     */
1691b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1692c1010edaSGreg Roach    {
1693a6f13a4aSGreg Roach        $id = '';
16947a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1695a6f13a4aSGreg Roach            $id = $match[1];
1696a6f13a4aSGreg Roach        }
1697a6f13a4aSGreg Roach
1698c21bdddcSGreg Roach        // Position the top corner of this box on the page
1699c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1700a6f13a4aSGreg Roach
1701c21bdddcSGreg Roach        // Position the left corner of this box on the page
1702c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1703a6f13a4aSGreg Roach
170483cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
170583cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1706a6f13a4aSGreg Roach
1707a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
170883cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1709a6f13a4aSGreg Roach
171083cdc021SGreg Roach        // Width, height (or both).
1711c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1712c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1713a6f13a4aSGreg Roach
17146b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
17154a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
171686a63f51SGreg Roach
1717a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1718a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1719b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
172029518ad2SGreg Roach
1721a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17223c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17233c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1724a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17253c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17263c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1727a6f13a4aSGreg Roach            } else {
17283c3b90deSGreg Roach                $width  = $attributes[0];
17293c3b90deSGreg Roach                $height = $attributes[1];
1730a6f13a4aSGreg Roach            }
1731a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1732e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1733a6f13a4aSGreg Roach        }
1734a6f13a4aSGreg Roach    }
1735a6f13a4aSGreg Roach
1736a6f13a4aSGreg Roach    /**
1737fab8f067SGreg Roach     * Handle <image/>
173876692c8bSGreg Roach     *
1739fab8f067SGreg Roach     * @param string[] $attrs
17408ba2e626SGreg Roach     *
17418ba2e626SGreg Roach     * @return void
1742a6f13a4aSGreg Roach     */
1743b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1744c1010edaSGreg Roach    {
174583cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1746c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1747a6f13a4aSGreg Roach
1748a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1749c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1750a6f13a4aSGreg Roach
175183cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
175283cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1753a6f13a4aSGreg Roach
1754a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
175583cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1756a6f13a4aSGreg Roach
175783cdc021SGreg Roach        // Width, height (or both).
1758c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1759c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1760a6f13a4aSGreg Roach
176183cdc021SGreg Roach        $file = $attrs['file'] ?? '';
176283cdc021SGreg Roach
1763044416d2SGreg Roach        if ($file === '@FILE') {
176413abd6f3SGreg Roach            $match = [];
1765a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17666b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17674a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1768cdf416fbSGreg Roach
1769a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1770a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1771b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
177229518ad2SGreg Roach
1773a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17743c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17753c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1776a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17773c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17783c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1779a6f13a4aSGreg Roach                    } else {
17803c3b90deSGreg Roach                        $width  = $attributes[0];
17813c3b90deSGreg Roach                        $height = $attributes[1];
1782a6f13a4aSGreg Roach                    }
1783a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1784e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1785a6f13a4aSGreg Roach                }
1786a6f13a4aSGreg Roach            }
1787a6f13a4aSGreg Roach        } else {
17887a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1789a6f13a4aSGreg Roach                $size = getimagesize($file);
1790a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1791a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1792a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1793a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1794a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1795a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1796a6f13a4aSGreg Roach                } else {
1797a6f13a4aSGreg Roach                    $width  = $size[0];
1798a6f13a4aSGreg Roach                    $height = $size[1];
1799a6f13a4aSGreg Roach                }
1800e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1801e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1802a6f13a4aSGreg Roach            }
1803a6f13a4aSGreg Roach        }
1804a6f13a4aSGreg Roach    }
1805a6f13a4aSGreg Roach
1806a6f13a4aSGreg Roach    /**
1807fab8f067SGreg Roach     * Handle <line>
1808a6f13a4aSGreg Roach     *
1809fab8f067SGreg Roach     * @param string[] $attrs
18108ba2e626SGreg Roach     *
18118ba2e626SGreg Roach     * @return void
1812a6f13a4aSGreg Roach     */
1813b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1814c1010edaSGreg Roach    {
1815a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1816c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1817a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18187a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1819a6f13a4aSGreg Roach                $x1 = 0;
18207a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1821c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1822a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1823c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1824a6f13a4aSGreg Roach            }
1825a6f13a4aSGreg Roach        }
1826a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1827c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1828a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18297a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1830a6f13a4aSGreg Roach                $y1 = 0;
18317a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1832c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1833a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1834c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1835a6f13a4aSGreg Roach            }
1836a6f13a4aSGreg Roach        }
1837a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1838c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1839a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18407a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1841a6f13a4aSGreg Roach                $x2 = 0;
18427a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1843c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1844a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1845c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1846a6f13a4aSGreg Roach            }
1847a6f13a4aSGreg Roach        }
1848a6f13a4aSGreg Roach        // End vertical position
1849c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1850a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18517a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1852a6f13a4aSGreg Roach                $y2 = 0;
18537a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1854c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1855a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1856c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1857a6f13a4aSGreg Roach            }
1858a6f13a4aSGreg Roach        }
1859a6f13a4aSGreg Roach
1860e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1861e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1862a6f13a4aSGreg Roach    }
1863a6f13a4aSGreg Roach
1864a6f13a4aSGreg Roach    /**
1865fab8f067SGreg Roach     * Handle <list>
1866a6f13a4aSGreg Roach     *
1867fab8f067SGreg Roach     * @param string[] $attrs
18688ba2e626SGreg Roach     *
18698ba2e626SGreg Roach     * @return void
1870a6f13a4aSGreg Roach     */
1871b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1872c1010edaSGreg Roach    {
1873a6f13a4aSGreg Roach        $this->process_repeats++;
1874a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1875a6f13a4aSGreg Roach            return;
1876a6f13a4aSGreg Roach        }
1877a6f13a4aSGreg Roach
187813abd6f3SGreg Roach        $match = [];
1879a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1880a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1881a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1882d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1883a6f13a4aSGreg Roach                $sortby = trim($sortby);
1884a6f13a4aSGreg Roach            }
1885a6f13a4aSGreg Roach        } else {
18867a6ee1acSGreg Roach            $sortby = 'NAME';
1887a6f13a4aSGreg Roach        }
1888a6f13a4aSGreg Roach
1889e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1890195b5e75SGreg Roach
1891a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1892a6f13a4aSGreg Roach        switch ($listname) {
18937a6ee1acSGreg Roach            case 'pending':
18946c0bef3aSGreg Roach                $xrefs = DB::table('change')
1895195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1896a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1897195b5e75SGreg Roach                            ->from('change')
1898195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1899195b5e75SGreg Roach                            ->where('status', '=', 'pending')
19007f5c2944SGreg Roach                            ->groupBy(['xref']);
1901195b5e75SGreg Roach                    })
19026c0bef3aSGreg Roach                    ->pluck('xref');
1903195b5e75SGreg Roach
190413abd6f3SGreg Roach                $this->list = [];
19056c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
19066b9cb339SGreg Roach                    $this->list[] = Registry::gedcomRecordFactory()->make($xref, $this->tree);
1907a6f13a4aSGreg Roach                }
1908a6f13a4aSGreg Roach                break;
1909a6f13a4aSGreg Roach            case 'individual':
19105985adfbSGreg Roach                $query = DB::table('individuals')
19115985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19125985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19135985adfbSGreg Roach                    ->distinct();
19145985adfbSGreg Roach
1915a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1916dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
191782759250SGreg Roach                        $value = $this->substituteVars($value, false);
1918a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1919a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19200b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19215985adfbSGreg Roach                                $join
19225985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19235985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19245985adfbSGreg Roach                            });
19255985adfbSGreg Roach
19265985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19275985adfbSGreg Roach
1928a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19295985adfbSGreg Roach
1930044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19315985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1932a6f13a4aSGreg Roach                            } else {
19335985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1934a6f13a4aSGreg Roach                            }
19355985adfbSGreg Roach
19365985adfbSGreg Roach                            // This filter has been fully processed
19375985adfbSGreg Roach                            unset($attrs[$attr]);
19387ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19390b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19405985adfbSGreg Roach                                $join
19415985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19425985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19435985adfbSGreg Roach                            });
1944a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19457a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19465d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1947b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1948a6f13a4aSGreg Roach                            }
19495985adfbSGreg Roach
19505985adfbSGreg Roach                            // This filter has been fully processed
19515985adfbSGreg Roach                            unset($attrs[$attr]);
1952a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19535985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1954a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19555985adfbSGreg Roach
1956a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19575985adfbSGreg Roach
19585985adfbSGreg Roach                            // This filter has been fully processed
19595985adfbSGreg Roach                            unset($attrs[$attr]);
19602fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19619dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19625985adfbSGreg Roach                            $query
19630b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19645985adfbSGreg Roach                                    $join
19657ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19667ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19675985adfbSGreg Roach                                })
19680b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19697ee4bfadSGreg Roach                                    $join
19707ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19717ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19727ee4bfadSGreg Roach                                })
19739b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19746e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19755985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19767ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19776e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19786e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
19796e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
19806e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19816e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1982e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19837ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1984a6f13a4aSGreg Roach                        }
1985a6f13a4aSGreg Roach                    }
1986a6f13a4aSGreg Roach                }
1987a6f13a4aSGreg Roach
198813abd6f3SGreg Roach                $this->list = [];
1989a6f13a4aSGreg Roach
19905985adfbSGreg Roach                foreach ($query->get() as $row) {
19916b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1992a6f13a4aSGreg Roach                }
1993a6f13a4aSGreg Roach                break;
1994a6f13a4aSGreg Roach
1995a6f13a4aSGreg Roach            case 'family':
199630fc2b1eSGreg Roach                $query = DB::table('families')
199730fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
199830fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
199930fc2b1eSGreg Roach                    ->distinct();
200030fc2b1eSGreg Roach
2001a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
2002dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
200382759250SGreg Roach                        $value = $this->substituteVars($value, false);
2004a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2005a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20060b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200730fc2b1eSGreg Roach                                $join
200830fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
200930fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
201030fc2b1eSGreg Roach                            });
201130fc2b1eSGreg Roach
201230fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
201330fc2b1eSGreg Roach
2014a6f13a4aSGreg Roach                            $date = new Date($match[3]);
201530fc2b1eSGreg Roach
2016044416d2SGreg Roach                            if ($match[2] === 'LTE') {
201730fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2018a6f13a4aSGreg Roach                            } else {
201930fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2020a6f13a4aSGreg Roach                            }
202130fc2b1eSGreg Roach
202230fc2b1eSGreg Roach                            // This filter has been fully processed
202330fc2b1eSGreg Roach                            unset($attrs[$attr]);
2024a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
202530fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2026a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
202730fc2b1eSGreg Roach
2028a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
202930fc2b1eSGreg Roach
203030fc2b1eSGreg Roach                            // This filter has been fully processed
203130fc2b1eSGreg Roach                            unset($attrs[$attr]);
203230fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20333b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20340b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
203530fc2b1eSGreg Roach                                    $join
203630fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20373b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
203830fc2b1eSGreg Roach                                            $query
203930fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
204030fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
204130fc2b1eSGreg Roach                                        });
204230fc2b1eSGreg Roach                                });
20435d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20447a6ee1acSGreg Roach                                if ($match[1] != '') {
20457a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20465d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2047b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20485d0bc43dSGreg Roach                                    }
20495d0bc43dSGreg Roach                                }
2050a6f13a4aSGreg Roach                            }
205130fc2b1eSGreg Roach
205230fc2b1eSGreg Roach                            // This filter has been fully processed
205330fc2b1eSGreg Roach                            unset($attrs[$attr]);
20546e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20559dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
205630fc2b1eSGreg Roach                            $query
20570b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
205830fc2b1eSGreg Roach                                    $join
20596e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20606e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
206130fc2b1eSGreg Roach                                })
20620b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20636e5c0963SGreg Roach                                    $join
20646e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20656e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20666e5c0963SGreg Roach                                })
2067b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20686e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
206930fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20706e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20716e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20726e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20736e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20746e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20756e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2076e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20776e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2078a6f13a4aSGreg Roach                        }
2079a6f13a4aSGreg Roach                    }
2080a6f13a4aSGreg Roach                }
2081a6f13a4aSGreg Roach
208213abd6f3SGreg Roach                $this->list = [];
2083a6f13a4aSGreg Roach
208430fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20856b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2086a6f13a4aSGreg Roach                }
2087a6f13a4aSGreg Roach                break;
2088a6f13a4aSGreg Roach
2089a6f13a4aSGreg Roach            default:
20906ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2091a6f13a4aSGreg Roach        }
2092a6f13a4aSGreg Roach
209313abd6f3SGreg Roach        $filters  = [];
209413abd6f3SGreg Roach        $filters2 = [];
2095a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2096a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2097a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2098a6f13a4aSGreg Roach                    $condition = $value;
2099a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2100a6f13a4aSGreg Roach                        $id    = $match[1];
2101a6f13a4aSGreg Roach                        $value = "''";
2102044416d2SGreg Roach                        if ($id === 'ID') {
21037a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2104a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2105a6f13a4aSGreg Roach                            }
2106044416d2SGreg Roach                        } elseif ($id === 'fact') {
2107a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2108044416d2SGreg Roach                        } elseif ($id === 'desc') {
2109a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2110a6f13a4aSGreg Roach                        } else {
2111a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21127a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2113a6f13a4aSGreg Roach                            }
2114a6f13a4aSGreg Roach                        }
2115a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2116a6f13a4aSGreg Roach                    }
2117a6f13a4aSGreg Roach                    //-- handle regular expressions
2118a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2119a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2120a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2121a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2122a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2123d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2124a6f13a4aSGreg Roach                            $val = trim($val);
2125a6f13a4aSGreg Roach                        }
2126a6f13a4aSGreg Roach                        if ($val) {
21277a6ee1acSGreg Roach                            $searchstr = '';
21287a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2129a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2130a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2131a6f13a4aSGreg Roach                                $level = 1;
2132f71a7dedSGreg Roach                                $t = 'XXXX';
2133a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2134a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2135a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2136a6f13a4aSGreg Roach                                    }
2137a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2138044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21397a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2140a6f13a4aSGreg Roach                                    }
21417a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2142a6f13a4aSGreg Roach                                    $level++;
2143a6f13a4aSGreg Roach                                }
2144a6f13a4aSGreg Roach                            } else {
2145044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21467a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2147a6f13a4aSGreg Roach                                }
2148a6f13a4aSGreg Roach                                $t         = $tag;
21497a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2150a6f13a4aSGreg Roach                            }
2151a6f13a4aSGreg Roach                            switch ($expr) {
21527a6ee1acSGreg Roach                                case 'CONTAINS':
2153044416d2SGreg Roach                                    if ($t === 'PLAC') {
2154a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2155a6f13a4aSGreg Roach                                    } else {
2156a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2157a6f13a4aSGreg Roach                                    }
2158a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2159a6f13a4aSGreg Roach                                    break;
2160a6f13a4aSGreg Roach                                default:
2161c1010edaSGreg Roach                                    $filters2[] = [
2162c1010edaSGreg Roach                                        'tag'  => $tag,
2163c1010edaSGreg Roach                                        'expr' => $expr,
2164c1010edaSGreg Roach                                        'val'  => $val,
2165c1010edaSGreg Roach                                    ];
2166a6f13a4aSGreg Roach                                    break;
2167a6f13a4aSGreg Roach                            }
2168a6f13a4aSGreg Roach                        }
2169a6f13a4aSGreg Roach                    }
2170a6f13a4aSGreg Roach                }
2171a6f13a4aSGreg Roach            }
2172a6f13a4aSGreg Roach        }
2173a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2174a6f13a4aSGreg Roach        if ($filters) {
2175a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2176a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2177299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2178a6f13a4aSGreg Roach                        unset($this->list[$key]);
2179a6f13a4aSGreg Roach                        break;
2180a6f13a4aSGreg Roach                    }
2181a6f13a4aSGreg Roach                }
2182a6f13a4aSGreg Roach            }
2183a6f13a4aSGreg Roach        }
2184a6f13a4aSGreg Roach        if ($filters2) {
218513abd6f3SGreg Roach            $mylist = [];
2186a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2187c0935879SGreg Roach                $key  = $indi->xref();
2188299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2189a6f13a4aSGreg Roach                $keep = true;
2190a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2191a6f13a4aSGreg Roach                    if ($keep) {
2192a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2193a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2194a6f13a4aSGreg Roach                        $val  = $filter['val'];
2195b2448a1bSGreg Roach                        if ($val === "''") {
21967a6ee1acSGreg Roach                            $val = '';
2197a6f13a4aSGreg Roach                        }
21987a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2199a6f13a4aSGreg Roach                        $t    = end($tags);
22003d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2201a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2202044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
22037a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
22047a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2205a6f13a4aSGreg Roach                            $t    = end($tags);
22063d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2207a6f13a4aSGreg Roach                        }
2208a6f13a4aSGreg Roach
2209a6f13a4aSGreg Roach                        switch ($expr) {
22107a6ee1acSGreg Roach                            case 'GTE':
2211044416d2SGreg Roach                                if ($t === 'DATE') {
2212a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2213a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2214a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2215a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2216a6f13a4aSGreg Roach                                    $keep = true;
2217a6f13a4aSGreg Roach                                }
2218a6f13a4aSGreg Roach                                break;
22197a6ee1acSGreg Roach                            case 'LTE':
2220044416d2SGreg Roach                                if ($t === 'DATE') {
2221a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2222a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2223a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2224a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2225a6f13a4aSGreg Roach                                    $keep = true;
2226a6f13a4aSGreg Roach                                }
2227a6f13a4aSGreg Roach                                break;
2228a6f13a4aSGreg Roach                            default:
2229a6f13a4aSGreg Roach                                if ($v == $val) {
2230a6f13a4aSGreg Roach                                    $keep = true;
2231a6f13a4aSGreg Roach                                } else {
2232a6f13a4aSGreg Roach                                    $keep = false;
2233a6f13a4aSGreg Roach                                }
2234a6f13a4aSGreg Roach                                break;
2235a6f13a4aSGreg Roach                        }
2236a6f13a4aSGreg Roach                    }
2237a6f13a4aSGreg Roach                }
2238a6f13a4aSGreg Roach                if ($keep) {
2239a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2240a6f13a4aSGreg Roach                }
2241a6f13a4aSGreg Roach            }
2242a6f13a4aSGreg Roach            $this->list = $mylist;
2243a6f13a4aSGreg Roach        }
2244a6f13a4aSGreg Roach
2245a6f13a4aSGreg Roach        switch ($sortby) {
2246a6f13a4aSGreg Roach            case 'NAME':
2247c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2248a6f13a4aSGreg Roach                break;
2249a6f13a4aSGreg Roach            case 'CHAN':
2250c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2251a6f13a4aSGreg Roach                break;
2252a6f13a4aSGreg Roach            case 'BIRT:DATE':
2253c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2254a6f13a4aSGreg Roach                break;
2255a6f13a4aSGreg Roach            case 'DEAT:DATE':
2256c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2257a6f13a4aSGreg Roach                break;
2258a6f13a4aSGreg Roach            case 'MARR:DATE':
2259c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2260a6f13a4aSGreg Roach                break;
2261a6f13a4aSGreg Roach            default:
2262a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2263a6f13a4aSGreg Roach                break;
2264a6f13a4aSGreg Roach        }
2265a6f13a4aSGreg Roach
22669b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2267e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2268a6f13a4aSGreg Roach    }
2269a6f13a4aSGreg Roach
2270a6f13a4aSGreg Roach    /**
2271fab8f067SGreg Roach     * Handle </list>
22728ba2e626SGreg Roach     *
22738ba2e626SGreg Roach     * @return void
2274a6f13a4aSGreg Roach     */
2275b702978eSGreg Roach    protected function listEndHandler(): void
2276c1010edaSGreg Roach    {
2277a6f13a4aSGreg Roach        $this->process_repeats--;
2278a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2279a6f13a4aSGreg Roach            return;
2280a6f13a4aSGreg Roach        }
2281a6f13a4aSGreg Roach
2282a6f13a4aSGreg Roach        // Check if there is any list
2283a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2284a6f13a4aSGreg Roach            $lineoffset = 0;
2285a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2286a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2287a6f13a4aSGreg Roach            }
2288a6f13a4aSGreg Roach            //-- read the xml from the file
2289299d100dSGreg Roach            $lines = file($this->report);
2290dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2291a6f13a4aSGreg Roach                $lineoffset--;
2292a6f13a4aSGreg Roach            }
2293a6f13a4aSGreg Roach            $lineoffset++;
2294a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2295a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2296a6f13a4aSGreg Roach            // List Level counter
2297a6f13a4aSGreg Roach            $count = 1;
2298a6f13a4aSGreg Roach            while (0 < $count) {
2299dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2300a6f13a4aSGreg Roach                    $count++;
2301dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2302a6f13a4aSGreg Roach                    $count--;
2303a6f13a4aSGreg Roach                }
2304a6f13a4aSGreg Roach                if (0 < $count) {
2305a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2306a6f13a4aSGreg Roach                }
2307a6f13a4aSGreg Roach                $line_nr++;
2308a6f13a4aSGreg Roach            }
2309a6f13a4aSGreg Roach            // No need to drag this
2310a6f13a4aSGreg Roach            unset($lines);
23117a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2312a6f13a4aSGreg Roach            // Save original values
23139b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2314a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2315a6f13a4aSGreg Roach
2316a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2317a6f13a4aSGreg Roach            $this->list_private = 0;
2318a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2319a6f13a4aSGreg Roach                if ($record->canShow()) {
2320f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2321a6f13a4aSGreg Roach                    //-- start the sax parser
2322a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2323e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2324a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23251aa04befSGreg Roach
23261aa04befSGreg Roach                    xml_set_element_handler(
23271aa04befSGreg Roach                        $repeat_parser,
23289d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23291aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23301aa04befSGreg Roach                        },
23319d454b6bSGreg Roach                        function ($parser, string $name): void {
23321aa04befSGreg Roach                            $this->endElement($parser, $name);
23331aa04befSGreg Roach                        }
23341aa04befSGreg Roach                    );
23351aa04befSGreg Roach
23361aa04befSGreg Roach                    xml_set_character_data_handler(
23371aa04befSGreg Roach                        $repeat_parser,
23389d454b6bSGreg Roach                        function ($parser, string $data): void {
23391aa04befSGreg Roach                            $this->characterData($parser, $data);
23401aa04befSGreg Roach                        }
23411aa04befSGreg Roach                    );
23421aa04befSGreg Roach
2343a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23446ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2345a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2346a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2347a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2348a6f13a4aSGreg Roach                        ));
2349a6f13a4aSGreg Roach                    }
2350a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2351a6f13a4aSGreg Roach                } else {
2352a6f13a4aSGreg Roach                    $this->list_private++;
2353a6f13a4aSGreg Roach                }
2354a6f13a4aSGreg Roach            }
235513abd6f3SGreg Roach            $this->list   = [];
2356e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2357a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2358a6f13a4aSGreg Roach        }
235965e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2360a6f13a4aSGreg Roach    }
2361a6f13a4aSGreg Roach
2362a6f13a4aSGreg Roach    /**
2363fab8f067SGreg Roach     * Handle <listTotal>
2364a6f13a4aSGreg Roach     * Prints the total number of records in a list
2365fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23668ba2e626SGreg Roach     *
23678ba2e626SGreg Roach     * @return void
2368a6f13a4aSGreg Roach     */
2369b702978eSGreg Roach    protected function listTotalStartHandler(): void
2370c1010edaSGreg Roach    {
2371a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2372589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2373a6f13a4aSGreg Roach        } else {
23747a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2375a6f13a4aSGreg Roach        }
2376a6f13a4aSGreg Roach    }
2377a6f13a4aSGreg Roach
2378a6f13a4aSGreg Roach    /**
2379fab8f067SGreg Roach     * Handle <relatives>
238076692c8bSGreg Roach     *
2381fab8f067SGreg Roach     * @param string[] $attrs
23828ba2e626SGreg Roach     *
23838ba2e626SGreg Roach     * @return void
2384a6f13a4aSGreg Roach     */
2385b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2386c1010edaSGreg Roach    {
2387a6f13a4aSGreg Roach        $this->process_repeats++;
2388a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2389a6f13a4aSGreg Roach            return;
2390a6f13a4aSGreg Roach        }
2391a6f13a4aSGreg Roach
2392e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2393e364afe4SGreg Roach
239413abd6f3SGreg Roach        $match = [];
2395a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2396d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2397a6f13a4aSGreg Roach            $sortby = trim($sortby);
2398a6f13a4aSGreg Roach        }
2399a6f13a4aSGreg Roach
2400a6f13a4aSGreg Roach        $maxgen = -1;
2401a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2402c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2403a6f13a4aSGreg Roach        }
2404a6f13a4aSGreg Roach
2405e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2406e364afe4SGreg Roach
2407a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2408d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2409a6f13a4aSGreg Roach            $group = trim($group);
2410a6f13a4aSGreg Roach        }
2411a6f13a4aSGreg Roach
2412e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2413e364afe4SGreg Roach
2414a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2415d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2416a6f13a4aSGreg Roach            $id = trim($id);
2417a6f13a4aSGreg Roach        }
2418a6f13a4aSGreg Roach
241913abd6f3SGreg Roach        $this->list = [];
24206b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2421d965cc1aSGreg Roach        if ($person instanceof Individual) {
2422a6f13a4aSGreg Roach            $this->list[$id] = $person;
2423a6f13a4aSGreg Roach            switch ($group) {
24247a6ee1acSGreg Roach                case 'child-family':
242539ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2426820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2427820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2428a6f13a4aSGreg Roach                        }
2429820b62dfSGreg Roach
2430820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2431c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2432a6f13a4aSGreg Roach                        }
2433a6f13a4aSGreg Roach                    }
2434a6f13a4aSGreg Roach                    break;
24357a6ee1acSGreg Roach                case 'spouse-family':
243639ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2437820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2438820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2439a6f13a4aSGreg Roach                        }
2440820b62dfSGreg Roach
2441820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2442c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2443a6f13a4aSGreg Roach                        }
2444a6f13a4aSGreg Roach                    }
2445a6f13a4aSGreg Roach                    break;
24467a6ee1acSGreg Roach                case 'direct-ancestors':
24473d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2448a6f13a4aSGreg Roach                    break;
24497a6ee1acSGreg Roach                case 'ancestors':
24503d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2451a6f13a4aSGreg Roach                    break;
24527a6ee1acSGreg Roach                case 'descendants':
2453a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24543d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2455a6f13a4aSGreg Roach                    break;
24567a6ee1acSGreg Roach                case 'all':
24573d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24583d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2459a6f13a4aSGreg Roach                    break;
2460a6f13a4aSGreg Roach            }
2461a6f13a4aSGreg Roach        }
2462a6f13a4aSGreg Roach
2463a6f13a4aSGreg Roach        switch ($sortby) {
2464a6f13a4aSGreg Roach            case 'NAME':
2465c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2466a6f13a4aSGreg Roach                break;
2467a6f13a4aSGreg Roach            case 'BIRT:DATE':
2468c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2469a6f13a4aSGreg Roach                break;
2470a6f13a4aSGreg Roach            case 'DEAT:DATE':
2471c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2472a6f13a4aSGreg Roach                break;
2473a6f13a4aSGreg Roach            case 'generation':
247413abd6f3SGreg Roach                $newarray = [];
2475a6f13a4aSGreg Roach                reset($this->list);
2476a6f13a4aSGreg Roach                $genCounter = 1;
2477a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2478a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2479a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2480a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
248179529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2482a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2483a6f13a4aSGreg Roach                        }
2484a6f13a4aSGreg Roach                    }
2485a6f13a4aSGreg Roach                    $genCounter++;
2486a6f13a4aSGreg Roach                }
2487a6f13a4aSGreg Roach                $this->list = $newarray;
2488a6f13a4aSGreg Roach                break;
2489a6f13a4aSGreg Roach            default:
2490a6f13a4aSGreg Roach                // unsorted
2491a6f13a4aSGreg Roach                break;
2492a6f13a4aSGreg Roach        }
24939b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2494e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2495a6f13a4aSGreg Roach    }
2496a6f13a4aSGreg Roach
2497a6f13a4aSGreg Roach    /**
2498fab8f067SGreg Roach     * Handle </relatives>
24998ba2e626SGreg Roach     *
25008ba2e626SGreg Roach     * @return void
2501a6f13a4aSGreg Roach     */
2502b702978eSGreg Roach    protected function relativesEndHandler(): void
2503c1010edaSGreg Roach    {
2504a6f13a4aSGreg Roach        $this->process_repeats--;
2505a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2506a6f13a4aSGreg Roach            return;
2507a6f13a4aSGreg Roach        }
2508a6f13a4aSGreg Roach
2509a6f13a4aSGreg Roach        // Check if there is any relatives
2510a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2511a6f13a4aSGreg Roach            $lineoffset = 0;
2512a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2513a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2514a6f13a4aSGreg Roach            }
2515a6f13a4aSGreg Roach            //-- read the xml from the file
2516299d100dSGreg Roach            $lines = file($this->report);
2517dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2518a6f13a4aSGreg Roach                $lineoffset--;
2519a6f13a4aSGreg Roach            }
2520a6f13a4aSGreg Roach            $lineoffset++;
2521a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2522a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2523a6f13a4aSGreg Roach            // Relatives Level counter
2524a6f13a4aSGreg Roach            $count = 1;
2525a6f13a4aSGreg Roach            while (0 < $count) {
2526dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2527a6f13a4aSGreg Roach                    $count++;
2528dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2529a6f13a4aSGreg Roach                    $count--;
2530a6f13a4aSGreg Roach                }
2531a6f13a4aSGreg Roach                if (0 < $count) {
2532a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2533a6f13a4aSGreg Roach                }
2534a6f13a4aSGreg Roach                $line_nr++;
2535a6f13a4aSGreg Roach            }
2536a6f13a4aSGreg Roach            // No need to drag this
2537a6f13a4aSGreg Roach            unset($lines);
2538a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2539a6f13a4aSGreg Roach            // Save original values
25409b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2541a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2542a6f13a4aSGreg Roach
2543a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2544a6f13a4aSGreg Roach            $this->list_private = 0;
2545b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2546a6f13a4aSGreg Roach                if (isset($value->generation)) {
2547a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2548a6f13a4aSGreg Roach                }
25496b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2550299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2551a6f13a4aSGreg Roach
2552a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2553e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2554a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25551aa04befSGreg Roach
25561aa04befSGreg Roach                xml_set_element_handler(
25571aa04befSGreg Roach                    $repeat_parser,
25589d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25591aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25601aa04befSGreg Roach                    },
25619d454b6bSGreg Roach                    function ($parser, string $name): void {
25621aa04befSGreg Roach                        $this->endElement($parser, $name);
25631aa04befSGreg Roach                    }
25641aa04befSGreg Roach                );
25651aa04befSGreg Roach
25661aa04befSGreg Roach                xml_set_character_data_handler(
25671aa04befSGreg Roach                    $repeat_parser,
25689d454b6bSGreg Roach                    function ($parser, string $data): void {
25691aa04befSGreg Roach                        $this->characterData($parser, $data);
25701aa04befSGreg Roach                    }
25711aa04befSGreg Roach                );
2572a6f13a4aSGreg Roach
2573a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25746ccdf4f0SGreg 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)));
2575a6f13a4aSGreg Roach                }
2576a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2577a6f13a4aSGreg Roach            }
2578a6f13a4aSGreg Roach            // Clean up the list array
257913abd6f3SGreg Roach            $this->list   = [];
2580e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2581a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2582a6f13a4aSGreg Roach        }
258365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2584a6f13a4aSGreg Roach    }
2585a6f13a4aSGreg Roach
2586a6f13a4aSGreg Roach    /**
2587fab8f067SGreg Roach     * Handle <generation />
2588a6f13a4aSGreg Roach     * Prints the number of generations
25898ba2e626SGreg Roach     *
25908ba2e626SGreg Roach     * @return void
2591a6f13a4aSGreg Roach     */
2592b702978eSGreg Roach    protected function generationStartHandler(): void
2593c1010edaSGreg Roach    {
2594589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2595a6f13a4aSGreg Roach    }
2596a6f13a4aSGreg Roach
2597a6f13a4aSGreg Roach    /**
2598fab8f067SGreg Roach     * Handle <newPage />
2599a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
26008ba2e626SGreg Roach     *
26018ba2e626SGreg Roach     * @return void
2602a6f13a4aSGreg Roach     */
2603b702978eSGreg Roach    protected function newPageStartHandler(): void
2604c1010edaSGreg Roach    {
26057a6ee1acSGreg Roach        $temp = 'addpage';
2606e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2607a6f13a4aSGreg Roach    }
2608a6f13a4aSGreg Roach
2609a6f13a4aSGreg Roach    /**
2610fab8f067SGreg Roach     * Handle </title>
26118ba2e626SGreg Roach     *
26128ba2e626SGreg Roach     * @return void
2613a6f13a4aSGreg Roach     */
2614b702978eSGreg Roach    protected function titleEndHandler(): void
2615c1010edaSGreg Roach    {
26162836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2617a6f13a4aSGreg Roach    }
2618a6f13a4aSGreg Roach
2619a6f13a4aSGreg Roach    /**
2620fab8f067SGreg Roach     * Handle </description>
26218ba2e626SGreg Roach     *
26228ba2e626SGreg Roach     * @return void
2623a6f13a4aSGreg Roach     */
2624b702978eSGreg Roach    protected function descriptionEndHandler(): void
2625c1010edaSGreg Roach    {
26262836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2627a6f13a4aSGreg Roach    }
2628729ce104SGreg Roach
2629729ce104SGreg Roach    /**
263076692c8bSGreg Roach     * Create a list of all descendants.
263176692c8bSGreg Roach     *
2632729ce104SGreg Roach     * @param string[] $list
2633729ce104SGreg Roach     * @param string   $pid
2634729ce104SGreg Roach     * @param bool     $parents
2635729ce104SGreg Roach     * @param int      $generations
26368ba2e626SGreg Roach     *
26378ba2e626SGreg Roach     * @return void
2638729ce104SGreg Roach     */
26393b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2640c1010edaSGreg Roach    {
26416b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2642729ce104SGreg Roach        if ($person === null) {
2643729ce104SGreg Roach            return;
2644729ce104SGreg Roach        }
2645729ce104SGreg Roach        if (!isset($list[$pid])) {
2646729ce104SGreg Roach            $list[$pid] = $person;
2647729ce104SGreg Roach        }
2648729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2649729ce104SGreg Roach            $list[$pid]->generation = 0;
2650729ce104SGreg Roach        }
265139ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2652729ce104SGreg Roach            if ($parents) {
265339ca88baSGreg Roach                $husband = $family->husband();
265439ca88baSGreg Roach                $wife    = $family->wife();
2655729ce104SGreg Roach                if ($husband) {
2656c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2657729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2658c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2659729ce104SGreg Roach                    } else {
2660c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2661729ce104SGreg Roach                    }
2662729ce104SGreg Roach                }
2663729ce104SGreg Roach                if ($wife) {
2664c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2665729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2666c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2667729ce104SGreg Roach                    } else {
2668c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2669729ce104SGreg Roach                    }
2670729ce104SGreg Roach                }
2671729ce104SGreg Roach            }
2672820b62dfSGreg Roach
267339ca88baSGreg Roach            $children = $family->children();
2674820b62dfSGreg Roach
2675729ce104SGreg Roach            foreach ($children as $child) {
2676729ce104SGreg Roach                if ($child) {
2677c0935879SGreg Roach                    $list[$child->xref()] = $child;
2678820b62dfSGreg Roach
2679729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2680c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2681729ce104SGreg Roach                    } else {
2682c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2683729ce104SGreg Roach                    }
2684729ce104SGreg Roach                }
2685729ce104SGreg Roach            }
2686729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2687729ce104SGreg Roach                foreach ($children as $child) {
2688c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2689729ce104SGreg Roach                }
2690729ce104SGreg Roach            }
2691729ce104SGreg Roach        }
2692729ce104SGreg Roach    }
2693729ce104SGreg Roach
2694729ce104SGreg Roach    /**
269576692c8bSGreg Roach     * Create a list of all ancestors.
269676692c8bSGreg Roach     *
26970cc533a6SGreg Roach     * @param stdClass[] $list
2698729ce104SGreg Roach     * @param string     $pid
2699729ce104SGreg Roach     * @param bool       $children
2700729ce104SGreg Roach     * @param int        $generations
27018ba2e626SGreg Roach     *
27028ba2e626SGreg Roach     * @return void
2703729ce104SGreg Roach     */
27043b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2705c1010edaSGreg Roach    {
270613abd6f3SGreg Roach        $genlist                = [$pid];
2707729ce104SGreg Roach        $list[$pid]->generation = 1;
2708729ce104SGreg Roach        while (count($genlist) > 0) {
2709729ce104SGreg Roach            $id = array_shift($genlist);
2710dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2711729ce104SGreg Roach                continue; // id can be something like “empty7”
2712729ce104SGreg Roach            }
27136b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
271439ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
271539ca88baSGreg Roach                $husband = $family->husband();
271639ca88baSGreg Roach                $wife    = $family->wife();
2717729ce104SGreg Roach                if ($husband) {
2718c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2719c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2720729ce104SGreg Roach                }
2721729ce104SGreg Roach                if ($wife) {
2722c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2723c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2724729ce104SGreg Roach                }
2725729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2726729ce104SGreg Roach                    if ($husband) {
2727c0935879SGreg Roach                        $genlist[] = $husband->xref();
2728729ce104SGreg Roach                    }
2729729ce104SGreg Roach                    if ($wife) {
2730c0935879SGreg Roach                        $genlist[] = $wife->xref();
2731729ce104SGreg Roach                    }
2732729ce104SGreg Roach                }
2733729ce104SGreg Roach                if ($children) {
273439ca88baSGreg Roach                    foreach ($family->children() as $child) {
2735c0935879SGreg Roach                        $list[$child->xref()] = $child;
2736e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2737729ce104SGreg Roach                    }
2738729ce104SGreg Roach                }
2739729ce104SGreg Roach            }
2740729ce104SGreg Roach        }
2741729ce104SGreg Roach    }
2742729ce104SGreg Roach
2743729ce104SGreg Roach    /**
2744729ce104SGreg Roach     * get gedcom tag value
2745729ce104SGreg Roach     *
2746729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2747729ce104SGreg 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
2748729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2749729ce104SGreg Roach     *
2750729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2751729ce104SGreg Roach     */
2752b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2753c1010edaSGreg Roach    {
2754b2448a1bSGreg Roach        if ($gedrec === '') {
2755729ce104SGreg Roach            return '';
2756729ce104SGreg Roach        }
2757729ce104SGreg Roach        $tags      = explode(':', $tag);
2758729ce104SGreg Roach        $origlevel = $level;
2759b2448a1bSGreg Roach        if ($level === 0) {
27603c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2761729ce104SGreg Roach        }
2762729ce104SGreg Roach
2763729ce104SGreg Roach        $subrec = $gedrec;
2764f71a7dedSGreg Roach        $t = 'XXXX';
2765729ce104SGreg Roach        foreach ($tags as $t) {
2766729ce104SGreg Roach            $lastsubrec = $subrec;
27673d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2768729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2769729ce104SGreg Roach                $level--;
27703d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2771729ce104SGreg Roach            }
2772729ce104SGreg Roach            if (empty($subrec)) {
2773044416d2SGreg Roach                if ($t === 'TITL') {
27743d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2775729ce104SGreg Roach                    if (!empty($subrec)) {
27767a6ee1acSGreg Roach                        $t = 'ABBR';
2777729ce104SGreg Roach                    }
2778729ce104SGreg Roach                }
2779b2448a1bSGreg Roach                if ($subrec === '') {
2780729ce104SGreg Roach                    if ($level > 0) {
2781729ce104SGreg Roach                        $level--;
2782729ce104SGreg Roach                    }
27833d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2784b2448a1bSGreg Roach                    if ($subrec === '') {
2785729ce104SGreg Roach                        return '';
2786729ce104SGreg Roach                    }
2787729ce104SGreg Roach                }
2788729ce104SGreg Roach            }
2789729ce104SGreg Roach            $level++;
2790729ce104SGreg Roach        }
2791729ce104SGreg Roach        $level--;
2792729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2793f71a7dedSGreg Roach        if ($ct === 0) {
2794729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2795729ce104SGreg Roach        }
2796f71a7dedSGreg Roach        if ($ct === 0) {
2797729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2798729ce104SGreg Roach        }
2799729ce104SGreg Roach        if ($ct > 0) {
2800729ce104SGreg Roach            $value = trim($match[1]);
2801044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
28026b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2803ff166e64SGreg Roach                if ($note instanceof Note) {
2804729ce104SGreg Roach                    $value = $note->getNote();
2805729ce104SGreg Roach                } else {
2806729ce104SGreg Roach                    //-- set the value to the id without the @
2807729ce104SGreg Roach                    $value = $match[1];
2808729ce104SGreg Roach                }
2809729ce104SGreg Roach            }
2810f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
28113d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2812729ce104SGreg Roach            }
2813729ce104SGreg Roach
2814729ce104SGreg Roach            return $value;
2815729ce104SGreg Roach        }
2816729ce104SGreg Roach
28177a6ee1acSGreg Roach        return '';
2818729ce104SGreg Roach    }
2819d1286247SGreg Roach
2820d1286247SGreg Roach    /**
2821d1286247SGreg Roach     * Replace variable identifiers with their values.
2822d1286247SGreg Roach     *
2823d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
282482759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2825d1286247SGreg Roach     *
2826d1286247SGreg Roach     * @return string
2827d1286247SGreg Roach     */
28288f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2829c1010edaSGreg Roach    {
2830d1286247SGreg Roach        return preg_replace_callback(
2831d1286247SGreg Roach            '/\$(\w+)/',
283218d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28332118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
283482759250SGreg Roach                    if ($quote) {
28352118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2836b2ce94c6SRico Sonntag                    }
2837b2ce94c6SRico Sonntag
28382118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
283982759250SGreg Roach                }
2840b2ce94c6SRico Sonntag
2841d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28423d7a8a4cSGreg Roach
2843d1286247SGreg Roach                return '$' . $matches[1];
2844d1286247SGreg Roach            },
2845d1286247SGreg Roach            $expression
2846d1286247SGreg Roach        );
2847d1286247SGreg Roach    }
2848a6f13a4aSGreg Roach}
2849