xref: /webtrees/app/Report/ReportParserGenerate.php (revision 6c0bef3a4c19efb993d095d3d050baadbaf19970)
1a6f13a4aSGreg Roach<?php
2a6f13a4aSGreg Roach/**
3a6f13a4aSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a6f13a4aSGreg Roach * This program is free software: you can redistribute it and/or modify
6a6f13a4aSGreg Roach * it under the terms of the GNU General Public License as published by
7a6f13a4aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a6f13a4aSGreg Roach * (at your option) any later version.
9a6f13a4aSGreg Roach * This program is distributed in the hope that it will be useful,
10a6f13a4aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a6f13a4aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a6f13a4aSGreg Roach * GNU General Public License for more details.
13a6f13a4aSGreg Roach * You should have received a copy of the GNU General Public License
14a6f13a4aSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a6f13a4aSGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
1976692c8bSGreg Roach
20a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Auth;
21a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Database;
22a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Date;
23a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Family;
24a4d703aeSGreg Roachuse Fisharebest\Webtrees\Filter;
253d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
263d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
278d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
28a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
29a6f13a4aSGreg Roachuse Fisharebest\Webtrees\GedcomTag;
30a6f13a4aSGreg Roachuse Fisharebest\Webtrees\I18N;
31a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Individual;
32d1286247SGreg Roachuse Fisharebest\Webtrees\Log;
33a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Media;
34729ce104SGreg Roachuse Fisharebest\Webtrees\Note;
35a6f13a4aSGreg Roachuse Fisharebest\Webtrees\Place;
36299d100dSGreg Roachuse Fisharebest\Webtrees\Tree;
37195b5e75SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
38195b5e75SGreg Roachuse Illuminate\Database\Query\Builder;
399a9e551aSGreg Roachuse Illuminate\Support\Str;
4079529c87SGreg Roachuse stdClass;
41c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
425809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
43a6f13a4aSGreg Roach
44a6f13a4aSGreg Roach/**
45a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
46a6f13a4aSGreg Roach */
47c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
48c1010edaSGreg Roach{
49a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
50a6f13a4aSGreg Roach    private $process_footnote = true;
51a6f13a4aSGreg Roach
52a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
53a6f13a4aSGreg Roach    private $print_data = false;
54a6f13a4aSGreg Roach
55a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5613abd6f3SGreg Roach    private $print_data_stack = [];
57a6f13a4aSGreg Roach
5876692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
59a6f13a4aSGreg Roach    private $process_gedcoms = 0;
60a6f13a4aSGreg Roach
6176692c8bSGreg Roach    /** @var int Are we processing conditionals */
62a6f13a4aSGreg Roach    private $process_ifs = 0;
63a6f13a4aSGreg Roach
6476692c8bSGreg Roach    /** @var int Are we processing repeats */
65a6f13a4aSGreg Roach    private $process_repeats = 0;
66a6f13a4aSGreg Roach
67a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
68a6f13a4aSGreg Roach    private $repeat_bytes = 0;
69a6f13a4aSGreg Roach
705b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
7113abd6f3SGreg Roach    private $repeats = [];
72a6f13a4aSGreg Roach
73a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
7413abd6f3SGreg Roach    private $repeats_stack = [];
75a6f13a4aSGreg Roach
76208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
7713abd6f3SGreg Roach    private $wt_report_stack = [];
78e8e7866bSGreg Roach
79e8e7866bSGreg Roach    /** @var resource Nested repeating data */
80e8e7866bSGreg Roach    private $parser;
81e8e7866bSGreg Roach
82e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
8313abd6f3SGreg Roach    private $parser_stack = [];
84e8e7866bSGreg Roach
85a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
86a6f13a4aSGreg Roach    private $gedrec = '';
87a6f13a4aSGreg Roach
88a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
8913abd6f3SGreg Roach    private $gedrec_stack = [];
90a6f13a4aSGreg Roach
91a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
92a6f13a4aSGreg Roach    private $current_element;
93a6f13a4aSGreg Roach
94a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
95a6f13a4aSGreg Roach    private $footnote_element;
96a6f13a4aSGreg Roach
97a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
98a6f13a4aSGreg Roach    private $fact = '';
99a6f13a4aSGreg Roach
100a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
101a6f13a4aSGreg Roach    private $desc = '';
102a6f13a4aSGreg Roach
103a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
104a6f13a4aSGreg Roach    private $type = '';
105a6f13a4aSGreg Roach
106a6f13a4aSGreg Roach    /** @var int The current generational level */
107a6f13a4aSGreg Roach    private $generation = 1;
108a6f13a4aSGreg Roach
109a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
11013abd6f3SGreg Roach    private $list = [];
111a6f13a4aSGreg Roach
112a6f13a4aSGreg Roach    /** @var int Number of items in lists */
113a6f13a4aSGreg Roach    private $list_total = 0;
114a6f13a4aSGreg Roach
115a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
116a6f13a4aSGreg Roach    private $list_private = 0;
117a6f13a4aSGreg Roach
118299d100dSGreg Roach    /** @var string The filename of the XML report */
119299d100dSGreg Roach    protected $report;
120299d100dSGreg Roach
121208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
122e8e7866bSGreg Roach    private $report_root;
123e8e7866bSGreg Roach
124208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
125e8e7866bSGreg Roach    private $wt_report;
126e8e7866bSGreg Roach
127d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1282118c0e3SGreg Roach    private $vars;
129d1286247SGreg Roach
130299d100dSGreg Roach    /** @var Tree The current tree */
131299d100dSGreg Roach    private $tree;
132299d100dSGreg Roach
13376692c8bSGreg Roach    /**
13476692c8bSGreg Roach     * Create a parser for a report
13576692c8bSGreg Roach     *
13676692c8bSGreg Roach     * @param string         $report The XML filename
137208e9f76SGreg Roach     * @param AbstractReport $report_root
13876692c8bSGreg Roach     * @param string[][]     $vars
139299d100dSGreg Roach     * @param Tree           $tree
14076692c8bSGreg Roach     */
141208e9f76SGreg Roach    public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree)
142c1010edaSGreg Roach    {
143299d100dSGreg Roach        $this->report          = $report;
144e8e7866bSGreg Roach        $this->report_root     = $report_root;
145e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14659f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
147d1286247SGreg Roach        $this->vars            = $vars;
148299d100dSGreg Roach        $this->tree            = $tree;
149299d100dSGreg Roach
15076f666f4SGreg Roach        parent::__construct($report);
151a6f13a4aSGreg Roach    }
152a6f13a4aSGreg Roach
153a6f13a4aSGreg Roach    /**
154a6f13a4aSGreg Roach     * XML start element handler
155a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
156a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
157a6f13a4aSGreg Roach     *
158a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
159a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1608a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
16118d7a90dSGreg Roach     *
16218d7a90dSGreg Roach     * @return void
163a6f13a4aSGreg Roach     */
1648a4ee39cSGreg Roach    protected function startElement($parser, string $name, array $attrs)
165c1010edaSGreg Roach    {
16613abd6f3SGreg Roach        $newattrs = [];
167a6f13a4aSGreg Roach
168a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
169a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
170d1286247SGreg Roach                if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
171d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
172a6f13a4aSGreg Roach                }
173a6f13a4aSGreg Roach            }
174a6f13a4aSGreg Roach            $newattrs[$key] = $value;
175a6f13a4aSGreg Roach        }
176a6f13a4aSGreg Roach        $attrs = $newattrs;
1777a6ee1acSGreg 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')) {
178a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
179a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
180208e9f76SGreg Roach
181a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
182a6f13a4aSGreg Roach                $this->$start_method($attrs);
183a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
184a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
185a6f13a4aSGreg Roach            }
186a6f13a4aSGreg Roach        }
187a6f13a4aSGreg Roach    }
188a6f13a4aSGreg Roach
189a6f13a4aSGreg Roach    /**
190a6f13a4aSGreg Roach     * XML end element handler
191a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
192a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
193a6f13a4aSGreg Roach     *
194a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
195a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
19618d7a90dSGreg Roach     *
19718d7a90dSGreg Roach     * @return void
198a6f13a4aSGreg Roach     */
1998a4ee39cSGreg Roach    protected function endElement($parser, string $name)
200c1010edaSGreg Roach    {
2017a6ee1acSGreg 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')) {
202a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
203a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
204a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
205a6f13a4aSGreg Roach                $this->$end_method();
206a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
207a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
208a6f13a4aSGreg Roach            }
209a6f13a4aSGreg Roach        }
210a6f13a4aSGreg Roach    }
211a6f13a4aSGreg Roach
212a6f13a4aSGreg Roach    /**
213a6f13a4aSGreg Roach     * XML character data handler
214a6f13a4aSGreg Roach     *
215a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
216a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
21718d7a90dSGreg Roach     *
21818d7a90dSGreg Roach     * @return void
219a6f13a4aSGreg Roach     */
220c1010edaSGreg Roach    protected function characterData($parser, $data)
221c1010edaSGreg Roach    {
222e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
223a6f13a4aSGreg Roach            $this->current_element->addText($data);
224a6f13a4aSGreg Roach        }
225a6f13a4aSGreg Roach    }
226a6f13a4aSGreg Roach
227a6f13a4aSGreg Roach    /**
22876692c8bSGreg Roach     * XML <style>
229a6f13a4aSGreg Roach     *
230c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2318ba2e626SGreg Roach     *
2328ba2e626SGreg Roach     * @return void
233a6f13a4aSGreg Roach     */
234c0fe75acSGreg Roach    private function styleStartHandler(array $attrs)
235c1010edaSGreg Roach    {
236a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
237a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
238a6f13a4aSGreg Roach        }
239a6f13a4aSGreg Roach
240a6f13a4aSGreg Roach        // array Style that will be passed on
24113abd6f3SGreg Roach        $s = [];
242a6f13a4aSGreg Roach
243a6f13a4aSGreg Roach        // string Name af the style
244a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
245a6f13a4aSGreg Roach
246a6f13a4aSGreg Roach        // string Name of the DEFAULT font
247208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
248a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
249a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
250a6f13a4aSGreg Roach        }
251a6f13a4aSGreg Roach
252a6f13a4aSGreg Roach        // int The size of the font in points
253208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
254a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
255a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
256a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
257a6f13a4aSGreg Roach
258a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2597a6ee1acSGreg Roach        $s['style'] = '';
260a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
261a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
262a6f13a4aSGreg Roach        }
263a6f13a4aSGreg Roach
264e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
265a6f13a4aSGreg Roach    }
266a6f13a4aSGreg Roach
267a6f13a4aSGreg Roach    /**
26876692c8bSGreg Roach     * XML <Doc>
269a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
270a6f13a4aSGreg Roach     *
271c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
2728ba2e626SGreg Roach     *
2738ba2e626SGreg Roach     * @return void
274a6f13a4aSGreg Roach     */
275c0fe75acSGreg Roach    private function docStartHandler(array $attrs)
276c1010edaSGreg Roach    {
277e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
278a6f13a4aSGreg Roach
279a6f13a4aSGreg Roach        // Custom page width
280a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
281208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
282a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
283a6f13a4aSGreg Roach        // Custom Page height
284a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
285208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
286a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
287a6f13a4aSGreg Roach
288a6f13a4aSGreg Roach        // Left Margin
289a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2907a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
291208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
292a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
293208e9f76SGreg 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))
294a6f13a4aSGreg Roach            }
295a6f13a4aSGreg Roach        }
296a6f13a4aSGreg Roach        // Right Margin
297a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
2987a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
299208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
300a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
301208e9f76SGreg 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))
302a6f13a4aSGreg Roach            }
303a6f13a4aSGreg Roach        }
304a6f13a4aSGreg Roach        // Top Margin
305a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3067a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
307208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
308a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
309208e9f76SGreg 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))
310a6f13a4aSGreg Roach            }
311a6f13a4aSGreg Roach        }
312a6f13a4aSGreg Roach        // Bottom Margin
313a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3147a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
315208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
316a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
317208e9f76SGreg 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))
318a6f13a4aSGreg Roach            }
319a6f13a4aSGreg Roach        }
320a6f13a4aSGreg Roach        // Header Margin
321a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3227a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
323208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
324a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
325208e9f76SGreg 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))
326a6f13a4aSGreg Roach            }
327a6f13a4aSGreg Roach        }
328a6f13a4aSGreg Roach        // Footer Margin
329a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3307a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
331208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
332a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
333208e9f76SGreg 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))
334a6f13a4aSGreg Roach            }
335a6f13a4aSGreg Roach        }
336a6f13a4aSGreg Roach
337a6f13a4aSGreg Roach        // Page Orientation
338a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
339044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3407a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
341044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3427a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
343a6f13a4aSGreg Roach            }
344a6f13a4aSGreg Roach        }
345a6f13a4aSGreg Roach        // Page Size
346a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
347208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
348a6f13a4aSGreg Roach        }
349a6f13a4aSGreg Roach
350a6f13a4aSGreg Roach        // Show Generated By...
351a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3527a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
353208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3547a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
355208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
356a6f13a4aSGreg Roach            }
357a6f13a4aSGreg Roach        }
358a6f13a4aSGreg Roach
359e8e7866bSGreg Roach        $this->wt_report->setup();
360a6f13a4aSGreg Roach    }
361a6f13a4aSGreg Roach
362a6f13a4aSGreg Roach    /**
36376692c8bSGreg Roach     * XML </Doc>
3648ba2e626SGreg Roach     *
3658ba2e626SGreg Roach     * @return void
366a6f13a4aSGreg Roach     */
367c1010edaSGreg Roach    private function docEndHandler()
368c1010edaSGreg Roach    {
369e8e7866bSGreg Roach        $this->wt_report->run();
370a6f13a4aSGreg Roach    }
371a6f13a4aSGreg Roach
372a6f13a4aSGreg Roach    /**
37376692c8bSGreg Roach     * XML <Header>
3748ba2e626SGreg Roach     *
3758ba2e626SGreg Roach     * @return void
376a6f13a4aSGreg Roach     */
377c1010edaSGreg Roach    private function headerStartHandler()
378c1010edaSGreg Roach    {
379a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
380e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3817a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
382a6f13a4aSGreg Roach    }
383a6f13a4aSGreg Roach
384a6f13a4aSGreg Roach    /**
38576692c8bSGreg Roach     * XML <PageHeader>
3868ba2e626SGreg Roach     *
3878ba2e626SGreg Roach     * @return void
388a6f13a4aSGreg Roach     */
389c1010edaSGreg Roach    private function pageHeaderStartHandler()
390c1010edaSGreg Roach    {
3919b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
392a6f13a4aSGreg Roach        $this->print_data         = false;
3939b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
394e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
395a6f13a4aSGreg Roach    }
396a6f13a4aSGreg Roach
397a6f13a4aSGreg Roach    /**
39876692c8bSGreg Roach     * XML <pageHeaderEndHandler>
3998ba2e626SGreg Roach     *
4008ba2e626SGreg Roach     * @return void
401a6f13a4aSGreg Roach     */
402c1010edaSGreg Roach    private function pageHeaderEndHandler()
403c1010edaSGreg Roach    {
404a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
405e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
406e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
407e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
408a6f13a4aSGreg Roach    }
409a6f13a4aSGreg Roach
410a6f13a4aSGreg Roach    /**
41176692c8bSGreg Roach     * XML <bodyStartHandler>
4128ba2e626SGreg Roach     *
4138ba2e626SGreg Roach     * @return void
414a6f13a4aSGreg Roach     */
415c1010edaSGreg Roach    private function bodyStartHandler()
416c1010edaSGreg Roach    {
4177a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
418a6f13a4aSGreg Roach    }
419a6f13a4aSGreg Roach
420a6f13a4aSGreg Roach    /**
42176692c8bSGreg Roach     * XML <footerStartHandler>
4228ba2e626SGreg Roach     *
4238ba2e626SGreg Roach     * @return void
424a6f13a4aSGreg Roach     */
425c1010edaSGreg Roach    private function footerStartHandler()
426c1010edaSGreg Roach    {
4277a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
428a6f13a4aSGreg Roach    }
429a6f13a4aSGreg Roach
430a6f13a4aSGreg Roach    /**
43176692c8bSGreg Roach     * XML <Cell>
432a6f13a4aSGreg Roach     *
433c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4348ba2e626SGreg Roach     *
4358ba2e626SGreg Roach     * @return void
436a6f13a4aSGreg Roach     */
437c0fe75acSGreg Roach    private function cellStartHandler(array $attrs)
438c1010edaSGreg Roach    {
439a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4407a6ee1acSGreg Roach        $align = '';
441a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
442a6f13a4aSGreg Roach            $align = $attrs['align'];
443a6f13a4aSGreg Roach            // RTL supported left/right alignment
444044416d2SGreg Roach            if ($align === 'rightrtl') {
445e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4467a6ee1acSGreg Roach                    $align = 'left';
447a6f13a4aSGreg Roach                } else {
4487a6ee1acSGreg Roach                    $align = 'right';
449a6f13a4aSGreg Roach                }
450044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
451e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4527a6ee1acSGreg Roach                    $align = 'right';
453a6f13a4aSGreg Roach                } else {
4547a6ee1acSGreg Roach                    $align = 'left';
455a6f13a4aSGreg Roach                }
456a6f13a4aSGreg Roach            }
457a6f13a4aSGreg Roach        }
458a6f13a4aSGreg Roach
459a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4607a6ee1acSGreg Roach        $bgcolor = '';
461a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
462a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
463a6f13a4aSGreg Roach        }
464a6f13a4aSGreg Roach
465a6f13a4aSGreg Roach        // int Whether or not the background should be painted
466a6f13a4aSGreg Roach        $fill = 1;
467a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4687a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
469a6f13a4aSGreg Roach                $fill = 0;
4707a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
471a6f13a4aSGreg Roach                $fill = 1;
472a6f13a4aSGreg Roach            }
473a6f13a4aSGreg Roach        }
474a6f13a4aSGreg Roach
475a6f13a4aSGreg Roach        $reseth = true;
476a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
477a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4787a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
479a6f13a4aSGreg Roach                $reseth = false;
4807a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
481a6f13a4aSGreg Roach                $reseth = true;
482a6f13a4aSGreg Roach            }
483a6f13a4aSGreg Roach        }
484a6f13a4aSGreg Roach
485a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
486a6f13a4aSGreg Roach        $border = 0;
487a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
488a6f13a4aSGreg Roach            $border = $attrs['border'];
489a6f13a4aSGreg Roach        }
490a6f13a4aSGreg Roach        // string Border color in HTML code
4917a6ee1acSGreg Roach        $bocolor = '';
492a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
493a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
494a6f13a4aSGreg Roach        }
495a6f13a4aSGreg Roach
496a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
497a6f13a4aSGreg Roach        $height = 0;
498a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
499589feda3SGreg Roach            $height = $attrs['height'];
500a6f13a4aSGreg Roach        }
501a6f13a4aSGreg 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.
502a6f13a4aSGreg Roach        $width = 0;
503a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
504589feda3SGreg Roach            $width = $attrs['width'];
505a6f13a4aSGreg Roach        }
506a6f13a4aSGreg Roach
507a6f13a4aSGreg Roach        // int Stretch carachter mode
508a6f13a4aSGreg Roach        $stretch = 0;
509a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
510a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
511a6f13a4aSGreg Roach        }
512a6f13a4aSGreg Roach
513a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
514c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
515a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5167a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
517c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
518a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
519a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5207a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
521a6f13a4aSGreg Roach                $left = 0;
522a6f13a4aSGreg Roach            }
523a6f13a4aSGreg Roach        }
524a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
525c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
526a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5277a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
528c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
529a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
530a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5317a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
532a6f13a4aSGreg Roach                $top = 0;
533a6f13a4aSGreg Roach            }
534a6f13a4aSGreg Roach        }
535a6f13a4aSGreg Roach
536a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5377a6ee1acSGreg Roach        $style = '';
538a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
539a6f13a4aSGreg Roach            $style = $attrs['style'];
540a6f13a4aSGreg Roach        }
541a6f13a4aSGreg Roach
542a6f13a4aSGreg Roach        // string Text color in html code
5437a6ee1acSGreg Roach        $tcolor = '';
544a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
545a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
546a6f13a4aSGreg Roach        }
547a6f13a4aSGreg Roach
548a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
549a6f13a4aSGreg Roach        $ln = 0;
550a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
551a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
552a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5537a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
554a6f13a4aSGreg Roach                $ln = 0;
555a6f13a4aSGreg Roach            }
556a6f13a4aSGreg Roach        }
557a6f13a4aSGreg Roach
558044416d2SGreg Roach        if ($align === 'left') {
5597a6ee1acSGreg Roach            $align = 'L';
560044416d2SGreg Roach        } elseif ($align === 'right') {
5617a6ee1acSGreg Roach            $align = 'R';
562044416d2SGreg Roach        } elseif ($align === 'center') {
5637a6ee1acSGreg Roach            $align = 'C';
564044416d2SGreg Roach        } elseif ($align === 'justify') {
5657a6ee1acSGreg Roach            $align = 'J';
566a6f13a4aSGreg Roach        }
567a6f13a4aSGreg Roach
5689b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
569a6f13a4aSGreg Roach        $this->print_data         = true;
570a6f13a4aSGreg Roach
571e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
572a6f13a4aSGreg Roach            $width,
573a6f13a4aSGreg Roach            $height,
574a6f13a4aSGreg Roach            $border,
575a6f13a4aSGreg Roach            $align,
576a6f13a4aSGreg Roach            $bgcolor,
577a6f13a4aSGreg Roach            $style,
578a6f13a4aSGreg Roach            $ln,
579a6f13a4aSGreg Roach            $top,
580a6f13a4aSGreg Roach            $left,
581a6f13a4aSGreg Roach            $fill,
582a6f13a4aSGreg Roach            $stretch,
583a6f13a4aSGreg Roach            $bocolor,
584a6f13a4aSGreg Roach            $tcolor,
585a6f13a4aSGreg Roach            $reseth
586a6f13a4aSGreg Roach        );
587a6f13a4aSGreg Roach    }
588a6f13a4aSGreg Roach
589a6f13a4aSGreg Roach    /**
59076692c8bSGreg Roach     * XML </Cell>
5918ba2e626SGreg Roach     *
5928ba2e626SGreg Roach     * @return void
593a6f13a4aSGreg Roach     */
594c1010edaSGreg Roach    private function cellEndHandler()
595c1010edaSGreg Roach    {
596a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
597e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
598a6f13a4aSGreg Roach    }
599a6f13a4aSGreg Roach
600a6f13a4aSGreg Roach    /**
601a6f13a4aSGreg Roach     * XML <Now /> element handler
6028ba2e626SGreg Roach     *
6038ba2e626SGreg Roach     * @return void
604a6f13a4aSGreg Roach     */
605c1010edaSGreg Roach    private function nowStartHandler()
606c1010edaSGreg Roach    {
6073d7a8a4cSGreg Roach        $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
608a6f13a4aSGreg Roach        $this->current_element->addText($g->display());
609a6f13a4aSGreg Roach    }
610a6f13a4aSGreg Roach
611a6f13a4aSGreg Roach    /**
612a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6138ba2e626SGreg Roach     *
6148ba2e626SGreg Roach     * @return void
615a6f13a4aSGreg Roach     */
616c1010edaSGreg Roach    private function pageNumStartHandler()
617c1010edaSGreg Roach    {
6187a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
619a6f13a4aSGreg Roach    }
620a6f13a4aSGreg Roach
621a6f13a4aSGreg Roach    /**
622a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6238ba2e626SGreg Roach     *
6248ba2e626SGreg Roach     * @return void
625a6f13a4aSGreg Roach     */
626c1010edaSGreg Roach    private function totalPagesStartHandler()
627c1010edaSGreg Roach    {
6287a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
629a6f13a4aSGreg Roach    }
630a6f13a4aSGreg Roach
631a6f13a4aSGreg Roach    /**
632a6f13a4aSGreg Roach     * Called at the start of an element.
633a6f13a4aSGreg Roach     *
634c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6358ba2e626SGreg Roach     *
6368ba2e626SGreg Roach     * @return void
637a6f13a4aSGreg Roach     */
638c0fe75acSGreg Roach    private function gedcomStartHandler(array $attrs)
639c1010edaSGreg Roach    {
640a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
641a6f13a4aSGreg Roach            $this->process_gedcoms++;
642a6f13a4aSGreg Roach
643a6f13a4aSGreg Roach            return;
644a6f13a4aSGreg Roach        }
645a6f13a4aSGreg Roach
646a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6477a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6487a6ee1acSGreg Roach        $tags      = explode(':', $tag);
649a6f13a4aSGreg Roach        $newgedrec = '';
650a6f13a4aSGreg Roach        if (count($tags) < 2) {
651299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
652299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
653a6f13a4aSGreg Roach        }
654a6f13a4aSGreg Roach        if (empty($newgedrec)) {
655a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
656a6f13a4aSGreg Roach            $newgedrec = '';
657a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6587a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
659d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
660d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
661a6f13a4aSGreg Roach                    } else {
662299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
663299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
664a6f13a4aSGreg Roach                    }
665a6f13a4aSGreg Roach                } else {
6667a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66713abd6f3SGreg Roach                        $gmatch = [];
668a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
669299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
670299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
671a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
672a6f13a4aSGreg Roach                        } else {
673a6f13a4aSGreg Roach                            $newgedrec = '';
674a6f13a4aSGreg Roach                            break;
675a6f13a4aSGreg Roach                        }
676a6f13a4aSGreg Roach                    } else {
6777a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
678a6f13a4aSGreg Roach                        $level     = $temp[0] + 1;
6793d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
680a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
681a6f13a4aSGreg Roach                    }
682a6f13a4aSGreg Roach                }
683a6f13a4aSGreg Roach            }
684a6f13a4aSGreg Roach        }
685a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6869b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
687a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
688a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
689a6f13a4aSGreg Roach                $this->fact = $match[2];
690a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
691a6f13a4aSGreg Roach            }
692a6f13a4aSGreg Roach        } else {
693a6f13a4aSGreg Roach            $this->process_gedcoms++;
694a6f13a4aSGreg Roach        }
695a6f13a4aSGreg Roach    }
696a6f13a4aSGreg Roach
697a6f13a4aSGreg Roach    /**
698a6f13a4aSGreg Roach     * Called at the end of an element.
6998ba2e626SGreg Roach     *
7008ba2e626SGreg Roach     * @return void
701a6f13a4aSGreg Roach     */
702c1010edaSGreg Roach    private function gedcomEndHandler()
703c1010edaSGreg Roach    {
704a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
705a6f13a4aSGreg Roach            $this->process_gedcoms--;
706a6f13a4aSGreg Roach        } else {
70765e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
708a6f13a4aSGreg Roach        }
709a6f13a4aSGreg Roach    }
710a6f13a4aSGreg Roach
711a6f13a4aSGreg Roach    /**
71276692c8bSGreg Roach     * XML <textBoxStartHandler>
713a6f13a4aSGreg Roach     *
714c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7158ba2e626SGreg Roach     *
7168ba2e626SGreg Roach     * @return void
717a6f13a4aSGreg Roach     */
718c0fe75acSGreg Roach    private function textBoxStartHandler(array $attrs)
719c1010edaSGreg Roach    {
720a6f13a4aSGreg Roach        // string Background color code
7217a6ee1acSGreg Roach        $bgcolor = '';
722a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
723a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
724a6f13a4aSGreg Roach        }
725a6f13a4aSGreg Roach
726a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
727a6f13a4aSGreg Roach        $fill = true;
728a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7297a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
730a6f13a4aSGreg Roach                $fill = false;
7317a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
732a6f13a4aSGreg Roach                $fill = true;
733a6f13a4aSGreg Roach            }
734a6f13a4aSGreg Roach        }
735a6f13a4aSGreg Roach
736a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
737a6f13a4aSGreg Roach        $border = false;
738a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7397a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
740a6f13a4aSGreg Roach                $border = true;
7417a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
742a6f13a4aSGreg Roach                $border = false;
743a6f13a4aSGreg Roach            }
744a6f13a4aSGreg Roach        }
745a6f13a4aSGreg Roach
746a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
747a6f13a4aSGreg Roach        $height = 0;
748a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
749a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
750a6f13a4aSGreg Roach        }
751a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
752a6f13a4aSGreg Roach        $width = 0;
753a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
754a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
755a6f13a4aSGreg Roach        }
756a6f13a4aSGreg Roach
757a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
758c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
759a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7607a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
761c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
762a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
763a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7647a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
765a6f13a4aSGreg Roach                $left = 0;
766a6f13a4aSGreg Roach            }
767a6f13a4aSGreg Roach        }
768a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
769c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
770a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7717a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
772c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
773a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
774a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7757a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
776a6f13a4aSGreg Roach                $top = 0;
777a6f13a4aSGreg Roach            }
778a6f13a4aSGreg Roach        }
779a6f13a4aSGreg 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
780a6f13a4aSGreg Roach        $newline = false;
781a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7827a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
783a6f13a4aSGreg Roach                $newline = true;
7847a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
785a6f13a4aSGreg Roach                $newline = false;
786a6f13a4aSGreg Roach            }
787a6f13a4aSGreg Roach        }
788a6f13a4aSGreg Roach        // boolean
789a6f13a4aSGreg Roach        $pagecheck = true;
790a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7917a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
792a6f13a4aSGreg Roach                $pagecheck = false;
7937a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
794a6f13a4aSGreg Roach                $pagecheck = true;
795a6f13a4aSGreg Roach            }
796a6f13a4aSGreg Roach        }
797a6f13a4aSGreg Roach        // boolean Cell padding
798a6f13a4aSGreg Roach        $padding = true;
799a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8007a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
801a6f13a4aSGreg Roach                $padding = false;
8027a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
803a6f13a4aSGreg Roach                $padding = true;
804a6f13a4aSGreg Roach            }
805a6f13a4aSGreg Roach        }
806a6f13a4aSGreg Roach        // boolean Reset this box Height
807a6f13a4aSGreg Roach        $reseth = false;
808a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8097a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
810a6f13a4aSGreg Roach                $reseth = true;
8117a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
812a6f13a4aSGreg Roach                $reseth = false;
813a6f13a4aSGreg Roach            }
814a6f13a4aSGreg Roach        }
815a6f13a4aSGreg Roach
816a6f13a4aSGreg Roach        // string Style of rendering
8177a6ee1acSGreg Roach        $style = '';
818a6f13a4aSGreg Roach
8199b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
820a6f13a4aSGreg Roach        $this->print_data         = false;
821a6f13a4aSGreg Roach
8229b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
823e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
824a6f13a4aSGreg Roach            $width,
825a6f13a4aSGreg Roach            $height,
826a6f13a4aSGreg Roach            $border,
827a6f13a4aSGreg Roach            $bgcolor,
828a6f13a4aSGreg Roach            $newline,
829a6f13a4aSGreg Roach            $left,
830a6f13a4aSGreg Roach            $top,
831a6f13a4aSGreg Roach            $pagecheck,
832a6f13a4aSGreg Roach            $style,
833a6f13a4aSGreg Roach            $fill,
834a6f13a4aSGreg Roach            $padding,
835a6f13a4aSGreg Roach            $reseth
836a6f13a4aSGreg Roach        );
837a6f13a4aSGreg Roach    }
838a6f13a4aSGreg Roach
839a6f13a4aSGreg Roach    /**
84076692c8bSGreg Roach     * XML <textBoxEndHandler>
8418ba2e626SGreg Roach     *
8428ba2e626SGreg Roach     * @return void
843a6f13a4aSGreg Roach     */
844c1010edaSGreg Roach    private function textBoxEndHandler()
845c1010edaSGreg Roach    {
846a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
847e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
848e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
849e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
850a6f13a4aSGreg Roach    }
851a6f13a4aSGreg Roach
852a6f13a4aSGreg Roach    /**
85376692c8bSGreg Roach     * XLM <Text>.
85476692c8bSGreg Roach     *
855c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8568ba2e626SGreg Roach     *
8578ba2e626SGreg Roach     * @return void
858a6f13a4aSGreg Roach     */
859c0fe75acSGreg Roach    private function textStartHandler(array $attrs)
860c1010edaSGreg Roach    {
8619b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
862a6f13a4aSGreg Roach        $this->print_data         = true;
863a6f13a4aSGreg Roach
864a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8657a6ee1acSGreg Roach        $style = '';
866a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
867a6f13a4aSGreg Roach            $style = $attrs['style'];
868a6f13a4aSGreg Roach        }
869a6f13a4aSGreg Roach
870a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8717a6ee1acSGreg Roach        $color = '';
872a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
873a6f13a4aSGreg Roach            $color = $attrs['color'];
874a6f13a4aSGreg Roach        }
875a6f13a4aSGreg Roach
876e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
877a6f13a4aSGreg Roach    }
878a6f13a4aSGreg Roach
879a6f13a4aSGreg Roach    /**
88076692c8bSGreg Roach     * XML </Text>
8818ba2e626SGreg Roach     *
8828ba2e626SGreg Roach     * @return void
883a6f13a4aSGreg Roach     */
884c1010edaSGreg Roach    private function textEndHandler()
885c1010edaSGreg Roach    {
886a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
887e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
888a6f13a4aSGreg Roach    }
889a6f13a4aSGreg Roach
890a6f13a4aSGreg Roach    /**
89176692c8bSGreg Roach     * XML <GetPersonName/>
892a6f13a4aSGreg Roach     * Get the name
893a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
894a6f13a4aSGreg Roach     * 2. id is set with a record id
895a6f13a4aSGreg Roach     *
896c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8978ba2e626SGreg Roach     *
8988ba2e626SGreg Roach     * @return void
899a6f13a4aSGreg Roach     */
900c0fe75acSGreg Roach    private function getPersonNameStartHandler(array $attrs)
901c1010edaSGreg Roach    {
9027a6ee1acSGreg Roach        $id    = '';
90313abd6f3SGreg Roach        $match = [];
904a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9057a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
906a6f13a4aSGreg Roach                $id = $match[1];
907a6f13a4aSGreg Roach            }
908a6f13a4aSGreg Roach        } else {
9097a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
910d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
911d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
912a6f13a4aSGreg Roach                }
913a6f13a4aSGreg Roach            } else {
9147a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91513abd6f3SGreg Roach                    $gmatch = [];
916a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
917a6f13a4aSGreg Roach                        $id = $gmatch[1];
918a6f13a4aSGreg Roach                    }
919a6f13a4aSGreg Roach                } else {
920a6f13a4aSGreg Roach                    $id = $attrs['id'];
921a6f13a4aSGreg Roach                }
922a6f13a4aSGreg Roach            }
923a6f13a4aSGreg Roach        }
924a6f13a4aSGreg Roach        if (!empty($id)) {
925299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9268f038c36SRico Sonntag            if ($record === null) {
927a6f13a4aSGreg Roach                return;
928a6f13a4aSGreg Roach            }
929a6f13a4aSGreg Roach            if (!$record->canShowName()) {
930a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
931a6f13a4aSGreg Roach            } else {
932a6f13a4aSGreg Roach                $name = $record->getFullName();
933a6f13a4aSGreg Roach                $name = preg_replace(
934c1010edaSGreg Roach                    [
935c1010edaSGreg Roach                        '/<span class="starredname">/',
936c1010edaSGreg Roach                        '/<\/span><\/span>/',
937c1010edaSGreg Roach                        '/<\/span>/',
938c1010edaSGreg Roach                    ],
939c1010edaSGreg Roach                    [
940c1010edaSGreg Roach                        '«',
941c1010edaSGreg Roach                        '',
942c1010edaSGreg Roach                        '»',
943c1010edaSGreg Roach                    ],
944a6f13a4aSGreg Roach                    $name
945a6f13a4aSGreg Roach                );
946a6f13a4aSGreg Roach                $name = strip_tags($name);
947a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
9489a9e551aSGreg Roach                    $name = Str::limit($name, $attrs['truncate'], I18N::translate('…'));
949a6f13a4aSGreg Roach                } else {
950a6f13a4aSGreg Roach                    $addname = $record->getAddName();
951a6f13a4aSGreg Roach                    $addname = preg_replace(
952c1010edaSGreg Roach                        [
953c1010edaSGreg Roach                            '/<span class="starredname">/',
954c1010edaSGreg Roach                            '/<\/span><\/span>/',
955c1010edaSGreg Roach                            '/<\/span>/',
956c1010edaSGreg Roach                        ],
957c1010edaSGreg Roach                        [
958c1010edaSGreg Roach                            '«',
959c1010edaSGreg Roach                            '',
960c1010edaSGreg Roach                            '»',
961c1010edaSGreg Roach                        ],
962a6f13a4aSGreg Roach                        $addname
963a6f13a4aSGreg Roach                    );
964a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
965a6f13a4aSGreg Roach                    if (!empty($addname)) {
9667a6ee1acSGreg Roach                        $name .= ' ' . $addname;
967a6f13a4aSGreg Roach                    }
968a6f13a4aSGreg Roach                }
969a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
970a6f13a4aSGreg Roach            }
971a6f13a4aSGreg Roach        }
972a6f13a4aSGreg Roach    }
973a6f13a4aSGreg Roach
974a6f13a4aSGreg Roach    /**
97576692c8bSGreg Roach     * XML <GedcomValue/>
976a6f13a4aSGreg Roach     *
977c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9788ba2e626SGreg Roach     *
9798ba2e626SGreg Roach     * @return void
980a6f13a4aSGreg Roach     */
981c0fe75acSGreg Roach    private function gedcomValueStartHandler(array $attrs)
982c1010edaSGreg Roach    {
9837a6ee1acSGreg Roach        $id    = '';
98413abd6f3SGreg Roach        $match = [];
9857a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
986a6f13a4aSGreg Roach            $id = $match[1];
987a6f13a4aSGreg Roach        }
988a6f13a4aSGreg Roach
989044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9907a6ee1acSGreg Roach            $useBreak = '1';
991a6f13a4aSGreg Roach        } else {
9927a6ee1acSGreg Roach            $useBreak = '0';
993a6f13a4aSGreg Roach        }
994a6f13a4aSGreg Roach
995a6f13a4aSGreg Roach        $tag = $attrs['tag'];
996a6f13a4aSGreg Roach        if (!empty($tag)) {
997044416d2SGreg Roach            if ($tag === '@desc') {
998a6f13a4aSGreg Roach                $value = $this->desc;
999a6f13a4aSGreg Roach                $value = trim($value);
1000a6f13a4aSGreg Roach                $this->current_element->addText($value);
1001a6f13a4aSGreg Roach            }
1002044416d2SGreg Roach            if ($tag === '@id') {
1003a6f13a4aSGreg Roach                $this->current_element->addText($id);
1004a6f13a4aSGreg Roach            } else {
10057a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1006a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
10077a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
1008a6f13a4aSGreg Roach                    $level = $temp[0];
1009a6f13a4aSGreg Roach                    if ($level == 0) {
1010a6f13a4aSGreg Roach                        $level++;
1011a6f13a4aSGreg Roach                    }
1012a6f13a4aSGreg Roach                } else {
1013a6f13a4aSGreg Roach                    $level = $attrs['level'];
1014a6f13a4aSGreg Roach                }
1015a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10163d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1017a6f13a4aSGreg Roach                switch (end($tags)) {
1018a6f13a4aSGreg Roach                    case 'DATE':
1019a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1020a6f13a4aSGreg Roach                        $value = $tmp->display();
1021a6f13a4aSGreg Roach                        break;
1022a6f13a4aSGreg Roach                    case 'PLAC':
1023299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1024a6f13a4aSGreg Roach                        $value = $tmp->getShortName();
1025a6f13a4aSGreg Roach                        break;
1026a6f13a4aSGreg Roach                }
1027044416d2SGreg Roach                if ($useBreak === '1') {
1028a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1029a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1030a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1031a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1032a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
1033044416d2SGreg Roach                    if (substr($value, 0, 6) === '<br>') {
1034a6f13a4aSGreg Roach                        $value = substr($value, 6);
1035a6f13a4aSGreg Roach                    }
1036a6f13a4aSGreg Roach                }
1037d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1038c1010edaSGreg Roach                if (in_array(end($tmp), [
1039c1010edaSGreg Roach                    'NOTE',
1040c1010edaSGreg Roach                    'TEXT',
1041c1010edaSGreg Roach                ])) {
1042299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1043a4d703aeSGreg Roach                }
1044a6f13a4aSGreg Roach                $this->current_element->addText($value);
1045a6f13a4aSGreg Roach            }
1046a6f13a4aSGreg Roach        }
1047a6f13a4aSGreg Roach    }
1048a6f13a4aSGreg Roach
1049a6f13a4aSGreg Roach    /**
105076692c8bSGreg Roach     * XML <RepeatTag>
1051a6f13a4aSGreg Roach     *
1052c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10538ba2e626SGreg Roach     *
10548ba2e626SGreg Roach     * @return void
1055a6f13a4aSGreg Roach     */
1056c0fe75acSGreg Roach    private function repeatTagStartHandler(array $attrs)
1057c1010edaSGreg Roach    {
1058a6f13a4aSGreg Roach        $this->process_repeats++;
1059a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1060a6f13a4aSGreg Roach            return;
1061a6f13a4aSGreg Roach        }
1062a6f13a4aSGreg Roach
10639b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
106413abd6f3SGreg Roach        $this->repeats         = [];
1065e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1066a6f13a4aSGreg Roach
10677a6ee1acSGreg Roach        $tag = '';
1068a6f13a4aSGreg Roach        if (isset($attrs['tag'])) {
1069a6f13a4aSGreg Roach            $tag = $attrs['tag'];
1070a6f13a4aSGreg Roach        }
1071a6f13a4aSGreg Roach        if (!empty($tag)) {
1072044416d2SGreg Roach            if ($tag === '@desc') {
1073a6f13a4aSGreg Roach                $value = $this->desc;
1074a6f13a4aSGreg Roach                $value = trim($value);
1075a6f13a4aSGreg Roach                $this->current_element->addText($value);
1076a6f13a4aSGreg Roach            } else {
10777a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10787a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10797a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1080a6f13a4aSGreg Roach                $level = $temp[0];
1081a6f13a4aSGreg Roach                if ($level == 0) {
1082a6f13a4aSGreg Roach                    $level++;
1083a6f13a4aSGreg Roach                }
1084a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1085a6f13a4aSGreg Roach                $t      = $tag;
1086a6f13a4aSGreg Roach                $count  = count($tags);
1087a6f13a4aSGreg Roach                $i      = 0;
1088a6f13a4aSGreg Roach                while ($i < $count) {
1089a6f13a4aSGreg Roach                    $t = $tags[$i];
1090a6f13a4aSGreg Roach                    if (!empty($t)) {
1091a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10923d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1093a6f13a4aSGreg Roach                            if (empty($subrec)) {
1094a6f13a4aSGreg Roach                                $level--;
10953d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1096a6f13a4aSGreg Roach                                if (empty($subrec)) {
1097a6f13a4aSGreg Roach                                    return;
1098a6f13a4aSGreg Roach                                }
1099a6f13a4aSGreg Roach                            }
1100a6f13a4aSGreg Roach                        }
1101a6f13a4aSGreg Roach                        $level++;
1102a6f13a4aSGreg Roach                    }
1103a6f13a4aSGreg Roach                    $i++;
1104a6f13a4aSGreg Roach                }
1105a6f13a4aSGreg Roach                $level--;
1106a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1107a6f13a4aSGreg Roach                $i     = 0;
1108a6f13a4aSGreg Roach                while ($i < $count) {
1109a6f13a4aSGreg Roach                    $i++;
1110a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1111a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11128d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1113299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1114a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1115a9007102SGreg Roach                            continue;
1116a9007102SGreg Roach                        }
1117a9007102SGreg Roach                    }
1118a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1119a6f13a4aSGreg Roach                }
1120a6f13a4aSGreg Roach            }
1121a6f13a4aSGreg Roach        }
1122a6f13a4aSGreg Roach    }
1123a6f13a4aSGreg Roach
1124a6f13a4aSGreg Roach    /**
112576692c8bSGreg Roach     * XML </ RepeatTag>
11268ba2e626SGreg Roach     *
11278ba2e626SGreg Roach     * @return void
1128a6f13a4aSGreg Roach     */
1129c1010edaSGreg Roach    private function repeatTagEndHandler()
1130c1010edaSGreg Roach    {
1131a6f13a4aSGreg Roach        $this->process_repeats--;
1132a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1133a6f13a4aSGreg Roach            return;
1134a6f13a4aSGreg Roach        }
1135a6f13a4aSGreg Roach
1136a6f13a4aSGreg Roach        // Check if there is anything to repeat
1137a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1138a6f13a4aSGreg Roach            // No need to load them if not used...
1139a6f13a4aSGreg Roach
1140a6f13a4aSGreg Roach            $lineoffset = 0;
1141a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1142a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1143a6f13a4aSGreg Roach            }
1144a6f13a4aSGreg Roach            //-- read the xml from the file
1145299d100dSGreg Roach            $lines = file($this->report);
11467a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1147a6f13a4aSGreg Roach                $lineoffset--;
1148a6f13a4aSGreg Roach            }
1149a6f13a4aSGreg Roach            $lineoffset++;
1150a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1151a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1152a6f13a4aSGreg Roach            // RepeatTag Level counter
1153a6f13a4aSGreg Roach            $count = 1;
1154a6f13a4aSGreg Roach            while (0 < $count) {
11557a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1156a6f13a4aSGreg Roach                    $count++;
11577a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1158a6f13a4aSGreg Roach                    $count--;
1159a6f13a4aSGreg Roach                }
1160a6f13a4aSGreg Roach                if (0 < $count) {
1161a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1162a6f13a4aSGreg Roach                }
1163a6f13a4aSGreg Roach                $line_nr++;
1164a6f13a4aSGreg Roach            }
1165a6f13a4aSGreg Roach            // No need to drag this
1166a6f13a4aSGreg Roach            unset($lines);
1167a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1168a6f13a4aSGreg Roach            // Save original values
11699b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1170a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1171a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1172a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1173a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1174e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1175a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11761aa04befSGreg Roach
11771aa04befSGreg Roach                xml_set_element_handler(
11781aa04befSGreg Roach                    $repeat_parser,
11791aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
11801aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11811aa04befSGreg Roach                    },
11821aa04befSGreg Roach                    function ($parser, string $name) {
11831aa04befSGreg Roach                        $this->endElement($parser, $name);
11841aa04befSGreg Roach                    }
11851aa04befSGreg Roach                );
11861aa04befSGreg Roach
11871aa04befSGreg Roach                xml_set_character_data_handler(
11881aa04befSGreg Roach                    $repeat_parser,
11891aa04befSGreg Roach                    function ($parser, $data) {
11901aa04befSGreg Roach                        $this->characterData($parser, $data);
11911aa04befSGreg Roach                    }
11921aa04befSGreg Roach                );
11931aa04befSGreg Roach
1194a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1195a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1196a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1197a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1198a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1199a6f13a4aSGreg Roach                    ));
1200a6f13a4aSGreg Roach                }
1201a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1202a6f13a4aSGreg Roach            }
1203a6f13a4aSGreg Roach            // Restore original values
1204a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1205e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1206a6f13a4aSGreg Roach        }
120765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1208a6f13a4aSGreg Roach    }
1209a6f13a4aSGreg Roach
1210a6f13a4aSGreg Roach    /**
1211a6f13a4aSGreg Roach     * Variable lookup
1212a6f13a4aSGreg Roach     * Retrieve predefined variables :
1213a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1214a6f13a4aSGreg Roach     *        1 EVEN This is a description
1215a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1216a6f13a4aSGreg Roach     * $ I18N::translate('....')
1217a6f13a4aSGreg Roach     * $ language_settings[]
1218a6f13a4aSGreg Roach     *
1219c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12208ba2e626SGreg Roach     *
12218ba2e626SGreg Roach     * @return void
1222a6f13a4aSGreg Roach     */
1223c0fe75acSGreg Roach    private function varStartHandler(array $attrs)
1224c1010edaSGreg Roach    {
1225a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
1226e8e7866bSGreg 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));
1227a6f13a4aSGreg Roach        }
1228a6f13a4aSGreg Roach
1229a6f13a4aSGreg Roach        $var = $attrs['var'];
1230a6f13a4aSGreg Roach        // SetVar element preset variables
1231d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1232d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1233a6f13a4aSGreg Roach        } else {
1234a6f13a4aSGreg Roach            $tfact = $this->fact;
12357a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1236a6f13a4aSGreg Roach                // Use :
1237a6f13a4aSGreg Roach                // n TYPE This text if string
1238a6f13a4aSGreg Roach                $tfact = $this->type;
1239a6f13a4aSGreg Roach            }
1240c1010edaSGreg Roach            $var = str_replace([
1241c1010edaSGreg Roach                '@fact',
1242c1010edaSGreg Roach                '@desc',
1243c1010edaSGreg Roach            ], [
1244c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1245c1010edaSGreg Roach                $this->desc,
1246c1010edaSGreg Roach            ], $var);
1247a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1248da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1249a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1250a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1251a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1252a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1253a6f13a4aSGreg Roach            }
1254a6f13a4aSGreg Roach        }
1255a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1256a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12577a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1258a6f13a4aSGreg Roach                $g   = new Date($var);
1259a6f13a4aSGreg Roach                $var = $g->display();
1260a6f13a4aSGreg Roach            }
1261a6f13a4aSGreg Roach        }
1262a6f13a4aSGreg Roach        $this->current_element->addText($var);
12632836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1264a6f13a4aSGreg Roach    }
1265a6f13a4aSGreg Roach
1266a6f13a4aSGreg Roach    /**
126776692c8bSGreg Roach     * XML <Facts>
126876692c8bSGreg Roach     *
1269c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12708ba2e626SGreg Roach     *
12718ba2e626SGreg Roach     * @return void
1272a6f13a4aSGreg Roach     */
1273c0fe75acSGreg Roach    private function factsStartHandler(array $attrs)
1274c1010edaSGreg Roach    {
1275a6f13a4aSGreg Roach        $this->process_repeats++;
1276a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1277a6f13a4aSGreg Roach            return;
1278a6f13a4aSGreg Roach        }
1279a6f13a4aSGreg Roach
12809b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
128113abd6f3SGreg Roach        $this->repeats         = [];
1282e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1283a6f13a4aSGreg Roach
12847a6ee1acSGreg Roach        $id    = '';
128513abd6f3SGreg Roach        $match = [];
12867a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1287a6f13a4aSGreg Roach            $id = $match[1];
1288a6f13a4aSGreg Roach        }
12897a6ee1acSGreg Roach        $tag = '';
1290a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1291a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1292a6f13a4aSGreg Roach        }
12937a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1294d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1295a6f13a4aSGreg Roach        }
1296a6f13a4aSGreg Roach
1297299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1298a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
129930158ae7SGreg Roach            $facts = $record->facts();
13003d7a8a4cSGreg Roach            Functions::sortFacts($facts);
130113abd6f3SGreg Roach            $this->repeats = [];
1302a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1303195b5e75SGreg Roach            foreach ($facts as $fact) {
1304195b5e75SGreg Roach                if (!in_array($fact->getTag(), $nonfacts)) {
1305195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1306a6f13a4aSGreg Roach                }
1307a6f13a4aSGreg Roach            }
1308a6f13a4aSGreg Roach        } else {
130930158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1310195b5e75SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1311138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1312a6f13a4aSGreg Roach                }
1313a6f13a4aSGreg Roach            }
1314a6f13a4aSGreg Roach        }
1315a6f13a4aSGreg Roach    }
1316a6f13a4aSGreg Roach
1317a6f13a4aSGreg Roach    /**
131876692c8bSGreg Roach     * XML </Facts>
13198ba2e626SGreg Roach     *
13208ba2e626SGreg Roach     * @return void
1321a6f13a4aSGreg Roach     */
1322c1010edaSGreg Roach    private function factsEndHandler()
1323c1010edaSGreg Roach    {
1324a6f13a4aSGreg Roach        $this->process_repeats--;
1325a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1326a6f13a4aSGreg Roach            return;
1327a6f13a4aSGreg Roach        }
1328a6f13a4aSGreg Roach
1329a6f13a4aSGreg Roach        // Check if there is anything to repeat
1330a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1331e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1332a6f13a4aSGreg Roach            $lineoffset = 0;
1333a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1334a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1335a6f13a4aSGreg Roach            }
1336a6f13a4aSGreg Roach
1337a6f13a4aSGreg Roach            //-- read the xml from the file
1338299d100dSGreg Roach            $lines = file($this->report);
1339a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1340a6f13a4aSGreg Roach                $lineoffset--;
1341a6f13a4aSGreg Roach            }
1342a6f13a4aSGreg Roach            $lineoffset++;
1343a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1344a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1345a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1346a6f13a4aSGreg Roach            while ($line_nr < $i) {
1347a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1348a6f13a4aSGreg Roach                $line_nr++;
1349a6f13a4aSGreg Roach            }
1350a6f13a4aSGreg Roach            // No need to drag this
1351a6f13a4aSGreg Roach            unset($lines);
1352a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1353a6f13a4aSGreg Roach            // Save original values
13549b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1355a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1356a6f13a4aSGreg Roach            $count                = count($this->repeats);
1357a6f13a4aSGreg Roach            $i                    = 0;
1358a6f13a4aSGreg Roach            while ($i < $count) {
1359a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1360a6f13a4aSGreg Roach                $this->fact   = '';
1361a6f13a4aSGreg Roach                $this->desc   = '';
1362a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1363a6f13a4aSGreg Roach                    $this->fact = $match[1];
1364a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
136513abd6f3SGreg Roach                        $tmatch = [];
1366a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1367a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1368a6f13a4aSGreg Roach                        } else {
1369a6f13a4aSGreg Roach                            $this->type = ' ';
1370a6f13a4aSGreg Roach                        }
1371a6f13a4aSGreg Roach                    }
1372a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13733d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1374a6f13a4aSGreg Roach                }
1375a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1376e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1377a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13781aa04befSGreg Roach
13791aa04befSGreg Roach                xml_set_element_handler(
13801aa04befSGreg Roach                    $repeat_parser,
13811aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
13821aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13831aa04befSGreg Roach                    },
13841aa04befSGreg Roach                    function ($parser, string $name) {
13851aa04befSGreg Roach                        $this->endElement($parser, $name);
13861aa04befSGreg Roach                    }
13871aa04befSGreg Roach                );
13881aa04befSGreg Roach
13891aa04befSGreg Roach                xml_set_character_data_handler(
13901aa04befSGreg Roach                    $repeat_parser,
13911aa04befSGreg Roach                    function ($parser, $data) {
13921aa04befSGreg Roach                        $this->characterData($parser, $data);
13931aa04befSGreg Roach                    }
13941aa04befSGreg Roach                );
13951aa04befSGreg Roach
1396a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1397a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1398a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1399a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1400a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1401a6f13a4aSGreg Roach                    ));
1402a6f13a4aSGreg Roach                }
1403a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1404a6f13a4aSGreg Roach                $i++;
1405a6f13a4aSGreg Roach            }
1406a6f13a4aSGreg Roach            // Restore original values
1407e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1408a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1409a6f13a4aSGreg Roach        }
141065e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1411a6f13a4aSGreg Roach    }
1412a6f13a4aSGreg Roach
1413a6f13a4aSGreg Roach    /**
1414a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1415d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1416a6f13a4aSGreg Roach     *
1417c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14188ba2e626SGreg Roach     *
14198ba2e626SGreg Roach     * @return void
1420a6f13a4aSGreg Roach     */
1421c0fe75acSGreg Roach    private function setVarStartHandler(array $attrs)
1422c1010edaSGreg Roach    {
1423a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
1424a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1425a6f13a4aSGreg Roach        }
1426a6f13a4aSGreg Roach
1427a6f13a4aSGreg Roach        $name  = $attrs['name'];
1428a6f13a4aSGreg Roach        $value = $attrs['value'];
142913abd6f3SGreg Roach        $match = [];
1430a6f13a4aSGreg Roach        // Current GEDCOM record strings
1431044416d2SGreg Roach        if ($value === '@ID') {
14327a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1433a6f13a4aSGreg Roach                $value = $match[1];
1434a6f13a4aSGreg Roach            }
1435044416d2SGreg Roach        } elseif ($value === '@fact') {
1436a6f13a4aSGreg Roach            $value = $this->fact;
1437044416d2SGreg Roach        } elseif ($value === '@desc') {
1438a6f13a4aSGreg Roach            $value = $this->desc;
1439044416d2SGreg Roach        } elseif ($value === '@generation') {
1440589feda3SGreg Roach            $value = (string) $this->generation;
1441a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144213abd6f3SGreg Roach            $gmatch = [];
1443a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14447a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1445a6f13a4aSGreg Roach            }
1446a6f13a4aSGreg Roach        }
1447a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1448d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1449a6f13a4aSGreg Roach        }
1450a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1451a6f13a4aSGreg Roach        $i     = 0;
1452a6f13a4aSGreg Roach        while ($i < $count) {
1453d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14547a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1455a6f13a4aSGreg Roach            $i++;
1456a6f13a4aSGreg Roach        }
1457a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1458da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1459a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1460a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1461a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1462a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1463a6f13a4aSGreg Roach        }
146452868398SGreg Roach
1465a6f13a4aSGreg Roach        // Arithmetic functions
1466a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
146752868398SGreg Roach            // Create an expression language with the functions used by our reports.
146852868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1469c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1470c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
147152868398SGreg Roach
147252868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1473a6f13a4aSGreg Roach        }
147452868398SGreg Roach
14757a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14767a6ee1acSGreg Roach            $value = '';
1477a6f13a4aSGreg Roach        }
1478d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1479a6f13a4aSGreg Roach    }
1480a6f13a4aSGreg Roach
1481a6f13a4aSGreg Roach    /**
1482a6f13a4aSGreg Roach     * XML <if > start element
1483a6f13a4aSGreg Roach     *
1484c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14858ba2e626SGreg Roach     *
14868ba2e626SGreg Roach     * @return void
1487a6f13a4aSGreg Roach     */
1488c0fe75acSGreg Roach    private function ifStartHandler(array $attrs)
1489c1010edaSGreg Roach    {
1490a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1491a6f13a4aSGreg Roach            $this->process_ifs++;
1492a6f13a4aSGreg Roach
1493a6f13a4aSGreg Roach            return;
1494a6f13a4aSGreg Roach        }
1495a6f13a4aSGreg Roach
1496a6f13a4aSGreg Roach        $condition = $attrs['condition'];
149782759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1498c1010edaSGreg Roach        $condition = str_replace([
1499c1010edaSGreg Roach            ' LT ',
1500c1010edaSGreg Roach            ' GT ',
1501c1010edaSGreg Roach        ], [
1502c1010edaSGreg Roach            '<',
1503c1010edaSGreg Roach            '>',
1504c1010edaSGreg Roach        ], $condition);
1505a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15067a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
150713abd6f3SGreg Roach        $match     = [];
1508a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1509a6f13a4aSGreg Roach        $i         = 0;
1510a6f13a4aSGreg Roach        while ($i < $count) {
1511a6f13a4aSGreg Roach            $id    = $match[$i][1];
1512a6f13a4aSGreg Roach            $value = '""';
1513044416d2SGreg Roach            if ($id === 'ID') {
15147a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1515a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1516a6f13a4aSGreg Roach                }
15177a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1518a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15197a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1520a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15217a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1522a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1523a6f13a4aSGreg Roach            } else {
15247a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1525a6f13a4aSGreg Roach                $level = $temp[0];
1526a6f13a4aSGreg Roach                if ($level == 0) {
1527a6f13a4aSGreg Roach                    $level++;
1528a6f13a4aSGreg Roach                }
15293d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1530a6f13a4aSGreg Roach                if (empty($value)) {
1531a6f13a4aSGreg Roach                    $level++;
15323d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1533a6f13a4aSGreg Roach                }
15348d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15355e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1536a6f13a4aSGreg Roach            }
1537a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1538a6f13a4aSGreg Roach            $i++;
1539a6f13a4aSGreg Roach        }
15405809450fSGreg Roach
1541cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1542cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1543c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1544c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1545cb63a60eSGreg Roach
1546cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15475809450fSGreg Roach
1548a6f13a4aSGreg Roach        if (!$ret) {
1549a6f13a4aSGreg Roach            $this->process_ifs++;
1550a6f13a4aSGreg Roach        }
1551a6f13a4aSGreg Roach    }
1552a6f13a4aSGreg Roach
1553a6f13a4aSGreg Roach    /**
1554a6f13a4aSGreg Roach     * XML <if /> end element
15558ba2e626SGreg Roach     *
15568ba2e626SGreg Roach     * @return void
1557a6f13a4aSGreg Roach     */
1558c1010edaSGreg Roach    private function ifEndHandler()
1559c1010edaSGreg Roach    {
1560a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1561a6f13a4aSGreg Roach            $this->process_ifs--;
1562a6f13a4aSGreg Roach        }
1563a6f13a4aSGreg Roach    }
1564a6f13a4aSGreg Roach
1565a6f13a4aSGreg Roach    /**
1566a6f13a4aSGreg Roach     * XML <Footnote > start element
1567a6f13a4aSGreg Roach     * Collect the Footnote links
1568a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1569a6f13a4aSGreg Roach     *
1570c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15718ba2e626SGreg Roach     *
15728ba2e626SGreg Roach     * @return void
1573a6f13a4aSGreg Roach     */
1574c0fe75acSGreg Roach    private function footnoteStartHandler(array $attrs)
1575c1010edaSGreg Roach    {
15767a6ee1acSGreg Roach        $id = '';
15777a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1578a6f13a4aSGreg Roach            $id = $match[2];
1579a6f13a4aSGreg Roach        }
1580299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1581a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15829b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1583a6f13a4aSGreg Roach            $this->print_data         = true;
15847a6ee1acSGreg Roach            $style                    = '';
1585a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1586a6f13a4aSGreg Roach                $style = $attrs['style'];
1587a6f13a4aSGreg Roach            }
1588a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1589e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1590a6f13a4aSGreg Roach        } else {
1591a6f13a4aSGreg Roach            $this->print_data       = false;
1592a6f13a4aSGreg Roach            $this->process_footnote = false;
1593a6f13a4aSGreg Roach        }
1594a6f13a4aSGreg Roach    }
1595a6f13a4aSGreg Roach
1596a6f13a4aSGreg Roach    /**
1597a6f13a4aSGreg Roach     * XML <Footnote /> end element
1598a6f13a4aSGreg Roach     * Print the collected Footnote data
15998ba2e626SGreg Roach     *
16008ba2e626SGreg Roach     * @return void
1601a6f13a4aSGreg Roach     */
1602c1010edaSGreg Roach    private function footnoteEndHandler()
1603c1010edaSGreg Roach    {
1604a6f13a4aSGreg Roach        if ($this->process_footnote) {
1605a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1606a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1607a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1608e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1609a6f13a4aSGreg Roach            }
1610a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1611a6f13a4aSGreg Roach        } else {
1612a6f13a4aSGreg Roach            $this->process_footnote = true;
1613a6f13a4aSGreg Roach        }
1614a6f13a4aSGreg Roach    }
1615a6f13a4aSGreg Roach
1616a6f13a4aSGreg Roach    /**
1617a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16188ba2e626SGreg Roach     *
16198ba2e626SGreg Roach     * @return void
1620a6f13a4aSGreg Roach     */
1621c1010edaSGreg Roach    private function footnoteTextsStartHandler()
1622c1010edaSGreg Roach    {
16237a6ee1acSGreg Roach        $temp = 'footnotetexts';
1624e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1625a6f13a4aSGreg Roach    }
1626a6f13a4aSGreg Roach
1627a6f13a4aSGreg Roach    /**
1628a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16298ba2e626SGreg Roach     *
16308ba2e626SGreg Roach     * @return void
1631a6f13a4aSGreg Roach     */
1632c1010edaSGreg Roach    private function brStartHandler()
1633c1010edaSGreg Roach    {
1634a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1635a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1636a6f13a4aSGreg Roach        }
1637a6f13a4aSGreg Roach    }
1638a6f13a4aSGreg Roach
1639a6f13a4aSGreg Roach    /**
1640a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16418ba2e626SGreg Roach     *
16428ba2e626SGreg Roach     * @return void
1643a6f13a4aSGreg Roach     */
1644c1010edaSGreg Roach    private function spStartHandler()
1645c1010edaSGreg Roach    {
1646a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1647a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1648a6f13a4aSGreg Roach        }
1649a6f13a4aSGreg Roach    }
1650a6f13a4aSGreg Roach
1651a6f13a4aSGreg Roach    /**
165276692c8bSGreg Roach     * XML <HighlightedImage/>
165376692c8bSGreg Roach     *
1654c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16558ba2e626SGreg Roach     *
16568ba2e626SGreg Roach     * @return void
1657a6f13a4aSGreg Roach     */
1658c0fe75acSGreg Roach    private function highlightedImageStartHandler(array $attrs)
1659c1010edaSGreg Roach    {
1660a6f13a4aSGreg Roach        $id = '';
16617a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1662a6f13a4aSGreg Roach            $id = $match[1];
1663a6f13a4aSGreg Roach        }
1664a6f13a4aSGreg Roach
1665c21bdddcSGreg Roach        // Position the top corner of this box on the page
1666c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1667a6f13a4aSGreg Roach
1668c21bdddcSGreg Roach        // Position the left corner of this box on the page
1669c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1670a6f13a4aSGreg Roach
167183cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167283cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1673a6f13a4aSGreg Roach
1674a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
167583cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1676a6f13a4aSGreg Roach
167783cdc021SGreg Roach        // Width, height (or both).
1678c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1679c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1680a6f13a4aSGreg Roach
1681299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16824a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
168386a63f51SGreg Roach
168486a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1685c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1686c1010edaSGreg Roach                0,
1687c1010edaSGreg Roach                0,
1688c1010edaSGreg Roach            ];
1689a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16903c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16913c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1692a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16933c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16943c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1695a6f13a4aSGreg Roach            } else {
16963c3b90deSGreg Roach                $width  = $attributes[0];
16973c3b90deSGreg Roach                $height = $attributes[1];
1698a6f13a4aSGreg Roach            }
16994a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1700e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1701a6f13a4aSGreg Roach        }
1702a6f13a4aSGreg Roach    }
1703a6f13a4aSGreg Roach
1704a6f13a4aSGreg Roach    /**
170576692c8bSGreg Roach     * XML <Image/>
170676692c8bSGreg Roach     *
1707c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17088ba2e626SGreg Roach     *
17098ba2e626SGreg Roach     * @return void
1710a6f13a4aSGreg Roach     */
1711c0fe75acSGreg Roach    private function imageStartHandler(array $attrs)
1712c1010edaSGreg Roach    {
171383cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1714c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1715a6f13a4aSGreg Roach
1716a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1717c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1718a6f13a4aSGreg Roach
171983cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172083cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1721a6f13a4aSGreg Roach
1722a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172383cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1724a6f13a4aSGreg Roach
172583cdc021SGreg Roach        // Width, height (or both).
1726c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1727c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1728a6f13a4aSGreg Roach
172983cdc021SGreg Roach        $file = $attrs['file'] ?? '';
173083cdc021SGreg Roach
1731044416d2SGreg Roach        if ($file === '@FILE') {
173213abd6f3SGreg Roach            $match = [];
1733a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1734299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17354a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1736cdf416fbSGreg Roach
17374a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1738c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1739c1010edaSGreg Roach                        0,
1740c1010edaSGreg Roach                        0,
1741c1010edaSGreg Roach                    ];
1742a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17433c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17443c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1745a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17463c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17473c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1748a6f13a4aSGreg Roach                    } else {
17493c3b90deSGreg Roach                        $width  = $attributes[0];
17503c3b90deSGreg Roach                        $height = $attributes[1];
1751a6f13a4aSGreg Roach                    }
17524a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1753e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1754a6f13a4aSGreg Roach                }
1755a6f13a4aSGreg Roach            }
1756a6f13a4aSGreg Roach        } else {
17577a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1758a6f13a4aSGreg Roach                $size = getimagesize($file);
1759a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1760a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1761a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1762a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1763a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1764a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1765a6f13a4aSGreg Roach                } else {
1766a6f13a4aSGreg Roach                    $width  = $size[0];
1767a6f13a4aSGreg Roach                    $height = $size[1];
1768a6f13a4aSGreg Roach                }
1769e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1770e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1771a6f13a4aSGreg Roach            }
1772a6f13a4aSGreg Roach        }
1773a6f13a4aSGreg Roach    }
1774a6f13a4aSGreg Roach
1775a6f13a4aSGreg Roach    /**
1776a6f13a4aSGreg Roach     * XML <Line> element handler
1777a6f13a4aSGreg Roach     *
1778c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17798ba2e626SGreg Roach     *
17808ba2e626SGreg Roach     * @return void
1781a6f13a4aSGreg Roach     */
1782c0fe75acSGreg Roach    private function lineStartHandler(array $attrs)
1783c1010edaSGreg Roach    {
1784a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1785c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1786a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17877a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1788a6f13a4aSGreg Roach                $x1 = 0;
17897a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1790c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1791a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1792c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1793a6f13a4aSGreg Roach            }
1794a6f13a4aSGreg Roach        }
1795a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1796c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1797a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17987a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1799a6f13a4aSGreg Roach                $y1 = 0;
18007a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1801c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1802a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1803c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1804a6f13a4aSGreg Roach            }
1805a6f13a4aSGreg Roach        }
1806a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1807c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1808a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18097a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1810a6f13a4aSGreg Roach                $x2 = 0;
18117a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1812c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1813a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1814c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1815a6f13a4aSGreg Roach            }
1816a6f13a4aSGreg Roach        }
1817a6f13a4aSGreg Roach        // End vertical position
1818c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1819a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18207a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1821a6f13a4aSGreg Roach                $y2 = 0;
18227a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1823c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1824a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1825c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1826a6f13a4aSGreg Roach            }
1827a6f13a4aSGreg Roach        }
1828a6f13a4aSGreg Roach
1829e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1830e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1831a6f13a4aSGreg Roach    }
1832a6f13a4aSGreg Roach
1833a6f13a4aSGreg Roach    /**
183476692c8bSGreg Roach     * XML <List>
1835a6f13a4aSGreg Roach     *
1836c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18378ba2e626SGreg Roach     *
18388ba2e626SGreg Roach     * @return void
1839a6f13a4aSGreg Roach     */
1840c0fe75acSGreg Roach    private function listStartHandler(array $attrs)
1841c1010edaSGreg Roach    {
1842a6f13a4aSGreg Roach        $this->process_repeats++;
1843a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1844a6f13a4aSGreg Roach            return;
1845a6f13a4aSGreg Roach        }
1846a6f13a4aSGreg Roach
184713abd6f3SGreg Roach        $match = [];
1848a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1849a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1850a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1851d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1852a6f13a4aSGreg Roach                $sortby = trim($sortby);
1853a6f13a4aSGreg Roach            }
1854a6f13a4aSGreg Roach        } else {
18557a6ee1acSGreg Roach            $sortby = 'NAME';
1856a6f13a4aSGreg Roach        }
1857a6f13a4aSGreg Roach
1858a6f13a4aSGreg Roach        if (isset($attrs['list'])) {
1859a6f13a4aSGreg Roach            $listname = $attrs['list'];
1860a6f13a4aSGreg Roach        } else {
18617a6ee1acSGreg Roach            $listname = 'individual';
1862a6f13a4aSGreg Roach        }
1863195b5e75SGreg Roach
1864a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1865a6f13a4aSGreg Roach        switch ($listname) {
18667a6ee1acSGreg Roach            case 'pending':
1867*6c0bef3aSGreg Roach                $xrefs = DB::table('change')
1868195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1869195b5e75SGreg Roach                        $query->select(DB::raw('MAX(change_id)'))
1870195b5e75SGreg Roach                            ->from('change')
1871195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1872195b5e75SGreg Roach                            ->where('status', '=', 'pending')
1873195b5e75SGreg Roach                            ->groupBy('xref');
1874195b5e75SGreg Roach                    })
1875*6c0bef3aSGreg Roach                    ->pluck('xref');
1876195b5e75SGreg Roach
187713abd6f3SGreg Roach                $this->list = [];
1878*6c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
1879*6c0bef3aSGreg Roach                    $this->list[] = GedcomRecord::getInstance($xref, $this->tree);
1880a6f13a4aSGreg Roach                }
1881a6f13a4aSGreg Roach                break;
1882a6f13a4aSGreg Roach            case 'individual':
188376156db1SGreg Roach                $sql_select   = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` ";
1884a6f13a4aSGreg Roach                $sql_join     = "";
1885825006d2SGreg Roach                $sql_where    = " WHERE i_file = :tree_id";
1886a6f13a4aSGreg Roach                $sql_order_by = "";
188772cf66d4SGreg Roach                $sql_params   = ['tree_id' => $this->tree->id()];
1888a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1889a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
189082759250SGreg Roach                        $value = $this->substituteVars($value, false);
1891a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1892a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1893a6f13a4aSGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)";
1894b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
18955d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1896a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
1897044416d2SGreg Roach                            if ($match[2] === 'LTE') {
18985d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
18995d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1900a6f13a4aSGreg Roach                            } else {
19015d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19025d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1903a6f13a4aSGreg Roach                            }
1904a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
1905a6f13a4aSGreg Roach                                $sortby = "";
1906a6f13a4aSGreg Roach                                $sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1";
1907a6f13a4aSGreg Roach                            }
1908a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1909a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
1910a6f13a4aSGreg Roach                            // Do nothing, unless you have to
1911044416d2SGreg Roach                            if ($match[1] != '' || $sortby === 'NAME') {
1912a6f13a4aSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)";
1913a6f13a4aSGreg Roach                                // Search the DB only if there is any name supplied
19147a6ee1acSGreg Roach                                if ($match[1] != '') {
19157a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19165d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19175d0bc43dSGreg Roach                                        $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
19185d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
1919a6f13a4aSGreg Roach                                    }
1920a6f13a4aSGreg Roach                                }
1921a6f13a4aSGreg Roach                                // Let the DB do the name sorting even when no name was entered
1922044416d2SGreg Roach                                if ($sortby === 'NAME') {
19237a6ee1acSGreg Roach                                    $sortby = '';
19247a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
1925a6f13a4aSGreg Roach                                }
1926a6f13a4aSGreg Roach                            }
1927a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1928a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19295d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom";
1930b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1931b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1932a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1933a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
19344f927915SGreg Roach                            // Don't unset this filter. This is just initial filtering
1935a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)";
1936a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##placelinks` AS {$attr}b ON ({$attr}a.p_file = {$attr}b.pl_file AND {$attr}b.pl_p_id = {$attr}a.p_id AND {$attr}b.pl_gid = i_id)";
19375d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
19385d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
1939a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19404f927915SGreg Roach                            // Don't unset this filter. This is just initial filtering
19415d0bc43dSGreg Roach                            $sql_where .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
19425d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
19435d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
19445d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
1945a6f13a4aSGreg Roach                        }
1946a6f13a4aSGreg Roach                    }
1947a6f13a4aSGreg Roach                }
1948a6f13a4aSGreg Roach
194913abd6f3SGreg Roach                $this->list = [];
1950a6f13a4aSGreg Roach                $rows       = Database::prepare(
1951a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
19525d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
1953a6f13a4aSGreg Roach
1954a6f13a4aSGreg Roach                foreach ($rows as $row) {
1955299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1956a6f13a4aSGreg Roach                }
1957a6f13a4aSGreg Roach                break;
1958a6f13a4aSGreg Roach
1959a6f13a4aSGreg Roach            case 'family':
196076156db1SGreg Roach                $sql_select   = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`";
1961a6f13a4aSGreg Roach                $sql_join     = "";
1962825006d2SGreg Roach                $sql_where    = " WHERE f_file = :tree_id";
1963a6f13a4aSGreg Roach                $sql_order_by = "";
196472cf66d4SGreg Roach                $sql_params   = ['tree_id' => $this->tree->id()];
1965a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1966a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
196782759250SGreg Roach                        $value = $this->substituteVars($value, false);
1968a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1969a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
1970a9eb55f8SGreg Roach                            $sql_join                   .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)";
1971b0d2e743SGreg Roach                            $sql_where                  .= " AND {$attr}.d_fact = :{$attr}fact";
19725d0bc43dSGreg Roach                            $sql_params[$attr . 'fact'] = $match[1];
1973a6f13a4aSGreg Roach                            $date                       = new Date($match[3]);
1974044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19755d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday2 <= :{$attr}date";
19765d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->maximumJulianDay();
1977a6f13a4aSGreg Roach                            } else {
19785d0bc43dSGreg Roach                                $sql_where                  .= " AND {$attr}.d_julianday1 >= :{$attr}date";
19795d0bc43dSGreg Roach                                $sql_params[$attr . 'date'] = $date->minimumJulianDay();
1980a6f13a4aSGreg Roach                            }
1981a6f13a4aSGreg Roach                            if ($sortby == $match[1]) {
19827a6ee1acSGreg Roach                                $sortby = '';
19837a6ee1acSGreg Roach                                $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1";
1984a6f13a4aSGreg Roach                            }
1985a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1986a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19875d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom";
1988b4e512fdSGreg Roach                            // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT"
1989b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
1990a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
1991a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) {
19925d0bc43dSGreg Roach                            // Do nothing, unless you have to
1993044416d2SGreg Roach                            if ($match[1] != '' || $sortby === 'NAME') {
19945d0bc43dSGreg Roach                                $sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)";
19955d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
19967a6ee1acSGreg Roach                                if ($match[1] != '') {
19977a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19985d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19995d0bc43dSGreg Roach                                        $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')";
20005d0bc43dSGreg Roach                                        $sql_params[$attr . 'name' . $n] = $name;
20015d0bc43dSGreg Roach                                    }
20025d0bc43dSGreg Roach                                }
20035d0bc43dSGreg Roach                                // Let the DB do the name sorting even when no name was entered
2004044416d2SGreg Roach                                if ($sortby === 'NAME') {
20057a6ee1acSGreg Roach                                    $sortby = '';
20067a6ee1acSGreg Roach                                    $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort";
2007a6f13a4aSGreg Roach                                }
20085d0bc43dSGreg Roach                            }
2009a6f13a4aSGreg Roach                            unset($attrs[$attr]); // This filter has been fully processed
2010a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
2011a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)";
2012a6f13a4aSGreg Roach                            $sql_join                    .= " JOIN `##placelinks` AS {$attr}b ON ({$attr}a.p_file={$attr}b.pl_file AND {$attr}b.pl_p_id={$attr}a.p_id AND {$attr}b.pl_gid=f_id)";
20135d0bc43dSGreg Roach                            $sql_where                   .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')";
20145d0bc43dSGreg Roach                            $sql_params[$attr . 'place'] = $match[1];
2015a6f13a4aSGreg Roach                        // Don't unset this filter. This is just initial filtering
2016a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
20175d0bc43dSGreg Roach                            $sql_where .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')";
20185d0bc43dSGreg Roach                            $sql_params[$attr . 'contains1'] = $match[1];
20195d0bc43dSGreg Roach                            $sql_params[$attr . 'contains2'] = $match[2];
20205d0bc43dSGreg Roach                            $sql_params[$attr . 'contains3'] = $match[3];
2021a6f13a4aSGreg Roach                            // Don't unset this filter. This is just initial filtering
2022a6f13a4aSGreg Roach                        }
2023a6f13a4aSGreg Roach                    }
2024a6f13a4aSGreg Roach                }
2025a6f13a4aSGreg Roach
202613abd6f3SGreg Roach                $this->list = [];
2027a6f13a4aSGreg Roach                $rows       = Database::prepare(
2028a6f13a4aSGreg Roach                    $sql_select . $sql_join . $sql_where . $sql_order_by
20295d0bc43dSGreg Roach                )->execute($sql_params)->fetchAll();
2030a6f13a4aSGreg Roach
2031a6f13a4aSGreg Roach                foreach ($rows as $row) {
2032299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2033a6f13a4aSGreg Roach                }
2034a6f13a4aSGreg Roach                break;
2035a6f13a4aSGreg Roach
2036a6f13a4aSGreg Roach            default:
2037a6f13a4aSGreg Roach                throw new \DomainException('Invalid list name: ' . $listname);
2038a6f13a4aSGreg Roach        }
2039a6f13a4aSGreg Roach
204013abd6f3SGreg Roach        $filters  = [];
204113abd6f3SGreg Roach        $filters2 = [];
2042a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2043a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2044a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2045a6f13a4aSGreg Roach                    $condition = $value;
2046a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2047a6f13a4aSGreg Roach                        $id    = $match[1];
2048a6f13a4aSGreg Roach                        $value = "''";
2049044416d2SGreg Roach                        if ($id === 'ID') {
20507a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2051a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2052a6f13a4aSGreg Roach                            }
2053044416d2SGreg Roach                        } elseif ($id === 'fact') {
2054a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2055044416d2SGreg Roach                        } elseif ($id === 'desc') {
2056a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2057a6f13a4aSGreg Roach                        } else {
2058a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20597a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2060a6f13a4aSGreg Roach                            }
2061a6f13a4aSGreg Roach                        }
2062a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2063a6f13a4aSGreg Roach                    }
2064a6f13a4aSGreg Roach                    //-- handle regular expressions
2065a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2066a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2067a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2068a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2069a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2070d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2071a6f13a4aSGreg Roach                            $val = trim($val);
2072a6f13a4aSGreg Roach                        }
2073a6f13a4aSGreg Roach                        if ($val) {
20747a6ee1acSGreg Roach                            $searchstr = '';
20757a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2076a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2077a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2078a6f13a4aSGreg Roach                                $level = 1;
2079a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2080a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2081a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2082a6f13a4aSGreg Roach                                    }
2083a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2084044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
20857a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2086a6f13a4aSGreg Roach                                    }
20877a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2088a6f13a4aSGreg Roach                                    $level++;
2089a6f13a4aSGreg Roach                                }
2090a6f13a4aSGreg Roach                            } else {
2091044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
20927a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2093a6f13a4aSGreg Roach                                }
2094a6f13a4aSGreg Roach                                $t         = $tag;
20957a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2096a6f13a4aSGreg Roach                            }
2097a6f13a4aSGreg Roach                            switch ($expr) {
20987a6ee1acSGreg Roach                                case 'CONTAINS':
2099044416d2SGreg Roach                                    if ($t === 'PLAC') {
2100a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2101a6f13a4aSGreg Roach                                    } else {
2102a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2103a6f13a4aSGreg Roach                                    }
2104a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2105a6f13a4aSGreg Roach                                    break;
2106a6f13a4aSGreg Roach                                default:
2107c1010edaSGreg Roach                                    $filters2[] = [
2108c1010edaSGreg Roach                                        'tag'  => $tag,
2109c1010edaSGreg Roach                                        'expr' => $expr,
2110c1010edaSGreg Roach                                        'val'  => $val,
2111c1010edaSGreg Roach                                    ];
2112a6f13a4aSGreg Roach                                    break;
2113a6f13a4aSGreg Roach                            }
2114a6f13a4aSGreg Roach                        }
2115a6f13a4aSGreg Roach                    }
2116a6f13a4aSGreg Roach                }
2117a6f13a4aSGreg Roach            }
2118a6f13a4aSGreg Roach        }
2119a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2120a6f13a4aSGreg Roach        if ($filters) {
2121a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2122a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2123299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2124a6f13a4aSGreg Roach                        unset($this->list[$key]);
2125a6f13a4aSGreg Roach                        break;
2126a6f13a4aSGreg Roach                    }
2127a6f13a4aSGreg Roach                }
2128a6f13a4aSGreg Roach            }
2129a6f13a4aSGreg Roach        }
2130a6f13a4aSGreg Roach        if ($filters2) {
213113abd6f3SGreg Roach            $mylist = [];
2132a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2133c0935879SGreg Roach                $key  = $indi->xref();
2134299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2135a6f13a4aSGreg Roach                $keep = true;
2136a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2137a6f13a4aSGreg Roach                    if ($keep) {
2138a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2139a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2140a6f13a4aSGreg Roach                        $val  = $filter['val'];
2141a6f13a4aSGreg Roach                        if ($val == "''") {
21427a6ee1acSGreg Roach                            $val = '';
2143a6f13a4aSGreg Roach                        }
21447a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2145a6f13a4aSGreg Roach                        $t    = end($tags);
21463d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2147a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2148044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21497a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21507a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2151a6f13a4aSGreg Roach                            $t    = end($tags);
21523d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2153a6f13a4aSGreg Roach                        }
2154a6f13a4aSGreg Roach
2155a6f13a4aSGreg Roach                        switch ($expr) {
21567a6ee1acSGreg Roach                            case 'GTE':
2157044416d2SGreg Roach                                if ($t === 'DATE') {
2158a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2159a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2160a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2161a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2162a6f13a4aSGreg Roach                                    $keep = true;
2163a6f13a4aSGreg Roach                                }
2164a6f13a4aSGreg Roach                                break;
21657a6ee1acSGreg Roach                            case 'LTE':
2166044416d2SGreg Roach                                if ($t === 'DATE') {
2167a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2168a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2169a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2170a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2171a6f13a4aSGreg Roach                                    $keep = true;
2172a6f13a4aSGreg Roach                                }
2173a6f13a4aSGreg Roach                                break;
2174a6f13a4aSGreg Roach                            default:
2175a6f13a4aSGreg Roach                                if ($v == $val) {
2176a6f13a4aSGreg Roach                                    $keep = true;
2177a6f13a4aSGreg Roach                                } else {
2178a6f13a4aSGreg Roach                                    $keep = false;
2179a6f13a4aSGreg Roach                                }
2180a6f13a4aSGreg Roach                                break;
2181a6f13a4aSGreg Roach                        }
2182a6f13a4aSGreg Roach                    }
2183a6f13a4aSGreg Roach                }
2184a6f13a4aSGreg Roach                if ($keep) {
2185a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2186a6f13a4aSGreg Roach                }
2187a6f13a4aSGreg Roach            }
2188a6f13a4aSGreg Roach            $this->list = $mylist;
2189a6f13a4aSGreg Roach        }
2190a6f13a4aSGreg Roach
2191a6f13a4aSGreg Roach        switch ($sortby) {
2192a6f13a4aSGreg Roach            case 'NAME':
2193a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2194a6f13a4aSGreg Roach                break;
2195a6f13a4aSGreg Roach            case 'CHAN':
219618d7a90dSGreg Roach                uasort($this->list, function (GedcomRecord $x, GedcomRecord $y): int {
2197a6f13a4aSGreg Roach                    return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2198a6f13a4aSGreg Roach                });
2199a6f13a4aSGreg Roach                break;
2200a6f13a4aSGreg Roach            case 'BIRT:DATE':
2201a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2202a6f13a4aSGreg Roach                break;
2203a6f13a4aSGreg Roach            case 'DEAT:DATE':
2204a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2205a6f13a4aSGreg Roach                break;
2206a6f13a4aSGreg Roach            case 'MARR:DATE':
22075d0bc43dSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2208a6f13a4aSGreg Roach                break;
2209a6f13a4aSGreg Roach            default:
2210a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2211a6f13a4aSGreg Roach                break;
2212a6f13a4aSGreg Roach        }
2213a6f13a4aSGreg Roach
22149b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2215e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2216a6f13a4aSGreg Roach    }
2217a6f13a4aSGreg Roach
2218a6f13a4aSGreg Roach    /**
221976692c8bSGreg Roach     * XML <List>
22208ba2e626SGreg Roach     *
22218ba2e626SGreg Roach     * @return void
2222a6f13a4aSGreg Roach     */
2223c1010edaSGreg Roach    private function listEndHandler()
2224c1010edaSGreg Roach    {
2225a6f13a4aSGreg Roach        $this->process_repeats--;
2226a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2227a6f13a4aSGreg Roach            return;
2228a6f13a4aSGreg Roach        }
2229a6f13a4aSGreg Roach
2230a6f13a4aSGreg Roach        // Check if there is any list
2231a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2232a6f13a4aSGreg Roach            $lineoffset = 0;
2233a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2234a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2235a6f13a4aSGreg Roach            }
2236a6f13a4aSGreg Roach            //-- read the xml from the file
2237299d100dSGreg Roach            $lines = file($this->report);
22387a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2239a6f13a4aSGreg Roach                $lineoffset--;
2240a6f13a4aSGreg Roach            }
2241a6f13a4aSGreg Roach            $lineoffset++;
2242a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2243a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2244a6f13a4aSGreg Roach            // List Level counter
2245a6f13a4aSGreg Roach            $count = 1;
2246a6f13a4aSGreg Roach            while (0 < $count) {
22477a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2248a6f13a4aSGreg Roach                    $count++;
22497a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2250a6f13a4aSGreg Roach                    $count--;
2251a6f13a4aSGreg Roach                }
2252a6f13a4aSGreg Roach                if (0 < $count) {
2253a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2254a6f13a4aSGreg Roach                }
2255a6f13a4aSGreg Roach                $line_nr++;
2256a6f13a4aSGreg Roach            }
2257a6f13a4aSGreg Roach            // No need to drag this
2258a6f13a4aSGreg Roach            unset($lines);
22597a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2260a6f13a4aSGreg Roach            // Save original values
22619b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2262a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2263a6f13a4aSGreg Roach
2264a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2265a6f13a4aSGreg Roach            $this->list_private = 0;
2266a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2267a6f13a4aSGreg Roach                if ($record->canShow()) {
2268f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2269a6f13a4aSGreg Roach                    //-- start the sax parser
2270a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2271e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2272a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22731aa04befSGreg Roach
22741aa04befSGreg Roach                    xml_set_element_handler(
22751aa04befSGreg Roach                        $repeat_parser,
22761aa04befSGreg Roach                        function ($parser, string $name, array $attrs) {
22771aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
22781aa04befSGreg Roach                        },
22791aa04befSGreg Roach                        function ($parser, string $name) {
22801aa04befSGreg Roach                            $this->endElement($parser, $name);
22811aa04befSGreg Roach                        }
22821aa04befSGreg Roach                    );
22831aa04befSGreg Roach
22841aa04befSGreg Roach                    xml_set_character_data_handler(
22851aa04befSGreg Roach                        $repeat_parser,
22861aa04befSGreg Roach                        function ($parser, $data) {
22871aa04befSGreg Roach                            $this->characterData($parser, $data);
22881aa04befSGreg Roach                        }
22891aa04befSGreg Roach                    );
22901aa04befSGreg Roach
2291a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
2292a6f13a4aSGreg Roach                        throw new \DomainException(sprintf(
2293a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2294a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2295a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2296a6f13a4aSGreg Roach                        ));
2297a6f13a4aSGreg Roach                    }
2298a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2299a6f13a4aSGreg Roach                } else {
2300a6f13a4aSGreg Roach                    $this->list_private++;
2301a6f13a4aSGreg Roach                }
2302a6f13a4aSGreg Roach            }
230313abd6f3SGreg Roach            $this->list   = [];
2304e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2305a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2306a6f13a4aSGreg Roach        }
230765e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2308a6f13a4aSGreg Roach    }
2309a6f13a4aSGreg Roach
2310a6f13a4aSGreg Roach    /**
2311a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2312a6f13a4aSGreg Roach     * Prints the total number of records in a list
2313a6f13a4aSGreg Roach     * The total number is collected from
2314a6f13a4aSGreg Roach     * List and Relatives
23158ba2e626SGreg Roach     *
23168ba2e626SGreg Roach     * @return void
2317a6f13a4aSGreg Roach     */
2318c1010edaSGreg Roach    private function listTotalStartHandler()
2319c1010edaSGreg Roach    {
2320a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2321589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2322a6f13a4aSGreg Roach        } else {
23237a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2324a6f13a4aSGreg Roach        }
2325a6f13a4aSGreg Roach    }
2326a6f13a4aSGreg Roach
2327a6f13a4aSGreg Roach    /**
232876692c8bSGreg Roach     * XML <Relatives>
232976692c8bSGreg Roach     *
2330c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23318ba2e626SGreg Roach     *
23328ba2e626SGreg Roach     * @return void
2333a6f13a4aSGreg Roach     */
2334c0fe75acSGreg Roach    private function relativesStartHandler(array $attrs)
2335c1010edaSGreg Roach    {
2336a6f13a4aSGreg Roach        $this->process_repeats++;
2337a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2338a6f13a4aSGreg Roach            return;
2339a6f13a4aSGreg Roach        }
2340a6f13a4aSGreg Roach
23417a6ee1acSGreg Roach        $sortby = 'NAME';
2342a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
2343a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
2344a6f13a4aSGreg Roach        }
234513abd6f3SGreg Roach        $match = [];
2346a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2347d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2348a6f13a4aSGreg Roach            $sortby = trim($sortby);
2349a6f13a4aSGreg Roach        }
2350a6f13a4aSGreg Roach
2351a6f13a4aSGreg Roach        $maxgen = -1;
2352a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2353a6f13a4aSGreg Roach            $maxgen = $attrs['maxgen'];
2354a6f13a4aSGreg Roach        }
2355044416d2SGreg Roach        if ($maxgen === '*') {
2356a6f13a4aSGreg Roach            $maxgen = -1;
2357a6f13a4aSGreg Roach        }
2358a6f13a4aSGreg Roach
23597a6ee1acSGreg Roach        $group = 'child-family';
2360a6f13a4aSGreg Roach        if (isset($attrs['group'])) {
2361a6f13a4aSGreg Roach            $group = $attrs['group'];
2362a6f13a4aSGreg Roach        }
2363a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2364d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2365a6f13a4aSGreg Roach            $group = trim($group);
2366a6f13a4aSGreg Roach        }
2367a6f13a4aSGreg Roach
23687a6ee1acSGreg Roach        $id = '';
2369a6f13a4aSGreg Roach        if (isset($attrs['id'])) {
2370a6f13a4aSGreg Roach            $id = $attrs['id'];
2371a6f13a4aSGreg Roach        }
2372a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2373d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2374a6f13a4aSGreg Roach            $id = trim($id);
2375a6f13a4aSGreg Roach        }
2376a6f13a4aSGreg Roach
237713abd6f3SGreg Roach        $this->list = [];
2378299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2379a6f13a4aSGreg Roach        if (!empty($person)) {
2380a6f13a4aSGreg Roach            $this->list[$id] = $person;
2381a6f13a4aSGreg Roach            switch ($group) {
23827a6ee1acSGreg Roach                case 'child-family':
2383a6f13a4aSGreg Roach                    foreach ($person->getChildFamilies() as $family) {
2384a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2385a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2386a6f13a4aSGreg Roach                        if (!empty($husband)) {
2387c0935879SGreg Roach                            $this->list[$husband->xref()] = $husband;
2388a6f13a4aSGreg Roach                        }
2389a6f13a4aSGreg Roach                        if (!empty($wife)) {
2390c0935879SGreg Roach                            $this->list[$wife->xref()] = $wife;
2391a6f13a4aSGreg Roach                        }
2392a6f13a4aSGreg Roach                        $children = $family->getChildren();
2393a6f13a4aSGreg Roach                        foreach ($children as $child) {
2394a6f13a4aSGreg Roach                            if (!empty($child)) {
2395c0935879SGreg Roach                                $this->list[$child->xref()] = $child;
2396a6f13a4aSGreg Roach                            }
2397a6f13a4aSGreg Roach                        }
2398a6f13a4aSGreg Roach                    }
2399a6f13a4aSGreg Roach                    break;
24007a6ee1acSGreg Roach                case 'spouse-family':
2401a6f13a4aSGreg Roach                    foreach ($person->getSpouseFamilies() as $family) {
2402a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2403a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2404a6f13a4aSGreg Roach                        if (!empty($husband)) {
2405c0935879SGreg Roach                            $this->list[$husband->xref()] = $husband;
2406a6f13a4aSGreg Roach                        }
2407a6f13a4aSGreg Roach                        if (!empty($wife)) {
2408c0935879SGreg Roach                            $this->list[$wife->xref()] = $wife;
2409a6f13a4aSGreg Roach                        }
2410a6f13a4aSGreg Roach                        $children = $family->getChildren();
2411a6f13a4aSGreg Roach                        foreach ($children as $child) {
2412a6f13a4aSGreg Roach                            if (!empty($child)) {
2413c0935879SGreg Roach                                $this->list[$child->xref()] = $child;
2414a6f13a4aSGreg Roach                            }
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':
2437a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2438a6f13a4aSGreg Roach                break;
2439a6f13a4aSGreg Roach            case 'BIRT:DATE':
2440a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2441a6f13a4aSGreg Roach                break;
2442a6f13a4aSGreg Roach            case 'DEAT:DATE':
2443a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
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     */
2474c1010edaSGreg Roach    private function relativesEndHandler()
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;
2517a6f13a4aSGreg Roach            foreach ($this->list as $key => $value) {
2518a6f13a4aSGreg Roach                if (isset($value->generation)) {
2519a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2520a6f13a4aSGreg Roach                }
2521299d100dSGreg Roach                $tmp          = GedcomRecord::getInstance($key, $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,
25301aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
25311aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25321aa04befSGreg Roach                    },
25331aa04befSGreg Roach                    function ($parser, string $name) {
25341aa04befSGreg Roach                        $this->endElement($parser, $name);
25351aa04befSGreg Roach                    }
25361aa04befSGreg Roach                );
25371aa04befSGreg Roach
25381aa04befSGreg Roach                xml_set_character_data_handler(
25391aa04befSGreg Roach                    $repeat_parser,
25401aa04befSGreg Roach                    function ($parser, $data) {
25411aa04befSGreg Roach                        $this->characterData($parser, $data);
25421aa04befSGreg Roach                    }
25431aa04befSGreg Roach                );
2544a6f13a4aSGreg Roach
2545a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25467a6ee1acSGreg 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     */
2564c1010edaSGreg Roach    private function generationStartHandler()
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     */
2575c1010edaSGreg Roach    private function newPageStartHandler()
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     */
2589c0fe75acSGreg Roach    private function htmlStartHandler(string $tag, array $attrs)
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     */
2609c1010edaSGreg Roach    private function htmlEndHandler($tag)
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     */
2630c1010edaSGreg Roach    private function inputStartHandler()
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     */
2640c1010edaSGreg Roach    private function inputEndHandler()
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     */
2650c1010edaSGreg Roach    private function reportStartHandler()
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     */
2660c1010edaSGreg Roach    private function reportEndHandler()
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     */
2670c1010edaSGreg Roach    private function titleEndHandler()
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     */
2680c1010edaSGreg Roach    private function descriptionEndHandler()
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     */
2695c1010edaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1)
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        }
2707729ce104SGreg Roach        foreach ($person->getSpouseFamilies() as $family) {
2708729ce104SGreg Roach            if ($parents) {
2709729ce104SGreg Roach                $husband = $family->getHusband();
2710729ce104SGreg Roach                $wife    = $family->getWife();
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            }
2728729ce104SGreg Roach            $children = $family->getChildren();
2729729ce104SGreg Roach            foreach ($children as $child) {
2730729ce104SGreg Roach                if ($child) {
2731c0935879SGreg Roach                    $list[$child->xref()] = $child;
2732729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2733c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2734729ce104SGreg Roach                    } else {
2735c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2736729ce104SGreg Roach                    }
2737729ce104SGreg Roach                }
2738729ce104SGreg Roach            }
2739729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2740729ce104SGreg Roach                foreach ($children as $child) {
2741c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2742729ce104SGreg Roach                }
2743729ce104SGreg Roach            }
2744729ce104SGreg Roach        }
2745729ce104SGreg Roach    }
2746729ce104SGreg Roach
2747729ce104SGreg Roach    /**
274876692c8bSGreg Roach     * Create a list of all ancestors.
274976692c8bSGreg Roach     *
2750729ce104SGreg Roach     * @param string[] $list
2751729ce104SGreg Roach     * @param string   $pid
2752729ce104SGreg Roach     * @param bool     $children
2753729ce104SGreg Roach     * @param int      $generations
27548ba2e626SGreg Roach     *
27558ba2e626SGreg Roach     * @return void
2756729ce104SGreg Roach     */
2757c1010edaSGreg Roach    private function addAncestors(&$list, $pid, $children = false, $generations = -1)
2758c1010edaSGreg Roach    {
275913abd6f3SGreg Roach        $genlist                = [$pid];
2760729ce104SGreg Roach        $list[$pid]->generation = 1;
2761729ce104SGreg Roach        while (count($genlist) > 0) {
2762729ce104SGreg Roach            $id = array_shift($genlist);
2763729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2764729ce104SGreg Roach                continue; // id can be something like “empty7”
2765729ce104SGreg Roach            }
2766299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
2767729ce104SGreg Roach            foreach ($person->getChildFamilies() as $family) {
2768729ce104SGreg Roach                $husband = $family->getHusband();
2769729ce104SGreg Roach                $wife    = $family->getWife();
2770729ce104SGreg Roach                if ($husband) {
2771c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2772c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2773729ce104SGreg Roach                }
2774729ce104SGreg Roach                if ($wife) {
2775c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2776c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2777729ce104SGreg Roach                }
2778729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2779729ce104SGreg Roach                    if ($husband) {
2780c0935879SGreg Roach                        $genlist[] = $husband->xref();
2781729ce104SGreg Roach                    }
2782729ce104SGreg Roach                    if ($wife) {
2783c0935879SGreg Roach                        $genlist[] = $wife->xref();
2784729ce104SGreg Roach                    }
2785729ce104SGreg Roach                }
2786729ce104SGreg Roach                if ($children) {
2787729ce104SGreg Roach                    foreach ($family->getChildren() as $child) {
2788c0935879SGreg Roach                        $list[$child->xref()] = $child;
2789729ce104SGreg Roach                        if (isset($list[$id]->generation)) {
2790c0935879SGreg Roach                            $list[$child->xref()]->generation = $list[$id]->generation;
2791729ce104SGreg Roach                        } else {
2792c0935879SGreg Roach                            $list[$child->xref()]->generation = 1;
2793729ce104SGreg Roach                        }
2794729ce104SGreg Roach                    }
2795729ce104SGreg Roach                }
2796729ce104SGreg Roach            }
2797729ce104SGreg Roach        }
2798729ce104SGreg Roach    }
2799729ce104SGreg Roach
2800729ce104SGreg Roach    /**
2801729ce104SGreg Roach     * get gedcom tag value
2802729ce104SGreg Roach     *
2803729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2804729ce104SGreg 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
2805729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2806729ce104SGreg Roach     *
2807729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2808729ce104SGreg Roach     */
28098f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2810c1010edaSGreg Roach    {
2811729ce104SGreg Roach        if (empty($gedrec)) {
2812729ce104SGreg Roach            return '';
2813729ce104SGreg Roach        }
2814729ce104SGreg Roach        $tags      = explode(':', $tag);
2815729ce104SGreg Roach        $origlevel = $level;
2816729ce104SGreg Roach        if ($level == 0) {
28173c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2818729ce104SGreg Roach        }
2819729ce104SGreg Roach
2820729ce104SGreg Roach        $subrec = $gedrec;
2821729ce104SGreg Roach        foreach ($tags as $t) {
2822729ce104SGreg Roach            $lastsubrec = $subrec;
28233d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2824729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2825729ce104SGreg Roach                $level--;
28263d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2827729ce104SGreg Roach            }
2828729ce104SGreg Roach            if (empty($subrec)) {
2829044416d2SGreg Roach                if ($t === 'TITL') {
28303d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2831729ce104SGreg Roach                    if (!empty($subrec)) {
28327a6ee1acSGreg Roach                        $t = 'ABBR';
2833729ce104SGreg Roach                    }
2834729ce104SGreg Roach                }
2835729ce104SGreg Roach                if (empty($subrec)) {
2836729ce104SGreg Roach                    if ($level > 0) {
2837729ce104SGreg Roach                        $level--;
2838729ce104SGreg Roach                    }
28393d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2840729ce104SGreg Roach                    if (empty($subrec)) {
2841729ce104SGreg Roach                        return '';
2842729ce104SGreg Roach                    }
2843729ce104SGreg Roach                }
2844729ce104SGreg Roach            }
2845729ce104SGreg Roach            $level++;
2846729ce104SGreg Roach        }
2847729ce104SGreg Roach        $level--;
2848729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2849729ce104SGreg Roach        if ($ct == 0) {
2850729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2851729ce104SGreg Roach        }
2852729ce104SGreg Roach        if ($ct == 0) {
2853729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2854729ce104SGreg Roach        }
2855729ce104SGreg Roach        if ($ct > 0) {
2856729ce104SGreg Roach            $value = trim($match[1]);
2857044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2858299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2859ff166e64SGreg Roach                if ($note instanceof Note) {
2860729ce104SGreg Roach                    $value = $note->getNote();
2861729ce104SGreg Roach                } else {
2862729ce104SGreg Roach                    //-- set the value to the id without the @
2863729ce104SGreg Roach                    $value = $match[1];
2864729ce104SGreg Roach                }
2865729ce104SGreg Roach            }
28667a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28673d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2868729ce104SGreg Roach            }
2869729ce104SGreg Roach
2870729ce104SGreg Roach            return $value;
2871729ce104SGreg Roach        }
2872729ce104SGreg Roach
28737a6ee1acSGreg Roach        return '';
2874729ce104SGreg Roach    }
2875d1286247SGreg Roach
2876d1286247SGreg Roach    /**
2877d1286247SGreg Roach     * Replace variable identifiers with their values.
2878d1286247SGreg Roach     *
2879d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
288082759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2881d1286247SGreg Roach     *
2882d1286247SGreg Roach     * @return string
2883d1286247SGreg Roach     */
28848f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2885c1010edaSGreg Roach    {
2886d1286247SGreg Roach        return preg_replace_callback(
2887d1286247SGreg Roach            '/\$(\w+)/',
288818d7a90dSGreg Roach            function (array $matches) use ($quote): string {
28892118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
289082759250SGreg Roach                    if ($quote) {
28912118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2892b2ce94c6SRico Sonntag                    }
2893b2ce94c6SRico Sonntag
28942118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
289582759250SGreg Roach                }
2896b2ce94c6SRico Sonntag
2897d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
28983d7a8a4cSGreg Roach
2899d1286247SGreg Roach                return '$' . $matches[1];
2900d1286247SGreg Roach            },
2901d1286247SGreg Roach            $expression
2902d1286247SGreg Roach        );
2903d1286247SGreg Roach    }
2904a6f13a4aSGreg Roach}
2905