xref: /webtrees/app/Report/ReportParserGenerate.php (revision b6f35a76f16ee5da672b7d3d886becc6b838498e)
1a6f13a4aSGreg Roach<?php
23976b470SGreg Roach
3a6f13a4aSGreg Roach/**
4a6f13a4aSGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a6f13a4aSGreg Roach * along with this program. If not, see <http://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;
26a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
27a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
283d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
299a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
33a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
34d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
35a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Media;
36a04bb9a2SGreg Roachuse Fisharebest\Webtrees\MediaFile;
37729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
38a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
39299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
40195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
41195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
42a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
435985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
449a9e551aSGreg Roachuse Illuminate\Support\Str;
45a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface;
4631d9777aSGreg Roachuse LogicException;
4779529c87SGreg Roachuse stdClass;
48c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
495809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
5059597b37SGreg Roach
51*b6f35a76SGreg Roachuse function addcslashes;
52*b6f35a76SGreg Roachuse function addslashes;
53*b6f35a76SGreg Roachuse function array_pop;
54*b6f35a76SGreg Roachuse function array_shift;
55*b6f35a76SGreg Roachuse function assert;
56b19e047dSGreg Roachuse function call_user_func;
57*b6f35a76SGreg Roachuse function count;
58*b6f35a76SGreg Roachuse function end;
59b2448a1bSGreg Roachuse function explode;
60*b6f35a76SGreg Roachuse function file;
61*b6f35a76SGreg Roachuse function file_exists;
62*b6f35a76SGreg Roachuse function getimagesize;
6329518ad2SGreg Roachuse function imagecreatefromstring;
6429518ad2SGreg Roachuse function imagesx;
6529518ad2SGreg Roachuse function imagesy;
66*b6f35a76SGreg Roachuse function in_array;
67b19e047dSGreg Roachuse function method_exists;
68*b6f35a76SGreg Roachuse function preg_match;
69*b6f35a76SGreg Roachuse function preg_match_all;
70*b6f35a76SGreg Roachuse function preg_replace;
71*b6f35a76SGreg Roachuse function preg_replace_callback;
72*b6f35a76SGreg Roachuse function preg_split;
73*b6f35a76SGreg Roachuse function reset;
74*b6f35a76SGreg Roachuse function round;
75*b6f35a76SGreg Roachuse function sprintf;
76*b6f35a76SGreg Roachuse function str_replace;
77*b6f35a76SGreg Roachuse function strip_tags;
78*b6f35a76SGreg Roachuse function strlen;
79*b6f35a76SGreg Roachuse function strpos;
80*b6f35a76SGreg Roachuse function strstr;
81*b6f35a76SGreg Roachuse function strtoupper;
82*b6f35a76SGreg Roachuse function substr;
83b2448a1bSGreg Roachuse function trim;
84*b6f35a76SGreg Roachuse function uasort;
85*b6f35a76SGreg Roachuse function xml_error_string;
86*b6f35a76SGreg Roachuse function xml_get_current_line_number;
87*b6f35a76SGreg Roachuse function xml_get_error_code;
88*b6f35a76SGreg Roachuse function xml_parse;
89*b6f35a76SGreg Roachuse function xml_parser_create;
90*b6f35a76SGreg Roachuse function xml_parser_free;
91*b6f35a76SGreg Roachuse function xml_parser_set_option;
92*b6f35a76SGreg Roachuse function xml_set_character_data_handler;
93*b6f35a76SGreg Roachuse function xml_set_element_handler;
94*b6f35a76SGreg Roach
95*b6f35a76SGreg Roachuse const PREG_SET_ORDER;
96*b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
9729518ad2SGreg Roach
98a6f13a4aSGreg Roach/**
99a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
100a6f13a4aSGreg Roach */
101c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
102c1010edaSGreg Roach{
103a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
104a6f13a4aSGreg Roach    private $process_footnote = true;
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
107a6f13a4aSGreg Roach    private $print_data = false;
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
11013abd6f3SGreg Roach    private $print_data_stack = [];
111a6f13a4aSGreg Roach
11276692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
113a6f13a4aSGreg Roach    private $process_gedcoms = 0;
114a6f13a4aSGreg Roach
11576692c8bSGreg Roach    /** @var int Are we processing conditionals */
116a6f13a4aSGreg Roach    private $process_ifs = 0;
117a6f13a4aSGreg Roach
11876692c8bSGreg Roach    /** @var int Are we processing repeats */
119a6f13a4aSGreg Roach    private $process_repeats = 0;
120a6f13a4aSGreg Roach
121a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
122a6f13a4aSGreg Roach    private $repeat_bytes = 0;
123a6f13a4aSGreg Roach
1245b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
12513abd6f3SGreg Roach    private $repeats = [];
126a6f13a4aSGreg Roach
127a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
12813abd6f3SGreg Roach    private $repeats_stack = [];
129a6f13a4aSGreg Roach
130*b6f35a76SGreg Roach    /** @var AbstractRenderer[] Nested repeating data */
13113abd6f3SGreg Roach    private $wt_report_stack = [];
132e8e7866bSGreg Roach
133e8e7866bSGreg Roach    /** @var resource Nested repeating data */
134e8e7866bSGreg Roach    private $parser;
135e8e7866bSGreg Roach
136e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
13713abd6f3SGreg Roach    private $parser_stack = [];
138e8e7866bSGreg Roach
139a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
140a6f13a4aSGreg Roach    private $gedrec = '';
141a6f13a4aSGreg Roach
142a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
14313abd6f3SGreg Roach    private $gedrec_stack = [];
144a6f13a4aSGreg Roach
145a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
146a6f13a4aSGreg Roach    private $current_element;
147a6f13a4aSGreg Roach
148a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
149a6f13a4aSGreg Roach    private $footnote_element;
150a6f13a4aSGreg Roach
151a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
152a6f13a4aSGreg Roach    private $fact = '';
153a6f13a4aSGreg Roach
154a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
155a6f13a4aSGreg Roach    private $desc = '';
156a6f13a4aSGreg Roach
157a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
158a6f13a4aSGreg Roach    private $type = '';
159a6f13a4aSGreg Roach
160a6f13a4aSGreg Roach    /** @var int The current generational level */
161a6f13a4aSGreg Roach    private $generation = 1;
162a6f13a4aSGreg Roach
163a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
16413abd6f3SGreg Roach    private $list = [];
165a6f13a4aSGreg Roach
166a6f13a4aSGreg Roach    /** @var int Number of items in lists */
167a6f13a4aSGreg Roach    private $list_total = 0;
168a6f13a4aSGreg Roach
169a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
170a6f13a4aSGreg Roach    private $list_private = 0;
171a6f13a4aSGreg Roach
172299d100dSGreg Roach    /** @var string The filename of the XML report */
173299d100dSGreg Roach    protected $report;
174299d100dSGreg Roach
175*b6f35a76SGreg Roach    /** @var AbstractRenderer A factory for creating report elements */
176e8e7866bSGreg Roach    private $report_root;
177e8e7866bSGreg Roach
178*b6f35a76SGreg Roach    /** @var AbstractRenderer Nested report elements */
179e8e7866bSGreg Roach    private $wt_report;
180e8e7866bSGreg Roach
181d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1822118c0e3SGreg Roach    private $vars;
183d1286247SGreg Roach
184299d100dSGreg Roach    /** @var Tree The current tree */
185299d100dSGreg Roach    private $tree;
186299d100dSGreg Roach
187a04bb9a2SGreg Roach    /** @var FilesystemInterface */
188a04bb9a2SGreg Roach    private $data_filesystem;
189a04bb9a2SGreg Roach
19076692c8bSGreg Roach    /**
19176692c8bSGreg Roach     * Create a parser for a report
19276692c8bSGreg Roach     *
19376692c8bSGreg Roach     * @param string              $report The XML filename
194*b6f35a76SGreg Roach     * @param AbstractRenderer    $report_root
19576692c8bSGreg Roach     * @param string[][]          $vars
196299d100dSGreg Roach     * @param Tree                $tree
197a04bb9a2SGreg Roach     * @param FilesystemInterface $data_filesystem
19876692c8bSGreg Roach     */
199a04bb9a2SGreg Roach    public function __construct(
200a04bb9a2SGreg Roach        string $report,
201*b6f35a76SGreg Roach        AbstractRenderer $report_root,
202a04bb9a2SGreg Roach        array $vars,
203a04bb9a2SGreg Roach        Tree $tree,
204a04bb9a2SGreg Roach        FilesystemInterface $data_filesystem
205a04bb9a2SGreg Roach    ) {
206299d100dSGreg Roach        $this->report          = $report;
207e8e7866bSGreg Roach        $this->report_root     = $report_root;
208e8e7866bSGreg Roach        $this->wt_report       = $report_root;
20959f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
210d1286247SGreg Roach        $this->vars            = $vars;
211299d100dSGreg Roach        $this->tree            = $tree;
212a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
213299d100dSGreg Roach
21476f666f4SGreg Roach        parent::__construct($report);
215a6f13a4aSGreg Roach    }
216a6f13a4aSGreg Roach
217a6f13a4aSGreg Roach    /**
218a6f13a4aSGreg Roach     * XML start element handler
219a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
220a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
221a6f13a4aSGreg Roach     *
222a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
223a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
2248a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
22518d7a90dSGreg Roach     *
22618d7a90dSGreg Roach     * @return void
227a6f13a4aSGreg Roach     */
228af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
229c1010edaSGreg Roach    {
23013abd6f3SGreg Roach        $newattrs = [];
231a6f13a4aSGreg Roach
232a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
233a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
234e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
235d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
236a6f13a4aSGreg Roach                }
237a6f13a4aSGreg Roach            }
238a6f13a4aSGreg Roach            $newattrs[$key] = $value;
239a6f13a4aSGreg Roach        }
240a6f13a4aSGreg Roach        $attrs = $newattrs;
2417a6ee1acSGreg Roach        if ($this->process_footnote && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag')) {
242a0e2939aSGreg Roach            $method = $name . 'StartHandler';
243208e9f76SGreg Roach
244a0e2939aSGreg Roach            if (method_exists($this, $method)) {
245b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
246a6f13a4aSGreg Roach            }
247a6f13a4aSGreg Roach        }
248a6f13a4aSGreg Roach    }
249a6f13a4aSGreg Roach
250a6f13a4aSGreg Roach    /**
251a6f13a4aSGreg Roach     * XML end element handler
252a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
253a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
254a6f13a4aSGreg Roach     *
255a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
256a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
25718d7a90dSGreg Roach     *
25818d7a90dSGreg Roach     * @return void
259a6f13a4aSGreg Roach     */
260af14d238SGreg Roach    protected function endElement($parser, string $name): void
261c1010edaSGreg Roach    {
2627a6ee1acSGreg Roach        if (($this->process_footnote || $name === 'Footnote') && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag' || $name === 'List' || $name === 'Relatives')) {
263a0e2939aSGreg Roach            $method = $name . 'EndHandler';
264a0e2939aSGreg Roach
265a0e2939aSGreg Roach            if (method_exists($this, $method)) {
266b19e047dSGreg Roach                call_user_func([$this, $method]);
267a6f13a4aSGreg Roach            }
268a6f13a4aSGreg Roach        }
269a6f13a4aSGreg Roach    }
270a6f13a4aSGreg Roach
271a6f13a4aSGreg Roach    /**
272a6f13a4aSGreg Roach     * XML character data handler
273a6f13a4aSGreg Roach     *
274a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
275a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
27618d7a90dSGreg Roach     *
27718d7a90dSGreg Roach     * @return void
278a6f13a4aSGreg Roach     */
279af14d238SGreg Roach    protected function characterData($parser, $data): void
280c1010edaSGreg Roach    {
281e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
282a6f13a4aSGreg Roach            $this->current_element->addText($data);
283a6f13a4aSGreg Roach        }
284a6f13a4aSGreg Roach    }
285a6f13a4aSGreg Roach
286a6f13a4aSGreg Roach    /**
28776692c8bSGreg Roach     * XML <style>
288a6f13a4aSGreg Roach     *
289c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2908ba2e626SGreg Roach     *
2918ba2e626SGreg Roach     * @return void
292a6f13a4aSGreg Roach     */
293b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
294c1010edaSGreg Roach    {
295a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2966ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
297a6f13a4aSGreg Roach        }
298a6f13a4aSGreg Roach
299a6f13a4aSGreg Roach        // array Style that will be passed on
30013abd6f3SGreg Roach        $s = [];
301a6f13a4aSGreg Roach
302a6f13a4aSGreg Roach        // string Name af the style
303a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
304a6f13a4aSGreg Roach
305a6f13a4aSGreg Roach        // string Name of the DEFAULT font
306208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
307a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
308a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
309a6f13a4aSGreg Roach        }
310a6f13a4aSGreg Roach
311a6f13a4aSGreg Roach        // int The size of the font in points
312208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
313a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
31459597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
31559597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
31659597b37SGreg Roach        }
317a6f13a4aSGreg Roach
318a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
3197a6ee1acSGreg Roach        $s['style'] = '';
320a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
321a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
322a6f13a4aSGreg Roach        }
323a6f13a4aSGreg Roach
324e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
325a6f13a4aSGreg Roach    }
326a6f13a4aSGreg Roach
327a6f13a4aSGreg Roach    /**
32876692c8bSGreg Roach     * XML <Doc>
329a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
330a6f13a4aSGreg Roach     *
331c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
3328ba2e626SGreg Roach     *
3338ba2e626SGreg Roach     * @return void
334a6f13a4aSGreg Roach     */
335b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
336c1010edaSGreg Roach    {
337e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
338a6f13a4aSGreg Roach
339a6f13a4aSGreg Roach        // Custom page width
340a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
341208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
342a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
343a6f13a4aSGreg Roach        // Custom Page height
344a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
345208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
346a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
347a6f13a4aSGreg Roach
348a6f13a4aSGreg Roach        // Left Margin
349a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
3507a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
351208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
352a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
353208e9f76SGreg Roach                $this->wt_report->left_margin = (int) $attrs['leftmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
354a6f13a4aSGreg Roach            }
355a6f13a4aSGreg Roach        }
356a6f13a4aSGreg Roach        // Right Margin
357a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3587a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
359208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
360a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
361208e9f76SGreg Roach                $this->wt_report->right_margin = (int) $attrs['rightmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
362a6f13a4aSGreg Roach            }
363a6f13a4aSGreg Roach        }
364a6f13a4aSGreg Roach        // Top Margin
365a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3667a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
367208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
368a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
369208e9f76SGreg Roach                $this->wt_report->top_margin = (int) $attrs['topmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
370a6f13a4aSGreg Roach            }
371a6f13a4aSGreg Roach        }
372a6f13a4aSGreg Roach        // Bottom Margin
373a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3747a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
375208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
376a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
377208e9f76SGreg Roach                $this->wt_report->bottom_margin = (int) $attrs['bottommargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
378a6f13a4aSGreg Roach            }
379a6f13a4aSGreg Roach        }
380a6f13a4aSGreg Roach        // Header Margin
381a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3827a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
383208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
384a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
385208e9f76SGreg Roach                $this->wt_report->header_margin = (int) $attrs['headermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
386a6f13a4aSGreg Roach            }
387a6f13a4aSGreg Roach        }
388a6f13a4aSGreg Roach        // Footer Margin
389a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3907a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
391208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
392a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
393208e9f76SGreg Roach                $this->wt_report->footer_margin = (int) $attrs['footermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0))
394a6f13a4aSGreg Roach            }
395a6f13a4aSGreg Roach        }
396a6f13a4aSGreg Roach
397a6f13a4aSGreg Roach        // Page Orientation
398a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
399044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
4007a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
401044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
4027a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
403a6f13a4aSGreg Roach            }
404a6f13a4aSGreg Roach        }
405a6f13a4aSGreg Roach        // Page Size
406a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
407208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
408a6f13a4aSGreg Roach        }
409a6f13a4aSGreg Roach
410a6f13a4aSGreg Roach        // Show Generated By...
411a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
4127a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
413208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
4147a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
415208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
416a6f13a4aSGreg Roach            }
417a6f13a4aSGreg Roach        }
418a6f13a4aSGreg Roach
419e8e7866bSGreg Roach        $this->wt_report->setup();
420a6f13a4aSGreg Roach    }
421a6f13a4aSGreg Roach
422a6f13a4aSGreg Roach    /**
42376692c8bSGreg Roach     * XML </Doc>
4248ba2e626SGreg Roach     *
4258ba2e626SGreg Roach     * @return void
426a6f13a4aSGreg Roach     */
427b702978eSGreg Roach    protected function docEndHandler(): void
428c1010edaSGreg Roach    {
429e8e7866bSGreg Roach        $this->wt_report->run();
430a6f13a4aSGreg Roach    }
431a6f13a4aSGreg Roach
432a6f13a4aSGreg Roach    /**
43376692c8bSGreg Roach     * XML <Header>
4348ba2e626SGreg Roach     *
4358ba2e626SGreg Roach     * @return void
436a6f13a4aSGreg Roach     */
437b702978eSGreg Roach    protected function headerStartHandler(): void
438c1010edaSGreg Roach    {
439a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
440e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4417a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
442a6f13a4aSGreg Roach    }
443a6f13a4aSGreg Roach
444a6f13a4aSGreg Roach    /**
44576692c8bSGreg Roach     * XML <bodyStartHandler>
4468ba2e626SGreg Roach     *
4478ba2e626SGreg Roach     * @return void
448a6f13a4aSGreg Roach     */
449b702978eSGreg Roach    protected function bodyStartHandler(): void
450c1010edaSGreg Roach    {
4517a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
452a6f13a4aSGreg Roach    }
453a6f13a4aSGreg Roach
454a6f13a4aSGreg Roach    /**
45576692c8bSGreg Roach     * XML <footerStartHandler>
4568ba2e626SGreg Roach     *
4578ba2e626SGreg Roach     * @return void
458a6f13a4aSGreg Roach     */
459b702978eSGreg Roach    protected function footerStartHandler(): void
460c1010edaSGreg Roach    {
4617a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
462a6f13a4aSGreg Roach    }
463a6f13a4aSGreg Roach
464a6f13a4aSGreg Roach    /**
46576692c8bSGreg Roach     * XML <Cell>
466a6f13a4aSGreg Roach     *
467c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4688ba2e626SGreg Roach     *
4698ba2e626SGreg Roach     * @return void
470a6f13a4aSGreg Roach     */
471b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
472c1010edaSGreg Roach    {
473a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4747a6ee1acSGreg Roach        $align = '';
475a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
476a6f13a4aSGreg Roach            $align = $attrs['align'];
477a6f13a4aSGreg Roach            // RTL supported left/right alignment
478044416d2SGreg Roach            if ($align === 'rightrtl') {
479e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4807a6ee1acSGreg Roach                    $align = 'left';
481a6f13a4aSGreg Roach                } else {
4827a6ee1acSGreg Roach                    $align = 'right';
483a6f13a4aSGreg Roach                }
484044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
485e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4867a6ee1acSGreg Roach                    $align = 'right';
487a6f13a4aSGreg Roach                } else {
4887a6ee1acSGreg Roach                    $align = 'left';
489a6f13a4aSGreg Roach                }
490a6f13a4aSGreg Roach            }
491a6f13a4aSGreg Roach        }
492a6f13a4aSGreg Roach
493a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4947a6ee1acSGreg Roach        $bgcolor = '';
495a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
496a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
497a6f13a4aSGreg Roach        }
498a6f13a4aSGreg Roach
499a6f13a4aSGreg Roach        // int Whether or not the background should be painted
500a6f13a4aSGreg Roach        $fill = 1;
501a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
5027a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
503a6f13a4aSGreg Roach                $fill = 0;
5047a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
505a6f13a4aSGreg Roach                $fill = 1;
506a6f13a4aSGreg Roach            }
507a6f13a4aSGreg Roach        }
508a6f13a4aSGreg Roach
509a6f13a4aSGreg Roach        $reseth = true;
510a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
511a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
5127a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
513a6f13a4aSGreg Roach                $reseth = false;
5147a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
515a6f13a4aSGreg Roach                $reseth = true;
516a6f13a4aSGreg Roach            }
517a6f13a4aSGreg Roach        }
518a6f13a4aSGreg Roach
519a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
520a6f13a4aSGreg Roach        $border = 0;
521a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
522a6f13a4aSGreg Roach            $border = $attrs['border'];
523a6f13a4aSGreg Roach        }
524a6f13a4aSGreg Roach        // string Border color in HTML code
5257a6ee1acSGreg Roach        $bocolor = '';
526a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
527a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
528a6f13a4aSGreg Roach        }
529a6f13a4aSGreg Roach
530a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
531a6f13a4aSGreg Roach        $height = 0;
532a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
533589feda3SGreg Roach            $height = $attrs['height'];
534a6f13a4aSGreg Roach        }
535a6f13a4aSGreg Roach        // int Cell width (expressed in points) Setting the width to 0 will make it the width from the current location to the right margin.
536a6f13a4aSGreg Roach        $width = 0;
537a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
538589feda3SGreg Roach            $width = $attrs['width'];
539a6f13a4aSGreg Roach        }
540a6f13a4aSGreg Roach
541a6f13a4aSGreg Roach        // int Stretch carachter mode
542a6f13a4aSGreg Roach        $stretch = 0;
543a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
544a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
545a6f13a4aSGreg Roach        }
546a6f13a4aSGreg Roach
547a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
548c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
549a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5507a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
551c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
552a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
553a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5547a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
555a6f13a4aSGreg Roach                $left = 0;
556a6f13a4aSGreg Roach            }
557a6f13a4aSGreg Roach        }
558a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
559c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
560a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5617a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
562c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
563a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
564a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5657a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
566a6f13a4aSGreg Roach                $top = 0;
567a6f13a4aSGreg Roach            }
568a6f13a4aSGreg Roach        }
569a6f13a4aSGreg Roach
570a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5717a6ee1acSGreg Roach        $style = '';
572a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
573a6f13a4aSGreg Roach            $style = $attrs['style'];
574a6f13a4aSGreg Roach        }
575a6f13a4aSGreg Roach
576a6f13a4aSGreg Roach        // string Text color in html code
5777a6ee1acSGreg Roach        $tcolor = '';
578a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
579a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
580a6f13a4aSGreg Roach        }
581a6f13a4aSGreg Roach
582a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
583a6f13a4aSGreg Roach        $ln = 0;
584a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
585a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
586a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5877a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
588a6f13a4aSGreg Roach                $ln = 0;
589a6f13a4aSGreg Roach            }
590a6f13a4aSGreg Roach        }
591a6f13a4aSGreg Roach
592044416d2SGreg Roach        if ($align === 'left') {
5937a6ee1acSGreg Roach            $align = 'L';
594044416d2SGreg Roach        } elseif ($align === 'right') {
5957a6ee1acSGreg Roach            $align = 'R';
596044416d2SGreg Roach        } elseif ($align === 'center') {
5977a6ee1acSGreg Roach            $align = 'C';
598044416d2SGreg Roach        } elseif ($align === 'justify') {
5997a6ee1acSGreg Roach            $align = 'J';
600a6f13a4aSGreg Roach        }
601a6f13a4aSGreg Roach
6029b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
603a6f13a4aSGreg Roach        $this->print_data         = true;
604a6f13a4aSGreg Roach
605e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
606a6f13a4aSGreg Roach            $width,
607a6f13a4aSGreg Roach            $height,
608a6f13a4aSGreg Roach            $border,
609a6f13a4aSGreg Roach            $align,
610a6f13a4aSGreg Roach            $bgcolor,
611a6f13a4aSGreg Roach            $style,
612a6f13a4aSGreg Roach            $ln,
613a6f13a4aSGreg Roach            $top,
614a6f13a4aSGreg Roach            $left,
615a6f13a4aSGreg Roach            $fill,
616a6f13a4aSGreg Roach            $stretch,
617a6f13a4aSGreg Roach            $bocolor,
618a6f13a4aSGreg Roach            $tcolor,
619a6f13a4aSGreg Roach            $reseth
620a6f13a4aSGreg Roach        );
621a6f13a4aSGreg Roach    }
622a6f13a4aSGreg Roach
623a6f13a4aSGreg Roach    /**
62476692c8bSGreg Roach     * XML </Cell>
6258ba2e626SGreg Roach     *
6268ba2e626SGreg Roach     * @return void
627a6f13a4aSGreg Roach     */
628b702978eSGreg Roach    protected function cellEndHandler(): void
629c1010edaSGreg Roach    {
630a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
631e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
632a6f13a4aSGreg Roach    }
633a6f13a4aSGreg Roach
634a6f13a4aSGreg Roach    /**
635a6f13a4aSGreg Roach     * XML <Now /> element handler
6368ba2e626SGreg Roach     *
6378ba2e626SGreg Roach     * @return void
638a6f13a4aSGreg Roach     */
639b702978eSGreg Roach    protected function nowStartHandler(): void
640c1010edaSGreg Roach    {
6414459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
642a6f13a4aSGreg Roach    }
643a6f13a4aSGreg Roach
644a6f13a4aSGreg Roach    /**
645a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6468ba2e626SGreg Roach     *
6478ba2e626SGreg Roach     * @return void
648a6f13a4aSGreg Roach     */
649b702978eSGreg Roach    protected function pageNumStartHandler(): void
650c1010edaSGreg Roach    {
6517a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
652a6f13a4aSGreg Roach    }
653a6f13a4aSGreg Roach
654a6f13a4aSGreg Roach    /**
655a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6568ba2e626SGreg Roach     *
6578ba2e626SGreg Roach     * @return void
658a6f13a4aSGreg Roach     */
659b702978eSGreg Roach    protected function totalPagesStartHandler(): void
660c1010edaSGreg Roach    {
6617a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
662a6f13a4aSGreg Roach    }
663a6f13a4aSGreg Roach
664a6f13a4aSGreg Roach    /**
665a6f13a4aSGreg Roach     * Called at the start of an element.
666a6f13a4aSGreg Roach     *
667c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6688ba2e626SGreg Roach     *
6698ba2e626SGreg Roach     * @return void
670a6f13a4aSGreg Roach     */
671b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
672c1010edaSGreg Roach    {
673a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
674a6f13a4aSGreg Roach            $this->process_gedcoms++;
675a6f13a4aSGreg Roach
676a6f13a4aSGreg Roach            return;
677a6f13a4aSGreg Roach        }
678a6f13a4aSGreg Roach
679a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6807a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6817a6ee1acSGreg Roach        $tags      = explode(':', $tag);
682a6f13a4aSGreg Roach        $newgedrec = '';
683a6f13a4aSGreg Roach        if (count($tags) < 2) {
684299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
685299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
686a6f13a4aSGreg Roach        }
687a6f13a4aSGreg Roach        if (empty($newgedrec)) {
688a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
689a6f13a4aSGreg Roach            $newgedrec = '';
690a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6917a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
692d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
693d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
694a6f13a4aSGreg Roach                    } else {
695299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
696299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
697a6f13a4aSGreg Roach                    }
698a6f13a4aSGreg Roach                } else {
6997a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
70013abd6f3SGreg Roach                        $gmatch = [];
701a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
702299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
703299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
704a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
705a6f13a4aSGreg Roach                        } else {
706a6f13a4aSGreg Roach                            $newgedrec = '';
707a6f13a4aSGreg Roach                            break;
708a6f13a4aSGreg Roach                        }
709a6f13a4aSGreg Roach                    } else {
710b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
7113d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
712a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
713a6f13a4aSGreg Roach                    }
714a6f13a4aSGreg Roach                }
715a6f13a4aSGreg Roach            }
716a6f13a4aSGreg Roach        }
717a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
7189b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
719a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
720a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
721a6f13a4aSGreg Roach                $this->fact = $match[2];
722a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
723a6f13a4aSGreg Roach            }
724a6f13a4aSGreg Roach        } else {
725a6f13a4aSGreg Roach            $this->process_gedcoms++;
726a6f13a4aSGreg Roach        }
727a6f13a4aSGreg Roach    }
728a6f13a4aSGreg Roach
729a6f13a4aSGreg Roach    /**
730a6f13a4aSGreg Roach     * Called at the end of an element.
7318ba2e626SGreg Roach     *
7328ba2e626SGreg Roach     * @return void
733a6f13a4aSGreg Roach     */
734b702978eSGreg Roach    protected function gedcomEndHandler(): void
735c1010edaSGreg Roach    {
736a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
737a6f13a4aSGreg Roach            $this->process_gedcoms--;
738a6f13a4aSGreg Roach        } else {
73965e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
740a6f13a4aSGreg Roach        }
741a6f13a4aSGreg Roach    }
742a6f13a4aSGreg Roach
743a6f13a4aSGreg Roach    /**
74476692c8bSGreg Roach     * XML <textBoxStartHandler>
745a6f13a4aSGreg Roach     *
746c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7478ba2e626SGreg Roach     *
7488ba2e626SGreg Roach     * @return void
749a6f13a4aSGreg Roach     */
750b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
751c1010edaSGreg Roach    {
752a6f13a4aSGreg Roach        // string Background color code
7537a6ee1acSGreg Roach        $bgcolor = '';
754a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
755a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
756a6f13a4aSGreg Roach        }
757a6f13a4aSGreg Roach
758a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
759a6f13a4aSGreg Roach        $fill = true;
760a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7617a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
762a6f13a4aSGreg Roach                $fill = false;
7637a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
764a6f13a4aSGreg Roach                $fill = true;
765a6f13a4aSGreg Roach            }
766a6f13a4aSGreg Roach        }
767a6f13a4aSGreg Roach
768a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
769a6f13a4aSGreg Roach        $border = false;
770a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7717a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
772a6f13a4aSGreg Roach                $border = true;
7737a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
774a6f13a4aSGreg Roach                $border = false;
775a6f13a4aSGreg Roach            }
776a6f13a4aSGreg Roach        }
777a6f13a4aSGreg Roach
778a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
779a6f13a4aSGreg Roach        $height = 0;
780a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
781a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
782a6f13a4aSGreg Roach        }
783a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
784a6f13a4aSGreg Roach        $width = 0;
785a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
786a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
787a6f13a4aSGreg Roach        }
788a6f13a4aSGreg Roach
789a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
790c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
791a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7927a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
793c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
794a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
795a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7967a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
797a6f13a4aSGreg Roach                $left = 0;
798a6f13a4aSGreg Roach            }
799a6f13a4aSGreg Roach        }
800a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
801c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
802a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
8037a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
804c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
805a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
806a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
8077a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
808a6f13a4aSGreg Roach                $top = 0;
809a6f13a4aSGreg Roach            }
810a6f13a4aSGreg Roach        }
811a6f13a4aSGreg Roach        // boolean After this box is finished rendering, should the next section of text start immediately after the this box or should it start on a new line under this box. 0 = no new line, 1 = force new line. Default is 0
812a6f13a4aSGreg Roach        $newline = false;
813a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
8147a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
815a6f13a4aSGreg Roach                $newline = true;
8167a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
817a6f13a4aSGreg Roach                $newline = false;
818a6f13a4aSGreg Roach            }
819a6f13a4aSGreg Roach        }
820a6f13a4aSGreg Roach        // boolean
821a6f13a4aSGreg Roach        $pagecheck = true;
822a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
8237a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
824a6f13a4aSGreg Roach                $pagecheck = false;
8257a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
826a6f13a4aSGreg Roach                $pagecheck = true;
827a6f13a4aSGreg Roach            }
828a6f13a4aSGreg Roach        }
829a6f13a4aSGreg Roach        // boolean Cell padding
830a6f13a4aSGreg Roach        $padding = true;
831a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8327a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
833a6f13a4aSGreg Roach                $padding = false;
8347a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
835a6f13a4aSGreg Roach                $padding = true;
836a6f13a4aSGreg Roach            }
837a6f13a4aSGreg Roach        }
838a6f13a4aSGreg Roach        // boolean Reset this box Height
839a6f13a4aSGreg Roach        $reseth = false;
840a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8417a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
842a6f13a4aSGreg Roach                $reseth = true;
8437a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
844a6f13a4aSGreg Roach                $reseth = false;
845a6f13a4aSGreg Roach            }
846a6f13a4aSGreg Roach        }
847a6f13a4aSGreg Roach
848a6f13a4aSGreg Roach        // string Style of rendering
8497a6ee1acSGreg Roach        $style = '';
850a6f13a4aSGreg Roach
8519b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
852a6f13a4aSGreg Roach        $this->print_data         = false;
853a6f13a4aSGreg Roach
8549b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
855e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
856a6f13a4aSGreg Roach            $width,
857a6f13a4aSGreg Roach            $height,
858a6f13a4aSGreg Roach            $border,
859a6f13a4aSGreg Roach            $bgcolor,
860a6f13a4aSGreg Roach            $newline,
861a6f13a4aSGreg Roach            $left,
862a6f13a4aSGreg Roach            $top,
863a6f13a4aSGreg Roach            $pagecheck,
864a6f13a4aSGreg Roach            $style,
865a6f13a4aSGreg Roach            $fill,
866a6f13a4aSGreg Roach            $padding,
867a6f13a4aSGreg Roach            $reseth
868a6f13a4aSGreg Roach        );
869a6f13a4aSGreg Roach    }
870a6f13a4aSGreg Roach
871a6f13a4aSGreg Roach    /**
87276692c8bSGreg Roach     * XML <textBoxEndHandler>
8738ba2e626SGreg Roach     *
8748ba2e626SGreg Roach     * @return void
875a6f13a4aSGreg Roach     */
876b702978eSGreg Roach    protected function textBoxEndHandler(): void
877c1010edaSGreg Roach    {
878a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
879e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
88031d9777aSGreg Roach
88131d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
88231d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
88331d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
88431d9777aSGreg Roach
885e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
886e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
887a6f13a4aSGreg Roach    }
888a6f13a4aSGreg Roach
889a6f13a4aSGreg Roach    /**
89076692c8bSGreg Roach     * XLM <Text>.
89176692c8bSGreg Roach     *
892c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8938ba2e626SGreg Roach     *
8948ba2e626SGreg Roach     * @return void
895a6f13a4aSGreg Roach     */
896b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
897c1010edaSGreg Roach    {
8989b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
899a6f13a4aSGreg Roach        $this->print_data         = true;
900a6f13a4aSGreg Roach
901a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
9027a6ee1acSGreg Roach        $style = '';
903a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
904a6f13a4aSGreg Roach            $style = $attrs['style'];
905a6f13a4aSGreg Roach        }
906a6f13a4aSGreg Roach
907a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
9087a6ee1acSGreg Roach        $color = '';
909a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
910a6f13a4aSGreg Roach            $color = $attrs['color'];
911a6f13a4aSGreg Roach        }
912a6f13a4aSGreg Roach
913e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
914a6f13a4aSGreg Roach    }
915a6f13a4aSGreg Roach
916a6f13a4aSGreg Roach    /**
91776692c8bSGreg Roach     * XML </Text>
9188ba2e626SGreg Roach     *
9198ba2e626SGreg Roach     * @return void
920a6f13a4aSGreg Roach     */
921b702978eSGreg Roach    protected function textEndHandler(): void
922c1010edaSGreg Roach    {
923a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
924e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
925a6f13a4aSGreg Roach    }
926a6f13a4aSGreg Roach
927a6f13a4aSGreg Roach    /**
92876692c8bSGreg Roach     * XML <GetPersonName/>
929a6f13a4aSGreg Roach     * Get the name
930a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
931a6f13a4aSGreg Roach     * 2. id is set with a record id
932a6f13a4aSGreg Roach     *
933c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9348ba2e626SGreg Roach     *
9358ba2e626SGreg Roach     * @return void
936a6f13a4aSGreg Roach     */
937b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
938c1010edaSGreg Roach    {
9397a6ee1acSGreg Roach        $id    = '';
94013abd6f3SGreg Roach        $match = [];
941a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9427a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
943a6f13a4aSGreg Roach                $id = $match[1];
944a6f13a4aSGreg Roach            }
945a6f13a4aSGreg Roach        } else {
9467a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
947d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
948d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
949a6f13a4aSGreg Roach                }
950a6f13a4aSGreg Roach            } else {
9517a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
95213abd6f3SGreg Roach                    $gmatch = [];
953a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
954a6f13a4aSGreg Roach                        $id = $gmatch[1];
955a6f13a4aSGreg Roach                    }
956a6f13a4aSGreg Roach                } else {
957a6f13a4aSGreg Roach                    $id = $attrs['id'];
958a6f13a4aSGreg Roach                }
959a6f13a4aSGreg Roach            }
960a6f13a4aSGreg Roach        }
961a6f13a4aSGreg Roach        if (!empty($id)) {
962299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9638f038c36SRico Sonntag            if ($record === null) {
964a6f13a4aSGreg Roach                return;
965a6f13a4aSGreg Roach            }
966a6f13a4aSGreg Roach            if (!$record->canShowName()) {
967a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
968a6f13a4aSGreg Roach            } else {
96939ca88baSGreg Roach                $name = $record->fullName();
970a6f13a4aSGreg Roach                $name = preg_replace(
971c1010edaSGreg Roach                    [
972c1010edaSGreg Roach                        '/<span class="starredname">/',
973c1010edaSGreg Roach                        '/<\/span><\/span>/',
974c1010edaSGreg Roach                        '/<\/span>/',
975c1010edaSGreg Roach                    ],
976c1010edaSGreg Roach                    [
977c1010edaSGreg Roach                        '«',
978c1010edaSGreg Roach                        '',
979c1010edaSGreg Roach                        '»',
980c1010edaSGreg Roach                    ],
981a6f13a4aSGreg Roach                    $name
982a6f13a4aSGreg Roach                );
983a6f13a4aSGreg Roach                $name = strip_tags($name);
984a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
985381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
986a6f13a4aSGreg Roach                } else {
98739ca88baSGreg Roach                    $addname = $record->alternateName();
988a6f13a4aSGreg Roach                    $addname = preg_replace(
989c1010edaSGreg Roach                        [
990c1010edaSGreg Roach                            '/<span class="starredname">/',
991c1010edaSGreg Roach                            '/<\/span><\/span>/',
992c1010edaSGreg Roach                            '/<\/span>/',
993c1010edaSGreg Roach                        ],
994c1010edaSGreg Roach                        [
995c1010edaSGreg Roach                            '«',
996c1010edaSGreg Roach                            '',
997c1010edaSGreg Roach                            '»',
998c1010edaSGreg Roach                        ],
999a6f13a4aSGreg Roach                        $addname
1000a6f13a4aSGreg Roach                    );
1001a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
1002a6f13a4aSGreg Roach                    if (!empty($addname)) {
10037a6ee1acSGreg Roach                        $name .= ' ' . $addname;
1004a6f13a4aSGreg Roach                    }
1005a6f13a4aSGreg Roach                }
1006a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
1007a6f13a4aSGreg Roach            }
1008a6f13a4aSGreg Roach        }
1009a6f13a4aSGreg Roach    }
1010a6f13a4aSGreg Roach
1011a6f13a4aSGreg Roach    /**
101276692c8bSGreg Roach     * XML <GedcomValue/>
1013a6f13a4aSGreg Roach     *
1014c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10158ba2e626SGreg Roach     *
10168ba2e626SGreg Roach     * @return void
1017a6f13a4aSGreg Roach     */
1018b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
1019c1010edaSGreg Roach    {
10207a6ee1acSGreg Roach        $id    = '';
102113abd6f3SGreg Roach        $match = [];
10227a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1023a6f13a4aSGreg Roach            $id = $match[1];
1024a6f13a4aSGreg Roach        }
1025a6f13a4aSGreg Roach
1026044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
10277a6ee1acSGreg Roach            $useBreak = '1';
1028a6f13a4aSGreg Roach        } else {
10297a6ee1acSGreg Roach            $useBreak = '0';
1030a6f13a4aSGreg Roach        }
1031a6f13a4aSGreg Roach
1032a6f13a4aSGreg Roach        $tag = $attrs['tag'];
1033a6f13a4aSGreg Roach        if (!empty($tag)) {
1034044416d2SGreg Roach            if ($tag === '@desc') {
1035a6f13a4aSGreg Roach                $value = $this->desc;
1036a6f13a4aSGreg Roach                $value = trim($value);
1037a6f13a4aSGreg Roach                $this->current_element->addText($value);
1038a6f13a4aSGreg Roach            }
1039044416d2SGreg Roach            if ($tag === '@id') {
1040a6f13a4aSGreg Roach                $this->current_element->addText($id);
1041a6f13a4aSGreg Roach            } else {
10427a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1043a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1044b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1045b2448a1bSGreg Roach                    if ($level === 0) {
1046a6f13a4aSGreg Roach                        $level++;
1047a6f13a4aSGreg Roach                    }
1048a6f13a4aSGreg Roach                } else {
1049b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1050a6f13a4aSGreg Roach                }
1051a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10523d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1053a6f13a4aSGreg Roach                switch (end($tags)) {
1054a6f13a4aSGreg Roach                    case 'DATE':
1055a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1056a6f13a4aSGreg Roach                        $value = $tmp->display();
1057a6f13a4aSGreg Roach                        break;
1058a6f13a4aSGreg Roach                    case 'PLAC':
1059299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1060392561bbSGreg Roach                        $value = $tmp->shortName();
1061a6f13a4aSGreg Roach                        break;
1062a6f13a4aSGreg Roach                }
1063044416d2SGreg Roach                if ($useBreak === '1') {
1064a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1065a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1066a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1067a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1068a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10693b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10703b3cfeeaSGreg Roach                        $value = substr($value, 4);
1071a6f13a4aSGreg Roach                    }
1072a6f13a4aSGreg Roach                }
1073d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10743cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
1075299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1076a4d703aeSGreg Roach                }
1077a2a485c3SGreg Roach
1078a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
10799c32db08SGreg Roach                    $value = strip_tags($value);
1080381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1081a2a485c3SGreg Roach                }
1082a6f13a4aSGreg Roach                $this->current_element->addText($value);
1083a6f13a4aSGreg Roach            }
1084a6f13a4aSGreg Roach        }
1085a6f13a4aSGreg Roach    }
1086a6f13a4aSGreg Roach
1087a6f13a4aSGreg Roach    /**
108876692c8bSGreg Roach     * XML <RepeatTag>
1089a6f13a4aSGreg Roach     *
1090c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10918ba2e626SGreg Roach     *
10928ba2e626SGreg Roach     * @return void
1093a6f13a4aSGreg Roach     */
1094b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1095c1010edaSGreg Roach    {
1096a6f13a4aSGreg Roach        $this->process_repeats++;
1097a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1098a6f13a4aSGreg Roach            return;
1099a6f13a4aSGreg Roach        }
1100a6f13a4aSGreg Roach
11019b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
110213abd6f3SGreg Roach        $this->repeats         = [];
1103e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1104a6f13a4aSGreg Roach
1105e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1106a6f13a4aSGreg Roach        if (!empty($tag)) {
1107044416d2SGreg Roach            if ($tag === '@desc') {
1108a6f13a4aSGreg Roach                $value = $this->desc;
1109a6f13a4aSGreg Roach                $value = trim($value);
1110a6f13a4aSGreg Roach                $this->current_element->addText($value);
1111a6f13a4aSGreg Roach            } else {
11127a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
11137a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1114b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1115b2448a1bSGreg Roach                if ($level === 0) {
1116a6f13a4aSGreg Roach                    $level++;
1117a6f13a4aSGreg Roach                }
1118a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1119a6f13a4aSGreg Roach                $t      = $tag;
1120a6f13a4aSGreg Roach                $count  = count($tags);
1121a6f13a4aSGreg Roach                $i      = 0;
1122a6f13a4aSGreg Roach                while ($i < $count) {
1123a6f13a4aSGreg Roach                    $t = $tags[$i];
1124a6f13a4aSGreg Roach                    if (!empty($t)) {
1125a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
11263d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1127a6f13a4aSGreg Roach                            if (empty($subrec)) {
1128a6f13a4aSGreg Roach                                $level--;
11293d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1130a6f13a4aSGreg Roach                                if (empty($subrec)) {
1131a6f13a4aSGreg Roach                                    return;
1132a6f13a4aSGreg Roach                                }
1133a6f13a4aSGreg Roach                            }
1134a6f13a4aSGreg Roach                        }
1135a6f13a4aSGreg Roach                        $level++;
1136a6f13a4aSGreg Roach                    }
1137a6f13a4aSGreg Roach                    $i++;
1138a6f13a4aSGreg Roach                }
1139a6f13a4aSGreg Roach                $level--;
1140a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1141a6f13a4aSGreg Roach                $i     = 0;
1142a6f13a4aSGreg Roach                while ($i < $count) {
1143a6f13a4aSGreg Roach                    $i++;
1144a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1145a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11468d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1147299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1148a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1149a9007102SGreg Roach                            continue;
1150a9007102SGreg Roach                        }
1151a9007102SGreg Roach                    }
1152a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1153a6f13a4aSGreg Roach                }
1154a6f13a4aSGreg Roach            }
1155a6f13a4aSGreg Roach        }
1156a6f13a4aSGreg Roach    }
1157a6f13a4aSGreg Roach
1158a6f13a4aSGreg Roach    /**
115976692c8bSGreg Roach     * XML </ RepeatTag>
11608ba2e626SGreg Roach     *
11618ba2e626SGreg Roach     * @return void
1162a6f13a4aSGreg Roach     */
1163b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1164c1010edaSGreg Roach    {
1165a6f13a4aSGreg Roach        $this->process_repeats--;
1166a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1167a6f13a4aSGreg Roach            return;
1168a6f13a4aSGreg Roach        }
1169a6f13a4aSGreg Roach
1170a6f13a4aSGreg Roach        // Check if there is anything to repeat
1171a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1172a6f13a4aSGreg Roach            // No need to load them if not used...
1173a6f13a4aSGreg Roach
1174a6f13a4aSGreg Roach            $lineoffset = 0;
1175a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1176a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1177a6f13a4aSGreg Roach            }
1178a6f13a4aSGreg Roach            //-- read the xml from the file
1179299d100dSGreg Roach            $lines = file($this->report);
11807a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1181a6f13a4aSGreg Roach                $lineoffset--;
1182a6f13a4aSGreg Roach            }
1183a6f13a4aSGreg Roach            $lineoffset++;
1184a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1185a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1186a6f13a4aSGreg Roach            // RepeatTag Level counter
1187a6f13a4aSGreg Roach            $count = 1;
1188a6f13a4aSGreg Roach            while (0 < $count) {
11897a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1190a6f13a4aSGreg Roach                    $count++;
11917a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1192a6f13a4aSGreg Roach                    $count--;
1193a6f13a4aSGreg Roach                }
1194a6f13a4aSGreg Roach                if (0 < $count) {
1195a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1196a6f13a4aSGreg Roach                }
1197a6f13a4aSGreg Roach                $line_nr++;
1198a6f13a4aSGreg Roach            }
1199a6f13a4aSGreg Roach            // No need to drag this
1200a6f13a4aSGreg Roach            unset($lines);
1201a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1202a6f13a4aSGreg Roach            // Save original values
12039b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1204a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1205a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1206a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1207a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1208e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1209a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
12101aa04befSGreg Roach
12111aa04befSGreg Roach                xml_set_element_handler(
12121aa04befSGreg Roach                    $repeat_parser,
12139d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
12141aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
12151aa04befSGreg Roach                    },
12169d454b6bSGreg Roach                    function ($parser, string $name): void {
12171aa04befSGreg Roach                        $this->endElement($parser, $name);
12181aa04befSGreg Roach                    }
12191aa04befSGreg Roach                );
12201aa04befSGreg Roach
12211aa04befSGreg Roach                xml_set_character_data_handler(
12221aa04befSGreg Roach                    $repeat_parser,
12239d454b6bSGreg Roach                    function ($parser, string $data): void {
12241aa04befSGreg Roach                        $this->characterData($parser, $data);
12251aa04befSGreg Roach                    }
12261aa04befSGreg Roach                );
12271aa04befSGreg Roach
1228a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
12296ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1230a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1231a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1232a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1233a6f13a4aSGreg Roach                    ));
1234a6f13a4aSGreg Roach                }
1235a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1236a6f13a4aSGreg Roach            }
1237a6f13a4aSGreg Roach            // Restore original values
1238a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1239e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1240a6f13a4aSGreg Roach        }
124165e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1242a6f13a4aSGreg Roach    }
1243a6f13a4aSGreg Roach
1244a6f13a4aSGreg Roach    /**
1245a6f13a4aSGreg Roach     * Variable lookup
1246a6f13a4aSGreg Roach     * Retrieve predefined variables :
1247a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1248a6f13a4aSGreg Roach     *        1 EVEN This is a description
1249a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1250a6f13a4aSGreg Roach     * $ I18N::translate('....')
1251a6f13a4aSGreg Roach     * $ language_settings[]
1252a6f13a4aSGreg Roach     *
1253c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12548ba2e626SGreg Roach     *
12558ba2e626SGreg Roach     * @return void
1256a6f13a4aSGreg Roach     */
1257b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1258c1010edaSGreg Roach    {
1259a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12606ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "var=" is missing or not set in the XML file on line: ' . xml_get_current_line_number($this->parser));
1261a6f13a4aSGreg Roach        }
1262a6f13a4aSGreg Roach
1263a6f13a4aSGreg Roach        $var = $attrs['var'];
1264a6f13a4aSGreg Roach        // SetVar element preset variables
1265d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1266d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1267a6f13a4aSGreg Roach        } else {
1268a6f13a4aSGreg Roach            $tfact = $this->fact;
12697a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1270a6f13a4aSGreg Roach                // Use :
1271a6f13a4aSGreg Roach                // n TYPE This text if string
1272a6f13a4aSGreg Roach                $tfact = $this->type;
1273a6f13a4aSGreg Roach            }
1274c1010edaSGreg Roach            $var = str_replace([
1275c1010edaSGreg Roach                '@fact',
1276c1010edaSGreg Roach                '@desc',
1277c1010edaSGreg Roach            ], [
1278c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1279c1010edaSGreg Roach                $this->desc,
1280c1010edaSGreg Roach            ], $var);
1281a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1282da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1283a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1284a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1285a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1286a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1287a6f13a4aSGreg Roach            }
1288a6f13a4aSGreg Roach        }
1289a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1290a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12917a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1292a6f13a4aSGreg Roach                $g   = new Date($var);
1293a6f13a4aSGreg Roach                $var = $g->display();
1294a6f13a4aSGreg Roach            }
1295a6f13a4aSGreg Roach        }
1296a6f13a4aSGreg Roach        $this->current_element->addText($var);
12972836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1298a6f13a4aSGreg Roach    }
1299a6f13a4aSGreg Roach
1300a6f13a4aSGreg Roach    /**
130176692c8bSGreg Roach     * XML <Facts>
130276692c8bSGreg Roach     *
1303c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
13048ba2e626SGreg Roach     *
13058ba2e626SGreg Roach     * @return void
1306a6f13a4aSGreg Roach     */
1307b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1308c1010edaSGreg Roach    {
1309a6f13a4aSGreg Roach        $this->process_repeats++;
1310a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1311a6f13a4aSGreg Roach            return;
1312a6f13a4aSGreg Roach        }
1313a6f13a4aSGreg Roach
13149b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
131513abd6f3SGreg Roach        $this->repeats         = [];
1316e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1317a6f13a4aSGreg Roach
13187a6ee1acSGreg Roach        $id    = '';
131913abd6f3SGreg Roach        $match = [];
13207a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1321a6f13a4aSGreg Roach            $id = $match[1];
1322a6f13a4aSGreg Roach        }
13237a6ee1acSGreg Roach        $tag = '';
1324a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1325a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1326a6f13a4aSGreg Roach        }
13277a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1328d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1329a6f13a4aSGreg Roach        }
1330a6f13a4aSGreg Roach
1331299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1332a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1333820b62dfSGreg Roach            $facts = $record->facts([], true);
133413abd6f3SGreg Roach            $this->repeats = [];
1335a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1336195b5e75SGreg Roach            foreach ($facts as $fact) {
1337e364afe4SGreg Roach                if (!in_array($fact->getTag(), $nonfacts, true)) {
1338195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1339a6f13a4aSGreg Roach                }
1340a6f13a4aSGreg Roach            }
1341a6f13a4aSGreg Roach        } else {
134230158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1343195b5e75SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1344138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1345a6f13a4aSGreg Roach                }
1346a6f13a4aSGreg Roach            }
1347a6f13a4aSGreg Roach        }
1348a6f13a4aSGreg Roach    }
1349a6f13a4aSGreg Roach
1350a6f13a4aSGreg Roach    /**
135176692c8bSGreg Roach     * XML </Facts>
13528ba2e626SGreg Roach     *
13538ba2e626SGreg Roach     * @return void
1354a6f13a4aSGreg Roach     */
1355b702978eSGreg Roach    protected function factsEndHandler(): void
1356c1010edaSGreg Roach    {
1357a6f13a4aSGreg Roach        $this->process_repeats--;
1358a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1359a6f13a4aSGreg Roach            return;
1360a6f13a4aSGreg Roach        }
1361a6f13a4aSGreg Roach
1362a6f13a4aSGreg Roach        // Check if there is anything to repeat
1363a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1364e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1365a6f13a4aSGreg Roach            $lineoffset = 0;
1366a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1367a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1368a6f13a4aSGreg Roach            }
1369a6f13a4aSGreg Roach
1370a6f13a4aSGreg Roach            //-- read the xml from the file
1371299d100dSGreg Roach            $lines = file($this->report);
1372a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1373a6f13a4aSGreg Roach                $lineoffset--;
1374a6f13a4aSGreg Roach            }
1375a6f13a4aSGreg Roach            $lineoffset++;
1376a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1377a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1378a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1379a6f13a4aSGreg Roach            while ($line_nr < $i) {
1380a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1381a6f13a4aSGreg Roach                $line_nr++;
1382a6f13a4aSGreg Roach            }
1383a6f13a4aSGreg Roach            // No need to drag this
1384a6f13a4aSGreg Roach            unset($lines);
1385a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1386a6f13a4aSGreg Roach            // Save original values
13879b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1388a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1389a6f13a4aSGreg Roach            $count                = count($this->repeats);
1390a6f13a4aSGreg Roach            $i                    = 0;
1391a6f13a4aSGreg Roach            while ($i < $count) {
1392a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1393a6f13a4aSGreg Roach                $this->fact   = '';
1394a6f13a4aSGreg Roach                $this->desc   = '';
1395a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1396a6f13a4aSGreg Roach                    $this->fact = $match[1];
1397a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
139813abd6f3SGreg Roach                        $tmatch = [];
1399a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1400a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1401a6f13a4aSGreg Roach                        } else {
1402a6f13a4aSGreg Roach                            $this->type = ' ';
1403a6f13a4aSGreg Roach                        }
1404a6f13a4aSGreg Roach                    }
1405a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
14063d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1407a6f13a4aSGreg Roach                }
1408a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1409e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1410a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
14111aa04befSGreg Roach
14121aa04befSGreg Roach                xml_set_element_handler(
14131aa04befSGreg Roach                    $repeat_parser,
14149d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
14151aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
14161aa04befSGreg Roach                    },
14179d454b6bSGreg Roach                    function ($parser, string $name): void {
14181aa04befSGreg Roach                        $this->endElement($parser, $name);
14191aa04befSGreg Roach                    }
14201aa04befSGreg Roach                );
14211aa04befSGreg Roach
14221aa04befSGreg Roach                xml_set_character_data_handler(
14231aa04befSGreg Roach                    $repeat_parser,
14249d454b6bSGreg Roach                    function ($parser, string $data): void {
14251aa04befSGreg Roach                        $this->characterData($parser, $data);
14261aa04befSGreg Roach                    }
14271aa04befSGreg Roach                );
14281aa04befSGreg Roach
1429a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14306ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1431a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1432a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1433a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1434a6f13a4aSGreg Roach                    ));
1435a6f13a4aSGreg Roach                }
1436a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1437a6f13a4aSGreg Roach                $i++;
1438a6f13a4aSGreg Roach            }
1439a6f13a4aSGreg Roach            // Restore original values
1440e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1441a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1442a6f13a4aSGreg Roach        }
144365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1444a6f13a4aSGreg Roach    }
1445a6f13a4aSGreg Roach
1446a6f13a4aSGreg Roach    /**
1447a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1448d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1449a6f13a4aSGreg Roach     *
1450c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14518ba2e626SGreg Roach     *
14528ba2e626SGreg Roach     * @return void
1453a6f13a4aSGreg Roach     */
1454b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1455c1010edaSGreg Roach    {
1456a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14576ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1458a6f13a4aSGreg Roach        }
1459a6f13a4aSGreg Roach
1460a6f13a4aSGreg Roach        $name  = $attrs['name'];
1461a6f13a4aSGreg Roach        $value = $attrs['value'];
146213abd6f3SGreg Roach        $match = [];
1463a6f13a4aSGreg Roach        // Current GEDCOM record strings
1464044416d2SGreg Roach        if ($value === '@ID') {
14657a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1466a6f13a4aSGreg Roach                $value = $match[1];
1467a6f13a4aSGreg Roach            }
1468044416d2SGreg Roach        } elseif ($value === '@fact') {
1469a6f13a4aSGreg Roach            $value = $this->fact;
1470044416d2SGreg Roach        } elseif ($value === '@desc') {
1471a6f13a4aSGreg Roach            $value = $this->desc;
1472044416d2SGreg Roach        } elseif ($value === '@generation') {
1473589feda3SGreg Roach            $value = (string) $this->generation;
1474a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
147513abd6f3SGreg Roach            $gmatch = [];
1476a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14777a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1478a6f13a4aSGreg Roach            }
1479a6f13a4aSGreg Roach        }
1480a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1481d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1482a6f13a4aSGreg Roach        }
1483a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1484a6f13a4aSGreg Roach        $i     = 0;
1485a6f13a4aSGreg Roach        while ($i < $count) {
1486d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14877a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1488a6f13a4aSGreg Roach            $i++;
1489a6f13a4aSGreg Roach        }
1490a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1491da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1492a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1493a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1494a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1495a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1496a6f13a4aSGreg Roach        }
149752868398SGreg Roach
1498a6f13a4aSGreg Roach        // Arithmetic functions
14993cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
150052868398SGreg Roach            // Create an expression language with the functions used by our reports.
150152868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1502c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1503c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
150452868398SGreg Roach
150552868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1506a6f13a4aSGreg Roach        }
150752868398SGreg Roach
15087a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
15097a6ee1acSGreg Roach            $value = '';
1510a6f13a4aSGreg Roach        }
1511d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1512a6f13a4aSGreg Roach    }
1513a6f13a4aSGreg Roach
1514a6f13a4aSGreg Roach    /**
1515a6f13a4aSGreg Roach     * XML <if > start element
1516a6f13a4aSGreg Roach     *
1517c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15188ba2e626SGreg Roach     *
15198ba2e626SGreg Roach     * @return void
1520a6f13a4aSGreg Roach     */
1521b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1522c1010edaSGreg Roach    {
1523a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1524a6f13a4aSGreg Roach            $this->process_ifs++;
1525a6f13a4aSGreg Roach
1526a6f13a4aSGreg Roach            return;
1527a6f13a4aSGreg Roach        }
1528a6f13a4aSGreg Roach
1529a6f13a4aSGreg Roach        $condition = $attrs['condition'];
153082759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1531c1010edaSGreg Roach        $condition = str_replace([
1532c1010edaSGreg Roach            ' LT ',
1533c1010edaSGreg Roach            ' GT ',
1534c1010edaSGreg Roach        ], [
1535c1010edaSGreg Roach            '<',
1536c1010edaSGreg Roach            '>',
1537c1010edaSGreg Roach        ], $condition);
15383cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15397a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
154013abd6f3SGreg Roach        $match     = [];
15413cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1542a6f13a4aSGreg Roach        $i         = 0;
1543a6f13a4aSGreg Roach        while ($i < $count) {
1544a6f13a4aSGreg Roach            $id    = $match[$i][1];
1545a6f13a4aSGreg Roach            $value = '""';
1546044416d2SGreg Roach            if ($id === 'ID') {
15477a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1548a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1549a6f13a4aSGreg Roach                }
15507a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1551a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15527a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1553a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15547a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1555a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1556a6f13a4aSGreg Roach            } else {
1557b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1558b2448a1bSGreg Roach                if ($level === 0) {
1559a6f13a4aSGreg Roach                    $level++;
1560a6f13a4aSGreg Roach                }
15613d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1562a6f13a4aSGreg Roach                if (empty($value)) {
1563a6f13a4aSGreg Roach                    $level++;
15643d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1565a6f13a4aSGreg Roach                }
15668d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15675e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1568a6f13a4aSGreg Roach            }
1569a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1570a6f13a4aSGreg Roach            $i++;
1571a6f13a4aSGreg Roach        }
15725809450fSGreg Roach
1573cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1574cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1575c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1576c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1577cb63a60eSGreg Roach
1578cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15795809450fSGreg Roach
1580a6f13a4aSGreg Roach        if (!$ret) {
1581a6f13a4aSGreg Roach            $this->process_ifs++;
1582a6f13a4aSGreg Roach        }
1583a6f13a4aSGreg Roach    }
1584a6f13a4aSGreg Roach
1585a6f13a4aSGreg Roach    /**
1586a6f13a4aSGreg Roach     * XML <if /> end element
15878ba2e626SGreg Roach     *
15888ba2e626SGreg Roach     * @return void
1589a6f13a4aSGreg Roach     */
1590b702978eSGreg Roach    protected function ifEndHandler(): void
1591c1010edaSGreg Roach    {
1592a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1593a6f13a4aSGreg Roach            $this->process_ifs--;
1594a6f13a4aSGreg Roach        }
1595a6f13a4aSGreg Roach    }
1596a6f13a4aSGreg Roach
1597a6f13a4aSGreg Roach    /**
1598a6f13a4aSGreg Roach     * XML <Footnote > start element
1599a6f13a4aSGreg Roach     * Collect the Footnote links
1600a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1601a6f13a4aSGreg Roach     *
1602c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16038ba2e626SGreg Roach     *
16048ba2e626SGreg Roach     * @return void
1605a6f13a4aSGreg Roach     */
1606b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1607c1010edaSGreg Roach    {
16087a6ee1acSGreg Roach        $id = '';
16097a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1610a6f13a4aSGreg Roach            $id = $match[2];
1611a6f13a4aSGreg Roach        }
1612299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1613a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
16149b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1615a6f13a4aSGreg Roach            $this->print_data         = true;
16167a6ee1acSGreg Roach            $style                    = '';
1617a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1618a6f13a4aSGreg Roach                $style = $attrs['style'];
1619a6f13a4aSGreg Roach            }
1620a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1621e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1622a6f13a4aSGreg Roach        } else {
1623a6f13a4aSGreg Roach            $this->print_data       = false;
1624a6f13a4aSGreg Roach            $this->process_footnote = false;
1625a6f13a4aSGreg Roach        }
1626a6f13a4aSGreg Roach    }
1627a6f13a4aSGreg Roach
1628a6f13a4aSGreg Roach    /**
1629a6f13a4aSGreg Roach     * XML <Footnote /> end element
1630a6f13a4aSGreg Roach     * Print the collected Footnote data
16318ba2e626SGreg Roach     *
16328ba2e626SGreg Roach     * @return void
1633a6f13a4aSGreg Roach     */
1634b702978eSGreg Roach    protected function footnoteEndHandler(): void
1635c1010edaSGreg Roach    {
1636a6f13a4aSGreg Roach        if ($this->process_footnote) {
1637a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1638a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1639a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1640e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1641a6f13a4aSGreg Roach            }
1642a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1643a6f13a4aSGreg Roach        } else {
1644a6f13a4aSGreg Roach            $this->process_footnote = true;
1645a6f13a4aSGreg Roach        }
1646a6f13a4aSGreg Roach    }
1647a6f13a4aSGreg Roach
1648a6f13a4aSGreg Roach    /**
1649a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16508ba2e626SGreg Roach     *
16518ba2e626SGreg Roach     * @return void
1652a6f13a4aSGreg Roach     */
1653b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1654c1010edaSGreg Roach    {
16557a6ee1acSGreg Roach        $temp = 'footnotetexts';
1656e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1657a6f13a4aSGreg Roach    }
1658a6f13a4aSGreg Roach
1659a6f13a4aSGreg Roach    /**
1660a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16618ba2e626SGreg Roach     *
16628ba2e626SGreg Roach     * @return void
1663a6f13a4aSGreg Roach     */
1664b702978eSGreg Roach    protected function brStartHandler(): void
1665c1010edaSGreg Roach    {
1666a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1667a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1668a6f13a4aSGreg Roach        }
1669a6f13a4aSGreg Roach    }
1670a6f13a4aSGreg Roach
1671a6f13a4aSGreg Roach    /**
1672a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16738ba2e626SGreg Roach     *
16748ba2e626SGreg Roach     * @return void
1675a6f13a4aSGreg Roach     */
1676b702978eSGreg Roach    protected function spStartHandler(): void
1677c1010edaSGreg Roach    {
1678a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1679a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1680a6f13a4aSGreg Roach        }
1681a6f13a4aSGreg Roach    }
1682a6f13a4aSGreg Roach
1683a6f13a4aSGreg Roach    /**
168476692c8bSGreg Roach     * XML <HighlightedImage/>
168576692c8bSGreg Roach     *
1686c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16878ba2e626SGreg Roach     *
16888ba2e626SGreg Roach     * @return void
1689a6f13a4aSGreg Roach     */
1690b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1691c1010edaSGreg Roach    {
1692a6f13a4aSGreg Roach        $id = '';
16937a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1694a6f13a4aSGreg Roach            $id = $match[1];
1695a6f13a4aSGreg Roach        }
1696a6f13a4aSGreg Roach
1697c21bdddcSGreg Roach        // Position the top corner of this box on the page
1698c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1699a6f13a4aSGreg Roach
1700c21bdddcSGreg Roach        // Position the left corner of this box on the page
1701c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1702a6f13a4aSGreg Roach
170383cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
170483cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1705a6f13a4aSGreg Roach
1706a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
170783cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1708a6f13a4aSGreg Roach
170983cdc021SGreg Roach        // Width, height (or both).
1710c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1711c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1712a6f13a4aSGreg Roach
1713299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
17144a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
171586a63f51SGreg Roach
1716a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1717a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1718*b6f35a76SGreg Roach            $attributes = [imagesx($image), imagesy($image)];
171929518ad2SGreg Roach
1720a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
17213c3b90deSGreg Roach                $perc   = $width / $attributes[0];
17223c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1723a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
17243c3b90deSGreg Roach                $perc  = $height / $attributes[1];
17253c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1726a6f13a4aSGreg Roach            } else {
17273c3b90deSGreg Roach                $width  = $attributes[0];
17283c3b90deSGreg Roach                $height = $attributes[1];
1729a6f13a4aSGreg Roach            }
1730a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1731e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1732a6f13a4aSGreg Roach        }
1733a6f13a4aSGreg Roach    }
1734a6f13a4aSGreg Roach
1735a6f13a4aSGreg Roach    /**
173676692c8bSGreg Roach     * XML <Image/>
173776692c8bSGreg Roach     *
1738c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17398ba2e626SGreg Roach     *
17408ba2e626SGreg Roach     * @return void
1741a6f13a4aSGreg Roach     */
1742b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1743c1010edaSGreg Roach    {
174483cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1745c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1746a6f13a4aSGreg Roach
1747a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1748c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1749a6f13a4aSGreg Roach
175083cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
175183cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1752a6f13a4aSGreg Roach
1753a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
175483cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1755a6f13a4aSGreg Roach
175683cdc021SGreg Roach        // Width, height (or both).
1757c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1758c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1759a6f13a4aSGreg Roach
176083cdc021SGreg Roach        $file = $attrs['file'] ?? '';
176183cdc021SGreg Roach
1762044416d2SGreg Roach        if ($file === '@FILE') {
176313abd6f3SGreg Roach            $match = [];
1764a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1765299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17664a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1767cdf416fbSGreg Roach
1768a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1769a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
1770*b6f35a76SGreg Roach                    $attributes = [imagesx($image), imagesy($image)];
177129518ad2SGreg Roach
1772a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17733c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17743c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1775a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17763c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17773c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1778a6f13a4aSGreg Roach                    } else {
17793c3b90deSGreg Roach                        $width  = $attributes[0];
17803c3b90deSGreg Roach                        $height = $attributes[1];
1781a6f13a4aSGreg Roach                    }
1782a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1783e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1784a6f13a4aSGreg Roach                }
1785a6f13a4aSGreg Roach            }
1786a6f13a4aSGreg Roach        } else {
17877a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1788a6f13a4aSGreg Roach                $size = getimagesize($file);
1789a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1790a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1791a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1792a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1793a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1794a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1795a6f13a4aSGreg Roach                } else {
1796a6f13a4aSGreg Roach                    $width  = $size[0];
1797a6f13a4aSGreg Roach                    $height = $size[1];
1798a6f13a4aSGreg Roach                }
1799e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1800e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1801a6f13a4aSGreg Roach            }
1802a6f13a4aSGreg Roach        }
1803a6f13a4aSGreg Roach    }
1804a6f13a4aSGreg Roach
1805a6f13a4aSGreg Roach    /**
1806a6f13a4aSGreg Roach     * XML <Line> element handler
1807a6f13a4aSGreg Roach     *
1808c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18098ba2e626SGreg Roach     *
18108ba2e626SGreg Roach     * @return void
1811a6f13a4aSGreg Roach     */
1812b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1813c1010edaSGreg Roach    {
1814a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1815c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1816a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
18177a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1818a6f13a4aSGreg Roach                $x1 = 0;
18197a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1820c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1821a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1822c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1823a6f13a4aSGreg Roach            }
1824a6f13a4aSGreg Roach        }
1825a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1826c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1827a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18287a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1829a6f13a4aSGreg Roach                $y1 = 0;
18307a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1831c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1832a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1833c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1834a6f13a4aSGreg Roach            }
1835a6f13a4aSGreg Roach        }
1836a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1837c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1838a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18397a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1840a6f13a4aSGreg Roach                $x2 = 0;
18417a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1842c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1843a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1844c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1845a6f13a4aSGreg Roach            }
1846a6f13a4aSGreg Roach        }
1847a6f13a4aSGreg Roach        // End vertical position
1848c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1849a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18507a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1851a6f13a4aSGreg Roach                $y2 = 0;
18527a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1853c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1854a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1855c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1856a6f13a4aSGreg Roach            }
1857a6f13a4aSGreg Roach        }
1858a6f13a4aSGreg Roach
1859e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1860e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1861a6f13a4aSGreg Roach    }
1862a6f13a4aSGreg Roach
1863a6f13a4aSGreg Roach    /**
186476692c8bSGreg Roach     * XML <List>
1865a6f13a4aSGreg Roach     *
1866c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18678ba2e626SGreg Roach     *
18688ba2e626SGreg Roach     * @return void
1869a6f13a4aSGreg Roach     */
1870b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1871c1010edaSGreg Roach    {
1872a6f13a4aSGreg Roach        $this->process_repeats++;
1873a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1874a6f13a4aSGreg Roach            return;
1875a6f13a4aSGreg Roach        }
1876a6f13a4aSGreg Roach
187713abd6f3SGreg Roach        $match = [];
1878a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1879a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1880a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1881d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1882a6f13a4aSGreg Roach                $sortby = trim($sortby);
1883a6f13a4aSGreg Roach            }
1884a6f13a4aSGreg Roach        } else {
18857a6ee1acSGreg Roach            $sortby = 'NAME';
1886a6f13a4aSGreg Roach        }
1887a6f13a4aSGreg Roach
1888e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1889195b5e75SGreg Roach
1890a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1891a6f13a4aSGreg Roach        switch ($listname) {
18927a6ee1acSGreg Roach            case 'pending':
18936c0bef3aSGreg Roach                $xrefs = DB::table('change')
1894195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1895a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1896195b5e75SGreg Roach                            ->from('change')
1897195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1898195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18997f5c2944SGreg Roach                            ->groupBy(['xref']);
1900195b5e75SGreg Roach                    })
19016c0bef3aSGreg Roach                    ->pluck('xref');
1902195b5e75SGreg Roach
190313abd6f3SGreg Roach                $this->list = [];
19046c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
19056c0bef3aSGreg Roach                    $this->list[] = GedcomRecord::getInstance($xref, $this->tree);
1906a6f13a4aSGreg Roach                }
1907a6f13a4aSGreg Roach                break;
1908a6f13a4aSGreg Roach            case 'individual':
19095985adfbSGreg Roach                $query = DB::table('individuals')
19105985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
19115985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
19125985adfbSGreg Roach                    ->distinct();
19135985adfbSGreg Roach
1914a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1915a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
191682759250SGreg Roach                        $value = $this->substituteVars($value, false);
1917a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1918a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19190b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19205985adfbSGreg Roach                                $join
19215985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
19225985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
19235985adfbSGreg Roach                            });
19245985adfbSGreg Roach
19255985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19265985adfbSGreg Roach
1927a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19285985adfbSGreg Roach
1929044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19305985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1931a6f13a4aSGreg Roach                            } else {
19325985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1933a6f13a4aSGreg Roach                            }
19345985adfbSGreg Roach
19355985adfbSGreg Roach                            // This filter has been fully processed
19365985adfbSGreg Roach                            unset($attrs[$attr]);
19377ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19380b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19395985adfbSGreg Roach                                $join
19405985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19415985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19425985adfbSGreg Roach                            });
1943a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19447a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19455d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1946fbbe964bSGreg Roach                                $query->whereContains($attr . '.n_full', $name);
1947a6f13a4aSGreg Roach                            }
19485985adfbSGreg Roach
19495985adfbSGreg Roach                            // This filter has been fully processed
19505985adfbSGreg Roach                            unset($attrs[$attr]);
1951a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19525985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1953a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19545985adfbSGreg Roach
1955a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19565985adfbSGreg Roach
19575985adfbSGreg Roach                            // This filter has been fully processed
19585985adfbSGreg Roach                            unset($attrs[$attr]);
19592fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19609dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19615985adfbSGreg Roach                            $query
19620b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19635985adfbSGreg Roach                                    $join
19647ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19657ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19665985adfbSGreg Roach                                })
19670b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19687ee4bfadSGreg Roach                                    $join
19697ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19707ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19717ee4bfadSGreg Roach                                })
19727ee4bfadSGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
19736e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19745985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19757ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19766e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19776e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
19786e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
19796e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19806e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1981e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19827ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1983a6f13a4aSGreg Roach                        }
1984a6f13a4aSGreg Roach                    }
1985a6f13a4aSGreg Roach                }
1986a6f13a4aSGreg Roach
198713abd6f3SGreg Roach                $this->list = [];
1988a6f13a4aSGreg Roach
19895985adfbSGreg Roach                foreach ($query->get() as $row) {
1990299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1991a6f13a4aSGreg Roach                }
1992a6f13a4aSGreg Roach                break;
1993a6f13a4aSGreg Roach
1994a6f13a4aSGreg Roach            case 'family':
199530fc2b1eSGreg Roach                $query = DB::table('families')
199630fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
199730fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
199830fc2b1eSGreg Roach                    ->distinct();
199930fc2b1eSGreg Roach
2000a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
2001a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
200282759250SGreg Roach                        $value = $this->substituteVars($value, false);
2003a6f13a4aSGreg Roach                        // Convert the various filters into SQL
2004a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
20050b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200630fc2b1eSGreg Roach                                $join
200730fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
200830fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
200930fc2b1eSGreg Roach                            });
201030fc2b1eSGreg Roach
201130fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
201230fc2b1eSGreg Roach
2013a6f13a4aSGreg Roach                            $date = new Date($match[3]);
201430fc2b1eSGreg Roach
2015044416d2SGreg Roach                            if ($match[2] === 'LTE') {
201630fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
2017a6f13a4aSGreg Roach                            } else {
201830fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
2019a6f13a4aSGreg Roach                            }
202030fc2b1eSGreg Roach
202130fc2b1eSGreg Roach                            // This filter has been fully processed
202230fc2b1eSGreg Roach                            unset($attrs[$attr]);
2023a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
202430fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2025a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
202630fc2b1eSGreg Roach
2027a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
202830fc2b1eSGreg Roach
202930fc2b1eSGreg Roach                            // This filter has been fully processed
203030fc2b1eSGreg Roach                            unset($attrs[$attr]);
203130fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20323b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20330b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
203430fc2b1eSGreg Roach                                    $join
203530fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20363b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
203730fc2b1eSGreg Roach                                            $query
203830fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
203930fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
204030fc2b1eSGreg Roach                                        });
204130fc2b1eSGreg Roach                                });
20425d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20437a6ee1acSGreg Roach                                if ($match[1] != '') {
20447a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20455d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2046fbbe964bSGreg Roach                                        $query->whereContains($attr . '.n_full', $name);
20475d0bc43dSGreg Roach                                    }
20485d0bc43dSGreg Roach                                }
2049a6f13a4aSGreg Roach                            }
205030fc2b1eSGreg Roach
205130fc2b1eSGreg Roach                            // This filter has been fully processed
205230fc2b1eSGreg Roach                            unset($attrs[$attr]);
20536e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20549dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
205530fc2b1eSGreg Roach                            $query
20560b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
205730fc2b1eSGreg Roach                                    $join
20586e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20596e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
206030fc2b1eSGreg Roach                                })
20610b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20626e5c0963SGreg Roach                                    $join
20636e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20646e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20656e5c0963SGreg Roach                                })
20666e5c0963SGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
20676e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
206830fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20696e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20706e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20716e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20726e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20736e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20746e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2075e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20766e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2077a6f13a4aSGreg Roach                        }
2078a6f13a4aSGreg Roach                    }
2079a6f13a4aSGreg Roach                }
2080a6f13a4aSGreg Roach
208113abd6f3SGreg Roach                $this->list = [];
2082a6f13a4aSGreg Roach
208330fc2b1eSGreg Roach                foreach ($query->get() as $row) {
2084299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2085a6f13a4aSGreg Roach                }
2086a6f13a4aSGreg Roach                break;
2087a6f13a4aSGreg Roach
2088a6f13a4aSGreg Roach            default:
20896ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2090a6f13a4aSGreg Roach        }
2091a6f13a4aSGreg Roach
209213abd6f3SGreg Roach        $filters  = [];
209313abd6f3SGreg Roach        $filters2 = [];
2094a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2095a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2096a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2097a6f13a4aSGreg Roach                    $condition = $value;
2098a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2099a6f13a4aSGreg Roach                        $id    = $match[1];
2100a6f13a4aSGreg Roach                        $value = "''";
2101044416d2SGreg Roach                        if ($id === 'ID') {
21027a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2103a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2104a6f13a4aSGreg Roach                            }
2105044416d2SGreg Roach                        } elseif ($id === 'fact') {
2106a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2107044416d2SGreg Roach                        } elseif ($id === 'desc') {
2108a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2109a6f13a4aSGreg Roach                        } else {
2110a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
21117a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2112a6f13a4aSGreg Roach                            }
2113a6f13a4aSGreg Roach                        }
2114a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2115a6f13a4aSGreg Roach                    }
2116a6f13a4aSGreg Roach                    //-- handle regular expressions
2117a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2118a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2119a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2120a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2121a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2122d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2123a6f13a4aSGreg Roach                            $val = trim($val);
2124a6f13a4aSGreg Roach                        }
2125a6f13a4aSGreg Roach                        if ($val) {
21267a6ee1acSGreg Roach                            $searchstr = '';
21277a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2128a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2129a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2130a6f13a4aSGreg Roach                                $level = 1;
2131a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2132a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2133a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2134a6f13a4aSGreg Roach                                    }
2135a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2136044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21377a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2138a6f13a4aSGreg Roach                                    }
21397a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2140a6f13a4aSGreg Roach                                    $level++;
2141a6f13a4aSGreg Roach                                }
2142a6f13a4aSGreg Roach                            } else {
2143044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21447a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2145a6f13a4aSGreg Roach                                }
2146a6f13a4aSGreg Roach                                $t         = $tag;
21477a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2148a6f13a4aSGreg Roach                            }
2149a6f13a4aSGreg Roach                            switch ($expr) {
21507a6ee1acSGreg Roach                                case 'CONTAINS':
2151044416d2SGreg Roach                                    if ($t === 'PLAC') {
2152a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2153a6f13a4aSGreg Roach                                    } else {
2154a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2155a6f13a4aSGreg Roach                                    }
2156a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2157a6f13a4aSGreg Roach                                    break;
2158a6f13a4aSGreg Roach                                default:
2159c1010edaSGreg Roach                                    $filters2[] = [
2160c1010edaSGreg Roach                                        'tag'  => $tag,
2161c1010edaSGreg Roach                                        'expr' => $expr,
2162c1010edaSGreg Roach                                        'val'  => $val,
2163c1010edaSGreg Roach                                    ];
2164a6f13a4aSGreg Roach                                    break;
2165a6f13a4aSGreg Roach                            }
2166a6f13a4aSGreg Roach                        }
2167a6f13a4aSGreg Roach                    }
2168a6f13a4aSGreg Roach                }
2169a6f13a4aSGreg Roach            }
2170a6f13a4aSGreg Roach        }
2171a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2172a6f13a4aSGreg Roach        if ($filters) {
2173a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2174a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2175299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2176a6f13a4aSGreg Roach                        unset($this->list[$key]);
2177a6f13a4aSGreg Roach                        break;
2178a6f13a4aSGreg Roach                    }
2179a6f13a4aSGreg Roach                }
2180a6f13a4aSGreg Roach            }
2181a6f13a4aSGreg Roach        }
2182a6f13a4aSGreg Roach        if ($filters2) {
218313abd6f3SGreg Roach            $mylist = [];
2184a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2185c0935879SGreg Roach                $key  = $indi->xref();
2186299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2187a6f13a4aSGreg Roach                $keep = true;
2188a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2189a6f13a4aSGreg Roach                    if ($keep) {
2190a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2191a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2192a6f13a4aSGreg Roach                        $val  = $filter['val'];
2193b2448a1bSGreg Roach                        if ($val === "''") {
21947a6ee1acSGreg Roach                            $val = '';
2195a6f13a4aSGreg Roach                        }
21967a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2197a6f13a4aSGreg Roach                        $t    = end($tags);
21983d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2199a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2200044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
22017a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
22027a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2203a6f13a4aSGreg Roach                            $t    = end($tags);
22043d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2205a6f13a4aSGreg Roach                        }
2206a6f13a4aSGreg Roach
2207a6f13a4aSGreg Roach                        switch ($expr) {
22087a6ee1acSGreg Roach                            case 'GTE':
2209044416d2SGreg Roach                                if ($t === 'DATE') {
2210a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2211a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2212a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2213a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2214a6f13a4aSGreg Roach                                    $keep = true;
2215a6f13a4aSGreg Roach                                }
2216a6f13a4aSGreg Roach                                break;
22177a6ee1acSGreg Roach                            case 'LTE':
2218044416d2SGreg Roach                                if ($t === 'DATE') {
2219a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2220a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2221a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2222a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2223a6f13a4aSGreg Roach                                    $keep = true;
2224a6f13a4aSGreg Roach                                }
2225a6f13a4aSGreg Roach                                break;
2226a6f13a4aSGreg Roach                            default:
2227a6f13a4aSGreg Roach                                if ($v == $val) {
2228a6f13a4aSGreg Roach                                    $keep = true;
2229a6f13a4aSGreg Roach                                } else {
2230a6f13a4aSGreg Roach                                    $keep = false;
2231a6f13a4aSGreg Roach                                }
2232a6f13a4aSGreg Roach                                break;
2233a6f13a4aSGreg Roach                        }
2234a6f13a4aSGreg Roach                    }
2235a6f13a4aSGreg Roach                }
2236a6f13a4aSGreg Roach                if ($keep) {
2237a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2238a6f13a4aSGreg Roach                }
2239a6f13a4aSGreg Roach            }
2240a6f13a4aSGreg Roach            $this->list = $mylist;
2241a6f13a4aSGreg Roach        }
2242a6f13a4aSGreg Roach
2243a6f13a4aSGreg Roach        switch ($sortby) {
2244a6f13a4aSGreg Roach            case 'NAME':
2245c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2246a6f13a4aSGreg Roach                break;
2247a6f13a4aSGreg Roach            case 'CHAN':
2248c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2249a6f13a4aSGreg Roach                break;
2250a6f13a4aSGreg Roach            case 'BIRT:DATE':
2251c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2252a6f13a4aSGreg Roach                break;
2253a6f13a4aSGreg Roach            case 'DEAT:DATE':
2254c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2255a6f13a4aSGreg Roach                break;
2256a6f13a4aSGreg Roach            case 'MARR:DATE':
2257c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2258a6f13a4aSGreg Roach                break;
2259a6f13a4aSGreg Roach            default:
2260a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2261a6f13a4aSGreg Roach                break;
2262a6f13a4aSGreg Roach        }
2263a6f13a4aSGreg Roach
22649b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2265e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2266a6f13a4aSGreg Roach    }
2267a6f13a4aSGreg Roach
2268a6f13a4aSGreg Roach    /**
226976692c8bSGreg Roach     * XML <List>
22708ba2e626SGreg Roach     *
22718ba2e626SGreg Roach     * @return void
2272a6f13a4aSGreg Roach     */
2273b702978eSGreg Roach    protected function listEndHandler(): void
2274c1010edaSGreg Roach    {
2275a6f13a4aSGreg Roach        $this->process_repeats--;
2276a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2277a6f13a4aSGreg Roach            return;
2278a6f13a4aSGreg Roach        }
2279a6f13a4aSGreg Roach
2280a6f13a4aSGreg Roach        // Check if there is any list
2281a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2282a6f13a4aSGreg Roach            $lineoffset = 0;
2283a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2284a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2285a6f13a4aSGreg Roach            }
2286a6f13a4aSGreg Roach            //-- read the xml from the file
2287299d100dSGreg Roach            $lines = file($this->report);
22887a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2289a6f13a4aSGreg Roach                $lineoffset--;
2290a6f13a4aSGreg Roach            }
2291a6f13a4aSGreg Roach            $lineoffset++;
2292a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2293a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2294a6f13a4aSGreg Roach            // List Level counter
2295a6f13a4aSGreg Roach            $count = 1;
2296a6f13a4aSGreg Roach            while (0 < $count) {
22977a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2298a6f13a4aSGreg Roach                    $count++;
22997a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2300a6f13a4aSGreg Roach                    $count--;
2301a6f13a4aSGreg Roach                }
2302a6f13a4aSGreg Roach                if (0 < $count) {
2303a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2304a6f13a4aSGreg Roach                }
2305a6f13a4aSGreg Roach                $line_nr++;
2306a6f13a4aSGreg Roach            }
2307a6f13a4aSGreg Roach            // No need to drag this
2308a6f13a4aSGreg Roach            unset($lines);
23097a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2310a6f13a4aSGreg Roach            // Save original values
23119b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2312a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2313a6f13a4aSGreg Roach
2314a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2315a6f13a4aSGreg Roach            $this->list_private = 0;
2316a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2317a6f13a4aSGreg Roach                if ($record->canShow()) {
2318f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2319a6f13a4aSGreg Roach                    //-- start the sax parser
2320a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2321e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2322a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
23231aa04befSGreg Roach
23241aa04befSGreg Roach                    xml_set_element_handler(
23251aa04befSGreg Roach                        $repeat_parser,
23269d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23271aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23281aa04befSGreg Roach                        },
23299d454b6bSGreg Roach                        function ($parser, string $name): void {
23301aa04befSGreg Roach                            $this->endElement($parser, $name);
23311aa04befSGreg Roach                        }
23321aa04befSGreg Roach                    );
23331aa04befSGreg Roach
23341aa04befSGreg Roach                    xml_set_character_data_handler(
23351aa04befSGreg Roach                        $repeat_parser,
23369d454b6bSGreg Roach                        function ($parser, string $data): void {
23371aa04befSGreg Roach                            $this->characterData($parser, $data);
23381aa04befSGreg Roach                        }
23391aa04befSGreg Roach                    );
23401aa04befSGreg Roach
2341a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23426ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2343a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2344a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2345a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2346a6f13a4aSGreg Roach                        ));
2347a6f13a4aSGreg Roach                    }
2348a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2349a6f13a4aSGreg Roach                } else {
2350a6f13a4aSGreg Roach                    $this->list_private++;
2351a6f13a4aSGreg Roach                }
2352a6f13a4aSGreg Roach            }
235313abd6f3SGreg Roach            $this->list   = [];
2354e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2355a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2356a6f13a4aSGreg Roach        }
235765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2358a6f13a4aSGreg Roach    }
2359a6f13a4aSGreg Roach
2360a6f13a4aSGreg Roach    /**
2361a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2362a6f13a4aSGreg Roach     * Prints the total number of records in a list
2363a6f13a4aSGreg Roach     * The total number is collected from
2364a6f13a4aSGreg Roach     * List and Relatives
23658ba2e626SGreg Roach     *
23668ba2e626SGreg Roach     * @return void
2367a6f13a4aSGreg Roach     */
2368b702978eSGreg Roach    protected function listTotalStartHandler(): void
2369c1010edaSGreg Roach    {
2370a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2371589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2372a6f13a4aSGreg Roach        } else {
23737a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2374a6f13a4aSGreg Roach        }
2375a6f13a4aSGreg Roach    }
2376a6f13a4aSGreg Roach
2377a6f13a4aSGreg Roach    /**
237876692c8bSGreg Roach     * XML <Relatives>
237976692c8bSGreg Roach     *
2380c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23818ba2e626SGreg Roach     *
23828ba2e626SGreg Roach     * @return void
2383a6f13a4aSGreg Roach     */
2384b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2385c1010edaSGreg Roach    {
2386a6f13a4aSGreg Roach        $this->process_repeats++;
2387a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2388a6f13a4aSGreg Roach            return;
2389a6f13a4aSGreg Roach        }
2390a6f13a4aSGreg Roach
2391e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2392e364afe4SGreg Roach
239313abd6f3SGreg Roach        $match = [];
2394a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2395d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2396a6f13a4aSGreg Roach            $sortby = trim($sortby);
2397a6f13a4aSGreg Roach        }
2398a6f13a4aSGreg Roach
2399a6f13a4aSGreg Roach        $maxgen = -1;
2400a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2401c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2402a6f13a4aSGreg Roach        }
2403a6f13a4aSGreg Roach
2404e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2405e364afe4SGreg Roach
2406a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2407d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2408a6f13a4aSGreg Roach            $group = trim($group);
2409a6f13a4aSGreg Roach        }
2410a6f13a4aSGreg Roach
2411e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2412e364afe4SGreg Roach
2413a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2414d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2415a6f13a4aSGreg Roach            $id = trim($id);
2416a6f13a4aSGreg Roach        }
2417a6f13a4aSGreg Roach
241813abd6f3SGreg Roach        $this->list = [];
2419299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2420d965cc1aSGreg Roach        if ($person instanceof Individual) {
2421a6f13a4aSGreg Roach            $this->list[$id] = $person;
2422a6f13a4aSGreg Roach            switch ($group) {
24237a6ee1acSGreg Roach                case 'child-family':
242439ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2425820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2426820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2427a6f13a4aSGreg Roach                        }
2428820b62dfSGreg Roach
2429820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2430c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2431a6f13a4aSGreg Roach                        }
2432a6f13a4aSGreg Roach                    }
2433a6f13a4aSGreg Roach                    break;
24347a6ee1acSGreg Roach                case 'spouse-family':
243539ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2436820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2437820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2438a6f13a4aSGreg Roach                        }
2439820b62dfSGreg Roach
2440820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2441c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2442a6f13a4aSGreg Roach                        }
2443a6f13a4aSGreg Roach                    }
2444a6f13a4aSGreg Roach                    break;
24457a6ee1acSGreg Roach                case 'direct-ancestors':
24463d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2447a6f13a4aSGreg Roach                    break;
24487a6ee1acSGreg Roach                case 'ancestors':
24493d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2450a6f13a4aSGreg Roach                    break;
24517a6ee1acSGreg Roach                case 'descendants':
2452a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24533d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2454a6f13a4aSGreg Roach                    break;
24557a6ee1acSGreg Roach                case 'all':
24563d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24573d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2458a6f13a4aSGreg Roach                    break;
2459a6f13a4aSGreg Roach            }
2460a6f13a4aSGreg Roach        }
2461a6f13a4aSGreg Roach
2462a6f13a4aSGreg Roach        switch ($sortby) {
2463a6f13a4aSGreg Roach            case 'NAME':
2464c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2465a6f13a4aSGreg Roach                break;
2466a6f13a4aSGreg Roach            case 'BIRT:DATE':
2467c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2468a6f13a4aSGreg Roach                break;
2469a6f13a4aSGreg Roach            case 'DEAT:DATE':
2470c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2471a6f13a4aSGreg Roach                break;
2472a6f13a4aSGreg Roach            case 'generation':
247313abd6f3SGreg Roach                $newarray = [];
2474a6f13a4aSGreg Roach                reset($this->list);
2475a6f13a4aSGreg Roach                $genCounter = 1;
2476a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2477a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2478a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2479a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
248079529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2481a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2482a6f13a4aSGreg Roach                        }
2483a6f13a4aSGreg Roach                    }
2484a6f13a4aSGreg Roach                    $genCounter++;
2485a6f13a4aSGreg Roach                }
2486a6f13a4aSGreg Roach                $this->list = $newarray;
2487a6f13a4aSGreg Roach                break;
2488a6f13a4aSGreg Roach            default:
2489a6f13a4aSGreg Roach                // unsorted
2490a6f13a4aSGreg Roach                break;
2491a6f13a4aSGreg Roach        }
24929b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2493e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2494a6f13a4aSGreg Roach    }
2495a6f13a4aSGreg Roach
2496a6f13a4aSGreg Roach    /**
249776692c8bSGreg Roach     * XML </ Relatives>
24988ba2e626SGreg Roach     *
24998ba2e626SGreg Roach     * @return void
2500a6f13a4aSGreg Roach     */
2501b702978eSGreg Roach    protected function relativesEndHandler(): void
2502c1010edaSGreg Roach    {
2503a6f13a4aSGreg Roach        $this->process_repeats--;
2504a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2505a6f13a4aSGreg Roach            return;
2506a6f13a4aSGreg Roach        }
2507a6f13a4aSGreg Roach
2508a6f13a4aSGreg Roach        // Check if there is any relatives
2509a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2510a6f13a4aSGreg Roach            $lineoffset = 0;
2511a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2512a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2513a6f13a4aSGreg Roach            }
2514a6f13a4aSGreg Roach            //-- read the xml from the file
2515299d100dSGreg Roach            $lines = file($this->report);
25167a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2517a6f13a4aSGreg Roach                $lineoffset--;
2518a6f13a4aSGreg Roach            }
2519a6f13a4aSGreg Roach            $lineoffset++;
2520a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2521a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2522a6f13a4aSGreg Roach            // Relatives Level counter
2523a6f13a4aSGreg Roach            $count = 1;
2524a6f13a4aSGreg Roach            while (0 < $count) {
25257a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2526a6f13a4aSGreg Roach                    $count++;
25277a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2528a6f13a4aSGreg Roach                    $count--;
2529a6f13a4aSGreg Roach                }
2530a6f13a4aSGreg Roach                if (0 < $count) {
2531a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2532a6f13a4aSGreg Roach                }
2533a6f13a4aSGreg Roach                $line_nr++;
2534a6f13a4aSGreg Roach            }
2535a6f13a4aSGreg Roach            // No need to drag this
2536a6f13a4aSGreg Roach            unset($lines);
2537a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2538a6f13a4aSGreg Roach            // Save original values
25399b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2540a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2541a6f13a4aSGreg Roach
2542a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2543a6f13a4aSGreg Roach            $this->list_private = 0;
2544b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2545a6f13a4aSGreg Roach                if (isset($value->generation)) {
2546a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2547a6f13a4aSGreg Roach                }
2548b092a991SGreg Roach                $tmp          = GedcomRecord::getInstance((string) $xref, $this->tree);
2549299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2550a6f13a4aSGreg Roach
2551a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2552e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2553a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25541aa04befSGreg Roach
25551aa04befSGreg Roach                xml_set_element_handler(
25561aa04befSGreg Roach                    $repeat_parser,
25579d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25581aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25591aa04befSGreg Roach                    },
25609d454b6bSGreg Roach                    function ($parser, string $name): void {
25611aa04befSGreg Roach                        $this->endElement($parser, $name);
25621aa04befSGreg Roach                    }
25631aa04befSGreg Roach                );
25641aa04befSGreg Roach
25651aa04befSGreg Roach                xml_set_character_data_handler(
25661aa04befSGreg Roach                    $repeat_parser,
25679d454b6bSGreg Roach                    function ($parser, string $data): void {
25681aa04befSGreg Roach                        $this->characterData($parser, $data);
25691aa04befSGreg Roach                    }
25701aa04befSGreg Roach                );
2571a6f13a4aSGreg Roach
2572a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25736ccdf4f0SGreg 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)));
2574a6f13a4aSGreg Roach                }
2575a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2576a6f13a4aSGreg Roach            }
2577a6f13a4aSGreg Roach            // Clean up the list array
257813abd6f3SGreg Roach            $this->list   = [];
2579e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2580a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2581a6f13a4aSGreg Roach        }
258265e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2583a6f13a4aSGreg Roach    }
2584a6f13a4aSGreg Roach
2585a6f13a4aSGreg Roach    /**
2586a6f13a4aSGreg Roach     * XML <Generation /> element handler
2587a6f13a4aSGreg Roach     * Prints the number of generations
25888ba2e626SGreg Roach     *
25898ba2e626SGreg Roach     * @return void
2590a6f13a4aSGreg Roach     */
2591b702978eSGreg Roach    protected function generationStartHandler(): void
2592c1010edaSGreg Roach    {
2593589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2594a6f13a4aSGreg Roach    }
2595a6f13a4aSGreg Roach
2596a6f13a4aSGreg Roach    /**
2597a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2598a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25998ba2e626SGreg Roach     *
26008ba2e626SGreg Roach     * @return void
2601a6f13a4aSGreg Roach     */
2602b702978eSGreg Roach    protected function newPageStartHandler(): void
2603c1010edaSGreg Roach    {
26047a6ee1acSGreg Roach        $temp = 'addpage';
2605e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2606a6f13a4aSGreg Roach    }
2607a6f13a4aSGreg Roach
2608a6f13a4aSGreg Roach    /**
260976692c8bSGreg Roach     * XML </titleEndHandler>
26108ba2e626SGreg Roach     *
26118ba2e626SGreg Roach     * @return void
2612a6f13a4aSGreg Roach     */
2613b702978eSGreg Roach    protected function titleEndHandler(): void
2614c1010edaSGreg Roach    {
26152836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2616a6f13a4aSGreg Roach    }
2617a6f13a4aSGreg Roach
2618a6f13a4aSGreg Roach    /**
261976692c8bSGreg Roach     * XML </descriptionEndHandler>
26208ba2e626SGreg Roach     *
26218ba2e626SGreg Roach     * @return void
2622a6f13a4aSGreg Roach     */
2623b702978eSGreg Roach    protected function descriptionEndHandler(): void
2624c1010edaSGreg Roach    {
26252836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2626a6f13a4aSGreg Roach    }
2627729ce104SGreg Roach
2628729ce104SGreg Roach    /**
262976692c8bSGreg Roach     * Create a list of all descendants.
263076692c8bSGreg Roach     *
2631729ce104SGreg Roach     * @param string[] $list
2632729ce104SGreg Roach     * @param string   $pid
2633729ce104SGreg Roach     * @param bool     $parents
2634729ce104SGreg Roach     * @param int      $generations
26358ba2e626SGreg Roach     *
26368ba2e626SGreg Roach     * @return void
2637729ce104SGreg Roach     */
26383b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2639c1010edaSGreg Roach    {
2640299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2641729ce104SGreg Roach        if ($person === null) {
2642729ce104SGreg Roach            return;
2643729ce104SGreg Roach        }
2644729ce104SGreg Roach        if (!isset($list[$pid])) {
2645729ce104SGreg Roach            $list[$pid] = $person;
2646729ce104SGreg Roach        }
2647729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2648729ce104SGreg Roach            $list[$pid]->generation = 0;
2649729ce104SGreg Roach        }
265039ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2651729ce104SGreg Roach            if ($parents) {
265239ca88baSGreg Roach                $husband = $family->husband();
265339ca88baSGreg Roach                $wife    = $family->wife();
2654729ce104SGreg Roach                if ($husband) {
2655c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2656729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2657c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2658729ce104SGreg Roach                    } else {
2659c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2660729ce104SGreg Roach                    }
2661729ce104SGreg Roach                }
2662729ce104SGreg Roach                if ($wife) {
2663c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2664729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2665c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2666729ce104SGreg Roach                    } else {
2667c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2668729ce104SGreg Roach                    }
2669729ce104SGreg Roach                }
2670729ce104SGreg Roach            }
2671820b62dfSGreg Roach
267239ca88baSGreg Roach            $children = $family->children();
2673820b62dfSGreg Roach
2674729ce104SGreg Roach            foreach ($children as $child) {
2675729ce104SGreg Roach                if ($child) {
2676c0935879SGreg Roach                    $list[$child->xref()] = $child;
2677820b62dfSGreg Roach
2678729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2679c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2680729ce104SGreg Roach                    } else {
2681c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2682729ce104SGreg Roach                    }
2683729ce104SGreg Roach                }
2684729ce104SGreg Roach            }
2685729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2686729ce104SGreg Roach                foreach ($children as $child) {
2687c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2688729ce104SGreg Roach                }
2689729ce104SGreg Roach            }
2690729ce104SGreg Roach        }
2691729ce104SGreg Roach    }
2692729ce104SGreg Roach
2693729ce104SGreg Roach    /**
269476692c8bSGreg Roach     * Create a list of all ancestors.
269576692c8bSGreg Roach     *
26960cc533a6SGreg Roach     * @param stdClass[] $list
2697729ce104SGreg Roach     * @param string     $pid
2698729ce104SGreg Roach     * @param bool       $children
2699729ce104SGreg Roach     * @param int        $generations
27008ba2e626SGreg Roach     *
27018ba2e626SGreg Roach     * @return void
2702729ce104SGreg Roach     */
27033b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2704c1010edaSGreg Roach    {
270513abd6f3SGreg Roach        $genlist                = [$pid];
2706729ce104SGreg Roach        $list[$pid]->generation = 1;
2707729ce104SGreg Roach        while (count($genlist) > 0) {
2708729ce104SGreg Roach            $id = array_shift($genlist);
2709729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2710729ce104SGreg Roach                continue; // id can be something like “empty7”
2711729ce104SGreg Roach            }
2712299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
271339ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
271439ca88baSGreg Roach                $husband = $family->husband();
271539ca88baSGreg Roach                $wife    = $family->wife();
2716729ce104SGreg Roach                if ($husband) {
2717c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2718c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2719729ce104SGreg Roach                }
2720729ce104SGreg Roach                if ($wife) {
2721c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2722c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2723729ce104SGreg Roach                }
2724729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2725729ce104SGreg Roach                    if ($husband) {
2726c0935879SGreg Roach                        $genlist[] = $husband->xref();
2727729ce104SGreg Roach                    }
2728729ce104SGreg Roach                    if ($wife) {
2729c0935879SGreg Roach                        $genlist[] = $wife->xref();
2730729ce104SGreg Roach                    }
2731729ce104SGreg Roach                }
2732729ce104SGreg Roach                if ($children) {
273339ca88baSGreg Roach                    foreach ($family->children() as $child) {
2734c0935879SGreg Roach                        $list[$child->xref()] = $child;
2735e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2736729ce104SGreg Roach                    }
2737729ce104SGreg Roach                }
2738729ce104SGreg Roach            }
2739729ce104SGreg Roach        }
2740729ce104SGreg Roach    }
2741729ce104SGreg Roach
2742729ce104SGreg Roach    /**
2743729ce104SGreg Roach     * get gedcom tag value
2744729ce104SGreg Roach     *
2745729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2746729ce104SGreg 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
2747729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2748729ce104SGreg Roach     *
2749729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2750729ce104SGreg Roach     */
2751b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2752c1010edaSGreg Roach    {
2753b2448a1bSGreg Roach        if ($gedrec === '') {
2754729ce104SGreg Roach            return '';
2755729ce104SGreg Roach        }
2756729ce104SGreg Roach        $tags      = explode(':', $tag);
2757729ce104SGreg Roach        $origlevel = $level;
2758b2448a1bSGreg Roach        if ($level === 0) {
27593c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2760729ce104SGreg Roach        }
2761729ce104SGreg Roach
2762729ce104SGreg Roach        $subrec = $gedrec;
2763729ce104SGreg Roach        foreach ($tags as $t) {
2764729ce104SGreg Roach            $lastsubrec = $subrec;
27653d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2766729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2767729ce104SGreg Roach                $level--;
27683d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2769729ce104SGreg Roach            }
2770729ce104SGreg Roach            if (empty($subrec)) {
2771044416d2SGreg Roach                if ($t === 'TITL') {
27723d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2773729ce104SGreg Roach                    if (!empty($subrec)) {
27747a6ee1acSGreg Roach                        $t = 'ABBR';
2775729ce104SGreg Roach                    }
2776729ce104SGreg Roach                }
2777b2448a1bSGreg Roach                if ($subrec === '') {
2778729ce104SGreg Roach                    if ($level > 0) {
2779729ce104SGreg Roach                        $level--;
2780729ce104SGreg Roach                    }
27813d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2782b2448a1bSGreg Roach                    if ($subrec === '') {
2783729ce104SGreg Roach                        return '';
2784729ce104SGreg Roach                    }
2785729ce104SGreg Roach                }
2786729ce104SGreg Roach            }
2787729ce104SGreg Roach            $level++;
2788729ce104SGreg Roach        }
2789729ce104SGreg Roach        $level--;
2790729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2791729ce104SGreg Roach        if ($ct == 0) {
2792729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2793729ce104SGreg Roach        }
2794729ce104SGreg Roach        if ($ct == 0) {
2795729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2796729ce104SGreg Roach        }
2797729ce104SGreg Roach        if ($ct > 0) {
2798729ce104SGreg Roach            $value = trim($match[1]);
2799044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2800299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2801ff166e64SGreg Roach                if ($note instanceof Note) {
2802729ce104SGreg Roach                    $value = $note->getNote();
2803729ce104SGreg Roach                } else {
2804729ce104SGreg Roach                    //-- set the value to the id without the @
2805729ce104SGreg Roach                    $value = $match[1];
2806729ce104SGreg Roach                }
2807729ce104SGreg Roach            }
28087a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28093d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2810729ce104SGreg Roach            }
2811729ce104SGreg Roach
2812729ce104SGreg Roach            return $value;
2813729ce104SGreg Roach        }
2814729ce104SGreg Roach
28157a6ee1acSGreg Roach        return '';
2816729ce104SGreg Roach    }
2817d1286247SGreg Roach
2818d1286247SGreg Roach    /**
2819d1286247SGreg Roach     * Replace variable identifiers with their values.
2820d1286247SGreg Roach     *
2821d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
282282759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2823d1286247SGreg Roach     *
2824d1286247SGreg Roach     * @return string
2825d1286247SGreg Roach     */
28268f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2827c1010edaSGreg Roach    {
2828d1286247SGreg Roach        return preg_replace_callback(
2829d1286247SGreg Roach            '/\$(\w+)/',
283018d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28312118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
283282759250SGreg Roach                    if ($quote) {
28332118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2834b2ce94c6SRico Sonntag                    }
2835b2ce94c6SRico Sonntag
28362118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
283782759250SGreg Roach                }
2838b2ce94c6SRico Sonntag
2839d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28403d7a8a4cSGreg Roach
2841d1286247SGreg Roach                return '$' . $matches[1];
2842d1286247SGreg Roach            },
2843d1286247SGreg Roach            $expression
2844d1286247SGreg Roach        );
2845d1286247SGreg Roach    }
2846a6f13a4aSGreg Roach}
2847