xref: /webtrees/app/Report/ReportParserGenerate.php (revision 9dc7e9e3591406f239cf15f0cdb58517a816694f)
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;
395985adfbSGreg Roachuse Illuminate\Database\Query\JoinClause;
409a9e551aSGreg Roachuse Illuminate\Support\Str;
4179529c87SGreg Roachuse stdClass;
42c0fe75acSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
435809450fSGreg Roachuse Symfony\Component\ExpressionLanguage\ExpressionLanguage;
44a6f13a4aSGreg Roach
45a6f13a4aSGreg Roach/**
46a6f13a4aSGreg Roach * Class ReportParserGenerate - parse a report.xml file and generate the report.
47a6f13a4aSGreg Roach */
48c1010edaSGreg Roachclass ReportParserGenerate extends ReportParserBase
49c1010edaSGreg Roach{
50a6f13a4aSGreg Roach    /** @var bool Are we collecting data from <Footnote> elements */
51a6f13a4aSGreg Roach    private $process_footnote = true;
52a6f13a4aSGreg Roach
53a6f13a4aSGreg Roach    /** @var bool Are we currently outputing data? */
54a6f13a4aSGreg Roach    private $print_data = false;
55a6f13a4aSGreg Roach
56a6f13a4aSGreg Roach    /** @var bool[] Push-down stack of $print_data */
5713abd6f3SGreg Roach    private $print_data_stack = [];
58a6f13a4aSGreg Roach
5976692c8bSGreg Roach    /** @var int Are we processing GEDCOM data */
60a6f13a4aSGreg Roach    private $process_gedcoms = 0;
61a6f13a4aSGreg Roach
6276692c8bSGreg Roach    /** @var int Are we processing conditionals */
63a6f13a4aSGreg Roach    private $process_ifs = 0;
64a6f13a4aSGreg Roach
6576692c8bSGreg Roach    /** @var int Are we processing repeats */
66a6f13a4aSGreg Roach    private $process_repeats = 0;
67a6f13a4aSGreg Roach
68a6f13a4aSGreg Roach    /** @var int Quantity of data to repeat during loops */
69a6f13a4aSGreg Roach    private $repeat_bytes = 0;
70a6f13a4aSGreg Roach
715b084b24SGreg Roach    /** @var string[] Repeated data when iterating over loops */
7213abd6f3SGreg Roach    private $repeats = [];
73a6f13a4aSGreg Roach
74a6f13a4aSGreg Roach    /** @var array[] Nested repeating data */
7513abd6f3SGreg Roach    private $repeats_stack = [];
76a6f13a4aSGreg Roach
77208e9f76SGreg Roach    /** @var AbstractReport[] Nested repeating data */
7813abd6f3SGreg Roach    private $wt_report_stack = [];
79e8e7866bSGreg Roach
80e8e7866bSGreg Roach    /** @var resource Nested repeating data */
81e8e7866bSGreg Roach    private $parser;
82e8e7866bSGreg Roach
83e8e7866bSGreg Roach    /** @var resource[] Nested repeating data */
8413abd6f3SGreg Roach    private $parser_stack = [];
85e8e7866bSGreg Roach
86a6f13a4aSGreg Roach    /** @var string The current GEDCOM record */
87a6f13a4aSGreg Roach    private $gedrec = '';
88a6f13a4aSGreg Roach
89a6f13a4aSGreg Roach    /** @var string[] Nested GEDCOM records */
9013abd6f3SGreg Roach    private $gedrec_stack = [];
91a6f13a4aSGreg Roach
92a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
93a6f13a4aSGreg Roach    private $current_element;
94a6f13a4aSGreg Roach
95a6f13a4aSGreg Roach    /** @var ReportBaseElement The currently processed element */
96a6f13a4aSGreg Roach    private $footnote_element;
97a6f13a4aSGreg Roach
98a6f13a4aSGreg Roach    /** @var string The GEDCOM fact currently being processed */
99a6f13a4aSGreg Roach    private $fact = '';
100a6f13a4aSGreg Roach
101a6f13a4aSGreg Roach    /** @var string The GEDCOM value currently being processed */
102a6f13a4aSGreg Roach    private $desc = '';
103a6f13a4aSGreg Roach
104a6f13a4aSGreg Roach    /** @var string The GEDCOM type currently being processed */
105a6f13a4aSGreg Roach    private $type = '';
106a6f13a4aSGreg Roach
107a6f13a4aSGreg Roach    /** @var int The current generational level */
108a6f13a4aSGreg Roach    private $generation = 1;
109a6f13a4aSGreg Roach
110a6f13a4aSGreg Roach    /** @var array Source data for processing lists */
11113abd6f3SGreg Roach    private $list = [];
112a6f13a4aSGreg Roach
113a6f13a4aSGreg Roach    /** @var int Number of items in lists */
114a6f13a4aSGreg Roach    private $list_total = 0;
115a6f13a4aSGreg Roach
116a6f13a4aSGreg Roach    /** @var int Number of items filtered from lists */
117a6f13a4aSGreg Roach    private $list_private = 0;
118a6f13a4aSGreg Roach
119299d100dSGreg Roach    /** @var string The filename of the XML report */
120299d100dSGreg Roach    protected $report;
121299d100dSGreg Roach
122208e9f76SGreg Roach    /** @var AbstractReport A factory for creating report elements */
123e8e7866bSGreg Roach    private $report_root;
124e8e7866bSGreg Roach
125208e9f76SGreg Roach    /** @var AbstractReport Nested report elements */
126e8e7866bSGreg Roach    private $wt_report;
127e8e7866bSGreg Roach
128d1286247SGreg Roach    /** @var string[][] Variables defined in the report at run-time */
1292118c0e3SGreg Roach    private $vars;
130d1286247SGreg Roach
131299d100dSGreg Roach    /** @var Tree The current tree */
132299d100dSGreg Roach    private $tree;
133299d100dSGreg Roach
13476692c8bSGreg Roach    /**
13576692c8bSGreg Roach     * Create a parser for a report
13676692c8bSGreg Roach     *
13776692c8bSGreg Roach     * @param string         $report The XML filename
138208e9f76SGreg Roach     * @param AbstractReport $report_root
13976692c8bSGreg Roach     * @param string[][]     $vars
140299d100dSGreg Roach     * @param Tree           $tree
14176692c8bSGreg Roach     */
142208e9f76SGreg Roach    public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree)
143c1010edaSGreg Roach    {
144299d100dSGreg Roach        $this->report          = $report;
145e8e7866bSGreg Roach        $this->report_root     = $report_root;
146e8e7866bSGreg Roach        $this->wt_report       = $report_root;
14759f2f229SGreg Roach        $this->current_element = new ReportBaseElement();
148d1286247SGreg Roach        $this->vars            = $vars;
149299d100dSGreg Roach        $this->tree            = $tree;
150299d100dSGreg Roach
15176f666f4SGreg Roach        parent::__construct($report);
152a6f13a4aSGreg Roach    }
153a6f13a4aSGreg Roach
154a6f13a4aSGreg Roach    /**
155a6f13a4aSGreg Roach     * XML start element handler
156a6f13a4aSGreg Roach     * This function is called whenever a starting element is reached
157a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
158a6f13a4aSGreg Roach     *
159a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
160a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
1618a4ee39cSGreg Roach     * @param string[] $attrs  an array of key value pairs for the attributes
16218d7a90dSGreg Roach     *
16318d7a90dSGreg Roach     * @return void
164a6f13a4aSGreg Roach     */
1658a4ee39cSGreg Roach    protected function startElement($parser, string $name, array $attrs)
166c1010edaSGreg Roach    {
16713abd6f3SGreg Roach        $newattrs = [];
168a6f13a4aSGreg Roach
169a6f13a4aSGreg Roach        foreach ($attrs as $key => $value) {
170a6f13a4aSGreg Roach            if (preg_match("/^\\$(\w+)$/", $value, $match)) {
171d1286247SGreg Roach                if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) {
172d1286247SGreg Roach                    $value = $this->vars[$match[1]]['id'];
173a6f13a4aSGreg Roach                }
174a6f13a4aSGreg Roach            }
175a6f13a4aSGreg Roach            $newattrs[$key] = $value;
176a6f13a4aSGreg Roach        }
177a6f13a4aSGreg Roach        $attrs = $newattrs;
1787a6ee1acSGreg 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')) {
179a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
180a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
181208e9f76SGreg Roach
182a6f13a4aSGreg Roach            if (method_exists($this, $start_method)) {
183a6f13a4aSGreg Roach                $this->$start_method($attrs);
184a6f13a4aSGreg Roach            } elseif (!method_exists($this, $end_method)) {
185a6f13a4aSGreg Roach                $this->htmlStartHandler($name, $attrs);
186a6f13a4aSGreg Roach            }
187a6f13a4aSGreg Roach        }
188a6f13a4aSGreg Roach    }
189a6f13a4aSGreg Roach
190a6f13a4aSGreg Roach    /**
191a6f13a4aSGreg Roach     * XML end element handler
192a6f13a4aSGreg Roach     * This function is called whenever an ending element is reached
193a6f13a4aSGreg Roach     * The element handler will be called if found, otherwise it must be HTML
194a6f13a4aSGreg Roach     *
195a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
196a6f13a4aSGreg Roach     * @param string   $name   the name of the XML element parsed
19718d7a90dSGreg Roach     *
19818d7a90dSGreg Roach     * @return void
199a6f13a4aSGreg Roach     */
2008a4ee39cSGreg Roach    protected function endElement($parser, string $name)
201c1010edaSGreg Roach    {
2027a6ee1acSGreg 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')) {
203a6f13a4aSGreg Roach            $start_method = $name . 'StartHandler';
204a6f13a4aSGreg Roach            $end_method   = $name . 'EndHandler';
205a6f13a4aSGreg Roach            if (method_exists($this, $end_method)) {
206a6f13a4aSGreg Roach                $this->$end_method();
207a6f13a4aSGreg Roach            } elseif (!method_exists($this, $start_method)) {
208a6f13a4aSGreg Roach                $this->htmlEndHandler($name);
209a6f13a4aSGreg Roach            }
210a6f13a4aSGreg Roach        }
211a6f13a4aSGreg Roach    }
212a6f13a4aSGreg Roach
213a6f13a4aSGreg Roach    /**
214a6f13a4aSGreg Roach     * XML character data handler
215a6f13a4aSGreg Roach     *
216a6f13a4aSGreg Roach     * @param resource $parser the resource handler for the XML parser
217a6f13a4aSGreg Roach     * @param string   $data   the name of the XML element parsed
21818d7a90dSGreg Roach     *
21918d7a90dSGreg Roach     * @return void
220a6f13a4aSGreg Roach     */
221c1010edaSGreg Roach    protected function characterData($parser, $data)
222c1010edaSGreg Roach    {
223e8e7866bSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) {
224a6f13a4aSGreg Roach            $this->current_element->addText($data);
225a6f13a4aSGreg Roach        }
226a6f13a4aSGreg Roach    }
227a6f13a4aSGreg Roach
228a6f13a4aSGreg Roach    /**
22976692c8bSGreg Roach     * XML <style>
230a6f13a4aSGreg Roach     *
231c0fe75acSGreg Roach     * @param string[]  $attrs an array of key value pairs for the attributes
2328ba2e626SGreg Roach     *
2338ba2e626SGreg Roach     * @return void
234a6f13a4aSGreg Roach     */
235c0fe75acSGreg Roach    private function styleStartHandler(array $attrs)
236c1010edaSGreg Roach    {
237a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
238a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.');
239a6f13a4aSGreg Roach        }
240a6f13a4aSGreg Roach
241a6f13a4aSGreg Roach        // array Style that will be passed on
24213abd6f3SGreg Roach        $s = [];
243a6f13a4aSGreg Roach
244a6f13a4aSGreg Roach        // string Name af the style
245a6f13a4aSGreg Roach        $s['name'] = $attrs['name'];
246a6f13a4aSGreg Roach
247a6f13a4aSGreg Roach        // string Name of the DEFAULT font
248208e9f76SGreg Roach        $s['font'] = $this->wt_report->default_font;
249a6f13a4aSGreg Roach        if (!empty($attrs['font'])) {
250a6f13a4aSGreg Roach            $s['font'] = $attrs['font'];
251a6f13a4aSGreg Roach        }
252a6f13a4aSGreg Roach
253a6f13a4aSGreg Roach        // int The size of the font in points
254208e9f76SGreg Roach        $s['size'] = $this->wt_report->default_font_size;
255a6f13a4aSGreg Roach        if (!empty($attrs['size'])) {
256a6f13a4aSGreg Roach            $s['size'] = (int) $attrs['size'];
257a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
258a6f13a4aSGreg Roach
259a6f13a4aSGreg Roach        // string B: bold, I: italic, U: underline, D: line trough, The default value is regular.
2607a6ee1acSGreg Roach        $s['style'] = '';
261a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
262a6f13a4aSGreg Roach            $s['style'] = $attrs['style'];
263a6f13a4aSGreg Roach        }
264a6f13a4aSGreg Roach
265e8e7866bSGreg Roach        $this->wt_report->addStyle($s);
266a6f13a4aSGreg Roach    }
267a6f13a4aSGreg Roach
268a6f13a4aSGreg Roach    /**
26976692c8bSGreg Roach     * XML <Doc>
270a6f13a4aSGreg Roach     * Sets up the basics of the document proparties
271a6f13a4aSGreg Roach     *
272c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
2738ba2e626SGreg Roach     *
2748ba2e626SGreg Roach     * @return void
275a6f13a4aSGreg Roach     */
276c0fe75acSGreg Roach    private function docStartHandler(array $attrs)
277c1010edaSGreg Roach    {
278e8e7866bSGreg Roach        $this->parser = $this->xml_parser;
279a6f13a4aSGreg Roach
280a6f13a4aSGreg Roach        // Custom page width
281a6f13a4aSGreg Roach        if (!empty($attrs['customwidth'])) {
282208e9f76SGreg Roach            $this->wt_report->page_width = (int) $attrs['customwidth'];
283a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
284a6f13a4aSGreg Roach        // Custom Page height
285a6f13a4aSGreg Roach        if (!empty($attrs['customheight'])) {
286208e9f76SGreg Roach            $this->wt_report->page_height = (int) $attrs['customheight'];
287a6f13a4aSGreg Roach        } // Get it as int to ignore all decimal points or text (if any text then int(0))
288a6f13a4aSGreg Roach
289a6f13a4aSGreg Roach        // Left Margin
290a6f13a4aSGreg Roach        if (isset($attrs['leftmargin'])) {
2917a6ee1acSGreg Roach            if ($attrs['leftmargin'] === '0') {
292208e9f76SGreg Roach                $this->wt_report->left_margin = 0;
293a6f13a4aSGreg Roach            } elseif (!empty($attrs['leftmargin'])) {
294208e9f76SGreg 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))
295a6f13a4aSGreg Roach            }
296a6f13a4aSGreg Roach        }
297a6f13a4aSGreg Roach        // Right Margin
298a6f13a4aSGreg Roach        if (isset($attrs['rightmargin'])) {
2997a6ee1acSGreg Roach            if ($attrs['rightmargin'] === '0') {
300208e9f76SGreg Roach                $this->wt_report->right_margin = 0;
301a6f13a4aSGreg Roach            } elseif (!empty($attrs['rightmargin'])) {
302208e9f76SGreg 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))
303a6f13a4aSGreg Roach            }
304a6f13a4aSGreg Roach        }
305a6f13a4aSGreg Roach        // Top Margin
306a6f13a4aSGreg Roach        if (isset($attrs['topmargin'])) {
3077a6ee1acSGreg Roach            if ($attrs['topmargin'] === '0') {
308208e9f76SGreg Roach                $this->wt_report->top_margin = 0;
309a6f13a4aSGreg Roach            } elseif (!empty($attrs['topmargin'])) {
310208e9f76SGreg 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))
311a6f13a4aSGreg Roach            }
312a6f13a4aSGreg Roach        }
313a6f13a4aSGreg Roach        // Bottom Margin
314a6f13a4aSGreg Roach        if (isset($attrs['bottommargin'])) {
3157a6ee1acSGreg Roach            if ($attrs['bottommargin'] === '0') {
316208e9f76SGreg Roach                $this->wt_report->bottom_margin = 0;
317a6f13a4aSGreg Roach            } elseif (!empty($attrs['bottommargin'])) {
318208e9f76SGreg 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))
319a6f13a4aSGreg Roach            }
320a6f13a4aSGreg Roach        }
321a6f13a4aSGreg Roach        // Header Margin
322a6f13a4aSGreg Roach        if (isset($attrs['headermargin'])) {
3237a6ee1acSGreg Roach            if ($attrs['headermargin'] === '0') {
324208e9f76SGreg Roach                $this->wt_report->header_margin = 0;
325a6f13a4aSGreg Roach            } elseif (!empty($attrs['headermargin'])) {
326208e9f76SGreg 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))
327a6f13a4aSGreg Roach            }
328a6f13a4aSGreg Roach        }
329a6f13a4aSGreg Roach        // Footer Margin
330a6f13a4aSGreg Roach        if (isset($attrs['footermargin'])) {
3317a6ee1acSGreg Roach            if ($attrs['footermargin'] === '0') {
332208e9f76SGreg Roach                $this->wt_report->footer_margin = 0;
333a6f13a4aSGreg Roach            } elseif (!empty($attrs['footermargin'])) {
334208e9f76SGreg 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))
335a6f13a4aSGreg Roach            }
336a6f13a4aSGreg Roach        }
337a6f13a4aSGreg Roach
338a6f13a4aSGreg Roach        // Page Orientation
339a6f13a4aSGreg Roach        if (!empty($attrs['orientation'])) {
340044416d2SGreg Roach            if ($attrs['orientation'] === 'landscape') {
3417a6ee1acSGreg Roach                $this->wt_report->orientation = 'landscape';
342044416d2SGreg Roach            } elseif ($attrs['orientation'] === 'portrait') {
3437a6ee1acSGreg Roach                $this->wt_report->orientation = 'portrait';
344a6f13a4aSGreg Roach            }
345a6f13a4aSGreg Roach        }
346a6f13a4aSGreg Roach        // Page Size
347a6f13a4aSGreg Roach        if (!empty($attrs['pageSize'])) {
348208e9f76SGreg Roach            $this->wt_report->page_format = strtoupper($attrs['pageSize']);
349a6f13a4aSGreg Roach        }
350a6f13a4aSGreg Roach
351a6f13a4aSGreg Roach        // Show Generated By...
352a6f13a4aSGreg Roach        if (isset($attrs['showGeneratedBy'])) {
3537a6ee1acSGreg Roach            if ($attrs['showGeneratedBy'] === '0') {
354208e9f76SGreg Roach                $this->wt_report->show_generated_by = false;
3557a6ee1acSGreg Roach            } elseif ($attrs['showGeneratedBy'] === '1') {
356208e9f76SGreg Roach                $this->wt_report->show_generated_by = true;
357a6f13a4aSGreg Roach            }
358a6f13a4aSGreg Roach        }
359a6f13a4aSGreg Roach
360e8e7866bSGreg Roach        $this->wt_report->setup();
361a6f13a4aSGreg Roach    }
362a6f13a4aSGreg Roach
363a6f13a4aSGreg Roach    /**
36476692c8bSGreg Roach     * XML </Doc>
3658ba2e626SGreg Roach     *
3668ba2e626SGreg Roach     * @return void
367a6f13a4aSGreg Roach     */
368c1010edaSGreg Roach    private function docEndHandler()
369c1010edaSGreg Roach    {
370e8e7866bSGreg Roach        $this->wt_report->run();
371a6f13a4aSGreg Roach    }
372a6f13a4aSGreg Roach
373a6f13a4aSGreg Roach    /**
37476692c8bSGreg Roach     * XML <Header>
3758ba2e626SGreg Roach     *
3768ba2e626SGreg Roach     * @return void
377a6f13a4aSGreg Roach     */
378c1010edaSGreg Roach    private function headerStartHandler()
379c1010edaSGreg Roach    {
380a6f13a4aSGreg Roach        // Clear the Header before any new elements are added
381e8e7866bSGreg Roach        $this->wt_report->clearHeader();
3827a6ee1acSGreg Roach        $this->wt_report->setProcessing('H');
383a6f13a4aSGreg Roach    }
384a6f13a4aSGreg Roach
385a6f13a4aSGreg Roach    /**
38676692c8bSGreg Roach     * XML <PageHeader>
3878ba2e626SGreg Roach     *
3888ba2e626SGreg Roach     * @return void
389a6f13a4aSGreg Roach     */
390c1010edaSGreg Roach    private function pageHeaderStartHandler()
391c1010edaSGreg Roach    {
3929b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
393a6f13a4aSGreg Roach        $this->print_data         = false;
3949b3dd960SGreg Roach        $this->wt_report_stack[]  = $this->wt_report;
395e8e7866bSGreg Roach        $this->wt_report          = $this->report_root->createPageHeader();
396a6f13a4aSGreg Roach    }
397a6f13a4aSGreg Roach
398a6f13a4aSGreg Roach    /**
39976692c8bSGreg Roach     * XML <pageHeaderEndHandler>
4008ba2e626SGreg Roach     *
4018ba2e626SGreg Roach     * @return void
402a6f13a4aSGreg Roach     */
403c1010edaSGreg Roach    private function pageHeaderEndHandler()
404c1010edaSGreg Roach    {
405a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
406e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
407e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
408e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
409a6f13a4aSGreg Roach    }
410a6f13a4aSGreg Roach
411a6f13a4aSGreg Roach    /**
41276692c8bSGreg Roach     * XML <bodyStartHandler>
4138ba2e626SGreg Roach     *
4148ba2e626SGreg Roach     * @return void
415a6f13a4aSGreg Roach     */
416c1010edaSGreg Roach    private function bodyStartHandler()
417c1010edaSGreg Roach    {
4187a6ee1acSGreg Roach        $this->wt_report->setProcessing('B');
419a6f13a4aSGreg Roach    }
420a6f13a4aSGreg Roach
421a6f13a4aSGreg Roach    /**
42276692c8bSGreg Roach     * XML <footerStartHandler>
4238ba2e626SGreg Roach     *
4248ba2e626SGreg Roach     * @return void
425a6f13a4aSGreg Roach     */
426c1010edaSGreg Roach    private function footerStartHandler()
427c1010edaSGreg Roach    {
4287a6ee1acSGreg Roach        $this->wt_report->setProcessing('F');
429a6f13a4aSGreg Roach    }
430a6f13a4aSGreg Roach
431a6f13a4aSGreg Roach    /**
43276692c8bSGreg Roach     * XML <Cell>
433a6f13a4aSGreg Roach     *
434c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
4358ba2e626SGreg Roach     *
4368ba2e626SGreg Roach     * @return void
437a6f13a4aSGreg Roach     */
438c0fe75acSGreg Roach    private function cellStartHandler(array $attrs)
439c1010edaSGreg Roach    {
440a6f13a4aSGreg Roach        // string The text alignment of the text in this box.
4417a6ee1acSGreg Roach        $align = '';
442a6f13a4aSGreg Roach        if (!empty($attrs['align'])) {
443a6f13a4aSGreg Roach            $align = $attrs['align'];
444a6f13a4aSGreg Roach            // RTL supported left/right alignment
445044416d2SGreg Roach            if ($align === 'rightrtl') {
446e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4477a6ee1acSGreg Roach                    $align = 'left';
448a6f13a4aSGreg Roach                } else {
4497a6ee1acSGreg Roach                    $align = 'right';
450a6f13a4aSGreg Roach                }
451044416d2SGreg Roach            } elseif ($align === 'leftrtl') {
452e8e7866bSGreg Roach                if ($this->wt_report->rtl) {
4537a6ee1acSGreg Roach                    $align = 'right';
454a6f13a4aSGreg Roach                } else {
4557a6ee1acSGreg Roach                    $align = 'left';
456a6f13a4aSGreg Roach                }
457a6f13a4aSGreg Roach            }
458a6f13a4aSGreg Roach        }
459a6f13a4aSGreg Roach
460a6f13a4aSGreg Roach        // string The color to fill the background of this cell
4617a6ee1acSGreg Roach        $bgcolor = '';
462a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
463a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
464a6f13a4aSGreg Roach        }
465a6f13a4aSGreg Roach
466a6f13a4aSGreg Roach        // int Whether or not the background should be painted
467a6f13a4aSGreg Roach        $fill = 1;
468a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
4697a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
470a6f13a4aSGreg Roach                $fill = 0;
4717a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
472a6f13a4aSGreg Roach                $fill = 1;
473a6f13a4aSGreg Roach            }
474a6f13a4aSGreg Roach        }
475a6f13a4aSGreg Roach
476a6f13a4aSGreg Roach        $reseth = true;
477a6f13a4aSGreg Roach        // boolean   if true reset the last cell height (default true)
478a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
4797a6ee1acSGreg Roach            if ($attrs['reseth'] === '0') {
480a6f13a4aSGreg Roach                $reseth = false;
4817a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '1') {
482a6f13a4aSGreg Roach                $reseth = true;
483a6f13a4aSGreg Roach            }
484a6f13a4aSGreg Roach        }
485a6f13a4aSGreg Roach
486a6f13a4aSGreg Roach        // mixed Whether or not a border should be printed around this box
487a6f13a4aSGreg Roach        $border = 0;
488a6f13a4aSGreg Roach        if (!empty($attrs['border'])) {
489a6f13a4aSGreg Roach            $border = $attrs['border'];
490a6f13a4aSGreg Roach        }
491a6f13a4aSGreg Roach        // string Border color in HTML code
4927a6ee1acSGreg Roach        $bocolor = '';
493a6f13a4aSGreg Roach        if (!empty($attrs['bocolor'])) {
494a6f13a4aSGreg Roach            $bocolor = $attrs['bocolor'];
495a6f13a4aSGreg Roach        }
496a6f13a4aSGreg Roach
497a6f13a4aSGreg Roach        // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted.
498a6f13a4aSGreg Roach        $height = 0;
499a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
500589feda3SGreg Roach            $height = $attrs['height'];
501a6f13a4aSGreg Roach        }
502a6f13a4aSGreg 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.
503a6f13a4aSGreg Roach        $width = 0;
504a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
505589feda3SGreg Roach            $width = $attrs['width'];
506a6f13a4aSGreg Roach        }
507a6f13a4aSGreg Roach
508a6f13a4aSGreg Roach        // int Stretch carachter mode
509a6f13a4aSGreg Roach        $stretch = 0;
510a6f13a4aSGreg Roach        if (!empty($attrs['stretch'])) {
511a6f13a4aSGreg Roach            $stretch = (int) $attrs['stretch'];
512a6f13a4aSGreg Roach        }
513a6f13a4aSGreg Roach
514a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
515c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
516a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
5177a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
518c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
519a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
520a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
5217a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
522a6f13a4aSGreg Roach                $left = 0;
523a6f13a4aSGreg Roach            }
524a6f13a4aSGreg Roach        }
525a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
526c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
527a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
5287a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
529c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
530a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
531a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
5327a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
533a6f13a4aSGreg Roach                $top = 0;
534a6f13a4aSGreg Roach            }
535a6f13a4aSGreg Roach        }
536a6f13a4aSGreg Roach
537a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
5387a6ee1acSGreg Roach        $style = '';
539a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
540a6f13a4aSGreg Roach            $style = $attrs['style'];
541a6f13a4aSGreg Roach        }
542a6f13a4aSGreg Roach
543a6f13a4aSGreg Roach        // string Text color in html code
5447a6ee1acSGreg Roach        $tcolor = '';
545a6f13a4aSGreg Roach        if (!empty($attrs['tcolor'])) {
546a6f13a4aSGreg Roach            $tcolor = $attrs['tcolor'];
547a6f13a4aSGreg Roach        }
548a6f13a4aSGreg Roach
549a6f13a4aSGreg Roach        // int Indicates where the current position should go after the call.
550a6f13a4aSGreg Roach        $ln = 0;
551a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
552a6f13a4aSGreg Roach            if (!empty($attrs['newline'])) {
553a6f13a4aSGreg Roach                $ln = (int) $attrs['newline'];
5547a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
555a6f13a4aSGreg Roach                $ln = 0;
556a6f13a4aSGreg Roach            }
557a6f13a4aSGreg Roach        }
558a6f13a4aSGreg Roach
559044416d2SGreg Roach        if ($align === 'left') {
5607a6ee1acSGreg Roach            $align = 'L';
561044416d2SGreg Roach        } elseif ($align === 'right') {
5627a6ee1acSGreg Roach            $align = 'R';
563044416d2SGreg Roach        } elseif ($align === 'center') {
5647a6ee1acSGreg Roach            $align = 'C';
565044416d2SGreg Roach        } elseif ($align === 'justify') {
5667a6ee1acSGreg Roach            $align = 'J';
567a6f13a4aSGreg Roach        }
568a6f13a4aSGreg Roach
5699b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
570a6f13a4aSGreg Roach        $this->print_data         = true;
571a6f13a4aSGreg Roach
572e8e7866bSGreg Roach        $this->current_element = $this->report_root->createCell(
573a6f13a4aSGreg Roach            $width,
574a6f13a4aSGreg Roach            $height,
575a6f13a4aSGreg Roach            $border,
576a6f13a4aSGreg Roach            $align,
577a6f13a4aSGreg Roach            $bgcolor,
578a6f13a4aSGreg Roach            $style,
579a6f13a4aSGreg Roach            $ln,
580a6f13a4aSGreg Roach            $top,
581a6f13a4aSGreg Roach            $left,
582a6f13a4aSGreg Roach            $fill,
583a6f13a4aSGreg Roach            $stretch,
584a6f13a4aSGreg Roach            $bocolor,
585a6f13a4aSGreg Roach            $tcolor,
586a6f13a4aSGreg Roach            $reseth
587a6f13a4aSGreg Roach        );
588a6f13a4aSGreg Roach    }
589a6f13a4aSGreg Roach
590a6f13a4aSGreg Roach    /**
59176692c8bSGreg Roach     * XML </Cell>
5928ba2e626SGreg Roach     *
5938ba2e626SGreg Roach     * @return void
594a6f13a4aSGreg Roach     */
595c1010edaSGreg Roach    private function cellEndHandler()
596c1010edaSGreg Roach    {
597a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
598e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
599a6f13a4aSGreg Roach    }
600a6f13a4aSGreg Roach
601a6f13a4aSGreg Roach    /**
602a6f13a4aSGreg Roach     * XML <Now /> element handler
6038ba2e626SGreg Roach     *
6048ba2e626SGreg Roach     * @return void
605a6f13a4aSGreg Roach     */
606c1010edaSGreg Roach    private function nowStartHandler()
607c1010edaSGreg Roach    {
6083d7a8a4cSGreg Roach        $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET);
609a6f13a4aSGreg Roach        $this->current_element->addText($g->display());
610a6f13a4aSGreg Roach    }
611a6f13a4aSGreg Roach
612a6f13a4aSGreg Roach    /**
613a6f13a4aSGreg Roach     * XML <PageNum /> element handler
6148ba2e626SGreg Roach     *
6158ba2e626SGreg Roach     * @return void
616a6f13a4aSGreg Roach     */
617c1010edaSGreg Roach    private function pageNumStartHandler()
618c1010edaSGreg Roach    {
6197a6ee1acSGreg Roach        $this->current_element->addText('#PAGENUM#');
620a6f13a4aSGreg Roach    }
621a6f13a4aSGreg Roach
622a6f13a4aSGreg Roach    /**
623a6f13a4aSGreg Roach     * XML <TotalPages /> element handler
6248ba2e626SGreg Roach     *
6258ba2e626SGreg Roach     * @return void
626a6f13a4aSGreg Roach     */
627c1010edaSGreg Roach    private function totalPagesStartHandler()
628c1010edaSGreg Roach    {
6297a6ee1acSGreg Roach        $this->current_element->addText('{{:ptp:}}');
630a6f13a4aSGreg Roach    }
631a6f13a4aSGreg Roach
632a6f13a4aSGreg Roach    /**
633a6f13a4aSGreg Roach     * Called at the start of an element.
634a6f13a4aSGreg Roach     *
635c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
6368ba2e626SGreg Roach     *
6378ba2e626SGreg Roach     * @return void
638a6f13a4aSGreg Roach     */
639c0fe75acSGreg Roach    private function gedcomStartHandler(array $attrs)
640c1010edaSGreg Roach    {
641a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
642a6f13a4aSGreg Roach            $this->process_gedcoms++;
643a6f13a4aSGreg Roach
644a6f13a4aSGreg Roach            return;
645a6f13a4aSGreg Roach        }
646a6f13a4aSGreg Roach
647a6f13a4aSGreg Roach        $tag       = $attrs['id'];
6487a6ee1acSGreg Roach        $tag       = str_replace('@fact', $this->fact, $tag);
6497a6ee1acSGreg Roach        $tags      = explode(':', $tag);
650a6f13a4aSGreg Roach        $newgedrec = '';
651a6f13a4aSGreg Roach        if (count($tags) < 2) {
652299d100dSGreg Roach            $tmp       = GedcomRecord::getInstance($attrs['id'], $this->tree);
653299d100dSGreg Roach            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
654a6f13a4aSGreg Roach        }
655a6f13a4aSGreg Roach        if (empty($newgedrec)) {
656a6f13a4aSGreg Roach            $tgedrec   = $this->gedrec;
657a6f13a4aSGreg Roach            $newgedrec = '';
658a6f13a4aSGreg Roach            foreach ($tags as $tag) {
6597a6ee1acSGreg Roach                if (preg_match('/\$(.+)/', $tag, $match)) {
660d1286247SGreg Roach                    if (isset($this->vars[$match[1]]['gedcom'])) {
661d1286247SGreg Roach                        $newgedrec = $this->vars[$match[1]]['gedcom'];
662a6f13a4aSGreg Roach                    } else {
663299d100dSGreg Roach                        $tmp       = GedcomRecord::getInstance($match[1], $this->tree);
664299d100dSGreg Roach                        $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
665a6f13a4aSGreg Roach                    }
666a6f13a4aSGreg Roach                } else {
6677a6ee1acSGreg Roach                    if (preg_match('/@(.+)/', $tag, $match)) {
66813abd6f3SGreg Roach                        $gmatch = [];
669a6f13a4aSGreg Roach                        if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) {
670299d100dSGreg Roach                            $tmp       = GedcomRecord::getInstance($gmatch[1], $this->tree);
671299d100dSGreg Roach                            $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : '';
672a6f13a4aSGreg Roach                            $tgedrec   = $newgedrec;
673a6f13a4aSGreg Roach                        } else {
674a6f13a4aSGreg Roach                            $newgedrec = '';
675a6f13a4aSGreg Roach                            break;
676a6f13a4aSGreg Roach                        }
677a6f13a4aSGreg Roach                    } else {
6787a6ee1acSGreg Roach                        $temp      = explode(' ', trim($tgedrec));
679a6f13a4aSGreg Roach                        $level     = $temp[0] + 1;
6803d7a8a4cSGreg Roach                        $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec);
681a6f13a4aSGreg Roach                        $tgedrec   = $newgedrec;
682a6f13a4aSGreg Roach                    }
683a6f13a4aSGreg Roach                }
684a6f13a4aSGreg Roach            }
685a6f13a4aSGreg Roach        }
686a6f13a4aSGreg Roach        if (!empty($newgedrec)) {
6879b3dd960SGreg Roach            $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc];
688a6f13a4aSGreg Roach            $this->gedrec         = $newgedrec;
689a6f13a4aSGreg Roach            if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) {
690a6f13a4aSGreg Roach                $this->fact = $match[2];
691a6f13a4aSGreg Roach                $this->desc = trim($match[3]);
692a6f13a4aSGreg Roach            }
693a6f13a4aSGreg Roach        } else {
694a6f13a4aSGreg Roach            $this->process_gedcoms++;
695a6f13a4aSGreg Roach        }
696a6f13a4aSGreg Roach    }
697a6f13a4aSGreg Roach
698a6f13a4aSGreg Roach    /**
699a6f13a4aSGreg Roach     * Called at the end of an element.
7008ba2e626SGreg Roach     *
7018ba2e626SGreg Roach     * @return void
702a6f13a4aSGreg Roach     */
703c1010edaSGreg Roach    private function gedcomEndHandler()
704c1010edaSGreg Roach    {
705a6f13a4aSGreg Roach        if ($this->process_gedcoms > 0) {
706a6f13a4aSGreg Roach            $this->process_gedcoms--;
707a6f13a4aSGreg Roach        } else {
70865e02381SGreg Roach            [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack);
709a6f13a4aSGreg Roach        }
710a6f13a4aSGreg Roach    }
711a6f13a4aSGreg Roach
712a6f13a4aSGreg Roach    /**
71376692c8bSGreg Roach     * XML <textBoxStartHandler>
714a6f13a4aSGreg Roach     *
715c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
7168ba2e626SGreg Roach     *
7178ba2e626SGreg Roach     * @return void
718a6f13a4aSGreg Roach     */
719c0fe75acSGreg Roach    private function textBoxStartHandler(array $attrs)
720c1010edaSGreg Roach    {
721a6f13a4aSGreg Roach        // string Background color code
7227a6ee1acSGreg Roach        $bgcolor = '';
723a6f13a4aSGreg Roach        if (!empty($attrs['bgcolor'])) {
724a6f13a4aSGreg Roach            $bgcolor = $attrs['bgcolor'];
725a6f13a4aSGreg Roach        }
726a6f13a4aSGreg Roach
727a6f13a4aSGreg Roach        // boolean Wether or not fill the background color
728a6f13a4aSGreg Roach        $fill = true;
729a6f13a4aSGreg Roach        if (isset($attrs['fill'])) {
7307a6ee1acSGreg Roach            if ($attrs['fill'] === '0') {
731a6f13a4aSGreg Roach                $fill = false;
7327a6ee1acSGreg Roach            } elseif ($attrs['fill'] === '1') {
733a6f13a4aSGreg Roach                $fill = true;
734a6f13a4aSGreg Roach            }
735a6f13a4aSGreg Roach        }
736a6f13a4aSGreg Roach
737a6f13a4aSGreg Roach        // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0
738a6f13a4aSGreg Roach        $border = false;
739a6f13a4aSGreg Roach        if (isset($attrs['border'])) {
7407a6ee1acSGreg Roach            if ($attrs['border'] === '1') {
741a6f13a4aSGreg Roach                $border = true;
7427a6ee1acSGreg Roach            } elseif ($attrs['border'] === '0') {
743a6f13a4aSGreg Roach                $border = false;
744a6f13a4aSGreg Roach            }
745a6f13a4aSGreg Roach        }
746a6f13a4aSGreg Roach
747a6f13a4aSGreg Roach        // int The starting height of this cell. If the text wraps the height will automatically be adjusted
748a6f13a4aSGreg Roach        $height = 0;
749a6f13a4aSGreg Roach        if (!empty($attrs['height'])) {
750a6f13a4aSGreg Roach            $height = (int) $attrs['height'];
751a6f13a4aSGreg Roach        }
752a6f13a4aSGreg Roach        // int Setting the width to 0 will make it the width from the current location to the margin
753a6f13a4aSGreg Roach        $width = 0;
754a6f13a4aSGreg Roach        if (!empty($attrs['width'])) {
755a6f13a4aSGreg Roach            $width = (int) $attrs['width'];
756a6f13a4aSGreg Roach        }
757a6f13a4aSGreg Roach
758a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. The default is the current position.
759c21bdddcSGreg Roach        $left = ReportBaseElement::CURRENT_POSITION;
760a6f13a4aSGreg Roach        if (isset($attrs['left'])) {
7617a6ee1acSGreg Roach            if ($attrs['left'] === '.') {
762c21bdddcSGreg Roach                $left = ReportBaseElement::CURRENT_POSITION;
763a6f13a4aSGreg Roach            } elseif (!empty($attrs['left'])) {
764a6f13a4aSGreg Roach                $left = (int) $attrs['left'];
7657a6ee1acSGreg Roach            } elseif ($attrs['left'] === '0') {
766a6f13a4aSGreg Roach                $left = 0;
767a6f13a4aSGreg Roach            }
768a6f13a4aSGreg Roach        }
769a6f13a4aSGreg Roach        // mixed Position the top corner of this box on the page. the default is the current position
770c21bdddcSGreg Roach        $top = ReportBaseElement::CURRENT_POSITION;
771a6f13a4aSGreg Roach        if (isset($attrs['top'])) {
7727a6ee1acSGreg Roach            if ($attrs['top'] === '.') {
773c21bdddcSGreg Roach                $top = ReportBaseElement::CURRENT_POSITION;
774a6f13a4aSGreg Roach            } elseif (!empty($attrs['top'])) {
775a6f13a4aSGreg Roach                $top = (int) $attrs['top'];
7767a6ee1acSGreg Roach            } elseif ($attrs['top'] === '0') {
777a6f13a4aSGreg Roach                $top = 0;
778a6f13a4aSGreg Roach            }
779a6f13a4aSGreg Roach        }
780a6f13a4aSGreg 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
781a6f13a4aSGreg Roach        $newline = false;
782a6f13a4aSGreg Roach        if (isset($attrs['newline'])) {
7837a6ee1acSGreg Roach            if ($attrs['newline'] === '1') {
784a6f13a4aSGreg Roach                $newline = true;
7857a6ee1acSGreg Roach            } elseif ($attrs['newline'] === '0') {
786a6f13a4aSGreg Roach                $newline = false;
787a6f13a4aSGreg Roach            }
788a6f13a4aSGreg Roach        }
789a6f13a4aSGreg Roach        // boolean
790a6f13a4aSGreg Roach        $pagecheck = true;
791a6f13a4aSGreg Roach        if (isset($attrs['pagecheck'])) {
7927a6ee1acSGreg Roach            if ($attrs['pagecheck'] === '0') {
793a6f13a4aSGreg Roach                $pagecheck = false;
7947a6ee1acSGreg Roach            } elseif ($attrs['pagecheck'] === '1') {
795a6f13a4aSGreg Roach                $pagecheck = true;
796a6f13a4aSGreg Roach            }
797a6f13a4aSGreg Roach        }
798a6f13a4aSGreg Roach        // boolean Cell padding
799a6f13a4aSGreg Roach        $padding = true;
800a6f13a4aSGreg Roach        if (isset($attrs['padding'])) {
8017a6ee1acSGreg Roach            if ($attrs['padding'] === '0') {
802a6f13a4aSGreg Roach                $padding = false;
8037a6ee1acSGreg Roach            } elseif ($attrs['padding'] === '1') {
804a6f13a4aSGreg Roach                $padding = true;
805a6f13a4aSGreg Roach            }
806a6f13a4aSGreg Roach        }
807a6f13a4aSGreg Roach        // boolean Reset this box Height
808a6f13a4aSGreg Roach        $reseth = false;
809a6f13a4aSGreg Roach        if (isset($attrs['reseth'])) {
8107a6ee1acSGreg Roach            if ($attrs['reseth'] === '1') {
811a6f13a4aSGreg Roach                $reseth = true;
8127a6ee1acSGreg Roach            } elseif ($attrs['reseth'] === '0') {
813a6f13a4aSGreg Roach                $reseth = false;
814a6f13a4aSGreg Roach            }
815a6f13a4aSGreg Roach        }
816a6f13a4aSGreg Roach
817a6f13a4aSGreg Roach        // string Style of rendering
8187a6ee1acSGreg Roach        $style = '';
819a6f13a4aSGreg Roach
8209b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
821a6f13a4aSGreg Roach        $this->print_data         = false;
822a6f13a4aSGreg Roach
8239b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
824e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createTextBox(
825a6f13a4aSGreg Roach            $width,
826a6f13a4aSGreg Roach            $height,
827a6f13a4aSGreg Roach            $border,
828a6f13a4aSGreg Roach            $bgcolor,
829a6f13a4aSGreg Roach            $newline,
830a6f13a4aSGreg Roach            $left,
831a6f13a4aSGreg Roach            $top,
832a6f13a4aSGreg Roach            $pagecheck,
833a6f13a4aSGreg Roach            $style,
834a6f13a4aSGreg Roach            $fill,
835a6f13a4aSGreg Roach            $padding,
836a6f13a4aSGreg Roach            $reseth
837a6f13a4aSGreg Roach        );
838a6f13a4aSGreg Roach    }
839a6f13a4aSGreg Roach
840a6f13a4aSGreg Roach    /**
84176692c8bSGreg Roach     * XML <textBoxEndHandler>
8428ba2e626SGreg Roach     *
8438ba2e626SGreg Roach     * @return void
844a6f13a4aSGreg Roach     */
845c1010edaSGreg Roach    private function textBoxEndHandler()
846c1010edaSGreg Roach    {
847a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
848e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
849e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
850e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
851a6f13a4aSGreg Roach    }
852a6f13a4aSGreg Roach
853a6f13a4aSGreg Roach    /**
85476692c8bSGreg Roach     * XLM <Text>.
85576692c8bSGreg Roach     *
856c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8578ba2e626SGreg Roach     *
8588ba2e626SGreg Roach     * @return void
859a6f13a4aSGreg Roach     */
860c0fe75acSGreg Roach    private function textStartHandler(array $attrs)
861c1010edaSGreg Roach    {
8629b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
863a6f13a4aSGreg Roach        $this->print_data         = true;
864a6f13a4aSGreg Roach
865a6f13a4aSGreg Roach        // string The name of the Style that should be used to render the text.
8667a6ee1acSGreg Roach        $style = '';
867a6f13a4aSGreg Roach        if (!empty($attrs['style'])) {
868a6f13a4aSGreg Roach            $style = $attrs['style'];
869a6f13a4aSGreg Roach        }
870a6f13a4aSGreg Roach
871a6f13a4aSGreg Roach        // string  The color of the text - Keep the black color as default
8727a6ee1acSGreg Roach        $color = '';
873a6f13a4aSGreg Roach        if (!empty($attrs['color'])) {
874a6f13a4aSGreg Roach            $color = $attrs['color'];
875a6f13a4aSGreg Roach        }
876a6f13a4aSGreg Roach
877e8e7866bSGreg Roach        $this->current_element = $this->report_root->createText($style, $color);
878a6f13a4aSGreg Roach    }
879a6f13a4aSGreg Roach
880a6f13a4aSGreg Roach    /**
88176692c8bSGreg Roach     * XML </Text>
8828ba2e626SGreg Roach     *
8838ba2e626SGreg Roach     * @return void
884a6f13a4aSGreg Roach     */
885c1010edaSGreg Roach    private function textEndHandler()
886c1010edaSGreg Roach    {
887a6f13a4aSGreg Roach        $this->print_data = array_pop($this->print_data_stack);
888e8e7866bSGreg Roach        $this->wt_report->addElement($this->current_element);
889a6f13a4aSGreg Roach    }
890a6f13a4aSGreg Roach
891a6f13a4aSGreg Roach    /**
89276692c8bSGreg Roach     * XML <GetPersonName/>
893a6f13a4aSGreg Roach     * Get the name
894a6f13a4aSGreg Roach     * 1. id is empty - current GEDCOM record
895a6f13a4aSGreg Roach     * 2. id is set with a record id
896a6f13a4aSGreg Roach     *
897c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
8988ba2e626SGreg Roach     *
8998ba2e626SGreg Roach     * @return void
900a6f13a4aSGreg Roach     */
901c0fe75acSGreg Roach    private function getPersonNameStartHandler(array $attrs)
902c1010edaSGreg Roach    {
9037a6ee1acSGreg Roach        $id    = '';
90413abd6f3SGreg Roach        $match = [];
905a6f13a4aSGreg Roach        if (empty($attrs['id'])) {
9067a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
907a6f13a4aSGreg Roach                $id = $match[1];
908a6f13a4aSGreg Roach            }
909a6f13a4aSGreg Roach        } else {
9107a6ee1acSGreg Roach            if (preg_match('/\$(.+)/', $attrs['id'], $match)) {
911d1286247SGreg Roach                if (isset($this->vars[$match[1]]['id'])) {
912d1286247SGreg Roach                    $id = $this->vars[$match[1]]['id'];
913a6f13a4aSGreg Roach                }
914a6f13a4aSGreg Roach            } else {
9157a6ee1acSGreg Roach                if (preg_match('/@(.+)/', $attrs['id'], $match)) {
91613abd6f3SGreg Roach                    $gmatch = [];
917a6f13a4aSGreg Roach                    if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) {
918a6f13a4aSGreg Roach                        $id = $gmatch[1];
919a6f13a4aSGreg Roach                    }
920a6f13a4aSGreg Roach                } else {
921a6f13a4aSGreg Roach                    $id = $attrs['id'];
922a6f13a4aSGreg Roach                }
923a6f13a4aSGreg Roach            }
924a6f13a4aSGreg Roach        }
925a6f13a4aSGreg Roach        if (!empty($id)) {
926299d100dSGreg Roach            $record = GedcomRecord::getInstance($id, $this->tree);
9278f038c36SRico Sonntag            if ($record === null) {
928a6f13a4aSGreg Roach                return;
929a6f13a4aSGreg Roach            }
930a6f13a4aSGreg Roach            if (!$record->canShowName()) {
931a6f13a4aSGreg Roach                $this->current_element->addText(I18N::translate('Private'));
932a6f13a4aSGreg Roach            } else {
933a6f13a4aSGreg Roach                $name = $record->getFullName();
934a6f13a4aSGreg Roach                $name = preg_replace(
935c1010edaSGreg Roach                    [
936c1010edaSGreg Roach                        '/<span class="starredname">/',
937c1010edaSGreg Roach                        '/<\/span><\/span>/',
938c1010edaSGreg Roach                        '/<\/span>/',
939c1010edaSGreg Roach                    ],
940c1010edaSGreg Roach                    [
941c1010edaSGreg Roach                        '«',
942c1010edaSGreg Roach                        '',
943c1010edaSGreg Roach                        '»',
944c1010edaSGreg Roach                    ],
945a6f13a4aSGreg Roach                    $name
946a6f13a4aSGreg Roach                );
947a6f13a4aSGreg Roach                $name = strip_tags($name);
948a6f13a4aSGreg Roach                if (!empty($attrs['truncate'])) {
9499a9e551aSGreg Roach                    $name = Str::limit($name, $attrs['truncate'], I18N::translate('…'));
950a6f13a4aSGreg Roach                } else {
951a6f13a4aSGreg Roach                    $addname = $record->getAddName();
952a6f13a4aSGreg Roach                    $addname = preg_replace(
953c1010edaSGreg Roach                        [
954c1010edaSGreg Roach                            '/<span class="starredname">/',
955c1010edaSGreg Roach                            '/<\/span><\/span>/',
956c1010edaSGreg Roach                            '/<\/span>/',
957c1010edaSGreg Roach                        ],
958c1010edaSGreg Roach                        [
959c1010edaSGreg Roach                            '«',
960c1010edaSGreg Roach                            '',
961c1010edaSGreg Roach                            '»',
962c1010edaSGreg Roach                        ],
963a6f13a4aSGreg Roach                        $addname
964a6f13a4aSGreg Roach                    );
965a6f13a4aSGreg Roach                    $addname = strip_tags($addname);
966a6f13a4aSGreg Roach                    if (!empty($addname)) {
9677a6ee1acSGreg Roach                        $name .= ' ' . $addname;
968a6f13a4aSGreg Roach                    }
969a6f13a4aSGreg Roach                }
970a6f13a4aSGreg Roach                $this->current_element->addText(trim($name));
971a6f13a4aSGreg Roach            }
972a6f13a4aSGreg Roach        }
973a6f13a4aSGreg Roach    }
974a6f13a4aSGreg Roach
975a6f13a4aSGreg Roach    /**
97676692c8bSGreg Roach     * XML <GedcomValue/>
977a6f13a4aSGreg Roach     *
978c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
9798ba2e626SGreg Roach     *
9808ba2e626SGreg Roach     * @return void
981a6f13a4aSGreg Roach     */
982c0fe75acSGreg Roach    private function gedcomValueStartHandler(array $attrs)
983c1010edaSGreg Roach    {
9847a6ee1acSGreg Roach        $id    = '';
98513abd6f3SGreg Roach        $match = [];
9867a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
987a6f13a4aSGreg Roach            $id = $match[1];
988a6f13a4aSGreg Roach        }
989a6f13a4aSGreg Roach
990044416d2SGreg Roach        if (isset($attrs['newline']) && $attrs['newline'] === '1') {
9917a6ee1acSGreg Roach            $useBreak = '1';
992a6f13a4aSGreg Roach        } else {
9937a6ee1acSGreg Roach            $useBreak = '0';
994a6f13a4aSGreg Roach        }
995a6f13a4aSGreg Roach
996a6f13a4aSGreg Roach        $tag = $attrs['tag'];
997a6f13a4aSGreg Roach        if (!empty($tag)) {
998044416d2SGreg Roach            if ($tag === '@desc') {
999a6f13a4aSGreg Roach                $value = $this->desc;
1000a6f13a4aSGreg Roach                $value = trim($value);
1001a6f13a4aSGreg Roach                $this->current_element->addText($value);
1002a6f13a4aSGreg Roach            }
1003044416d2SGreg Roach            if ($tag === '@id') {
1004a6f13a4aSGreg Roach                $this->current_element->addText($id);
1005a6f13a4aSGreg Roach            } else {
10067a6ee1acSGreg Roach                $tag = str_replace('@fact', $this->fact, $tag);
1007a6f13a4aSGreg Roach                if (empty($attrs['level'])) {
10087a6ee1acSGreg Roach                    $temp  = explode(' ', trim($this->gedrec));
1009a6f13a4aSGreg Roach                    $level = $temp[0];
1010a6f13a4aSGreg Roach                    if ($level == 0) {
1011a6f13a4aSGreg Roach                        $level++;
1012a6f13a4aSGreg Roach                    }
1013a6f13a4aSGreg Roach                } else {
1014a6f13a4aSGreg Roach                    $level = $attrs['level'];
1015a6f13a4aSGreg Roach                }
1016a6f13a4aSGreg Roach                $tags  = preg_split('/[: ]/', $tag);
10173d7a8a4cSGreg Roach                $value = $this->getGedcomValue($tag, $level, $this->gedrec);
1018a6f13a4aSGreg Roach                switch (end($tags)) {
1019a6f13a4aSGreg Roach                    case 'DATE':
1020a6f13a4aSGreg Roach                        $tmp   = new Date($value);
1021a6f13a4aSGreg Roach                        $value = $tmp->display();
1022a6f13a4aSGreg Roach                        break;
1023a6f13a4aSGreg Roach                    case 'PLAC':
1024299d100dSGreg Roach                        $tmp   = new Place($value, $this->tree);
1025a6f13a4aSGreg Roach                        $value = $tmp->getShortName();
1026a6f13a4aSGreg Roach                        break;
1027a6f13a4aSGreg Roach                }
1028044416d2SGreg Roach                if ($useBreak === '1') {
1029a6f13a4aSGreg Roach                    // Insert <br> when multiple dates exist.
1030a6f13a4aSGreg Roach                    // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages
1031a6f13a4aSGreg Roach                    $value = str_replace('(', '<br>(', $value);
1032a6f13a4aSGreg Roach                    $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value);
1033a6f13a4aSGreg Roach                    $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value);
1034044416d2SGreg Roach                    if (substr($value, 0, 6) === '<br>') {
1035a6f13a4aSGreg Roach                        $value = substr($value, 6);
1036a6f13a4aSGreg Roach                    }
1037a6f13a4aSGreg Roach                }
1038d4d660b7SGreg Roach                $tmp = explode(':', $tag);
1039c1010edaSGreg Roach                if (in_array(end($tmp), [
1040c1010edaSGreg Roach                    'NOTE',
1041c1010edaSGreg Roach                    'TEXT',
1042c1010edaSGreg Roach                ])) {
1043299d100dSGreg Roach                    $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText()
1044a4d703aeSGreg Roach                }
1045a6f13a4aSGreg Roach                $this->current_element->addText($value);
1046a6f13a4aSGreg Roach            }
1047a6f13a4aSGreg Roach        }
1048a6f13a4aSGreg Roach    }
1049a6f13a4aSGreg Roach
1050a6f13a4aSGreg Roach    /**
105176692c8bSGreg Roach     * XML <RepeatTag>
1052a6f13a4aSGreg Roach     *
1053c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
10548ba2e626SGreg Roach     *
10558ba2e626SGreg Roach     * @return void
1056a6f13a4aSGreg Roach     */
1057c0fe75acSGreg Roach    private function repeatTagStartHandler(array $attrs)
1058c1010edaSGreg Roach    {
1059a6f13a4aSGreg Roach        $this->process_repeats++;
1060a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1061a6f13a4aSGreg Roach            return;
1062a6f13a4aSGreg Roach        }
1063a6f13a4aSGreg Roach
10649b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
106513abd6f3SGreg Roach        $this->repeats         = [];
1066e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1067a6f13a4aSGreg Roach
10687a6ee1acSGreg Roach        $tag = '';
1069a6f13a4aSGreg Roach        if (isset($attrs['tag'])) {
1070a6f13a4aSGreg Roach            $tag = $attrs['tag'];
1071a6f13a4aSGreg Roach        }
1072a6f13a4aSGreg Roach        if (!empty($tag)) {
1073044416d2SGreg Roach            if ($tag === '@desc') {
1074a6f13a4aSGreg Roach                $value = $this->desc;
1075a6f13a4aSGreg Roach                $value = trim($value);
1076a6f13a4aSGreg Roach                $this->current_element->addText($value);
1077a6f13a4aSGreg Roach            } else {
10787a6ee1acSGreg Roach                $tag   = str_replace('@fact', $this->fact, $tag);
10797a6ee1acSGreg Roach                $tags  = explode(':', $tag);
10807a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1081a6f13a4aSGreg Roach                $level = $temp[0];
1082a6f13a4aSGreg Roach                if ($level == 0) {
1083a6f13a4aSGreg Roach                    $level++;
1084a6f13a4aSGreg Roach                }
1085a6f13a4aSGreg Roach                $subrec = $this->gedrec;
1086a6f13a4aSGreg Roach                $t      = $tag;
1087a6f13a4aSGreg Roach                $count  = count($tags);
1088a6f13a4aSGreg Roach                $i      = 0;
1089a6f13a4aSGreg Roach                while ($i < $count) {
1090a6f13a4aSGreg Roach                    $t = $tags[$i];
1091a6f13a4aSGreg Roach                    if (!empty($t)) {
1092a6f13a4aSGreg Roach                        if ($i < ($count - 1)) {
10933d7a8a4cSGreg Roach                            $subrec = Functions::getSubRecord($level, "$level $t", $subrec);
1094a6f13a4aSGreg Roach                            if (empty($subrec)) {
1095a6f13a4aSGreg Roach                                $level--;
10963d7a8a4cSGreg Roach                                $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec);
1097a6f13a4aSGreg Roach                                if (empty($subrec)) {
1098a6f13a4aSGreg Roach                                    return;
1099a6f13a4aSGreg Roach                                }
1100a6f13a4aSGreg Roach                            }
1101a6f13a4aSGreg Roach                        }
1102a6f13a4aSGreg Roach                        $level++;
1103a6f13a4aSGreg Roach                    }
1104a6f13a4aSGreg Roach                    $i++;
1105a6f13a4aSGreg Roach                }
1106a6f13a4aSGreg Roach                $level--;
1107a6f13a4aSGreg Roach                $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER);
1108a6f13a4aSGreg Roach                $i     = 0;
1109a6f13a4aSGreg Roach                while ($i < $count) {
1110a6f13a4aSGreg Roach                    $i++;
1111a9007102SGreg Roach                    // Privacy check - is this a link, and are we allowed to view the linked object?
1112a9007102SGreg Roach                    $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i);
11138d0ebef0SGreg Roach                    if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) {
1114299d100dSGreg Roach                        $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree);
1115a9007102SGreg Roach                        if ($linked_object && !$linked_object->canShow()) {
1116a9007102SGreg Roach                            continue;
1117a9007102SGreg Roach                        }
1118a9007102SGreg Roach                    }
1119a9007102SGreg Roach                    $this->repeats[] = $subrecord;
1120a6f13a4aSGreg Roach                }
1121a6f13a4aSGreg Roach            }
1122a6f13a4aSGreg Roach        }
1123a6f13a4aSGreg Roach    }
1124a6f13a4aSGreg Roach
1125a6f13a4aSGreg Roach    /**
112676692c8bSGreg Roach     * XML </ RepeatTag>
11278ba2e626SGreg Roach     *
11288ba2e626SGreg Roach     * @return void
1129a6f13a4aSGreg Roach     */
1130c1010edaSGreg Roach    private function repeatTagEndHandler()
1131c1010edaSGreg Roach    {
1132a6f13a4aSGreg Roach        $this->process_repeats--;
1133a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1134a6f13a4aSGreg Roach            return;
1135a6f13a4aSGreg Roach        }
1136a6f13a4aSGreg Roach
1137a6f13a4aSGreg Roach        // Check if there is anything to repeat
1138a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1139a6f13a4aSGreg Roach            // No need to load them if not used...
1140a6f13a4aSGreg Roach
1141a6f13a4aSGreg Roach            $lineoffset = 0;
1142a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1143a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1144a6f13a4aSGreg Roach            }
1145a6f13a4aSGreg Roach            //-- read the xml from the file
1146299d100dSGreg Roach            $lines = file($this->report);
11477a6ee1acSGreg Roach            while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) {
1148a6f13a4aSGreg Roach                $lineoffset--;
1149a6f13a4aSGreg Roach            }
1150a6f13a4aSGreg Roach            $lineoffset++;
1151a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1152a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
1153a6f13a4aSGreg Roach            // RepeatTag Level counter
1154a6f13a4aSGreg Roach            $count = 1;
1155a6f13a4aSGreg Roach            while (0 < $count) {
11567a6ee1acSGreg Roach                if (strstr($lines[$line_nr], '<RepeatTag') !== false) {
1157a6f13a4aSGreg Roach                    $count++;
11587a6ee1acSGreg Roach                } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) {
1159a6f13a4aSGreg Roach                    $count--;
1160a6f13a4aSGreg Roach                }
1161a6f13a4aSGreg Roach                if (0 < $count) {
1162a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
1163a6f13a4aSGreg Roach                }
1164a6f13a4aSGreg Roach                $line_nr++;
1165a6f13a4aSGreg Roach            }
1166a6f13a4aSGreg Roach            // No need to drag this
1167a6f13a4aSGreg Roach            unset($lines);
1168a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1169a6f13a4aSGreg Roach            // Save original values
11709b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1171a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1172a6f13a4aSGreg Roach            foreach ($this->repeats as $gedrec) {
1173a6f13a4aSGreg Roach                $this->gedrec  = $gedrec;
1174a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1175e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1176a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
11771aa04befSGreg Roach
11781aa04befSGreg Roach                xml_set_element_handler(
11791aa04befSGreg Roach                    $repeat_parser,
11801aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
11811aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
11821aa04befSGreg Roach                    },
11831aa04befSGreg Roach                    function ($parser, string $name) {
11841aa04befSGreg Roach                        $this->endElement($parser, $name);
11851aa04befSGreg Roach                    }
11861aa04befSGreg Roach                );
11871aa04befSGreg Roach
11881aa04befSGreg Roach                xml_set_character_data_handler(
11891aa04befSGreg Roach                    $repeat_parser,
11901aa04befSGreg Roach                    function ($parser, $data) {
11911aa04befSGreg Roach                        $this->characterData($parser, $data);
11921aa04befSGreg Roach                    }
11931aa04befSGreg Roach                );
11941aa04befSGreg Roach
1195a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1196a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1197a6f13a4aSGreg Roach                        'RepeatTagEHandler XML error: %s at line %d',
1198a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1199a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1200a6f13a4aSGreg Roach                    ));
1201a6f13a4aSGreg Roach                }
1202a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1203a6f13a4aSGreg Roach            }
1204a6f13a4aSGreg Roach            // Restore original values
1205a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1206e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1207a6f13a4aSGreg Roach        }
120865e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1209a6f13a4aSGreg Roach    }
1210a6f13a4aSGreg Roach
1211a6f13a4aSGreg Roach    /**
1212a6f13a4aSGreg Roach     * Variable lookup
1213a6f13a4aSGreg Roach     * Retrieve predefined variables :
1214a6f13a4aSGreg Roach     * @ desc GEDCOM fact description, example:
1215a6f13a4aSGreg Roach     *        1 EVEN This is a description
1216a6f13a4aSGreg Roach     * @ fact GEDCOM fact tag, such as BIRT, DEAT etc.
1217a6f13a4aSGreg Roach     * $ I18N::translate('....')
1218a6f13a4aSGreg Roach     * $ language_settings[]
1219a6f13a4aSGreg Roach     *
1220c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12218ba2e626SGreg Roach     *
12228ba2e626SGreg Roach     * @return void
1223a6f13a4aSGreg Roach     */
1224c0fe75acSGreg Roach    private function varStartHandler(array $attrs)
1225c1010edaSGreg Roach    {
1226a6f13a4aSGreg Roach        if (empty($attrs['var'])) {
1227e8e7866bSGreg 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));
1228a6f13a4aSGreg Roach        }
1229a6f13a4aSGreg Roach
1230a6f13a4aSGreg Roach        $var = $attrs['var'];
1231a6f13a4aSGreg Roach        // SetVar element preset variables
1232d1286247SGreg Roach        if (!empty($this->vars[$var]['id'])) {
1233d1286247SGreg Roach            $var = $this->vars[$var]['id'];
1234a6f13a4aSGreg Roach        } else {
1235a6f13a4aSGreg Roach            $tfact = $this->fact;
12367a6ee1acSGreg Roach            if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') {
1237a6f13a4aSGreg Roach                // Use :
1238a6f13a4aSGreg Roach                // n TYPE This text if string
1239a6f13a4aSGreg Roach                $tfact = $this->type;
1240a6f13a4aSGreg Roach            }
1241c1010edaSGreg Roach            $var = str_replace([
1242c1010edaSGreg Roach                '@fact',
1243c1010edaSGreg Roach                '@desc',
1244c1010edaSGreg Roach            ], [
1245c1010edaSGreg Roach                GedcomTag::getLabel($tfact),
1246c1010edaSGreg Roach                $this->desc,
1247c1010edaSGreg Roach            ], $var);
1248a6f13a4aSGreg Roach            if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) {
1249da46f7cdSGreg Roach                $var = I18N::number((int) $match[1]);
1250a6f13a4aSGreg Roach            } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) {
1251a6f13a4aSGreg Roach                $var = I18N::translate($match[1]);
1252a4956c0eSGreg Roach            } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) {
1253a6f13a4aSGreg Roach                $var = I18N::translateContext($match[1], $match[2]);
1254a6f13a4aSGreg Roach            }
1255a6f13a4aSGreg Roach        }
1256a6f13a4aSGreg Roach        // Check if variable is set as a date and reformat the date
1257a6f13a4aSGreg Roach        if (isset($attrs['date'])) {
12587a6ee1acSGreg Roach            if ($attrs['date'] === '1') {
1259a6f13a4aSGreg Roach                $g   = new Date($var);
1260a6f13a4aSGreg Roach                $var = $g->display();
1261a6f13a4aSGreg Roach            }
1262a6f13a4aSGreg Roach        }
1263a6f13a4aSGreg Roach        $this->current_element->addText($var);
12642836aa05SGreg Roach        $this->text = $var; // Used for title/descriptio
1265a6f13a4aSGreg Roach    }
1266a6f13a4aSGreg Roach
1267a6f13a4aSGreg Roach    /**
126876692c8bSGreg Roach     * XML <Facts>
126976692c8bSGreg Roach     *
1270c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
12718ba2e626SGreg Roach     *
12728ba2e626SGreg Roach     * @return void
1273a6f13a4aSGreg Roach     */
1274c0fe75acSGreg Roach    private function factsStartHandler(array $attrs)
1275c1010edaSGreg Roach    {
1276a6f13a4aSGreg Roach        $this->process_repeats++;
1277a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1278a6f13a4aSGreg Roach            return;
1279a6f13a4aSGreg Roach        }
1280a6f13a4aSGreg Roach
12819b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
128213abd6f3SGreg Roach        $this->repeats         = [];
1283e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser);
1284a6f13a4aSGreg Roach
12857a6ee1acSGreg Roach        $id    = '';
128613abd6f3SGreg Roach        $match = [];
12877a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1288a6f13a4aSGreg Roach            $id = $match[1];
1289a6f13a4aSGreg Roach        }
12907a6ee1acSGreg Roach        $tag = '';
1291a6f13a4aSGreg Roach        if (isset($attrs['ignore'])) {
1292a6f13a4aSGreg Roach            $tag .= $attrs['ignore'];
1293a6f13a4aSGreg Roach        }
12947a6ee1acSGreg Roach        if (preg_match('/\$(.+)/', $tag, $match)) {
1295d1286247SGreg Roach            $tag = $this->vars[$match[1]]['id'];
1296a6f13a4aSGreg Roach        }
1297a6f13a4aSGreg Roach
1298299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1299a6f13a4aSGreg Roach        if (empty($attrs['diff']) && !empty($id)) {
130030158ae7SGreg Roach            $facts = $record->facts();
13013d7a8a4cSGreg Roach            Functions::sortFacts($facts);
130213abd6f3SGreg Roach            $this->repeats = [];
1303a6f13a4aSGreg Roach            $nonfacts      = explode(',', $tag);
1304195b5e75SGreg Roach            foreach ($facts as $fact) {
1305195b5e75SGreg Roach                if (!in_array($fact->getTag(), $nonfacts)) {
1306195b5e75SGreg Roach                    $this->repeats[] = $fact->gedcom();
1307a6f13a4aSGreg Roach                }
1308a6f13a4aSGreg Roach            }
1309a6f13a4aSGreg Roach        } else {
131030158ae7SGreg Roach            foreach ($record->facts() as $fact) {
1311195b5e75SGreg Roach                if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') {
1312138ca96cSGreg Roach                    $this->repeats[] = $fact->gedcom();
1313a6f13a4aSGreg Roach                }
1314a6f13a4aSGreg Roach            }
1315a6f13a4aSGreg Roach        }
1316a6f13a4aSGreg Roach    }
1317a6f13a4aSGreg Roach
1318a6f13a4aSGreg Roach    /**
131976692c8bSGreg Roach     * XML </Facts>
13208ba2e626SGreg Roach     *
13218ba2e626SGreg Roach     * @return void
1322a6f13a4aSGreg Roach     */
1323c1010edaSGreg Roach    private function factsEndHandler()
1324c1010edaSGreg Roach    {
1325a6f13a4aSGreg Roach        $this->process_repeats--;
1326a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
1327a6f13a4aSGreg Roach            return;
1328a6f13a4aSGreg Roach        }
1329a6f13a4aSGreg Roach
1330a6f13a4aSGreg Roach        // Check if there is anything to repeat
1331a6f13a4aSGreg Roach        if (count($this->repeats) > 0) {
1332e8e7866bSGreg Roach            $line       = xml_get_current_line_number($this->parser) - 1;
1333a6f13a4aSGreg Roach            $lineoffset = 0;
1334a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
1335a6f13a4aSGreg Roach                $lineoffset += $rep[1];
1336a6f13a4aSGreg Roach            }
1337a6f13a4aSGreg Roach
1338a6f13a4aSGreg Roach            //-- read the xml from the file
1339299d100dSGreg Roach            $lines = file($this->report);
1340a6f13a4aSGreg Roach            while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) {
1341a6f13a4aSGreg Roach                $lineoffset--;
1342a6f13a4aSGreg Roach            }
1343a6f13a4aSGreg Roach            $lineoffset++;
1344a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
1345a6f13a4aSGreg Roach            $i         = $line + $lineoffset;
1346a6f13a4aSGreg Roach            $line_nr   = $this->repeat_bytes + $lineoffset;
1347a6f13a4aSGreg Roach            while ($line_nr < $i) {
1348a6f13a4aSGreg Roach                $reportxml .= $lines[$line_nr];
1349a6f13a4aSGreg Roach                $line_nr++;
1350a6f13a4aSGreg Roach            }
1351a6f13a4aSGreg Roach            // No need to drag this
1352a6f13a4aSGreg Roach            unset($lines);
1353a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
1354a6f13a4aSGreg Roach            // Save original values
13559b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
1356a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
1357a6f13a4aSGreg Roach            $count                = count($this->repeats);
1358a6f13a4aSGreg Roach            $i                    = 0;
1359a6f13a4aSGreg Roach            while ($i < $count) {
1360a6f13a4aSGreg Roach                $this->gedrec = $this->repeats[$i];
1361a6f13a4aSGreg Roach                $this->fact   = '';
1362a6f13a4aSGreg Roach                $this->desc   = '';
1363a6f13a4aSGreg Roach                if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) {
1364a6f13a4aSGreg Roach                    $this->fact = $match[1];
1365a6f13a4aSGreg Roach                    if ($this->fact === 'EVEN' || $this->fact === 'FACT') {
136613abd6f3SGreg Roach                        $tmatch = [];
1367a6f13a4aSGreg Roach                        if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) {
1368a6f13a4aSGreg Roach                            $this->type = trim($tmatch[1]);
1369a6f13a4aSGreg Roach                        } else {
1370a6f13a4aSGreg Roach                            $this->type = ' ';
1371a6f13a4aSGreg Roach                        }
1372a6f13a4aSGreg Roach                    }
1373a6f13a4aSGreg Roach                    $this->desc = trim($match[2]);
13743d7a8a4cSGreg Roach                    $this->desc .= Functions::getCont(2, $this->gedrec);
1375a6f13a4aSGreg Roach                }
1376a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
1377e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
1378a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
13791aa04befSGreg Roach
13801aa04befSGreg Roach                xml_set_element_handler(
13811aa04befSGreg Roach                    $repeat_parser,
13821aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
13831aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
13841aa04befSGreg Roach                    },
13851aa04befSGreg Roach                    function ($parser, string $name) {
13861aa04befSGreg Roach                        $this->endElement($parser, $name);
13871aa04befSGreg Roach                    }
13881aa04befSGreg Roach                );
13891aa04befSGreg Roach
13901aa04befSGreg Roach                xml_set_character_data_handler(
13911aa04befSGreg Roach                    $repeat_parser,
13921aa04befSGreg Roach                    function ($parser, $data) {
13931aa04befSGreg Roach                        $this->characterData($parser, $data);
13941aa04befSGreg Roach                    }
13951aa04befSGreg Roach                );
13961aa04befSGreg Roach
1397a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
1398a6f13a4aSGreg Roach                    throw new \DomainException(sprintf(
1399a6f13a4aSGreg Roach                        'FactsEHandler XML error: %s at line %d',
1400a6f13a4aSGreg Roach                        xml_error_string(xml_get_error_code($repeat_parser)),
1401a6f13a4aSGreg Roach                        xml_get_current_line_number($repeat_parser)
1402a6f13a4aSGreg Roach                    ));
1403a6f13a4aSGreg Roach                }
1404a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
1405a6f13a4aSGreg Roach                $i++;
1406a6f13a4aSGreg Roach            }
1407a6f13a4aSGreg Roach            // Restore original values
1408e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
1409a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
1410a6f13a4aSGreg Roach        }
141165e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
1412a6f13a4aSGreg Roach    }
1413a6f13a4aSGreg Roach
1414a6f13a4aSGreg Roach    /**
1415a6f13a4aSGreg Roach     * Setting upp or changing variables in the XML
1416d1286247SGreg Roach     * The XML variable name and value is stored in $this->vars
1417a6f13a4aSGreg Roach     *
1418c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14198ba2e626SGreg Roach     *
14208ba2e626SGreg Roach     * @return void
1421a6f13a4aSGreg Roach     */
1422c0fe75acSGreg Roach    private function setVarStartHandler(array $attrs)
1423c1010edaSGreg Roach    {
1424a6f13a4aSGreg Roach        if (empty($attrs['name'])) {
1425a6f13a4aSGreg Roach            throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file');
1426a6f13a4aSGreg Roach        }
1427a6f13a4aSGreg Roach
1428a6f13a4aSGreg Roach        $name  = $attrs['name'];
1429a6f13a4aSGreg Roach        $value = $attrs['value'];
143013abd6f3SGreg Roach        $match = [];
1431a6f13a4aSGreg Roach        // Current GEDCOM record strings
1432044416d2SGreg Roach        if ($value === '@ID') {
14337a6ee1acSGreg Roach            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1434a6f13a4aSGreg Roach                $value = $match[1];
1435a6f13a4aSGreg Roach            }
1436044416d2SGreg Roach        } elseif ($value === '@fact') {
1437a6f13a4aSGreg Roach            $value = $this->fact;
1438044416d2SGreg Roach        } elseif ($value === '@desc') {
1439a6f13a4aSGreg Roach            $value = $this->desc;
1440044416d2SGreg Roach        } elseif ($value === '@generation') {
1441589feda3SGreg Roach            $value = (string) $this->generation;
1442a6f13a4aSGreg Roach        } elseif (preg_match("/@(\w+)/", $value, $match)) {
144313abd6f3SGreg Roach            $gmatch = [];
1444a6f13a4aSGreg Roach            if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) {
14457a6ee1acSGreg Roach                $value = str_replace('@', '', trim($gmatch[1]));
1446a6f13a4aSGreg Roach            }
1447a6f13a4aSGreg Roach        }
1448a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $name, $match)) {
1449d1286247SGreg Roach            $name = $this->vars["'" . $match[1] . "'"]['id'];
1450a6f13a4aSGreg Roach        }
1451a6f13a4aSGreg Roach        $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER);
1452a6f13a4aSGreg Roach        $i     = 0;
1453a6f13a4aSGreg Roach        while ($i < $count) {
1454d1286247SGreg Roach            $t     = $this->vars[$match[$i][1]]['id'];
14557a6ee1acSGreg Roach            $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1);
1456a6f13a4aSGreg Roach            $i++;
1457a6f13a4aSGreg Roach        }
1458a6f13a4aSGreg Roach        if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) {
1459da46f7cdSGreg Roach            $value = I18N::number((int) $match[1]);
1460a6f13a4aSGreg Roach        } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
1461a6f13a4aSGreg Roach            $value = I18N::translate($match[1]);
1462a4956c0eSGreg Roach        } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
1463a6f13a4aSGreg Roach            $value = I18N::translateContext($match[1], $match[2]);
1464a6f13a4aSGreg Roach        }
146552868398SGreg Roach
1466a6f13a4aSGreg Roach        // Arithmetic functions
1467a6f13a4aSGreg Roach        if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) {
146852868398SGreg Roach            // Create an expression language with the functions used by our reports.
146952868398SGreg Roach            $expression_provider  = new ReportExpressionLanguageProvider();
1470c0fe75acSGreg Roach            $expression_cache     = new NullAdapter();
1471c0fe75acSGreg Roach            $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
147252868398SGreg Roach
147352868398SGreg Roach            $value = (string) $expression_language->evaluate($value);
1474a6f13a4aSGreg Roach        }
147552868398SGreg Roach
14767a6ee1acSGreg Roach        if (strpos($value, '@') !== false) {
14777a6ee1acSGreg Roach            $value = '';
1478a6f13a4aSGreg Roach        }
1479d1286247SGreg Roach        $this->vars[$name]['id'] = $value;
1480a6f13a4aSGreg Roach    }
1481a6f13a4aSGreg Roach
1482a6f13a4aSGreg Roach    /**
1483a6f13a4aSGreg Roach     * XML <if > start element
1484a6f13a4aSGreg Roach     *
1485c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
14868ba2e626SGreg Roach     *
14878ba2e626SGreg Roach     * @return void
1488a6f13a4aSGreg Roach     */
1489c0fe75acSGreg Roach    private function ifStartHandler(array $attrs)
1490c1010edaSGreg Roach    {
1491a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1492a6f13a4aSGreg Roach            $this->process_ifs++;
1493a6f13a4aSGreg Roach
1494a6f13a4aSGreg Roach            return;
1495a6f13a4aSGreg Roach        }
1496a6f13a4aSGreg Roach
1497a6f13a4aSGreg Roach        $condition = $attrs['condition'];
149882759250SGreg Roach        $condition = $this->substituteVars($condition, true);
1499c1010edaSGreg Roach        $condition = str_replace([
1500c1010edaSGreg Roach            ' LT ',
1501c1010edaSGreg Roach            ' GT ',
1502c1010edaSGreg Roach        ], [
1503c1010edaSGreg Roach            '<',
1504c1010edaSGreg Roach            '>',
1505c1010edaSGreg Roach        ], $condition);
1506a6f13a4aSGreg Roach        // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT
15077a6ee1acSGreg Roach        $condition = str_replace('@fact:', $this->fact . ':', $condition);
150813abd6f3SGreg Roach        $match     = [];
1509a6f13a4aSGreg Roach        $count     = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER);
1510a6f13a4aSGreg Roach        $i         = 0;
1511a6f13a4aSGreg Roach        while ($i < $count) {
1512a6f13a4aSGreg Roach            $id    = $match[$i][1];
1513a6f13a4aSGreg Roach            $value = '""';
1514044416d2SGreg Roach            if ($id === 'ID') {
15157a6ee1acSGreg Roach                if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1516a6f13a4aSGreg Roach                    $value = "'" . $match[1] . "'";
1517a6f13a4aSGreg Roach                }
15187a6ee1acSGreg Roach            } elseif ($id === 'fact') {
1519a6f13a4aSGreg Roach                $value = '"' . $this->fact . '"';
15207a6ee1acSGreg Roach            } elseif ($id === 'desc') {
1521a6f13a4aSGreg Roach                $value = '"' . addslashes($this->desc) . '"';
15227a6ee1acSGreg Roach            } elseif ($id === 'generation') {
1523a6f13a4aSGreg Roach                $value = '"' . $this->generation . '"';
1524a6f13a4aSGreg Roach            } else {
15257a6ee1acSGreg Roach                $temp  = explode(' ', trim($this->gedrec));
1526a6f13a4aSGreg Roach                $level = $temp[0];
1527a6f13a4aSGreg Roach                if ($level == 0) {
1528a6f13a4aSGreg Roach                    $level++;
1529a6f13a4aSGreg Roach                }
15303d7a8a4cSGreg Roach                $value = $this->getGedcomValue($id, $level, $this->gedrec);
1531a6f13a4aSGreg Roach                if (empty($value)) {
1532a6f13a4aSGreg Roach                    $level++;
15333d7a8a4cSGreg Roach                    $value = $this->getGedcomValue($id, $level, $this->gedrec);
1534a6f13a4aSGreg Roach                }
15358d0ebef0SGreg Roach                $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value);
15365e8c88c1SGreg Roach                $value = '"' . addslashes($value) . '"';
1537a6f13a4aSGreg Roach            }
1538a6f13a4aSGreg Roach            $condition = str_replace("@$id", $value, $condition);
1539a6f13a4aSGreg Roach            $i++;
1540a6f13a4aSGreg Roach        }
15415809450fSGreg Roach
1542cb63a60eSGreg Roach        // Create an expression language with the functions used by our reports.
1543cb63a60eSGreg Roach        $expression_provider  = new ReportExpressionLanguageProvider();
1544c0fe75acSGreg Roach        $expression_cache     = new NullAdapter();
1545c0fe75acSGreg Roach        $expression_language  = new ExpressionLanguage($expression_cache, [$expression_provider]);
1546cb63a60eSGreg Roach
1547cb63a60eSGreg Roach        $ret = $expression_language->evaluate($condition);
15485809450fSGreg Roach
1549a6f13a4aSGreg Roach        if (!$ret) {
1550a6f13a4aSGreg Roach            $this->process_ifs++;
1551a6f13a4aSGreg Roach        }
1552a6f13a4aSGreg Roach    }
1553a6f13a4aSGreg Roach
1554a6f13a4aSGreg Roach    /**
1555a6f13a4aSGreg Roach     * XML <if /> end element
15568ba2e626SGreg Roach     *
15578ba2e626SGreg Roach     * @return void
1558a6f13a4aSGreg Roach     */
1559c1010edaSGreg Roach    private function ifEndHandler()
1560c1010edaSGreg Roach    {
1561a6f13a4aSGreg Roach        if ($this->process_ifs > 0) {
1562a6f13a4aSGreg Roach            $this->process_ifs--;
1563a6f13a4aSGreg Roach        }
1564a6f13a4aSGreg Roach    }
1565a6f13a4aSGreg Roach
1566a6f13a4aSGreg Roach    /**
1567a6f13a4aSGreg Roach     * XML <Footnote > start element
1568a6f13a4aSGreg Roach     * Collect the Footnote links
1569a6f13a4aSGreg Roach     * GEDCOM Records that are protected by Privacy setting will be ignore
1570a6f13a4aSGreg Roach     *
1571c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
15728ba2e626SGreg Roach     *
15738ba2e626SGreg Roach     * @return void
1574a6f13a4aSGreg Roach     */
1575c0fe75acSGreg Roach    private function footnoteStartHandler(array $attrs)
1576c1010edaSGreg Roach    {
15777a6ee1acSGreg Roach        $id = '';
15787a6ee1acSGreg Roach        if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) {
1579a6f13a4aSGreg Roach            $id = $match[2];
1580a6f13a4aSGreg Roach        }
1581299d100dSGreg Roach        $record = GedcomRecord::getInstance($id, $this->tree);
1582a6f13a4aSGreg Roach        if ($record && $record->canShow()) {
15839b3dd960SGreg Roach            $this->print_data_stack[] = $this->print_data;
1584a6f13a4aSGreg Roach            $this->print_data         = true;
15857a6ee1acSGreg Roach            $style                    = '';
1586a6f13a4aSGreg Roach            if (!empty($attrs['style'])) {
1587a6f13a4aSGreg Roach                $style = $attrs['style'];
1588a6f13a4aSGreg Roach            }
1589a6f13a4aSGreg Roach            $this->footnote_element = $this->current_element;
1590e8e7866bSGreg Roach            $this->current_element  = $this->report_root->createFootnote($style);
1591a6f13a4aSGreg Roach        } else {
1592a6f13a4aSGreg Roach            $this->print_data       = false;
1593a6f13a4aSGreg Roach            $this->process_footnote = false;
1594a6f13a4aSGreg Roach        }
1595a6f13a4aSGreg Roach    }
1596a6f13a4aSGreg Roach
1597a6f13a4aSGreg Roach    /**
1598a6f13a4aSGreg Roach     * XML <Footnote /> end element
1599a6f13a4aSGreg Roach     * Print the collected Footnote data
16008ba2e626SGreg Roach     *
16018ba2e626SGreg Roach     * @return void
1602a6f13a4aSGreg Roach     */
1603c1010edaSGreg Roach    private function footnoteEndHandler()
1604c1010edaSGreg Roach    {
1605a6f13a4aSGreg Roach        if ($this->process_footnote) {
1606a6f13a4aSGreg Roach            $this->print_data = array_pop($this->print_data_stack);
1607a6f13a4aSGreg Roach            $temp             = trim($this->current_element->getValue());
1608a6f13a4aSGreg Roach            if (strlen($temp) > 3) {
1609e8e7866bSGreg Roach                $this->wt_report->addElement($this->current_element);
1610a6f13a4aSGreg Roach            }
1611a6f13a4aSGreg Roach            $this->current_element = $this->footnote_element;
1612a6f13a4aSGreg Roach        } else {
1613a6f13a4aSGreg Roach            $this->process_footnote = true;
1614a6f13a4aSGreg Roach        }
1615a6f13a4aSGreg Roach    }
1616a6f13a4aSGreg Roach
1617a6f13a4aSGreg Roach    /**
1618a6f13a4aSGreg Roach     * XML <FootnoteTexts /> element
16198ba2e626SGreg Roach     *
16208ba2e626SGreg Roach     * @return void
1621a6f13a4aSGreg Roach     */
1622c1010edaSGreg Roach    private function footnoteTextsStartHandler()
1623c1010edaSGreg Roach    {
16247a6ee1acSGreg Roach        $temp = 'footnotetexts';
1625e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
1626a6f13a4aSGreg Roach    }
1627a6f13a4aSGreg Roach
1628a6f13a4aSGreg Roach    /**
1629a6f13a4aSGreg Roach     * XML element Forced line break handler - HTML code
16308ba2e626SGreg Roach     *
16318ba2e626SGreg Roach     * @return void
1632a6f13a4aSGreg Roach     */
1633c1010edaSGreg Roach    private function brStartHandler()
1634c1010edaSGreg Roach    {
1635a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1636a6f13a4aSGreg Roach            $this->current_element->addText('<br>');
1637a6f13a4aSGreg Roach        }
1638a6f13a4aSGreg Roach    }
1639a6f13a4aSGreg Roach
1640a6f13a4aSGreg Roach    /**
1641a6f13a4aSGreg Roach     * XML <sp />element Forced space handler
16428ba2e626SGreg Roach     *
16438ba2e626SGreg Roach     * @return void
1644a6f13a4aSGreg Roach     */
1645c1010edaSGreg Roach    private function spStartHandler()
1646c1010edaSGreg Roach    {
1647a6f13a4aSGreg Roach        if ($this->print_data && $this->process_gedcoms === 0) {
1648a6f13a4aSGreg Roach            $this->current_element->addText(' ');
1649a6f13a4aSGreg Roach        }
1650a6f13a4aSGreg Roach    }
1651a6f13a4aSGreg Roach
1652a6f13a4aSGreg Roach    /**
165376692c8bSGreg Roach     * XML <HighlightedImage/>
165476692c8bSGreg Roach     *
1655c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
16568ba2e626SGreg Roach     *
16578ba2e626SGreg Roach     * @return void
1658a6f13a4aSGreg Roach     */
1659c0fe75acSGreg Roach    private function highlightedImageStartHandler(array $attrs)
1660c1010edaSGreg Roach    {
1661a6f13a4aSGreg Roach        $id = '';
16627a6ee1acSGreg Roach        if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
1663a6f13a4aSGreg Roach            $id = $match[1];
1664a6f13a4aSGreg Roach        }
1665a6f13a4aSGreg Roach
1666c21bdddcSGreg Roach        // Position the top corner of this box on the page
1667c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1668a6f13a4aSGreg Roach
1669c21bdddcSGreg Roach        // Position the left corner of this box on the page
1670c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1671a6f13a4aSGreg Roach
167283cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
167383cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1674a6f13a4aSGreg Roach
1675a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
167683cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1677a6f13a4aSGreg Roach
167883cdc021SGreg Roach        // Width, height (or both).
1679c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1680c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1681a6f13a4aSGreg Roach
1682299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
16834a9f750fSGreg Roach        $media_file = $person->findHighlightedMediaFile();
168486a63f51SGreg Roach
168586a63f51SGreg Roach        if ($media_file !== null && $media_file->fileExists()) {
1686c1010edaSGreg Roach            $attributes = getimagesize($media_file->getServerFilename()) ?: [
1687c1010edaSGreg Roach                0,
1688c1010edaSGreg Roach                0,
1689c1010edaSGreg Roach            ];
1690a6f13a4aSGreg Roach            if ($width > 0 && $height == 0) {
16913c3b90deSGreg Roach                $perc   = $width / $attributes[0];
16923c3b90deSGreg Roach                $height = round($attributes[1] * $perc);
1693a6f13a4aSGreg Roach            } elseif ($height > 0 && $width == 0) {
16943c3b90deSGreg Roach                $perc  = $height / $attributes[1];
16953c3b90deSGreg Roach                $width = round($attributes[0] * $perc);
1696a6f13a4aSGreg Roach            } else {
16973c3b90deSGreg Roach                $width  = $attributes[0];
16983c3b90deSGreg Roach                $height = $attributes[1];
1699a6f13a4aSGreg Roach            }
17004a9f750fSGreg Roach            $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1701e8e7866bSGreg Roach            $this->wt_report->addElement($image);
1702a6f13a4aSGreg Roach        }
1703a6f13a4aSGreg Roach    }
1704a6f13a4aSGreg Roach
1705a6f13a4aSGreg Roach    /**
170676692c8bSGreg Roach     * XML <Image/>
170776692c8bSGreg Roach     *
1708c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17098ba2e626SGreg Roach     *
17108ba2e626SGreg Roach     * @return void
1711a6f13a4aSGreg Roach     */
1712c0fe75acSGreg Roach    private function imageStartHandler(array $attrs)
1713c1010edaSGreg Roach    {
171483cdc021SGreg Roach        // Position the top corner of this box on the page. the default is the current position
1715c21bdddcSGreg Roach        $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION);
1716a6f13a4aSGreg Roach
1717a6f13a4aSGreg Roach        // mixed Position the left corner of this box on the page. the default is the current position
1718c21bdddcSGreg Roach        $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION);
1719a6f13a4aSGreg Roach
172083cdc021SGreg Roach        // string Align the image in left, center, right (or empty to use x/y position).
172183cdc021SGreg Roach        $align = $attrs['align'] ?? '';
1722a6f13a4aSGreg Roach
1723a6f13a4aSGreg Roach        // string Next Line should be T:next to the image, N:next line
172483cdc021SGreg Roach        $ln = $attrs['ln'] ?? 'T';
1725a6f13a4aSGreg Roach
172683cdc021SGreg Roach        // Width, height (or both).
1727c21bdddcSGreg Roach        $width  = (float) ($attrs['width'] ?? 0.0);
1728c21bdddcSGreg Roach        $height = (float) ($attrs['height'] ?? 0.0);
1729a6f13a4aSGreg Roach
173083cdc021SGreg Roach        $file = $attrs['file'] ?? '';
173183cdc021SGreg Roach
1732044416d2SGreg Roach        if ($file === '@FILE') {
173313abd6f3SGreg Roach            $match = [];
1734a6f13a4aSGreg Roach            if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) {
1735299d100dSGreg Roach                $mediaobject = Media::getInstance($match[1], $this->tree);
17364a9f750fSGreg Roach                $media_file  = $mediaobject->firstImageFile();
1737cdf416fbSGreg Roach
17384a9f750fSGreg Roach                if ($media_file !== null && $media_file->fileExists()) {
1739c1010edaSGreg Roach                    $attributes = getimagesize($media_file->getServerFilename()) ?: [
1740c1010edaSGreg Roach                        0,
1741c1010edaSGreg Roach                        0,
1742c1010edaSGreg Roach                    ];
1743a6f13a4aSGreg Roach                    if ($width > 0 && $height == 0) {
17443c3b90deSGreg Roach                        $perc   = $width / $attributes[0];
17453c3b90deSGreg Roach                        $height = round($attributes[1] * $perc);
1746a6f13a4aSGreg Roach                    } elseif ($height > 0 && $width == 0) {
17473c3b90deSGreg Roach                        $perc  = $height / $attributes[1];
17483c3b90deSGreg Roach                        $width = round($attributes[0] * $perc);
1749a6f13a4aSGreg Roach                    } else {
17503c3b90deSGreg Roach                        $width  = $attributes[0];
17513c3b90deSGreg Roach                        $height = $attributes[1];
1752a6f13a4aSGreg Roach                    }
17534a9f750fSGreg Roach                    $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln);
1754e8e7866bSGreg Roach                    $this->wt_report->addElement($image);
1755a6f13a4aSGreg Roach                }
1756a6f13a4aSGreg Roach            }
1757a6f13a4aSGreg Roach        } else {
17587a6ee1acSGreg Roach            if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) {
1759a6f13a4aSGreg Roach                $size = getimagesize($file);
1760a6f13a4aSGreg Roach                if ($width > 0 && $height == 0) {
1761a6f13a4aSGreg Roach                    $perc   = $width / $size[0];
1762a6f13a4aSGreg Roach                    $height = round($size[1] * $perc);
1763a6f13a4aSGreg Roach                } elseif ($height > 0 && $width == 0) {
1764a6f13a4aSGreg Roach                    $perc  = $height / $size[1];
1765a6f13a4aSGreg Roach                    $width = round($size[0] * $perc);
1766a6f13a4aSGreg Roach                } else {
1767a6f13a4aSGreg Roach                    $width  = $size[0];
1768a6f13a4aSGreg Roach                    $height = $size[1];
1769a6f13a4aSGreg Roach                }
1770e8e7866bSGreg Roach                $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln);
1771e8e7866bSGreg Roach                $this->wt_report->addElement($image);
1772a6f13a4aSGreg Roach            }
1773a6f13a4aSGreg Roach        }
1774a6f13a4aSGreg Roach    }
1775a6f13a4aSGreg Roach
1776a6f13a4aSGreg Roach    /**
1777a6f13a4aSGreg Roach     * XML <Line> element handler
1778a6f13a4aSGreg Roach     *
1779c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
17808ba2e626SGreg Roach     *
17818ba2e626SGreg Roach     * @return void
1782a6f13a4aSGreg Roach     */
1783c0fe75acSGreg Roach    private function lineStartHandler(array $attrs)
1784c1010edaSGreg Roach    {
1785a6f13a4aSGreg Roach        // Start horizontal position, current position (default)
1786c21bdddcSGreg Roach        $x1 = ReportBaseElement::CURRENT_POSITION;
1787a6f13a4aSGreg Roach        if (isset($attrs['x1'])) {
17887a6ee1acSGreg Roach            if ($attrs['x1'] === '0') {
1789a6f13a4aSGreg Roach                $x1 = 0;
17907a6ee1acSGreg Roach            } elseif ($attrs['x1'] === '.') {
1791c21bdddcSGreg Roach                $x1 = ReportBaseElement::CURRENT_POSITION;
1792a6f13a4aSGreg Roach            } elseif (!empty($attrs['x1'])) {
1793c21bdddcSGreg Roach                $x1 = (float) $attrs['x1'];
1794a6f13a4aSGreg Roach            }
1795a6f13a4aSGreg Roach        }
1796a6f13a4aSGreg Roach        // Start vertical position, current position (default)
1797c21bdddcSGreg Roach        $y1 = ReportBaseElement::CURRENT_POSITION;
1798a6f13a4aSGreg Roach        if (isset($attrs['y1'])) {
17997a6ee1acSGreg Roach            if ($attrs['y1'] === '0') {
1800a6f13a4aSGreg Roach                $y1 = 0;
18017a6ee1acSGreg Roach            } elseif ($attrs['y1'] === '.') {
1802c21bdddcSGreg Roach                $y1 = ReportBaseElement::CURRENT_POSITION;
1803a6f13a4aSGreg Roach            } elseif (!empty($attrs['y1'])) {
1804c21bdddcSGreg Roach                $y1 = (float) $attrs['y1'];
1805a6f13a4aSGreg Roach            }
1806a6f13a4aSGreg Roach        }
1807a6f13a4aSGreg Roach        // End horizontal position, maximum width (default)
1808c21bdddcSGreg Roach        $x2 = ReportBaseElement::CURRENT_POSITION;
1809a6f13a4aSGreg Roach        if (isset($attrs['x2'])) {
18107a6ee1acSGreg Roach            if ($attrs['x2'] === '0') {
1811a6f13a4aSGreg Roach                $x2 = 0;
18127a6ee1acSGreg Roach            } elseif ($attrs['x2'] === '.') {
1813c21bdddcSGreg Roach                $x2 = ReportBaseElement::CURRENT_POSITION;
1814a6f13a4aSGreg Roach            } elseif (!empty($attrs['x2'])) {
1815c21bdddcSGreg Roach                $x2 = (float) $attrs['x2'];
1816a6f13a4aSGreg Roach            }
1817a6f13a4aSGreg Roach        }
1818a6f13a4aSGreg Roach        // End vertical position
1819c21bdddcSGreg Roach        $y2 = ReportBaseElement::CURRENT_POSITION;
1820a6f13a4aSGreg Roach        if (isset($attrs['y2'])) {
18217a6ee1acSGreg Roach            if ($attrs['y2'] === '0') {
1822a6f13a4aSGreg Roach                $y2 = 0;
18237a6ee1acSGreg Roach            } elseif ($attrs['y2'] === '.') {
1824c21bdddcSGreg Roach                $y2 = ReportBaseElement::CURRENT_POSITION;
1825a6f13a4aSGreg Roach            } elseif (!empty($attrs['y2'])) {
1826c21bdddcSGreg Roach                $y2 = (float) $attrs['y2'];
1827a6f13a4aSGreg Roach            }
1828a6f13a4aSGreg Roach        }
1829a6f13a4aSGreg Roach
1830e8e7866bSGreg Roach        $line = $this->report_root->createLine($x1, $y1, $x2, $y2);
1831e8e7866bSGreg Roach        $this->wt_report->addElement($line);
1832a6f13a4aSGreg Roach    }
1833a6f13a4aSGreg Roach
1834a6f13a4aSGreg Roach    /**
183576692c8bSGreg Roach     * XML <List>
1836a6f13a4aSGreg Roach     *
1837c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
18388ba2e626SGreg Roach     *
18398ba2e626SGreg Roach     * @return void
1840a6f13a4aSGreg Roach     */
1841c0fe75acSGreg Roach    private function listStartHandler(array $attrs)
1842c1010edaSGreg Roach    {
1843a6f13a4aSGreg Roach        $this->process_repeats++;
1844a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
1845a6f13a4aSGreg Roach            return;
1846a6f13a4aSGreg Roach        }
1847a6f13a4aSGreg Roach
184813abd6f3SGreg Roach        $match = [];
1849a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
1850a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
1851a6f13a4aSGreg Roach            if (preg_match("/\\$(\w+)/", $sortby, $match)) {
1852d1286247SGreg Roach                $sortby = $this->vars[$match[1]]['id'];
1853a6f13a4aSGreg Roach                $sortby = trim($sortby);
1854a6f13a4aSGreg Roach            }
1855a6f13a4aSGreg Roach        } else {
18567a6ee1acSGreg Roach            $sortby = 'NAME';
1857a6f13a4aSGreg Roach        }
1858a6f13a4aSGreg Roach
1859a6f13a4aSGreg Roach        if (isset($attrs['list'])) {
1860a6f13a4aSGreg Roach            $listname = $attrs['list'];
1861a6f13a4aSGreg Roach        } else {
18627a6ee1acSGreg Roach            $listname = 'individual';
1863a6f13a4aSGreg Roach        }
1864195b5e75SGreg Roach
1865a6f13a4aSGreg Roach        // Some filters/sorts can be applied using SQL, while others require PHP
1866a6f13a4aSGreg Roach        switch ($listname) {
18677a6ee1acSGreg Roach            case 'pending':
18686c0bef3aSGreg Roach                $xrefs = DB::table('change')
1869195b5e75SGreg Roach                    ->whereIn('change_id', function (Builder $query): void {
1870195b5e75SGreg Roach                        $query->select(DB::raw('MAX(change_id)'))
1871195b5e75SGreg Roach                            ->from('change')
1872195b5e75SGreg Roach                            ->where('gedcom_id', '=', $this->tree->id())
1873195b5e75SGreg Roach                            ->where('status', '=', 'pending')
1874195b5e75SGreg Roach                            ->groupBy('xref');
1875195b5e75SGreg Roach                    })
18766c0bef3aSGreg Roach                    ->pluck('xref');
1877195b5e75SGreg Roach
187813abd6f3SGreg Roach                $this->list = [];
18796c0bef3aSGreg Roach                foreach ($xrefs as $xref) {
18806c0bef3aSGreg Roach                    $this->list[] = GedcomRecord::getInstance($xref, $this->tree);
1881a6f13a4aSGreg Roach                }
1882a6f13a4aSGreg Roach                break;
1883a6f13a4aSGreg Roach            case 'individual':
18845985adfbSGreg Roach                $query = DB::table('individuals')
18855985adfbSGreg Roach                    ->where('i_file', '=', $this->tree->id())
18865985adfbSGreg Roach                    ->select(['i_id AS xref', 'i_gedcom AS gedcom'])
18875985adfbSGreg Roach                    ->distinct();
18885985adfbSGreg Roach
1889a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1890a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
189182759250SGreg Roach                        $value = $this->substituteVars($value, false);
1892a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1893a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
18945985adfbSGreg Roach                            $query->join('dates AS ' . $attr, function (JoinClause $join) use ($attr): void {
18955985adfbSGreg Roach                                $join
18965985adfbSGreg Roach                                    ->on($attr . '.d_gid', '=', 'i_id')
18975985adfbSGreg Roach                                    ->on($attr . '.d_file', '=', 'i_file');
18985985adfbSGreg Roach                            });
18995985adfbSGreg Roach
19005985adfbSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
19015985adfbSGreg Roach
1902a6f13a4aSGreg Roach                            $date = new Date($match[3]);
19035985adfbSGreg Roach
1904044416d2SGreg Roach                            if ($match[2] === 'LTE') {
19055985adfbSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1906a6f13a4aSGreg Roach                            } else {
19075985adfbSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1908a6f13a4aSGreg Roach                            }
19095985adfbSGreg Roach
19105985adfbSGreg Roach                            if ($sortby === $match[1]) {
19115985adfbSGreg Roach                                $sortby = '';
19125985adfbSGreg Roach                                $query->orderBy($attr . '.d_julianday1');
1913a6f13a4aSGreg Roach                            }
19145985adfbSGreg Roach
19155985adfbSGreg Roach                            // This filter has been fully processed
19165985adfbSGreg Roach                            unset($attrs[$attr]);
1917a6f13a4aSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
19185985adfbSGreg Roach                            if ($match[1] !== '' || $sortby === 'NAME') {
19195985adfbSGreg Roach                                $query->join('name AS ' . $attr, function (JoinClause $join) use ($attr): void {
19205985adfbSGreg Roach                                    $join
19215985adfbSGreg Roach                                        ->on($attr . '.n_id', '=', 'i_id')
19225985adfbSGreg Roach                                        ->on($attr . '.n_file', '=', 'i_file');
19235985adfbSGreg Roach                                });
1924a6f13a4aSGreg Roach                                // Search the DB only if there is any name supplied
19257a6ee1acSGreg Roach                                if ($match[1] != '') {
19267a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
19275d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
19285985adfbSGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . $name . '%');
1929a6f13a4aSGreg Roach                                    }
1930a6f13a4aSGreg Roach                                }
1931a6f13a4aSGreg Roach                            }
19325985adfbSGreg Roach
19335985adfbSGreg Roach                            // This filter has been fully processed
19345985adfbSGreg Roach                            unset($attrs[$attr]);
1935a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
19365985adfbSGreg Roach                            // Convert newline escape sequences to actual new lines
1937b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
19385985adfbSGreg Roach
19395985adfbSGreg Roach                            $query->where('i_gedcom', 'REGEXP', $match[1]);
19405985adfbSGreg Roach
19415985adfbSGreg Roach                            // This filter has been fully processed
19425985adfbSGreg Roach                            unset($attrs[$attr]);
1943a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
1944*9dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
19455985adfbSGreg Roach                            $query
19465985adfbSGreg Roach                                ->join('places AS ' . $attr . 'a', 'p_file', '=', 'i_file')
19475985adfbSGreg Roach                                ->join('placelinks AS ' . $attr . 'b', function (JoinClause $join) use ($attr): void {
19485985adfbSGreg Roach                                    $join
19495985adfbSGreg Roach                                        ->on($attr . 'b.pl_file', '=', $attr . 'a.p_file')
19505985adfbSGreg Roach                                        ->on($attr . 'b.pl_p_id', '=', $attr . 'a.p_id');
19515985adfbSGreg Roach                                })
19525985adfbSGreg Roach                                ->where($attr . 'a.p_place', 'LIKE', '%' . $match[1] . '%');
1953a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
19545985adfbSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
1955*9dc7e9e3SGreg Roach                            $query->where('i_gedcom', 'LIKE', '%' . $match[1] . '%' . $match[2] . '%' . $match[3] . '%');
1956a6f13a4aSGreg Roach                        }
1957a6f13a4aSGreg Roach                    }
1958a6f13a4aSGreg Roach                }
1959a6f13a4aSGreg Roach
196013abd6f3SGreg Roach                $this->list = [];
1961a6f13a4aSGreg Roach
19625985adfbSGreg Roach                foreach ($query->get() as $row) {
1963299d100dSGreg Roach                    $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom);
1964a6f13a4aSGreg Roach                }
1965a6f13a4aSGreg Roach                break;
1966a6f13a4aSGreg Roach
1967a6f13a4aSGreg Roach            case 'family':
196830fc2b1eSGreg Roach                $query = DB::table('families')
196930fc2b1eSGreg Roach                    ->where('f_file', '=', $this->tree->id())
197030fc2b1eSGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom'])
197130fc2b1eSGreg Roach                    ->distinct();
197230fc2b1eSGreg Roach
1973a6f13a4aSGreg Roach                foreach ($attrs as $attr => $value) {
1974a6f13a4aSGreg Roach                    if (strpos($attr, 'filter') === 0 && $value) {
197582759250SGreg Roach                        $value = $this->substituteVars($value, false);
1976a6f13a4aSGreg Roach                        // Convert the various filters into SQL
1977a6f13a4aSGreg Roach                        if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) {
197830fc2b1eSGreg Roach                            $query->join('dates AS ' . $attr, function (JoinClause $join) use ($attr): void {
197930fc2b1eSGreg Roach                                $join
198030fc2b1eSGreg Roach                                    ->on($attr . '.d_gid', '=', 'f_id')
198130fc2b1eSGreg Roach                                    ->on($attr . '.d_file', '=', 'f_file');
198230fc2b1eSGreg Roach                            });
198330fc2b1eSGreg Roach
198430fc2b1eSGreg Roach                            $query->where($attr . '.d_fact', '=', $match[1]);
198530fc2b1eSGreg Roach
1986a6f13a4aSGreg Roach                            $date = new Date($match[3]);
198730fc2b1eSGreg Roach
1988044416d2SGreg Roach                            if ($match[2] === 'LTE') {
198930fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay());
1990a6f13a4aSGreg Roach                            } else {
199130fc2b1eSGreg Roach                                $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay());
1992a6f13a4aSGreg Roach                            }
199330fc2b1eSGreg Roach
199430fc2b1eSGreg Roach                            if ($sortby === $match[1]) {
19957a6ee1acSGreg Roach                                $sortby = '';
199630fc2b1eSGreg Roach                                $query->orderBy($attr . '.d_julianday1');
1997a6f13a4aSGreg Roach                            }
199830fc2b1eSGreg Roach
199930fc2b1eSGreg Roach                            // This filter has been fully processed
200030fc2b1eSGreg Roach                            unset($attrs[$attr]);
2001a6f13a4aSGreg Roach                        } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) {
200230fc2b1eSGreg Roach                            // Convert newline escape sequences to actual new lines
2003b4e512fdSGreg Roach                            $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]);
200430fc2b1eSGreg Roach
200530fc2b1eSGreg Roach                            $query->where('f_gedcom', 'REGEXP', $match[1]);
200630fc2b1eSGreg Roach
200730fc2b1eSGreg Roach                            // This filter has been fully processed
200830fc2b1eSGreg Roach                            unset($attrs[$attr]);
200930fc2b1eSGreg Roach                        } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) {
201030fc2b1eSGreg Roach                            if ($match[1] !== '' || $sortby === 'NAME') {
201130fc2b1eSGreg Roach                                $query->join('name AS ' . $attr, function (JoinClause $join) use ($attr): void {
201230fc2b1eSGreg Roach                                    $join
201330fc2b1eSGreg Roach                                        ->on($attr . '.n_file', '=', 'f_file')
201430fc2b1eSGreg Roach                                        ->where(function (Builder $query) use ($attr): void {
201530fc2b1eSGreg Roach                                            $query
201630fc2b1eSGreg Roach                                                ->whereColumn('n_id', '=', 'f_husb')
201730fc2b1eSGreg Roach                                                ->orWhereColumn('n_id', '=', 'f_wife');
201830fc2b1eSGreg Roach                                        });
201930fc2b1eSGreg Roach                                });
20205d0bc43dSGreg Roach                                // Search the DB only if there is any name supplied
20217a6ee1acSGreg Roach                                if ($match[1] != '') {
20227a6ee1acSGreg Roach                                    $names = explode(' ', $match[1]);
20235d0bc43dSGreg Roach                                    foreach ($names as $n => $name) {
202430fc2b1eSGreg Roach                                        $query->where($attr . '.n_full', 'LIKE', '%' . $name . '%');
20255d0bc43dSGreg Roach                                    }
20265d0bc43dSGreg Roach                                }
2027a6f13a4aSGreg Roach                            }
202830fc2b1eSGreg Roach
202930fc2b1eSGreg Roach                            // This filter has been fully processed
203030fc2b1eSGreg Roach                            unset($attrs[$attr]);
2031a6f13a4aSGreg Roach                        } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) {
2032*9dc7e9e3SGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
203330fc2b1eSGreg Roach                            $query
203430fc2b1eSGreg Roach                                ->join('places AS ' . $attr . 'a', 'p_file', '=', 'f_file')
203530fc2b1eSGreg Roach                                ->join('placelinks AS ' . $attr . 'b', function (JoinClause $join) use ($attr): void {
203630fc2b1eSGreg Roach                                    $join
203730fc2b1eSGreg Roach                                        ->on($attr . 'b.pl_file', '=', $attr . 'a.p_file')
203830fc2b1eSGreg Roach                                        ->on($attr . 'b.pl_p_id', '=', $attr . 'a.p_id');
203930fc2b1eSGreg Roach                                })
204030fc2b1eSGreg Roach                                ->where($attr . 'a.p_place', 'LIKE', '%' . $match[1] . '%');
2041a6f13a4aSGreg Roach                        } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) {
204230fc2b1eSGreg Roach                            // Don't unset this filter. This is just initial filtering for performance
2043*9dc7e9e3SGreg Roach                            $query->where('f_gedcom', 'LIKE', '%' . $match[1] . '%' . $match[2] . '%' . $match[3] . '%');
2044a6f13a4aSGreg Roach                        }
2045a6f13a4aSGreg Roach                    }
2046a6f13a4aSGreg Roach                }
2047a6f13a4aSGreg Roach
204813abd6f3SGreg Roach                $this->list = [];
2049a6f13a4aSGreg Roach
205030fc2b1eSGreg Roach                foreach ($query->get() as $row) {
2051299d100dSGreg Roach                    $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom);
2052a6f13a4aSGreg Roach                }
2053a6f13a4aSGreg Roach                break;
2054a6f13a4aSGreg Roach
2055a6f13a4aSGreg Roach            default:
2056a6f13a4aSGreg Roach                throw new \DomainException('Invalid list name: ' . $listname);
2057a6f13a4aSGreg Roach        }
2058a6f13a4aSGreg Roach
205913abd6f3SGreg Roach        $filters  = [];
206013abd6f3SGreg Roach        $filters2 = [];
2061a6f13a4aSGreg Roach        if (isset($attrs['filter1']) && count($this->list) > 0) {
2062a6f13a4aSGreg Roach            foreach ($attrs as $key => $value) {
2063a6f13a4aSGreg Roach                if (preg_match("/filter(\d)/", $key)) {
2064a6f13a4aSGreg Roach                    $condition = $value;
2065a6f13a4aSGreg Roach                    if (preg_match("/@(\w+)/", $condition, $match)) {
2066a6f13a4aSGreg Roach                        $id    = $match[1];
2067a6f13a4aSGreg Roach                        $value = "''";
2068044416d2SGreg Roach                        if ($id === 'ID') {
20697a6ee1acSGreg Roach                            if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) {
2070a6f13a4aSGreg Roach                                $value = "'" . $match[1] . "'";
2071a6f13a4aSGreg Roach                            }
2072044416d2SGreg Roach                        } elseif ($id === 'fact') {
2073a6f13a4aSGreg Roach                            $value = "'" . $this->fact . "'";
2074044416d2SGreg Roach                        } elseif ($id === 'desc') {
2075a6f13a4aSGreg Roach                            $value = "'" . $this->desc . "'";
2076a6f13a4aSGreg Roach                        } else {
2077a6f13a4aSGreg Roach                            if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) {
20787a6ee1acSGreg Roach                                $value = "'" . str_replace('@', '', trim($match[1])) . "'";
2079a6f13a4aSGreg Roach                            }
2080a6f13a4aSGreg Roach                        }
2081a6f13a4aSGreg Roach                        $condition = preg_replace("/@$id/", $value, $condition);
2082a6f13a4aSGreg Roach                    }
2083a6f13a4aSGreg Roach                    //-- handle regular expressions
2084a6f13a4aSGreg Roach                    if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) {
2085a6f13a4aSGreg Roach                        $tag  = trim($match[1]);
2086a6f13a4aSGreg Roach                        $expr = trim($match[2]);
2087a6f13a4aSGreg Roach                        $val  = trim($match[3]);
2088a6f13a4aSGreg Roach                        if (preg_match("/\\$(\w+)/", $val, $match)) {
2089d1286247SGreg Roach                            $val = $this->vars[$match[1]]['id'];
2090a6f13a4aSGreg Roach                            $val = trim($val);
2091a6f13a4aSGreg Roach                        }
2092a6f13a4aSGreg Roach                        if ($val) {
20937a6ee1acSGreg Roach                            $searchstr = '';
20947a6ee1acSGreg Roach                            $tags      = explode(':', $tag);
2095a6f13a4aSGreg Roach                            //-- only limit to a level number if we are specifically looking at a level
2096a6f13a4aSGreg Roach                            if (count($tags) > 1) {
2097a6f13a4aSGreg Roach                                $level = 1;
2098a6f13a4aSGreg Roach                                foreach ($tags as $t) {
2099a6f13a4aSGreg Roach                                    if (!empty($searchstr)) {
2100a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n";
2101a6f13a4aSGreg Roach                                    }
2102a6f13a4aSGreg Roach                                    //-- search for both EMAIL and _EMAIL... silly double gedcom standard
2103044416d2SGreg Roach                                    if ($t === 'EMAIL' || $t === '_EMAIL') {
21047a6ee1acSGreg Roach                                        $t = '_?EMAIL';
2105a6f13a4aSGreg Roach                                    }
21067a6ee1acSGreg Roach                                    $searchstr .= $level . ' ' . $t;
2107a6f13a4aSGreg Roach                                    $level++;
2108a6f13a4aSGreg Roach                                }
2109a6f13a4aSGreg Roach                            } else {
2110044416d2SGreg Roach                                if ($tag === 'EMAIL' || $tag === '_EMAIL') {
21117a6ee1acSGreg Roach                                    $tag = '_?EMAIL';
2112a6f13a4aSGreg Roach                                }
2113a6f13a4aSGreg Roach                                $t         = $tag;
21147a6ee1acSGreg Roach                                $searchstr = '1 ' . $tag;
2115a6f13a4aSGreg Roach                            }
2116a6f13a4aSGreg Roach                            switch ($expr) {
21177a6ee1acSGreg Roach                                case 'CONTAINS':
2118044416d2SGreg Roach                                    if ($t === 'PLAC') {
2119a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*[, ]*" . $val;
2120a6f13a4aSGreg Roach                                    } else {
2121a6f13a4aSGreg Roach                                        $searchstr .= "[^\n]*" . $val;
2122a6f13a4aSGreg Roach                                    }
2123a6f13a4aSGreg Roach                                    $filters[] = $searchstr;
2124a6f13a4aSGreg Roach                                    break;
2125a6f13a4aSGreg Roach                                default:
2126c1010edaSGreg Roach                                    $filters2[] = [
2127c1010edaSGreg Roach                                        'tag'  => $tag,
2128c1010edaSGreg Roach                                        'expr' => $expr,
2129c1010edaSGreg Roach                                        'val'  => $val,
2130c1010edaSGreg Roach                                    ];
2131a6f13a4aSGreg Roach                                    break;
2132a6f13a4aSGreg Roach                            }
2133a6f13a4aSGreg Roach                        }
2134a6f13a4aSGreg Roach                    }
2135a6f13a4aSGreg Roach                }
2136a6f13a4aSGreg Roach            }
2137a6f13a4aSGreg Roach        }
2138a6f13a4aSGreg Roach        //-- apply other filters to the list that could not be added to the search string
2139a6f13a4aSGreg Roach        if ($filters) {
2140a6f13a4aSGreg Roach            foreach ($this->list as $key => $record) {
2141a6f13a4aSGreg Roach                foreach ($filters as $filter) {
2142299d100dSGreg Roach                    if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) {
2143a6f13a4aSGreg Roach                        unset($this->list[$key]);
2144a6f13a4aSGreg Roach                        break;
2145a6f13a4aSGreg Roach                    }
2146a6f13a4aSGreg Roach                }
2147a6f13a4aSGreg Roach            }
2148a6f13a4aSGreg Roach        }
2149a6f13a4aSGreg Roach        if ($filters2) {
215013abd6f3SGreg Roach            $mylist = [];
2151a6f13a4aSGreg Roach            foreach ($this->list as $indi) {
2152c0935879SGreg Roach                $key  = $indi->xref();
2153299d100dSGreg Roach                $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree));
2154a6f13a4aSGreg Roach                $keep = true;
2155a6f13a4aSGreg Roach                foreach ($filters2 as $filter) {
2156a6f13a4aSGreg Roach                    if ($keep) {
2157a6f13a4aSGreg Roach                        $tag  = $filter['tag'];
2158a6f13a4aSGreg Roach                        $expr = $filter['expr'];
2159a6f13a4aSGreg Roach                        $val  = $filter['val'];
2160a6f13a4aSGreg Roach                        if ($val == "''") {
21617a6ee1acSGreg Roach                            $val = '';
2162a6f13a4aSGreg Roach                        }
21637a6ee1acSGreg Roach                        $tags = explode(':', $tag);
2164a6f13a4aSGreg Roach                        $t    = end($tags);
21653d7a8a4cSGreg Roach                        $v    = $this->getGedcomValue($tag, 1, $grec);
2166a6f13a4aSGreg Roach                        //-- check for EMAIL and _EMAIL (silly double gedcom standard :P)
2167044416d2SGreg Roach                        if ($t === 'EMAIL' && empty($v)) {
21687a6ee1acSGreg Roach                            $tag  = str_replace('EMAIL', '_EMAIL', $tag);
21697a6ee1acSGreg Roach                            $tags = explode(':', $tag);
2170a6f13a4aSGreg Roach                            $t    = end($tags);
21713d7a8a4cSGreg Roach                            $v    = Functions::getSubRecord(1, $tag, $grec);
2172a6f13a4aSGreg Roach                        }
2173a6f13a4aSGreg Roach
2174a6f13a4aSGreg Roach                        switch ($expr) {
21757a6ee1acSGreg Roach                            case 'GTE':
2176044416d2SGreg Roach                                if ($t === 'DATE') {
2177a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2178a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2179a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) >= 0);
2180a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2181a6f13a4aSGreg Roach                                    $keep = true;
2182a6f13a4aSGreg Roach                                }
2183a6f13a4aSGreg Roach                                break;
21847a6ee1acSGreg Roach                            case 'LTE':
2185044416d2SGreg Roach                                if ($t === 'DATE') {
2186a6f13a4aSGreg Roach                                    $date1 = new Date($v);
2187a6f13a4aSGreg Roach                                    $date2 = new Date($val);
2188a6f13a4aSGreg Roach                                    $keep  = (Date::compare($date1, $date2) <= 0);
2189a6f13a4aSGreg Roach                                } elseif ($val >= $v) {
2190a6f13a4aSGreg Roach                                    $keep = true;
2191a6f13a4aSGreg Roach                                }
2192a6f13a4aSGreg Roach                                break;
2193a6f13a4aSGreg Roach                            default:
2194a6f13a4aSGreg Roach                                if ($v == $val) {
2195a6f13a4aSGreg Roach                                    $keep = true;
2196a6f13a4aSGreg Roach                                } else {
2197a6f13a4aSGreg Roach                                    $keep = false;
2198a6f13a4aSGreg Roach                                }
2199a6f13a4aSGreg Roach                                break;
2200a6f13a4aSGreg Roach                        }
2201a6f13a4aSGreg Roach                    }
2202a6f13a4aSGreg Roach                }
2203a6f13a4aSGreg Roach                if ($keep) {
2204a6f13a4aSGreg Roach                    $mylist[$key] = $indi;
2205a6f13a4aSGreg Roach                }
2206a6f13a4aSGreg Roach            }
2207a6f13a4aSGreg Roach            $this->list = $mylist;
2208a6f13a4aSGreg Roach        }
2209a6f13a4aSGreg Roach
2210a6f13a4aSGreg Roach        switch ($sortby) {
2211a6f13a4aSGreg Roach            case 'NAME':
2212a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2213a6f13a4aSGreg Roach                break;
2214a6f13a4aSGreg Roach            case 'CHAN':
221518d7a90dSGreg Roach                uasort($this->list, function (GedcomRecord $x, GedcomRecord $y): int {
2216a6f13a4aSGreg Roach                    return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true);
2217a6f13a4aSGreg Roach                });
2218a6f13a4aSGreg Roach                break;
2219a6f13a4aSGreg Roach            case 'BIRT:DATE':
2220a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2221a6f13a4aSGreg Roach                break;
2222a6f13a4aSGreg Roach            case 'DEAT:DATE':
2223a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2224a6f13a4aSGreg Roach                break;
2225a6f13a4aSGreg Roach            case 'MARR:DATE':
22265d0bc43dSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate');
2227a6f13a4aSGreg Roach                break;
2228a6f13a4aSGreg Roach            default:
2229a6f13a4aSGreg Roach                // unsorted or already sorted by SQL
2230a6f13a4aSGreg Roach                break;
2231a6f13a4aSGreg Roach        }
2232a6f13a4aSGreg Roach
22339b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2234e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2235a6f13a4aSGreg Roach    }
2236a6f13a4aSGreg Roach
2237a6f13a4aSGreg Roach    /**
223876692c8bSGreg Roach     * XML <List>
22398ba2e626SGreg Roach     *
22408ba2e626SGreg Roach     * @return void
2241a6f13a4aSGreg Roach     */
2242c1010edaSGreg Roach    private function listEndHandler()
2243c1010edaSGreg Roach    {
2244a6f13a4aSGreg Roach        $this->process_repeats--;
2245a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2246a6f13a4aSGreg Roach            return;
2247a6f13a4aSGreg Roach        }
2248a6f13a4aSGreg Roach
2249a6f13a4aSGreg Roach        // Check if there is any list
2250a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2251a6f13a4aSGreg Roach            $lineoffset = 0;
2252a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2253a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2254a6f13a4aSGreg Roach            }
2255a6f13a4aSGreg Roach            //-- read the xml from the file
2256299d100dSGreg Roach            $lines = file($this->report);
22577a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2258a6f13a4aSGreg Roach                $lineoffset--;
2259a6f13a4aSGreg Roach            }
2260a6f13a4aSGreg Roach            $lineoffset++;
2261a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2262a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2263a6f13a4aSGreg Roach            // List Level counter
2264a6f13a4aSGreg Roach            $count = 1;
2265a6f13a4aSGreg Roach            while (0 < $count) {
22667a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<List') !== false) {
2267a6f13a4aSGreg Roach                    $count++;
22687a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</List') !== false) {
2269a6f13a4aSGreg Roach                    $count--;
2270a6f13a4aSGreg Roach                }
2271a6f13a4aSGreg Roach                if (0 < $count) {
2272a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2273a6f13a4aSGreg Roach                }
2274a6f13a4aSGreg Roach                $line_nr++;
2275a6f13a4aSGreg Roach            }
2276a6f13a4aSGreg Roach            // No need to drag this
2277a6f13a4aSGreg Roach            unset($lines);
22787a6ee1acSGreg Roach            $reportxml .= '</tempdoc>';
2279a6f13a4aSGreg Roach            // Save original values
22809b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2281a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2282a6f13a4aSGreg Roach
2283a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2284a6f13a4aSGreg Roach            $this->list_private = 0;
2285a6f13a4aSGreg Roach            foreach ($this->list as $record) {
2286a6f13a4aSGreg Roach                if ($record->canShow()) {
2287f4afa648SGreg Roach                    $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree()));
2288a6f13a4aSGreg Roach                    //-- start the sax parser
2289a6f13a4aSGreg Roach                    $repeat_parser = xml_parser_create();
2290e8e7866bSGreg Roach                    $this->parser  = $repeat_parser;
2291a6f13a4aSGreg Roach                    xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
22921aa04befSGreg Roach
22931aa04befSGreg Roach                    xml_set_element_handler(
22941aa04befSGreg Roach                        $repeat_parser,
22951aa04befSGreg Roach                        function ($parser, string $name, array $attrs) {
22961aa04befSGreg Roach                            $this->startElement($parser, $name, $attrs);
22971aa04befSGreg Roach                        },
22981aa04befSGreg Roach                        function ($parser, string $name) {
22991aa04befSGreg Roach                            $this->endElement($parser, $name);
23001aa04befSGreg Roach                        }
23011aa04befSGreg Roach                    );
23021aa04befSGreg Roach
23031aa04befSGreg Roach                    xml_set_character_data_handler(
23041aa04befSGreg Roach                        $repeat_parser,
23051aa04befSGreg Roach                        function ($parser, $data) {
23061aa04befSGreg Roach                            $this->characterData($parser, $data);
23071aa04befSGreg Roach                        }
23081aa04befSGreg Roach                    );
23091aa04befSGreg Roach
2310a6f13a4aSGreg Roach                    if (!xml_parse($repeat_parser, $reportxml, true)) {
2311a6f13a4aSGreg Roach                        throw new \DomainException(sprintf(
2312a6f13a4aSGreg Roach                            'ListEHandler XML error: %s at line %d',
2313a6f13a4aSGreg Roach                            xml_error_string(xml_get_error_code($repeat_parser)),
2314a6f13a4aSGreg Roach                            xml_get_current_line_number($repeat_parser)
2315a6f13a4aSGreg Roach                        ));
2316a6f13a4aSGreg Roach                    }
2317a6f13a4aSGreg Roach                    xml_parser_free($repeat_parser);
2318a6f13a4aSGreg Roach                } else {
2319a6f13a4aSGreg Roach                    $this->list_private++;
2320a6f13a4aSGreg Roach                }
2321a6f13a4aSGreg Roach            }
232213abd6f3SGreg Roach            $this->list   = [];
2323e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2324a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2325a6f13a4aSGreg Roach        }
232665e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2327a6f13a4aSGreg Roach    }
2328a6f13a4aSGreg Roach
2329a6f13a4aSGreg Roach    /**
2330a6f13a4aSGreg Roach     * XML <ListTotal> element handler
2331a6f13a4aSGreg Roach     * Prints the total number of records in a list
2332a6f13a4aSGreg Roach     * The total number is collected from
2333a6f13a4aSGreg Roach     * List and Relatives
23348ba2e626SGreg Roach     *
23358ba2e626SGreg Roach     * @return void
2336a6f13a4aSGreg Roach     */
2337c1010edaSGreg Roach    private function listTotalStartHandler()
2338c1010edaSGreg Roach    {
2339a6f13a4aSGreg Roach        if ($this->list_private == 0) {
2340589feda3SGreg Roach            $this->current_element->addText((string) $this->list_total);
2341a6f13a4aSGreg Roach        } else {
23427a6ee1acSGreg Roach            $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total);
2343a6f13a4aSGreg Roach        }
2344a6f13a4aSGreg Roach    }
2345a6f13a4aSGreg Roach
2346a6f13a4aSGreg Roach    /**
234776692c8bSGreg Roach     * XML <Relatives>
234876692c8bSGreg Roach     *
2349c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
23508ba2e626SGreg Roach     *
23518ba2e626SGreg Roach     * @return void
2352a6f13a4aSGreg Roach     */
2353c0fe75acSGreg Roach    private function relativesStartHandler(array $attrs)
2354c1010edaSGreg Roach    {
2355a6f13a4aSGreg Roach        $this->process_repeats++;
2356a6f13a4aSGreg Roach        if ($this->process_repeats > 1) {
2357a6f13a4aSGreg Roach            return;
2358a6f13a4aSGreg Roach        }
2359a6f13a4aSGreg Roach
23607a6ee1acSGreg Roach        $sortby = 'NAME';
2361a6f13a4aSGreg Roach        if (isset($attrs['sortby'])) {
2362a6f13a4aSGreg Roach            $sortby = $attrs['sortby'];
2363a6f13a4aSGreg Roach        }
236413abd6f3SGreg Roach        $match = [];
2365a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $sortby, $match)) {
2366d1286247SGreg Roach            $sortby = $this->vars[$match[1]]['id'];
2367a6f13a4aSGreg Roach            $sortby = trim($sortby);
2368a6f13a4aSGreg Roach        }
2369a6f13a4aSGreg Roach
2370a6f13a4aSGreg Roach        $maxgen = -1;
2371a6f13a4aSGreg Roach        if (isset($attrs['maxgen'])) {
2372a6f13a4aSGreg Roach            $maxgen = $attrs['maxgen'];
2373a6f13a4aSGreg Roach        }
2374044416d2SGreg Roach        if ($maxgen === '*') {
2375a6f13a4aSGreg Roach            $maxgen = -1;
2376a6f13a4aSGreg Roach        }
2377a6f13a4aSGreg Roach
23787a6ee1acSGreg Roach        $group = 'child-family';
2379a6f13a4aSGreg Roach        if (isset($attrs['group'])) {
2380a6f13a4aSGreg Roach            $group = $attrs['group'];
2381a6f13a4aSGreg Roach        }
2382a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $group, $match)) {
2383d1286247SGreg Roach            $group = $this->vars[$match[1]]['id'];
2384a6f13a4aSGreg Roach            $group = trim($group);
2385a6f13a4aSGreg Roach        }
2386a6f13a4aSGreg Roach
23877a6ee1acSGreg Roach        $id = '';
2388a6f13a4aSGreg Roach        if (isset($attrs['id'])) {
2389a6f13a4aSGreg Roach            $id = $attrs['id'];
2390a6f13a4aSGreg Roach        }
2391a6f13a4aSGreg Roach        if (preg_match("/\\$(\w+)/", $id, $match)) {
2392d1286247SGreg Roach            $id = $this->vars[$match[1]]['id'];
2393a6f13a4aSGreg Roach            $id = trim($id);
2394a6f13a4aSGreg Roach        }
2395a6f13a4aSGreg Roach
239613abd6f3SGreg Roach        $this->list = [];
2397299d100dSGreg Roach        $person     = Individual::getInstance($id, $this->tree);
2398a6f13a4aSGreg Roach        if (!empty($person)) {
2399a6f13a4aSGreg Roach            $this->list[$id] = $person;
2400a6f13a4aSGreg Roach            switch ($group) {
24017a6ee1acSGreg Roach                case 'child-family':
2402a6f13a4aSGreg Roach                    foreach ($person->getChildFamilies() as $family) {
2403a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2404a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2405a6f13a4aSGreg Roach                        if (!empty($husband)) {
2406c0935879SGreg Roach                            $this->list[$husband->xref()] = $husband;
2407a6f13a4aSGreg Roach                        }
2408a6f13a4aSGreg Roach                        if (!empty($wife)) {
2409c0935879SGreg Roach                            $this->list[$wife->xref()] = $wife;
2410a6f13a4aSGreg Roach                        }
2411a6f13a4aSGreg Roach                        $children = $family->getChildren();
2412a6f13a4aSGreg Roach                        foreach ($children as $child) {
2413a6f13a4aSGreg Roach                            if (!empty($child)) {
2414c0935879SGreg Roach                                $this->list[$child->xref()] = $child;
2415a6f13a4aSGreg Roach                            }
2416a6f13a4aSGreg Roach                        }
2417a6f13a4aSGreg Roach                    }
2418a6f13a4aSGreg Roach                    break;
24197a6ee1acSGreg Roach                case 'spouse-family':
2420a6f13a4aSGreg Roach                    foreach ($person->getSpouseFamilies() as $family) {
2421a6f13a4aSGreg Roach                        $husband = $family->getHusband();
2422a6f13a4aSGreg Roach                        $wife    = $family->getWife();
2423a6f13a4aSGreg Roach                        if (!empty($husband)) {
2424c0935879SGreg Roach                            $this->list[$husband->xref()] = $husband;
2425a6f13a4aSGreg Roach                        }
2426a6f13a4aSGreg Roach                        if (!empty($wife)) {
2427c0935879SGreg Roach                            $this->list[$wife->xref()] = $wife;
2428a6f13a4aSGreg Roach                        }
2429a6f13a4aSGreg Roach                        $children = $family->getChildren();
2430a6f13a4aSGreg Roach                        foreach ($children as $child) {
2431a6f13a4aSGreg Roach                            if (!empty($child)) {
2432c0935879SGreg Roach                                $this->list[$child->xref()] = $child;
2433a6f13a4aSGreg Roach                            }
2434a6f13a4aSGreg Roach                        }
2435a6f13a4aSGreg Roach                    }
2436a6f13a4aSGreg Roach                    break;
24377a6ee1acSGreg Roach                case 'direct-ancestors':
24383d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, false, $maxgen);
2439a6f13a4aSGreg Roach                    break;
24407a6ee1acSGreg Roach                case 'ancestors':
24413d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
2442a6f13a4aSGreg Roach                    break;
24437a6ee1acSGreg Roach                case 'descendants':
2444a6f13a4aSGreg Roach                    $this->list[$id]->generation = 1;
24453d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, false, $maxgen);
2446a6f13a4aSGreg Roach                    break;
24477a6ee1acSGreg Roach                case 'all':
24483d7a8a4cSGreg Roach                    $this->addAncestors($this->list, $id, true, $maxgen);
24493d7a8a4cSGreg Roach                    $this->addDescendancy($this->list, $id, true, $maxgen);
2450a6f13a4aSGreg Roach                    break;
2451a6f13a4aSGreg Roach            }
2452a6f13a4aSGreg Roach        }
2453a6f13a4aSGreg Roach
2454a6f13a4aSGreg Roach        switch ($sortby) {
2455a6f13a4aSGreg Roach            case 'NAME':
2456a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare');
2457a6f13a4aSGreg Roach                break;
2458a6f13a4aSGreg Roach            case 'BIRT:DATE':
2459a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate');
2460a6f13a4aSGreg Roach                break;
2461a6f13a4aSGreg Roach            case 'DEAT:DATE':
2462a6f13a4aSGreg Roach                uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate');
2463a6f13a4aSGreg Roach                break;
2464a6f13a4aSGreg Roach            case 'generation':
246513abd6f3SGreg Roach                $newarray = [];
2466a6f13a4aSGreg Roach                reset($this->list);
2467a6f13a4aSGreg Roach                $genCounter = 1;
2468a6f13a4aSGreg Roach                while (count($newarray) < count($this->list)) {
2469a6f13a4aSGreg Roach                    foreach ($this->list as $key => $value) {
2470a6f13a4aSGreg Roach                        $this->generation = $value->generation;
2471a6f13a4aSGreg Roach                        if ($this->generation == $genCounter) {
247279529c87SGreg Roach                            $newarray[$key]             = new stdClass();
2473a6f13a4aSGreg Roach                            $newarray[$key]->generation = $this->generation;
2474a6f13a4aSGreg Roach                        }
2475a6f13a4aSGreg Roach                    }
2476a6f13a4aSGreg Roach                    $genCounter++;
2477a6f13a4aSGreg Roach                }
2478a6f13a4aSGreg Roach                $this->list = $newarray;
2479a6f13a4aSGreg Roach                break;
2480a6f13a4aSGreg Roach            default:
2481a6f13a4aSGreg Roach                // unsorted
2482a6f13a4aSGreg Roach                break;
2483a6f13a4aSGreg Roach        }
24849b3dd960SGreg Roach        $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes];
2485e8e7866bSGreg Roach        $this->repeat_bytes    = xml_get_current_line_number($this->parser) + 1;
2486a6f13a4aSGreg Roach    }
2487a6f13a4aSGreg Roach
2488a6f13a4aSGreg Roach    /**
248976692c8bSGreg Roach     * XML </ Relatives>
24908ba2e626SGreg Roach     *
24918ba2e626SGreg Roach     * @return void
2492a6f13a4aSGreg Roach     */
2493c1010edaSGreg Roach    private function relativesEndHandler()
2494c1010edaSGreg Roach    {
2495a6f13a4aSGreg Roach        $this->process_repeats--;
2496a6f13a4aSGreg Roach        if ($this->process_repeats > 0) {
2497a6f13a4aSGreg Roach            return;
2498a6f13a4aSGreg Roach        }
2499a6f13a4aSGreg Roach
2500a6f13a4aSGreg Roach        // Check if there is any relatives
2501a6f13a4aSGreg Roach        if (count($this->list) > 0) {
2502a6f13a4aSGreg Roach            $lineoffset = 0;
2503a6f13a4aSGreg Roach            foreach ($this->repeats_stack as $rep) {
2504a6f13a4aSGreg Roach                $lineoffset += $rep[1];
2505a6f13a4aSGreg Roach            }
2506a6f13a4aSGreg Roach            //-- read the xml from the file
2507299d100dSGreg Roach            $lines = file($this->report);
25087a6ee1acSGreg Roach            while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) {
2509a6f13a4aSGreg Roach                $lineoffset--;
2510a6f13a4aSGreg Roach            }
2511a6f13a4aSGreg Roach            $lineoffset++;
2512a6f13a4aSGreg Roach            $reportxml = "<tempdoc>\n";
2513a6f13a4aSGreg Roach            $line_nr   = $lineoffset + $this->repeat_bytes;
2514a6f13a4aSGreg Roach            // Relatives Level counter
2515a6f13a4aSGreg Roach            $count = 1;
2516a6f13a4aSGreg Roach            while (0 < $count) {
25177a6ee1acSGreg Roach                if (strpos($lines[$line_nr], '<Relatives') !== false) {
2518a6f13a4aSGreg Roach                    $count++;
25197a6ee1acSGreg Roach                } elseif (strpos($lines[$line_nr], '</Relatives') !== false) {
2520a6f13a4aSGreg Roach                    $count--;
2521a6f13a4aSGreg Roach                }
2522a6f13a4aSGreg Roach                if (0 < $count) {
2523a6f13a4aSGreg Roach                    $reportxml .= $lines[$line_nr];
2524a6f13a4aSGreg Roach                }
2525a6f13a4aSGreg Roach                $line_nr++;
2526a6f13a4aSGreg Roach            }
2527a6f13a4aSGreg Roach            // No need to drag this
2528a6f13a4aSGreg Roach            unset($lines);
2529a6f13a4aSGreg Roach            $reportxml .= "</tempdoc>\n";
2530a6f13a4aSGreg Roach            // Save original values
25319b3dd960SGreg Roach            $this->parser_stack[] = $this->parser;
2532a6f13a4aSGreg Roach            $oldgedrec            = $this->gedrec;
2533a6f13a4aSGreg Roach
2534a6f13a4aSGreg Roach            $this->list_total   = count($this->list);
2535a6f13a4aSGreg Roach            $this->list_private = 0;
2536a6f13a4aSGreg Roach            foreach ($this->list as $key => $value) {
2537a6f13a4aSGreg Roach                if (isset($value->generation)) {
2538a6f13a4aSGreg Roach                    $this->generation = $value->generation;
2539a6f13a4aSGreg Roach                }
2540299d100dSGreg Roach                $tmp          = GedcomRecord::getInstance($key, $this->tree);
2541299d100dSGreg Roach                $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree));
2542a6f13a4aSGreg Roach
2543a6f13a4aSGreg Roach                $repeat_parser = xml_parser_create();
2544e8e7866bSGreg Roach                $this->parser  = $repeat_parser;
2545a6f13a4aSGreg Roach                xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false);
25461aa04befSGreg Roach
25471aa04befSGreg Roach                xml_set_element_handler(
25481aa04befSGreg Roach                    $repeat_parser,
25491aa04befSGreg Roach                    function ($parser, string $name, array $attrs) {
25501aa04befSGreg Roach                        $this->startElement($parser, $name, $attrs);
25511aa04befSGreg Roach                    },
25521aa04befSGreg Roach                    function ($parser, string $name) {
25531aa04befSGreg Roach                        $this->endElement($parser, $name);
25541aa04befSGreg Roach                    }
25551aa04befSGreg Roach                );
25561aa04befSGreg Roach
25571aa04befSGreg Roach                xml_set_character_data_handler(
25581aa04befSGreg Roach                    $repeat_parser,
25591aa04befSGreg Roach                    function ($parser, $data) {
25601aa04befSGreg Roach                        $this->characterData($parser, $data);
25611aa04befSGreg Roach                    }
25621aa04befSGreg Roach                );
2563a6f13a4aSGreg Roach
2564a6f13a4aSGreg Roach                if (!xml_parse($repeat_parser, $reportxml, true)) {
25657a6ee1acSGreg 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)));
2566a6f13a4aSGreg Roach                }
2567a6f13a4aSGreg Roach                xml_parser_free($repeat_parser);
2568a6f13a4aSGreg Roach            }
2569a6f13a4aSGreg Roach            // Clean up the list array
257013abd6f3SGreg Roach            $this->list   = [];
2571e8e7866bSGreg Roach            $this->parser = array_pop($this->parser_stack);
2572a6f13a4aSGreg Roach            $this->gedrec = $oldgedrec;
2573a6f13a4aSGreg Roach        }
257465e02381SGreg Roach        [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack);
2575a6f13a4aSGreg Roach    }
2576a6f13a4aSGreg Roach
2577a6f13a4aSGreg Roach    /**
2578a6f13a4aSGreg Roach     * XML <Generation /> element handler
2579a6f13a4aSGreg Roach     * Prints the number of generations
25808ba2e626SGreg Roach     *
25818ba2e626SGreg Roach     * @return void
2582a6f13a4aSGreg Roach     */
2583c1010edaSGreg Roach    private function generationStartHandler()
2584c1010edaSGreg Roach    {
2585589feda3SGreg Roach        $this->current_element->addText((string) $this->generation);
2586a6f13a4aSGreg Roach    }
2587a6f13a4aSGreg Roach
2588a6f13a4aSGreg Roach    /**
2589a6f13a4aSGreg Roach     * XML <NewPage /> element handler
2590a6f13a4aSGreg Roach     * Has to be placed in an element (header, pageheader, body or footer)
25918ba2e626SGreg Roach     *
25928ba2e626SGreg Roach     * @return void
2593a6f13a4aSGreg Roach     */
2594c1010edaSGreg Roach    private function newPageStartHandler()
2595c1010edaSGreg Roach    {
25967a6ee1acSGreg Roach        $temp = 'addpage';
2597e8e7866bSGreg Roach        $this->wt_report->addElement($temp);
2598a6f13a4aSGreg Roach    }
2599a6f13a4aSGreg Roach
2600a6f13a4aSGreg Roach    /**
260176692c8bSGreg Roach     * XML <html>
260276692c8bSGreg Roach     *
2603a6f13a4aSGreg Roach     * @param string   $tag   HTML tag name
2604c0fe75acSGreg Roach     * @param string[] $attrs an array of key value pairs for the attributes
26058ba2e626SGreg Roach     *
26068ba2e626SGreg Roach     * @return void
2607a6f13a4aSGreg Roach     */
2608c0fe75acSGreg Roach    private function htmlStartHandler(string $tag, array $attrs)
2609c1010edaSGreg Roach    {
26107a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2611a6f13a4aSGreg Roach            return;
2612a6f13a4aSGreg Roach        }
26139b3dd960SGreg Roach        $this->wt_report_stack[] = $this->wt_report;
2614e8e7866bSGreg Roach        $this->wt_report         = $this->report_root->createHTML($tag, $attrs);
2615e8e7866bSGreg Roach        $this->current_element   = $this->wt_report;
2616a6f13a4aSGreg Roach
26179b3dd960SGreg Roach        $this->print_data_stack[] = $this->print_data;
2618a6f13a4aSGreg Roach        $this->print_data         = true;
2619a6f13a4aSGreg Roach    }
2620a6f13a4aSGreg Roach
2621a6f13a4aSGreg Roach    /**
262276692c8bSGreg Roach     * XML </html>
262376692c8bSGreg Roach     *
2624a6f13a4aSGreg Roach     * @param string $tag
26258ba2e626SGreg Roach     *
26268ba2e626SGreg Roach     * @return void
2627a6f13a4aSGreg Roach     */
2628c1010edaSGreg Roach    private function htmlEndHandler($tag)
2629c1010edaSGreg Roach    {
26307a6ee1acSGreg Roach        if ($tag === 'tempdoc') {
2631a6f13a4aSGreg Roach            return;
2632a6f13a4aSGreg Roach        }
2633a6f13a4aSGreg Roach
2634a6f13a4aSGreg Roach        $this->print_data      = array_pop($this->print_data_stack);
2635e8e7866bSGreg Roach        $this->current_element = $this->wt_report;
2636e8e7866bSGreg Roach        $this->wt_report       = array_pop($this->wt_report_stack);
26378f038c36SRico Sonntag        if ($this->wt_report !== null) {
2638e8e7866bSGreg Roach            $this->wt_report->addElement($this->current_element);
2639a6f13a4aSGreg Roach        } else {
2640e8e7866bSGreg Roach            $this->wt_report = $this->current_element;
2641a6f13a4aSGreg Roach        }
2642a6f13a4aSGreg Roach    }
2643a6f13a4aSGreg Roach
2644a6f13a4aSGreg Roach    /**
2645a6f13a4aSGreg Roach     * Handle <Input>
26468ba2e626SGreg Roach     *
26478ba2e626SGreg Roach     * @return void
2648a6f13a4aSGreg Roach     */
2649c1010edaSGreg Roach    private function inputStartHandler()
2650c1010edaSGreg Roach    {
2651a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2652a6f13a4aSGreg Roach    }
2653a6f13a4aSGreg Roach
2654a6f13a4aSGreg Roach    /**
2655a6f13a4aSGreg Roach     * Handle </Input>
26568ba2e626SGreg Roach     *
26578ba2e626SGreg Roach     * @return void
2658a6f13a4aSGreg Roach     */
2659c1010edaSGreg Roach    private function inputEndHandler()
2660c1010edaSGreg Roach    {
2661a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2662a6f13a4aSGreg Roach    }
2663a6f13a4aSGreg Roach
2664a6f13a4aSGreg Roach    /**
2665a6f13a4aSGreg Roach     * Handle <Report>
26668ba2e626SGreg Roach     *
26678ba2e626SGreg Roach     * @return void
2668a6f13a4aSGreg Roach     */
2669c1010edaSGreg Roach    private function reportStartHandler()
2670c1010edaSGreg Roach    {
2671a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlStartHandler() being called
2672a6f13a4aSGreg Roach    }
2673a6f13a4aSGreg Roach
2674a6f13a4aSGreg Roach    /**
2675a6f13a4aSGreg Roach     * Handle </Report>
26768ba2e626SGreg Roach     *
26778ba2e626SGreg Roach     * @return void
2678a6f13a4aSGreg Roach     */
2679c1010edaSGreg Roach    private function reportEndHandler()
2680c1010edaSGreg Roach    {
2681a6f13a4aSGreg Roach        // Dummy function, to prevent the default HtmlEndHandler() being called
2682a6f13a4aSGreg Roach    }
2683a6f13a4aSGreg Roach
2684a6f13a4aSGreg Roach    /**
268576692c8bSGreg Roach     * XML </titleEndHandler>
26868ba2e626SGreg Roach     *
26878ba2e626SGreg Roach     * @return void
2688a6f13a4aSGreg Roach     */
2689c1010edaSGreg Roach    private function titleEndHandler()
2690c1010edaSGreg Roach    {
26912836aa05SGreg Roach        $this->report_root->addTitle($this->text);
2692a6f13a4aSGreg Roach    }
2693a6f13a4aSGreg Roach
2694a6f13a4aSGreg Roach    /**
269576692c8bSGreg Roach     * XML </descriptionEndHandler>
26968ba2e626SGreg Roach     *
26978ba2e626SGreg Roach     * @return void
2698a6f13a4aSGreg Roach     */
2699c1010edaSGreg Roach    private function descriptionEndHandler()
2700c1010edaSGreg Roach    {
27012836aa05SGreg Roach        $this->report_root->addDescription($this->text);
2702a6f13a4aSGreg Roach    }
2703729ce104SGreg Roach
2704729ce104SGreg Roach    /**
270576692c8bSGreg Roach     * Create a list of all descendants.
270676692c8bSGreg Roach     *
2707729ce104SGreg Roach     * @param string[] $list
2708729ce104SGreg Roach     * @param string   $pid
2709729ce104SGreg Roach     * @param bool     $parents
2710729ce104SGreg Roach     * @param int      $generations
27118ba2e626SGreg Roach     *
27128ba2e626SGreg Roach     * @return void
2713729ce104SGreg Roach     */
2714c1010edaSGreg Roach    private function addDescendancy(&$list, $pid, $parents = false, $generations = -1)
2715c1010edaSGreg Roach    {
2716299d100dSGreg Roach        $person = Individual::getInstance($pid, $this->tree);
2717729ce104SGreg Roach        if ($person === null) {
2718729ce104SGreg Roach            return;
2719729ce104SGreg Roach        }
2720729ce104SGreg Roach        if (!isset($list[$pid])) {
2721729ce104SGreg Roach            $list[$pid] = $person;
2722729ce104SGreg Roach        }
2723729ce104SGreg Roach        if (!isset($list[$pid]->generation)) {
2724729ce104SGreg Roach            $list[$pid]->generation = 0;
2725729ce104SGreg Roach        }
2726729ce104SGreg Roach        foreach ($person->getSpouseFamilies() as $family) {
2727729ce104SGreg Roach            if ($parents) {
2728729ce104SGreg Roach                $husband = $family->getHusband();
2729729ce104SGreg Roach                $wife    = $family->getWife();
2730729ce104SGreg Roach                if ($husband) {
2731c0935879SGreg Roach                    $list[$husband->xref()] = $husband;
2732729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2733c0935879SGreg Roach                        $list[$husband->xref()]->generation = $list[$pid]->generation - 1;
2734729ce104SGreg Roach                    } else {
2735c0935879SGreg Roach                        $list[$husband->xref()]->generation = 1;
2736729ce104SGreg Roach                    }
2737729ce104SGreg Roach                }
2738729ce104SGreg Roach                if ($wife) {
2739c0935879SGreg Roach                    $list[$wife->xref()] = $wife;
2740729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2741c0935879SGreg Roach                        $list[$wife->xref()]->generation = $list[$pid]->generation - 1;
2742729ce104SGreg Roach                    } else {
2743c0935879SGreg Roach                        $list[$wife->xref()]->generation = 1;
2744729ce104SGreg Roach                    }
2745729ce104SGreg Roach                }
2746729ce104SGreg Roach            }
2747729ce104SGreg Roach            $children = $family->getChildren();
2748729ce104SGreg Roach            foreach ($children as $child) {
2749729ce104SGreg Roach                if ($child) {
2750c0935879SGreg Roach                    $list[$child->xref()] = $child;
2751729ce104SGreg Roach                    if (isset($list[$pid]->generation)) {
2752c0935879SGreg Roach                        $list[$child->xref()]->generation = $list[$pid]->generation + 1;
2753729ce104SGreg Roach                    } else {
2754c0935879SGreg Roach                        $list[$child->xref()]->generation = 2;
2755729ce104SGreg Roach                    }
2756729ce104SGreg Roach                }
2757729ce104SGreg Roach            }
2758729ce104SGreg Roach            if ($generations == -1 || $list[$pid]->generation + 1 < $generations) {
2759729ce104SGreg Roach                foreach ($children as $child) {
2760c0935879SGreg Roach                    $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family
2761729ce104SGreg Roach                }
2762729ce104SGreg Roach            }
2763729ce104SGreg Roach        }
2764729ce104SGreg Roach    }
2765729ce104SGreg Roach
2766729ce104SGreg Roach    /**
276776692c8bSGreg Roach     * Create a list of all ancestors.
276876692c8bSGreg Roach     *
2769729ce104SGreg Roach     * @param string[] $list
2770729ce104SGreg Roach     * @param string   $pid
2771729ce104SGreg Roach     * @param bool     $children
2772729ce104SGreg Roach     * @param int      $generations
27738ba2e626SGreg Roach     *
27748ba2e626SGreg Roach     * @return void
2775729ce104SGreg Roach     */
2776c1010edaSGreg Roach    private function addAncestors(&$list, $pid, $children = false, $generations = -1)
2777c1010edaSGreg Roach    {
277813abd6f3SGreg Roach        $genlist                = [$pid];
2779729ce104SGreg Roach        $list[$pid]->generation = 1;
2780729ce104SGreg Roach        while (count($genlist) > 0) {
2781729ce104SGreg Roach            $id = array_shift($genlist);
2782729ce104SGreg Roach            if (strpos($id, 'empty') === 0) {
2783729ce104SGreg Roach                continue; // id can be something like “empty7”
2784729ce104SGreg Roach            }
2785299d100dSGreg Roach            $person = Individual::getInstance($id, $this->tree);
2786729ce104SGreg Roach            foreach ($person->getChildFamilies() as $family) {
2787729ce104SGreg Roach                $husband = $family->getHusband();
2788729ce104SGreg Roach                $wife    = $family->getWife();
2789729ce104SGreg Roach                if ($husband) {
2790c0935879SGreg Roach                    $list[$husband->xref()]             = $husband;
2791c0935879SGreg Roach                    $list[$husband->xref()]->generation = $list[$id]->generation + 1;
2792729ce104SGreg Roach                }
2793729ce104SGreg Roach                if ($wife) {
2794c0935879SGreg Roach                    $list[$wife->xref()]             = $wife;
2795c0935879SGreg Roach                    $list[$wife->xref()]->generation = $list[$id]->generation + 1;
2796729ce104SGreg Roach                }
2797729ce104SGreg Roach                if ($generations == -1 || $list[$id]->generation + 1 < $generations) {
2798729ce104SGreg Roach                    if ($husband) {
2799c0935879SGreg Roach                        $genlist[] = $husband->xref();
2800729ce104SGreg Roach                    }
2801729ce104SGreg Roach                    if ($wife) {
2802c0935879SGreg Roach                        $genlist[] = $wife->xref();
2803729ce104SGreg Roach                    }
2804729ce104SGreg Roach                }
2805729ce104SGreg Roach                if ($children) {
2806729ce104SGreg Roach                    foreach ($family->getChildren() as $child) {
2807c0935879SGreg Roach                        $list[$child->xref()] = $child;
2808729ce104SGreg Roach                        if (isset($list[$id]->generation)) {
2809c0935879SGreg Roach                            $list[$child->xref()]->generation = $list[$id]->generation;
2810729ce104SGreg Roach                        } else {
2811c0935879SGreg Roach                            $list[$child->xref()]->generation = 1;
2812729ce104SGreg Roach                        }
2813729ce104SGreg Roach                    }
2814729ce104SGreg Roach                }
2815729ce104SGreg Roach            }
2816729ce104SGreg Roach        }
2817729ce104SGreg Roach    }
2818729ce104SGreg Roach
2819729ce104SGreg Roach    /**
2820729ce104SGreg Roach     * get gedcom tag value
2821729ce104SGreg Roach     *
2822729ce104SGreg Roach     * @param string $tag    The tag to find, use : to delineate subtags
2823729ce104SGreg 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
2824729ce104SGreg Roach     * @param string $gedrec The gedcom record to get the value from
2825729ce104SGreg Roach     *
2826729ce104SGreg Roach     * @return string the value of a gedcom tag from the given gedcom record
2827729ce104SGreg Roach     */
28288f53f488SRico Sonntag    private function getGedcomValue($tag, $level, $gedrec): string
2829c1010edaSGreg Roach    {
2830729ce104SGreg Roach        if (empty($gedrec)) {
2831729ce104SGreg Roach            return '';
2832729ce104SGreg Roach        }
2833729ce104SGreg Roach        $tags      = explode(':', $tag);
2834729ce104SGreg Roach        $origlevel = $level;
2835729ce104SGreg Roach        if ($level == 0) {
28363c12f3e5SGreg Roach            $level = $gedrec[0] + 1;
2837729ce104SGreg Roach        }
2838729ce104SGreg Roach
2839729ce104SGreg Roach        $subrec = $gedrec;
2840729ce104SGreg Roach        foreach ($tags as $t) {
2841729ce104SGreg Roach            $lastsubrec = $subrec;
28423d7a8a4cSGreg Roach            $subrec     = Functions::getSubRecord($level, "$level $t", $subrec);
2843729ce104SGreg Roach            if (empty($subrec) && $origlevel == 0) {
2844729ce104SGreg Roach                $level--;
28453d7a8a4cSGreg Roach                $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec);
2846729ce104SGreg Roach            }
2847729ce104SGreg Roach            if (empty($subrec)) {
2848044416d2SGreg Roach                if ($t === 'TITL') {
28493d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec);
2850729ce104SGreg Roach                    if (!empty($subrec)) {
28517a6ee1acSGreg Roach                        $t = 'ABBR';
2852729ce104SGreg Roach                    }
2853729ce104SGreg Roach                }
2854729ce104SGreg Roach                if (empty($subrec)) {
2855729ce104SGreg Roach                    if ($level > 0) {
2856729ce104SGreg Roach                        $level--;
2857729ce104SGreg Roach                    }
28583d7a8a4cSGreg Roach                    $subrec = Functions::getSubRecord($level, "@ $t", $gedrec);
2859729ce104SGreg Roach                    if (empty($subrec)) {
2860729ce104SGreg Roach                        return '';
2861729ce104SGreg Roach                    }
2862729ce104SGreg Roach                }
2863729ce104SGreg Roach            }
2864729ce104SGreg Roach            $level++;
2865729ce104SGreg Roach        }
2866729ce104SGreg Roach        $level--;
2867729ce104SGreg Roach        $ct = preg_match("/$level $t(.*)/", $subrec, $match);
2868729ce104SGreg Roach        if ($ct == 0) {
2869729ce104SGreg Roach            $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match);
2870729ce104SGreg Roach        }
2871729ce104SGreg Roach        if ($ct == 0) {
2872729ce104SGreg Roach            $ct = preg_match("/@ $t (.+)/", $subrec, $match);
2873729ce104SGreg Roach        }
2874729ce104SGreg Roach        if ($ct > 0) {
2875729ce104SGreg Roach            $value = trim($match[1]);
2876044416d2SGreg Roach            if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) {
2877299d100dSGreg Roach                $note = Note::getInstance($match[1], $this->tree);
2878ff166e64SGreg Roach                if ($note instanceof Note) {
2879729ce104SGreg Roach                    $value = $note->getNote();
2880729ce104SGreg Roach                } else {
2881729ce104SGreg Roach                    //-- set the value to the id without the @
2882729ce104SGreg Roach                    $value = $match[1];
2883729ce104SGreg Roach                }
2884729ce104SGreg Roach            }
28857a6ee1acSGreg Roach            if ($level != 0 || $t != 'NOTE') {
28863d7a8a4cSGreg Roach                $value .= Functions::getCont($level + 1, $subrec);
2887729ce104SGreg Roach            }
2888729ce104SGreg Roach
2889729ce104SGreg Roach            return $value;
2890729ce104SGreg Roach        }
2891729ce104SGreg Roach
28927a6ee1acSGreg Roach        return '';
2893729ce104SGreg Roach    }
2894d1286247SGreg Roach
2895d1286247SGreg Roach    /**
2896d1286247SGreg Roach     * Replace variable identifiers with their values.
2897d1286247SGreg Roach     *
2898d1286247SGreg Roach     * @param string $expression An expression such as "$foo == 123"
289982759250SGreg Roach     * @param bool   $quote      Whether to add quotation marks
2900d1286247SGreg Roach     *
2901d1286247SGreg Roach     * @return string
2902d1286247SGreg Roach     */
29038f53f488SRico Sonntag    private function substituteVars($expression, $quote): string
2904c1010edaSGreg Roach    {
2905d1286247SGreg Roach        return preg_replace_callback(
2906d1286247SGreg Roach            '/\$(\w+)/',
290718d7a90dSGreg Roach            function (array $matches) use ($quote): string {
29082118c0e3SGreg Roach                if (isset($this->vars[$matches[1]]['id'])) {
290982759250SGreg Roach                    if ($quote) {
29102118c0e3SGreg Roach                        return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'";
2911b2ce94c6SRico Sonntag                    }
2912b2ce94c6SRico Sonntag
29132118c0e3SGreg Roach                    return $this->vars[$matches[1]]['id'];
291482759250SGreg Roach                }
2915b2ce94c6SRico Sonntag
2916d1286247SGreg Roach                Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1]));
29173d7a8a4cSGreg Roach
2918d1286247SGreg Roach                return '$' . $matches[1];
2919d1286247SGreg Roach            },
2920d1286247SGreg Roach            $expression
2921d1286247SGreg Roach        );
2922d1286247SGreg Roach    }
2923a6f13a4aSGreg Roach}
2924