xref: /webtrees/app/Report/ReportParserGenerate.php (revision 97def6bce3212579328010dabdb3cfd539e566f6)
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;
26*97def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
27a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
283d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
299a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
33d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
34a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
35729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
36a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
37d0889c63SGreg Roachuse Fisharebest\Webtrees\Registry;
38299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
39195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
40195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
41a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
425985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
439a9e551aSGreg Roachuse Illuminate\Support\Str;
44f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
4531d9777aSGreg Roachuse LogicException;
4679529c87SGreg Roachuse stdClass;
47c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
485809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
4959597b37SGreg Roach
50b6f35a76SGreg Roachuse function addcslashes;
51b6f35a76SGreg Roachuse function addslashes;
52b6f35a76SGreg Roachuse function array_pop;
53b6f35a76SGreg Roachuse function array_shift;
54b6f35a76SGreg Roachuse function assert;
55b19e047dSGreg Roachuse function call_user_func;
56b6f35a76SGreg Roachuse function count;
57b6f35a76SGreg Roachuse function end;
58b2448a1bSGreg Roachuse function explode;
59b6f35a76SGreg Roachuse function file;
60b6f35a76SGreg Roachuse function file_exists;
61b6f35a76SGreg Roachuse function getimagesize;
6229518ad2SGreg Roachuse function imagecreatefromstring;
6329518ad2SGreg Roachuse function imagesx;
6429518ad2SGreg Roachuse function imagesy;
65b6f35a76SGreg Roachuse function in_array;
66b19e047dSGreg Roachuse function method_exists;
67b6f35a76SGreg Roachuse function preg_match;
68b6f35a76SGreg Roachuse function preg_match_all;
69b6f35a76SGreg Roachuse function preg_replace;
70b6f35a76SGreg Roachuse function preg_replace_callback;
71b6f35a76SGreg Roachuse function preg_split;
72b6f35a76SGreg Roachuse function reset;
73b6f35a76SGreg Roachuse function round;
74b6f35a76SGreg Roachuse function sprintf;
75dec352c1SGreg Roachuse function str_contains;
76b6f35a76SGreg Roachuse function str_replace;
77dec352c1SGreg Roachuse function str_starts_with;
78b6f35a76SGreg Roachuse function strip_tags;
79b6f35a76SGreg Roachuse function strlen;
80b6f35a76SGreg Roachuse function strtoupper;
81b6f35a76SGreg Roachuse function substr;
82b2448a1bSGreg Roachuse function trim;
83b6f35a76SGreg Roachuse function uasort;
84b6f35a76SGreg Roachuse function xml_error_string;
85b6f35a76SGreg Roachuse function xml_get_current_line_number;
86b6f35a76SGreg Roachuse function xml_get_error_code;
87b6f35a76SGreg Roachuse function xml_parse;
88b6f35a76SGreg Roachuse function xml_parser_create;
89b6f35a76SGreg Roachuse function xml_parser_free;
90b6f35a76SGreg Roachuse function xml_parser_set_option;
91b6f35a76SGreg Roachuse function xml_set_character_data_handler;
92b6f35a76SGreg Roachuse function xml_set_element_handler;
93b6f35a76SGreg Roach
94b6f35a76SGreg Roachuse const PREG_SET_ORDER;
95b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9629518ad2SGreg Roach
97a6f13a4aSGreg Roach/**
98a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
99a6f13a4aSGreg Roach */
100c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
101c1010edaSGreg Roach{
102a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
103a6f13a4aSGreg Roach    private $process_footnote = true;
104a6f13a4aSGreg Roach
105a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
106a6f13a4aSGreg Roach    private $print_data = false;
107a6f13a4aSGreg Roach
108a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
10913abd6f3SGreg Roach    private $print_data_stack = [];
110a6f13a4aSGreg Roach
11176692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
112a6f13a4aSGreg Roach    private $process_gedcoms = 0;
113a6f13a4aSGreg Roach
11476692c8bSGreg Roach    /** @var int Are we processing conditionals */
115a6f13a4aSGreg Roach    private $process_ifs = 0;
116a6f13a4aSGreg Roach
11776692c8bSGreg Roach    /** @var int Are we processing repeats */
118a6f13a4aSGreg Roach    private $process_repeats = 0;
119a6f13a4aSGreg Roach
120a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
121a6f13a4aSGreg Roach    private $repeat_bytes = 0;
122a6f13a4aSGreg Roach
1235b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
12413abd6f3SGreg Roach    private $repeats = [];
125a6f13a4aSGreg Roach
126a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
12713abd6f3SGreg Roach    private $repeats_stack = [];
128a6f13a4aSGreg Roach
129b6f35a76SGreg Roach    /** @var AbstractRenderer[] Nested repeating data */
13013abd6f3SGreg Roach    private $wt_report_stack = [];
131e8e7866bSGreg Roach
132e8e7866bSGreg Roach    /** @var resource Nested repeating data */
133e8e7866bSGreg Roach    private $parser;
134e8e7866bSGreg Roach
135e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
13613abd6f3SGreg Roach    private $parser_stack = [];
137e8e7866bSGreg Roach
138a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
139a6f13a4aSGreg Roach    private $gedrec = '';
140a6f13a4aSGreg Roach
141a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
14213abd6f3SGreg Roach    private $gedrec_stack = [];
143a6f13a4aSGreg Roach
144a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
145a6f13a4aSGreg Roach    private $current_element;
146a6f13a4aSGreg Roach
147a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
148a6f13a4aSGreg Roach    private $footnote_element;
149a6f13a4aSGreg Roach
150a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
151a6f13a4aSGreg Roach    private $fact = '';
152a6f13a4aSGreg Roach
153a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
154a6f13a4aSGreg Roach    private $desc = '';
155a6f13a4aSGreg Roach
156a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
157a6f13a4aSGreg Roach    private $type = '';
158a6f13a4aSGreg Roach
159a6f13a4aSGreg Roach    /** @var int The current generational level */
160a6f13a4aSGreg Roach    private $generation = 1;
161a6f13a4aSGreg Roach
162a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
16313abd6f3SGreg Roach    private $list = [];
164a6f13a4aSGreg Roach
165a6f13a4aSGreg Roach    /** @var int Number of items in lists */
166a6f13a4aSGreg Roach    private $list_total = 0;
167a6f13a4aSGreg Roach
168a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
169a6f13a4aSGreg Roach    private $list_private = 0;
170a6f13a4aSGreg Roach
171299d100dSGreg Roach    /** @var string The filename of the XML report */
172299d100dSGreg Roach    protected $report;
173299d100dSGreg Roach
174b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
175e8e7866bSGreg Roach    private $report_root;
176e8e7866bSGreg Roach
177b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
178e8e7866bSGreg Roach    private $wt_report;
179e8e7866bSGreg Roach
180d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1812118c0e3SGreg Roach    private $vars;
182d1286247SGreg Roach
183299d100dSGreg Roach    /** @var Tree The current tree */
184299d100dSGreg Roach    private $tree;
185299d100dSGreg Roach
186f7cf8a15SGreg Roach    /** @var FilesystemOperator */
187a04bb9a2SGreg Roach    private $data_filesystem;
188a04bb9a2SGreg Roach
18976692c8bSGreg Roach    /**
19076692c8bSGreg Roach     * Create a parser for a report
19176692c8bSGreg Roach     *
19276692c8bSGreg Roach     * @param string             $report The XML filename
193b6f35a76SGreg Roach     * @param AbstractRenderer   $report_root
19476692c8bSGreg Roach     * @param string[][]         $vars
195299d100dSGreg Roach     * @param Tree               $tree
196f7cf8a15SGreg Roach     * @param FilesystemOperator $data_filesystem
19776692c8bSGreg Roach     */
198a04bb9a2SGreg Roach    public function __construct(
199a04bb9a2SGreg Roach        string $report,
200b6f35a76SGreg Roach        AbstractRenderer $report_root,
201a04bb9a2SGreg Roach        array $vars,
202a04bb9a2SGreg Roach        Tree $tree,
203f7cf8a15SGreg Roach        FilesystemOperator $data_filesystem
204a04bb9a2SGreg Roach    ) {
205299d100dSGreg Roach        $this->report          = $report;
206e8e7866bSGreg Roach        $this->report_root     = $report_root;
207e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20859f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
209d1286247SGreg Roach        $this->vars            = $vars;
210299d100dSGreg Roach        $this->tree            = $tree;
211a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
212299d100dSGreg Roach
21376f666f4SGreg Roach        parent::__construct($report);
214a6f13a4aSGreg Roach    }
215a6f13a4aSGreg Roach
216a6f13a4aSGreg Roach    /**
217a6f13a4aSGreg Roach     * XML start element handler
218a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
219a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
220a6f13a4aSGreg Roach     *
221a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
222a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
2238a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
22418d7a90dSGreg Roach     *
22518d7a90dSGreg Roach     * @return void
226a6f13a4aSGreg Roach     */
227af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
228c1010edaSGreg Roach    {
22913abd6f3SGreg Roach        $newattrs = [];
230a6f13a4aSGreg Roach
231a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
232a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
233e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
234d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
235a6f13a4aSGreg Roach                }
236a6f13a4aSGreg Roach            }
237a6f13a4aSGreg Roach            $newattrs[$key] = $value;
238a6f13a4aSGreg Roach        }
239a6f13a4aSGreg Roach        $attrs = $newattrs;
2407a6ee1acSGreg 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')) {
241a0e2939aSGreg Roach            $method = $name . 'StartHandler';
242208e9f76SGreg Roach
243a0e2939aSGreg Roach            if (method_exists($this, $method)) {
244b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
245a6f13a4aSGreg Roach            }
246a6f13a4aSGreg Roach        }
247a6f13a4aSGreg Roach    }
248a6f13a4aSGreg Roach
249a6f13a4aSGreg Roach    /**
250a6f13a4aSGreg Roach     * XML end element handler
251a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
252a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
253a6f13a4aSGreg Roach     *
254a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
255a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
25618d7a90dSGreg Roach     *
25718d7a90dSGreg Roach     * @return void
258a6f13a4aSGreg Roach     */
259af14d238SGreg Roach    protected function endElement($parser, string $name): void
260c1010edaSGreg Roach    {
2617a6ee1acSGreg 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')) {
262a0e2939aSGreg Roach            $method = $name . 'EndHandler';
263a0e2939aSGreg Roach
264a0e2939aSGreg Roach            if (method_exists($this, $method)) {
265b19e047dSGreg Roach                call_user_func([$this, $method]);
266a6f13a4aSGreg Roach            }
267a6f13a4aSGreg Roach        }
268a6f13a4aSGreg Roach    }
269a6f13a4aSGreg Roach
270a6f13a4aSGreg Roach    /**
271a6f13a4aSGreg Roach     * XML character data handler
272a6f13a4aSGreg Roach     *
273a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
274a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
27518d7a90dSGreg Roach     *
27618d7a90dSGreg Roach     * @return void
277a6f13a4aSGreg Roach     */
27824f2a3afSGreg Roach    protected function characterData($parser, string $data): void
279c1010edaSGreg Roach    {
280e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
281a6f13a4aSGreg Roach            $this->current_element->addText($data);
282a6f13a4aSGreg Roach        }
283a6f13a4aSGreg Roach    }
284a6f13a4aSGreg Roach
285a6f13a4aSGreg Roach    /**
286fab8f067SGreg Roach     * Handle <style>
287a6f13a4aSGreg Roach     *
288fab8f067SGreg Roach     * @param string[] $attrs
2898ba2e626SGreg Roach     *
2908ba2e626SGreg Roach     * @return void
291a6f13a4aSGreg Roach     */
292b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
293c1010edaSGreg Roach    {
294a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2956ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
296a6f13a4aSGreg Roach        }
297a6f13a4aSGreg Roach
298a6f13a4aSGreg Roach        // array Style that will be passed on
29913abd6f3SGreg Roach        $s = [];
300a6f13a4aSGreg Roach
301a6f13a4aSGreg Roach        // string Name af the style
302a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
303a6f13a4aSGreg Roach
304a6f13a4aSGreg Roach        // string Name of the DEFAULT font
305208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
306a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
307a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
308a6f13a4aSGreg Roach        }
309a6f13a4aSGreg Roach
310a6f13a4aSGreg Roach        // int The size of the font in points
311208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
312a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
31359597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
31459597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
31559597b37SGreg Roach        }
316a6f13a4aSGreg Roach
317a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3187a6ee1acSGreg Roach        $s['style'] = '';
319a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
320a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
321a6f13a4aSGreg Roach        }
322a6f13a4aSGreg Roach
323e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
324a6f13a4aSGreg Roach    }
325a6f13a4aSGreg Roach
326a6f13a4aSGreg Roach    /**
327fab8f067SGreg Roach     * Handle <doc>
328a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
329a6f13a4aSGreg Roach     *
330fab8f067SGreg Roach     * @param string[] $attrs
3318ba2e626SGreg Roach     *
3328ba2e626SGreg Roach     * @return void
333a6f13a4aSGreg Roach     */
334b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
335c1010edaSGreg Roach    {
336e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
337a6f13a4aSGreg Roach
338a6f13a4aSGreg Roach        // Custom page width
339a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
340208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
341a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
342a6f13a4aSGreg Roach        // Custom Page height
343a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
344208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
345a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
346a6f13a4aSGreg Roach
347a6f13a4aSGreg Roach        // Left Margin
348a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
3497a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
350208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
351a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
352208e9f76SGreg 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))
353a6f13a4aSGreg Roach            }
354a6f13a4aSGreg Roach        }
355a6f13a4aSGreg Roach        // Right Margin
356a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3577a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
358208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
359a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
360208e9f76SGreg 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))
361a6f13a4aSGreg Roach            }
362a6f13a4aSGreg Roach        }
363a6f13a4aSGreg Roach        // Top Margin
364a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3657a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
366208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
367a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
368208e9f76SGreg 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))
369a6f13a4aSGreg Roach            }
370a6f13a4aSGreg Roach        }
371a6f13a4aSGreg Roach        // Bottom Margin
372a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3737a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
374208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
375a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
376208e9f76SGreg 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))
377a6f13a4aSGreg Roach            }
378a6f13a4aSGreg Roach        }
379a6f13a4aSGreg Roach        // Header Margin
380a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3817a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
382208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
383a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
384208e9f76SGreg 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))
385a6f13a4aSGreg Roach            }
386a6f13a4aSGreg Roach        }
387a6f13a4aSGreg Roach        // Footer Margin
388a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3897a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
390208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
391a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
392208e9f76SGreg 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))
393a6f13a4aSGreg Roach            }
394a6f13a4aSGreg Roach        }
395a6f13a4aSGreg Roach
396a6f13a4aSGreg Roach        // Page Orientation
397a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
398044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3997a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
400044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4017a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
402a6f13a4aSGreg Roach            }
403a6f13a4aSGreg Roach        }
404a6f13a4aSGreg Roach        // Page Size
405a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
406208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
407a6f13a4aSGreg Roach        }
408a6f13a4aSGreg Roach
409a6f13a4aSGreg Roach        // Show Generated By...
410a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4117a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
412208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4137a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
414208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
415a6f13a4aSGreg Roach            }
416a6f13a4aSGreg Roach        }
417a6f13a4aSGreg Roach
418e8e7866bSGreg Roach        $this->wt_report->setup();
419a6f13a4aSGreg Roach    }
420a6f13a4aSGreg Roach
421a6f13a4aSGreg Roach    /**
422fab8f067SGreg Roach     * Handle </doc>
4238ba2e626SGreg Roach     *
4248ba2e626SGreg Roach     * @return void
425a6f13a4aSGreg Roach     */
426b702978eSGreg Roach    protected function docEndHandler(): void
427c1010edaSGreg Roach    {
428e8e7866bSGreg Roach        $this->wt_report->run();
429a6f13a4aSGreg Roach    }
430a6f13a4aSGreg Roach
431a6f13a4aSGreg Roach    /**
432fab8f067SGreg Roach     * Handle <header>
4338ba2e626SGreg Roach     *
4348ba2e626SGreg Roach     * @return void
435a6f13a4aSGreg Roach     */
436b702978eSGreg Roach    protected function headerStartHandler(): void
437c1010edaSGreg Roach    {
438a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
439e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4407a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
441a6f13a4aSGreg Roach    }
442a6f13a4aSGreg Roach
443a6f13a4aSGreg Roach    /**
444fab8f067SGreg Roach     * Handle <body>
4458ba2e626SGreg Roach     *
4468ba2e626SGreg Roach     * @return void
447a6f13a4aSGreg Roach     */
448b702978eSGreg Roach    protected function bodyStartHandler(): void
449c1010edaSGreg Roach    {
4507a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
451a6f13a4aSGreg Roach    }
452a6f13a4aSGreg Roach
453a6f13a4aSGreg Roach    /**
454fab8f067SGreg Roach     * Handle <footer>
4558ba2e626SGreg Roach     *
4568ba2e626SGreg Roach     * @return void
457a6f13a4aSGreg Roach     */
458b702978eSGreg Roach    protected function footerStartHandler(): void
459c1010edaSGreg Roach    {
4607a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
461a6f13a4aSGreg Roach    }
462a6f13a4aSGreg Roach
463a6f13a4aSGreg Roach    /**
464fab8f067SGreg Roach     * Handle <cell>
465a6f13a4aSGreg Roach     *
466fab8f067SGreg Roach     * @param string[] $attrs
4678ba2e626SGreg Roach     *
4688ba2e626SGreg Roach     * @return void
469a6f13a4aSGreg Roach     */
470b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
471c1010edaSGreg Roach    {
472a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4737a6ee1acSGreg Roach        $align = '';
474a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
475a6f13a4aSGreg Roach            $align = $attrs['align'];
476a6f13a4aSGreg Roach            // RTL supported left/right alignment
477044416d2SGreg Roach            if ($align === 'rightrtl') {
478e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4797a6ee1acSGreg Roach                    $align = 'left';
480a6f13a4aSGreg Roach                } else {
4817a6ee1acSGreg Roach                    $align = 'right';
482a6f13a4aSGreg Roach                }
483044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
484e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4857a6ee1acSGreg Roach                    $align = 'right';
486a6f13a4aSGreg Roach                } else {
4877a6ee1acSGreg Roach                    $align = 'left';
488a6f13a4aSGreg Roach                }
489a6f13a4aSGreg Roach            }
490a6f13a4aSGreg Roach        }
491a6f13a4aSGreg Roach
492a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4937a6ee1acSGreg Roach        $bgcolor = '';
494a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
495a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
496a6f13a4aSGreg Roach        }
497a6f13a4aSGreg Roach
498a6f13a4aSGreg Roach        // int Whether or not the background should be painted
499a6f13a4aSGreg Roach        $fill = 1;
500a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
5017a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
502a6f13a4aSGreg Roach                $fill = 0;
5037a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
504a6f13a4aSGreg Roach                $fill = 1;
505a6f13a4aSGreg Roach            }
506a6f13a4aSGreg Roach        }
507a6f13a4aSGreg Roach
508a6f13a4aSGreg Roach        $reseth = true;
509a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
510a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
5117a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
512a6f13a4aSGreg Roach                $reseth = false;
5137a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
514a6f13a4aSGreg Roach                $reseth = true;
515a6f13a4aSGreg Roach            }
516a6f13a4aSGreg Roach        }
517a6f13a4aSGreg Roach
518a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
519a6f13a4aSGreg Roach        $border = 0;
520a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
521a6f13a4aSGreg Roach            $border = $attrs['border'];
522a6f13a4aSGreg Roach        }
523a6f13a4aSGreg Roach        // string Border color in HTML code
5247a6ee1acSGreg Roach        $bocolor = '';
525a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
526a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
527a6f13a4aSGreg Roach        }
528a6f13a4aSGreg Roach
529a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
530a6f13a4aSGreg Roach        $height = 0;
531a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
532589feda3SGreg Roach            $height = $attrs['height'];
533a6f13a4aSGreg Roach        }
534a6f13a4aSGreg 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.
535a6f13a4aSGreg Roach        $width = 0;
536a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
537589feda3SGreg Roach            $width = $attrs['width'];
538a6f13a4aSGreg Roach        }
539a6f13a4aSGreg Roach
540a6f13a4aSGreg Roach        // int Stretch carachter mode
541a6f13a4aSGreg Roach        $stretch = 0;
542a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
543a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
544a6f13a4aSGreg Roach        }
545a6f13a4aSGreg Roach
546a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
547c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
548a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5497a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
550c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
551a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
552a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5537a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
554a6f13a4aSGreg Roach                $left = 0;
555a6f13a4aSGreg Roach            }
556a6f13a4aSGreg Roach        }
557a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
558c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
559a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5607a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
561c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
562a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
563a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5647a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
565a6f13a4aSGreg Roach                $top = 0;
566a6f13a4aSGreg Roach            }
567a6f13a4aSGreg Roach        }
568a6f13a4aSGreg Roach
569a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5707a6ee1acSGreg Roach        $style = '';
571a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
572a6f13a4aSGreg Roach            $style = $attrs['style'];
573a6f13a4aSGreg Roach        }
574a6f13a4aSGreg Roach
575a6f13a4aSGreg Roach        // string Text color in html code
5767a6ee1acSGreg Roach        $tcolor = '';
577a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
578a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
579a6f13a4aSGreg Roach        }
580a6f13a4aSGreg Roach
581a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
582a6f13a4aSGreg Roach        $ln = 0;
583a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
584a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
585a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5867a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
587a6f13a4aSGreg Roach                $ln = 0;
588a6f13a4aSGreg Roach            }
589a6f13a4aSGreg Roach        }
590a6f13a4aSGreg Roach
591044416d2SGreg Roach        if ($align === 'left') {
5927a6ee1acSGreg Roach            $align = 'L';
593044416d2SGreg Roach        } elseif ($align === 'right') {
5947a6ee1acSGreg Roach            $align = 'R';
595044416d2SGreg Roach        } elseif ($align === 'center') {
5967a6ee1acSGreg Roach            $align = 'C';
597044416d2SGreg Roach        } elseif ($align === 'justify') {
5987a6ee1acSGreg Roach            $align = 'J';
599a6f13a4aSGreg Roach        }
600a6f13a4aSGreg Roach
6019b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
602a6f13a4aSGreg Roach        $this->print_data         = true;
603a6f13a4aSGreg Roach
604e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
60524f2a3afSGreg Roach            (int) $width,
60624f2a3afSGreg Roach            (int) $height,
607a6f13a4aSGreg Roach            $border,
608a6f13a4aSGreg Roach            $align,
609a6f13a4aSGreg Roach            $bgcolor,
610a6f13a4aSGreg Roach            $style,
611a6f13a4aSGreg Roach            $ln,
612a6f13a4aSGreg Roach            $top,
613a6f13a4aSGreg Roach            $left,
614a6f13a4aSGreg Roach            $fill,
615a6f13a4aSGreg Roach            $stretch,
616a6f13a4aSGreg Roach            $bocolor,
617a6f13a4aSGreg Roach            $tcolor,
618a6f13a4aSGreg Roach            $reseth
619a6f13a4aSGreg Roach        );
620a6f13a4aSGreg Roach    }
621a6f13a4aSGreg Roach
622a6f13a4aSGreg Roach    /**
623fab8f067SGreg Roach     * Handle </cell>
6248ba2e626SGreg Roach     *
6258ba2e626SGreg Roach     * @return void
626a6f13a4aSGreg Roach     */
627b702978eSGreg Roach    protected function cellEndHandler(): void
628c1010edaSGreg Roach    {
629a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
630e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
631a6f13a4aSGreg Roach    }
632a6f13a4aSGreg Roach
633a6f13a4aSGreg Roach    /**
634fab8f067SGreg Roach     * Handle <now />
6358ba2e626SGreg Roach     *
6368ba2e626SGreg Roach     * @return void
637a6f13a4aSGreg Roach     */
638b702978eSGreg Roach    protected function nowStartHandler(): void
639c1010edaSGreg Roach    {
6404459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
641a6f13a4aSGreg Roach    }
642a6f13a4aSGreg Roach
643a6f13a4aSGreg Roach    /**
644fab8f067SGreg Roach     * Handle <pageNum />
6458ba2e626SGreg Roach     *
6468ba2e626SGreg Roach     * @return void
647a6f13a4aSGreg Roach     */
648b702978eSGreg Roach    protected function pageNumStartHandler(): void
649c1010edaSGreg Roach    {
6507a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
651a6f13a4aSGreg Roach    }
652a6f13a4aSGreg Roach
653a6f13a4aSGreg Roach    /**
654fab8f067SGreg Roach     * Handle <totalPages />
6558ba2e626SGreg Roach     *
6568ba2e626SGreg Roach     * @return void
657a6f13a4aSGreg Roach     */
658b702978eSGreg Roach    protected function totalPagesStartHandler(): void
659c1010edaSGreg Roach    {
6607a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
661a6f13a4aSGreg Roach    }
662a6f13a4aSGreg Roach
663a6f13a4aSGreg Roach    /**
664a6f13a4aSGreg Roach     * Called at the start of an element.
665a6f13a4aSGreg Roach     *
666c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6678ba2e626SGreg Roach     *
6688ba2e626SGreg Roach     * @return void
669a6f13a4aSGreg Roach     */
670b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
671c1010edaSGreg Roach    {
672a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
673a6f13a4aSGreg Roach            $this->process_gedcoms++;
674a6f13a4aSGreg Roach
675a6f13a4aSGreg Roach            return;
676a6f13a4aSGreg Roach        }
677a6f13a4aSGreg Roach
678a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6797a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6807a6ee1acSGreg Roach        $tags      = explode(':', $tag);
681a6f13a4aSGreg Roach        $newgedrec = '';
682a6f13a4aSGreg Roach        if (count($tags) < 2) {
6836b9cb339SGreg Roach            $tmp       = Registry::gedcomRecordFactory()->make($attrs['id'], $this->tree);
684299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
685a6f13a4aSGreg Roach        }
686a6f13a4aSGreg Roach        if (empty($newgedrec)) {
687a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
688a6f13a4aSGreg Roach            $newgedrec = '';
689a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6907a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
691d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
692d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
693a6f13a4aSGreg Roach                    } else {
6946b9cb339SGreg Roach                        $tmp       = Registry::gedcomRecordFactory()->make($match[1], $this->tree);
695299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
696a6f13a4aSGreg Roach                    }
697a6f13a4aSGreg Roach                } else {
6987a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
69913abd6f3SGreg Roach                        $gmatch = [];
700a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
7016b9cb339SGreg Roach                            $tmp       = Registry::gedcomRecordFactory()->make($gmatch[1], $this->tree);
702299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
703a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
704a6f13a4aSGreg Roach                        } else {
705a6f13a4aSGreg Roach                            $newgedrec = '';
706a6f13a4aSGreg Roach                            break;
707a6f13a4aSGreg Roach                        }
708a6f13a4aSGreg Roach                    } else {
709b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
7103d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
711a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
712a6f13a4aSGreg Roach                    }
713a6f13a4aSGreg Roach                }
714a6f13a4aSGreg Roach            }
715a6f13a4aSGreg Roach        }
716a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7179b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
718a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
719a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
720a6f13a4aSGreg Roach                $this->fact = $match[2];
721a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
722a6f13a4aSGreg Roach            }
723a6f13a4aSGreg Roach        } else {
724a6f13a4aSGreg Roach            $this->process_gedcoms++;
725a6f13a4aSGreg Roach        }
726a6f13a4aSGreg Roach    }
727a6f13a4aSGreg Roach
728a6f13a4aSGreg Roach    /**
729a6f13a4aSGreg Roach     * Called at the end of an element.
7308ba2e626SGreg Roach     *
7318ba2e626SGreg Roach     * @return void
732a6f13a4aSGreg Roach     */
733b702978eSGreg Roach    protected function gedcomEndHandler(): void
734c1010edaSGreg Roach    {
735a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
736a6f13a4aSGreg Roach            $this->process_gedcoms--;
737a6f13a4aSGreg Roach        } else {
73865e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
739a6f13a4aSGreg Roach        }
740a6f13a4aSGreg Roach    }
741a6f13a4aSGreg Roach
742a6f13a4aSGreg Roach    /**
743fab8f067SGreg Roach     * Handle <textBox>
744a6f13a4aSGreg Roach     *
745fab8f067SGreg Roach     * @param string[] $attrs
7468ba2e626SGreg Roach     *
7478ba2e626SGreg Roach     * @return void
748a6f13a4aSGreg Roach     */
749b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
750c1010edaSGreg Roach    {
751a6f13a4aSGreg Roach        // string Background color code
7527a6ee1acSGreg Roach        $bgcolor = '';
753a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
754a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
755a6f13a4aSGreg Roach        }
756a6f13a4aSGreg Roach
757a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
758a6f13a4aSGreg Roach        $fill = true;
759a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7607a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
761a6f13a4aSGreg Roach                $fill = false;
7627a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
763a6f13a4aSGreg Roach                $fill = true;
764a6f13a4aSGreg Roach            }
765a6f13a4aSGreg Roach        }
766a6f13a4aSGreg Roach
767a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
768a6f13a4aSGreg Roach        $border = false;
769a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7707a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
771a6f13a4aSGreg Roach                $border = true;
7727a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
773a6f13a4aSGreg Roach                $border = false;
774a6f13a4aSGreg Roach            }
775a6f13a4aSGreg Roach        }
776a6f13a4aSGreg Roach
777a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
778a6f13a4aSGreg Roach        $height = 0;
779a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
780a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
781a6f13a4aSGreg Roach        }
782a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
783a6f13a4aSGreg Roach        $width = 0;
784a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
785a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
786a6f13a4aSGreg Roach        }
787a6f13a4aSGreg Roach
788a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
789c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
790a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7917a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
792c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
793a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
794a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7957a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
796a6f13a4aSGreg Roach                $left = 0;
797a6f13a4aSGreg Roach            }
798a6f13a4aSGreg Roach        }
799a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
800c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
801a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8027a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
803c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
804a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
805a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8067a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
807a6f13a4aSGreg Roach                $top = 0;
808a6f13a4aSGreg Roach            }
809a6f13a4aSGreg Roach        }
810a6f13a4aSGreg 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
811a6f13a4aSGreg Roach        $newline = false;
812a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8137a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
814a6f13a4aSGreg Roach                $newline = true;
8157a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
816a6f13a4aSGreg Roach                $newline = false;
817a6f13a4aSGreg Roach            }
818a6f13a4aSGreg Roach        }
819a6f13a4aSGreg Roach        // boolean
820a6f13a4aSGreg Roach        $pagecheck = true;
821a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8227a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
823a6f13a4aSGreg Roach                $pagecheck = false;
8247a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
825a6f13a4aSGreg Roach                $pagecheck = true;
826a6f13a4aSGreg Roach            }
827a6f13a4aSGreg Roach        }
828a6f13a4aSGreg Roach        // boolean Cell padding
829a6f13a4aSGreg Roach        $padding = true;
830a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8317a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
832a6f13a4aSGreg Roach                $padding = false;
8337a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
834a6f13a4aSGreg Roach                $padding = true;
835a6f13a4aSGreg Roach            }
836a6f13a4aSGreg Roach        }
837a6f13a4aSGreg Roach        // boolean Reset this box Height
838a6f13a4aSGreg Roach        $reseth = false;
839a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8407a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
841a6f13a4aSGreg Roach                $reseth = true;
8427a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
843a6f13a4aSGreg Roach                $reseth = false;
844a6f13a4aSGreg Roach            }
845a6f13a4aSGreg Roach        }
846a6f13a4aSGreg Roach
847a6f13a4aSGreg Roach        // string Style of rendering
8487a6ee1acSGreg Roach        $style = '';
849a6f13a4aSGreg Roach
8509b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
851a6f13a4aSGreg Roach        $this->print_data         = false;
852a6f13a4aSGreg Roach
8539b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
854e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
855a6f13a4aSGreg Roach            $width,
856a6f13a4aSGreg Roach            $height,
857a6f13a4aSGreg Roach            $border,
858a6f13a4aSGreg Roach            $bgcolor,
859a6f13a4aSGreg Roach            $newline,
860a6f13a4aSGreg Roach            $left,
861a6f13a4aSGreg Roach            $top,
862a6f13a4aSGreg Roach            $pagecheck,
863a6f13a4aSGreg Roach            $style,
864a6f13a4aSGreg Roach            $fill,
865a6f13a4aSGreg Roach            $padding,
866a6f13a4aSGreg Roach            $reseth
867a6f13a4aSGreg Roach        );
868a6f13a4aSGreg Roach    }
869a6f13a4aSGreg Roach
870a6f13a4aSGreg Roach    /**
871fab8f067SGreg Roach     * Handle <textBox>
8728ba2e626SGreg Roach     *
8738ba2e626SGreg Roach     * @return void
874a6f13a4aSGreg Roach     */
875b702978eSGreg Roach    protected function textBoxEndHandler(): void
876c1010edaSGreg Roach    {
877a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
878e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
87931d9777aSGreg Roach
88031d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
88131d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
88231d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
88331d9777aSGreg Roach
884e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
885e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
886a6f13a4aSGreg Roach    }
887a6f13a4aSGreg Roach
888a6f13a4aSGreg Roach    /**
88976692c8bSGreg Roach     * XLM <Text>.
89076692c8bSGreg Roach     *
891c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8928ba2e626SGreg Roach     *
8938ba2e626SGreg Roach     * @return void
894a6f13a4aSGreg Roach     */
895b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
896c1010edaSGreg Roach    {
8979b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
898a6f13a4aSGreg Roach        $this->print_data         = true;
899a6f13a4aSGreg Roach
900a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9017a6ee1acSGreg Roach        $style = '';
902a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
903a6f13a4aSGreg Roach            $style = $attrs['style'];
904a6f13a4aSGreg Roach        }
905a6f13a4aSGreg Roach
906a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9077a6ee1acSGreg Roach        $color = '';
908a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
909a6f13a4aSGreg Roach            $color = $attrs['color'];
910a6f13a4aSGreg Roach        }
911a6f13a4aSGreg Roach
912e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
913a6f13a4aSGreg Roach    }
914a6f13a4aSGreg Roach
915a6f13a4aSGreg Roach    /**
916fab8f067SGreg Roach     * Handle </text>
9178ba2e626SGreg Roach     *
9188ba2e626SGreg Roach     * @return void
919a6f13a4aSGreg Roach     */
920b702978eSGreg Roach    protected function textEndHandler(): void
921c1010edaSGreg Roach    {
922a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
923e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
924a6f13a4aSGreg Roach    }
925a6f13a4aSGreg Roach
926a6f13a4aSGreg Roach    /**
927fab8f067SGreg Roach     * Handle <getPersonName />
928a6f13a4aSGreg Roach     * Get the name
929a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
930a6f13a4aSGreg Roach     * 2. id is set with a record id
931a6f13a4aSGreg Roach     *
932c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9338ba2e626SGreg Roach     *
9348ba2e626SGreg Roach     * @return void
935a6f13a4aSGreg Roach     */
936b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
937c1010edaSGreg Roach    {
9387a6ee1acSGreg Roach        $id    = '';
93913abd6f3SGreg Roach        $match = [];
940a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9417a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
942a6f13a4aSGreg Roach                $id = $match[1];
943a6f13a4aSGreg Roach            }
944a6f13a4aSGreg Roach        } else {
9457a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
946d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
947d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
948a6f13a4aSGreg Roach                }
949a6f13a4aSGreg Roach            } else {
9507a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
95113abd6f3SGreg Roach                    $gmatch = [];
952a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
953a6f13a4aSGreg Roach                        $id = $gmatch[1];
954a6f13a4aSGreg Roach                    }
955a6f13a4aSGreg Roach                } else {
956a6f13a4aSGreg Roach                    $id = $attrs['id'];
957a6f13a4aSGreg Roach                }
958a6f13a4aSGreg Roach            }
959a6f13a4aSGreg Roach        }
960a6f13a4aSGreg Roach        if (!empty($id)) {
9616b9cb339SGreg Roach            $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
9628f038c36SRico Sonntag            if ($record === null) {
963a6f13a4aSGreg Roach                return;
964a6f13a4aSGreg Roach            }
965a6f13a4aSGreg Roach            if (!$record->canShowName()) {
966a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
967a6f13a4aSGreg Roach            } else {
96839ca88baSGreg Roach                $name = $record->fullName();
969a6f13a4aSGreg Roach                $name = strip_tags($name);
970a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
971381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
972a6f13a4aSGreg Roach                } else {
973911124f5SGreg Roach                    $addname = (string) $record->alternateName();
974a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
975a6f13a4aSGreg Roach                    if (!empty($addname)) {
9767a6ee1acSGreg Roach                        $name .= ' ' . $addname;
977a6f13a4aSGreg Roach                    }
978a6f13a4aSGreg Roach                }
979a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
980a6f13a4aSGreg Roach            }
981a6f13a4aSGreg Roach        }
982a6f13a4aSGreg Roach    }
983a6f13a4aSGreg Roach
984a6f13a4aSGreg Roach    /**
985fab8f067SGreg Roach     * Handle <gedcomValue />
986a6f13a4aSGreg Roach     *
987fab8f067SGreg Roach     * @param string[] $attrs
9888ba2e626SGreg Roach     *
9898ba2e626SGreg Roach     * @return void
990a6f13a4aSGreg Roach     */
991b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
992c1010edaSGreg Roach    {
9937a6ee1acSGreg Roach        $id    = '';
99413abd6f3SGreg Roach        $match = [];
9957a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
996a6f13a4aSGreg Roach            $id = $match[1];
997a6f13a4aSGreg Roach        }
998a6f13a4aSGreg Roach
999044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10007a6ee1acSGreg Roach            $useBreak = '1';
1001a6f13a4aSGreg Roach        } else {
10027a6ee1acSGreg Roach            $useBreak = '0';
1003a6f13a4aSGreg Roach        }
1004a6f13a4aSGreg Roach
1005a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1006a6f13a4aSGreg Roach        if (!empty($tag)) {
1007044416d2SGreg Roach            if ($tag === '@desc') {
1008a6f13a4aSGreg Roach                $value = $this->desc;
1009a6f13a4aSGreg Roach                $value = trim($value);
1010a6f13a4aSGreg Roach                $this->current_element->addText($value);
1011a6f13a4aSGreg Roach            }
1012044416d2SGreg Roach            if ($tag === '@id') {
1013a6f13a4aSGreg Roach                $this->current_element->addText($id);
1014a6f13a4aSGreg Roach            } else {
10157a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1016a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1017b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1018b2448a1bSGreg Roach                    if ($level === 0) {
1019a6f13a4aSGreg Roach                        $level++;
1020a6f13a4aSGreg Roach                    }
1021a6f13a4aSGreg Roach                } else {
1022b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1023a6f13a4aSGreg Roach                }
1024a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10253d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1026a6f13a4aSGreg Roach                switch (end($tags)) {
1027a6f13a4aSGreg Roach                    case 'DATE':
1028a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1029a6f13a4aSGreg Roach                        $value = $tmp->display();
1030a6f13a4aSGreg Roach                        break;
1031a6f13a4aSGreg Roach                    case 'PLAC':
1032299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1033392561bbSGreg Roach                        $value = $tmp->shortName();
1034a6f13a4aSGreg Roach                        break;
1035a6f13a4aSGreg Roach                }
1036044416d2SGreg Roach                if ($useBreak === '1') {
1037a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1038a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1039a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1040a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1041a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10423b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10433b3cfeeaSGreg Roach                        $value = substr($value, 4);
1044a6f13a4aSGreg Roach                    }
1045a6f13a4aSGreg Roach                }
1046d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10473cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
10484d35caa7SGreg Roach                    if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
10494d35caa7SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->markdown($this->tree)->convertToHtml($value));
10504d35caa7SGreg Roach                    } else {
10514d35caa7SGreg Roach                        $value = strip_tags(Registry::markdownFactory()->autolink($this->tree)->convertToHtml($value));
10524d35caa7SGreg Roach                    }
1053a4d703aeSGreg Roach                }
1054a2a485c3SGreg Roach
1055a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
1056381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1057a2a485c3SGreg Roach                }
1058a6f13a4aSGreg Roach                $this->current_element->addText($value);
1059a6f13a4aSGreg Roach            }
1060a6f13a4aSGreg Roach        }
1061a6f13a4aSGreg Roach    }
1062a6f13a4aSGreg Roach
1063a6f13a4aSGreg Roach    /**
1064fab8f067SGreg Roach     * Handle <repeatTag>
1065a6f13a4aSGreg Roach     *
1066fab8f067SGreg Roach     * @param string[] $attrs
10678ba2e626SGreg Roach     *
10688ba2e626SGreg Roach     * @return void
1069a6f13a4aSGreg Roach     */
1070b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1071c1010edaSGreg Roach    {
1072a6f13a4aSGreg Roach        $this->process_repeats++;
1073a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1074a6f13a4aSGreg Roach            return;
1075a6f13a4aSGreg Roach        }
1076a6f13a4aSGreg Roach
10779b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
107813abd6f3SGreg Roach        $this->repeats         = [];
1079e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1080a6f13a4aSGreg Roach
1081e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1082a6f13a4aSGreg Roach        if (!empty($tag)) {
1083044416d2SGreg Roach            if ($tag === '@desc') {
1084a6f13a4aSGreg Roach                $value = $this->desc;
1085a6f13a4aSGreg Roach                $value = trim($value);
1086a6f13a4aSGreg Roach                $this->current_element->addText($value);
1087a6f13a4aSGreg Roach            } else {
10887a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10897a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1090b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1091b2448a1bSGreg Roach                if ($level === 0) {
1092a6f13a4aSGreg Roach                    $level++;
1093a6f13a4aSGreg Roach                }
1094a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1095a6f13a4aSGreg Roach                $t      = $tag;
1096a6f13a4aSGreg Roach                $count  = count($tags);
1097a6f13a4aSGreg Roach                $i      = 0;
1098a6f13a4aSGreg Roach                while ($i < $count) {
1099a6f13a4aSGreg Roach                    $t = $tags[$i];
1100a6f13a4aSGreg Roach                    if (!empty($t)) {
1101a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
11023d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1103a6f13a4aSGreg Roach                            if (empty($subrec)) {
1104a6f13a4aSGreg Roach                                $level--;
11053d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1106a6f13a4aSGreg Roach                                if (empty($subrec)) {
1107a6f13a4aSGreg Roach                                    return;
1108a6f13a4aSGreg Roach                                }
1109a6f13a4aSGreg Roach                            }
1110a6f13a4aSGreg Roach                        }
1111a6f13a4aSGreg Roach                        $level++;
1112a6f13a4aSGreg Roach                    }
1113a6f13a4aSGreg Roach                    $i++;
1114a6f13a4aSGreg Roach                }
1115a6f13a4aSGreg Roach                $level--;
1116a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1117a6f13a4aSGreg Roach                $i     = 0;
1118a6f13a4aSGreg Roach                while ($i < $count) {
1119a6f13a4aSGreg Roach                    $i++;
1120a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1121a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11228d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
11236b9cb339SGreg Roach                        $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree);
1124a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1125a9007102SGreg Roach                            continue;
1126a9007102SGreg Roach                        }
1127a9007102SGreg Roach                    }
1128a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1129a6f13a4aSGreg Roach                }
1130a6f13a4aSGreg Roach            }
1131a6f13a4aSGreg Roach        }
1132a6f13a4aSGreg Roach    }
1133a6f13a4aSGreg Roach
1134a6f13a4aSGreg Roach    /**
1135fab8f067SGreg Roach     * Handle </repeatTag>
11368ba2e626SGreg Roach     *
11378ba2e626SGreg Roach     * @return void
1138a6f13a4aSGreg Roach     */
1139b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1140c1010edaSGreg Roach    {
1141a6f13a4aSGreg Roach        $this->process_repeats--;
1142a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1143a6f13a4aSGreg Roach            return;
1144a6f13a4aSGreg Roach        }
1145a6f13a4aSGreg Roach
1146a6f13a4aSGreg Roach        // Check if there is anything to repeat
1147a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1148a6f13a4aSGreg Roach            // No need to load them if not used...
1149a6f13a4aSGreg Roach
1150a6f13a4aSGreg Roach            $lineoffset = 0;
1151a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1152a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1153a6f13a4aSGreg Roach            }
1154a6f13a4aSGreg Roach            //-- read the xml from the file
1155299d100dSGreg Roach            $lines = file($this->report);
1156dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) {
1157a6f13a4aSGreg Roach                $lineoffset--;
1158a6f13a4aSGreg Roach            }
1159a6f13a4aSGreg Roach            $lineoffset++;
1160a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1161a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1162a6f13a4aSGreg Roach            // RepeatTag Level counter
1163a6f13a4aSGreg Roach            $count = 1;
1164a6f13a4aSGreg Roach            while (0 < $count) {
1165dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<RepeatTag')) {
1166a6f13a4aSGreg Roach                    $count++;
1167dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</RepeatTag')) {
1168a6f13a4aSGreg Roach                    $count--;
1169a6f13a4aSGreg Roach                }
1170a6f13a4aSGreg Roach                if (0 < $count) {
1171a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1172a6f13a4aSGreg Roach                }
1173a6f13a4aSGreg Roach                $line_nr++;
1174a6f13a4aSGreg Roach            }
1175a6f13a4aSGreg Roach            // No need to drag this
1176a6f13a4aSGreg Roach            unset($lines);
1177a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1178a6f13a4aSGreg Roach            // Save original values
11799b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1180a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1181a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1182a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1183a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1184e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1185a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11861aa04befSGreg Roach
11871aa04befSGreg Roach                xml_set_element_handler(
11881aa04befSGreg Roach                    $repeat_parser,
11899d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11901aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11911aa04befSGreg Roach                    },
11929d454b6bSGreg Roach                    function ($parser, string $name): void {
11931aa04befSGreg Roach                        $this->endElement($parser, $name);
11941aa04befSGreg Roach                    }
11951aa04befSGreg Roach                );
11961aa04befSGreg Roach
11971aa04befSGreg Roach                xml_set_character_data_handler(
11981aa04befSGreg Roach                    $repeat_parser,
11999d454b6bSGreg Roach                    function ($parser, string $data): void {
12001aa04befSGreg Roach                        $this->characterData($parser, $data);
12011aa04befSGreg Roach                    }
12021aa04befSGreg Roach                );
12031aa04befSGreg Roach
1204a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12056ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1206a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1207a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1208a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1209a6f13a4aSGreg Roach                    ));
1210a6f13a4aSGreg Roach                }
1211a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1212a6f13a4aSGreg Roach            }
1213a6f13a4aSGreg Roach            // Restore original values
1214a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1215e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1216a6f13a4aSGreg Roach        }
121765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1218a6f13a4aSGreg Roach    }
1219a6f13a4aSGreg Roach
1220a6f13a4aSGreg Roach    /**
1221a6f13a4aSGreg Roach     * Variable lookup
1222a6f13a4aSGreg Roach     * Retrieve predefined variables :
1223a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1224a6f13a4aSGreg Roach     *        1 EVEN This is a description
1225a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1226a6f13a4aSGreg Roach     * $ I18N::translate('....')
1227a6f13a4aSGreg Roach     * $ language_settings[]
1228a6f13a4aSGreg Roach     *
1229c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12308ba2e626SGreg Roach     *
12318ba2e626SGreg Roach     * @return void
1232a6f13a4aSGreg Roach     */
1233b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1234c1010edaSGreg Roach    {
1235a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12366ccdf4f0SGreg 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));
1237a6f13a4aSGreg Roach        }
1238a6f13a4aSGreg Roach
1239a6f13a4aSGreg Roach        $var = $attrs['var'];
1240a6f13a4aSGreg Roach        // SetVar element preset variables
1241d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1242d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1243a6f13a4aSGreg Roach        } else {
1244a6f13a4aSGreg Roach            $tfact = $this->fact;
12457a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') {
1246a6f13a4aSGreg Roach                // Use :
1247a6f13a4aSGreg Roach                // n TYPE This text if string
1248a6f13a4aSGreg Roach                $tfact = $this->type;
1249*97def6bcSGreg Roach            } else {
1250*97def6bcSGreg Roach                foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) {
1251*97def6bcSGreg Roach                    $element = Registry::elementFactory()->make($record_type . ':' . $this->fact);
1252*97def6bcSGreg Roach
1253*97def6bcSGreg Roach                    if (!$element instanceof UnknownElement) {
1254*97def6bcSGreg Roach                        $tfact = $element->label();
1255*97def6bcSGreg Roach                        break;
1256a6f13a4aSGreg Roach                    }
1257*97def6bcSGreg Roach                }
1258*97def6bcSGreg Roach            }
1259*97def6bcSGreg Roach
1260*97def6bcSGreg Roach            $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]);
1261*97def6bcSGreg Roach
1262a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1263da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1264a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1265a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1266a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1267a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1268a6f13a4aSGreg Roach            }
1269a6f13a4aSGreg Roach        }
1270a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1271a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12727a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1273a6f13a4aSGreg Roach                $g   = new Date($var);
1274a6f13a4aSGreg Roach                $var = $g->display();
1275a6f13a4aSGreg Roach            }
1276a6f13a4aSGreg Roach        }
1277a6f13a4aSGreg Roach        $this->current_element->addText($var);
12782836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1279a6f13a4aSGreg Roach    }
1280a6f13a4aSGreg Roach
1281a6f13a4aSGreg Roach    /**
1282fab8f067SGreg Roach     * Handle <facts>
128376692c8bSGreg Roach     *
1284fab8f067SGreg Roach     * @param string[] $attrs
12858ba2e626SGreg Roach     *
12868ba2e626SGreg Roach     * @return void
1287a6f13a4aSGreg Roach     */
1288b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1289c1010edaSGreg Roach    {
1290a6f13a4aSGreg Roach        $this->process_repeats++;
1291a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1292a6f13a4aSGreg Roach            return;
1293a6f13a4aSGreg Roach        }
1294a6f13a4aSGreg Roach
12959b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
129613abd6f3SGreg Roach        $this->repeats         = [];
1297e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1298a6f13a4aSGreg Roach
12997a6ee1acSGreg Roach        $id    = '';
130013abd6f3SGreg Roach        $match = [];
13017a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1302a6f13a4aSGreg Roach            $id = $match[1];
1303a6f13a4aSGreg Roach        }
13047a6ee1acSGreg Roach        $tag = '';
1305a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1306a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1307a6f13a4aSGreg Roach        }
13087a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1309d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1310a6f13a4aSGreg Roach        }
1311a6f13a4aSGreg Roach
13126b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1313a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1314820b62dfSGreg Roach            $facts = $record->facts([], true);
131513abd6f3SGreg Roach            $this->repeats = [];
1316a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1317195b5e75SGreg Roach            foreach ($facts as $fact) {
1318d0889c63SGreg Roach                $tag = explode(':', $fact->tag())[1];
1319d0889c63SGreg Roach
1320d0889c63SGreg Roach                if (!in_array($tag, $nonfacts, true)) {
1321195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1322a6f13a4aSGreg Roach                }
1323a6f13a4aSGreg Roach            }
1324a6f13a4aSGreg Roach        } else {
132530158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1326d0889c63SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) {
1327138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1328a6f13a4aSGreg Roach                }
1329a6f13a4aSGreg Roach            }
1330a6f13a4aSGreg Roach        }
1331a6f13a4aSGreg Roach    }
1332a6f13a4aSGreg Roach
1333a6f13a4aSGreg Roach    /**
1334fab8f067SGreg Roach     * Handle </facts>
13358ba2e626SGreg Roach     *
13368ba2e626SGreg Roach     * @return void
1337a6f13a4aSGreg Roach     */
1338b702978eSGreg Roach    protected function factsEndHandler(): void
1339c1010edaSGreg Roach    {
1340a6f13a4aSGreg Roach        $this->process_repeats--;
1341a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1342a6f13a4aSGreg Roach            return;
1343a6f13a4aSGreg Roach        }
1344a6f13a4aSGreg Roach
1345a6f13a4aSGreg Roach        // Check if there is anything to repeat
1346a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1347e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1348a6f13a4aSGreg Roach            $lineoffset = 0;
1349a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1350a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1351a6f13a4aSGreg Roach            }
1352a6f13a4aSGreg Roach
1353a6f13a4aSGreg Roach            //-- read the xml from the file
1354299d100dSGreg Roach            $lines = file($this->report);
1355dec352c1SGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) {
1356a6f13a4aSGreg Roach                $lineoffset--;
1357a6f13a4aSGreg Roach            }
1358a6f13a4aSGreg Roach            $lineoffset++;
1359a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1360a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1361a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1362a6f13a4aSGreg Roach            while ($line_nr < $i) {
1363a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1364a6f13a4aSGreg Roach                $line_nr++;
1365a6f13a4aSGreg Roach            }
1366a6f13a4aSGreg Roach            // No need to drag this
1367a6f13a4aSGreg Roach            unset($lines);
1368a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1369a6f13a4aSGreg Roach            // Save original values
13709b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1371a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1372a6f13a4aSGreg Roach            $count                = count($this->repeats);
1373a6f13a4aSGreg Roach            $i                    = 0;
1374a6f13a4aSGreg Roach            while ($i < $count) {
1375a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1376a6f13a4aSGreg Roach                $this->fact   = '';
1377a6f13a4aSGreg Roach                $this->desc   = '';
1378a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1379a6f13a4aSGreg Roach                    $this->fact = $match[1];
1380a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
138113abd6f3SGreg Roach                        $tmatch = [];
1382a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1383a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1384a6f13a4aSGreg Roach                        } else {
1385a6f13a4aSGreg Roach                            $this->type = ' ';
1386a6f13a4aSGreg Roach                        }
1387a6f13a4aSGreg Roach                    }
1388a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13893d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1390a6f13a4aSGreg Roach                }
1391a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1392e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1393a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13941aa04befSGreg Roach
13951aa04befSGreg Roach                xml_set_element_handler(
13961aa04befSGreg Roach                    $repeat_parser,
13979d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13981aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13991aa04befSGreg Roach                    },
14009d454b6bSGreg Roach                    function ($parser, string $name): void {
14011aa04befSGreg Roach                        $this->endElement($parser, $name);
14021aa04befSGreg Roach                    }
14031aa04befSGreg Roach                );
14041aa04befSGreg Roach
14051aa04befSGreg Roach                xml_set_character_data_handler(
14061aa04befSGreg Roach                    $repeat_parser,
14079d454b6bSGreg Roach                    function ($parser, string $data): void {
14081aa04befSGreg Roach                        $this->characterData($parser, $data);
14091aa04befSGreg Roach                    }
14101aa04befSGreg Roach                );
14111aa04befSGreg Roach
1412a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14136ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1414a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1415a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1416a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1417a6f13a4aSGreg Roach                    ));
1418a6f13a4aSGreg Roach                }
1419a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1420a6f13a4aSGreg Roach                $i++;
1421a6f13a4aSGreg Roach            }
1422a6f13a4aSGreg Roach            // Restore original values
1423e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1424a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1425a6f13a4aSGreg Roach        }
142665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1427a6f13a4aSGreg Roach    }
1428a6f13a4aSGreg Roach
1429a6f13a4aSGreg Roach    /**
1430a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1431d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1432a6f13a4aSGreg Roach     *
1433c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14348ba2e626SGreg Roach     *
14358ba2e626SGreg Roach     * @return void
1436a6f13a4aSGreg Roach     */
1437b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1438c1010edaSGreg Roach    {
1439a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14406ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1441a6f13a4aSGreg Roach        }
1442a6f13a4aSGreg Roach
1443a6f13a4aSGreg Roach        $name  = $attrs['name'];
1444a6f13a4aSGreg Roach        $value = $attrs['value'];
144513abd6f3SGreg Roach        $match = [];
1446a6f13a4aSGreg Roach        // Current GEDCOM record strings
1447044416d2SGreg Roach        if ($value === '@ID') {
14487a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1449a6f13a4aSGreg Roach                $value = $match[1];
1450a6f13a4aSGreg Roach            }
1451044416d2SGreg Roach        } elseif ($value === '@fact') {
1452a6f13a4aSGreg Roach            $value = $this->fact;
1453044416d2SGreg Roach        } elseif ($value === '@desc') {
1454a6f13a4aSGreg Roach            $value = $this->desc;
1455044416d2SGreg Roach        } elseif ($value === '@generation') {
1456589feda3SGreg Roach            $value = (string) $this->generation;
1457a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
145813abd6f3SGreg Roach            $gmatch = [];
1459a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14607a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1461a6f13a4aSGreg Roach            }
1462a6f13a4aSGreg Roach        }
1463a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1464d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1465a6f13a4aSGreg Roach        }
1466a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1467a6f13a4aSGreg Roach        $i     = 0;
1468a6f13a4aSGreg Roach        while ($i < $count) {
1469d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14707a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1471a6f13a4aSGreg Roach            $i++;
1472a6f13a4aSGreg Roach        }
1473a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1474da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1475a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1476a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1477a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1478a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1479a6f13a4aSGreg Roach        }
148052868398SGreg Roach
1481a6f13a4aSGreg Roach        // Arithmetic functions
14823cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
148352868398SGreg Roach            // Create an expression language with the functions used by our reports.
148452868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1485c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1486c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
148752868398SGreg Roach
148852868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1489a6f13a4aSGreg Roach        }
149052868398SGreg Roach
1491dec352c1SGreg Roach        if (str_contains($value, '@')) {
14927a6ee1acSGreg Roach            $value = '';
1493a6f13a4aSGreg Roach        }
1494d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1495a6f13a4aSGreg Roach    }
1496a6f13a4aSGreg Roach
1497a6f13a4aSGreg Roach    /**
1498fab8f067SGreg Roach     * Handle <if>
1499a6f13a4aSGreg Roach     *
1500fab8f067SGreg Roach     * @param string[] $attrs
15018ba2e626SGreg Roach     *
15028ba2e626SGreg Roach     * @return void
1503a6f13a4aSGreg Roach     */
1504b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1505c1010edaSGreg Roach    {
1506a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1507a6f13a4aSGreg Roach            $this->process_ifs++;
1508a6f13a4aSGreg Roach
1509a6f13a4aSGreg Roach            return;
1510a6f13a4aSGreg Roach        }
1511a6f13a4aSGreg Roach
1512a6f13a4aSGreg Roach        $condition = $attrs['condition'];
151382759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1514c1010edaSGreg Roach        $condition = str_replace([
1515c1010edaSGreg Roach            ' LT ',
1516c1010edaSGreg Roach            ' GT ',
1517c1010edaSGreg Roach        ], [
1518c1010edaSGreg Roach            '<',
1519c1010edaSGreg Roach            '>',
1520c1010edaSGreg Roach        ], $condition);
15213cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15227a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
152313abd6f3SGreg Roach        $match     = [];
15243cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1525a6f13a4aSGreg Roach        $i         = 0;
1526a6f13a4aSGreg Roach        while ($i < $count) {
1527a6f13a4aSGreg Roach            $id    = $match[$i][1];
1528a6f13a4aSGreg Roach            $value = '""';
1529044416d2SGreg Roach            if ($id === 'ID') {
15307a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1531a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1532a6f13a4aSGreg Roach                }
15337a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1534a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15357a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1536a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15377a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1538a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1539a6f13a4aSGreg Roach            } else {
1540b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1541b2448a1bSGreg Roach                if ($level === 0) {
1542a6f13a4aSGreg Roach                    $level++;
1543a6f13a4aSGreg Roach                }
15443d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1545a6f13a4aSGreg Roach                if (empty($value)) {
1546a6f13a4aSGreg Roach                    $level++;
15473d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1548a6f13a4aSGreg Roach                }
15498d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15505e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1551a6f13a4aSGreg Roach            }
1552a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1553a6f13a4aSGreg Roach            $i++;
1554a6f13a4aSGreg Roach        }
15555809450fSGreg Roach
1556cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1557cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1558c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1559c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1560cb63a60eSGreg Roach
1561cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15625809450fSGreg Roach
1563a6f13a4aSGreg Roach        if (!$ret) {
1564a6f13a4aSGreg Roach            $this->process_ifs++;
1565a6f13a4aSGreg Roach        }
1566a6f13a4aSGreg Roach    }
1567a6f13a4aSGreg Roach
1568a6f13a4aSGreg Roach    /**
1569fab8f067SGreg Roach     * Handle </if>
15708ba2e626SGreg Roach     *
15718ba2e626SGreg Roach     * @return void
1572a6f13a4aSGreg Roach     */
1573b702978eSGreg Roach    protected function ifEndHandler(): void
1574c1010edaSGreg Roach    {
1575a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1576a6f13a4aSGreg Roach            $this->process_ifs--;
1577a6f13a4aSGreg Roach        }
1578a6f13a4aSGreg Roach    }
1579a6f13a4aSGreg Roach
1580a6f13a4aSGreg Roach    /**
1581fab8f067SGreg Roach     * Handle <footnote>
1582a6f13a4aSGreg Roach     * Collect the Footnote links
1583fab8f067SGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignored
1584a6f13a4aSGreg Roach     *
1585fab8f067SGreg Roach     * @param string[] $attrs
15868ba2e626SGreg Roach     *
15878ba2e626SGreg Roach     * @return void
1588a6f13a4aSGreg Roach     */
1589b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1590c1010edaSGreg Roach    {
15917a6ee1acSGreg Roach        $id = '';
15927a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1593a6f13a4aSGreg Roach            $id = $match[2];
1594a6f13a4aSGreg Roach        }
15956b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($id, $this->tree);
1596a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15979b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1598a6f13a4aSGreg Roach            $this->print_data         = true;
15997a6ee1acSGreg Roach            $style                    = '';
1600a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1601a6f13a4aSGreg Roach                $style = $attrs['style'];
1602a6f13a4aSGreg Roach            }
1603a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1604e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1605a6f13a4aSGreg Roach        } else {
1606a6f13a4aSGreg Roach            $this->print_data       = false;
1607a6f13a4aSGreg Roach            $this->process_footnote = false;
1608a6f13a4aSGreg Roach        }
1609a6f13a4aSGreg Roach    }
1610a6f13a4aSGreg Roach
1611a6f13a4aSGreg Roach    /**
1612fab8f067SGreg Roach     * Handle </footnote>
1613a6f13a4aSGreg Roach     * Print the collected Footnote data
16148ba2e626SGreg Roach     *
16158ba2e626SGreg Roach     * @return void
1616a6f13a4aSGreg Roach     */
1617b702978eSGreg Roach    protected function footnoteEndHandler(): void
1618c1010edaSGreg Roach    {
1619a6f13a4aSGreg Roach        if ($this->process_footnote) {
1620a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1621a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1622a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1623e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1624a6f13a4aSGreg Roach            }
1625a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1626a6f13a4aSGreg Roach        } else {
1627a6f13a4aSGreg Roach            $this->process_footnote = true;
1628a6f13a4aSGreg Roach        }
1629a6f13a4aSGreg Roach    }
1630a6f13a4aSGreg Roach
1631a6f13a4aSGreg Roach    /**
1632fab8f067SGreg Roach     * Handle <footnoteTexts />
16338ba2e626SGreg Roach     *
16348ba2e626SGreg Roach     * @return void
1635a6f13a4aSGreg Roach     */
1636b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1637c1010edaSGreg Roach    {
16387a6ee1acSGreg Roach        $temp = 'footnotetexts';
1639e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1640a6f13a4aSGreg Roach    }
1641a6f13a4aSGreg Roach
1642a6f13a4aSGreg Roach    /**
1643a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16448ba2e626SGreg Roach     *
16458ba2e626SGreg Roach     * @return void
1646a6f13a4aSGreg Roach     */
1647b702978eSGreg Roach    protected function brStartHandler(): void
1648c1010edaSGreg Roach    {
1649a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1650a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1651a6f13a4aSGreg Roach        }
1652a6f13a4aSGreg Roach    }
1653a6f13a4aSGreg Roach
1654a6f13a4aSGreg Roach    /**
1655fab8f067SGreg Roach     * Handle <sp />
1656fab8f067SGreg Roach     * Forced space
16578ba2e626SGreg Roach     *
16588ba2e626SGreg Roach     * @return void
1659a6f13a4aSGreg Roach     */
1660b702978eSGreg Roach    protected function spStartHandler(): void
1661c1010edaSGreg Roach    {
1662a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1663a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1664a6f13a4aSGreg Roach        }
1665a6f13a4aSGreg Roach    }
1666a6f13a4aSGreg Roach
1667a6f13a4aSGreg Roach    /**
1668fab8f067SGreg Roach     * Handle <highlightedImage />
166976692c8bSGreg Roach     *
1670fab8f067SGreg Roach     * @param string[] $attrs
16718ba2e626SGreg Roach     *
16728ba2e626SGreg Roach     * @return void
1673a6f13a4aSGreg Roach     */
1674b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1675c1010edaSGreg Roach    {
1676a6f13a4aSGreg Roach        $id = '';
16777a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1678a6f13a4aSGreg Roach            $id = $match[1];
1679a6f13a4aSGreg Roach        }
1680a6f13a4aSGreg Roach
1681c21bdddcSGreg Roach        // Position the top corner of this box on the page
1682c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1683a6f13a4aSGreg Roach
1684c21bdddcSGreg Roach        // Position the left corner of this box on the page
1685c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1686a6f13a4aSGreg Roach
168783cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
168883cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1689a6f13a4aSGreg Roach
1690a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
169183cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1692a6f13a4aSGreg Roach
169383cdc021SGreg Roach        // Width, height (or both).
1694c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1695c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1696a6f13a4aSGreg Roach
16976b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
16984a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
169986a63f51SGreg Roach
1700a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1701a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1702b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
170329518ad2SGreg Roach
1704a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17053c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17063c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1707a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17083c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17093c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1710a6f13a4aSGreg Roach            } else {
17113c3b90deSGreg Roach                $width  = $attributes[0];
17123c3b90deSGreg Roach                $height = $attributes[1];
1713a6f13a4aSGreg Roach            }
1714a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1715e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1716a6f13a4aSGreg Roach        }
1717a6f13a4aSGreg Roach    }
1718a6f13a4aSGreg Roach
1719a6f13a4aSGreg Roach    /**
1720fab8f067SGreg Roach     * Handle <image/>
172176692c8bSGreg Roach     *
1722fab8f067SGreg Roach     * @param string[] $attrs
17238ba2e626SGreg Roach     *
17248ba2e626SGreg Roach     * @return void
1725a6f13a4aSGreg Roach     */
1726b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1727c1010edaSGreg Roach    {
172883cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1729c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1730a6f13a4aSGreg Roach
1731a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1732c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1733a6f13a4aSGreg Roach
173483cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
173583cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1736a6f13a4aSGreg Roach
1737a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
173883cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1739a6f13a4aSGreg Roach
174083cdc021SGreg Roach        // Width, height (or both).
1741c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1742c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1743a6f13a4aSGreg Roach
174483cdc021SGreg Roach        $file = $attrs['file'] ?? '';
174583cdc021SGreg Roach
1746044416d2SGreg Roach        if ($file === '@FILE') {
174713abd6f3SGreg Roach            $match = [];
1748a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
17496b9cb339SGreg Roach                $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree);
17504a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1751cdf416fbSGreg Roach
1752a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1753a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1754b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
175529518ad2SGreg Roach
1756a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17573c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17583c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1759a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17603c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17613c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1762a6f13a4aSGreg Roach                    } else {
17633c3b90deSGreg Roach                        $width  = $attributes[0];
17643c3b90deSGreg Roach                        $height = $attributes[1];
1765a6f13a4aSGreg Roach                    }
1766a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1767e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1768a6f13a4aSGreg Roach                }
1769a6f13a4aSGreg Roach            }
1770a6f13a4aSGreg Roach        } else {
17717a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1772a6f13a4aSGreg Roach                $size = getimagesize($file);
1773a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1774a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1775a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1776a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1777a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1778a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1779a6f13a4aSGreg Roach                } else {
1780a6f13a4aSGreg Roach                    $width  = $size[0];
1781a6f13a4aSGreg Roach                    $height = $size[1];
1782a6f13a4aSGreg Roach                }
1783e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1784e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1785a6f13a4aSGreg Roach            }
1786a6f13a4aSGreg Roach        }
1787a6f13a4aSGreg Roach    }
1788a6f13a4aSGreg Roach
1789a6f13a4aSGreg Roach    /**
1790fab8f067SGreg Roach     * Handle <line>
1791a6f13a4aSGreg Roach     *
1792fab8f067SGreg Roach     * @param string[] $attrs
17938ba2e626SGreg Roach     *
17948ba2e626SGreg Roach     * @return void
1795a6f13a4aSGreg Roach     */
1796b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1797c1010edaSGreg Roach    {
1798a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1799c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1800a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18017a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1802a6f13a4aSGreg Roach                $x1 = 0;
18037a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1804c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1805a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1806c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1807a6f13a4aSGreg Roach            }
1808a6f13a4aSGreg Roach        }
1809a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1810c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1811a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18127a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1813a6f13a4aSGreg Roach                $y1 = 0;
18147a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1815c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1816a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1817c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1818a6f13a4aSGreg Roach            }
1819a6f13a4aSGreg Roach        }
1820a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1821c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1822a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18237a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1824a6f13a4aSGreg Roach                $x2 = 0;
18257a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1826c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1827a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1828c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1829a6f13a4aSGreg Roach            }
1830a6f13a4aSGreg Roach        }
1831a6f13a4aSGreg Roach        // End vertical position
1832c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1833a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18347a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1835a6f13a4aSGreg Roach                $y2 = 0;
18367a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1837c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1838a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1839c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1840a6f13a4aSGreg Roach            }
1841a6f13a4aSGreg Roach        }
1842a6f13a4aSGreg Roach
1843e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1844e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1845a6f13a4aSGreg Roach    }
1846a6f13a4aSGreg Roach
1847a6f13a4aSGreg Roach    /**
1848fab8f067SGreg Roach     * Handle <list>
1849a6f13a4aSGreg Roach     *
1850fab8f067SGreg Roach     * @param string[] $attrs
18518ba2e626SGreg Roach     *
18528ba2e626SGreg Roach     * @return void
1853a6f13a4aSGreg Roach     */
1854b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1855c1010edaSGreg Roach    {
1856a6f13a4aSGreg Roach        $this->process_repeats++;
1857a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1858a6f13a4aSGreg Roach            return;
1859a6f13a4aSGreg Roach        }
1860a6f13a4aSGreg Roach
186113abd6f3SGreg Roach        $match = [];
1862a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1863a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1864a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1865d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1866a6f13a4aSGreg Roach                $sortby = trim($sortby);
1867a6f13a4aSGreg Roach            }
1868a6f13a4aSGreg Roach        } else {
18697a6ee1acSGreg Roach            $sortby = 'NAME';
1870a6f13a4aSGreg Roach        }
1871a6f13a4aSGreg Roach
1872e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1873195b5e75SGreg Roach
1874a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1875a6f13a4aSGreg Roach        switch ($listname) {
18767a6ee1acSGreg Roach            case 'pending':
18776c0bef3aSGreg Roach                $xrefs = DB::table('change')
1878195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1879a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1880195b5e75SGreg Roach                            ->from('change')
1881195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1882195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18837f5c2944SGreg Roach                            ->groupBy(['xref']);
1884195b5e75SGreg Roach                    })
18856c0bef3aSGreg Roach                    ->pluck('xref');
1886195b5e75SGreg Roach
188713abd6f3SGreg Roach                $this->list = [];
18886c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18896b9cb339SGreg Roach                    $this->list[] = Registry::gedcomRecordFactory()->make($xref, $this->tree);
1890a6f13a4aSGreg Roach                }
1891a6f13a4aSGreg Roach                break;
1892a6f13a4aSGreg Roach            case 'individual':
18935985adfbSGreg Roach                $query = DB::table('individuals')
18945985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18955985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18965985adfbSGreg Roach                    ->distinct();
18975985adfbSGreg Roach
1898a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1899dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
190082759250SGreg Roach                        $value = $this->substituteVars($value, false);
1901a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1902a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19030b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19045985adfbSGreg Roach                                $join
19055985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19065985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19075985adfbSGreg Roach                            });
19085985adfbSGreg Roach
19095985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19105985adfbSGreg Roach
1911a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19125985adfbSGreg Roach
1913044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19145985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1915a6f13a4aSGreg Roach                            } else {
19165985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1917a6f13a4aSGreg Roach                            }
19185985adfbSGreg Roach
19195985adfbSGreg Roach                            // This filter has been fully processed
19205985adfbSGreg Roach                            unset($attrs[$attr]);
19217ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19220b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19235985adfbSGreg Roach                                $join
19245985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19255985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19265985adfbSGreg Roach                            });
1927a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19287a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19295d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1930b5961194SGreg Roach                                $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
1931a6f13a4aSGreg Roach                            }
19325985adfbSGreg Roach
19335985adfbSGreg Roach                            // This filter has been fully processed
19345985adfbSGreg Roach                            unset($attrs[$attr]);
1935a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19365985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1937a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19385985adfbSGreg Roach
1939a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19405985adfbSGreg Roach
19415985adfbSGreg Roach                            // This filter has been fully processed
19425985adfbSGreg Roach                            unset($attrs[$attr]);
19432fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19449dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19455985adfbSGreg Roach                            $query
19460b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19475985adfbSGreg Roach                                    $join
19487ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19497ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19505985adfbSGreg Roach                                })
19510b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19527ee4bfadSGreg Roach                                    $join
19537ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19547ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19557ee4bfadSGreg Roach                                })
19569b44b7f5SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
19576e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19585985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19597ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19606e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19616e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
196247a2bfb7SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) {
19636e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19646e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1965e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19667ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1967a6f13a4aSGreg Roach                        }
1968a6f13a4aSGreg Roach                    }
1969a6f13a4aSGreg Roach                }
1970a6f13a4aSGreg Roach
197113abd6f3SGreg Roach                $this->list = [];
1972a6f13a4aSGreg Roach
19735985adfbSGreg Roach                foreach ($query->get() as $row) {
19746b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom);
1975a6f13a4aSGreg Roach                }
1976a6f13a4aSGreg Roach                break;
1977a6f13a4aSGreg Roach
1978a6f13a4aSGreg Roach            case 'family':
197930fc2b1eSGreg Roach                $query = DB::table('families')
198030fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
198130fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
198230fc2b1eSGreg Roach                    ->distinct();
198330fc2b1eSGreg Roach
1984a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1985dec352c1SGreg Roach                    if (str_starts_with($attr, 'filter') && $value !== '') {
198682759250SGreg Roach                        $value = $this->substituteVars($value, false);
1987a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1988a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19890b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
199030fc2b1eSGreg Roach                                $join
199130fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
199230fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
199330fc2b1eSGreg Roach                            });
199430fc2b1eSGreg Roach
199530fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
199630fc2b1eSGreg Roach
1997a6f13a4aSGreg Roach                            $date = new Date($match[3]);
199830fc2b1eSGreg Roach
1999044416d2SGreg Roach                            if ($match[2] === 'LTE') {
200030fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2001a6f13a4aSGreg Roach                            } else {
200230fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2003a6f13a4aSGreg Roach                            }
200430fc2b1eSGreg Roach
200530fc2b1eSGreg Roach                            // This filter has been fully processed
200630fc2b1eSGreg Roach                            unset($attrs[$attr]);
2007a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
200830fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2009a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
201030fc2b1eSGreg Roach
2011a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
201230fc2b1eSGreg Roach
201330fc2b1eSGreg Roach                            // This filter has been fully processed
201430fc2b1eSGreg Roach                            unset($attrs[$attr]);
201530fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20163b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20170b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
201830fc2b1eSGreg Roach                                    $join
201930fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20203b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
202130fc2b1eSGreg Roach                                            $query
202230fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
202330fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
202430fc2b1eSGreg Roach                                        });
202530fc2b1eSGreg Roach                                });
20265d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20277a6ee1acSGreg Roach                                if ($match[1] != '') {
20287a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20295d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2030b5961194SGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%');
20315d0bc43dSGreg Roach                                    }
20325d0bc43dSGreg Roach                                }
2033a6f13a4aSGreg Roach                            }
203430fc2b1eSGreg Roach
203530fc2b1eSGreg Roach                            // This filter has been fully processed
203630fc2b1eSGreg Roach                            unset($attrs[$attr]);
20376e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20389dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
203930fc2b1eSGreg Roach                            $query
20400b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
204130fc2b1eSGreg Roach                                    $join
20426e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20436e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
204430fc2b1eSGreg Roach                                })
20450b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20466e5c0963SGreg Roach                                    $join
20476e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20486e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20496e5c0963SGreg Roach                                })
2050b5961194SGreg Roach                                ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%');
20516e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
205230fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20536e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20546e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20556e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20566e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20576e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20586e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2059e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20606e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2061a6f13a4aSGreg Roach                        }
2062a6f13a4aSGreg Roach                    }
2063a6f13a4aSGreg Roach                }
2064a6f13a4aSGreg Roach
206513abd6f3SGreg Roach                $this->list = [];
2066a6f13a4aSGreg Roach
206730fc2b1eSGreg Roach                foreach ($query->get() as $row) {
20686b9cb339SGreg Roach                    $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom);
2069a6f13a4aSGreg Roach                }
2070a6f13a4aSGreg Roach                break;
2071a6f13a4aSGreg Roach
2072a6f13a4aSGreg Roach            default:
20736ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2074a6f13a4aSGreg Roach        }
2075a6f13a4aSGreg Roach
207613abd6f3SGreg Roach        $filters  = [];
207713abd6f3SGreg Roach        $filters2 = [];
2078a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2079a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2080a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2081a6f13a4aSGreg Roach                    $condition = $value;
2082a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2083a6f13a4aSGreg Roach                        $id    = $match[1];
2084a6f13a4aSGreg Roach                        $value = "''";
2085044416d2SGreg Roach                        if ($id === 'ID') {
20867a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2087a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2088a6f13a4aSGreg Roach                            }
2089044416d2SGreg Roach                        } elseif ($id === 'fact') {
2090a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2091044416d2SGreg Roach                        } elseif ($id === 'desc') {
2092a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2093a6f13a4aSGreg Roach                        } else {
2094a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20957a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2096a6f13a4aSGreg Roach                            }
2097a6f13a4aSGreg Roach                        }
2098a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2099a6f13a4aSGreg Roach                    }
2100a6f13a4aSGreg Roach                    //-- handle regular expressions
2101a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2102a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2103a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2104a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2105a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2106d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2107a6f13a4aSGreg Roach                            $val = trim($val);
2108a6f13a4aSGreg Roach                        }
2109a6f13a4aSGreg Roach                        if ($val) {
21107a6ee1acSGreg Roach                            $searchstr = '';
21117a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2112a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2113a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2114a6f13a4aSGreg Roach                                $level = 1;
2115f71a7dedSGreg Roach                                $t = 'XXXX';
2116a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2117a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2118a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2119a6f13a4aSGreg Roach                                    }
2120a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2121044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21227a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2123a6f13a4aSGreg Roach                                    }
21247a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2125a6f13a4aSGreg Roach                                    $level++;
2126a6f13a4aSGreg Roach                                }
2127a6f13a4aSGreg Roach                            } else {
2128044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21297a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2130a6f13a4aSGreg Roach                                }
2131a6f13a4aSGreg Roach                                $t         = $tag;
21327a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2133a6f13a4aSGreg Roach                            }
2134a6f13a4aSGreg Roach                            switch ($expr) {
21357a6ee1acSGreg Roach                                case 'CONTAINS':
2136044416d2SGreg Roach                                    if ($t === 'PLAC') {
2137a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2138a6f13a4aSGreg Roach                                    } else {
2139a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2140a6f13a4aSGreg Roach                                    }
2141a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2142a6f13a4aSGreg Roach                                    break;
2143a6f13a4aSGreg Roach                                default:
2144c1010edaSGreg Roach                                    $filters2[] = [
2145c1010edaSGreg Roach                                        'tag'  => $tag,
2146c1010edaSGreg Roach                                        'expr' => $expr,
2147c1010edaSGreg Roach                                        'val'  => $val,
2148c1010edaSGreg Roach                                    ];
2149a6f13a4aSGreg Roach                                    break;
2150a6f13a4aSGreg Roach                            }
2151a6f13a4aSGreg Roach                        }
2152a6f13a4aSGreg Roach                    }
2153a6f13a4aSGreg Roach                }
2154a6f13a4aSGreg Roach            }
2155a6f13a4aSGreg Roach        }
2156a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2157a6f13a4aSGreg Roach        if ($filters) {
2158a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2159a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2160299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2161a6f13a4aSGreg Roach                        unset($this->list[$key]);
2162a6f13a4aSGreg Roach                        break;
2163a6f13a4aSGreg Roach                    }
2164a6f13a4aSGreg Roach                }
2165a6f13a4aSGreg Roach            }
2166a6f13a4aSGreg Roach        }
2167a6f13a4aSGreg Roach        if ($filters2) {
216813abd6f3SGreg Roach            $mylist = [];
2169a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2170c0935879SGreg Roach                $key  = $indi->xref();
2171299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2172a6f13a4aSGreg Roach                $keep = true;
2173a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2174a6f13a4aSGreg Roach                    if ($keep) {
2175a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2176a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2177a6f13a4aSGreg Roach                        $val  = $filter['val'];
2178b2448a1bSGreg Roach                        if ($val === "''") {
21797a6ee1acSGreg Roach                            $val = '';
2180a6f13a4aSGreg Roach                        }
21817a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2182a6f13a4aSGreg Roach                        $t    = end($tags);
21833d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2184a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2185044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21867a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21877a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2188a6f13a4aSGreg Roach                            $t    = end($tags);
21893d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2190a6f13a4aSGreg Roach                        }
2191a6f13a4aSGreg Roach
2192a6f13a4aSGreg Roach                        switch ($expr) {
21937a6ee1acSGreg Roach                            case 'GTE':
2194044416d2SGreg Roach                                if ($t === 'DATE') {
2195a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2196a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2197a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2198a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2199a6f13a4aSGreg Roach                                    $keep = true;
2200a6f13a4aSGreg Roach                                }
2201a6f13a4aSGreg Roach                                break;
22027a6ee1acSGreg Roach                            case 'LTE':
2203044416d2SGreg Roach                                if ($t === 'DATE') {
2204a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2205a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2206a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2207a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2208a6f13a4aSGreg Roach                                    $keep = true;
2209a6f13a4aSGreg Roach                                }
2210a6f13a4aSGreg Roach                                break;
2211a6f13a4aSGreg Roach                            default:
2212a6f13a4aSGreg Roach                                if ($v == $val) {
2213a6f13a4aSGreg Roach                                    $keep = true;
2214a6f13a4aSGreg Roach                                } else {
2215a6f13a4aSGreg Roach                                    $keep = false;
2216a6f13a4aSGreg Roach                                }
2217a6f13a4aSGreg Roach                                break;
2218a6f13a4aSGreg Roach                        }
2219a6f13a4aSGreg Roach                    }
2220a6f13a4aSGreg Roach                }
2221a6f13a4aSGreg Roach                if ($keep) {
2222a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2223a6f13a4aSGreg Roach                }
2224a6f13a4aSGreg Roach            }
2225a6f13a4aSGreg Roach            $this->list = $mylist;
2226a6f13a4aSGreg Roach        }
2227a6f13a4aSGreg Roach
2228a6f13a4aSGreg Roach        switch ($sortby) {
2229a6f13a4aSGreg Roach            case 'NAME':
2230c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2231a6f13a4aSGreg Roach                break;
2232a6f13a4aSGreg Roach            case 'CHAN':
2233c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2234a6f13a4aSGreg Roach                break;
2235a6f13a4aSGreg Roach            case 'BIRT:DATE':
2236c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2237a6f13a4aSGreg Roach                break;
2238a6f13a4aSGreg Roach            case 'DEAT:DATE':
2239c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2240a6f13a4aSGreg Roach                break;
2241a6f13a4aSGreg Roach            case 'MARR:DATE':
2242c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2243a6f13a4aSGreg Roach                break;
2244a6f13a4aSGreg Roach            default:
2245a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2246a6f13a4aSGreg Roach                break;
2247a6f13a4aSGreg Roach        }
2248a6f13a4aSGreg Roach
22499b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2250e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2251a6f13a4aSGreg Roach    }
2252a6f13a4aSGreg Roach
2253a6f13a4aSGreg Roach    /**
2254fab8f067SGreg Roach     * Handle </list>
22558ba2e626SGreg Roach     *
22568ba2e626SGreg Roach     * @return void
2257a6f13a4aSGreg Roach     */
2258b702978eSGreg Roach    protected function listEndHandler(): void
2259c1010edaSGreg Roach    {
2260a6f13a4aSGreg Roach        $this->process_repeats--;
2261a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2262a6f13a4aSGreg Roach            return;
2263a6f13a4aSGreg Roach        }
2264a6f13a4aSGreg Roach
2265a6f13a4aSGreg Roach        // Check if there is any list
2266a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2267a6f13a4aSGreg Roach            $lineoffset = 0;
2268a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2269a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2270a6f13a4aSGreg Roach            }
2271a6f13a4aSGreg Roach            //-- read the xml from the file
2272299d100dSGreg Roach            $lines = file($this->report);
2273dec352c1SGreg Roach            while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) {
2274a6f13a4aSGreg Roach                $lineoffset--;
2275a6f13a4aSGreg Roach            }
2276a6f13a4aSGreg Roach            $lineoffset++;
2277a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2278a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2279a6f13a4aSGreg Roach            // List Level counter
2280a6f13a4aSGreg Roach            $count = 1;
2281a6f13a4aSGreg Roach            while (0 < $count) {
2282dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<List')) {
2283a6f13a4aSGreg Roach                    $count++;
2284dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</List')) {
2285a6f13a4aSGreg Roach                    $count--;
2286a6f13a4aSGreg Roach                }
2287a6f13a4aSGreg Roach                if (0 < $count) {
2288a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2289a6f13a4aSGreg Roach                }
2290a6f13a4aSGreg Roach                $line_nr++;
2291a6f13a4aSGreg Roach            }
2292a6f13a4aSGreg Roach            // No need to drag this
2293a6f13a4aSGreg Roach            unset($lines);
22947a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2295a6f13a4aSGreg Roach            // Save original values
22969b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2297a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2298a6f13a4aSGreg Roach
2299a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2300a6f13a4aSGreg Roach            $this->list_private = 0;
2301a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2302a6f13a4aSGreg Roach                if ($record->canShow()) {
2303f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2304a6f13a4aSGreg Roach                    //-- start the sax parser
2305a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2306e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2307a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23081aa04befSGreg Roach
23091aa04befSGreg Roach                    xml_set_element_handler(
23101aa04befSGreg Roach                        $repeat_parser,
23119d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23121aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23131aa04befSGreg Roach                        },
23149d454b6bSGreg Roach                        function ($parser, string $name): void {
23151aa04befSGreg Roach                            $this->endElement($parser, $name);
23161aa04befSGreg Roach                        }
23171aa04befSGreg Roach                    );
23181aa04befSGreg Roach
23191aa04befSGreg Roach                    xml_set_character_data_handler(
23201aa04befSGreg Roach                        $repeat_parser,
23219d454b6bSGreg Roach                        function ($parser, string $data): void {
23221aa04befSGreg Roach                            $this->characterData($parser, $data);
23231aa04befSGreg Roach                        }
23241aa04befSGreg Roach                    );
23251aa04befSGreg Roach
2326a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23276ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2328a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2329a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2330a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2331a6f13a4aSGreg Roach                        ));
2332a6f13a4aSGreg Roach                    }
2333a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2334a6f13a4aSGreg Roach                } else {
2335a6f13a4aSGreg Roach                    $this->list_private++;
2336a6f13a4aSGreg Roach                }
2337a6f13a4aSGreg Roach            }
233813abd6f3SGreg Roach            $this->list   = [];
2339e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2340a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2341a6f13a4aSGreg Roach        }
234265e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2343a6f13a4aSGreg Roach    }
2344a6f13a4aSGreg Roach
2345a6f13a4aSGreg Roach    /**
2346fab8f067SGreg Roach     * Handle <listTotal>
2347a6f13a4aSGreg Roach     * Prints the total number of records in a list
2348fab8f067SGreg Roach     * The total number is collected from <list> and <relatives>
23498ba2e626SGreg Roach     *
23508ba2e626SGreg Roach     * @return void
2351a6f13a4aSGreg Roach     */
2352b702978eSGreg Roach    protected function listTotalStartHandler(): void
2353c1010edaSGreg Roach    {
2354a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2355589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2356a6f13a4aSGreg Roach        } else {
23577a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2358a6f13a4aSGreg Roach        }
2359a6f13a4aSGreg Roach    }
2360a6f13a4aSGreg Roach
2361a6f13a4aSGreg Roach    /**
2362fab8f067SGreg Roach     * Handle <relatives>
236376692c8bSGreg Roach     *
2364fab8f067SGreg Roach     * @param string[] $attrs
23658ba2e626SGreg Roach     *
23668ba2e626SGreg Roach     * @return void
2367a6f13a4aSGreg Roach     */
2368b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2369c1010edaSGreg Roach    {
2370a6f13a4aSGreg Roach        $this->process_repeats++;
2371a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2372a6f13a4aSGreg Roach            return;
2373a6f13a4aSGreg Roach        }
2374a6f13a4aSGreg Roach
2375e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2376e364afe4SGreg Roach
237713abd6f3SGreg Roach        $match = [];
2378a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2379d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2380a6f13a4aSGreg Roach            $sortby = trim($sortby);
2381a6f13a4aSGreg Roach        }
2382a6f13a4aSGreg Roach
2383a6f13a4aSGreg Roach        $maxgen = -1;
2384a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2385c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2386a6f13a4aSGreg Roach        }
2387a6f13a4aSGreg Roach
2388e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2389e364afe4SGreg Roach
2390a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2391d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2392a6f13a4aSGreg Roach            $group = trim($group);
2393a6f13a4aSGreg Roach        }
2394a6f13a4aSGreg Roach
2395e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2396e364afe4SGreg Roach
2397a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2398d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2399a6f13a4aSGreg Roach            $id = trim($id);
2400a6f13a4aSGreg Roach        }
2401a6f13a4aSGreg Roach
240213abd6f3SGreg Roach        $this->list = [];
24036b9cb339SGreg Roach        $person     = Registry::individualFactory()->make($id, $this->tree);
2404d965cc1aSGreg Roach        if ($person instanceof Individual) {
2405a6f13a4aSGreg Roach            $this->list[$id] = $person;
2406a6f13a4aSGreg Roach            switch ($group) {
24077a6ee1acSGreg Roach                case 'child-family':
240839ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2409820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2410820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2411a6f13a4aSGreg Roach                        }
2412820b62dfSGreg Roach
2413820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2414c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2415a6f13a4aSGreg Roach                        }
2416a6f13a4aSGreg Roach                    }
2417a6f13a4aSGreg Roach                    break;
24187a6ee1acSGreg Roach                case 'spouse-family':
241939ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2420820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2421820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2422a6f13a4aSGreg Roach                        }
2423820b62dfSGreg Roach
2424820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2425c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2426a6f13a4aSGreg Roach                        }
2427a6f13a4aSGreg Roach                    }
2428a6f13a4aSGreg Roach                    break;
24297a6ee1acSGreg Roach                case 'direct-ancestors':
24303d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2431a6f13a4aSGreg Roach                    break;
24327a6ee1acSGreg Roach                case 'ancestors':
24333d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2434a6f13a4aSGreg Roach                    break;
24357a6ee1acSGreg Roach                case 'descendants':
2436a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24373d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2438a6f13a4aSGreg Roach                    break;
24397a6ee1acSGreg Roach                case 'all':
24403d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24413d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2442a6f13a4aSGreg Roach                    break;
2443a6f13a4aSGreg Roach            }
2444a6f13a4aSGreg Roach        }
2445a6f13a4aSGreg Roach
2446a6f13a4aSGreg Roach        switch ($sortby) {
2447a6f13a4aSGreg Roach            case 'NAME':
2448c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2449a6f13a4aSGreg Roach                break;
2450a6f13a4aSGreg Roach            case 'BIRT:DATE':
2451c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2452a6f13a4aSGreg Roach                break;
2453a6f13a4aSGreg Roach            case 'DEAT:DATE':
2454c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2455a6f13a4aSGreg Roach                break;
2456a6f13a4aSGreg Roach            case 'generation':
245713abd6f3SGreg Roach                $newarray = [];
2458a6f13a4aSGreg Roach                reset($this->list);
2459a6f13a4aSGreg Roach                $genCounter = 1;
2460a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2461a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2462a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2463a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
246479529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2465a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2466a6f13a4aSGreg Roach                        }
2467a6f13a4aSGreg Roach                    }
2468a6f13a4aSGreg Roach                    $genCounter++;
2469a6f13a4aSGreg Roach                }
2470a6f13a4aSGreg Roach                $this->list = $newarray;
2471a6f13a4aSGreg Roach                break;
2472a6f13a4aSGreg Roach            default:
2473a6f13a4aSGreg Roach                // unsorted
2474a6f13a4aSGreg Roach                break;
2475a6f13a4aSGreg Roach        }
24769b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2477e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2478a6f13a4aSGreg Roach    }
2479a6f13a4aSGreg Roach
2480a6f13a4aSGreg Roach    /**
2481fab8f067SGreg Roach     * Handle </relatives>
24828ba2e626SGreg Roach     *
24838ba2e626SGreg Roach     * @return void
2484a6f13a4aSGreg Roach     */
2485b702978eSGreg Roach    protected function relativesEndHandler(): void
2486c1010edaSGreg Roach    {
2487a6f13a4aSGreg Roach        $this->process_repeats--;
2488a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2489a6f13a4aSGreg Roach            return;
2490a6f13a4aSGreg Roach        }
2491a6f13a4aSGreg Roach
2492a6f13a4aSGreg Roach        // Check if there is any relatives
2493a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2494a6f13a4aSGreg Roach            $lineoffset = 0;
2495a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2496a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2497a6f13a4aSGreg Roach            }
2498a6f13a4aSGreg Roach            //-- read the xml from the file
2499299d100dSGreg Roach            $lines = file($this->report);
2500dec352c1SGreg Roach            while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) {
2501a6f13a4aSGreg Roach                $lineoffset--;
2502a6f13a4aSGreg Roach            }
2503a6f13a4aSGreg Roach            $lineoffset++;
2504a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2505a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2506a6f13a4aSGreg Roach            // Relatives Level counter
2507a6f13a4aSGreg Roach            $count = 1;
2508a6f13a4aSGreg Roach            while (0 < $count) {
2509dec352c1SGreg Roach                if (str_contains($lines[$line_nr], '<Relatives')) {
2510a6f13a4aSGreg Roach                    $count++;
2511dec352c1SGreg Roach                } elseif (str_contains($lines[$line_nr], '</Relatives')) {
2512a6f13a4aSGreg Roach                    $count--;
2513a6f13a4aSGreg Roach                }
2514a6f13a4aSGreg Roach                if (0 < $count) {
2515a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2516a6f13a4aSGreg Roach                }
2517a6f13a4aSGreg Roach                $line_nr++;
2518a6f13a4aSGreg Roach            }
2519a6f13a4aSGreg Roach            // No need to drag this
2520a6f13a4aSGreg Roach            unset($lines);
2521a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2522a6f13a4aSGreg Roach            // Save original values
25239b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2524a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2525a6f13a4aSGreg Roach
2526a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2527a6f13a4aSGreg Roach            $this->list_private = 0;
2528b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2529a6f13a4aSGreg Roach                if (isset($value->generation)) {
2530a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2531a6f13a4aSGreg Roach                }
25326b9cb339SGreg Roach                $tmp          = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree);
2533299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2534a6f13a4aSGreg Roach
2535a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2536e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2537a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25381aa04befSGreg Roach
25391aa04befSGreg Roach                xml_set_element_handler(
25401aa04befSGreg Roach                    $repeat_parser,
25419d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25421aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25431aa04befSGreg Roach                    },
25449d454b6bSGreg Roach                    function ($parser, string $name): void {
25451aa04befSGreg Roach                        $this->endElement($parser, $name);
25461aa04befSGreg Roach                    }
25471aa04befSGreg Roach                );
25481aa04befSGreg Roach
25491aa04befSGreg Roach                xml_set_character_data_handler(
25501aa04befSGreg Roach                    $repeat_parser,
25519d454b6bSGreg Roach                    function ($parser, string $data): void {
25521aa04befSGreg Roach                        $this->characterData($parser, $data);
25531aa04befSGreg Roach                    }
25541aa04befSGreg Roach                );
2555a6f13a4aSGreg Roach
2556a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25576ccdf4f0SGreg 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)));
2558a6f13a4aSGreg Roach                }
2559a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2560a6f13a4aSGreg Roach            }
2561a6f13a4aSGreg Roach            // Clean up the list array
256213abd6f3SGreg Roach            $this->list   = [];
2563e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2564a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2565a6f13a4aSGreg Roach        }
256665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2567a6f13a4aSGreg Roach    }
2568a6f13a4aSGreg Roach
2569a6f13a4aSGreg Roach    /**
2570fab8f067SGreg Roach     * Handle <generation />
2571a6f13a4aSGreg Roach     * Prints the number of generations
25728ba2e626SGreg Roach     *
25738ba2e626SGreg Roach     * @return void
2574a6f13a4aSGreg Roach     */
2575b702978eSGreg Roach    protected function generationStartHandler(): void
2576c1010edaSGreg Roach    {
2577589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2578a6f13a4aSGreg Roach    }
2579a6f13a4aSGreg Roach
2580a6f13a4aSGreg Roach    /**
2581fab8f067SGreg Roach     * Handle <newPage />
2582a393a2a1SGreg Roach     * Has to be placed in an element (header, body or footer)
25838ba2e626SGreg Roach     *
25848ba2e626SGreg Roach     * @return void
2585a6f13a4aSGreg Roach     */
2586b702978eSGreg Roach    protected function newPageStartHandler(): void
2587c1010edaSGreg Roach    {
25887a6ee1acSGreg Roach        $temp = 'addpage';
2589e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2590a6f13a4aSGreg Roach    }
2591a6f13a4aSGreg Roach
2592a6f13a4aSGreg Roach    /**
2593fab8f067SGreg Roach     * Handle </title>
25948ba2e626SGreg Roach     *
25958ba2e626SGreg Roach     * @return void
2596a6f13a4aSGreg Roach     */
2597b702978eSGreg Roach    protected function titleEndHandler(): void
2598c1010edaSGreg Roach    {
25992836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2600a6f13a4aSGreg Roach    }
2601a6f13a4aSGreg Roach
2602a6f13a4aSGreg Roach    /**
2603fab8f067SGreg Roach     * Handle </description>
26048ba2e626SGreg Roach     *
26058ba2e626SGreg Roach     * @return void
2606a6f13a4aSGreg Roach     */
2607b702978eSGreg Roach    protected function descriptionEndHandler(): void
2608c1010edaSGreg Roach    {
26092836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2610a6f13a4aSGreg Roach    }
2611729ce104SGreg Roach
2612729ce104SGreg Roach    /**
261376692c8bSGreg Roach     * Create a list of all descendants.
261476692c8bSGreg Roach     *
2615729ce104SGreg Roach     * @param string[] $list
2616729ce104SGreg Roach     * @param string   $pid
2617729ce104SGreg Roach     * @param bool     $parents
2618729ce104SGreg Roach     * @param int      $generations
26198ba2e626SGreg Roach     *
26208ba2e626SGreg Roach     * @return void
2621729ce104SGreg Roach     */
26223b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2623c1010edaSGreg Roach    {
26246b9cb339SGreg Roach        $person = Registry::individualFactory()->make($pid, $this->tree);
2625729ce104SGreg Roach        if ($person === null) {
2626729ce104SGreg Roach            return;
2627729ce104SGreg Roach        }
2628729ce104SGreg Roach        if (!isset($list[$pid])) {
2629729ce104SGreg Roach            $list[$pid] = $person;
2630729ce104SGreg Roach        }
2631729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2632729ce104SGreg Roach            $list[$pid]->generation = 0;
2633729ce104SGreg Roach        }
263439ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2635729ce104SGreg Roach            if ($parents) {
263639ca88baSGreg Roach                $husband = $family->husband();
263739ca88baSGreg Roach                $wife    = $family->wife();
2638729ce104SGreg Roach                if ($husband) {
2639c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2640729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2641c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2642729ce104SGreg Roach                    } else {
2643c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2644729ce104SGreg Roach                    }
2645729ce104SGreg Roach                }
2646729ce104SGreg Roach                if ($wife) {
2647c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2648729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2649c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2650729ce104SGreg Roach                    } else {
2651c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2652729ce104SGreg Roach                    }
2653729ce104SGreg Roach                }
2654729ce104SGreg Roach            }
2655820b62dfSGreg Roach
265639ca88baSGreg Roach            $children = $family->children();
2657820b62dfSGreg Roach
2658729ce104SGreg Roach            foreach ($children as $child) {
2659729ce104SGreg Roach                if ($child) {
2660c0935879SGreg Roach                    $list[$child->xref()] = $child;
2661820b62dfSGreg Roach
2662729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2663c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2664729ce104SGreg Roach                    } else {
2665c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2666729ce104SGreg Roach                    }
2667729ce104SGreg Roach                }
2668729ce104SGreg Roach            }
2669729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2670729ce104SGreg Roach                foreach ($children as $child) {
2671c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2672729ce104SGreg Roach                }
2673729ce104SGreg Roach            }
2674729ce104SGreg Roach        }
2675729ce104SGreg Roach    }
2676729ce104SGreg Roach
2677729ce104SGreg Roach    /**
267876692c8bSGreg Roach     * Create a list of all ancestors.
267976692c8bSGreg Roach     *
2680870ec5a3SGreg Roach     * @param array<stdClass> $list
2681729ce104SGreg Roach     * @param string          $pid
2682729ce104SGreg Roach     * @param bool            $children
2683729ce104SGreg Roach     * @param int             $generations
26848ba2e626SGreg Roach     *
26858ba2e626SGreg Roach     * @return void
2686729ce104SGreg Roach     */
26873b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2688c1010edaSGreg Roach    {
268913abd6f3SGreg Roach        $genlist                = [$pid];
2690729ce104SGreg Roach        $list[$pid]->generation = 1;
2691729ce104SGreg Roach        while (count($genlist) > 0) {
2692729ce104SGreg Roach            $id = array_shift($genlist);
2693dec352c1SGreg Roach            if (str_starts_with($id, 'empty')) {
2694729ce104SGreg Roach                continue; // id can be something like “empty7”
2695729ce104SGreg Roach            }
26966b9cb339SGreg Roach            $person = Registry::individualFactory()->make($id, $this->tree);
269739ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
269839ca88baSGreg Roach                $husband = $family->husband();
269939ca88baSGreg Roach                $wife    = $family->wife();
2700729ce104SGreg Roach                if ($husband) {
2701c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2702c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2703729ce104SGreg Roach                }
2704729ce104SGreg Roach                if ($wife) {
2705c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2706c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2707729ce104SGreg Roach                }
2708729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2709729ce104SGreg Roach                    if ($husband) {
2710c0935879SGreg Roach                        $genlist[] = $husband->xref();
2711729ce104SGreg Roach                    }
2712729ce104SGreg Roach                    if ($wife) {
2713c0935879SGreg Roach                        $genlist[] = $wife->xref();
2714729ce104SGreg Roach                    }
2715729ce104SGreg Roach                }
2716729ce104SGreg Roach                if ($children) {
271739ca88baSGreg Roach                    foreach ($family->children() as $child) {
2718c0935879SGreg Roach                        $list[$child->xref()] = $child;
2719e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2720729ce104SGreg Roach                    }
2721729ce104SGreg Roach                }
2722729ce104SGreg Roach            }
2723729ce104SGreg Roach        }
2724729ce104SGreg Roach    }
2725729ce104SGreg Roach
2726729ce104SGreg Roach    /**
2727729ce104SGreg Roach     * get gedcom tag value
2728729ce104SGreg Roach     *
2729729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2730729ce104SGreg 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
2731729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2732729ce104SGreg Roach     *
2733729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2734729ce104SGreg Roach     */
2735b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2736c1010edaSGreg Roach    {
2737b2448a1bSGreg Roach        if ($gedrec === '') {
2738729ce104SGreg Roach            return '';
2739729ce104SGreg Roach        }
2740729ce104SGreg Roach        $tags      = explode(':', $tag);
2741729ce104SGreg Roach        $origlevel = $level;
2742b2448a1bSGreg Roach        if ($level === 0) {
27433c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2744729ce104SGreg Roach        }
2745729ce104SGreg Roach
2746729ce104SGreg Roach        $subrec = $gedrec;
2747f71a7dedSGreg Roach        $t = 'XXXX';
2748729ce104SGreg Roach        foreach ($tags as $t) {
2749729ce104SGreg Roach            $lastsubrec = $subrec;
27503d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2751729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2752729ce104SGreg Roach                $level--;
27533d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2754729ce104SGreg Roach            }
2755729ce104SGreg Roach            if (empty($subrec)) {
2756044416d2SGreg Roach                if ($t === 'TITL') {
27573d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2758729ce104SGreg Roach                    if (!empty($subrec)) {
27597a6ee1acSGreg Roach                        $t = 'ABBR';
2760729ce104SGreg Roach                    }
2761729ce104SGreg Roach                }
2762b2448a1bSGreg Roach                if ($subrec === '') {
2763729ce104SGreg Roach                    if ($level > 0) {
2764729ce104SGreg Roach                        $level--;
2765729ce104SGreg Roach                    }
27663d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2767b2448a1bSGreg Roach                    if ($subrec === '') {
2768729ce104SGreg Roach                        return '';
2769729ce104SGreg Roach                    }
2770729ce104SGreg Roach                }
2771729ce104SGreg Roach            }
2772729ce104SGreg Roach            $level++;
2773729ce104SGreg Roach        }
2774729ce104SGreg Roach        $level--;
2775729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2776f71a7dedSGreg Roach        if ($ct === 0) {
2777729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2778729ce104SGreg Roach        }
2779f71a7dedSGreg Roach        if ($ct === 0) {
2780729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2781729ce104SGreg Roach        }
2782729ce104SGreg Roach        if ($ct > 0) {
2783729ce104SGreg Roach            $value = trim($match[1]);
2784044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
27856b9cb339SGreg Roach                $note = Registry::noteFactory()->make($match[1], $this->tree);
2786ff166e64SGreg Roach                if ($note instanceof Note) {
2787729ce104SGreg Roach                    $value = $note->getNote();
2788729ce104SGreg Roach                } else {
2789729ce104SGreg Roach                    //-- set the value to the id without the @
2790729ce104SGreg Roach                    $value = $match[1];
2791729ce104SGreg Roach                }
2792729ce104SGreg Roach            }
2793f71a7dedSGreg Roach            if ($level !== 0 || $t !== 'NOTE') {
27943d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2795729ce104SGreg Roach            }
2796729ce104SGreg Roach
2797729ce104SGreg Roach            return $value;
2798729ce104SGreg Roach        }
2799729ce104SGreg Roach
28007a6ee1acSGreg Roach        return '';
2801729ce104SGreg Roach    }
2802d1286247SGreg Roach
2803d1286247SGreg Roach    /**
2804d1286247SGreg Roach     * Replace variable identifiers with their values.
2805d1286247SGreg Roach     *
2806d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
280782759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2808d1286247SGreg Roach     *
2809d1286247SGreg Roach     * @return string
2810d1286247SGreg Roach     */
28118f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2812c1010edaSGreg Roach    {
2813d1286247SGreg Roach        return preg_replace_callback(
2814d1286247SGreg Roach            '/\$(\w+)/',
281518d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28162118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
281782759250SGreg Roach                    if ($quote) {
28182118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2819b2ce94c6SRico Sonntag                    }
2820b2ce94c6SRico Sonntag
28212118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
282282759250SGreg Roach                }
2823b2ce94c6SRico Sonntag
2824d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28253d7a8a4cSGreg Roach
2826d1286247SGreg Roach                return '$' . $matches[1];
2827d1286247SGreg Roach            },
2828d1286247SGreg Roach            $expression
2829d1286247SGreg Roach        );
2830d1286247SGreg Roach    }
2831a6f13a4aSGreg Roach}
2832