xref: /webtrees/app/Report/ReportParserGenerate.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1a6f13a4aSGreg Roach<?php
2*3976b470SGreg 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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2076692c8bSGreg Roach
216ccdf4f0SGreg Roachuse DomainException;
22a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
25a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
26a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
273d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
289a27f660SGreg Roachuse Fisharebest\Webtrees\Gedcom;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
32a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
33d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
34a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Media;
35729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
36a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
37299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
38195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
39195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
40a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
415985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
429a9e551aSGreg Roachuse Illuminate\Support\Str;
4379529c87SGreg Roachuse stdClass;
44c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
455809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
46a6f13a4aSGreg Roach
47a6f13a4aSGreg Roach/**
48a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
49a6f13a4aSGreg Roach */
50c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
51c1010edaSGreg Roach{
52a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
53a6f13a4aSGreg Roach    private $process_footnote = true;
54a6f13a4aSGreg Roach
55a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
56a6f13a4aSGreg Roach    private $print_data = false;
57a6f13a4aSGreg Roach
58a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5913abd6f3SGreg Roach    private $print_data_stack = [];
60a6f13a4aSGreg Roach
6176692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
62a6f13a4aSGreg Roach    private $process_gedcoms = 0;
63a6f13a4aSGreg Roach
6476692c8bSGreg Roach    /** @var int Are we processing conditionals */
65a6f13a4aSGreg Roach    private $process_ifs = 0;
66a6f13a4aSGreg Roach
6776692c8bSGreg Roach    /** @var int Are we processing repeats */
68a6f13a4aSGreg Roach    private $process_repeats = 0;
69a6f13a4aSGreg Roach
70a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
71a6f13a4aSGreg Roach    private $repeat_bytes = 0;
72a6f13a4aSGreg Roach
735b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
7413abd6f3SGreg Roach    private $repeats = [];
75a6f13a4aSGreg Roach
76a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
7713abd6f3SGreg Roach    private $repeats_stack = [];
78a6f13a4aSGreg Roach
79208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
8013abd6f3SGreg Roach    private $wt_report_stack = [];
81e8e7866bSGreg Roach
82e8e7866bSGreg Roach    /** @var resource Nested repeating data */
83e8e7866bSGreg Roach    private $parser;
84e8e7866bSGreg Roach
85e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
8613abd6f3SGreg Roach    private $parser_stack = [];
87e8e7866bSGreg Roach
88a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
89a6f13a4aSGreg Roach    private $gedrec = '';
90a6f13a4aSGreg Roach
91a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
9213abd6f3SGreg Roach    private $gedrec_stack = [];
93a6f13a4aSGreg Roach
94a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
95a6f13a4aSGreg Roach    private $current_element;
96a6f13a4aSGreg Roach
97a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
98a6f13a4aSGreg Roach    private $footnote_element;
99a6f13a4aSGreg Roach
100a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
101a6f13a4aSGreg Roach    private $fact = '';
102a6f13a4aSGreg Roach
103a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
104a6f13a4aSGreg Roach    private $desc = '';
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
107a6f13a4aSGreg Roach    private $type = '';
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var int The current generational level */
110a6f13a4aSGreg Roach    private $generation = 1;
111a6f13a4aSGreg Roach
112a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
11313abd6f3SGreg Roach    private $list = [];
114a6f13a4aSGreg Roach
115a6f13a4aSGreg Roach    /** @var int Number of items in lists */
116a6f13a4aSGreg Roach    private $list_total = 0;
117a6f13a4aSGreg Roach
118a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
119a6f13a4aSGreg Roach    private $list_private = 0;
120a6f13a4aSGreg Roach
121299d100dSGreg Roach    /** @var string The filename of the XML report */
122299d100dSGreg Roach    protected $report;
123299d100dSGreg Roach
124208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
125e8e7866bSGreg Roach    private $report_root;
126e8e7866bSGreg Roach
127208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
128e8e7866bSGreg Roach    private $wt_report;
129e8e7866bSGreg Roach
130d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1312118c0e3SGreg Roach    private $vars;
132d1286247SGreg Roach
133299d100dSGreg Roach    /** @var Tree The current tree */
134299d100dSGreg Roach    private $tree;
135299d100dSGreg Roach
13676692c8bSGreg Roach    /**
13776692c8bSGreg Roach     * Create a parser for a report
13876692c8bSGreg Roach     *
13976692c8bSGreg Roach     * @param string         $report The XML filename
140208e9f76SGreg Roach     * @param AbstractReport $report_root
14176692c8bSGreg Roach     * @param string[][]     $vars
142299d100dSGreg Roach     * @param Tree           $tree
14376692c8bSGreg Roach     */
144208e9f76SGreg Roach    public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree)
145c1010edaSGreg Roach    {
146299d100dSGreg Roach        $this->report          = $report;
147e8e7866bSGreg Roach        $this->report_root     = $report_root;
148e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14959f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
150d1286247SGreg Roach        $this->vars            = $vars;
151299d100dSGreg Roach        $this->tree            = $tree;
152299d100dSGreg Roach
15376f666f4SGreg Roach        parent::__construct($report);
154a6f13a4aSGreg Roach    }
155a6f13a4aSGreg Roach
156a6f13a4aSGreg Roach    /**
157a6f13a4aSGreg Roach     * XML start element handler
158a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
159a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
160a6f13a4aSGreg Roach     *
161a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
162a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1638a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
16418d7a90dSGreg Roach     *
16518d7a90dSGreg Roach     * @return void
166a6f13a4aSGreg Roach     */
167af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
168c1010edaSGreg Roach    {
16913abd6f3SGreg Roach        $newattrs = [];
170a6f13a4aSGreg Roach
171a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
172a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
173e364afe4SGreg Roach                if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) {
174d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
175a6f13a4aSGreg Roach                }
176a6f13a4aSGreg Roach            }
177a6f13a4aSGreg Roach            $newattrs[$key] = $value;
178a6f13a4aSGreg Roach        }
179a6f13a4aSGreg Roach        $attrs = $newattrs;
1807a6ee1acSGreg 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')) {
181a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
182a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
183208e9f76SGreg Roach
184a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
185a6f13a4aSGreg Roach                $this->$start_method($attrs);
186a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
187a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
188a6f13a4aSGreg Roach            }
189a6f13a4aSGreg Roach        }
190a6f13a4aSGreg Roach    }
191a6f13a4aSGreg Roach
192a6f13a4aSGreg Roach    /**
193a6f13a4aSGreg Roach     * XML end element handler
194a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
195a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
196a6f13a4aSGreg Roach     *
197a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
198a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
19918d7a90dSGreg Roach     *
20018d7a90dSGreg Roach     * @return void
201a6f13a4aSGreg Roach     */
202af14d238SGreg Roach    protected function endElement($parser, string $name): void
203c1010edaSGreg Roach    {
2047a6ee1acSGreg 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')) {
205a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
206a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
207a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
208a6f13a4aSGreg Roach                $this->$end_method();
209a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
210a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
211a6f13a4aSGreg Roach            }
212a6f13a4aSGreg Roach        }
213a6f13a4aSGreg Roach    }
214a6f13a4aSGreg Roach
215a6f13a4aSGreg Roach    /**
216a6f13a4aSGreg Roach     * XML character data handler
217a6f13a4aSGreg Roach     *
218a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
219a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
22018d7a90dSGreg Roach     *
22118d7a90dSGreg Roach     * @return void
222a6f13a4aSGreg Roach     */
223af14d238SGreg Roach    protected function characterData($parser, $data): void
224c1010edaSGreg Roach    {
225e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
226a6f13a4aSGreg Roach            $this->current_element->addText($data);
227a6f13a4aSGreg Roach        }
228a6f13a4aSGreg Roach    }
229a6f13a4aSGreg Roach
230a6f13a4aSGreg Roach    /**
23176692c8bSGreg Roach     * XML <style>
232a6f13a4aSGreg Roach     *
233c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2348ba2e626SGreg Roach     *
2358ba2e626SGreg Roach     * @return void
236a6f13a4aSGreg Roach     */
237b702978eSGreg Roach    protected function styleStartHandler(array $attrs): void
238c1010edaSGreg Roach    {
239a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
2406ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
241a6f13a4aSGreg Roach        }
242a6f13a4aSGreg Roach
243a6f13a4aSGreg Roach        // array Style that will be passed on
24413abd6f3SGreg Roach        $s = [];
245a6f13a4aSGreg Roach
246a6f13a4aSGreg Roach        // string Name af the style
247a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
248a6f13a4aSGreg Roach
249a6f13a4aSGreg Roach        // string Name of the DEFAULT font
250208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
251a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
252a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
253a6f13a4aSGreg Roach        }
254a6f13a4aSGreg Roach
255a6f13a4aSGreg Roach        // int The size of the font in points
256208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
257a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
258a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
259a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
260a6f13a4aSGreg Roach
261a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2627a6ee1acSGreg Roach        $s['style'] = '';
263a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
264a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
265a6f13a4aSGreg Roach        }
266a6f13a4aSGreg Roach
267e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
268a6f13a4aSGreg Roach    }
269a6f13a4aSGreg Roach
270a6f13a4aSGreg Roach    /**
27176692c8bSGreg Roach     * XML <Doc>
272a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
273a6f13a4aSGreg Roach     *
274c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
2758ba2e626SGreg Roach     *
2768ba2e626SGreg Roach     * @return void
277a6f13a4aSGreg Roach     */
278b702978eSGreg Roach    protected function docStartHandler(array $attrs): void
279c1010edaSGreg Roach    {
280e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
281a6f13a4aSGreg Roach
282a6f13a4aSGreg Roach        // Custom page width
283a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
284208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
285a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
286a6f13a4aSGreg Roach        // Custom Page height
287a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
288208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
289a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
290a6f13a4aSGreg Roach
291a6f13a4aSGreg Roach        // Left Margin
292a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2937a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
294208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
295a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
296208e9f76SGreg 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))
297a6f13a4aSGreg Roach            }
298a6f13a4aSGreg Roach        }
299a6f13a4aSGreg Roach        // Right Margin
300a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
3017a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
302208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
303a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
304208e9f76SGreg 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))
305a6f13a4aSGreg Roach            }
306a6f13a4aSGreg Roach        }
307a6f13a4aSGreg Roach        // Top Margin
308a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3097a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
310208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
311a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
312208e9f76SGreg 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))
313a6f13a4aSGreg Roach            }
314a6f13a4aSGreg Roach        }
315a6f13a4aSGreg Roach        // Bottom Margin
316a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3177a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
318208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
319a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
320208e9f76SGreg 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))
321a6f13a4aSGreg Roach            }
322a6f13a4aSGreg Roach        }
323a6f13a4aSGreg Roach        // Header Margin
324a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3257a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
326208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
327a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
328208e9f76SGreg 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))
329a6f13a4aSGreg Roach            }
330a6f13a4aSGreg Roach        }
331a6f13a4aSGreg Roach        // Footer Margin
332a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3337a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
334208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
335a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
336208e9f76SGreg 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))
337a6f13a4aSGreg Roach            }
338a6f13a4aSGreg Roach        }
339a6f13a4aSGreg Roach
340a6f13a4aSGreg Roach        // Page Orientation
341a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
342044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3437a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
344044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3457a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
346a6f13a4aSGreg Roach            }
347a6f13a4aSGreg Roach        }
348a6f13a4aSGreg Roach        // Page Size
349a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
350208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
351a6f13a4aSGreg Roach        }
352a6f13a4aSGreg Roach
353a6f13a4aSGreg Roach        // Show Generated By...
354a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3557a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
356208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3577a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
358208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
359a6f13a4aSGreg Roach            }
360a6f13a4aSGreg Roach        }
361a6f13a4aSGreg Roach
362e8e7866bSGreg Roach        $this->wt_report->setup();
363a6f13a4aSGreg Roach    }
364a6f13a4aSGreg Roach
365a6f13a4aSGreg Roach    /**
36676692c8bSGreg Roach     * XML </Doc>
3678ba2e626SGreg Roach     *
3688ba2e626SGreg Roach     * @return void
369a6f13a4aSGreg Roach     */
370b702978eSGreg Roach    protected function docEndHandler(): void
371c1010edaSGreg Roach    {
372e8e7866bSGreg Roach        $this->wt_report->run();
373a6f13a4aSGreg Roach    }
374a6f13a4aSGreg Roach
375a6f13a4aSGreg Roach    /**
37676692c8bSGreg Roach     * XML <Header>
3778ba2e626SGreg Roach     *
3788ba2e626SGreg Roach     * @return void
379a6f13a4aSGreg Roach     */
380b702978eSGreg Roach    protected function headerStartHandler(): void
381c1010edaSGreg Roach    {
382a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
383e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3847a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
385a6f13a4aSGreg Roach    }
386a6f13a4aSGreg Roach
387a6f13a4aSGreg Roach    /**
38876692c8bSGreg Roach     * XML <PageHeader>
3898ba2e626SGreg Roach     *
3908ba2e626SGreg Roach     * @return void
391a6f13a4aSGreg Roach     */
392b702978eSGreg Roach    protected function pageHeaderStartHandler(): void
393c1010edaSGreg Roach    {
3949b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
395a6f13a4aSGreg Roach        $this->print_data         = false;
3969b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
397e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
398a6f13a4aSGreg Roach    }
399a6f13a4aSGreg Roach
400a6f13a4aSGreg Roach    /**
40176692c8bSGreg Roach     * XML <pageHeaderEndHandler>
4028ba2e626SGreg Roach     *
4038ba2e626SGreg Roach     * @return void
404a6f13a4aSGreg Roach     */
405b702978eSGreg Roach    protected function pageHeaderEndHandler(): void
406c1010edaSGreg Roach    {
407a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
408e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
409e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
410e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
411a6f13a4aSGreg Roach    }
412a6f13a4aSGreg Roach
413a6f13a4aSGreg Roach    /**
41476692c8bSGreg Roach     * XML <bodyStartHandler>
4158ba2e626SGreg Roach     *
4168ba2e626SGreg Roach     * @return void
417a6f13a4aSGreg Roach     */
418b702978eSGreg Roach    protected function bodyStartHandler(): void
419c1010edaSGreg Roach    {
4207a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
421a6f13a4aSGreg Roach    }
422a6f13a4aSGreg Roach
423a6f13a4aSGreg Roach    /**
42476692c8bSGreg Roach     * XML <footerStartHandler>
4258ba2e626SGreg Roach     *
4268ba2e626SGreg Roach     * @return void
427a6f13a4aSGreg Roach     */
428b702978eSGreg Roach    protected function footerStartHandler(): void
429c1010edaSGreg Roach    {
4307a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
431a6f13a4aSGreg Roach    }
432a6f13a4aSGreg Roach
433a6f13a4aSGreg Roach    /**
43476692c8bSGreg Roach     * XML <Cell>
435a6f13a4aSGreg Roach     *
436c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4378ba2e626SGreg Roach     *
4388ba2e626SGreg Roach     * @return void
439a6f13a4aSGreg Roach     */
440b702978eSGreg Roach    protected function cellStartHandler(array $attrs): void
441c1010edaSGreg Roach    {
442a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4437a6ee1acSGreg Roach        $align = '';
444a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
445a6f13a4aSGreg Roach            $align = $attrs['align'];
446a6f13a4aSGreg Roach            // RTL supported left/right alignment
447044416d2SGreg Roach            if ($align === 'rightrtl') {
448e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4497a6ee1acSGreg Roach                    $align = 'left';
450a6f13a4aSGreg Roach                } else {
4517a6ee1acSGreg Roach                    $align = 'right';
452a6f13a4aSGreg Roach                }
453044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
454e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4557a6ee1acSGreg Roach                    $align = 'right';
456a6f13a4aSGreg Roach                } else {
4577a6ee1acSGreg Roach                    $align = 'left';
458a6f13a4aSGreg Roach                }
459a6f13a4aSGreg Roach            }
460a6f13a4aSGreg Roach        }
461a6f13a4aSGreg Roach
462a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4637a6ee1acSGreg Roach        $bgcolor = '';
464a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
465a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
466a6f13a4aSGreg Roach        }
467a6f13a4aSGreg Roach
468a6f13a4aSGreg Roach        // int Whether or not the background should be painted
469a6f13a4aSGreg Roach        $fill = 1;
470a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4717a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
472a6f13a4aSGreg Roach                $fill = 0;
4737a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
474a6f13a4aSGreg Roach                $fill = 1;
475a6f13a4aSGreg Roach            }
476a6f13a4aSGreg Roach        }
477a6f13a4aSGreg Roach
478a6f13a4aSGreg Roach        $reseth = true;
479a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
480a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4817a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
482a6f13a4aSGreg Roach                $reseth = false;
4837a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
484a6f13a4aSGreg Roach                $reseth = true;
485a6f13a4aSGreg Roach            }
486a6f13a4aSGreg Roach        }
487a6f13a4aSGreg Roach
488a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
489a6f13a4aSGreg Roach        $border = 0;
490a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
491a6f13a4aSGreg Roach            $border = $attrs['border'];
492a6f13a4aSGreg Roach        }
493a6f13a4aSGreg Roach        // string Border color in HTML code
4947a6ee1acSGreg Roach        $bocolor = '';
495a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
496a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
497a6f13a4aSGreg Roach        }
498a6f13a4aSGreg Roach
499a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
500a6f13a4aSGreg Roach        $height = 0;
501a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
502589feda3SGreg Roach            $height = $attrs['height'];
503a6f13a4aSGreg Roach        }
504a6f13a4aSGreg 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.
505a6f13a4aSGreg Roach        $width = 0;
506a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
507589feda3SGreg Roach            $width = $attrs['width'];
508a6f13a4aSGreg Roach        }
509a6f13a4aSGreg Roach
510a6f13a4aSGreg Roach        // int Stretch carachter mode
511a6f13a4aSGreg Roach        $stretch = 0;
512a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
513a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
514a6f13a4aSGreg Roach        }
515a6f13a4aSGreg Roach
516a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
517c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
518a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5197a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
520c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
521a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
522a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5237a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
524a6f13a4aSGreg Roach                $left = 0;
525a6f13a4aSGreg Roach            }
526a6f13a4aSGreg Roach        }
527a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
528c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
529a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5307a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
531c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
532a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
533a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5347a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
535a6f13a4aSGreg Roach                $top = 0;
536a6f13a4aSGreg Roach            }
537a6f13a4aSGreg Roach        }
538a6f13a4aSGreg Roach
539a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5407a6ee1acSGreg Roach        $style = '';
541a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
542a6f13a4aSGreg Roach            $style = $attrs['style'];
543a6f13a4aSGreg Roach        }
544a6f13a4aSGreg Roach
545a6f13a4aSGreg Roach        // string Text color in html code
5467a6ee1acSGreg Roach        $tcolor = '';
547a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
548a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
549a6f13a4aSGreg Roach        }
550a6f13a4aSGreg Roach
551a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
552a6f13a4aSGreg Roach        $ln = 0;
553a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
554a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
555a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5567a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
557a6f13a4aSGreg Roach                $ln = 0;
558a6f13a4aSGreg Roach            }
559a6f13a4aSGreg Roach        }
560a6f13a4aSGreg Roach
561044416d2SGreg Roach        if ($align === 'left') {
5627a6ee1acSGreg Roach            $align = 'L';
563044416d2SGreg Roach        } elseif ($align === 'right') {
5647a6ee1acSGreg Roach            $align = 'R';
565044416d2SGreg Roach        } elseif ($align === 'center') {
5667a6ee1acSGreg Roach            $align = 'C';
567044416d2SGreg Roach        } elseif ($align === 'justify') {
5687a6ee1acSGreg Roach            $align = 'J';
569a6f13a4aSGreg Roach        }
570a6f13a4aSGreg Roach
5719b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
572a6f13a4aSGreg Roach        $this->print_data         = true;
573a6f13a4aSGreg Roach
574e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
575a6f13a4aSGreg Roach            $width,
576a6f13a4aSGreg Roach            $height,
577a6f13a4aSGreg Roach            $border,
578a6f13a4aSGreg Roach            $align,
579a6f13a4aSGreg Roach            $bgcolor,
580a6f13a4aSGreg Roach            $style,
581a6f13a4aSGreg Roach            $ln,
582a6f13a4aSGreg Roach            $top,
583a6f13a4aSGreg Roach            $left,
584a6f13a4aSGreg Roach            $fill,
585a6f13a4aSGreg Roach            $stretch,
586a6f13a4aSGreg Roach            $bocolor,
587a6f13a4aSGreg Roach            $tcolor,
588a6f13a4aSGreg Roach            $reseth
589a6f13a4aSGreg Roach        );
590a6f13a4aSGreg Roach    }
591a6f13a4aSGreg Roach
592a6f13a4aSGreg Roach    /**
59376692c8bSGreg Roach     * XML </Cell>
5948ba2e626SGreg Roach     *
5958ba2e626SGreg Roach     * @return void
596a6f13a4aSGreg Roach     */
597b702978eSGreg Roach    protected function cellEndHandler(): void
598c1010edaSGreg Roach    {
599a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
600e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
601a6f13a4aSGreg Roach    }
602a6f13a4aSGreg Roach
603a6f13a4aSGreg Roach    /**
604a6f13a4aSGreg Roach     * XML <Now /> element handler
6058ba2e626SGreg Roach     *
6068ba2e626SGreg Roach     * @return void
607a6f13a4aSGreg Roach     */
608b702978eSGreg Roach    protected function nowStartHandler(): void
609c1010edaSGreg Roach    {
6104459dc9aSGreg Roach        $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL'));
611a6f13a4aSGreg Roach    }
612a6f13a4aSGreg Roach
613a6f13a4aSGreg Roach    /**
614a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6158ba2e626SGreg Roach     *
6168ba2e626SGreg Roach     * @return void
617a6f13a4aSGreg Roach     */
618b702978eSGreg Roach    protected function pageNumStartHandler(): void
619c1010edaSGreg Roach    {
6207a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
621a6f13a4aSGreg Roach    }
622a6f13a4aSGreg Roach
623a6f13a4aSGreg Roach    /**
624a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6258ba2e626SGreg Roach     *
6268ba2e626SGreg Roach     * @return void
627a6f13a4aSGreg Roach     */
628b702978eSGreg Roach    protected function totalPagesStartHandler(): void
629c1010edaSGreg Roach    {
6307a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
631a6f13a4aSGreg Roach    }
632a6f13a4aSGreg Roach
633a6f13a4aSGreg Roach    /**
634a6f13a4aSGreg Roach     * Called at the start of an element.
635a6f13a4aSGreg Roach     *
636c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6378ba2e626SGreg Roach     *
6388ba2e626SGreg Roach     * @return void
639a6f13a4aSGreg Roach     */
640b702978eSGreg Roach    protected function gedcomStartHandler(array $attrs): void
641c1010edaSGreg Roach    {
642a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
643a6f13a4aSGreg Roach            $this->process_gedcoms++;
644a6f13a4aSGreg Roach
645a6f13a4aSGreg Roach            return;
646a6f13a4aSGreg Roach        }
647a6f13a4aSGreg Roach
648a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6497a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6507a6ee1acSGreg Roach        $tags      = explode(':', $tag);
651a6f13a4aSGreg Roach        $newgedrec = '';
652a6f13a4aSGreg Roach        if (count($tags) < 2) {
653299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
654299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
655a6f13a4aSGreg Roach        }
656a6f13a4aSGreg Roach        if (empty($newgedrec)) {
657a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
658a6f13a4aSGreg Roach            $newgedrec = '';
659a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6607a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
661d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
662d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
663a6f13a4aSGreg Roach                    } else {
664299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
665299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
666a6f13a4aSGreg Roach                    }
667a6f13a4aSGreg Roach                } else {
6687a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66913abd6f3SGreg Roach                        $gmatch = [];
670a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
671299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
672299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
673a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
674a6f13a4aSGreg Roach                        } else {
675a6f13a4aSGreg Roach                            $newgedrec = '';
676a6f13a4aSGreg Roach                            break;
677a6f13a4aSGreg Roach                        }
678a6f13a4aSGreg Roach                    } else {
6797a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
6809a3accd7SGreg Roach                        $level     = 1 + (int) $temp[0];
6813d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
682a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
683a6f13a4aSGreg Roach                    }
684a6f13a4aSGreg Roach                }
685a6f13a4aSGreg Roach            }
686a6f13a4aSGreg Roach        }
687a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6889b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
689a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
690a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
691a6f13a4aSGreg Roach                $this->fact = $match[2];
692a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
693a6f13a4aSGreg Roach            }
694a6f13a4aSGreg Roach        } else {
695a6f13a4aSGreg Roach            $this->process_gedcoms++;
696a6f13a4aSGreg Roach        }
697a6f13a4aSGreg Roach    }
698a6f13a4aSGreg Roach
699a6f13a4aSGreg Roach    /**
700a6f13a4aSGreg Roach     * Called at the end of an element.
7018ba2e626SGreg Roach     *
7028ba2e626SGreg Roach     * @return void
703a6f13a4aSGreg Roach     */
704b702978eSGreg Roach    protected function gedcomEndHandler(): void
705c1010edaSGreg Roach    {
706a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
707a6f13a4aSGreg Roach            $this->process_gedcoms--;
708a6f13a4aSGreg Roach        } else {
70965e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
710a6f13a4aSGreg Roach        }
711a6f13a4aSGreg Roach    }
712a6f13a4aSGreg Roach
713a6f13a4aSGreg Roach    /**
71476692c8bSGreg Roach     * XML <textBoxStartHandler>
715a6f13a4aSGreg Roach     *
716c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7178ba2e626SGreg Roach     *
7188ba2e626SGreg Roach     * @return void
719a6f13a4aSGreg Roach     */
720b702978eSGreg Roach    protected function textBoxStartHandler(array $attrs): void
721c1010edaSGreg Roach    {
722a6f13a4aSGreg Roach        // string Background color code
7237a6ee1acSGreg Roach        $bgcolor = '';
724a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
725a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
726a6f13a4aSGreg Roach        }
727a6f13a4aSGreg Roach
728a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
729a6f13a4aSGreg Roach        $fill = true;
730a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7317a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
732a6f13a4aSGreg Roach                $fill = false;
7337a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
734a6f13a4aSGreg Roach                $fill = true;
735a6f13a4aSGreg Roach            }
736a6f13a4aSGreg Roach        }
737a6f13a4aSGreg Roach
738a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
739a6f13a4aSGreg Roach        $border = false;
740a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7417a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
742a6f13a4aSGreg Roach                $border = true;
7437a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
744a6f13a4aSGreg Roach                $border = false;
745a6f13a4aSGreg Roach            }
746a6f13a4aSGreg Roach        }
747a6f13a4aSGreg Roach
748a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
749a6f13a4aSGreg Roach        $height = 0;
750a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
751a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
752a6f13a4aSGreg Roach        }
753a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
754a6f13a4aSGreg Roach        $width = 0;
755a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
756a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
757a6f13a4aSGreg Roach        }
758a6f13a4aSGreg Roach
759a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
760c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
761a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7627a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
763c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
764a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
765a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7667a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
767a6f13a4aSGreg Roach                $left = 0;
768a6f13a4aSGreg Roach            }
769a6f13a4aSGreg Roach        }
770a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
771c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
772a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7737a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
774c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
775a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
776a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7777a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
778a6f13a4aSGreg Roach                $top = 0;
779a6f13a4aSGreg Roach            }
780a6f13a4aSGreg Roach        }
781a6f13a4aSGreg 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
782a6f13a4aSGreg Roach        $newline = false;
783a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7847a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
785a6f13a4aSGreg Roach                $newline = true;
7867a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
787a6f13a4aSGreg Roach                $newline = false;
788a6f13a4aSGreg Roach            }
789a6f13a4aSGreg Roach        }
790a6f13a4aSGreg Roach        // boolean
791a6f13a4aSGreg Roach        $pagecheck = true;
792a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7937a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
794a6f13a4aSGreg Roach                $pagecheck = false;
7957a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
796a6f13a4aSGreg Roach                $pagecheck = true;
797a6f13a4aSGreg Roach            }
798a6f13a4aSGreg Roach        }
799a6f13a4aSGreg Roach        // boolean Cell padding
800a6f13a4aSGreg Roach        $padding = true;
801a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8027a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
803a6f13a4aSGreg Roach                $padding = false;
8047a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
805a6f13a4aSGreg Roach                $padding = true;
806a6f13a4aSGreg Roach            }
807a6f13a4aSGreg Roach        }
808a6f13a4aSGreg Roach        // boolean Reset this box Height
809a6f13a4aSGreg Roach        $reseth = false;
810a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8117a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
812a6f13a4aSGreg Roach                $reseth = true;
8137a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
814a6f13a4aSGreg Roach                $reseth = false;
815a6f13a4aSGreg Roach            }
816a6f13a4aSGreg Roach        }
817a6f13a4aSGreg Roach
818a6f13a4aSGreg Roach        // string Style of rendering
8197a6ee1acSGreg Roach        $style = '';
820a6f13a4aSGreg Roach
8219b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
822a6f13a4aSGreg Roach        $this->print_data         = false;
823a6f13a4aSGreg Roach
8249b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
825e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
826a6f13a4aSGreg Roach            $width,
827a6f13a4aSGreg Roach            $height,
828a6f13a4aSGreg Roach            $border,
829a6f13a4aSGreg Roach            $bgcolor,
830a6f13a4aSGreg Roach            $newline,
831a6f13a4aSGreg Roach            $left,
832a6f13a4aSGreg Roach            $top,
833a6f13a4aSGreg Roach            $pagecheck,
834a6f13a4aSGreg Roach            $style,
835a6f13a4aSGreg Roach            $fill,
836a6f13a4aSGreg Roach            $padding,
837a6f13a4aSGreg Roach            $reseth
838a6f13a4aSGreg Roach        );
839a6f13a4aSGreg Roach    }
840a6f13a4aSGreg Roach
841a6f13a4aSGreg Roach    /**
84276692c8bSGreg Roach     * XML <textBoxEndHandler>
8438ba2e626SGreg Roach     *
8448ba2e626SGreg Roach     * @return void
845a6f13a4aSGreg Roach     */
846b702978eSGreg Roach    protected function textBoxEndHandler(): void
847c1010edaSGreg Roach    {
848a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
849e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
850e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
851e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
852a6f13a4aSGreg Roach    }
853a6f13a4aSGreg Roach
854a6f13a4aSGreg Roach    /**
85576692c8bSGreg Roach     * XLM <Text>.
85676692c8bSGreg Roach     *
857c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8588ba2e626SGreg Roach     *
8598ba2e626SGreg Roach     * @return void
860a6f13a4aSGreg Roach     */
861b702978eSGreg Roach    protected function textStartHandler(array $attrs): void
862c1010edaSGreg Roach    {
8639b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
864a6f13a4aSGreg Roach        $this->print_data         = true;
865a6f13a4aSGreg Roach
866a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8677a6ee1acSGreg Roach        $style = '';
868a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
869a6f13a4aSGreg Roach            $style = $attrs['style'];
870a6f13a4aSGreg Roach        }
871a6f13a4aSGreg Roach
872a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8737a6ee1acSGreg Roach        $color = '';
874a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
875a6f13a4aSGreg Roach            $color = $attrs['color'];
876a6f13a4aSGreg Roach        }
877a6f13a4aSGreg Roach
878e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
879a6f13a4aSGreg Roach    }
880a6f13a4aSGreg Roach
881a6f13a4aSGreg Roach    /**
88276692c8bSGreg Roach     * XML </Text>
8838ba2e626SGreg Roach     *
8848ba2e626SGreg Roach     * @return void
885a6f13a4aSGreg Roach     */
886b702978eSGreg Roach    protected function textEndHandler(): void
887c1010edaSGreg Roach    {
888a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
889e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
890a6f13a4aSGreg Roach    }
891a6f13a4aSGreg Roach
892a6f13a4aSGreg Roach    /**
89376692c8bSGreg Roach     * XML <GetPersonName/>
894a6f13a4aSGreg Roach     * Get the name
895a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
896a6f13a4aSGreg Roach     * 2. id is set with a record id
897a6f13a4aSGreg Roach     *
898c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8998ba2e626SGreg Roach     *
9008ba2e626SGreg Roach     * @return void
901a6f13a4aSGreg Roach     */
902b702978eSGreg Roach    protected function getPersonNameStartHandler(array $attrs): void
903c1010edaSGreg Roach    {
9047a6ee1acSGreg Roach        $id    = '';
90513abd6f3SGreg Roach        $match = [];
906a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9077a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
908a6f13a4aSGreg Roach                $id = $match[1];
909a6f13a4aSGreg Roach            }
910a6f13a4aSGreg Roach        } else {
9117a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
912d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
913d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
914a6f13a4aSGreg Roach                }
915a6f13a4aSGreg Roach            } else {
9167a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91713abd6f3SGreg Roach                    $gmatch = [];
918a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
919a6f13a4aSGreg Roach                        $id = $gmatch[1];
920a6f13a4aSGreg Roach                    }
921a6f13a4aSGreg Roach                } else {
922a6f13a4aSGreg Roach                    $id = $attrs['id'];
923a6f13a4aSGreg Roach                }
924a6f13a4aSGreg Roach            }
925a6f13a4aSGreg Roach        }
926a6f13a4aSGreg Roach        if (!empty($id)) {
927299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9288f038c36SRico Sonntag            if ($record === null) {
929a6f13a4aSGreg Roach                return;
930a6f13a4aSGreg Roach            }
931a6f13a4aSGreg Roach            if (!$record->canShowName()) {
932a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
933a6f13a4aSGreg Roach            } else {
93439ca88baSGreg Roach                $name = $record->fullName();
935a6f13a4aSGreg Roach                $name = preg_replace(
936c1010edaSGreg Roach                    [
937c1010edaSGreg Roach                        '/<span class="starredname">/',
938c1010edaSGreg Roach                        '/<\/span><\/span>/',
939c1010edaSGreg Roach                        '/<\/span>/',
940c1010edaSGreg Roach                    ],
941c1010edaSGreg Roach                    [
942c1010edaSGreg Roach                        '«',
943c1010edaSGreg Roach                        '',
944c1010edaSGreg Roach                        '»',
945c1010edaSGreg Roach                    ],
946a6f13a4aSGreg Roach                    $name
947a6f13a4aSGreg Roach                );
948a6f13a4aSGreg Roach                $name = strip_tags($name);
949a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
9509a9e551aSGreg Roach                    $name = Str::limit($name, $attrs['truncate'], I18N::translate('…'));
951a6f13a4aSGreg Roach                } else {
95239ca88baSGreg Roach                    $addname = $record->alternateName();
953a6f13a4aSGreg Roach                    $addname = preg_replace(
954c1010edaSGreg Roach                        [
955c1010edaSGreg Roach                            '/<span class="starredname">/',
956c1010edaSGreg Roach                            '/<\/span><\/span>/',
957c1010edaSGreg Roach                            '/<\/span>/',
958c1010edaSGreg Roach                        ],
959c1010edaSGreg Roach                        [
960c1010edaSGreg Roach                            '«',
961c1010edaSGreg Roach                            '',
962c1010edaSGreg Roach                            '»',
963c1010edaSGreg Roach                        ],
964a6f13a4aSGreg Roach                        $addname
965a6f13a4aSGreg Roach                    );
966a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
967a6f13a4aSGreg Roach                    if (!empty($addname)) {
9687a6ee1acSGreg Roach                        $name .= ' ' . $addname;
969a6f13a4aSGreg Roach                    }
970a6f13a4aSGreg Roach                }
971a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
972a6f13a4aSGreg Roach            }
973a6f13a4aSGreg Roach        }
974a6f13a4aSGreg Roach    }
975a6f13a4aSGreg Roach
976a6f13a4aSGreg Roach    /**
97776692c8bSGreg Roach     * XML <GedcomValue/>
978a6f13a4aSGreg Roach     *
979c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9808ba2e626SGreg Roach     *
9818ba2e626SGreg Roach     * @return void
982a6f13a4aSGreg Roach     */
983b702978eSGreg Roach    protected function gedcomValueStartHandler(array $attrs): void
984c1010edaSGreg Roach    {
9857a6ee1acSGreg Roach        $id    = '';
98613abd6f3SGreg Roach        $match = [];
9877a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
988a6f13a4aSGreg Roach            $id = $match[1];
989a6f13a4aSGreg Roach        }
990a6f13a4aSGreg Roach
991044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9927a6ee1acSGreg Roach            $useBreak = '1';
993a6f13a4aSGreg Roach        } else {
9947a6ee1acSGreg Roach            $useBreak = '0';
995a6f13a4aSGreg Roach        }
996a6f13a4aSGreg Roach
997a6f13a4aSGreg Roach        $tag = $attrs['tag'];
998a6f13a4aSGreg Roach        if (!empty($tag)) {
999044416d2SGreg Roach            if ($tag === '@desc') {
1000a6f13a4aSGreg Roach                $value = $this->desc;
1001a6f13a4aSGreg Roach                $value = trim($value);
1002a6f13a4aSGreg Roach                $this->current_element->addText($value);
1003a6f13a4aSGreg Roach            }
1004044416d2SGreg Roach            if ($tag === '@id') {
1005a6f13a4aSGreg Roach                $this->current_element->addText($id);
1006a6f13a4aSGreg Roach            } else {
10077a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1008a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
10097a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
1010a6f13a4aSGreg Roach                    $level = $temp[0];
1011a6f13a4aSGreg Roach                    if ($level == 0) {
1012a6f13a4aSGreg Roach                        $level++;
1013a6f13a4aSGreg Roach                    }
1014a6f13a4aSGreg Roach                } else {
1015a6f13a4aSGreg Roach                    $level = $attrs['level'];
1016a6f13a4aSGreg Roach                }
1017a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10183d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1019a6f13a4aSGreg Roach                switch (end($tags)) {
1020a6f13a4aSGreg Roach                    case 'DATE':
1021a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1022a6f13a4aSGreg Roach                        $value = $tmp->display();
1023a6f13a4aSGreg Roach                        break;
1024a6f13a4aSGreg Roach                    case 'PLAC':
1025299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1026392561bbSGreg Roach                        $value = $tmp->shortName();
1027a6f13a4aSGreg Roach                        break;
1028a6f13a4aSGreg Roach                }
1029044416d2SGreg Roach                if ($useBreak === '1') {
1030a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1031a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1032a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1033a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1034a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
10353b3cfeeaSGreg Roach                    if (substr($value, 0, 4) === '<br>') {
10363b3cfeeaSGreg Roach                        $value = substr($value, 4);
1037a6f13a4aSGreg Roach                    }
1038a6f13a4aSGreg Roach                }
1039d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1040c1010edaSGreg Roach                if (in_array(end($tmp), [
1041c1010edaSGreg Roach                    'NOTE',
1042c1010edaSGreg Roach                    'TEXT',
1043c1010edaSGreg Roach                ])) {
1044299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1045a4d703aeSGreg Roach                }
1046a2a485c3SGreg Roach
1047a2a485c3SGreg Roach                if (!empty($attrs['truncate'])) {
10489c32db08SGreg Roach                    $value = strip_tags($value);
1049a2a485c3SGreg Roach                    $value = Str::limit($value, $attrs['truncate'], I18N::translate('…'));
1050a2a485c3SGreg Roach                }
1051a6f13a4aSGreg Roach                $this->current_element->addText($value);
1052a6f13a4aSGreg Roach            }
1053a6f13a4aSGreg Roach        }
1054a6f13a4aSGreg Roach    }
1055a6f13a4aSGreg Roach
1056a6f13a4aSGreg Roach    /**
105776692c8bSGreg Roach     * XML <RepeatTag>
1058a6f13a4aSGreg Roach     *
1059c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10608ba2e626SGreg Roach     *
10618ba2e626SGreg Roach     * @return void
1062a6f13a4aSGreg Roach     */
1063b702978eSGreg Roach    protected function repeatTagStartHandler(array $attrs): void
1064c1010edaSGreg Roach    {
1065a6f13a4aSGreg Roach        $this->process_repeats++;
1066a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1067a6f13a4aSGreg Roach            return;
1068a6f13a4aSGreg Roach        }
1069a6f13a4aSGreg Roach
10709b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
107113abd6f3SGreg Roach        $this->repeats         = [];
1072e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1073a6f13a4aSGreg Roach
1074e364afe4SGreg Roach        $tag = $attrs['tag'] ?? '';
1075a6f13a4aSGreg Roach        if (!empty($tag)) {
1076044416d2SGreg Roach            if ($tag === '@desc') {
1077a6f13a4aSGreg Roach                $value = $this->desc;
1078a6f13a4aSGreg Roach                $value = trim($value);
1079a6f13a4aSGreg Roach                $this->current_element->addText($value);
1080a6f13a4aSGreg Roach            } else {
10817a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10827a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10837a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1084a6f13a4aSGreg Roach                $level = $temp[0];
1085a6f13a4aSGreg Roach                if ($level == 0) {
1086a6f13a4aSGreg Roach                    $level++;
1087a6f13a4aSGreg Roach                }
1088a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1089a6f13a4aSGreg Roach                $t      = $tag;
1090a6f13a4aSGreg Roach                $count  = count($tags);
1091a6f13a4aSGreg Roach                $i      = 0;
1092a6f13a4aSGreg Roach                while ($i < $count) {
1093a6f13a4aSGreg Roach                    $t = $tags[$i];
1094a6f13a4aSGreg Roach                    if (!empty($t)) {
1095a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10963d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1097a6f13a4aSGreg Roach                            if (empty($subrec)) {
1098a6f13a4aSGreg Roach                                $level--;
10993d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1100a6f13a4aSGreg Roach                                if (empty($subrec)) {
1101a6f13a4aSGreg Roach                                    return;
1102a6f13a4aSGreg Roach                                }
1103a6f13a4aSGreg Roach                            }
1104a6f13a4aSGreg Roach                        }
1105a6f13a4aSGreg Roach                        $level++;
1106a6f13a4aSGreg Roach                    }
1107a6f13a4aSGreg Roach                    $i++;
1108a6f13a4aSGreg Roach                }
1109a6f13a4aSGreg Roach                $level--;
1110a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1111a6f13a4aSGreg Roach                $i     = 0;
1112a6f13a4aSGreg Roach                while ($i < $count) {
1113a6f13a4aSGreg Roach                    $i++;
1114a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1115a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11168d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1117299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1118a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1119a9007102SGreg Roach                            continue;
1120a9007102SGreg Roach                        }
1121a9007102SGreg Roach                    }
1122a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1123a6f13a4aSGreg Roach                }
1124a6f13a4aSGreg Roach            }
1125a6f13a4aSGreg Roach        }
1126a6f13a4aSGreg Roach    }
1127a6f13a4aSGreg Roach
1128a6f13a4aSGreg Roach    /**
112976692c8bSGreg Roach     * XML </ RepeatTag>
11308ba2e626SGreg Roach     *
11318ba2e626SGreg Roach     * @return void
1132a6f13a4aSGreg Roach     */
1133b702978eSGreg Roach    protected function repeatTagEndHandler(): void
1134c1010edaSGreg Roach    {
1135a6f13a4aSGreg Roach        $this->process_repeats--;
1136a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1137a6f13a4aSGreg Roach            return;
1138a6f13a4aSGreg Roach        }
1139a6f13a4aSGreg Roach
1140a6f13a4aSGreg Roach        // Check if there is anything to repeat
1141a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1142a6f13a4aSGreg Roach            // No need to load them if not used...
1143a6f13a4aSGreg Roach
1144a6f13a4aSGreg Roach            $lineoffset = 0;
1145a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1146a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1147a6f13a4aSGreg Roach            }
1148a6f13a4aSGreg Roach            //-- read the xml from the file
1149299d100dSGreg Roach            $lines = file($this->report);
11507a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1151a6f13a4aSGreg Roach                $lineoffset--;
1152a6f13a4aSGreg Roach            }
1153a6f13a4aSGreg Roach            $lineoffset++;
1154a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1155a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1156a6f13a4aSGreg Roach            // RepeatTag Level counter
1157a6f13a4aSGreg Roach            $count = 1;
1158a6f13a4aSGreg Roach            while (0 < $count) {
11597a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1160a6f13a4aSGreg Roach                    $count++;
11617a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1162a6f13a4aSGreg Roach                    $count--;
1163a6f13a4aSGreg Roach                }
1164a6f13a4aSGreg Roach                if (0 < $count) {
1165a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1166a6f13a4aSGreg Roach                }
1167a6f13a4aSGreg Roach                $line_nr++;
1168a6f13a4aSGreg Roach            }
1169a6f13a4aSGreg Roach            // No need to drag this
1170a6f13a4aSGreg Roach            unset($lines);
1171a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1172a6f13a4aSGreg Roach            // Save original values
11739b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1174a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1175a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1176a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1177a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1178e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1179a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11801aa04befSGreg Roach
11811aa04befSGreg Roach                xml_set_element_handler(
11821aa04befSGreg Roach                    $repeat_parser,
11839d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
11841aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11851aa04befSGreg Roach                    },
11869d454b6bSGreg Roach                    function ($parser, string $name): void {
11871aa04befSGreg Roach                        $this->endElement($parser, $name);
11881aa04befSGreg Roach                    }
11891aa04befSGreg Roach                );
11901aa04befSGreg Roach
11911aa04befSGreg Roach                xml_set_character_data_handler(
11921aa04befSGreg Roach                    $repeat_parser,
11939d454b6bSGreg Roach                    function ($parser, string $data): void {
11941aa04befSGreg Roach                        $this->characterData($parser, $data);
11951aa04befSGreg Roach                    }
11961aa04befSGreg Roach                );
11971aa04befSGreg Roach
1198a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
11996ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1200a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1201a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1202a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1203a6f13a4aSGreg Roach                    ));
1204a6f13a4aSGreg Roach                }
1205a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1206a6f13a4aSGreg Roach            }
1207a6f13a4aSGreg Roach            // Restore original values
1208a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1209e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1210a6f13a4aSGreg Roach        }
121165e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1212a6f13a4aSGreg Roach    }
1213a6f13a4aSGreg Roach
1214a6f13a4aSGreg Roach    /**
1215a6f13a4aSGreg Roach     * Variable lookup
1216a6f13a4aSGreg Roach     * Retrieve predefined variables :
1217a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1218a6f13a4aSGreg Roach     *        1 EVEN This is a description
1219a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1220a6f13a4aSGreg Roach     * $ I18N::translate('....')
1221a6f13a4aSGreg Roach     * $ language_settings[]
1222a6f13a4aSGreg Roach     *
1223c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12248ba2e626SGreg Roach     *
12258ba2e626SGreg Roach     * @return void
1226a6f13a4aSGreg Roach     */
1227b702978eSGreg Roach    protected function varStartHandler(array $attrs): void
1228c1010edaSGreg Roach    {
1229a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
12306ccdf4f0SGreg 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));
1231a6f13a4aSGreg Roach        }
1232a6f13a4aSGreg Roach
1233a6f13a4aSGreg Roach        $var = $attrs['var'];
1234a6f13a4aSGreg Roach        // SetVar element preset variables
1235d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1236d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1237a6f13a4aSGreg Roach        } else {
1238a6f13a4aSGreg Roach            $tfact = $this->fact;
12397a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1240a6f13a4aSGreg Roach                // Use :
1241a6f13a4aSGreg Roach                // n TYPE This text if string
1242a6f13a4aSGreg Roach                $tfact = $this->type;
1243a6f13a4aSGreg Roach            }
1244c1010edaSGreg Roach            $var = str_replace([
1245c1010edaSGreg Roach                '@fact',
1246c1010edaSGreg Roach                '@desc',
1247c1010edaSGreg Roach            ], [
1248c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1249c1010edaSGreg Roach                $this->desc,
1250c1010edaSGreg Roach            ], $var);
1251a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1252da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1253a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1254a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1255a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1256a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1257a6f13a4aSGreg Roach            }
1258a6f13a4aSGreg Roach        }
1259a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1260a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12617a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1262a6f13a4aSGreg Roach                $g   = new Date($var);
1263a6f13a4aSGreg Roach                $var = $g->display();
1264a6f13a4aSGreg Roach            }
1265a6f13a4aSGreg Roach        }
1266a6f13a4aSGreg Roach        $this->current_element->addText($var);
12672836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1268a6f13a4aSGreg Roach    }
1269a6f13a4aSGreg Roach
1270a6f13a4aSGreg Roach    /**
127176692c8bSGreg Roach     * XML <Facts>
127276692c8bSGreg Roach     *
1273c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12748ba2e626SGreg Roach     *
12758ba2e626SGreg Roach     * @return void
1276a6f13a4aSGreg Roach     */
1277b702978eSGreg Roach    protected function factsStartHandler(array $attrs): void
1278c1010edaSGreg Roach    {
1279a6f13a4aSGreg Roach        $this->process_repeats++;
1280a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1281a6f13a4aSGreg Roach            return;
1282a6f13a4aSGreg Roach        }
1283a6f13a4aSGreg Roach
12849b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
128513abd6f3SGreg Roach        $this->repeats         = [];
1286e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1287a6f13a4aSGreg Roach
12887a6ee1acSGreg Roach        $id    = '';
128913abd6f3SGreg Roach        $match = [];
12907a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1291a6f13a4aSGreg Roach            $id = $match[1];
1292a6f13a4aSGreg Roach        }
12937a6ee1acSGreg Roach        $tag = '';
1294a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1295a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1296a6f13a4aSGreg Roach        }
12977a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1298d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1299a6f13a4aSGreg Roach        }
1300a6f13a4aSGreg Roach
1301299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1302a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
1303820b62dfSGreg Roach            $facts = $record->facts([], true);
130413abd6f3SGreg Roach            $this->repeats = [];
1305a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1306195b5e75SGreg Roach            foreach ($facts as $fact) {
1307e364afe4SGreg Roach                if (!in_array($fact->getTag(), $nonfacts, true)) {
1308195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1309a6f13a4aSGreg Roach                }
1310a6f13a4aSGreg Roach            }
1311a6f13a4aSGreg Roach        } else {
131230158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1313195b5e75SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1314138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1315a6f13a4aSGreg Roach                }
1316a6f13a4aSGreg Roach            }
1317a6f13a4aSGreg Roach        }
1318a6f13a4aSGreg Roach    }
1319a6f13a4aSGreg Roach
1320a6f13a4aSGreg Roach    /**
132176692c8bSGreg Roach     * XML </Facts>
13228ba2e626SGreg Roach     *
13238ba2e626SGreg Roach     * @return void
1324a6f13a4aSGreg Roach     */
1325b702978eSGreg Roach    protected function factsEndHandler(): void
1326c1010edaSGreg Roach    {
1327a6f13a4aSGreg Roach        $this->process_repeats--;
1328a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1329a6f13a4aSGreg Roach            return;
1330a6f13a4aSGreg Roach        }
1331a6f13a4aSGreg Roach
1332a6f13a4aSGreg Roach        // Check if there is anything to repeat
1333a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1334e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1335a6f13a4aSGreg Roach            $lineoffset = 0;
1336a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1337a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1338a6f13a4aSGreg Roach            }
1339a6f13a4aSGreg Roach
1340a6f13a4aSGreg Roach            //-- read the xml from the file
1341299d100dSGreg Roach            $lines = file($this->report);
1342a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1343a6f13a4aSGreg Roach                $lineoffset--;
1344a6f13a4aSGreg Roach            }
1345a6f13a4aSGreg Roach            $lineoffset++;
1346a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1347a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1348a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1349a6f13a4aSGreg Roach            while ($line_nr < $i) {
1350a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1351a6f13a4aSGreg Roach                $line_nr++;
1352a6f13a4aSGreg Roach            }
1353a6f13a4aSGreg Roach            // No need to drag this
1354a6f13a4aSGreg Roach            unset($lines);
1355a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1356a6f13a4aSGreg Roach            // Save original values
13579b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1358a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1359a6f13a4aSGreg Roach            $count                = count($this->repeats);
1360a6f13a4aSGreg Roach            $i                    = 0;
1361a6f13a4aSGreg Roach            while ($i < $count) {
1362a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1363a6f13a4aSGreg Roach                $this->fact   = '';
1364a6f13a4aSGreg Roach                $this->desc   = '';
1365a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1366a6f13a4aSGreg Roach                    $this->fact = $match[1];
1367a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
136813abd6f3SGreg Roach                        $tmatch = [];
1369a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1370a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1371a6f13a4aSGreg Roach                        } else {
1372a6f13a4aSGreg Roach                            $this->type = ' ';
1373a6f13a4aSGreg Roach                        }
1374a6f13a4aSGreg Roach                    }
1375a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13763d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1377a6f13a4aSGreg Roach                }
1378a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1379e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1380a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13811aa04befSGreg Roach
13821aa04befSGreg Roach                xml_set_element_handler(
13831aa04befSGreg Roach                    $repeat_parser,
13849d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
13851aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13861aa04befSGreg Roach                    },
13879d454b6bSGreg Roach                    function ($parser, string $name): void {
13881aa04befSGreg Roach                        $this->endElement($parser, $name);
13891aa04befSGreg Roach                    }
13901aa04befSGreg Roach                );
13911aa04befSGreg Roach
13921aa04befSGreg Roach                xml_set_character_data_handler(
13931aa04befSGreg Roach                    $repeat_parser,
13949d454b6bSGreg Roach                    function ($parser, string $data): void {
13951aa04befSGreg Roach                        $this->characterData($parser, $data);
13961aa04befSGreg Roach                    }
13971aa04befSGreg Roach                );
13981aa04befSGreg Roach
1399a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
14006ccdf4f0SGreg Roach                    throw new DomainException(sprintf(
1401a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1402a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1403a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1404a6f13a4aSGreg Roach                    ));
1405a6f13a4aSGreg Roach                }
1406a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1407a6f13a4aSGreg Roach                $i++;
1408a6f13a4aSGreg Roach            }
1409a6f13a4aSGreg Roach            // Restore original values
1410e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1411a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1412a6f13a4aSGreg Roach        }
141365e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1414a6f13a4aSGreg Roach    }
1415a6f13a4aSGreg Roach
1416a6f13a4aSGreg Roach    /**
1417a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1418d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1419a6f13a4aSGreg Roach     *
1420c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14218ba2e626SGreg Roach     *
14228ba2e626SGreg Roach     * @return void
1423a6f13a4aSGreg Roach     */
1424b702978eSGreg Roach    protected function setVarStartHandler(array $attrs): void
1425c1010edaSGreg Roach    {
1426a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
14276ccdf4f0SGreg Roach            throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1428a6f13a4aSGreg Roach        }
1429a6f13a4aSGreg Roach
1430a6f13a4aSGreg Roach        $name  = $attrs['name'];
1431a6f13a4aSGreg Roach        $value = $attrs['value'];
143213abd6f3SGreg Roach        $match = [];
1433a6f13a4aSGreg Roach        // Current GEDCOM record strings
1434044416d2SGreg Roach        if ($value === '@ID') {
14357a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1436a6f13a4aSGreg Roach                $value = $match[1];
1437a6f13a4aSGreg Roach            }
1438044416d2SGreg Roach        } elseif ($value === '@fact') {
1439a6f13a4aSGreg Roach            $value = $this->fact;
1440044416d2SGreg Roach        } elseif ($value === '@desc') {
1441a6f13a4aSGreg Roach            $value = $this->desc;
1442044416d2SGreg Roach        } elseif ($value === '@generation') {
1443589feda3SGreg Roach            $value = (string) $this->generation;
1444a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144513abd6f3SGreg Roach            $gmatch = [];
1446a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14477a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1448a6f13a4aSGreg Roach            }
1449a6f13a4aSGreg Roach        }
1450a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1451d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1452a6f13a4aSGreg Roach        }
1453a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1454a6f13a4aSGreg Roach        $i     = 0;
1455a6f13a4aSGreg Roach        while ($i < $count) {
1456d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14577a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1458a6f13a4aSGreg Roach            $i++;
1459a6f13a4aSGreg Roach        }
1460a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1461da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1462a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1463a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1464a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1465a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1466a6f13a4aSGreg Roach        }
146752868398SGreg Roach
1468a6f13a4aSGreg Roach        // Arithmetic functions
1469a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
147052868398SGreg Roach            // Create an expression language with the functions used by our reports.
147152868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1472c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1473c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
147452868398SGreg Roach
147552868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1476a6f13a4aSGreg Roach        }
147752868398SGreg Roach
14787a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14797a6ee1acSGreg Roach            $value = '';
1480a6f13a4aSGreg Roach        }
1481d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1482a6f13a4aSGreg Roach    }
1483a6f13a4aSGreg Roach
1484a6f13a4aSGreg Roach    /**
1485a6f13a4aSGreg Roach     * XML <if > start element
1486a6f13a4aSGreg Roach     *
1487c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14888ba2e626SGreg Roach     *
14898ba2e626SGreg Roach     * @return void
1490a6f13a4aSGreg Roach     */
1491b702978eSGreg Roach    protected function ifStartHandler(array $attrs): void
1492c1010edaSGreg Roach    {
1493a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1494a6f13a4aSGreg Roach            $this->process_ifs++;
1495a6f13a4aSGreg Roach
1496a6f13a4aSGreg Roach            return;
1497a6f13a4aSGreg Roach        }
1498a6f13a4aSGreg Roach
1499a6f13a4aSGreg Roach        $condition = $attrs['condition'];
150082759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1501c1010edaSGreg Roach        $condition = str_replace([
1502c1010edaSGreg Roach            ' LT ',
1503c1010edaSGreg Roach            ' GT ',
1504c1010edaSGreg Roach        ], [
1505c1010edaSGreg Roach            '<',
1506c1010edaSGreg Roach            '>',
1507c1010edaSGreg Roach        ], $condition);
1508a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15097a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
151013abd6f3SGreg Roach        $match     = [];
1511a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1512a6f13a4aSGreg Roach        $i         = 0;
1513a6f13a4aSGreg Roach        while ($i < $count) {
1514a6f13a4aSGreg Roach            $id    = $match[$i][1];
1515a6f13a4aSGreg Roach            $value = '""';
1516044416d2SGreg Roach            if ($id === 'ID') {
15177a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1518a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1519a6f13a4aSGreg Roach                }
15207a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1521a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15227a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1523a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15247a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1525a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1526a6f13a4aSGreg Roach            } else {
15277a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1528a6f13a4aSGreg Roach                $level = $temp[0];
1529a6f13a4aSGreg Roach                if ($level == 0) {
1530a6f13a4aSGreg Roach                    $level++;
1531a6f13a4aSGreg Roach                }
15323d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1533a6f13a4aSGreg Roach                if (empty($value)) {
1534a6f13a4aSGreg Roach                    $level++;
15353d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1536a6f13a4aSGreg Roach                }
15378d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15385e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1539a6f13a4aSGreg Roach            }
1540a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1541a6f13a4aSGreg Roach            $i++;
1542a6f13a4aSGreg Roach        }
15435809450fSGreg Roach
1544cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1545cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1546c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1547c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1548cb63a60eSGreg Roach
1549cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15505809450fSGreg Roach
1551a6f13a4aSGreg Roach        if (!$ret) {
1552a6f13a4aSGreg Roach            $this->process_ifs++;
1553a6f13a4aSGreg Roach        }
1554a6f13a4aSGreg Roach    }
1555a6f13a4aSGreg Roach
1556a6f13a4aSGreg Roach    /**
1557a6f13a4aSGreg Roach     * XML <if /> end element
15588ba2e626SGreg Roach     *
15598ba2e626SGreg Roach     * @return void
1560a6f13a4aSGreg Roach     */
1561b702978eSGreg Roach    protected function ifEndHandler(): void
1562c1010edaSGreg Roach    {
1563a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1564a6f13a4aSGreg Roach            $this->process_ifs--;
1565a6f13a4aSGreg Roach        }
1566a6f13a4aSGreg Roach    }
1567a6f13a4aSGreg Roach
1568a6f13a4aSGreg Roach    /**
1569a6f13a4aSGreg Roach     * XML <Footnote > start element
1570a6f13a4aSGreg Roach     * Collect the Footnote links
1571a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1572a6f13a4aSGreg Roach     *
1573c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15748ba2e626SGreg Roach     *
15758ba2e626SGreg Roach     * @return void
1576a6f13a4aSGreg Roach     */
1577b702978eSGreg Roach    protected function footnoteStartHandler(array $attrs): void
1578c1010edaSGreg Roach    {
15797a6ee1acSGreg Roach        $id = '';
15807a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1581a6f13a4aSGreg Roach            $id = $match[2];
1582a6f13a4aSGreg Roach        }
1583299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1584a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15859b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1586a6f13a4aSGreg Roach            $this->print_data         = true;
15877a6ee1acSGreg Roach            $style                    = '';
1588a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1589a6f13a4aSGreg Roach                $style = $attrs['style'];
1590a6f13a4aSGreg Roach            }
1591a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1592e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1593a6f13a4aSGreg Roach        } else {
1594a6f13a4aSGreg Roach            $this->print_data       = false;
1595a6f13a4aSGreg Roach            $this->process_footnote = false;
1596a6f13a4aSGreg Roach        }
1597a6f13a4aSGreg Roach    }
1598a6f13a4aSGreg Roach
1599a6f13a4aSGreg Roach    /**
1600a6f13a4aSGreg Roach     * XML <Footnote /> end element
1601a6f13a4aSGreg Roach     * Print the collected Footnote data
16028ba2e626SGreg Roach     *
16038ba2e626SGreg Roach     * @return void
1604a6f13a4aSGreg Roach     */
1605b702978eSGreg Roach    protected function footnoteEndHandler(): void
1606c1010edaSGreg Roach    {
1607a6f13a4aSGreg Roach        if ($this->process_footnote) {
1608a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1609a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1610a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1611e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1612a6f13a4aSGreg Roach            }
1613a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1614a6f13a4aSGreg Roach        } else {
1615a6f13a4aSGreg Roach            $this->process_footnote = true;
1616a6f13a4aSGreg Roach        }
1617a6f13a4aSGreg Roach    }
1618a6f13a4aSGreg Roach
1619a6f13a4aSGreg Roach    /**
1620a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16218ba2e626SGreg Roach     *
16228ba2e626SGreg Roach     * @return void
1623a6f13a4aSGreg Roach     */
1624b702978eSGreg Roach    protected function footnoteTextsStartHandler(): void
1625c1010edaSGreg Roach    {
16267a6ee1acSGreg Roach        $temp = 'footnotetexts';
1627e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1628a6f13a4aSGreg Roach    }
1629a6f13a4aSGreg Roach
1630a6f13a4aSGreg Roach    /**
1631a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16328ba2e626SGreg Roach     *
16338ba2e626SGreg Roach     * @return void
1634a6f13a4aSGreg Roach     */
1635b702978eSGreg Roach    protected function brStartHandler(): void
1636c1010edaSGreg Roach    {
1637a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1638a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1639a6f13a4aSGreg Roach        }
1640a6f13a4aSGreg Roach    }
1641a6f13a4aSGreg Roach
1642a6f13a4aSGreg Roach    /**
1643a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16448ba2e626SGreg Roach     *
16458ba2e626SGreg Roach     * @return void
1646a6f13a4aSGreg Roach     */
1647b702978eSGreg Roach    protected function spStartHandler(): void
1648c1010edaSGreg Roach    {
1649a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1650a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1651a6f13a4aSGreg Roach        }
1652a6f13a4aSGreg Roach    }
1653a6f13a4aSGreg Roach
1654a6f13a4aSGreg Roach    /**
165576692c8bSGreg Roach     * XML <HighlightedImage/>
165676692c8bSGreg Roach     *
1657c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16588ba2e626SGreg Roach     *
16598ba2e626SGreg Roach     * @return void
1660a6f13a4aSGreg Roach     */
1661b702978eSGreg Roach    protected function highlightedImageStartHandler(array $attrs): void
1662c1010edaSGreg Roach    {
1663a6f13a4aSGreg Roach        $id = '';
16647a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1665a6f13a4aSGreg Roach            $id = $match[1];
1666a6f13a4aSGreg Roach        }
1667a6f13a4aSGreg Roach
1668c21bdddcSGreg Roach        // Position the top corner of this box on the page
1669c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1670a6f13a4aSGreg Roach
1671c21bdddcSGreg Roach        // Position the left corner of this box on the page
1672c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1673a6f13a4aSGreg Roach
167483cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167583cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1676a6f13a4aSGreg Roach
1677a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
167883cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1679a6f13a4aSGreg Roach
168083cdc021SGreg Roach        // Width, height (or both).
1681c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1682c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1683a6f13a4aSGreg Roach
1684299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16854a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
168686a63f51SGreg Roach
168786a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1688c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1689c1010edaSGreg Roach                0,
1690c1010edaSGreg Roach                0,
1691c1010edaSGreg Roach            ];
1692a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16933c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16943c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1695a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16963c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16973c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1698a6f13a4aSGreg Roach            } else {
16993c3b90deSGreg Roach                $width  = $attributes[0];
17003c3b90deSGreg Roach                $height = $attributes[1];
1701a6f13a4aSGreg Roach            }
17024a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1703e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1704a6f13a4aSGreg Roach        }
1705a6f13a4aSGreg Roach    }
1706a6f13a4aSGreg Roach
1707a6f13a4aSGreg Roach    /**
170876692c8bSGreg Roach     * XML <Image/>
170976692c8bSGreg Roach     *
1710c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17118ba2e626SGreg Roach     *
17128ba2e626SGreg Roach     * @return void
1713a6f13a4aSGreg Roach     */
1714b702978eSGreg Roach    protected function imageStartHandler(array $attrs): void
1715c1010edaSGreg Roach    {
171683cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1717c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1718a6f13a4aSGreg Roach
1719a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1720c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1721a6f13a4aSGreg Roach
172283cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172383cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1724a6f13a4aSGreg Roach
1725a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172683cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1727a6f13a4aSGreg Roach
172883cdc021SGreg Roach        // Width, height (or both).
1729c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1730c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1731a6f13a4aSGreg Roach
173283cdc021SGreg Roach        $file = $attrs['file'] ?? '';
173383cdc021SGreg Roach
1734044416d2SGreg Roach        if ($file === '@FILE') {
173513abd6f3SGreg Roach            $match = [];
1736a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1737299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17384a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1739cdf416fbSGreg Roach
17404a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1741c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1742c1010edaSGreg Roach                        0,
1743c1010edaSGreg Roach                        0,
1744c1010edaSGreg Roach                    ];
1745a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17463c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17473c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1748a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17493c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17503c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1751a6f13a4aSGreg Roach                    } else {
17523c3b90deSGreg Roach                        $width  = $attributes[0];
17533c3b90deSGreg Roach                        $height = $attributes[1];
1754a6f13a4aSGreg Roach                    }
17554a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1756e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1757a6f13a4aSGreg Roach                }
1758a6f13a4aSGreg Roach            }
1759a6f13a4aSGreg Roach        } else {
17607a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1761a6f13a4aSGreg Roach                $size = getimagesize($file);
1762a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1763a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1764a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1765a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1766a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1767a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1768a6f13a4aSGreg Roach                } else {
1769a6f13a4aSGreg Roach                    $width  = $size[0];
1770a6f13a4aSGreg Roach                    $height = $size[1];
1771a6f13a4aSGreg Roach                }
1772e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1773e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1774a6f13a4aSGreg Roach            }
1775a6f13a4aSGreg Roach        }
1776a6f13a4aSGreg Roach    }
1777a6f13a4aSGreg Roach
1778a6f13a4aSGreg Roach    /**
1779a6f13a4aSGreg Roach     * XML <Line> element handler
1780a6f13a4aSGreg Roach     *
1781c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17828ba2e626SGreg Roach     *
17838ba2e626SGreg Roach     * @return void
1784a6f13a4aSGreg Roach     */
1785b702978eSGreg Roach    protected function lineStartHandler(array $attrs): void
1786c1010edaSGreg Roach    {
1787a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1788c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1789a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17907a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1791a6f13a4aSGreg Roach                $x1 = 0;
17927a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1793c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1794a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1795c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1796a6f13a4aSGreg Roach            }
1797a6f13a4aSGreg Roach        }
1798a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1799c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1800a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
18017a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1802a6f13a4aSGreg Roach                $y1 = 0;
18037a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1804c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1805a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1806c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1807a6f13a4aSGreg Roach            }
1808a6f13a4aSGreg Roach        }
1809a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1810c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1811a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18127a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1813a6f13a4aSGreg Roach                $x2 = 0;
18147a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1815c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1816a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1817c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1818a6f13a4aSGreg Roach            }
1819a6f13a4aSGreg Roach        }
1820a6f13a4aSGreg Roach        // End vertical position
1821c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1822a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18237a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1824a6f13a4aSGreg Roach                $y2 = 0;
18257a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1826c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1827a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1828c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1829a6f13a4aSGreg Roach            }
1830a6f13a4aSGreg Roach        }
1831a6f13a4aSGreg Roach
1832e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1833e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1834a6f13a4aSGreg Roach    }
1835a6f13a4aSGreg Roach
1836a6f13a4aSGreg Roach    /**
183776692c8bSGreg Roach     * XML <List>
1838a6f13a4aSGreg Roach     *
1839c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18408ba2e626SGreg Roach     *
18418ba2e626SGreg Roach     * @return void
1842a6f13a4aSGreg Roach     */
1843b702978eSGreg Roach    protected function listStartHandler(array $attrs): void
1844c1010edaSGreg Roach    {
1845a6f13a4aSGreg Roach        $this->process_repeats++;
1846a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1847a6f13a4aSGreg Roach            return;
1848a6f13a4aSGreg Roach        }
1849a6f13a4aSGreg Roach
185013abd6f3SGreg Roach        $match = [];
1851a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1852a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1853a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1854d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1855a6f13a4aSGreg Roach                $sortby = trim($sortby);
1856a6f13a4aSGreg Roach            }
1857a6f13a4aSGreg Roach        } else {
18587a6ee1acSGreg Roach            $sortby = 'NAME';
1859a6f13a4aSGreg Roach        }
1860a6f13a4aSGreg Roach
1861e364afe4SGreg Roach        $listname = $attrs['list'] ?? 'individual';
1862195b5e75SGreg Roach
1863a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1864a6f13a4aSGreg Roach        switch ($listname) {
18657a6ee1acSGreg Roach            case 'pending':
18666c0bef3aSGreg Roach                $xrefs = DB::table('change')
1867195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1868a69f5655SGreg Roach                        $query->select(new Expression('MAX(change_id)'))
1869195b5e75SGreg Roach                            ->from('change')
1870195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1871195b5e75SGreg Roach                            ->where('status', '=', 'pending')
18727f5c2944SGreg Roach                            ->groupBy(['xref']);
1873195b5e75SGreg Roach                    })
18746c0bef3aSGreg Roach                    ->pluck('xref');
1875195b5e75SGreg Roach
187613abd6f3SGreg Roach                $this->list = [];
18776c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18786c0bef3aSGreg Roach                    $this->list[] = GedcomRecord::getInstance($xref, $this->tree);
1879a6f13a4aSGreg Roach                }
1880a6f13a4aSGreg Roach                break;
1881a6f13a4aSGreg Roach            case 'individual':
18825985adfbSGreg Roach                $query = DB::table('individuals')
18835985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18845985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18855985adfbSGreg Roach                    ->distinct();
18865985adfbSGreg Roach
1887a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1888a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
188982759250SGreg Roach                        $value = $this->substituteVars($value, false);
1890a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1891a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
18920b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
18935985adfbSGreg Roach                                $join
18945985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
18955985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
18965985adfbSGreg Roach                            });
18975985adfbSGreg Roach
18985985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
18995985adfbSGreg Roach
1900a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19015985adfbSGreg Roach
1902044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19035985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1904a6f13a4aSGreg Roach                            } else {
19055985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1906a6f13a4aSGreg Roach                            }
19075985adfbSGreg Roach
19085985adfbSGreg Roach                            // This filter has been fully processed
19095985adfbSGreg Roach                            unset($attrs[$attr]);
19107ee4bfadSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19110b5fd0a6SGreg Roach                            $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
19125985adfbSGreg Roach                                $join
19135985adfbSGreg Roach                                    ->on($attr . '.n_id', '=', 'i_id')
19145985adfbSGreg Roach                                    ->on($attr . '.n_file', '=', 'i_file');
19155985adfbSGreg Roach                            });
1916a6f13a4aSGreg Roach                            // Search the DB only if there is any name supplied
19177a6ee1acSGreg Roach                            $names = explode(' ', $match[1]);
19185d0bc43dSGreg Roach                            foreach ($names as $n => $name) {
1919fbbe964bSGreg Roach                                $query->whereContains($attr . '.n_full', $name);
1920a6f13a4aSGreg Roach                            }
19215985adfbSGreg Roach
19225985adfbSGreg Roach                            // This filter has been fully processed
19235985adfbSGreg Roach                            unset($attrs[$attr]);
1924a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
19255985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1926a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
19275985adfbSGreg Roach
1928a1afa4f8SGreg Roach                            $query->where('i_gedcom', 'LIKE', $match[1]);
19295985adfbSGreg Roach
19305985adfbSGreg Roach                            // This filter has been fully processed
19315985adfbSGreg Roach                            unset($attrs[$attr]);
19322fac69aeSGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
19339dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19345985adfbSGreg Roach                            $query
19350b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
19365985adfbSGreg Roach                                    $join
19377ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'i_file')
19387ee4bfadSGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'i_id');
19395985adfbSGreg Roach                                })
19400b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
19417ee4bfadSGreg Roach                                    $join
19427ee4bfadSGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
19437ee4bfadSGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
19447ee4bfadSGreg Roach                                })
19457ee4bfadSGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
19466e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
19475985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19487ee4bfadSGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
19496e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
19506e5c0963SGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
19516e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
19526e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19536e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
1954e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
19557ee4bfadSGreg Roach                            $query->where('i_gedcom', 'LIKE', $like);
1956a6f13a4aSGreg Roach                        }
1957a6f13a4aSGreg Roach                    }
1958a6f13a4aSGreg Roach                }
1959a6f13a4aSGreg Roach
196013abd6f3SGreg Roach                $this->list = [];
1961a6f13a4aSGreg Roach
19625985adfbSGreg Roach                foreach ($query->get() as $row) {
1963299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1964a6f13a4aSGreg Roach                }
1965a6f13a4aSGreg Roach                break;
1966a6f13a4aSGreg Roach
1967a6f13a4aSGreg Roach            case 'family':
196830fc2b1eSGreg Roach                $query = DB::table('families')
196930fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
197030fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
197130fc2b1eSGreg Roach                    ->distinct();
197230fc2b1eSGreg Roach
1973a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1974a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
197582759250SGreg Roach                        $value = $this->substituteVars($value, false);
1976a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1977a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
19780b5fd0a6SGreg Roach                            $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void {
197930fc2b1eSGreg Roach                                $join
198030fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
198130fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
198230fc2b1eSGreg Roach                            });
198330fc2b1eSGreg Roach
198430fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
198530fc2b1eSGreg Roach
1986a6f13a4aSGreg Roach                            $date = new Date($match[3]);
198730fc2b1eSGreg Roach
1988044416d2SGreg Roach                            if ($match[2] === 'LTE') {
198930fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1990a6f13a4aSGreg Roach                            } else {
199130fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1992a6f13a4aSGreg Roach                            }
199330fc2b1eSGreg Roach
199430fc2b1eSGreg Roach                            // This filter has been fully processed
199530fc2b1eSGreg Roach                            unset($attrs[$attr]);
1996a1afa4f8SGreg Roach                        } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) {
199730fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
1998a1afa4f8SGreg Roach                            $match[1] = str_replace('\n', "\n", $match[1]);
199930fc2b1eSGreg Roach
2000a1afa4f8SGreg Roach                            $query->where('f_gedcom', 'LIKE', $match[1]);
200130fc2b1eSGreg Roach
200230fc2b1eSGreg Roach                            // This filter has been fully processed
200330fc2b1eSGreg Roach                            unset($attrs[$attr]);
200430fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
20053b3cfeeaSGreg Roach                            if ($sortby === 'NAME' || $match[1] !== '') {
20060b5fd0a6SGreg Roach                                $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void {
200730fc2b1eSGreg Roach                                    $join
200830fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
20093b3cfeeaSGreg Roach                                        ->where(static function (Builder $query): void {
201030fc2b1eSGreg Roach                                            $query
201130fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
201230fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
201330fc2b1eSGreg Roach                                        });
201430fc2b1eSGreg Roach                                });
20155d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20167a6ee1acSGreg Roach                                if ($match[1] != '') {
20177a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20185d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
2019fbbe964bSGreg Roach                                        $query->whereContains($attr . '.n_full', $name);
20205d0bc43dSGreg Roach                                    }
20215d0bc43dSGreg Roach                                }
2022a6f13a4aSGreg Roach                            }
202330fc2b1eSGreg Roach
202430fc2b1eSGreg Roach                            // This filter has been fully processed
202530fc2b1eSGreg Roach                            unset($attrs[$attr]);
20266e5c0963SGreg Roach                        } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) {
20279dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
202830fc2b1eSGreg Roach                            $query
20290b5fd0a6SGreg Roach                                ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void {
203030fc2b1eSGreg Roach                                    $join
20316e5c0963SGreg Roach                                        ->on($attr . 'a.pl_file', '=', 'f_file')
20326e5c0963SGreg Roach                                        ->on($attr . 'a.pl_gid', '=', 'f_id');
203330fc2b1eSGreg Roach                                })
20340b5fd0a6SGreg Roach                                ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void {
20356e5c0963SGreg Roach                                    $join
20366e5c0963SGreg Roach                                        ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file')
20376e5c0963SGreg Roach                                        ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id');
20386e5c0963SGreg Roach                                })
20396e5c0963SGreg Roach                                ->whereContains($attr . 'b.p_place', $match[1]);
20406e5c0963SGreg Roach                        } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) {
204130fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20426e5c0963SGreg Roach                            $match[3] = strtr($match[3], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
20436e5c0963SGreg Roach                            $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%';
20446e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
20456e5c0963SGreg Roach                        } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) {
20466e5c0963SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
20476e5c0963SGreg Roach                            $match[2] = strtr($match[2], ['\\' => '\\\\', '%'  => '\\%', '_'  => '\\_', ' ' => '%']);
2048e364afe4SGreg Roach                            $like = "%\n1 " . $match[1] . '%' . $match[2] . '%';
20496e5c0963SGreg Roach                            $query->where('f_gedcom', 'LIKE', $like);
2050a6f13a4aSGreg Roach                        }
2051a6f13a4aSGreg Roach                    }
2052a6f13a4aSGreg Roach                }
2053a6f13a4aSGreg Roach
205413abd6f3SGreg Roach                $this->list = [];
2055a6f13a4aSGreg Roach
205630fc2b1eSGreg Roach                foreach ($query->get() as $row) {
2057299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2058a6f13a4aSGreg Roach                }
2059a6f13a4aSGreg Roach                break;
2060a6f13a4aSGreg Roach
2061a6f13a4aSGreg Roach            default:
20626ccdf4f0SGreg Roach                throw new DomainException('Invalid list name: ' . $listname);
2063a6f13a4aSGreg Roach        }
2064a6f13a4aSGreg Roach
206513abd6f3SGreg Roach        $filters  = [];
206613abd6f3SGreg Roach        $filters2 = [];
2067a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2068a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2069a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2070a6f13a4aSGreg Roach                    $condition = $value;
2071a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2072a6f13a4aSGreg Roach                        $id    = $match[1];
2073a6f13a4aSGreg Roach                        $value = "''";
2074044416d2SGreg Roach                        if ($id === 'ID') {
20757a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2076a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2077a6f13a4aSGreg Roach                            }
2078044416d2SGreg Roach                        } elseif ($id === 'fact') {
2079a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2080044416d2SGreg Roach                        } elseif ($id === 'desc') {
2081a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2082a6f13a4aSGreg Roach                        } else {
2083a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20847a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2085a6f13a4aSGreg Roach                            }
2086a6f13a4aSGreg Roach                        }
2087a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2088a6f13a4aSGreg Roach                    }
2089a6f13a4aSGreg Roach                    //-- handle regular expressions
2090a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2091a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2092a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2093a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2094a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2095d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2096a6f13a4aSGreg Roach                            $val = trim($val);
2097a6f13a4aSGreg Roach                        }
2098a6f13a4aSGreg Roach                        if ($val) {
20997a6ee1acSGreg Roach                            $searchstr = '';
21007a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2101a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2102a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2103a6f13a4aSGreg Roach                                $level = 1;
2104a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2105a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2106a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2107a6f13a4aSGreg Roach                                    }
2108a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2109044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21107a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2111a6f13a4aSGreg Roach                                    }
21127a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2113a6f13a4aSGreg Roach                                    $level++;
2114a6f13a4aSGreg Roach                                }
2115a6f13a4aSGreg Roach                            } else {
2116044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21177a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2118a6f13a4aSGreg Roach                                }
2119a6f13a4aSGreg Roach                                $t         = $tag;
21207a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2121a6f13a4aSGreg Roach                            }
2122a6f13a4aSGreg Roach                            switch ($expr) {
21237a6ee1acSGreg Roach                                case 'CONTAINS':
2124044416d2SGreg Roach                                    if ($t === 'PLAC') {
2125a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2126a6f13a4aSGreg Roach                                    } else {
2127a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2128a6f13a4aSGreg Roach                                    }
2129a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2130a6f13a4aSGreg Roach                                    break;
2131a6f13a4aSGreg Roach                                default:
2132c1010edaSGreg Roach                                    $filters2[] = [
2133c1010edaSGreg Roach                                        'tag'  => $tag,
2134c1010edaSGreg Roach                                        'expr' => $expr,
2135c1010edaSGreg Roach                                        'val'  => $val,
2136c1010edaSGreg Roach                                    ];
2137a6f13a4aSGreg Roach                                    break;
2138a6f13a4aSGreg Roach                            }
2139a6f13a4aSGreg Roach                        }
2140a6f13a4aSGreg Roach                    }
2141a6f13a4aSGreg Roach                }
2142a6f13a4aSGreg Roach            }
2143a6f13a4aSGreg Roach        }
2144a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2145a6f13a4aSGreg Roach        if ($filters) {
2146a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2147a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2148299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2149a6f13a4aSGreg Roach                        unset($this->list[$key]);
2150a6f13a4aSGreg Roach                        break;
2151a6f13a4aSGreg Roach                    }
2152a6f13a4aSGreg Roach                }
2153a6f13a4aSGreg Roach            }
2154a6f13a4aSGreg Roach        }
2155a6f13a4aSGreg Roach        if ($filters2) {
215613abd6f3SGreg Roach            $mylist = [];
2157a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2158c0935879SGreg Roach                $key  = $indi->xref();
2159299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2160a6f13a4aSGreg Roach                $keep = true;
2161a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2162a6f13a4aSGreg Roach                    if ($keep) {
2163a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2164a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2165a6f13a4aSGreg Roach                        $val  = $filter['val'];
2166a6f13a4aSGreg Roach                        if ($val == "''") {
21677a6ee1acSGreg Roach                            $val = '';
2168a6f13a4aSGreg Roach                        }
21697a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2170a6f13a4aSGreg Roach                        $t    = end($tags);
21713d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2172a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2173044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21747a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21757a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2176a6f13a4aSGreg Roach                            $t    = end($tags);
21773d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2178a6f13a4aSGreg Roach                        }
2179a6f13a4aSGreg Roach
2180a6f13a4aSGreg Roach                        switch ($expr) {
21817a6ee1acSGreg Roach                            case 'GTE':
2182044416d2SGreg Roach                                if ($t === 'DATE') {
2183a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2184a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2185a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2186a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2187a6f13a4aSGreg Roach                                    $keep = true;
2188a6f13a4aSGreg Roach                                }
2189a6f13a4aSGreg Roach                                break;
21907a6ee1acSGreg Roach                            case 'LTE':
2191044416d2SGreg Roach                                if ($t === 'DATE') {
2192a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2193a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2194a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2195a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2196a6f13a4aSGreg Roach                                    $keep = true;
2197a6f13a4aSGreg Roach                                }
2198a6f13a4aSGreg Roach                                break;
2199a6f13a4aSGreg Roach                            default:
2200a6f13a4aSGreg Roach                                if ($v == $val) {
2201a6f13a4aSGreg Roach                                    $keep = true;
2202a6f13a4aSGreg Roach                                } else {
2203a6f13a4aSGreg Roach                                    $keep = false;
2204a6f13a4aSGreg Roach                                }
2205a6f13a4aSGreg Roach                                break;
2206a6f13a4aSGreg Roach                        }
2207a6f13a4aSGreg Roach                    }
2208a6f13a4aSGreg Roach                }
2209a6f13a4aSGreg Roach                if ($keep) {
2210a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2211a6f13a4aSGreg Roach                }
2212a6f13a4aSGreg Roach            }
2213a6f13a4aSGreg Roach            $this->list = $mylist;
2214a6f13a4aSGreg Roach        }
2215a6f13a4aSGreg Roach
2216a6f13a4aSGreg Roach        switch ($sortby) {
2217a6f13a4aSGreg Roach            case 'NAME':
2218c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2219a6f13a4aSGreg Roach                break;
2220a6f13a4aSGreg Roach            case 'CHAN':
2221c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::lastChangeComparator());
2222a6f13a4aSGreg Roach                break;
2223a6f13a4aSGreg Roach            case 'BIRT:DATE':
2224c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2225a6f13a4aSGreg Roach                break;
2226a6f13a4aSGreg Roach            case 'DEAT:DATE':
2227c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2228a6f13a4aSGreg Roach                break;
2229a6f13a4aSGreg Roach            case 'MARR:DATE':
2230c156e8f5SGreg Roach                uasort($this->list, Family::marriageDateComparator());
2231a6f13a4aSGreg Roach                break;
2232a6f13a4aSGreg Roach            default:
2233a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2234a6f13a4aSGreg Roach                break;
2235a6f13a4aSGreg Roach        }
2236a6f13a4aSGreg Roach
22379b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2238e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2239a6f13a4aSGreg Roach    }
2240a6f13a4aSGreg Roach
2241a6f13a4aSGreg Roach    /**
224276692c8bSGreg Roach     * XML <List>
22438ba2e626SGreg Roach     *
22448ba2e626SGreg Roach     * @return void
2245a6f13a4aSGreg Roach     */
2246b702978eSGreg Roach    protected function listEndHandler(): void
2247c1010edaSGreg Roach    {
2248a6f13a4aSGreg Roach        $this->process_repeats--;
2249a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2250a6f13a4aSGreg Roach            return;
2251a6f13a4aSGreg Roach        }
2252a6f13a4aSGreg Roach
2253a6f13a4aSGreg Roach        // Check if there is any list
2254a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2255a6f13a4aSGreg Roach            $lineoffset = 0;
2256a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2257a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2258a6f13a4aSGreg Roach            }
2259a6f13a4aSGreg Roach            //-- read the xml from the file
2260299d100dSGreg Roach            $lines = file($this->report);
22617a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2262a6f13a4aSGreg Roach                $lineoffset--;
2263a6f13a4aSGreg Roach            }
2264a6f13a4aSGreg Roach            $lineoffset++;
2265a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2266a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2267a6f13a4aSGreg Roach            // List Level counter
2268a6f13a4aSGreg Roach            $count = 1;
2269a6f13a4aSGreg Roach            while (0 < $count) {
22707a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2271a6f13a4aSGreg Roach                    $count++;
22727a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2273a6f13a4aSGreg Roach                    $count--;
2274a6f13a4aSGreg Roach                }
2275a6f13a4aSGreg Roach                if (0 < $count) {
2276a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2277a6f13a4aSGreg Roach                }
2278a6f13a4aSGreg Roach                $line_nr++;
2279a6f13a4aSGreg Roach            }
2280a6f13a4aSGreg Roach            // No need to drag this
2281a6f13a4aSGreg Roach            unset($lines);
22827a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2283a6f13a4aSGreg Roach            // Save original values
22849b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2285a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2286a6f13a4aSGreg Roach
2287a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2288a6f13a4aSGreg Roach            $this->list_private = 0;
2289a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2290a6f13a4aSGreg Roach                if ($record->canShow()) {
2291f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2292a6f13a4aSGreg Roach                    //-- start the sax parser
2293a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2294e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2295a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22961aa04befSGreg Roach
22971aa04befSGreg Roach                    xml_set_element_handler(
22981aa04befSGreg Roach                        $repeat_parser,
22999d454b6bSGreg Roach                        function ($parser, string $name, array $attrs): void {
23001aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
23011aa04befSGreg Roach                        },
23029d454b6bSGreg Roach                        function ($parser, string $name): void {
23031aa04befSGreg Roach                            $this->endElement($parser, $name);
23041aa04befSGreg Roach                        }
23051aa04befSGreg Roach                    );
23061aa04befSGreg Roach
23071aa04befSGreg Roach                    xml_set_character_data_handler(
23081aa04befSGreg Roach                        $repeat_parser,
23099d454b6bSGreg Roach                        function ($parser, string $data): void {
23101aa04befSGreg Roach                            $this->characterData($parser, $data);
23111aa04befSGreg Roach                        }
23121aa04befSGreg Roach                    );
23131aa04befSGreg Roach
2314a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
23156ccdf4f0SGreg Roach                        throw new DomainException(sprintf(
2316a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2317a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2318a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2319a6f13a4aSGreg Roach                        ));
2320a6f13a4aSGreg Roach                    }
2321a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2322a6f13a4aSGreg Roach                } else {
2323a6f13a4aSGreg Roach                    $this->list_private++;
2324a6f13a4aSGreg Roach                }
2325a6f13a4aSGreg Roach            }
232613abd6f3SGreg Roach            $this->list   = [];
2327e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2328a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2329a6f13a4aSGreg Roach        }
233065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2331a6f13a4aSGreg Roach    }
2332a6f13a4aSGreg Roach
2333a6f13a4aSGreg Roach    /**
2334a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2335a6f13a4aSGreg Roach     * Prints the total number of records in a list
2336a6f13a4aSGreg Roach     * The total number is collected from
2337a6f13a4aSGreg Roach     * List and Relatives
23388ba2e626SGreg Roach     *
23398ba2e626SGreg Roach     * @return void
2340a6f13a4aSGreg Roach     */
2341b702978eSGreg Roach    protected function listTotalStartHandler(): void
2342c1010edaSGreg Roach    {
2343a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2344589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2345a6f13a4aSGreg Roach        } else {
23467a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2347a6f13a4aSGreg Roach        }
2348a6f13a4aSGreg Roach    }
2349a6f13a4aSGreg Roach
2350a6f13a4aSGreg Roach    /**
235176692c8bSGreg Roach     * XML <Relatives>
235276692c8bSGreg Roach     *
2353c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23548ba2e626SGreg Roach     *
23558ba2e626SGreg Roach     * @return void
2356a6f13a4aSGreg Roach     */
2357b702978eSGreg Roach    protected function relativesStartHandler(array $attrs): void
2358c1010edaSGreg Roach    {
2359a6f13a4aSGreg Roach        $this->process_repeats++;
2360a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2361a6f13a4aSGreg Roach            return;
2362a6f13a4aSGreg Roach        }
2363a6f13a4aSGreg Roach
2364e364afe4SGreg Roach        $sortby = $attrs['sortby'] ?? 'NAME';
2365e364afe4SGreg Roach
236613abd6f3SGreg Roach        $match = [];
2367a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2368d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2369a6f13a4aSGreg Roach            $sortby = trim($sortby);
2370a6f13a4aSGreg Roach        }
2371a6f13a4aSGreg Roach
2372a6f13a4aSGreg Roach        $maxgen = -1;
2373a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2374c0624077SGreg Roach            $maxgen = (int) $attrs['maxgen'];
2375a6f13a4aSGreg Roach        }
2376a6f13a4aSGreg Roach
2377e364afe4SGreg Roach        $group = $attrs['group'] ?? 'child-family';
2378e364afe4SGreg Roach
2379a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2380d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2381a6f13a4aSGreg Roach            $group = trim($group);
2382a6f13a4aSGreg Roach        }
2383a6f13a4aSGreg Roach
2384e364afe4SGreg Roach        $id = $attrs['id'] ?? '';
2385e364afe4SGreg Roach
2386a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2387d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2388a6f13a4aSGreg Roach            $id = trim($id);
2389a6f13a4aSGreg Roach        }
2390a6f13a4aSGreg Roach
239113abd6f3SGreg Roach        $this->list = [];
2392299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2393d965cc1aSGreg Roach        if ($person instanceof Individual) {
2394a6f13a4aSGreg Roach            $this->list[$id] = $person;
2395a6f13a4aSGreg Roach            switch ($group) {
23967a6ee1acSGreg Roach                case 'child-family':
239739ca88baSGreg Roach                    foreach ($person->childFamilies() as $family) {
2398820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2399820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2400a6f13a4aSGreg Roach                        }
2401820b62dfSGreg Roach
2402820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2403c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2404a6f13a4aSGreg Roach                        }
2405a6f13a4aSGreg Roach                    }
2406a6f13a4aSGreg Roach                    break;
24077a6ee1acSGreg Roach                case 'spouse-family':
240839ca88baSGreg Roach                    foreach ($person->spouseFamilies() as $family) {
2409820b62dfSGreg Roach                        foreach ($family->spouses() as $spouse) {
2410820b62dfSGreg Roach                            $this->list[$spouse->xref()] = $spouse;
2411a6f13a4aSGreg Roach                        }
2412820b62dfSGreg Roach
2413820b62dfSGreg Roach                        foreach ($family->children() as $child) {
2414c0935879SGreg Roach                            $this->list[$child->xref()] = $child;
2415a6f13a4aSGreg Roach                        }
2416a6f13a4aSGreg Roach                    }
2417a6f13a4aSGreg Roach                    break;
24187a6ee1acSGreg Roach                case 'direct-ancestors':
24193d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2420a6f13a4aSGreg Roach                    break;
24217a6ee1acSGreg Roach                case 'ancestors':
24223d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2423a6f13a4aSGreg Roach                    break;
24247a6ee1acSGreg Roach                case 'descendants':
2425a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24263d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2427a6f13a4aSGreg Roach                    break;
24287a6ee1acSGreg Roach                case 'all':
24293d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24303d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2431a6f13a4aSGreg Roach                    break;
2432a6f13a4aSGreg Roach            }
2433a6f13a4aSGreg Roach        }
2434a6f13a4aSGreg Roach
2435a6f13a4aSGreg Roach        switch ($sortby) {
2436a6f13a4aSGreg Roach            case 'NAME':
2437c156e8f5SGreg Roach                uasort($this->list, GedcomRecord::nameComparator());
2438a6f13a4aSGreg Roach                break;
2439a6f13a4aSGreg Roach            case 'BIRT:DATE':
2440c156e8f5SGreg Roach                uasort($this->list, Individual::birthDateComparator());
2441a6f13a4aSGreg Roach                break;
2442a6f13a4aSGreg Roach            case 'DEAT:DATE':
2443c156e8f5SGreg Roach                uasort($this->list, Individual::deathDateComparator());
2444a6f13a4aSGreg Roach                break;
2445a6f13a4aSGreg Roach            case 'generation':
244613abd6f3SGreg Roach                $newarray = [];
2447a6f13a4aSGreg Roach                reset($this->list);
2448a6f13a4aSGreg Roach                $genCounter = 1;
2449a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2450a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2451a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2452a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
245379529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2454a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2455a6f13a4aSGreg Roach                        }
2456a6f13a4aSGreg Roach                    }
2457a6f13a4aSGreg Roach                    $genCounter++;
2458a6f13a4aSGreg Roach                }
2459a6f13a4aSGreg Roach                $this->list = $newarray;
2460a6f13a4aSGreg Roach                break;
2461a6f13a4aSGreg Roach            default:
2462a6f13a4aSGreg Roach                // unsorted
2463a6f13a4aSGreg Roach                break;
2464a6f13a4aSGreg Roach        }
24659b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2466e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2467a6f13a4aSGreg Roach    }
2468a6f13a4aSGreg Roach
2469a6f13a4aSGreg Roach    /**
247076692c8bSGreg Roach     * XML </ Relatives>
24718ba2e626SGreg Roach     *
24728ba2e626SGreg Roach     * @return void
2473a6f13a4aSGreg Roach     */
2474b702978eSGreg Roach    protected function relativesEndHandler(): void
2475c1010edaSGreg Roach    {
2476a6f13a4aSGreg Roach        $this->process_repeats--;
2477a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2478a6f13a4aSGreg Roach            return;
2479a6f13a4aSGreg Roach        }
2480a6f13a4aSGreg Roach
2481a6f13a4aSGreg Roach        // Check if there is any relatives
2482a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2483a6f13a4aSGreg Roach            $lineoffset = 0;
2484a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2485a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2486a6f13a4aSGreg Roach            }
2487a6f13a4aSGreg Roach            //-- read the xml from the file
2488299d100dSGreg Roach            $lines = file($this->report);
24897a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2490a6f13a4aSGreg Roach                $lineoffset--;
2491a6f13a4aSGreg Roach            }
2492a6f13a4aSGreg Roach            $lineoffset++;
2493a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2494a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2495a6f13a4aSGreg Roach            // Relatives Level counter
2496a6f13a4aSGreg Roach            $count = 1;
2497a6f13a4aSGreg Roach            while (0 < $count) {
24987a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2499a6f13a4aSGreg Roach                    $count++;
25007a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2501a6f13a4aSGreg Roach                    $count--;
2502a6f13a4aSGreg Roach                }
2503a6f13a4aSGreg Roach                if (0 < $count) {
2504a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2505a6f13a4aSGreg Roach                }
2506a6f13a4aSGreg Roach                $line_nr++;
2507a6f13a4aSGreg Roach            }
2508a6f13a4aSGreg Roach            // No need to drag this
2509a6f13a4aSGreg Roach            unset($lines);
2510a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2511a6f13a4aSGreg Roach            // Save original values
25129b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2513a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2514a6f13a4aSGreg Roach
2515a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2516a6f13a4aSGreg Roach            $this->list_private = 0;
2517b092a991SGreg Roach            foreach ($this->list as $xref => $value) {
2518a6f13a4aSGreg Roach                if (isset($value->generation)) {
2519a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2520a6f13a4aSGreg Roach                }
2521b092a991SGreg Roach                $tmp          = GedcomRecord::getInstance((string) $xref, $this->tree);
2522299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2523a6f13a4aSGreg Roach
2524a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2525e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2526a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25271aa04befSGreg Roach
25281aa04befSGreg Roach                xml_set_element_handler(
25291aa04befSGreg Roach                    $repeat_parser,
25309d454b6bSGreg Roach                    function ($parser, string $name, array $attrs): void {
25311aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25321aa04befSGreg Roach                    },
25339d454b6bSGreg Roach                    function ($parser, string $name): void {
25341aa04befSGreg Roach                        $this->endElement($parser, $name);
25351aa04befSGreg Roach                    }
25361aa04befSGreg Roach                );
25371aa04befSGreg Roach
25381aa04befSGreg Roach                xml_set_character_data_handler(
25391aa04befSGreg Roach                    $repeat_parser,
25409d454b6bSGreg Roach                    function ($parser, string $data): void {
25411aa04befSGreg Roach                        $this->characterData($parser, $data);
25421aa04befSGreg Roach                    }
25431aa04befSGreg Roach                );
2544a6f13a4aSGreg Roach
2545a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25466ccdf4f0SGreg 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)));
2547a6f13a4aSGreg Roach                }
2548a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2549a6f13a4aSGreg Roach            }
2550a6f13a4aSGreg Roach            // Clean up the list array
255113abd6f3SGreg Roach            $this->list   = [];
2552e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2553a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2554a6f13a4aSGreg Roach        }
255565e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2556a6f13a4aSGreg Roach    }
2557a6f13a4aSGreg Roach
2558a6f13a4aSGreg Roach    /**
2559a6f13a4aSGreg Roach     * XML <Generation /> element handler
2560a6f13a4aSGreg Roach     * Prints the number of generations
25618ba2e626SGreg Roach     *
25628ba2e626SGreg Roach     * @return void
2563a6f13a4aSGreg Roach     */
2564b702978eSGreg Roach    protected function generationStartHandler(): void
2565c1010edaSGreg Roach    {
2566589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2567a6f13a4aSGreg Roach    }
2568a6f13a4aSGreg Roach
2569a6f13a4aSGreg Roach    /**
2570a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2571a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25728ba2e626SGreg Roach     *
25738ba2e626SGreg Roach     * @return void
2574a6f13a4aSGreg Roach     */
2575b702978eSGreg Roach    protected function newPageStartHandler(): void
2576c1010edaSGreg Roach    {
25777a6ee1acSGreg Roach        $temp = 'addpage';
2578e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2579a6f13a4aSGreg Roach    }
2580a6f13a4aSGreg Roach
2581a6f13a4aSGreg Roach    /**
258276692c8bSGreg Roach     * XML <html>
258376692c8bSGreg Roach     *
2584a6f13a4aSGreg Roach     * @param string   $tag   HTML tag name
2585c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
25868ba2e626SGreg Roach     *
25878ba2e626SGreg Roach     * @return void
2588a6f13a4aSGreg Roach     */
2589b702978eSGreg Roach    protected function htmlStartHandler(string $tag, array $attrs): void
2590c1010edaSGreg Roach    {
25917a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2592a6f13a4aSGreg Roach            return;
2593a6f13a4aSGreg Roach        }
25949b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2595e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2596e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2597a6f13a4aSGreg Roach
25989b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2599a6f13a4aSGreg Roach        $this->print_data         = true;
2600a6f13a4aSGreg Roach    }
2601a6f13a4aSGreg Roach
2602a6f13a4aSGreg Roach    /**
260376692c8bSGreg Roach     * XML </html>
260476692c8bSGreg Roach     *
2605a6f13a4aSGreg Roach     * @param string $tag
26068ba2e626SGreg Roach     *
26078ba2e626SGreg Roach     * @return void
2608a6f13a4aSGreg Roach     */
2609b702978eSGreg Roach    protected function htmlEndHandler($tag): void
2610c1010edaSGreg Roach    {
26117a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2612a6f13a4aSGreg Roach            return;
2613a6f13a4aSGreg Roach        }
2614a6f13a4aSGreg Roach
2615a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2616e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2617e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
26188f038c36SRico Sonntag        if ($this->wt_report !== null) {
2619e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2620a6f13a4aSGreg Roach        } else {
2621e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2622a6f13a4aSGreg Roach        }
2623a6f13a4aSGreg Roach    }
2624a6f13a4aSGreg Roach
2625a6f13a4aSGreg Roach    /**
2626a6f13a4aSGreg Roach     * Handle <Input>
26278ba2e626SGreg Roach     *
26288ba2e626SGreg Roach     * @return void
2629a6f13a4aSGreg Roach     */
2630b702978eSGreg Roach    protected function inputStartHandler(): void
2631c1010edaSGreg Roach    {
2632a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2633a6f13a4aSGreg Roach    }
2634a6f13a4aSGreg Roach
2635a6f13a4aSGreg Roach    /**
2636a6f13a4aSGreg Roach     * Handle </Input>
26378ba2e626SGreg Roach     *
26388ba2e626SGreg Roach     * @return void
2639a6f13a4aSGreg Roach     */
2640b702978eSGreg Roach    protected function inputEndHandler(): void
2641c1010edaSGreg Roach    {
2642a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2643a6f13a4aSGreg Roach    }
2644a6f13a4aSGreg Roach
2645a6f13a4aSGreg Roach    /**
2646a6f13a4aSGreg Roach     * Handle <Report>
26478ba2e626SGreg Roach     *
26488ba2e626SGreg Roach     * @return void
2649a6f13a4aSGreg Roach     */
2650b702978eSGreg Roach    protected function reportStartHandler(): void
2651c1010edaSGreg Roach    {
2652a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2653a6f13a4aSGreg Roach    }
2654a6f13a4aSGreg Roach
2655a6f13a4aSGreg Roach    /**
2656a6f13a4aSGreg Roach     * Handle </Report>
26578ba2e626SGreg Roach     *
26588ba2e626SGreg Roach     * @return void
2659a6f13a4aSGreg Roach     */
2660b702978eSGreg Roach    protected function reportEndHandler(): void
2661c1010edaSGreg Roach    {
2662a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2663a6f13a4aSGreg Roach    }
2664a6f13a4aSGreg Roach
2665a6f13a4aSGreg Roach    /**
266676692c8bSGreg Roach     * XML </titleEndHandler>
26678ba2e626SGreg Roach     *
26688ba2e626SGreg Roach     * @return void
2669a6f13a4aSGreg Roach     */
2670b702978eSGreg Roach    protected function titleEndHandler(): void
2671c1010edaSGreg Roach    {
26722836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2673a6f13a4aSGreg Roach    }
2674a6f13a4aSGreg Roach
2675a6f13a4aSGreg Roach    /**
267676692c8bSGreg Roach     * XML </descriptionEndHandler>
26778ba2e626SGreg Roach     *
26788ba2e626SGreg Roach     * @return void
2679a6f13a4aSGreg Roach     */
2680b702978eSGreg Roach    protected function descriptionEndHandler(): void
2681c1010edaSGreg Roach    {
26822836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2683a6f13a4aSGreg Roach    }
2684729ce104SGreg Roach
2685729ce104SGreg Roach    /**
268676692c8bSGreg Roach     * Create a list of all descendants.
268776692c8bSGreg Roach     *
2688729ce104SGreg Roach     * @param string[] $list
2689729ce104SGreg Roach     * @param string   $pid
2690729ce104SGreg Roach     * @param bool     $parents
2691729ce104SGreg Roach     * @param int      $generations
26928ba2e626SGreg Roach     *
26938ba2e626SGreg Roach     * @return void
2694729ce104SGreg Roach     */
26953b3cfeeaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void
2696c1010edaSGreg Roach    {
2697299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2698729ce104SGreg Roach        if ($person === null) {
2699729ce104SGreg Roach            return;
2700729ce104SGreg Roach        }
2701729ce104SGreg Roach        if (!isset($list[$pid])) {
2702729ce104SGreg Roach            $list[$pid] = $person;
2703729ce104SGreg Roach        }
2704729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2705729ce104SGreg Roach            $list[$pid]->generation = 0;
2706729ce104SGreg Roach        }
270739ca88baSGreg Roach        foreach ($person->spouseFamilies() as $family) {
2708729ce104SGreg Roach            if ($parents) {
270939ca88baSGreg Roach                $husband = $family->husband();
271039ca88baSGreg Roach                $wife    = $family->wife();
2711729ce104SGreg Roach                if ($husband) {
2712c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2713729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2714c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2715729ce104SGreg Roach                    } else {
2716c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2717729ce104SGreg Roach                    }
2718729ce104SGreg Roach                }
2719729ce104SGreg Roach                if ($wife) {
2720c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2721729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2722c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2723729ce104SGreg Roach                    } else {
2724c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2725729ce104SGreg Roach                    }
2726729ce104SGreg Roach                }
2727729ce104SGreg Roach            }
2728820b62dfSGreg Roach
272939ca88baSGreg Roach            $children = $family->children();
2730820b62dfSGreg Roach
2731729ce104SGreg Roach            foreach ($children as $child) {
2732729ce104SGreg Roach                if ($child) {
2733c0935879SGreg Roach                    $list[$child->xref()] = $child;
2734820b62dfSGreg Roach
2735729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2736c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2737729ce104SGreg Roach                    } else {
2738c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2739729ce104SGreg Roach                    }
2740729ce104SGreg Roach                }
2741729ce104SGreg Roach            }
2742729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2743729ce104SGreg Roach                foreach ($children as $child) {
2744c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2745729ce104SGreg Roach                }
2746729ce104SGreg Roach            }
2747729ce104SGreg Roach        }
2748729ce104SGreg Roach    }
2749729ce104SGreg Roach
2750729ce104SGreg Roach    /**
275176692c8bSGreg Roach     * Create a list of all ancestors.
275276692c8bSGreg Roach     *
2753729ce104SGreg Roach     * @param string[] $list
2754729ce104SGreg Roach     * @param string   $pid
2755729ce104SGreg Roach     * @param bool     $children
2756729ce104SGreg Roach     * @param int      $generations
27578ba2e626SGreg Roach     *
27588ba2e626SGreg Roach     * @return void
2759729ce104SGreg Roach     */
27603b3cfeeaSGreg Roach    private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void
2761c1010edaSGreg Roach    {
276213abd6f3SGreg Roach        $genlist                = [$pid];
2763729ce104SGreg Roach        $list[$pid]->generation = 1;
2764729ce104SGreg Roach        while (count($genlist) > 0) {
2765729ce104SGreg Roach            $id = array_shift($genlist);
2766729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2767729ce104SGreg Roach                continue; // id can be something like “empty7”
2768729ce104SGreg Roach            }
2769299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
277039ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
277139ca88baSGreg Roach                $husband = $family->husband();
277239ca88baSGreg Roach                $wife    = $family->wife();
2773729ce104SGreg Roach                if ($husband) {
2774c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2775c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2776729ce104SGreg Roach                }
2777729ce104SGreg Roach                if ($wife) {
2778c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2779c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2780729ce104SGreg Roach                }
2781729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2782729ce104SGreg Roach                    if ($husband) {
2783c0935879SGreg Roach                        $genlist[] = $husband->xref();
2784729ce104SGreg Roach                    }
2785729ce104SGreg Roach                    if ($wife) {
2786c0935879SGreg Roach                        $genlist[] = $wife->xref();
2787729ce104SGreg Roach                    }
2788729ce104SGreg Roach                }
2789729ce104SGreg Roach                if ($children) {
279039ca88baSGreg Roach                    foreach ($family->children() as $child) {
2791c0935879SGreg Roach                        $list[$child->xref()] = $child;
2792e364afe4SGreg Roach                        $list[$child->xref()]->generation = $list[$id]->generation ?? 1;
2793729ce104SGreg Roach                    }
2794729ce104SGreg Roach                }
2795729ce104SGreg Roach            }
2796729ce104SGreg Roach        }
2797729ce104SGreg Roach    }
2798729ce104SGreg Roach
2799729ce104SGreg Roach    /**
2800729ce104SGreg Roach     * get gedcom tag value
2801729ce104SGreg Roach     *
2802729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2803729ce104SGreg 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
2804729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2805729ce104SGreg Roach     *
2806729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2807729ce104SGreg Roach     */
28088f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2809c1010edaSGreg Roach    {
2810729ce104SGreg Roach        if (empty($gedrec)) {
2811729ce104SGreg Roach            return '';
2812729ce104SGreg Roach        }
2813729ce104SGreg Roach        $tags      = explode(':', $tag);
2814729ce104SGreg Roach        $origlevel = $level;
2815729ce104SGreg Roach        if ($level == 0) {
28163c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2817729ce104SGreg Roach        }
2818729ce104SGreg Roach
2819729ce104SGreg Roach        $subrec = $gedrec;
2820729ce104SGreg Roach        foreach ($tags as $t) {
2821729ce104SGreg Roach            $lastsubrec = $subrec;
28223d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2823729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2824729ce104SGreg Roach                $level--;
28253d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2826729ce104SGreg Roach            }
2827729ce104SGreg Roach            if (empty($subrec)) {
2828044416d2SGreg Roach                if ($t === 'TITL') {
28293d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2830729ce104SGreg Roach                    if (!empty($subrec)) {
28317a6ee1acSGreg Roach                        $t = 'ABBR';
2832729ce104SGreg Roach                    }
2833729ce104SGreg Roach                }
2834729ce104SGreg Roach                if (empty($subrec)) {
2835729ce104SGreg Roach                    if ($level > 0) {
2836729ce104SGreg Roach                        $level--;
2837729ce104SGreg Roach                    }
28383d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2839729ce104SGreg Roach                    if (empty($subrec)) {
2840729ce104SGreg Roach                        return '';
2841729ce104SGreg Roach                    }
2842729ce104SGreg Roach                }
2843729ce104SGreg Roach            }
2844729ce104SGreg Roach            $level++;
2845729ce104SGreg Roach        }
2846729ce104SGreg Roach        $level--;
2847729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2848729ce104SGreg Roach        if ($ct == 0) {
2849729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2850729ce104SGreg Roach        }
2851729ce104SGreg Roach        if ($ct == 0) {
2852729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2853729ce104SGreg Roach        }
2854729ce104SGreg Roach        if ($ct > 0) {
2855729ce104SGreg Roach            $value = trim($match[1]);
2856044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2857299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2858ff166e64SGreg Roach                if ($note instanceof Note) {
2859729ce104SGreg Roach                    $value = $note->getNote();
2860729ce104SGreg Roach                } else {
2861729ce104SGreg Roach                    //-- set the value to the id without the @
2862729ce104SGreg Roach                    $value = $match[1];
2863729ce104SGreg Roach                }
2864729ce104SGreg Roach            }
28657a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28663d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2867729ce104SGreg Roach            }
2868729ce104SGreg Roach
2869729ce104SGreg Roach            return $value;
2870729ce104SGreg Roach        }
2871729ce104SGreg Roach
28727a6ee1acSGreg Roach        return '';
2873729ce104SGreg Roach    }
2874d1286247SGreg Roach
2875d1286247SGreg Roach    /**
2876d1286247SGreg Roach     * Replace variable identifiers with their values.
2877d1286247SGreg Roach     *
2878d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
287982759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2880d1286247SGreg Roach     *
2881d1286247SGreg Roach     * @return string
2882d1286247SGreg Roach     */
28838f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2884c1010edaSGreg Roach    {
2885d1286247SGreg Roach        return preg_replace_callback(
2886d1286247SGreg Roach            '/\$(\w+)/',
288718d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28882118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
288982759250SGreg Roach                    if ($quote) {
28902118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2891b2ce94c6SRico Sonntag                    }
2892b2ce94c6SRico Sonntag
28932118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
289482759250SGreg Roach                }
2895b2ce94c6SRico Sonntag
2896d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28973d7a8a4cSGreg Roach
2898d1286247SGreg Roach                return '$' . $matches[1];
2899d1286247SGreg Roach            },
2900d1286247SGreg Roach            $expression
2901d1286247SGreg Roach        );
2902d1286247SGreg Roach    }
2903a6f13a4aSGreg Roach}
2904