xref: /webtrees/app/Report/ReportParserGenerate.php (revision a04bb9a236e92915034f97b1229f5d47c9bc750f)
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;
36*a04bb9a2SGreg 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;
45*a04bb9a2SGreg 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
51b19e047dSGreg Roachuse function call_user_func;
52b2448a1bSGreg Roachuse function explode;
5329518ad2SGreg Roachuse function imagecreatefromstring;
5429518ad2SGreg Roachuse function imagesx;
5529518ad2SGreg Roachuse function imagesy;
56b19e047dSGreg Roachuse function method_exists;
57b2448a1bSGreg Roachuse function trim;
5829518ad2SGreg Roach
59a6f13a4aSGreg Roach/**
60a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
61a6f13a4aSGreg Roach */
62c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
63c1010edaSGreg Roach{
64a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
65a6f13a4aSGreg Roach    private $process_footnote = true;
66a6f13a4aSGreg Roach
67a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
68a6f13a4aSGreg Roach    private $print_data = false;
69a6f13a4aSGreg Roach
70a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
7113abd6f3SGreg Roach    private $print_data_stack = [];
72a6f13a4aSGreg Roach
7376692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
74a6f13a4aSGreg Roach    private $process_gedcoms = 0;
75a6f13a4aSGreg Roach
7676692c8bSGreg Roach    /** @var int Are we processing conditionals */
77a6f13a4aSGreg Roach    private $process_ifs = 0;
78a6f13a4aSGreg Roach
7976692c8bSGreg Roach    /** @var int Are we processing repeats */
80a6f13a4aSGreg Roach    private $process_repeats = 0;
81a6f13a4aSGreg Roach
82a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
83a6f13a4aSGreg Roach    private $repeat_bytes = 0;
84a6f13a4aSGreg Roach
855b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
8613abd6f3SGreg Roach    private $repeats = [];
87a6f13a4aSGreg Roach
88a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
8913abd6f3SGreg Roach    private $repeats_stack = [];
90a6f13a4aSGreg Roach
91208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
9213abd6f3SGreg Roach    private $wt_report_stack = [];
93e8e7866bSGreg Roach
94e8e7866bSGreg Roach    /** @var resource Nested repeating data */
95e8e7866bSGreg Roach    private $parser;
96e8e7866bSGreg Roach
97e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
9813abd6f3SGreg Roach    private $parser_stack = [];
99e8e7866bSGreg Roach
100a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
101a6f13a4aSGreg Roach    private $gedrec = '';
102a6f13a4aSGreg Roach
103a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
10413abd6f3SGreg Roach    private $gedrec_stack = [];
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
107a6f13a4aSGreg Roach    private $current_element;
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
110a6f13a4aSGreg Roach    private $footnote_element;
111a6f13a4aSGreg Roach
112a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
113a6f13a4aSGreg Roach    private $fact = '';
114a6f13a4aSGreg Roach
115a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
116a6f13a4aSGreg Roach    private $desc = '';
117a6f13a4aSGreg Roach
118a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
119a6f13a4aSGreg Roach    private $type = '';
120a6f13a4aSGreg Roach
121a6f13a4aSGreg Roach    /** @var int The current generational level */
122a6f13a4aSGreg Roach    private $generation = 1;
123a6f13a4aSGreg Roach
124a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
12513abd6f3SGreg Roach    private $list = [];
126a6f13a4aSGreg Roach
127a6f13a4aSGreg Roach    /** @var int Number of items in lists */
128a6f13a4aSGreg Roach    private $list_total = 0;
129a6f13a4aSGreg Roach
130a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
131a6f13a4aSGreg Roach    private $list_private = 0;
132a6f13a4aSGreg Roach
133299d100dSGreg Roach    /** @var string The filename of the XML report */
134299d100dSGreg Roach    protected $report;
135299d100dSGreg Roach
136208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
137e8e7866bSGreg Roach    private $report_root;
138e8e7866bSGreg Roach
139208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
140e8e7866bSGreg Roach    private $wt_report;
141e8e7866bSGreg Roach
142d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1432118c0e3SGreg Roach    private $vars;
144d1286247SGreg Roach
145299d100dSGreg Roach    /** @var Tree The current tree */
146299d100dSGreg Roach    private $tree;
147299d100dSGreg Roach
148*a04bb9a2SGreg Roach    /** @var FilesystemInterface */
149*a04bb9a2SGreg Roach    private $data_filesystem;
150*a04bb9a2SGreg Roach
15176692c8bSGreg Roach    /**
15276692c8bSGreg Roach     * Create a parser for a report
15376692c8bSGreg Roach     *
15476692c8bSGreg Roach     * @param string              $report The XML filename
155208e9f76SGreg Roach     * @param AbstractReport      $report_root
15676692c8bSGreg Roach     * @param string[][]          $vars
157299d100dSGreg Roach     * @param Tree                $tree
158*a04bb9a2SGreg Roach     * @param FilesystemInterface $data_filesystem
15976692c8bSGreg Roach     */
160*a04bb9a2SGreg Roach    public function __construct(
161*a04bb9a2SGreg Roach        string $report,
162*a04bb9a2SGreg Roach        AbstractReport $report_root,
163*a04bb9a2SGreg Roach        array $vars,
164*a04bb9a2SGreg Roach        Tree $tree,
165*a04bb9a2SGreg Roach        FilesystemInterface $data_filesystem
166*a04bb9a2SGreg Roach    ) {
167299d100dSGreg Roach        $this->report          = $report;
168e8e7866bSGreg Roach        $this->report_root     = $report_root;
169e8e7866bSGreg Roach        $this->wt_report       = $report_root;
17059f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
171d1286247SGreg Roach        $this->vars            = $vars;
172299d100dSGreg Roach        $this->tree            = $tree;
173*a04bb9a2SGreg Roach        $this->data_filesystem = $data_filesystem;
174299d100dSGreg Roach
17576f666f4SGreg Roach        parent::__construct($report);
176a6f13a4aSGreg Roach    }
177a6f13a4aSGreg Roach
178a6f13a4aSGreg Roach    /**
179a6f13a4aSGreg Roach     * XML start element handler
180a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
181a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
182a6f13a4aSGreg Roach     *
183a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
184a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1858a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
18618d7a90dSGreg Roach     *
18718d7a90dSGreg Roach     * @return void
188a6f13a4aSGreg Roach     */
189af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
190c1010edaSGreg Roach    {
19113abd6f3SGreg Roach        $newattrs = [];
192a6f13a4aSGreg Roach
193a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
194a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
195e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
196d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
197a6f13a4aSGreg Roach                }
198a6f13a4aSGreg Roach            }
199a6f13a4aSGreg Roach            $newattrs[$key] = $value;
200a6f13a4aSGreg Roach        }
201a6f13a4aSGreg Roach        $attrs = $newattrs;
2027a6ee1acSGreg 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')) {
203a0e2939aSGreg Roach            $method = $name . 'StartHandler';
204208e9f76SGreg Roach
205a0e2939aSGreg Roach            if (method_exists($this, $method)) {
206b19e047dSGreg Roach                call_user_func([$this, $method], $attrs);
207a6f13a4aSGreg Roach            }
208a6f13a4aSGreg Roach        }
209a6f13a4aSGreg Roach    }
210a6f13a4aSGreg Roach
211a6f13a4aSGreg Roach    /**
212a6f13a4aSGreg Roach     * XML end element handler
213a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
214a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
215a6f13a4aSGreg Roach     *
216a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
217a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
21818d7a90dSGreg Roach     *
21918d7a90dSGreg Roach     * @return void
220a6f13a4aSGreg Roach     */
221af14d238SGreg Roach    protected function endElement($parser, string $name): void
222c1010edaSGreg Roach    {
2237a6ee1acSGreg 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')) {
224a0e2939aSGreg Roach            $method = $name . 'EndHandler';
225a0e2939aSGreg Roach
226a0e2939aSGreg Roach            if (method_exists($this, $method)) {
227b19e047dSGreg Roach                call_user_func([$this, $method]);
228a6f13a4aSGreg Roach            }
229a6f13a4aSGreg Roach        }
230a6f13a4aSGreg Roach    }
231a6f13a4aSGreg Roach
232a6f13a4aSGreg Roach    /**
233a6f13a4aSGreg Roach     * XML character data handler
234a6f13a4aSGreg Roach     *
235a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
236a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
23718d7a90dSGreg Roach     *
23818d7a90dSGreg Roach     * @return void
239a6f13a4aSGreg Roach     */
240af14d238SGreg Roach    protected function characterData($parser, $data): void
241c1010edaSGreg Roach    {
242e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
243a6f13a4aSGreg Roach            $this->current_element->addText($data);
244a6f13a4aSGreg Roach        }
245a6f13a4aSGreg Roach    }
246a6f13a4aSGreg Roach
247a6f13a4aSGreg Roach    /**
24876692c8bSGreg Roach     * XML <style>
249a6f13a4aSGreg Roach     *
250c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2518ba2e626SGreg Roach     *
2528ba2e626SGreg Roach     * @return void
253a6f13a4aSGreg Roach     */
254b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
255c1010edaSGreg Roach    {
256a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2576ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
258a6f13a4aSGreg Roach        }
259a6f13a4aSGreg Roach
260a6f13a4aSGreg Roach        // array Style that will be passed on
26113abd6f3SGreg Roach        $s = [];
262a6f13a4aSGreg Roach
263a6f13a4aSGreg Roach        // string Name af the style
264a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
265a6f13a4aSGreg Roach
266a6f13a4aSGreg Roach        // string Name of the DEFAULT font
267208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
268a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
269a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
270a6f13a4aSGreg Roach        }
271a6f13a4aSGreg Roach
272a6f13a4aSGreg Roach        // int The size of the font in points
273208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
274a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
27559597b37SGreg Roach            // Get it as int to ignore all decimal points or text (if no text then 0)
27659597b37SGreg Roach            $s['size'] = (string) (int) $attrs['size'];
27759597b37SGreg Roach        }
278a6f13a4aSGreg Roach
279a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2807a6ee1acSGreg Roach        $s['style'] = '';
281a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
282a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
283a6f13a4aSGreg Roach        }
284a6f13a4aSGreg Roach
285e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
286a6f13a4aSGreg Roach    }
287a6f13a4aSGreg Roach
288a6f13a4aSGreg Roach    /**
28976692c8bSGreg Roach     * XML <Doc>
290a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
291a6f13a4aSGreg Roach     *
292c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
2938ba2e626SGreg Roach     *
2948ba2e626SGreg Roach     * @return void
295a6f13a4aSGreg Roach     */
296b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
297c1010edaSGreg Roach    {
298e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
299a6f13a4aSGreg Roach
300a6f13a4aSGreg Roach        // Custom page width
301a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
302208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
303a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
304a6f13a4aSGreg Roach        // Custom Page height
305a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
306208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
307a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
308a6f13a4aSGreg Roach
309a6f13a4aSGreg Roach        // Left Margin
310a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
3117a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
312208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
313a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
314208e9f76SGreg 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))
315a6f13a4aSGreg Roach            }
316a6f13a4aSGreg Roach        }
317a6f13a4aSGreg Roach        // Right Margin
318a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3197a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
320208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
321a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
322208e9f76SGreg 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))
323a6f13a4aSGreg Roach            }
324a6f13a4aSGreg Roach        }
325a6f13a4aSGreg Roach        // Top Margin
326a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3277a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
328208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
329a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
330208e9f76SGreg 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))
331a6f13a4aSGreg Roach            }
332a6f13a4aSGreg Roach        }
333a6f13a4aSGreg Roach        // Bottom Margin
334a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3357a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
336208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
337a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
338208e9f76SGreg 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))
339a6f13a4aSGreg Roach            }
340a6f13a4aSGreg Roach        }
341a6f13a4aSGreg Roach        // Header Margin
342a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3437a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
344208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
345a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
346208e9f76SGreg 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))
347a6f13a4aSGreg Roach            }
348a6f13a4aSGreg Roach        }
349a6f13a4aSGreg Roach        // Footer Margin
350a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3517a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
352208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
353a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
354208e9f76SGreg 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))
355a6f13a4aSGreg Roach            }
356a6f13a4aSGreg Roach        }
357a6f13a4aSGreg Roach
358a6f13a4aSGreg Roach        // Page Orientation
359a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
360044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3617a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
362044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3637a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
364a6f13a4aSGreg Roach            }
365a6f13a4aSGreg Roach        }
366a6f13a4aSGreg Roach        // Page Size
367a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
368208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
369a6f13a4aSGreg Roach        }
370a6f13a4aSGreg Roach
371a6f13a4aSGreg Roach        // Show Generated By...
372a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3737a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
374208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3757a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
376208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
377a6f13a4aSGreg Roach            }
378a6f13a4aSGreg Roach        }
379a6f13a4aSGreg Roach
380e8e7866bSGreg Roach        $this->wt_report->setup();
381a6f13a4aSGreg Roach    }
382a6f13a4aSGreg Roach
383a6f13a4aSGreg Roach    /**
38476692c8bSGreg Roach     * XML </Doc>
3858ba2e626SGreg Roach     *
3868ba2e626SGreg Roach     * @return void
387a6f13a4aSGreg Roach     */
388b702978eSGreg Roach    protected function docEndHandler(): void
389c1010edaSGreg Roach    {
390e8e7866bSGreg Roach        $this->wt_report->run();
391a6f13a4aSGreg Roach    }
392a6f13a4aSGreg Roach
393a6f13a4aSGreg Roach    /**
39476692c8bSGreg Roach     * XML <Header>
3958ba2e626SGreg Roach     *
3968ba2e626SGreg Roach     * @return void
397a6f13a4aSGreg Roach     */
398b702978eSGreg Roach    protected function headerStartHandler(): void
399c1010edaSGreg Roach    {
400a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
401e8e7866bSGreg Roach        $this->wt_report->clearHeader();
4027a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
403a6f13a4aSGreg Roach    }
404a6f13a4aSGreg Roach
405a6f13a4aSGreg Roach    /**
40676692c8bSGreg Roach     * XML <bodyStartHandler>
4078ba2e626SGreg Roach     *
4088ba2e626SGreg Roach     * @return void
409a6f13a4aSGreg Roach     */
410b702978eSGreg Roach    protected function bodyStartHandler(): void
411c1010edaSGreg Roach    {
4127a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
413a6f13a4aSGreg Roach    }
414a6f13a4aSGreg Roach
415a6f13a4aSGreg Roach    /**
41676692c8bSGreg Roach     * XML <footerStartHandler>
4178ba2e626SGreg Roach     *
4188ba2e626SGreg Roach     * @return void
419a6f13a4aSGreg Roach     */
420b702978eSGreg Roach    protected function footerStartHandler(): void
421c1010edaSGreg Roach    {
4227a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
423a6f13a4aSGreg Roach    }
424a6f13a4aSGreg Roach
425a6f13a4aSGreg Roach    /**
42676692c8bSGreg Roach     * XML <Cell>
427a6f13a4aSGreg Roach     *
428c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4298ba2e626SGreg Roach     *
4308ba2e626SGreg Roach     * @return void
431a6f13a4aSGreg Roach     */
432b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
433c1010edaSGreg Roach    {
434a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4357a6ee1acSGreg Roach        $align = '';
436a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
437a6f13a4aSGreg Roach            $align = $attrs['align'];
438a6f13a4aSGreg Roach            // RTL supported left/right alignment
439044416d2SGreg Roach            if ($align === 'rightrtl') {
440e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4417a6ee1acSGreg Roach                    $align = 'left';
442a6f13a4aSGreg Roach                } else {
4437a6ee1acSGreg Roach                    $align = 'right';
444a6f13a4aSGreg Roach                }
445044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
446e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4477a6ee1acSGreg Roach                    $align = 'right';
448a6f13a4aSGreg Roach                } else {
4497a6ee1acSGreg Roach                    $align = 'left';
450a6f13a4aSGreg Roach                }
451a6f13a4aSGreg Roach            }
452a6f13a4aSGreg Roach        }
453a6f13a4aSGreg Roach
454a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4557a6ee1acSGreg Roach        $bgcolor = '';
456a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
457a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
458a6f13a4aSGreg Roach        }
459a6f13a4aSGreg Roach
460a6f13a4aSGreg Roach        // int Whether or not the background should be painted
461a6f13a4aSGreg Roach        $fill = 1;
462a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4637a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
464a6f13a4aSGreg Roach                $fill = 0;
4657a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
466a6f13a4aSGreg Roach                $fill = 1;
467a6f13a4aSGreg Roach            }
468a6f13a4aSGreg Roach        }
469a6f13a4aSGreg Roach
470a6f13a4aSGreg Roach        $reseth = true;
471a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
472a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4737a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
474a6f13a4aSGreg Roach                $reseth = false;
4757a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
476a6f13a4aSGreg Roach                $reseth = true;
477a6f13a4aSGreg Roach            }
478a6f13a4aSGreg Roach        }
479a6f13a4aSGreg Roach
480a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
481a6f13a4aSGreg Roach        $border = 0;
482a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
483a6f13a4aSGreg Roach            $border = $attrs['border'];
484a6f13a4aSGreg Roach        }
485a6f13a4aSGreg Roach        // string Border color in HTML code
4867a6ee1acSGreg Roach        $bocolor = '';
487a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
488a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
489a6f13a4aSGreg Roach        }
490a6f13a4aSGreg Roach
491a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
492a6f13a4aSGreg Roach        $height = 0;
493a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
494589feda3SGreg Roach            $height = $attrs['height'];
495a6f13a4aSGreg Roach        }
496a6f13a4aSGreg 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.
497a6f13a4aSGreg Roach        $width = 0;
498a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
499589feda3SGreg Roach            $width = $attrs['width'];
500a6f13a4aSGreg Roach        }
501a6f13a4aSGreg Roach
502a6f13a4aSGreg Roach        // int Stretch carachter mode
503a6f13a4aSGreg Roach        $stretch = 0;
504a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
505a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
506a6f13a4aSGreg Roach        }
507a6f13a4aSGreg Roach
508a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
509c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
510a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5117a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
512c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
513a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
514a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5157a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
516a6f13a4aSGreg Roach                $left = 0;
517a6f13a4aSGreg Roach            }
518a6f13a4aSGreg Roach        }
519a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
520c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
521a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5227a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
523c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
524a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
525a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5267a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
527a6f13a4aSGreg Roach                $top = 0;
528a6f13a4aSGreg Roach            }
529a6f13a4aSGreg Roach        }
530a6f13a4aSGreg Roach
531a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5327a6ee1acSGreg Roach        $style = '';
533a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
534a6f13a4aSGreg Roach            $style = $attrs['style'];
535a6f13a4aSGreg Roach        }
536a6f13a4aSGreg Roach
537a6f13a4aSGreg Roach        // string Text color in html code
5387a6ee1acSGreg Roach        $tcolor = '';
539a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
540a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
541a6f13a4aSGreg Roach        }
542a6f13a4aSGreg Roach
543a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
544a6f13a4aSGreg Roach        $ln = 0;
545a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
546a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
547a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5487a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
549a6f13a4aSGreg Roach                $ln = 0;
550a6f13a4aSGreg Roach            }
551a6f13a4aSGreg Roach        }
552a6f13a4aSGreg Roach
553044416d2SGreg Roach        if ($align === 'left') {
5547a6ee1acSGreg Roach            $align = 'L';
555044416d2SGreg Roach        } elseif ($align === 'right') {
5567a6ee1acSGreg Roach            $align = 'R';
557044416d2SGreg Roach        } elseif ($align === 'center') {
5587a6ee1acSGreg Roach            $align = 'C';
559044416d2SGreg Roach        } elseif ($align === 'justify') {
5607a6ee1acSGreg Roach            $align = 'J';
561a6f13a4aSGreg Roach        }
562a6f13a4aSGreg Roach
5639b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
564a6f13a4aSGreg Roach        $this->print_data         = true;
565a6f13a4aSGreg Roach
566e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
567a6f13a4aSGreg Roach            $width,
568a6f13a4aSGreg Roach            $height,
569a6f13a4aSGreg Roach            $border,
570a6f13a4aSGreg Roach            $align,
571a6f13a4aSGreg Roach            $bgcolor,
572a6f13a4aSGreg Roach            $style,
573a6f13a4aSGreg Roach            $ln,
574a6f13a4aSGreg Roach            $top,
575a6f13a4aSGreg Roach            $left,
576a6f13a4aSGreg Roach            $fill,
577a6f13a4aSGreg Roach            $stretch,
578a6f13a4aSGreg Roach            $bocolor,
579a6f13a4aSGreg Roach            $tcolor,
580a6f13a4aSGreg Roach            $reseth
581a6f13a4aSGreg Roach        );
582a6f13a4aSGreg Roach    }
583a6f13a4aSGreg Roach
584a6f13a4aSGreg Roach    /**
58576692c8bSGreg Roach     * XML </Cell>
5868ba2e626SGreg Roach     *
5878ba2e626SGreg Roach     * @return void
588a6f13a4aSGreg Roach     */
589b702978eSGreg Roach    protected function cellEndHandler(): void
590c1010edaSGreg Roach    {
591a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
592e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
593a6f13a4aSGreg Roach    }
594a6f13a4aSGreg Roach
595a6f13a4aSGreg Roach    /**
596a6f13a4aSGreg Roach     * XML <Now /> element handler
5978ba2e626SGreg Roach     *
5988ba2e626SGreg Roach     * @return void
599a6f13a4aSGreg Roach     */
600b702978eSGreg Roach    protected function nowStartHandler(): void
601c1010edaSGreg Roach    {
6024459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
603a6f13a4aSGreg Roach    }
604a6f13a4aSGreg Roach
605a6f13a4aSGreg Roach    /**
606a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6078ba2e626SGreg Roach     *
6088ba2e626SGreg Roach     * @return void
609a6f13a4aSGreg Roach     */
610b702978eSGreg Roach    protected function pageNumStartHandler(): void
611c1010edaSGreg Roach    {
6127a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
613a6f13a4aSGreg Roach    }
614a6f13a4aSGreg Roach
615a6f13a4aSGreg Roach    /**
616a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6178ba2e626SGreg Roach     *
6188ba2e626SGreg Roach     * @return void
619a6f13a4aSGreg Roach     */
620b702978eSGreg Roach    protected function totalPagesStartHandler(): void
621c1010edaSGreg Roach    {
6227a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
623a6f13a4aSGreg Roach    }
624a6f13a4aSGreg Roach
625a6f13a4aSGreg Roach    /**
626a6f13a4aSGreg Roach     * Called at the start of an element.
627a6f13a4aSGreg Roach     *
628c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6298ba2e626SGreg Roach     *
6308ba2e626SGreg Roach     * @return void
631a6f13a4aSGreg Roach     */
632b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
633c1010edaSGreg Roach    {
634a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
635a6f13a4aSGreg Roach            $this->process_gedcoms++;
636a6f13a4aSGreg Roach
637a6f13a4aSGreg Roach            return;
638a6f13a4aSGreg Roach        }
639a6f13a4aSGreg Roach
640a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6417a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6427a6ee1acSGreg Roach        $tags      = explode(':', $tag);
643a6f13a4aSGreg Roach        $newgedrec = '';
644a6f13a4aSGreg Roach        if (count($tags) < 2) {
645299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
646299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
647a6f13a4aSGreg Roach        }
648a6f13a4aSGreg Roach        if (empty($newgedrec)) {
649a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
650a6f13a4aSGreg Roach            $newgedrec = '';
651a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6527a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
653d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
654d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
655a6f13a4aSGreg Roach                    } else {
656299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
657299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
658a6f13a4aSGreg Roach                    }
659a6f13a4aSGreg Roach                } else {
6607a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66113abd6f3SGreg Roach                        $gmatch = [];
662a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
663299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
664299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
665a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
666a6f13a4aSGreg Roach                        } else {
667a6f13a4aSGreg Roach                            $newgedrec = '';
668a6f13a4aSGreg Roach                            break;
669a6f13a4aSGreg Roach                        }
670a6f13a4aSGreg Roach                    } else {
671b2448a1bSGreg Roach                        $level     = 1 + (int) explode(' ', trim($tgedrec))[0];
6723d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
673a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
674a6f13a4aSGreg Roach                    }
675a6f13a4aSGreg Roach                }
676a6f13a4aSGreg Roach            }
677a6f13a4aSGreg Roach        }
678a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6799b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
680a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
681a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
682a6f13a4aSGreg Roach                $this->fact = $match[2];
683a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
684a6f13a4aSGreg Roach            }
685a6f13a4aSGreg Roach        } else {
686a6f13a4aSGreg Roach            $this->process_gedcoms++;
687a6f13a4aSGreg Roach        }
688a6f13a4aSGreg Roach    }
689a6f13a4aSGreg Roach
690a6f13a4aSGreg Roach    /**
691a6f13a4aSGreg Roach     * Called at the end of an element.
6928ba2e626SGreg Roach     *
6938ba2e626SGreg Roach     * @return void
694a6f13a4aSGreg Roach     */
695b702978eSGreg Roach    protected function gedcomEndHandler(): void
696c1010edaSGreg Roach    {
697a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
698a6f13a4aSGreg Roach            $this->process_gedcoms--;
699a6f13a4aSGreg Roach        } else {
70065e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
701a6f13a4aSGreg Roach        }
702a6f13a4aSGreg Roach    }
703a6f13a4aSGreg Roach
704a6f13a4aSGreg Roach    /**
70576692c8bSGreg Roach     * XML <textBoxStartHandler>
706a6f13a4aSGreg Roach     *
707c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7088ba2e626SGreg Roach     *
7098ba2e626SGreg Roach     * @return void
710a6f13a4aSGreg Roach     */
711b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
712c1010edaSGreg Roach    {
713a6f13a4aSGreg Roach        // string Background color code
7147a6ee1acSGreg Roach        $bgcolor = '';
715a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
716a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
717a6f13a4aSGreg Roach        }
718a6f13a4aSGreg Roach
719a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
720a6f13a4aSGreg Roach        $fill = true;
721a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7227a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
723a6f13a4aSGreg Roach                $fill = false;
7247a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
725a6f13a4aSGreg Roach                $fill = true;
726a6f13a4aSGreg Roach            }
727a6f13a4aSGreg Roach        }
728a6f13a4aSGreg Roach
729a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
730a6f13a4aSGreg Roach        $border = false;
731a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7327a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
733a6f13a4aSGreg Roach                $border = true;
7347a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
735a6f13a4aSGreg Roach                $border = false;
736a6f13a4aSGreg Roach            }
737a6f13a4aSGreg Roach        }
738a6f13a4aSGreg Roach
739a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
740a6f13a4aSGreg Roach        $height = 0;
741a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
742a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
743a6f13a4aSGreg Roach        }
744a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
745a6f13a4aSGreg Roach        $width = 0;
746a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
747a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
748a6f13a4aSGreg Roach        }
749a6f13a4aSGreg Roach
750a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
751c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
752a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7537a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
754c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
755a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
756a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7577a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
758a6f13a4aSGreg Roach                $left = 0;
759a6f13a4aSGreg Roach            }
760a6f13a4aSGreg Roach        }
761a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
762c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
763a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7647a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
765c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
766a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
767a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7687a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
769a6f13a4aSGreg Roach                $top = 0;
770a6f13a4aSGreg Roach            }
771a6f13a4aSGreg Roach        }
772a6f13a4aSGreg 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
773a6f13a4aSGreg Roach        $newline = false;
774a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7757a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
776a6f13a4aSGreg Roach                $newline = true;
7777a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
778a6f13a4aSGreg Roach                $newline = false;
779a6f13a4aSGreg Roach            }
780a6f13a4aSGreg Roach        }
781a6f13a4aSGreg Roach        // boolean
782a6f13a4aSGreg Roach        $pagecheck = true;
783a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7847a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
785a6f13a4aSGreg Roach                $pagecheck = false;
7867a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
787a6f13a4aSGreg Roach                $pagecheck = true;
788a6f13a4aSGreg Roach            }
789a6f13a4aSGreg Roach        }
790a6f13a4aSGreg Roach        // boolean Cell padding
791a6f13a4aSGreg Roach        $padding = true;
792a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
7937a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
794a6f13a4aSGreg Roach                $padding = false;
7957a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
796a6f13a4aSGreg Roach                $padding = true;
797a6f13a4aSGreg Roach            }
798a6f13a4aSGreg Roach        }
799a6f13a4aSGreg Roach        // boolean Reset this box Height
800a6f13a4aSGreg Roach        $reseth = false;
801a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8027a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
803a6f13a4aSGreg Roach                $reseth = true;
8047a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
805a6f13a4aSGreg Roach                $reseth = false;
806a6f13a4aSGreg Roach            }
807a6f13a4aSGreg Roach        }
808a6f13a4aSGreg Roach
809a6f13a4aSGreg Roach        // string Style of rendering
8107a6ee1acSGreg Roach        $style = '';
811a6f13a4aSGreg Roach
8129b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
813a6f13a4aSGreg Roach        $this->print_data         = false;
814a6f13a4aSGreg Roach
8159b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
816e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
817a6f13a4aSGreg Roach            $width,
818a6f13a4aSGreg Roach            $height,
819a6f13a4aSGreg Roach            $border,
820a6f13a4aSGreg Roach            $bgcolor,
821a6f13a4aSGreg Roach            $newline,
822a6f13a4aSGreg Roach            $left,
823a6f13a4aSGreg Roach            $top,
824a6f13a4aSGreg Roach            $pagecheck,
825a6f13a4aSGreg Roach            $style,
826a6f13a4aSGreg Roach            $fill,
827a6f13a4aSGreg Roach            $padding,
828a6f13a4aSGreg Roach            $reseth
829a6f13a4aSGreg Roach        );
830a6f13a4aSGreg Roach    }
831a6f13a4aSGreg Roach
832a6f13a4aSGreg Roach    /**
83376692c8bSGreg Roach     * XML <textBoxEndHandler>
8348ba2e626SGreg Roach     *
8358ba2e626SGreg Roach     * @return void
836a6f13a4aSGreg Roach     */
837b702978eSGreg Roach    protected function textBoxEndHandler(): void
838c1010edaSGreg Roach    {
839a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
840e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
84131d9777aSGreg Roach
84231d9777aSGreg Roach        // The TextBox handler is mis-using the wt_report attribute to store an element.
84331d9777aSGreg Roach        // Until this can be re-designed, we need this assertion to help static analysis tools.
84431d9777aSGreg Roach        assert($this->current_element instanceof ReportBaseElement, new LogicException());
84531d9777aSGreg Roach
846e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
847e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
848a6f13a4aSGreg Roach    }
849a6f13a4aSGreg Roach
850a6f13a4aSGreg Roach    /**
85176692c8bSGreg Roach     * XLM <Text>.
85276692c8bSGreg Roach     *
853c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8548ba2e626SGreg Roach     *
8558ba2e626SGreg Roach     * @return void
856a6f13a4aSGreg Roach     */
857b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
858c1010edaSGreg Roach    {
8599b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
860a6f13a4aSGreg Roach        $this->print_data         = true;
861a6f13a4aSGreg Roach
862a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8637a6ee1acSGreg Roach        $style = '';
864a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
865a6f13a4aSGreg Roach            $style = $attrs['style'];
866a6f13a4aSGreg Roach        }
867a6f13a4aSGreg Roach
868a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8697a6ee1acSGreg Roach        $color = '';
870a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
871a6f13a4aSGreg Roach            $color = $attrs['color'];
872a6f13a4aSGreg Roach        }
873a6f13a4aSGreg Roach
874e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
875a6f13a4aSGreg Roach    }
876a6f13a4aSGreg Roach
877a6f13a4aSGreg Roach    /**
87876692c8bSGreg Roach     * XML </Text>
8798ba2e626SGreg Roach     *
8808ba2e626SGreg Roach     * @return void
881a6f13a4aSGreg Roach     */
882b702978eSGreg Roach    protected function textEndHandler(): void
883c1010edaSGreg Roach    {
884a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
885e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
886a6f13a4aSGreg Roach    }
887a6f13a4aSGreg Roach
888a6f13a4aSGreg Roach    /**
88976692c8bSGreg Roach     * XML <GetPersonName/>
890a6f13a4aSGreg Roach     * Get the name
891a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
892a6f13a4aSGreg Roach     * 2. id is set with a record id
893a6f13a4aSGreg Roach     *
894c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8958ba2e626SGreg Roach     *
8968ba2e626SGreg Roach     * @return void
897a6f13a4aSGreg Roach     */
898b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
899c1010edaSGreg Roach    {
9007a6ee1acSGreg Roach        $id    = '';
90113abd6f3SGreg Roach        $match = [];
902a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9037a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
904a6f13a4aSGreg Roach                $id = $match[1];
905a6f13a4aSGreg Roach            }
906a6f13a4aSGreg Roach        } else {
9077a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
908d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
909d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
910a6f13a4aSGreg Roach                }
911a6f13a4aSGreg Roach            } else {
9127a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91313abd6f3SGreg Roach                    $gmatch = [];
914a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
915a6f13a4aSGreg Roach                        $id = $gmatch[1];
916a6f13a4aSGreg Roach                    }
917a6f13a4aSGreg Roach                } else {
918a6f13a4aSGreg Roach                    $id = $attrs['id'];
919a6f13a4aSGreg Roach                }
920a6f13a4aSGreg Roach            }
921a6f13a4aSGreg Roach        }
922a6f13a4aSGreg Roach        if (!empty($id)) {
923299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9248f038c36SRico Sonntag            if ($record === null) {
925a6f13a4aSGreg Roach                return;
926a6f13a4aSGreg Roach            }
927a6f13a4aSGreg Roach            if (!$record->canShowName()) {
928a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
929a6f13a4aSGreg Roach            } else {
93039ca88baSGreg Roach                $name = $record->fullName();
931a6f13a4aSGreg Roach                $name = preg_replace(
932c1010edaSGreg Roach                    [
933c1010edaSGreg Roach                        '/<span class="starredname">/',
934c1010edaSGreg Roach                        '/<\/span><\/span>/',
935c1010edaSGreg Roach                        '/<\/span>/',
936c1010edaSGreg Roach                    ],
937c1010edaSGreg Roach                    [
938c1010edaSGreg Roach                        '«',
939c1010edaSGreg Roach                        '',
940c1010edaSGreg Roach                        '»',
941c1010edaSGreg Roach                    ],
942a6f13a4aSGreg Roach                    $name
943a6f13a4aSGreg Roach                );
944a6f13a4aSGreg Roach                $name = strip_tags($name);
945a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
946381e28f4SGreg Roach                    $name = Str::limit($name, (int) $attrs['truncate'], I18N::translate('…'));
947a6f13a4aSGreg Roach                } else {
94839ca88baSGreg Roach                    $addname = $record->alternateName();
949a6f13a4aSGreg Roach                    $addname = preg_replace(
950c1010edaSGreg Roach                        [
951c1010edaSGreg Roach                            '/<span class="starredname">/',
952c1010edaSGreg Roach                            '/<\/span><\/span>/',
953c1010edaSGreg Roach                            '/<\/span>/',
954c1010edaSGreg Roach                        ],
955c1010edaSGreg Roach                        [
956c1010edaSGreg Roach                            '«',
957c1010edaSGreg Roach                            '',
958c1010edaSGreg Roach                            '»',
959c1010edaSGreg Roach                        ],
960a6f13a4aSGreg Roach                        $addname
961a6f13a4aSGreg Roach                    );
962a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
963a6f13a4aSGreg Roach                    if (!empty($addname)) {
9647a6ee1acSGreg Roach                        $name .= ' ' . $addname;
965a6f13a4aSGreg Roach                    }
966a6f13a4aSGreg Roach                }
967a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
968a6f13a4aSGreg Roach            }
969a6f13a4aSGreg Roach        }
970a6f13a4aSGreg Roach    }
971a6f13a4aSGreg Roach
972a6f13a4aSGreg Roach    /**
97376692c8bSGreg Roach     * XML <GedcomValue/>
974a6f13a4aSGreg Roach     *
975c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9768ba2e626SGreg Roach     *
9778ba2e626SGreg Roach     * @return void
978a6f13a4aSGreg Roach     */
979b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
980c1010edaSGreg Roach    {
9817a6ee1acSGreg Roach        $id    = '';
98213abd6f3SGreg Roach        $match = [];
9837a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
984a6f13a4aSGreg Roach            $id = $match[1];
985a6f13a4aSGreg Roach        }
986a6f13a4aSGreg Roach
987044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9887a6ee1acSGreg Roach            $useBreak = '1';
989a6f13a4aSGreg Roach        } else {
9907a6ee1acSGreg Roach            $useBreak = '0';
991a6f13a4aSGreg Roach        }
992a6f13a4aSGreg Roach
993a6f13a4aSGreg Roach        $tag = $attrs['tag'];
994a6f13a4aSGreg Roach        if (!empty($tag)) {
995044416d2SGreg Roach            if ($tag === '@desc') {
996a6f13a4aSGreg Roach                $value = $this->desc;
997a6f13a4aSGreg Roach                $value = trim($value);
998a6f13a4aSGreg Roach                $this->current_element->addText($value);
999a6f13a4aSGreg Roach            }
1000044416d2SGreg Roach            if ($tag === '@id') {
1001a6f13a4aSGreg Roach                $this->current_element->addText($id);
1002a6f13a4aSGreg Roach            } else {
10037a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1004a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
1005b2448a1bSGreg Roach                    $level = (int) explode(' ', trim($this->gedrec))[0];
1006b2448a1bSGreg Roach                    if ($level === 0) {
1007a6f13a4aSGreg Roach                        $level++;
1008a6f13a4aSGreg Roach                    }
1009a6f13a4aSGreg Roach                } else {
1010b2448a1bSGreg Roach                    $level = (int) $attrs['level'];
1011a6f13a4aSGreg Roach                }
1012a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10133d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1014a6f13a4aSGreg Roach                switch (end($tags)) {
1015a6f13a4aSGreg Roach                    case 'DATE':
1016a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1017a6f13a4aSGreg Roach                        $value = $tmp->display();
1018a6f13a4aSGreg Roach                        break;
1019a6f13a4aSGreg Roach                    case 'PLAC':
1020299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1021392561bbSGreg Roach                        $value = $tmp->shortName();
1022a6f13a4aSGreg Roach                        break;
1023a6f13a4aSGreg Roach                }
1024044416d2SGreg Roach                if ($useBreak === '1') {
1025a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1026a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1027a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1028a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1029a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10303b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10313b3cfeeaSGreg Roach                        $value = substr($value, 4);
1032a6f13a4aSGreg Roach                    }
1033a6f13a4aSGreg Roach                }
1034d4d660b7SGreg Roach                $tmp = explode(':', $tag);
10353cfcc809SGreg Roach                if (in_array(end($tmp), ['NOTE', 'TEXT'], true)) {
1036299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1037a4d703aeSGreg Roach                }
1038a2a485c3SGreg Roach
1039a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
10409c32db08SGreg Roach                    $value = strip_tags($value);
1041381e28f4SGreg Roach                    $value = Str::limit($value, (int) $attrs['truncate'], I18N::translate('…'));
1042a2a485c3SGreg Roach                }
1043a6f13a4aSGreg Roach                $this->current_element->addText($value);
1044a6f13a4aSGreg Roach            }
1045a6f13a4aSGreg Roach        }
1046a6f13a4aSGreg Roach    }
1047a6f13a4aSGreg Roach
1048a6f13a4aSGreg Roach    /**
104976692c8bSGreg Roach     * XML <RepeatTag>
1050a6f13a4aSGreg Roach     *
1051c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10528ba2e626SGreg Roach     *
10538ba2e626SGreg Roach     * @return void
1054a6f13a4aSGreg Roach     */
1055b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1056c1010edaSGreg Roach    {
1057a6f13a4aSGreg Roach        $this->process_repeats++;
1058a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1059a6f13a4aSGreg Roach            return;
1060a6f13a4aSGreg Roach        }
1061a6f13a4aSGreg Roach
10629b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
106313abd6f3SGreg Roach        $this->repeats         = [];
1064e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1065a6f13a4aSGreg Roach
1066e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1067a6f13a4aSGreg Roach        if (!empty($tag)) {
1068044416d2SGreg Roach            if ($tag === '@desc') {
1069a6f13a4aSGreg Roach                $value = $this->desc;
1070a6f13a4aSGreg Roach                $value = trim($value);
1071a6f13a4aSGreg Roach                $this->current_element->addText($value);
1072a6f13a4aSGreg Roach            } else {
10737a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10747a6ee1acSGreg Roach                $tags  = explode(':', $tag);
1075b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1076b2448a1bSGreg Roach                if ($level === 0) {
1077a6f13a4aSGreg Roach                    $level++;
1078a6f13a4aSGreg Roach                }
1079a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1080a6f13a4aSGreg Roach                $t      = $tag;
1081a6f13a4aSGreg Roach                $count  = count($tags);
1082a6f13a4aSGreg Roach                $i      = 0;
1083a6f13a4aSGreg Roach                while ($i < $count) {
1084a6f13a4aSGreg Roach                    $t = $tags[$i];
1085a6f13a4aSGreg Roach                    if (!empty($t)) {
1086a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10873d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1088a6f13a4aSGreg Roach                            if (empty($subrec)) {
1089a6f13a4aSGreg Roach                                $level--;
10903d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1091a6f13a4aSGreg Roach                                if (empty($subrec)) {
1092a6f13a4aSGreg Roach                                    return;
1093a6f13a4aSGreg Roach                                }
1094a6f13a4aSGreg Roach                            }
1095a6f13a4aSGreg Roach                        }
1096a6f13a4aSGreg Roach                        $level++;
1097a6f13a4aSGreg Roach                    }
1098a6f13a4aSGreg Roach                    $i++;
1099a6f13a4aSGreg Roach                }
1100a6f13a4aSGreg Roach                $level--;
1101a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1102a6f13a4aSGreg Roach                $i     = 0;
1103a6f13a4aSGreg Roach                while ($i < $count) {
1104a6f13a4aSGreg Roach                    $i++;
1105a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1106a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11078d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1108299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1109a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1110a9007102SGreg Roach                            continue;
1111a9007102SGreg Roach                        }
1112a9007102SGreg Roach                    }
1113a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1114a6f13a4aSGreg Roach                }
1115a6f13a4aSGreg Roach            }
1116a6f13a4aSGreg Roach        }
1117a6f13a4aSGreg Roach    }
1118a6f13a4aSGreg Roach
1119a6f13a4aSGreg Roach    /**
112076692c8bSGreg Roach     * XML </ RepeatTag>
11218ba2e626SGreg Roach     *
11228ba2e626SGreg Roach     * @return void
1123a6f13a4aSGreg Roach     */
1124b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1125c1010edaSGreg Roach    {
1126a6f13a4aSGreg Roach        $this->process_repeats--;
1127a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1128a6f13a4aSGreg Roach            return;
1129a6f13a4aSGreg Roach        }
1130a6f13a4aSGreg Roach
1131a6f13a4aSGreg Roach        // Check if there is anything to repeat
1132a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1133a6f13a4aSGreg Roach            // No need to load them if not used...
1134a6f13a4aSGreg Roach
1135a6f13a4aSGreg Roach            $lineoffset = 0;
1136a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1137a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1138a6f13a4aSGreg Roach            }
1139a6f13a4aSGreg Roach            //-- read the xml from the file
1140299d100dSGreg Roach            $lines = file($this->report);
11417a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1142a6f13a4aSGreg Roach                $lineoffset--;
1143a6f13a4aSGreg Roach            }
1144a6f13a4aSGreg Roach            $lineoffset++;
1145a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1146a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1147a6f13a4aSGreg Roach            // RepeatTag Level counter
1148a6f13a4aSGreg Roach            $count = 1;
1149a6f13a4aSGreg Roach            while (0 < $count) {
11507a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1151a6f13a4aSGreg Roach                    $count++;
11527a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1153a6f13a4aSGreg Roach                    $count--;
1154a6f13a4aSGreg Roach                }
1155a6f13a4aSGreg Roach                if (0 < $count) {
1156a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1157a6f13a4aSGreg Roach                }
1158a6f13a4aSGreg Roach                $line_nr++;
1159a6f13a4aSGreg Roach            }
1160a6f13a4aSGreg Roach            // No need to drag this
1161a6f13a4aSGreg Roach            unset($lines);
1162a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1163a6f13a4aSGreg Roach            // Save original values
11649b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1165a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1166a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1167a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1168a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1169e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1170a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11711aa04befSGreg Roach
11721aa04befSGreg Roach                xml_set_element_handler(
11731aa04befSGreg Roach                    $repeat_parser,
11749d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11751aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11761aa04befSGreg Roach                    },
11779d454b6bSGreg Roach                    function ($parser, string $name): void {
11781aa04befSGreg Roach                        $this->endElement($parser, $name);
11791aa04befSGreg Roach                    }
11801aa04befSGreg Roach                );
11811aa04befSGreg Roach
11821aa04befSGreg Roach                xml_set_character_data_handler(
11831aa04befSGreg Roach                    $repeat_parser,
11849d454b6bSGreg Roach                    function ($parser, string $data): void {
11851aa04befSGreg Roach                        $this->characterData($parser, $data);
11861aa04befSGreg Roach                    }
11871aa04befSGreg Roach                );
11881aa04befSGreg Roach
1189a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
11906ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1191a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1192a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1193a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1194a6f13a4aSGreg Roach                    ));
1195a6f13a4aSGreg Roach                }
1196a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1197a6f13a4aSGreg Roach            }
1198a6f13a4aSGreg Roach            // Restore original values
1199a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1200e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1201a6f13a4aSGreg Roach        }
120265e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1203a6f13a4aSGreg Roach    }
1204a6f13a4aSGreg Roach
1205a6f13a4aSGreg Roach    /**
1206a6f13a4aSGreg Roach     * Variable lookup
1207a6f13a4aSGreg Roach     * Retrieve predefined variables :
1208a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1209a6f13a4aSGreg Roach     *        1 EVEN This is a description
1210a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1211a6f13a4aSGreg Roach     * $ I18N::translate('....')
1212a6f13a4aSGreg Roach     * $ language_settings[]
1213a6f13a4aSGreg Roach     *
1214c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12158ba2e626SGreg Roach     *
12168ba2e626SGreg Roach     * @return void
1217a6f13a4aSGreg Roach     */
1218b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1219c1010edaSGreg Roach    {
1220a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12216ccdf4f0SGreg 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));
1222a6f13a4aSGreg Roach        }
1223a6f13a4aSGreg Roach
1224a6f13a4aSGreg Roach        $var = $attrs['var'];
1225a6f13a4aSGreg Roach        // SetVar element preset variables
1226d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1227d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1228a6f13a4aSGreg Roach        } else {
1229a6f13a4aSGreg Roach            $tfact = $this->fact;
12307a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1231a6f13a4aSGreg Roach                // Use :
1232a6f13a4aSGreg Roach                // n TYPE This text if string
1233a6f13a4aSGreg Roach                $tfact = $this->type;
1234a6f13a4aSGreg Roach            }
1235c1010edaSGreg Roach            $var = str_replace([
1236c1010edaSGreg Roach                '@fact',
1237c1010edaSGreg Roach                '@desc',
1238c1010edaSGreg Roach            ], [
1239c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1240c1010edaSGreg Roach                $this->desc,
1241c1010edaSGreg Roach            ], $var);
1242a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1243da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1244a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1245a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1246a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1247a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1248a6f13a4aSGreg Roach            }
1249a6f13a4aSGreg Roach        }
1250a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1251a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12527a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1253a6f13a4aSGreg Roach                $g   = new Date($var);
1254a6f13a4aSGreg Roach                $var = $g->display();
1255a6f13a4aSGreg Roach            }
1256a6f13a4aSGreg Roach        }
1257a6f13a4aSGreg Roach        $this->current_element->addText($var);
12582836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1259a6f13a4aSGreg Roach    }
1260a6f13a4aSGreg Roach
1261a6f13a4aSGreg Roach    /**
126276692c8bSGreg Roach     * XML <Facts>
126376692c8bSGreg Roach     *
1264c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12658ba2e626SGreg Roach     *
12668ba2e626SGreg Roach     * @return void
1267a6f13a4aSGreg Roach     */
1268b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1269c1010edaSGreg Roach    {
1270a6f13a4aSGreg Roach        $this->process_repeats++;
1271a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1272a6f13a4aSGreg Roach            return;
1273a6f13a4aSGreg Roach        }
1274a6f13a4aSGreg Roach
12759b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
127613abd6f3SGreg Roach        $this->repeats         = [];
1277e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1278a6f13a4aSGreg Roach
12797a6ee1acSGreg Roach        $id    = '';
128013abd6f3SGreg Roach        $match = [];
12817a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1282a6f13a4aSGreg Roach            $id = $match[1];
1283a6f13a4aSGreg Roach        }
12847a6ee1acSGreg Roach        $tag = '';
1285a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1286a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1287a6f13a4aSGreg Roach        }
12887a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1289d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1290a6f13a4aSGreg Roach        }
1291a6f13a4aSGreg Roach
1292299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1293a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1294820b62dfSGreg Roach            $facts = $record->facts([], true);
129513abd6f3SGreg Roach            $this->repeats = [];
1296a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1297195b5e75SGreg Roach            foreach ($facts as $fact) {
1298e364afe4SGreg Roach                if (!in_array($fact->getTag(), $nonfacts, true)) {
1299195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1300a6f13a4aSGreg Roach                }
1301a6f13a4aSGreg Roach            }
1302a6f13a4aSGreg Roach        } else {
130330158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1304195b5e75SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1305138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1306a6f13a4aSGreg Roach                }
1307a6f13a4aSGreg Roach            }
1308a6f13a4aSGreg Roach        }
1309a6f13a4aSGreg Roach    }
1310a6f13a4aSGreg Roach
1311a6f13a4aSGreg Roach    /**
131276692c8bSGreg Roach     * XML </Facts>
13138ba2e626SGreg Roach     *
13148ba2e626SGreg Roach     * @return void
1315a6f13a4aSGreg Roach     */
1316b702978eSGreg Roach    protected function factsEndHandler(): void
1317c1010edaSGreg Roach    {
1318a6f13a4aSGreg Roach        $this->process_repeats--;
1319a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1320a6f13a4aSGreg Roach            return;
1321a6f13a4aSGreg Roach        }
1322a6f13a4aSGreg Roach
1323a6f13a4aSGreg Roach        // Check if there is anything to repeat
1324a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1325e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1326a6f13a4aSGreg Roach            $lineoffset = 0;
1327a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1328a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1329a6f13a4aSGreg Roach            }
1330a6f13a4aSGreg Roach
1331a6f13a4aSGreg Roach            //-- read the xml from the file
1332299d100dSGreg Roach            $lines = file($this->report);
1333a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1334a6f13a4aSGreg Roach                $lineoffset--;
1335a6f13a4aSGreg Roach            }
1336a6f13a4aSGreg Roach            $lineoffset++;
1337a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1338a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1339a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1340a6f13a4aSGreg Roach            while ($line_nr < $i) {
1341a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1342a6f13a4aSGreg Roach                $line_nr++;
1343a6f13a4aSGreg Roach            }
1344a6f13a4aSGreg Roach            // No need to drag this
1345a6f13a4aSGreg Roach            unset($lines);
1346a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1347a6f13a4aSGreg Roach            // Save original values
13489b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1349a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1350a6f13a4aSGreg Roach            $count                = count($this->repeats);
1351a6f13a4aSGreg Roach            $i                    = 0;
1352a6f13a4aSGreg Roach            while ($i < $count) {
1353a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1354a6f13a4aSGreg Roach                $this->fact   = '';
1355a6f13a4aSGreg Roach                $this->desc   = '';
1356a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1357a6f13a4aSGreg Roach                    $this->fact = $match[1];
1358a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
135913abd6f3SGreg Roach                        $tmatch = [];
1360a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1361a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1362a6f13a4aSGreg Roach                        } else {
1363a6f13a4aSGreg Roach                            $this->type = ' ';
1364a6f13a4aSGreg Roach                        }
1365a6f13a4aSGreg Roach                    }
1366a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13673d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1368a6f13a4aSGreg Roach                }
1369a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1370e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1371a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13721aa04befSGreg Roach
13731aa04befSGreg Roach                xml_set_element_handler(
13741aa04befSGreg Roach                    $repeat_parser,
13759d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13761aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13771aa04befSGreg Roach                    },
13789d454b6bSGreg Roach                    function ($parser, string $name): void {
13791aa04befSGreg Roach                        $this->endElement($parser, $name);
13801aa04befSGreg Roach                    }
13811aa04befSGreg Roach                );
13821aa04befSGreg Roach
13831aa04befSGreg Roach                xml_set_character_data_handler(
13841aa04befSGreg Roach                    $repeat_parser,
13859d454b6bSGreg Roach                    function ($parser, string $data): void {
13861aa04befSGreg Roach                        $this->characterData($parser, $data);
13871aa04befSGreg Roach                    }
13881aa04befSGreg Roach                );
13891aa04befSGreg Roach
1390a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
13916ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1392a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1393a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1394a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1395a6f13a4aSGreg Roach                    ));
1396a6f13a4aSGreg Roach                }
1397a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1398a6f13a4aSGreg Roach                $i++;
1399a6f13a4aSGreg Roach            }
1400a6f13a4aSGreg Roach            // Restore original values
1401e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1402a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1403a6f13a4aSGreg Roach        }
140465e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1405a6f13a4aSGreg Roach    }
1406a6f13a4aSGreg Roach
1407a6f13a4aSGreg Roach    /**
1408a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1409d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1410a6f13a4aSGreg Roach     *
1411c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14128ba2e626SGreg Roach     *
14138ba2e626SGreg Roach     * @return void
1414a6f13a4aSGreg Roach     */
1415b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1416c1010edaSGreg Roach    {
1417a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14186ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1419a6f13a4aSGreg Roach        }
1420a6f13a4aSGreg Roach
1421a6f13a4aSGreg Roach        $name  = $attrs['name'];
1422a6f13a4aSGreg Roach        $value = $attrs['value'];
142313abd6f3SGreg Roach        $match = [];
1424a6f13a4aSGreg Roach        // Current GEDCOM record strings
1425044416d2SGreg Roach        if ($value === '@ID') {
14267a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1427a6f13a4aSGreg Roach                $value = $match[1];
1428a6f13a4aSGreg Roach            }
1429044416d2SGreg Roach        } elseif ($value === '@fact') {
1430a6f13a4aSGreg Roach            $value = $this->fact;
1431044416d2SGreg Roach        } elseif ($value === '@desc') {
1432a6f13a4aSGreg Roach            $value = $this->desc;
1433044416d2SGreg Roach        } elseif ($value === '@generation') {
1434589feda3SGreg Roach            $value = (string) $this->generation;
1435a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
143613abd6f3SGreg Roach            $gmatch = [];
1437a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14387a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1439a6f13a4aSGreg Roach            }
1440a6f13a4aSGreg Roach        }
1441a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1442d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1443a6f13a4aSGreg Roach        }
1444a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1445a6f13a4aSGreg Roach        $i     = 0;
1446a6f13a4aSGreg Roach        while ($i < $count) {
1447d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14487a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1449a6f13a4aSGreg Roach            $i++;
1450a6f13a4aSGreg Roach        }
1451a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1452da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1453a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1454a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1455a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1456a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1457a6f13a4aSGreg Roach        }
145852868398SGreg Roach
1459a6f13a4aSGreg Roach        // Arithmetic functions
14603cfcc809SGreg Roach        if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) {
146152868398SGreg Roach            // Create an expression language with the functions used by our reports.
146252868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1463c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1464c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
146552868398SGreg Roach
146652868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1467a6f13a4aSGreg Roach        }
146852868398SGreg Roach
14697a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14707a6ee1acSGreg Roach            $value = '';
1471a6f13a4aSGreg Roach        }
1472d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1473a6f13a4aSGreg Roach    }
1474a6f13a4aSGreg Roach
1475a6f13a4aSGreg Roach    /**
1476a6f13a4aSGreg Roach     * XML <if > start element
1477a6f13a4aSGreg Roach     *
1478c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14798ba2e626SGreg Roach     *
14808ba2e626SGreg Roach     * @return void
1481a6f13a4aSGreg Roach     */
1482b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1483c1010edaSGreg Roach    {
1484a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1485a6f13a4aSGreg Roach            $this->process_ifs++;
1486a6f13a4aSGreg Roach
1487a6f13a4aSGreg Roach            return;
1488a6f13a4aSGreg Roach        }
1489a6f13a4aSGreg Roach
1490a6f13a4aSGreg Roach        $condition = $attrs['condition'];
149182759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1492c1010edaSGreg Roach        $condition = str_replace([
1493c1010edaSGreg Roach            ' LT ',
1494c1010edaSGreg Roach            ' GT ',
1495c1010edaSGreg Roach        ], [
1496c1010edaSGreg Roach            '<',
1497c1010edaSGreg Roach            '>',
1498c1010edaSGreg Roach        ], $condition);
14993cfcc809SGreg Roach        // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15007a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
150113abd6f3SGreg Roach        $match     = [];
15023cfcc809SGreg Roach        $count     = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER);
1503a6f13a4aSGreg Roach        $i         = 0;
1504a6f13a4aSGreg Roach        while ($i < $count) {
1505a6f13a4aSGreg Roach            $id    = $match[$i][1];
1506a6f13a4aSGreg Roach            $value = '""';
1507044416d2SGreg Roach            if ($id === 'ID') {
15087a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1509a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1510a6f13a4aSGreg Roach                }
15117a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1512a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15137a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1514a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15157a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1516a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1517a6f13a4aSGreg Roach            } else {
1518b2448a1bSGreg Roach                $level = (int) explode(' ', trim($this->gedrec))[0];
1519b2448a1bSGreg Roach                if ($level === 0) {
1520a6f13a4aSGreg Roach                    $level++;
1521a6f13a4aSGreg Roach                }
15223d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1523a6f13a4aSGreg Roach                if (empty($value)) {
1524a6f13a4aSGreg Roach                    $level++;
15253d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1526a6f13a4aSGreg Roach                }
15278d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15285e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1529a6f13a4aSGreg Roach            }
1530a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1531a6f13a4aSGreg Roach            $i++;
1532a6f13a4aSGreg Roach        }
15335809450fSGreg Roach
1534cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1535cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1536c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1537c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1538cb63a60eSGreg Roach
1539cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15405809450fSGreg Roach
1541a6f13a4aSGreg Roach        if (!$ret) {
1542a6f13a4aSGreg Roach            $this->process_ifs++;
1543a6f13a4aSGreg Roach        }
1544a6f13a4aSGreg Roach    }
1545a6f13a4aSGreg Roach
1546a6f13a4aSGreg Roach    /**
1547a6f13a4aSGreg Roach     * XML <if /> end element
15488ba2e626SGreg Roach     *
15498ba2e626SGreg Roach     * @return void
1550a6f13a4aSGreg Roach     */
1551b702978eSGreg Roach    protected function ifEndHandler(): void
1552c1010edaSGreg Roach    {
1553a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1554a6f13a4aSGreg Roach            $this->process_ifs--;
1555a6f13a4aSGreg Roach        }
1556a6f13a4aSGreg Roach    }
1557a6f13a4aSGreg Roach
1558a6f13a4aSGreg Roach    /**
1559a6f13a4aSGreg Roach     * XML <Footnote > start element
1560a6f13a4aSGreg Roach     * Collect the Footnote links
1561a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1562a6f13a4aSGreg Roach     *
1563c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15648ba2e626SGreg Roach     *
15658ba2e626SGreg Roach     * @return void
1566a6f13a4aSGreg Roach     */
1567b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1568c1010edaSGreg Roach    {
15697a6ee1acSGreg Roach        $id = '';
15707a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1571a6f13a4aSGreg Roach            $id = $match[2];
1572a6f13a4aSGreg Roach        }
1573299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1574a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15759b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1576a6f13a4aSGreg Roach            $this->print_data         = true;
15777a6ee1acSGreg Roach            $style                    = '';
1578a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1579a6f13a4aSGreg Roach                $style = $attrs['style'];
1580a6f13a4aSGreg Roach            }
1581a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1582e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1583a6f13a4aSGreg Roach        } else {
1584a6f13a4aSGreg Roach            $this->print_data       = false;
1585a6f13a4aSGreg Roach            $this->process_footnote = false;
1586a6f13a4aSGreg Roach        }
1587a6f13a4aSGreg Roach    }
1588a6f13a4aSGreg Roach
1589a6f13a4aSGreg Roach    /**
1590a6f13a4aSGreg Roach     * XML <Footnote /> end element
1591a6f13a4aSGreg Roach     * Print the collected Footnote data
15928ba2e626SGreg Roach     *
15938ba2e626SGreg Roach     * @return void
1594a6f13a4aSGreg Roach     */
1595b702978eSGreg Roach    protected function footnoteEndHandler(): void
1596c1010edaSGreg Roach    {
1597a6f13a4aSGreg Roach        if ($this->process_footnote) {
1598a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1599a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1600a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1601e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1602a6f13a4aSGreg Roach            }
1603a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1604a6f13a4aSGreg Roach        } else {
1605a6f13a4aSGreg Roach            $this->process_footnote = true;
1606a6f13a4aSGreg Roach        }
1607a6f13a4aSGreg Roach    }
1608a6f13a4aSGreg Roach
1609a6f13a4aSGreg Roach    /**
1610a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16118ba2e626SGreg Roach     *
16128ba2e626SGreg Roach     * @return void
1613a6f13a4aSGreg Roach     */
1614b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1615c1010edaSGreg Roach    {
16167a6ee1acSGreg Roach        $temp = 'footnotetexts';
1617e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1618a6f13a4aSGreg Roach    }
1619a6f13a4aSGreg Roach
1620a6f13a4aSGreg Roach    /**
1621a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16228ba2e626SGreg Roach     *
16238ba2e626SGreg Roach     * @return void
1624a6f13a4aSGreg Roach     */
1625b702978eSGreg Roach    protected function brStartHandler(): void
1626c1010edaSGreg Roach    {
1627a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1628a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1629a6f13a4aSGreg Roach        }
1630a6f13a4aSGreg Roach    }
1631a6f13a4aSGreg Roach
1632a6f13a4aSGreg Roach    /**
1633a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16348ba2e626SGreg Roach     *
16358ba2e626SGreg Roach     * @return void
1636a6f13a4aSGreg Roach     */
1637b702978eSGreg Roach    protected function spStartHandler(): void
1638c1010edaSGreg Roach    {
1639a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1640a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1641a6f13a4aSGreg Roach        }
1642a6f13a4aSGreg Roach    }
1643a6f13a4aSGreg Roach
1644a6f13a4aSGreg Roach    /**
164576692c8bSGreg Roach     * XML <HighlightedImage/>
164676692c8bSGreg Roach     *
1647c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16488ba2e626SGreg Roach     *
16498ba2e626SGreg Roach     * @return void
1650a6f13a4aSGreg Roach     */
1651b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1652c1010edaSGreg Roach    {
1653a6f13a4aSGreg Roach        $id = '';
16547a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1655a6f13a4aSGreg Roach            $id = $match[1];
1656a6f13a4aSGreg Roach        }
1657a6f13a4aSGreg Roach
1658c21bdddcSGreg Roach        // Position the top corner of this box on the page
1659c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1660a6f13a4aSGreg Roach
1661c21bdddcSGreg Roach        // Position the left corner of this box on the page
1662c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1663a6f13a4aSGreg Roach
166483cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
166583cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1666a6f13a4aSGreg Roach
1667a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
166883cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1669a6f13a4aSGreg Roach
167083cdc021SGreg Roach        // Width, height (or both).
1671c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1672c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1673a6f13a4aSGreg Roach
1674299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16754a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
167686a63f51SGreg Roach
1677*a04bb9a2SGreg Roach        if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1678*a04bb9a2SGreg Roach            $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
167929518ad2SGreg Roach            $attributes = [(int) imagesx($image), (int) imagesy($image)];
168029518ad2SGreg Roach
1681a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16823c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16833c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1684a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16853c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16863c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1687a6f13a4aSGreg Roach            } else {
16883c3b90deSGreg Roach                $width  = $attributes[0];
16893c3b90deSGreg Roach                $height = $attributes[1];
1690a6f13a4aSGreg Roach            }
1691*a04bb9a2SGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1692e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1693a6f13a4aSGreg Roach        }
1694a6f13a4aSGreg Roach    }
1695a6f13a4aSGreg Roach
1696a6f13a4aSGreg Roach    /**
169776692c8bSGreg Roach     * XML <Image/>
169876692c8bSGreg Roach     *
1699c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17008ba2e626SGreg Roach     *
17018ba2e626SGreg Roach     * @return void
1702a6f13a4aSGreg Roach     */
1703b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1704c1010edaSGreg Roach    {
170583cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1706c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1707a6f13a4aSGreg Roach
1708a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1709c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1710a6f13a4aSGreg Roach
171183cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
171283cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1713a6f13a4aSGreg Roach
1714a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
171583cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1716a6f13a4aSGreg Roach
171783cdc021SGreg Roach        // Width, height (or both).
1718c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1719c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1720a6f13a4aSGreg Roach
172183cdc021SGreg Roach        $file = $attrs['file'] ?? '';
172283cdc021SGreg Roach
1723044416d2SGreg Roach        if ($file === '@FILE') {
172413abd6f3SGreg Roach            $match = [];
1725a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1726299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17274a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1728cdf416fbSGreg Roach
1729*a04bb9a2SGreg Roach                if ($media_file instanceof MediaFile && $media_file->fileExists($this->data_filesystem)) {
1730*a04bb9a2SGreg Roach                    $image      = imagecreatefromstring($media_file->fileContents($this->data_filesystem));
173129518ad2SGreg Roach                    $attributes = [(int) imagesx($image), (int) imagesy($image)];
173229518ad2SGreg Roach
1733a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17343c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17353c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1736a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17373c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17383c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1739a6f13a4aSGreg Roach                    } else {
17403c3b90deSGreg Roach                        $width  = $attributes[0];
17413c3b90deSGreg Roach                        $height = $attributes[1];
1742a6f13a4aSGreg Roach                    }
1743*a04bb9a2SGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln, $this->data_filesystem);
1744e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1745a6f13a4aSGreg Roach                }
1746a6f13a4aSGreg Roach            }
1747a6f13a4aSGreg Roach        } else {
17487a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1749a6f13a4aSGreg Roach                $size = getimagesize($file);
1750a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1751a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1752a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1753a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1754a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1755a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1756a6f13a4aSGreg Roach                } else {
1757a6f13a4aSGreg Roach                    $width  = $size[0];
1758a6f13a4aSGreg Roach                    $height = $size[1];
1759a6f13a4aSGreg Roach                }
1760e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1761e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1762a6f13a4aSGreg Roach            }
1763a6f13a4aSGreg Roach        }
1764a6f13a4aSGreg Roach    }
1765a6f13a4aSGreg Roach
1766a6f13a4aSGreg Roach    /**
1767a6f13a4aSGreg Roach     * XML <Line> element handler
1768a6f13a4aSGreg Roach     *
1769c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17708ba2e626SGreg Roach     *
17718ba2e626SGreg Roach     * @return void
1772a6f13a4aSGreg Roach     */
1773b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1774c1010edaSGreg Roach    {
1775a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1776c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1777a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17787a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1779a6f13a4aSGreg Roach                $x1 = 0;
17807a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1781c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1782a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1783c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1784a6f13a4aSGreg Roach            }
1785a6f13a4aSGreg Roach        }
1786a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1787c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1788a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17897a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1790a6f13a4aSGreg Roach                $y1 = 0;
17917a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1792c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1793a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1794c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1795a6f13a4aSGreg Roach            }
1796a6f13a4aSGreg Roach        }
1797a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1798c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1799a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18007a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1801a6f13a4aSGreg Roach                $x2 = 0;
18027a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1803c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1804a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1805c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1806a6f13a4aSGreg Roach            }
1807a6f13a4aSGreg Roach        }
1808a6f13a4aSGreg Roach        // End vertical position
1809c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1810a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18117a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1812a6f13a4aSGreg Roach                $y2 = 0;
18137a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1814c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1815a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1816c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1817a6f13a4aSGreg Roach            }
1818a6f13a4aSGreg Roach        }
1819a6f13a4aSGreg Roach
1820e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1821e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1822a6f13a4aSGreg Roach    }
1823a6f13a4aSGreg Roach
1824a6f13a4aSGreg Roach    /**
182576692c8bSGreg Roach     * XML <List>
1826a6f13a4aSGreg Roach     *
1827c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18288ba2e626SGreg Roach     *
18298ba2e626SGreg Roach     * @return void
1830a6f13a4aSGreg Roach     */
1831b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1832c1010edaSGreg Roach    {
1833a6f13a4aSGreg Roach        $this->process_repeats++;
1834a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1835a6f13a4aSGreg Roach            return;
1836a6f13a4aSGreg Roach        }
1837a6f13a4aSGreg Roach
183813abd6f3SGreg Roach        $match = [];
1839a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1840a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1841a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1842d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1843a6f13a4aSGreg Roach                $sortby = trim($sortby);
1844a6f13a4aSGreg Roach            }
1845a6f13a4aSGreg Roach        } else {
18467a6ee1acSGreg Roach            $sortby = 'NAME';
1847a6f13a4aSGreg Roach        }
1848a6f13a4aSGreg Roach
1849e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1850195b5e75SGreg Roach
1851a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1852a6f13a4aSGreg Roach        switch ($listname) {
18537a6ee1acSGreg Roach            case 'pending':
18546c0bef3aSGreg Roach                $xrefs = DB::table('change')
1855195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1856a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1857195b5e75SGreg Roach                            ->from('change')
1858195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1859195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18607f5c2944SGreg Roach                            ->groupBy(['xref']);
1861195b5e75SGreg Roach                    })
18626c0bef3aSGreg Roach                    ->pluck('xref');
1863195b5e75SGreg Roach
186413abd6f3SGreg Roach                $this->list = [];
18656c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18666c0bef3aSGreg Roach                    $this->list[] = GedcomRecord::getInstance($xref, $this->tree);
1867a6f13a4aSGreg Roach                }
1868a6f13a4aSGreg Roach                break;
1869a6f13a4aSGreg Roach            case 'individual':
18705985adfbSGreg Roach                $query = DB::table('individuals')
18715985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18725985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18735985adfbSGreg Roach                    ->distinct();
18745985adfbSGreg Roach
1875a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1876a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
187782759250SGreg Roach                        $value = $this->substituteVars($value, false);
1878a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1879a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
18800b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
18815985adfbSGreg Roach                                $join
18825985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
18835985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
18845985adfbSGreg Roach                            });
18855985adfbSGreg Roach
18865985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
18875985adfbSGreg Roach
1888a6f13a4aSGreg Roach                            $date = new Date($match[3]);
18895985adfbSGreg Roach
1890044416d2SGreg Roach                            if ($match[2] === 'LTE') {
18915985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1892a6f13a4aSGreg Roach                            } else {
18935985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1894a6f13a4aSGreg Roach                            }
18955985adfbSGreg Roach
18965985adfbSGreg Roach                            // This filter has been fully processed
18975985adfbSGreg Roach                            unset($attrs[$attr]);
18987ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
18990b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19005985adfbSGreg Roach                                $join
19015985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19025985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19035985adfbSGreg Roach                            });
1904a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19057a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19065d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1907fbbe964bSGreg Roach                                $query->whereContains($attr . '.n_full', $name);
1908a6f13a4aSGreg Roach                            }
19095985adfbSGreg Roach
19105985adfbSGreg Roach                            // This filter has been fully processed
19115985adfbSGreg Roach                            unset($attrs[$attr]);
1912a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19135985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1914a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19155985adfbSGreg Roach
1916a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19175985adfbSGreg Roach
19185985adfbSGreg Roach                            // This filter has been fully processed
19195985adfbSGreg Roach                            unset($attrs[$attr]);
19202fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19219dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19225985adfbSGreg Roach                            $query
19230b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19245985adfbSGreg Roach                                    $join
19257ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19267ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19275985adfbSGreg Roach                                })
19280b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19297ee4bfadSGreg Roach                                    $join
19307ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19317ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19327ee4bfadSGreg Roach                                })
19337ee4bfadSGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
19346e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19355985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19367ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19376e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19386e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
19396e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
19406e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19416e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1942e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19437ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1944a6f13a4aSGreg Roach                        }
1945a6f13a4aSGreg Roach                    }
1946a6f13a4aSGreg Roach                }
1947a6f13a4aSGreg Roach
194813abd6f3SGreg Roach                $this->list = [];
1949a6f13a4aSGreg Roach
19505985adfbSGreg Roach                foreach ($query->get() as $row) {
1951299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1952a6f13a4aSGreg Roach                }
1953a6f13a4aSGreg Roach                break;
1954a6f13a4aSGreg Roach
1955a6f13a4aSGreg Roach            case 'family':
195630fc2b1eSGreg Roach                $query = DB::table('families')
195730fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
195830fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
195930fc2b1eSGreg Roach                    ->distinct();
196030fc2b1eSGreg Roach
1961a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1962a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
196382759250SGreg Roach                        $value = $this->substituteVars($value, false);
1964a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1965a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19660b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
196730fc2b1eSGreg Roach                                $join
196830fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
196930fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
197030fc2b1eSGreg Roach                            });
197130fc2b1eSGreg Roach
197230fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
197330fc2b1eSGreg Roach
1974a6f13a4aSGreg Roach                            $date = new Date($match[3]);
197530fc2b1eSGreg Roach
1976044416d2SGreg Roach                            if ($match[2] === 'LTE') {
197730fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1978a6f13a4aSGreg Roach                            } else {
197930fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1980a6f13a4aSGreg Roach                            }
198130fc2b1eSGreg Roach
198230fc2b1eSGreg Roach                            // This filter has been fully processed
198330fc2b1eSGreg Roach                            unset($attrs[$attr]);
1984a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
198530fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
1986a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
198730fc2b1eSGreg Roach
1988a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
198930fc2b1eSGreg Roach
199030fc2b1eSGreg Roach                            // This filter has been fully processed
199130fc2b1eSGreg Roach                            unset($attrs[$attr]);
199230fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
19933b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
19940b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
199530fc2b1eSGreg Roach                                    $join
199630fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
19973b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
199830fc2b1eSGreg Roach                                            $query
199930fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
200030fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
200130fc2b1eSGreg Roach                                        });
200230fc2b1eSGreg Roach                                });
20035d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20047a6ee1acSGreg Roach                                if ($match[1] != '') {
20057a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20065d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2007fbbe964bSGreg Roach                                        $query->whereContains($attr . '.n_full', $name);
20085d0bc43dSGreg Roach                                    }
20095d0bc43dSGreg Roach                                }
2010a6f13a4aSGreg Roach                            }
201130fc2b1eSGreg Roach
201230fc2b1eSGreg Roach                            // This filter has been fully processed
201330fc2b1eSGreg Roach                            unset($attrs[$attr]);
20146e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20159dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
201630fc2b1eSGreg Roach                            $query
20170b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
201830fc2b1eSGreg Roach                                    $join
20196e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20206e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
202130fc2b1eSGreg Roach                                })
20220b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20236e5c0963SGreg Roach                                    $join
20246e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20256e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20266e5c0963SGreg Roach                                })
20276e5c0963SGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
20286e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
202930fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20306e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20316e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20326e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20336e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20346e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20356e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2036e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20376e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2038a6f13a4aSGreg Roach                        }
2039a6f13a4aSGreg Roach                    }
2040a6f13a4aSGreg Roach                }
2041a6f13a4aSGreg Roach
204213abd6f3SGreg Roach                $this->list = [];
2043a6f13a4aSGreg Roach
204430fc2b1eSGreg Roach                foreach ($query->get() as $row) {
2045299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2046a6f13a4aSGreg Roach                }
2047a6f13a4aSGreg Roach                break;
2048a6f13a4aSGreg Roach
2049a6f13a4aSGreg Roach            default:
20506ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2051a6f13a4aSGreg Roach        }
2052a6f13a4aSGreg Roach
205313abd6f3SGreg Roach        $filters  = [];
205413abd6f3SGreg Roach        $filters2 = [];
2055a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2056a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2057a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2058a6f13a4aSGreg Roach                    $condition = $value;
2059a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2060a6f13a4aSGreg Roach                        $id    = $match[1];
2061a6f13a4aSGreg Roach                        $value = "''";
2062044416d2SGreg Roach                        if ($id === 'ID') {
20637a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2064a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2065a6f13a4aSGreg Roach                            }
2066044416d2SGreg Roach                        } elseif ($id === 'fact') {
2067a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2068044416d2SGreg Roach                        } elseif ($id === 'desc') {
2069a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2070a6f13a4aSGreg Roach                        } else {
2071a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20727a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2073a6f13a4aSGreg Roach                            }
2074a6f13a4aSGreg Roach                        }
2075a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2076a6f13a4aSGreg Roach                    }
2077a6f13a4aSGreg Roach                    //-- handle regular expressions
2078a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2079a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2080a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2081a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2082a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2083d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2084a6f13a4aSGreg Roach                            $val = trim($val);
2085a6f13a4aSGreg Roach                        }
2086a6f13a4aSGreg Roach                        if ($val) {
20877a6ee1acSGreg Roach                            $searchstr = '';
20887a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2089a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2090a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2091a6f13a4aSGreg Roach                                $level = 1;
2092a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2093a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2094a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2095a6f13a4aSGreg Roach                                    }
2096a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2097044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
20987a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2099a6f13a4aSGreg Roach                                    }
21007a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2101a6f13a4aSGreg Roach                                    $level++;
2102a6f13a4aSGreg Roach                                }
2103a6f13a4aSGreg Roach                            } else {
2104044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21057a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2106a6f13a4aSGreg Roach                                }
2107a6f13a4aSGreg Roach                                $t         = $tag;
21087a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2109a6f13a4aSGreg Roach                            }
2110a6f13a4aSGreg Roach                            switch ($expr) {
21117a6ee1acSGreg Roach                                case 'CONTAINS':
2112044416d2SGreg Roach                                    if ($t === 'PLAC') {
2113a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2114a6f13a4aSGreg Roach                                    } else {
2115a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2116a6f13a4aSGreg Roach                                    }
2117a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2118a6f13a4aSGreg Roach                                    break;
2119a6f13a4aSGreg Roach                                default:
2120c1010edaSGreg Roach                                    $filters2[] = [
2121c1010edaSGreg Roach                                        'tag'  => $tag,
2122c1010edaSGreg Roach                                        'expr' => $expr,
2123c1010edaSGreg Roach                                        'val'  => $val,
2124c1010edaSGreg Roach                                    ];
2125a6f13a4aSGreg Roach                                    break;
2126a6f13a4aSGreg Roach                            }
2127a6f13a4aSGreg Roach                        }
2128a6f13a4aSGreg Roach                    }
2129a6f13a4aSGreg Roach                }
2130a6f13a4aSGreg Roach            }
2131a6f13a4aSGreg Roach        }
2132a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2133a6f13a4aSGreg Roach        if ($filters) {
2134a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2135a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2136299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2137a6f13a4aSGreg Roach                        unset($this->list[$key]);
2138a6f13a4aSGreg Roach                        break;
2139a6f13a4aSGreg Roach                    }
2140a6f13a4aSGreg Roach                }
2141a6f13a4aSGreg Roach            }
2142a6f13a4aSGreg Roach        }
2143a6f13a4aSGreg Roach        if ($filters2) {
214413abd6f3SGreg Roach            $mylist = [];
2145a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2146c0935879SGreg Roach                $key  = $indi->xref();
2147299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2148a6f13a4aSGreg Roach                $keep = true;
2149a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2150a6f13a4aSGreg Roach                    if ($keep) {
2151a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2152a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2153a6f13a4aSGreg Roach                        $val  = $filter['val'];
2154b2448a1bSGreg Roach                        if ($val === "''") {
21557a6ee1acSGreg Roach                            $val = '';
2156a6f13a4aSGreg Roach                        }
21577a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2158a6f13a4aSGreg Roach                        $t    = end($tags);
21593d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2160a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2161044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21627a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21637a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2164a6f13a4aSGreg Roach                            $t    = end($tags);
21653d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2166a6f13a4aSGreg Roach                        }
2167a6f13a4aSGreg Roach
2168a6f13a4aSGreg Roach                        switch ($expr) {
21697a6ee1acSGreg Roach                            case 'GTE':
2170044416d2SGreg Roach                                if ($t === 'DATE') {
2171a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2172a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2173a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2174a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2175a6f13a4aSGreg Roach                                    $keep = true;
2176a6f13a4aSGreg Roach                                }
2177a6f13a4aSGreg Roach                                break;
21787a6ee1acSGreg Roach                            case 'LTE':
2179044416d2SGreg Roach                                if ($t === 'DATE') {
2180a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2181a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2182a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2183a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2184a6f13a4aSGreg Roach                                    $keep = true;
2185a6f13a4aSGreg Roach                                }
2186a6f13a4aSGreg Roach                                break;
2187a6f13a4aSGreg Roach                            default:
2188a6f13a4aSGreg Roach                                if ($v == $val) {
2189a6f13a4aSGreg Roach                                    $keep = true;
2190a6f13a4aSGreg Roach                                } else {
2191a6f13a4aSGreg Roach                                    $keep = false;
2192a6f13a4aSGreg Roach                                }
2193a6f13a4aSGreg Roach                                break;
2194a6f13a4aSGreg Roach                        }
2195a6f13a4aSGreg Roach                    }
2196a6f13a4aSGreg Roach                }
2197a6f13a4aSGreg Roach                if ($keep) {
2198a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2199a6f13a4aSGreg Roach                }
2200a6f13a4aSGreg Roach            }
2201a6f13a4aSGreg Roach            $this->list = $mylist;
2202a6f13a4aSGreg Roach        }
2203a6f13a4aSGreg Roach
2204a6f13a4aSGreg Roach        switch ($sortby) {
2205a6f13a4aSGreg Roach            case 'NAME':
2206c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2207a6f13a4aSGreg Roach                break;
2208a6f13a4aSGreg Roach            case 'CHAN':
2209c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2210a6f13a4aSGreg Roach                break;
2211a6f13a4aSGreg Roach            case 'BIRT:DATE':
2212c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2213a6f13a4aSGreg Roach                break;
2214a6f13a4aSGreg Roach            case 'DEAT:DATE':
2215c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2216a6f13a4aSGreg Roach                break;
2217a6f13a4aSGreg Roach            case 'MARR:DATE':
2218c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2219a6f13a4aSGreg Roach                break;
2220a6f13a4aSGreg Roach            default:
2221a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2222a6f13a4aSGreg Roach                break;
2223a6f13a4aSGreg Roach        }
2224a6f13a4aSGreg Roach
22259b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2226e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2227a6f13a4aSGreg Roach    }
2228a6f13a4aSGreg Roach
2229a6f13a4aSGreg Roach    /**
223076692c8bSGreg Roach     * XML <List>
22318ba2e626SGreg Roach     *
22328ba2e626SGreg Roach     * @return void
2233a6f13a4aSGreg Roach     */
2234b702978eSGreg Roach    protected function listEndHandler(): void
2235c1010edaSGreg Roach    {
2236a6f13a4aSGreg Roach        $this->process_repeats--;
2237a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2238a6f13a4aSGreg Roach            return;
2239a6f13a4aSGreg Roach        }
2240a6f13a4aSGreg Roach
2241a6f13a4aSGreg Roach        // Check if there is any list
2242a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2243a6f13a4aSGreg Roach            $lineoffset = 0;
2244a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2245a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2246a6f13a4aSGreg Roach            }
2247a6f13a4aSGreg Roach            //-- read the xml from the file
2248299d100dSGreg Roach            $lines = file($this->report);
22497a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2250a6f13a4aSGreg Roach                $lineoffset--;
2251a6f13a4aSGreg Roach            }
2252a6f13a4aSGreg Roach            $lineoffset++;
2253a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2254a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2255a6f13a4aSGreg Roach            // List Level counter
2256a6f13a4aSGreg Roach            $count = 1;
2257a6f13a4aSGreg Roach            while (0 < $count) {
22587a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2259a6f13a4aSGreg Roach                    $count++;
22607a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2261a6f13a4aSGreg Roach                    $count--;
2262a6f13a4aSGreg Roach                }
2263a6f13a4aSGreg Roach                if (0 < $count) {
2264a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2265a6f13a4aSGreg Roach                }
2266a6f13a4aSGreg Roach                $line_nr++;
2267a6f13a4aSGreg Roach            }
2268a6f13a4aSGreg Roach            // No need to drag this
2269a6f13a4aSGreg Roach            unset($lines);
22707a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2271a6f13a4aSGreg Roach            // Save original values
22729b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2273a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2274a6f13a4aSGreg Roach
2275a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2276a6f13a4aSGreg Roach            $this->list_private = 0;
2277a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2278a6f13a4aSGreg Roach                if ($record->canShow()) {
2279f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2280a6f13a4aSGreg Roach                    //-- start the sax parser
2281a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2282e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2283a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22841aa04befSGreg Roach
22851aa04befSGreg Roach                    xml_set_element_handler(
22861aa04befSGreg Roach                        $repeat_parser,
22879d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
22881aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
22891aa04befSGreg Roach                        },
22909d454b6bSGreg Roach                        function ($parser, string $name): void {
22911aa04befSGreg Roach                            $this->endElement($parser, $name);
22921aa04befSGreg Roach                        }
22931aa04befSGreg Roach                    );
22941aa04befSGreg Roach
22951aa04befSGreg Roach                    xml_set_character_data_handler(
22961aa04befSGreg Roach                        $repeat_parser,
22979d454b6bSGreg Roach                        function ($parser, string $data): void {
22981aa04befSGreg Roach                            $this->characterData($parser, $data);
22991aa04befSGreg Roach                        }
23001aa04befSGreg Roach                    );
23011aa04befSGreg Roach
2302a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23036ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2304a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2305a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2306a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2307a6f13a4aSGreg Roach                        ));
2308a6f13a4aSGreg Roach                    }
2309a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2310a6f13a4aSGreg Roach                } else {
2311a6f13a4aSGreg Roach                    $this->list_private++;
2312a6f13a4aSGreg Roach                }
2313a6f13a4aSGreg Roach            }
231413abd6f3SGreg Roach            $this->list   = [];
2315e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2316a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2317a6f13a4aSGreg Roach        }
231865e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2319a6f13a4aSGreg Roach    }
2320a6f13a4aSGreg Roach
2321a6f13a4aSGreg Roach    /**
2322a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2323a6f13a4aSGreg Roach     * Prints the total number of records in a list
2324a6f13a4aSGreg Roach     * The total number is collected from
2325a6f13a4aSGreg Roach     * List and Relatives
23268ba2e626SGreg Roach     *
23278ba2e626SGreg Roach     * @return void
2328a6f13a4aSGreg Roach     */
2329b702978eSGreg Roach    protected function listTotalStartHandler(): void
2330c1010edaSGreg Roach    {
2331a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2332589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2333a6f13a4aSGreg Roach        } else {
23347a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2335a6f13a4aSGreg Roach        }
2336a6f13a4aSGreg Roach    }
2337a6f13a4aSGreg Roach
2338a6f13a4aSGreg Roach    /**
233976692c8bSGreg Roach     * XML <Relatives>
234076692c8bSGreg Roach     *
2341c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23428ba2e626SGreg Roach     *
23438ba2e626SGreg Roach     * @return void
2344a6f13a4aSGreg Roach     */
2345b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2346c1010edaSGreg Roach    {
2347a6f13a4aSGreg Roach        $this->process_repeats++;
2348a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2349a6f13a4aSGreg Roach            return;
2350a6f13a4aSGreg Roach        }
2351a6f13a4aSGreg Roach
2352e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2353e364afe4SGreg Roach
235413abd6f3SGreg Roach        $match = [];
2355a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2356d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2357a6f13a4aSGreg Roach            $sortby = trim($sortby);
2358a6f13a4aSGreg Roach        }
2359a6f13a4aSGreg Roach
2360a6f13a4aSGreg Roach        $maxgen = -1;
2361a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2362c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2363a6f13a4aSGreg Roach        }
2364a6f13a4aSGreg Roach
2365e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2366e364afe4SGreg Roach
2367a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2368d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2369a6f13a4aSGreg Roach            $group = trim($group);
2370a6f13a4aSGreg Roach        }
2371a6f13a4aSGreg Roach
2372e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2373e364afe4SGreg Roach
2374a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2375d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2376a6f13a4aSGreg Roach            $id = trim($id);
2377a6f13a4aSGreg Roach        }
2378a6f13a4aSGreg Roach
237913abd6f3SGreg Roach        $this->list = [];
2380299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2381d965cc1aSGreg Roach        if ($person instanceof Individual) {
2382a6f13a4aSGreg Roach            $this->list[$id] = $person;
2383a6f13a4aSGreg Roach            switch ($group) {
23847a6ee1acSGreg Roach                case 'child-family':
238539ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2386820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2387820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2388a6f13a4aSGreg Roach                        }
2389820b62dfSGreg Roach
2390820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2391c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2392a6f13a4aSGreg Roach                        }
2393a6f13a4aSGreg Roach                    }
2394a6f13a4aSGreg Roach                    break;
23957a6ee1acSGreg Roach                case 'spouse-family':
239639ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2397820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2398820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2399a6f13a4aSGreg Roach                        }
2400820b62dfSGreg Roach
2401820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2402c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2403a6f13a4aSGreg Roach                        }
2404a6f13a4aSGreg Roach                    }
2405a6f13a4aSGreg Roach                    break;
24067a6ee1acSGreg Roach                case 'direct-ancestors':
24073d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2408a6f13a4aSGreg Roach                    break;
24097a6ee1acSGreg Roach                case 'ancestors':
24103d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2411a6f13a4aSGreg Roach                    break;
24127a6ee1acSGreg Roach                case 'descendants':
2413a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24143d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2415a6f13a4aSGreg Roach                    break;
24167a6ee1acSGreg Roach                case 'all':
24173d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24183d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2419a6f13a4aSGreg Roach                    break;
2420a6f13a4aSGreg Roach            }
2421a6f13a4aSGreg Roach        }
2422a6f13a4aSGreg Roach
2423a6f13a4aSGreg Roach        switch ($sortby) {
2424a6f13a4aSGreg Roach            case 'NAME':
2425c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2426a6f13a4aSGreg Roach                break;
2427a6f13a4aSGreg Roach            case 'BIRT:DATE':
2428c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2429a6f13a4aSGreg Roach                break;
2430a6f13a4aSGreg Roach            case 'DEAT:DATE':
2431c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2432a6f13a4aSGreg Roach                break;
2433a6f13a4aSGreg Roach            case 'generation':
243413abd6f3SGreg Roach                $newarray = [];
2435a6f13a4aSGreg Roach                reset($this->list);
2436a6f13a4aSGreg Roach                $genCounter = 1;
2437a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2438a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2439a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2440a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
244179529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2442a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2443a6f13a4aSGreg Roach                        }
2444a6f13a4aSGreg Roach                    }
2445a6f13a4aSGreg Roach                    $genCounter++;
2446a6f13a4aSGreg Roach                }
2447a6f13a4aSGreg Roach                $this->list = $newarray;
2448a6f13a4aSGreg Roach                break;
2449a6f13a4aSGreg Roach            default:
2450a6f13a4aSGreg Roach                // unsorted
2451a6f13a4aSGreg Roach                break;
2452a6f13a4aSGreg Roach        }
24539b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2454e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2455a6f13a4aSGreg Roach    }
2456a6f13a4aSGreg Roach
2457a6f13a4aSGreg Roach    /**
245876692c8bSGreg Roach     * XML </ Relatives>
24598ba2e626SGreg Roach     *
24608ba2e626SGreg Roach     * @return void
2461a6f13a4aSGreg Roach     */
2462b702978eSGreg Roach    protected function relativesEndHandler(): void
2463c1010edaSGreg Roach    {
2464a6f13a4aSGreg Roach        $this->process_repeats--;
2465a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2466a6f13a4aSGreg Roach            return;
2467a6f13a4aSGreg Roach        }
2468a6f13a4aSGreg Roach
2469a6f13a4aSGreg Roach        // Check if there is any relatives
2470a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2471a6f13a4aSGreg Roach            $lineoffset = 0;
2472a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2473a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2474a6f13a4aSGreg Roach            }
2475a6f13a4aSGreg Roach            //-- read the xml from the file
2476299d100dSGreg Roach            $lines = file($this->report);
24777a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2478a6f13a4aSGreg Roach                $lineoffset--;
2479a6f13a4aSGreg Roach            }
2480a6f13a4aSGreg Roach            $lineoffset++;
2481a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2482a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2483a6f13a4aSGreg Roach            // Relatives Level counter
2484a6f13a4aSGreg Roach            $count = 1;
2485a6f13a4aSGreg Roach            while (0 < $count) {
24867a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2487a6f13a4aSGreg Roach                    $count++;
24887a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2489a6f13a4aSGreg Roach                    $count--;
2490a6f13a4aSGreg Roach                }
2491a6f13a4aSGreg Roach                if (0 < $count) {
2492a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2493a6f13a4aSGreg Roach                }
2494a6f13a4aSGreg Roach                $line_nr++;
2495a6f13a4aSGreg Roach            }
2496a6f13a4aSGreg Roach            // No need to drag this
2497a6f13a4aSGreg Roach            unset($lines);
2498a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2499a6f13a4aSGreg Roach            // Save original values
25009b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2501a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2502a6f13a4aSGreg Roach
2503a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2504a6f13a4aSGreg Roach            $this->list_private = 0;
2505b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2506a6f13a4aSGreg Roach                if (isset($value->generation)) {
2507a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2508a6f13a4aSGreg Roach                }
2509b092a991SGreg Roach                $tmp          = GedcomRecord::getInstance((string) $xref, $this->tree);
2510299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2511a6f13a4aSGreg Roach
2512a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2513e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2514a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25151aa04befSGreg Roach
25161aa04befSGreg Roach                xml_set_element_handler(
25171aa04befSGreg Roach                    $repeat_parser,
25189d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25191aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25201aa04befSGreg Roach                    },
25219d454b6bSGreg Roach                    function ($parser, string $name): void {
25221aa04befSGreg Roach                        $this->endElement($parser, $name);
25231aa04befSGreg Roach                    }
25241aa04befSGreg Roach                );
25251aa04befSGreg Roach
25261aa04befSGreg Roach                xml_set_character_data_handler(
25271aa04befSGreg Roach                    $repeat_parser,
25289d454b6bSGreg Roach                    function ($parser, string $data): void {
25291aa04befSGreg Roach                        $this->characterData($parser, $data);
25301aa04befSGreg Roach                    }
25311aa04befSGreg Roach                );
2532a6f13a4aSGreg Roach
2533a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25346ccdf4f0SGreg 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)));
2535a6f13a4aSGreg Roach                }
2536a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2537a6f13a4aSGreg Roach            }
2538a6f13a4aSGreg Roach            // Clean up the list array
253913abd6f3SGreg Roach            $this->list   = [];
2540e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2541a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2542a6f13a4aSGreg Roach        }
254365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2544a6f13a4aSGreg Roach    }
2545a6f13a4aSGreg Roach
2546a6f13a4aSGreg Roach    /**
2547a6f13a4aSGreg Roach     * XML <Generation /> element handler
2548a6f13a4aSGreg Roach     * Prints the number of generations
25498ba2e626SGreg Roach     *
25508ba2e626SGreg Roach     * @return void
2551a6f13a4aSGreg Roach     */
2552b702978eSGreg Roach    protected function generationStartHandler(): void
2553c1010edaSGreg Roach    {
2554589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2555a6f13a4aSGreg Roach    }
2556a6f13a4aSGreg Roach
2557a6f13a4aSGreg Roach    /**
2558a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2559a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25608ba2e626SGreg Roach     *
25618ba2e626SGreg Roach     * @return void
2562a6f13a4aSGreg Roach     */
2563b702978eSGreg Roach    protected function newPageStartHandler(): void
2564c1010edaSGreg Roach    {
25657a6ee1acSGreg Roach        $temp = 'addpage';
2566e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2567a6f13a4aSGreg Roach    }
2568a6f13a4aSGreg Roach
2569a6f13a4aSGreg Roach    /**
257076692c8bSGreg Roach     * XML </titleEndHandler>
25718ba2e626SGreg Roach     *
25728ba2e626SGreg Roach     * @return void
2573a6f13a4aSGreg Roach     */
2574b702978eSGreg Roach    protected function titleEndHandler(): void
2575c1010edaSGreg Roach    {
25762836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2577a6f13a4aSGreg Roach    }
2578a6f13a4aSGreg Roach
2579a6f13a4aSGreg Roach    /**
258076692c8bSGreg Roach     * XML </descriptionEndHandler>
25818ba2e626SGreg Roach     *
25828ba2e626SGreg Roach     * @return void
2583a6f13a4aSGreg Roach     */
2584b702978eSGreg Roach    protected function descriptionEndHandler(): void
2585c1010edaSGreg Roach    {
25862836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2587a6f13a4aSGreg Roach    }
2588729ce104SGreg Roach
2589729ce104SGreg Roach    /**
259076692c8bSGreg Roach     * Create a list of all descendants.
259176692c8bSGreg Roach     *
2592729ce104SGreg Roach     * @param string[] $list
2593729ce104SGreg Roach     * @param string   $pid
2594729ce104SGreg Roach     * @param bool     $parents
2595729ce104SGreg Roach     * @param int      $generations
25968ba2e626SGreg Roach     *
25978ba2e626SGreg Roach     * @return void
2598729ce104SGreg Roach     */
25993b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2600c1010edaSGreg Roach    {
2601299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2602729ce104SGreg Roach        if ($person === null) {
2603729ce104SGreg Roach            return;
2604729ce104SGreg Roach        }
2605729ce104SGreg Roach        if (!isset($list[$pid])) {
2606729ce104SGreg Roach            $list[$pid] = $person;
2607729ce104SGreg Roach        }
2608729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2609729ce104SGreg Roach            $list[$pid]->generation = 0;
2610729ce104SGreg Roach        }
261139ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2612729ce104SGreg Roach            if ($parents) {
261339ca88baSGreg Roach                $husband = $family->husband();
261439ca88baSGreg Roach                $wife    = $family->wife();
2615729ce104SGreg Roach                if ($husband) {
2616c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2617729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2618c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2619729ce104SGreg Roach                    } else {
2620c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2621729ce104SGreg Roach                    }
2622729ce104SGreg Roach                }
2623729ce104SGreg Roach                if ($wife) {
2624c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2625729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2626c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2627729ce104SGreg Roach                    } else {
2628c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2629729ce104SGreg Roach                    }
2630729ce104SGreg Roach                }
2631729ce104SGreg Roach            }
2632820b62dfSGreg Roach
263339ca88baSGreg Roach            $children = $family->children();
2634820b62dfSGreg Roach
2635729ce104SGreg Roach            foreach ($children as $child) {
2636729ce104SGreg Roach                if ($child) {
2637c0935879SGreg Roach                    $list[$child->xref()] = $child;
2638820b62dfSGreg Roach
2639729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2640c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2641729ce104SGreg Roach                    } else {
2642c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2643729ce104SGreg Roach                    }
2644729ce104SGreg Roach                }
2645729ce104SGreg Roach            }
2646729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2647729ce104SGreg Roach                foreach ($children as $child) {
2648c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2649729ce104SGreg Roach                }
2650729ce104SGreg Roach            }
2651729ce104SGreg Roach        }
2652729ce104SGreg Roach    }
2653729ce104SGreg Roach
2654729ce104SGreg Roach    /**
265576692c8bSGreg Roach     * Create a list of all ancestors.
265676692c8bSGreg Roach     *
26570cc533a6SGreg Roach     * @param stdClass[] $list
2658729ce104SGreg Roach     * @param string     $pid
2659729ce104SGreg Roach     * @param bool       $children
2660729ce104SGreg Roach     * @param int        $generations
26618ba2e626SGreg Roach     *
26628ba2e626SGreg Roach     * @return void
2663729ce104SGreg Roach     */
26643b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2665c1010edaSGreg Roach    {
266613abd6f3SGreg Roach        $genlist                = [$pid];
2667729ce104SGreg Roach        $list[$pid]->generation = 1;
2668729ce104SGreg Roach        while (count($genlist) > 0) {
2669729ce104SGreg Roach            $id = array_shift($genlist);
2670729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2671729ce104SGreg Roach                continue; // id can be something like “empty7”
2672729ce104SGreg Roach            }
2673299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
267439ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
267539ca88baSGreg Roach                $husband = $family->husband();
267639ca88baSGreg Roach                $wife    = $family->wife();
2677729ce104SGreg Roach                if ($husband) {
2678c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2679c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2680729ce104SGreg Roach                }
2681729ce104SGreg Roach                if ($wife) {
2682c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2683c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2684729ce104SGreg Roach                }
2685729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2686729ce104SGreg Roach                    if ($husband) {
2687c0935879SGreg Roach                        $genlist[] = $husband->xref();
2688729ce104SGreg Roach                    }
2689729ce104SGreg Roach                    if ($wife) {
2690c0935879SGreg Roach                        $genlist[] = $wife->xref();
2691729ce104SGreg Roach                    }
2692729ce104SGreg Roach                }
2693729ce104SGreg Roach                if ($children) {
269439ca88baSGreg Roach                    foreach ($family->children() as $child) {
2695c0935879SGreg Roach                        $list[$child->xref()] = $child;
2696e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2697729ce104SGreg Roach                    }
2698729ce104SGreg Roach                }
2699729ce104SGreg Roach            }
2700729ce104SGreg Roach        }
2701729ce104SGreg Roach    }
2702729ce104SGreg Roach
2703729ce104SGreg Roach    /**
2704729ce104SGreg Roach     * get gedcom tag value
2705729ce104SGreg Roach     *
2706729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2707729ce104SGreg 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
2708729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2709729ce104SGreg Roach     *
2710729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2711729ce104SGreg Roach     */
2712b2448a1bSGreg Roach    private function getGedcomValue(string $tag, int $level, string $gedrec): string
2713c1010edaSGreg Roach    {
2714b2448a1bSGreg Roach        if ($gedrec === '') {
2715729ce104SGreg Roach            return '';
2716729ce104SGreg Roach        }
2717729ce104SGreg Roach        $tags      = explode(':', $tag);
2718729ce104SGreg Roach        $origlevel = $level;
2719b2448a1bSGreg Roach        if ($level === 0) {
27203c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2721729ce104SGreg Roach        }
2722729ce104SGreg Roach
2723729ce104SGreg Roach        $subrec = $gedrec;
2724729ce104SGreg Roach        foreach ($tags as $t) {
2725729ce104SGreg Roach            $lastsubrec = $subrec;
27263d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2727729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2728729ce104SGreg Roach                $level--;
27293d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2730729ce104SGreg Roach            }
2731729ce104SGreg Roach            if (empty($subrec)) {
2732044416d2SGreg Roach                if ($t === 'TITL') {
27333d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2734729ce104SGreg Roach                    if (!empty($subrec)) {
27357a6ee1acSGreg Roach                        $t = 'ABBR';
2736729ce104SGreg Roach                    }
2737729ce104SGreg Roach                }
2738b2448a1bSGreg Roach                if ($subrec === '') {
2739729ce104SGreg Roach                    if ($level > 0) {
2740729ce104SGreg Roach                        $level--;
2741729ce104SGreg Roach                    }
27423d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2743b2448a1bSGreg Roach                    if ($subrec === '') {
2744729ce104SGreg Roach                        return '';
2745729ce104SGreg Roach                    }
2746729ce104SGreg Roach                }
2747729ce104SGreg Roach            }
2748729ce104SGreg Roach            $level++;
2749729ce104SGreg Roach        }
2750729ce104SGreg Roach        $level--;
2751729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2752729ce104SGreg Roach        if ($ct == 0) {
2753729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2754729ce104SGreg Roach        }
2755729ce104SGreg Roach        if ($ct == 0) {
2756729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2757729ce104SGreg Roach        }
2758729ce104SGreg Roach        if ($ct > 0) {
2759729ce104SGreg Roach            $value = trim($match[1]);
2760044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2761299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2762ff166e64SGreg Roach                if ($note instanceof Note) {
2763729ce104SGreg Roach                    $value = $note->getNote();
2764729ce104SGreg Roach                } else {
2765729ce104SGreg Roach                    //-- set the value to the id without the @
2766729ce104SGreg Roach                    $value = $match[1];
2767729ce104SGreg Roach                }
2768729ce104SGreg Roach            }
27697a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
27703d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2771729ce104SGreg Roach            }
2772729ce104SGreg Roach
2773729ce104SGreg Roach            return $value;
2774729ce104SGreg Roach        }
2775729ce104SGreg Roach
27767a6ee1acSGreg Roach        return '';
2777729ce104SGreg Roach    }
2778d1286247SGreg Roach
2779d1286247SGreg Roach    /**
2780d1286247SGreg Roach     * Replace variable identifiers with their values.
2781d1286247SGreg Roach     *
2782d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
278382759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2784d1286247SGreg Roach     *
2785d1286247SGreg Roach     * @return string
2786d1286247SGreg Roach     */
27878f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2788c1010edaSGreg Roach    {
2789d1286247SGreg Roach        return preg_replace_callback(
2790d1286247SGreg Roach            '/\$(\w+)/',
279118d7a90dSGreg Roach            function (array $matches) use ($quote): string {
27922118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
279382759250SGreg Roach                    if ($quote) {
27942118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2795b2ce94c6SRico Sonntag                    }
2796b2ce94c6SRico Sonntag
27972118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
279882759250SGreg Roach                }
2799b2ce94c6SRico Sonntag
2800d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28013d7a8a4cSGreg Roach
2802d1286247SGreg Roach                return '$' . $matches[1];
2803d1286247SGreg Roach            },
2804d1286247SGreg Roach            $expression
2805d1286247SGreg Roach        );
2806d1286247SGreg Roach    }
2807a6f13a4aSGreg Roach}
2808