1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Report; 20 21use DomainException; 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Carbon; 24use Fisharebest\Webtrees\Date; 25use Fisharebest\Webtrees\Family; 26use Fisharebest\Webtrees\Filter; 27use Fisharebest\Webtrees\Functions\Functions; 28use Fisharebest\Webtrees\Gedcom; 29use Fisharebest\Webtrees\GedcomRecord; 30use Fisharebest\Webtrees\GedcomTag; 31use Fisharebest\Webtrees\I18N; 32use Fisharebest\Webtrees\Individual; 33use Fisharebest\Webtrees\Log; 34use Fisharebest\Webtrees\Media; 35use Fisharebest\Webtrees\Note; 36use Fisharebest\Webtrees\Place; 37use Fisharebest\Webtrees\Tree; 38use Illuminate\Database\Capsule\Manager as DB; 39use Illuminate\Database\Query\Builder; 40use Illuminate\Database\Query\Expression; 41use Illuminate\Database\Query\JoinClause; 42use Illuminate\Support\Str; 43use stdClass; 44use Symfony\Component\Cache\Adapter\NullAdapter; 45use Symfony\Component\ExpressionLanguage\ExpressionLanguage; 46 47/** 48 * Class ReportParserGenerate - parse a report.xml file and generate the report. 49 */ 50class ReportParserGenerate extends ReportParserBase 51{ 52 /** @var bool Are we collecting data from <Footnote> elements */ 53 private $process_footnote = true; 54 55 /** @var bool Are we currently outputing data? */ 56 private $print_data = false; 57 58 /** @var bool[] Push-down stack of $print_data */ 59 private $print_data_stack = []; 60 61 /** @var int Are we processing GEDCOM data */ 62 private $process_gedcoms = 0; 63 64 /** @var int Are we processing conditionals */ 65 private $process_ifs = 0; 66 67 /** @var int Are we processing repeats */ 68 private $process_repeats = 0; 69 70 /** @var int Quantity of data to repeat during loops */ 71 private $repeat_bytes = 0; 72 73 /** @var string[] Repeated data when iterating over loops */ 74 private $repeats = []; 75 76 /** @var array[] Nested repeating data */ 77 private $repeats_stack = []; 78 79 /** @var AbstractReport[] Nested repeating data */ 80 private $wt_report_stack = []; 81 82 /** @var resource Nested repeating data */ 83 private $parser; 84 85 /** @var resource[] Nested repeating data */ 86 private $parser_stack = []; 87 88 /** @var string The current GEDCOM record */ 89 private $gedrec = ''; 90 91 /** @var string[] Nested GEDCOM records */ 92 private $gedrec_stack = []; 93 94 /** @var ReportBaseElement The currently processed element */ 95 private $current_element; 96 97 /** @var ReportBaseElement The currently processed element */ 98 private $footnote_element; 99 100 /** @var string The GEDCOM fact currently being processed */ 101 private $fact = ''; 102 103 /** @var string The GEDCOM value currently being processed */ 104 private $desc = ''; 105 106 /** @var string The GEDCOM type currently being processed */ 107 private $type = ''; 108 109 /** @var int The current generational level */ 110 private $generation = 1; 111 112 /** @var array Source data for processing lists */ 113 private $list = []; 114 115 /** @var int Number of items in lists */ 116 private $list_total = 0; 117 118 /** @var int Number of items filtered from lists */ 119 private $list_private = 0; 120 121 /** @var string The filename of the XML report */ 122 protected $report; 123 124 /** @var AbstractReport A factory for creating report elements */ 125 private $report_root; 126 127 /** @var AbstractReport Nested report elements */ 128 private $wt_report; 129 130 /** @var string[][] Variables defined in the report at run-time */ 131 private $vars; 132 133 /** @var Tree The current tree */ 134 private $tree; 135 136 /** 137 * Create a parser for a report 138 * 139 * @param string $report The XML filename 140 * @param AbstractReport $report_root 141 * @param string[][] $vars 142 * @param Tree $tree 143 */ 144 public function __construct(string $report, AbstractReport $report_root, array $vars, Tree $tree) 145 { 146 $this->report = $report; 147 $this->report_root = $report_root; 148 $this->wt_report = $report_root; 149 $this->current_element = new ReportBaseElement(); 150 $this->vars = $vars; 151 $this->tree = $tree; 152 153 parent::__construct($report); 154 } 155 156 /** 157 * XML start element handler 158 * This function is called whenever a starting element is reached 159 * The element handler will be called if found, otherwise it must be HTML 160 * 161 * @param resource $parser the resource handler for the XML parser 162 * @param string $name the name of the XML element parsed 163 * @param string[] $attrs an array of key value pairs for the attributes 164 * 165 * @return void 166 */ 167 protected function startElement($parser, string $name, array $attrs): void 168 { 169 $newattrs = []; 170 171 foreach ($attrs as $key => $value) { 172 if (preg_match("/^\\$(\w+)$/", $value, $match)) { 173 if (isset($this->vars[$match[1]]['id']) && !isset($this->vars[$match[1]]['gedcom'])) { 174 $value = $this->vars[$match[1]]['id']; 175 } 176 } 177 $newattrs[$key] = $value; 178 } 179 $attrs = $newattrs; 180 if ($this->process_footnote && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag')) { 181 $start_method = $name . 'StartHandler'; 182 $end_method = $name . 'EndHandler'; 183 184 if (method_exists($this, $start_method)) { 185 $this->$start_method($attrs); 186 } elseif (!method_exists($this, $end_method)) { 187 $this->htmlStartHandler($name, $attrs); 188 } 189 } 190 } 191 192 /** 193 * XML end element handler 194 * This function is called whenever an ending element is reached 195 * The element handler will be called if found, otherwise it must be HTML 196 * 197 * @param resource $parser the resource handler for the XML parser 198 * @param string $name the name of the XML element parsed 199 * 200 * @return void 201 */ 202 protected function endElement($parser, string $name): void 203 { 204 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')) { 205 $start_method = $name . 'StartHandler'; 206 $end_method = $name . 'EndHandler'; 207 if (method_exists($this, $end_method)) { 208 $this->$end_method(); 209 } elseif (!method_exists($this, $start_method)) { 210 $this->htmlEndHandler($name); 211 } 212 } 213 } 214 215 /** 216 * XML character data handler 217 * 218 * @param resource $parser the resource handler for the XML parser 219 * @param string $data the name of the XML element parsed 220 * 221 * @return void 222 */ 223 protected function characterData($parser, $data): void 224 { 225 if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) { 226 $this->current_element->addText($data); 227 } 228 } 229 230 /** 231 * XML <style> 232 * 233 * @param string[] $attrs an array of key value pairs for the attributes 234 * 235 * @return void 236 */ 237 protected function styleStartHandler(array $attrs): void 238 { 239 if (empty($attrs['name'])) { 240 throw new DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.'); 241 } 242 243 // array Style that will be passed on 244 $s = []; 245 246 // string Name af the style 247 $s['name'] = $attrs['name']; 248 249 // string Name of the DEFAULT font 250 $s['font'] = $this->wt_report->default_font; 251 if (!empty($attrs['font'])) { 252 $s['font'] = $attrs['font']; 253 } 254 255 // int The size of the font in points 256 $s['size'] = $this->wt_report->default_font_size; 257 if (!empty($attrs['size'])) { 258 $s['size'] = (int) $attrs['size']; 259 } // Get it as int to ignore all decimal points or text (if any text then int(0)) 260 261 // string B: bold, I: italic, U: underline, D: line trough, The default value is regular. 262 $s['style'] = ''; 263 if (!empty($attrs['style'])) { 264 $s['style'] = $attrs['style']; 265 } 266 267 $this->wt_report->addStyle($s); 268 } 269 270 /** 271 * XML <Doc> 272 * Sets up the basics of the document proparties 273 * 274 * @param string[] $attrs an array of key value pairs for the attributes 275 * 276 * @return void 277 */ 278 protected function docStartHandler(array $attrs): void 279 { 280 $this->parser = $this->xml_parser; 281 282 // Custom page width 283 if (!empty($attrs['customwidth'])) { 284 $this->wt_report->page_width = (int) $attrs['customwidth']; 285 } // Get it as int to ignore all decimal points or text (if any text then int(0)) 286 // Custom Page height 287 if (!empty($attrs['customheight'])) { 288 $this->wt_report->page_height = (int) $attrs['customheight']; 289 } // Get it as int to ignore all decimal points or text (if any text then int(0)) 290 291 // Left Margin 292 if (isset($attrs['leftmargin'])) { 293 if ($attrs['leftmargin'] === '0') { 294 $this->wt_report->left_margin = 0; 295 } elseif (!empty($attrs['leftmargin'])) { 296 $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)) 297 } 298 } 299 // Right Margin 300 if (isset($attrs['rightmargin'])) { 301 if ($attrs['rightmargin'] === '0') { 302 $this->wt_report->right_margin = 0; 303 } elseif (!empty($attrs['rightmargin'])) { 304 $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)) 305 } 306 } 307 // Top Margin 308 if (isset($attrs['topmargin'])) { 309 if ($attrs['topmargin'] === '0') { 310 $this->wt_report->top_margin = 0; 311 } elseif (!empty($attrs['topmargin'])) { 312 $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)) 313 } 314 } 315 // Bottom Margin 316 if (isset($attrs['bottommargin'])) { 317 if ($attrs['bottommargin'] === '0') { 318 $this->wt_report->bottom_margin = 0; 319 } elseif (!empty($attrs['bottommargin'])) { 320 $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)) 321 } 322 } 323 // Header Margin 324 if (isset($attrs['headermargin'])) { 325 if ($attrs['headermargin'] === '0') { 326 $this->wt_report->header_margin = 0; 327 } elseif (!empty($attrs['headermargin'])) { 328 $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)) 329 } 330 } 331 // Footer Margin 332 if (isset($attrs['footermargin'])) { 333 if ($attrs['footermargin'] === '0') { 334 $this->wt_report->footer_margin = 0; 335 } elseif (!empty($attrs['footermargin'])) { 336 $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)) 337 } 338 } 339 340 // Page Orientation 341 if (!empty($attrs['orientation'])) { 342 if ($attrs['orientation'] === 'landscape') { 343 $this->wt_report->orientation = 'landscape'; 344 } elseif ($attrs['orientation'] === 'portrait') { 345 $this->wt_report->orientation = 'portrait'; 346 } 347 } 348 // Page Size 349 if (!empty($attrs['pageSize'])) { 350 $this->wt_report->page_format = strtoupper($attrs['pageSize']); 351 } 352 353 // Show Generated By... 354 if (isset($attrs['showGeneratedBy'])) { 355 if ($attrs['showGeneratedBy'] === '0') { 356 $this->wt_report->show_generated_by = false; 357 } elseif ($attrs['showGeneratedBy'] === '1') { 358 $this->wt_report->show_generated_by = true; 359 } 360 } 361 362 $this->wt_report->setup(); 363 } 364 365 /** 366 * XML </Doc> 367 * 368 * @return void 369 */ 370 protected function docEndHandler(): void 371 { 372 $this->wt_report->run(); 373 } 374 375 /** 376 * XML <Header> 377 * 378 * @return void 379 */ 380 protected function headerStartHandler(): void 381 { 382 // Clear the Header before any new elements are added 383 $this->wt_report->clearHeader(); 384 $this->wt_report->setProcessing('H'); 385 } 386 387 /** 388 * XML <PageHeader> 389 * 390 * @return void 391 */ 392 protected function pageHeaderStartHandler(): void 393 { 394 $this->print_data_stack[] = $this->print_data; 395 $this->print_data = false; 396 $this->wt_report_stack[] = $this->wt_report; 397 $this->wt_report = $this->report_root->createPageHeader(); 398 } 399 400 /** 401 * XML <pageHeaderEndHandler> 402 * 403 * @return void 404 */ 405 protected function pageHeaderEndHandler(): void 406 { 407 $this->print_data = array_pop($this->print_data_stack); 408 $this->current_element = $this->wt_report; 409 $this->wt_report = array_pop($this->wt_report_stack); 410 $this->wt_report->addElement($this->current_element); 411 } 412 413 /** 414 * XML <bodyStartHandler> 415 * 416 * @return void 417 */ 418 protected function bodyStartHandler(): void 419 { 420 $this->wt_report->setProcessing('B'); 421 } 422 423 /** 424 * XML <footerStartHandler> 425 * 426 * @return void 427 */ 428 protected function footerStartHandler(): void 429 { 430 $this->wt_report->setProcessing('F'); 431 } 432 433 /** 434 * XML <Cell> 435 * 436 * @param string[] $attrs an array of key value pairs for the attributes 437 * 438 * @return void 439 */ 440 protected function cellStartHandler(array $attrs): void 441 { 442 // string The text alignment of the text in this box. 443 $align = ''; 444 if (!empty($attrs['align'])) { 445 $align = $attrs['align']; 446 // RTL supported left/right alignment 447 if ($align === 'rightrtl') { 448 if ($this->wt_report->rtl) { 449 $align = 'left'; 450 } else { 451 $align = 'right'; 452 } 453 } elseif ($align === 'leftrtl') { 454 if ($this->wt_report->rtl) { 455 $align = 'right'; 456 } else { 457 $align = 'left'; 458 } 459 } 460 } 461 462 // string The color to fill the background of this cell 463 $bgcolor = ''; 464 if (!empty($attrs['bgcolor'])) { 465 $bgcolor = $attrs['bgcolor']; 466 } 467 468 // int Whether or not the background should be painted 469 $fill = 1; 470 if (isset($attrs['fill'])) { 471 if ($attrs['fill'] === '0') { 472 $fill = 0; 473 } elseif ($attrs['fill'] === '1') { 474 $fill = 1; 475 } 476 } 477 478 $reseth = true; 479 // boolean if true reset the last cell height (default true) 480 if (isset($attrs['reseth'])) { 481 if ($attrs['reseth'] === '0') { 482 $reseth = false; 483 } elseif ($attrs['reseth'] === '1') { 484 $reseth = true; 485 } 486 } 487 488 // mixed Whether or not a border should be printed around this box 489 $border = 0; 490 if (!empty($attrs['border'])) { 491 $border = $attrs['border']; 492 } 493 // string Border color in HTML code 494 $bocolor = ''; 495 if (!empty($attrs['bocolor'])) { 496 $bocolor = $attrs['bocolor']; 497 } 498 499 // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted. 500 $height = 0; 501 if (!empty($attrs['height'])) { 502 $height = $attrs['height']; 503 } 504 // int Cell width (expressed in points) Setting the width to 0 will make it the width from the current location to the right margin. 505 $width = 0; 506 if (!empty($attrs['width'])) { 507 $width = $attrs['width']; 508 } 509 510 // int Stretch carachter mode 511 $stretch = 0; 512 if (!empty($attrs['stretch'])) { 513 $stretch = (int) $attrs['stretch']; 514 } 515 516 // mixed Position the left corner of this box on the page. The default is the current position. 517 $left = ReportBaseElement::CURRENT_POSITION; 518 if (isset($attrs['left'])) { 519 if ($attrs['left'] === '.') { 520 $left = ReportBaseElement::CURRENT_POSITION; 521 } elseif (!empty($attrs['left'])) { 522 $left = (int) $attrs['left']; 523 } elseif ($attrs['left'] === '0') { 524 $left = 0; 525 } 526 } 527 // mixed Position the top corner of this box on the page. the default is the current position 528 $top = ReportBaseElement::CURRENT_POSITION; 529 if (isset($attrs['top'])) { 530 if ($attrs['top'] === '.') { 531 $top = ReportBaseElement::CURRENT_POSITION; 532 } elseif (!empty($attrs['top'])) { 533 $top = (int) $attrs['top']; 534 } elseif ($attrs['top'] === '0') { 535 $top = 0; 536 } 537 } 538 539 // string The name of the Style that should be used to render the text. 540 $style = ''; 541 if (!empty($attrs['style'])) { 542 $style = $attrs['style']; 543 } 544 545 // string Text color in html code 546 $tcolor = ''; 547 if (!empty($attrs['tcolor'])) { 548 $tcolor = $attrs['tcolor']; 549 } 550 551 // int Indicates where the current position should go after the call. 552 $ln = 0; 553 if (isset($attrs['newline'])) { 554 if (!empty($attrs['newline'])) { 555 $ln = (int) $attrs['newline']; 556 } elseif ($attrs['newline'] === '0') { 557 $ln = 0; 558 } 559 } 560 561 if ($align === 'left') { 562 $align = 'L'; 563 } elseif ($align === 'right') { 564 $align = 'R'; 565 } elseif ($align === 'center') { 566 $align = 'C'; 567 } elseif ($align === 'justify') { 568 $align = 'J'; 569 } 570 571 $this->print_data_stack[] = $this->print_data; 572 $this->print_data = true; 573 574 $this->current_element = $this->report_root->createCell( 575 $width, 576 $height, 577 $border, 578 $align, 579 $bgcolor, 580 $style, 581 $ln, 582 $top, 583 $left, 584 $fill, 585 $stretch, 586 $bocolor, 587 $tcolor, 588 $reseth 589 ); 590 } 591 592 /** 593 * XML </Cell> 594 * 595 * @return void 596 */ 597 protected function cellEndHandler(): void 598 { 599 $this->print_data = array_pop($this->print_data_stack); 600 $this->wt_report->addElement($this->current_element); 601 } 602 603 /** 604 * XML <Now /> element handler 605 * 606 * @return void 607 */ 608 protected function nowStartHandler(): void 609 { 610 $this->current_element->addText(Carbon::now()->local()->isoFormat('LLLL')); 611 } 612 613 /** 614 * XML <PageNum /> element handler 615 * 616 * @return void 617 */ 618 protected function pageNumStartHandler(): void 619 { 620 $this->current_element->addText('#PAGENUM#'); 621 } 622 623 /** 624 * XML <TotalPages /> element handler 625 * 626 * @return void 627 */ 628 protected function totalPagesStartHandler(): void 629 { 630 $this->current_element->addText('{{:ptp:}}'); 631 } 632 633 /** 634 * Called at the start of an element. 635 * 636 * @param string[] $attrs an array of key value pairs for the attributes 637 * 638 * @return void 639 */ 640 protected function gedcomStartHandler(array $attrs): void 641 { 642 if ($this->process_gedcoms > 0) { 643 $this->process_gedcoms++; 644 645 return; 646 } 647 648 $tag = $attrs['id']; 649 $tag = str_replace('@fact', $this->fact, $tag); 650 $tags = explode(':', $tag); 651 $newgedrec = ''; 652 if (count($tags) < 2) { 653 $tmp = GedcomRecord::getInstance($attrs['id'], $this->tree); 654 $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : ''; 655 } 656 if (empty($newgedrec)) { 657 $tgedrec = $this->gedrec; 658 $newgedrec = ''; 659 foreach ($tags as $tag) { 660 if (preg_match('/\$(.+)/', $tag, $match)) { 661 if (isset($this->vars[$match[1]]['gedcom'])) { 662 $newgedrec = $this->vars[$match[1]]['gedcom']; 663 } else { 664 $tmp = GedcomRecord::getInstance($match[1], $this->tree); 665 $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : ''; 666 } 667 } else { 668 if (preg_match('/@(.+)/', $tag, $match)) { 669 $gmatch = []; 670 if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) { 671 $tmp = GedcomRecord::getInstance($gmatch[1], $this->tree); 672 $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : ''; 673 $tgedrec = $newgedrec; 674 } else { 675 $newgedrec = ''; 676 break; 677 } 678 } else { 679 $temp = explode(' ', trim($tgedrec)); 680 $level = 1 + (int) $temp[0]; 681 $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec); 682 $tgedrec = $newgedrec; 683 } 684 } 685 } 686 } 687 if (!empty($newgedrec)) { 688 $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc]; 689 $this->gedrec = $newgedrec; 690 if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) { 691 $this->fact = $match[2]; 692 $this->desc = trim($match[3]); 693 } 694 } else { 695 $this->process_gedcoms++; 696 } 697 } 698 699 /** 700 * Called at the end of an element. 701 * 702 * @return void 703 */ 704 protected function gedcomEndHandler(): void 705 { 706 if ($this->process_gedcoms > 0) { 707 $this->process_gedcoms--; 708 } else { 709 [$this->gedrec, $this->fact, $this->desc] = array_pop($this->gedrec_stack); 710 } 711 } 712 713 /** 714 * XML <textBoxStartHandler> 715 * 716 * @param string[] $attrs an array of key value pairs for the attributes 717 * 718 * @return void 719 */ 720 protected function textBoxStartHandler(array $attrs): void 721 { 722 // string Background color code 723 $bgcolor = ''; 724 if (!empty($attrs['bgcolor'])) { 725 $bgcolor = $attrs['bgcolor']; 726 } 727 728 // boolean Wether or not fill the background color 729 $fill = true; 730 if (isset($attrs['fill'])) { 731 if ($attrs['fill'] === '0') { 732 $fill = false; 733 } elseif ($attrs['fill'] === '1') { 734 $fill = true; 735 } 736 } 737 738 // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0 739 $border = false; 740 if (isset($attrs['border'])) { 741 if ($attrs['border'] === '1') { 742 $border = true; 743 } elseif ($attrs['border'] === '0') { 744 $border = false; 745 } 746 } 747 748 // int The starting height of this cell. If the text wraps the height will automatically be adjusted 749 $height = 0; 750 if (!empty($attrs['height'])) { 751 $height = (int) $attrs['height']; 752 } 753 // int Setting the width to 0 will make it the width from the current location to the margin 754 $width = 0; 755 if (!empty($attrs['width'])) { 756 $width = (int) $attrs['width']; 757 } 758 759 // mixed Position the left corner of this box on the page. The default is the current position. 760 $left = ReportBaseElement::CURRENT_POSITION; 761 if (isset($attrs['left'])) { 762 if ($attrs['left'] === '.') { 763 $left = ReportBaseElement::CURRENT_POSITION; 764 } elseif (!empty($attrs['left'])) { 765 $left = (int) $attrs['left']; 766 } elseif ($attrs['left'] === '0') { 767 $left = 0; 768 } 769 } 770 // mixed Position the top corner of this box on the page. the default is the current position 771 $top = ReportBaseElement::CURRENT_POSITION; 772 if (isset($attrs['top'])) { 773 if ($attrs['top'] === '.') { 774 $top = ReportBaseElement::CURRENT_POSITION; 775 } elseif (!empty($attrs['top'])) { 776 $top = (int) $attrs['top']; 777 } elseif ($attrs['top'] === '0') { 778 $top = 0; 779 } 780 } 781 // 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 782 $newline = false; 783 if (isset($attrs['newline'])) { 784 if ($attrs['newline'] === '1') { 785 $newline = true; 786 } elseif ($attrs['newline'] === '0') { 787 $newline = false; 788 } 789 } 790 // boolean 791 $pagecheck = true; 792 if (isset($attrs['pagecheck'])) { 793 if ($attrs['pagecheck'] === '0') { 794 $pagecheck = false; 795 } elseif ($attrs['pagecheck'] === '1') { 796 $pagecheck = true; 797 } 798 } 799 // boolean Cell padding 800 $padding = true; 801 if (isset($attrs['padding'])) { 802 if ($attrs['padding'] === '0') { 803 $padding = false; 804 } elseif ($attrs['padding'] === '1') { 805 $padding = true; 806 } 807 } 808 // boolean Reset this box Height 809 $reseth = false; 810 if (isset($attrs['reseth'])) { 811 if ($attrs['reseth'] === '1') { 812 $reseth = true; 813 } elseif ($attrs['reseth'] === '0') { 814 $reseth = false; 815 } 816 } 817 818 // string Style of rendering 819 $style = ''; 820 821 $this->print_data_stack[] = $this->print_data; 822 $this->print_data = false; 823 824 $this->wt_report_stack[] = $this->wt_report; 825 $this->wt_report = $this->report_root->createTextBox( 826 $width, 827 $height, 828 $border, 829 $bgcolor, 830 $newline, 831 $left, 832 $top, 833 $pagecheck, 834 $style, 835 $fill, 836 $padding, 837 $reseth 838 ); 839 } 840 841 /** 842 * XML <textBoxEndHandler> 843 * 844 * @return void 845 */ 846 protected function textBoxEndHandler(): void 847 { 848 $this->print_data = array_pop($this->print_data_stack); 849 $this->current_element = $this->wt_report; 850 $this->wt_report = array_pop($this->wt_report_stack); 851 $this->wt_report->addElement($this->current_element); 852 } 853 854 /** 855 * XLM <Text>. 856 * 857 * @param string[] $attrs an array of key value pairs for the attributes 858 * 859 * @return void 860 */ 861 protected function textStartHandler(array $attrs): void 862 { 863 $this->print_data_stack[] = $this->print_data; 864 $this->print_data = true; 865 866 // string The name of the Style that should be used to render the text. 867 $style = ''; 868 if (!empty($attrs['style'])) { 869 $style = $attrs['style']; 870 } 871 872 // string The color of the text - Keep the black color as default 873 $color = ''; 874 if (!empty($attrs['color'])) { 875 $color = $attrs['color']; 876 } 877 878 $this->current_element = $this->report_root->createText($style, $color); 879 } 880 881 /** 882 * XML </Text> 883 * 884 * @return void 885 */ 886 protected function textEndHandler(): void 887 { 888 $this->print_data = array_pop($this->print_data_stack); 889 $this->wt_report->addElement($this->current_element); 890 } 891 892 /** 893 * XML <GetPersonName/> 894 * Get the name 895 * 1. id is empty - current GEDCOM record 896 * 2. id is set with a record id 897 * 898 * @param string[] $attrs an array of key value pairs for the attributes 899 * 900 * @return void 901 */ 902 protected function getPersonNameStartHandler(array $attrs): void 903 { 904 $id = ''; 905 $match = []; 906 if (empty($attrs['id'])) { 907 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 908 $id = $match[1]; 909 } 910 } else { 911 if (preg_match('/\$(.+)/', $attrs['id'], $match)) { 912 if (isset($this->vars[$match[1]]['id'])) { 913 $id = $this->vars[$match[1]]['id']; 914 } 915 } else { 916 if (preg_match('/@(.+)/', $attrs['id'], $match)) { 917 $gmatch = []; 918 if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) { 919 $id = $gmatch[1]; 920 } 921 } else { 922 $id = $attrs['id']; 923 } 924 } 925 } 926 if (!empty($id)) { 927 $record = GedcomRecord::getInstance($id, $this->tree); 928 if ($record === null) { 929 return; 930 } 931 if (!$record->canShowName()) { 932 $this->current_element->addText(I18N::translate('Private')); 933 } else { 934 $name = $record->fullName(); 935 $name = preg_replace( 936 [ 937 '/<span class="starredname">/', 938 '/<\/span><\/span>/', 939 '/<\/span>/', 940 ], 941 [ 942 '«', 943 '', 944 '»', 945 ], 946 $name 947 ); 948 $name = strip_tags($name); 949 if (!empty($attrs['truncate'])) { 950 $name = Str::limit($name, $attrs['truncate'], I18N::translate('…')); 951 } else { 952 $addname = $record->alternateName(); 953 $addname = preg_replace( 954 [ 955 '/<span class="starredname">/', 956 '/<\/span><\/span>/', 957 '/<\/span>/', 958 ], 959 [ 960 '«', 961 '', 962 '»', 963 ], 964 $addname 965 ); 966 $addname = strip_tags($addname); 967 if (!empty($addname)) { 968 $name .= ' ' . $addname; 969 } 970 } 971 $this->current_element->addText(trim($name)); 972 } 973 } 974 } 975 976 /** 977 * XML <GedcomValue/> 978 * 979 * @param string[] $attrs an array of key value pairs for the attributes 980 * 981 * @return void 982 */ 983 protected function gedcomValueStartHandler(array $attrs): void 984 { 985 $id = ''; 986 $match = []; 987 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 988 $id = $match[1]; 989 } 990 991 if (isset($attrs['newline']) && $attrs['newline'] === '1') { 992 $useBreak = '1'; 993 } else { 994 $useBreak = '0'; 995 } 996 997 $tag = $attrs['tag']; 998 if (!empty($tag)) { 999 if ($tag === '@desc') { 1000 $value = $this->desc; 1001 $value = trim($value); 1002 $this->current_element->addText($value); 1003 } 1004 if ($tag === '@id') { 1005 $this->current_element->addText($id); 1006 } else { 1007 $tag = str_replace('@fact', $this->fact, $tag); 1008 if (empty($attrs['level'])) { 1009 $temp = explode(' ', trim($this->gedrec)); 1010 $level = $temp[0]; 1011 if ($level == 0) { 1012 $level++; 1013 } 1014 } else { 1015 $level = $attrs['level']; 1016 } 1017 $tags = preg_split('/[: ]/', $tag); 1018 $value = $this->getGedcomValue($tag, $level, $this->gedrec); 1019 switch (end($tags)) { 1020 case 'DATE': 1021 $tmp = new Date($value); 1022 $value = $tmp->display(); 1023 break; 1024 case 'PLAC': 1025 $tmp = new Place($value, $this->tree); 1026 $value = $tmp->shortName(); 1027 break; 1028 } 1029 if ($useBreak === '1') { 1030 // Insert <br> when multiple dates exist. 1031 // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages 1032 $value = str_replace('(', '<br>(', $value); 1033 $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value); 1034 $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value); 1035 if (substr($value, 0, 4) === '<br>') { 1036 $value = substr($value, 4); 1037 } 1038 } 1039 $tmp = explode(':', $tag); 1040 if (in_array(end($tmp), [ 1041 'NOTE', 1042 'TEXT', 1043 ])) { 1044 $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText() 1045 } 1046 1047 if (!empty($attrs['truncate'])) { 1048 $value = strip_tags($value); 1049 $value = Str::limit($value, $attrs['truncate'], I18N::translate('…')); 1050 } 1051 $this->current_element->addText($value); 1052 } 1053 } 1054 } 1055 1056 /** 1057 * XML <RepeatTag> 1058 * 1059 * @param string[] $attrs an array of key value pairs for the attributes 1060 * 1061 * @return void 1062 */ 1063 protected function repeatTagStartHandler(array $attrs): void 1064 { 1065 $this->process_repeats++; 1066 if ($this->process_repeats > 1) { 1067 return; 1068 } 1069 1070 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 1071 $this->repeats = []; 1072 $this->repeat_bytes = xml_get_current_line_number($this->parser); 1073 1074 $tag = $attrs['tag'] ?? ''; 1075 if (!empty($tag)) { 1076 if ($tag === '@desc') { 1077 $value = $this->desc; 1078 $value = trim($value); 1079 $this->current_element->addText($value); 1080 } else { 1081 $tag = str_replace('@fact', $this->fact, $tag); 1082 $tags = explode(':', $tag); 1083 $temp = explode(' ', trim($this->gedrec)); 1084 $level = $temp[0]; 1085 if ($level == 0) { 1086 $level++; 1087 } 1088 $subrec = $this->gedrec; 1089 $t = $tag; 1090 $count = count($tags); 1091 $i = 0; 1092 while ($i < $count) { 1093 $t = $tags[$i]; 1094 if (!empty($t)) { 1095 if ($i < ($count - 1)) { 1096 $subrec = Functions::getSubRecord($level, "$level $t", $subrec); 1097 if (empty($subrec)) { 1098 $level--; 1099 $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec); 1100 if (empty($subrec)) { 1101 return; 1102 } 1103 } 1104 } 1105 $level++; 1106 } 1107 $i++; 1108 } 1109 $level--; 1110 $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER); 1111 $i = 0; 1112 while ($i < $count) { 1113 $i++; 1114 // Privacy check - is this a link, and are we allowed to view the linked object? 1115 $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i); 1116 if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) { 1117 $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree); 1118 if ($linked_object && !$linked_object->canShow()) { 1119 continue; 1120 } 1121 } 1122 $this->repeats[] = $subrecord; 1123 } 1124 } 1125 } 1126 } 1127 1128 /** 1129 * XML </ RepeatTag> 1130 * 1131 * @return void 1132 */ 1133 protected function repeatTagEndHandler(): void 1134 { 1135 $this->process_repeats--; 1136 if ($this->process_repeats > 0) { 1137 return; 1138 } 1139 1140 // Check if there is anything to repeat 1141 if (count($this->repeats) > 0) { 1142 // No need to load them if not used... 1143 1144 $lineoffset = 0; 1145 foreach ($this->repeats_stack as $rep) { 1146 $lineoffset += $rep[1]; 1147 } 1148 //-- read the xml from the file 1149 $lines = file($this->report); 1150 while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) { 1151 $lineoffset--; 1152 } 1153 $lineoffset++; 1154 $reportxml = "<tempdoc>\n"; 1155 $line_nr = $lineoffset + $this->repeat_bytes; 1156 // RepeatTag Level counter 1157 $count = 1; 1158 while (0 < $count) { 1159 if (strstr($lines[$line_nr], '<RepeatTag') !== false) { 1160 $count++; 1161 } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) { 1162 $count--; 1163 } 1164 if (0 < $count) { 1165 $reportxml .= $lines[$line_nr]; 1166 } 1167 $line_nr++; 1168 } 1169 // No need to drag this 1170 unset($lines); 1171 $reportxml .= "</tempdoc>\n"; 1172 // Save original values 1173 $this->parser_stack[] = $this->parser; 1174 $oldgedrec = $this->gedrec; 1175 foreach ($this->repeats as $gedrec) { 1176 $this->gedrec = $gedrec; 1177 $repeat_parser = xml_parser_create(); 1178 $this->parser = $repeat_parser; 1179 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 1180 1181 xml_set_element_handler( 1182 $repeat_parser, 1183 function ($parser, string $name, array $attrs): void { 1184 $this->startElement($parser, $name, $attrs); 1185 }, 1186 function ($parser, string $name): void { 1187 $this->endElement($parser, $name); 1188 } 1189 ); 1190 1191 xml_set_character_data_handler( 1192 $repeat_parser, 1193 function ($parser, string $data): void { 1194 $this->characterData($parser, $data); 1195 } 1196 ); 1197 1198 if (!xml_parse($repeat_parser, $reportxml, true)) { 1199 throw new DomainException(sprintf( 1200 'RepeatTagEHandler XML error: %s at line %d', 1201 xml_error_string(xml_get_error_code($repeat_parser)), 1202 xml_get_current_line_number($repeat_parser) 1203 )); 1204 } 1205 xml_parser_free($repeat_parser); 1206 } 1207 // Restore original values 1208 $this->gedrec = $oldgedrec; 1209 $this->parser = array_pop($this->parser_stack); 1210 } 1211 [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); 1212 } 1213 1214 /** 1215 * Variable lookup 1216 * Retrieve predefined variables : 1217 * @ desc GEDCOM fact description, example: 1218 * 1 EVEN This is a description 1219 * @ fact GEDCOM fact tag, such as BIRT, DEAT etc. 1220 * $ I18N::translate('....') 1221 * $ language_settings[] 1222 * 1223 * @param string[] $attrs an array of key value pairs for the attributes 1224 * 1225 * @return void 1226 */ 1227 protected function varStartHandler(array $attrs): void 1228 { 1229 if (empty($attrs['var'])) { 1230 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)); 1231 } 1232 1233 $var = $attrs['var']; 1234 // SetVar element preset variables 1235 if (!empty($this->vars[$var]['id'])) { 1236 $var = $this->vars[$var]['id']; 1237 } else { 1238 $tfact = $this->fact; 1239 if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') { 1240 // Use : 1241 // n TYPE This text if string 1242 $tfact = $this->type; 1243 } 1244 $var = str_replace([ 1245 '@fact', 1246 '@desc', 1247 ], [ 1248 GedcomTag::getLabel($tfact), 1249 $this->desc, 1250 ], $var); 1251 if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) { 1252 $var = I18N::number((int) $match[1]); 1253 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) { 1254 $var = I18N::translate($match[1]); 1255 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) { 1256 $var = I18N::translateContext($match[1], $match[2]); 1257 } 1258 } 1259 // Check if variable is set as a date and reformat the date 1260 if (isset($attrs['date'])) { 1261 if ($attrs['date'] === '1') { 1262 $g = new Date($var); 1263 $var = $g->display(); 1264 } 1265 } 1266 $this->current_element->addText($var); 1267 $this->text = $var; // Used for title/descriptio 1268 } 1269 1270 /** 1271 * XML <Facts> 1272 * 1273 * @param string[] $attrs an array of key value pairs for the attributes 1274 * 1275 * @return void 1276 */ 1277 protected function factsStartHandler(array $attrs): void 1278 { 1279 $this->process_repeats++; 1280 if ($this->process_repeats > 1) { 1281 return; 1282 } 1283 1284 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 1285 $this->repeats = []; 1286 $this->repeat_bytes = xml_get_current_line_number($this->parser); 1287 1288 $id = ''; 1289 $match = []; 1290 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1291 $id = $match[1]; 1292 } 1293 $tag = ''; 1294 if (isset($attrs['ignore'])) { 1295 $tag .= $attrs['ignore']; 1296 } 1297 if (preg_match('/\$(.+)/', $tag, $match)) { 1298 $tag = $this->vars[$match[1]]['id']; 1299 } 1300 1301 $record = GedcomRecord::getInstance($id, $this->tree); 1302 if (empty($attrs['diff']) && !empty($id)) { 1303 $facts = $record->facts([], true); 1304 $this->repeats = []; 1305 $nonfacts = explode(',', $tag); 1306 foreach ($facts as $fact) { 1307 if (!in_array($fact->getTag(), $nonfacts, true)) { 1308 $this->repeats[] = $fact->gedcom(); 1309 } 1310 } 1311 } else { 1312 foreach ($record->facts() as $fact) { 1313 if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && $fact->getTag() !== 'CHAN') { 1314 $this->repeats[] = $fact->gedcom(); 1315 } 1316 } 1317 } 1318 } 1319 1320 /** 1321 * XML </Facts> 1322 * 1323 * @return void 1324 */ 1325 protected function factsEndHandler(): void 1326 { 1327 $this->process_repeats--; 1328 if ($this->process_repeats > 0) { 1329 return; 1330 } 1331 1332 // Check if there is anything to repeat 1333 if (count($this->repeats) > 0) { 1334 $line = xml_get_current_line_number($this->parser) - 1; 1335 $lineoffset = 0; 1336 foreach ($this->repeats_stack as $rep) { 1337 $lineoffset += $rep[1]; 1338 } 1339 1340 //-- read the xml from the file 1341 $lines = file($this->report); 1342 while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) { 1343 $lineoffset--; 1344 } 1345 $lineoffset++; 1346 $reportxml = "<tempdoc>\n"; 1347 $i = $line + $lineoffset; 1348 $line_nr = $this->repeat_bytes + $lineoffset; 1349 while ($line_nr < $i) { 1350 $reportxml .= $lines[$line_nr]; 1351 $line_nr++; 1352 } 1353 // No need to drag this 1354 unset($lines); 1355 $reportxml .= "</tempdoc>\n"; 1356 // Save original values 1357 $this->parser_stack[] = $this->parser; 1358 $oldgedrec = $this->gedrec; 1359 $count = count($this->repeats); 1360 $i = 0; 1361 while ($i < $count) { 1362 $this->gedrec = $this->repeats[$i]; 1363 $this->fact = ''; 1364 $this->desc = ''; 1365 if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) { 1366 $this->fact = $match[1]; 1367 if ($this->fact === 'EVEN' || $this->fact === 'FACT') { 1368 $tmatch = []; 1369 if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) { 1370 $this->type = trim($tmatch[1]); 1371 } else { 1372 $this->type = ' '; 1373 } 1374 } 1375 $this->desc = trim($match[2]); 1376 $this->desc .= Functions::getCont(2, $this->gedrec); 1377 } 1378 $repeat_parser = xml_parser_create(); 1379 $this->parser = $repeat_parser; 1380 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 1381 1382 xml_set_element_handler( 1383 $repeat_parser, 1384 function ($parser, string $name, array $attrs): void { 1385 $this->startElement($parser, $name, $attrs); 1386 }, 1387 function ($parser, string $name): void { 1388 $this->endElement($parser, $name); 1389 } 1390 ); 1391 1392 xml_set_character_data_handler( 1393 $repeat_parser, 1394 function ($parser, string $data): void { 1395 $this->characterData($parser, $data); 1396 } 1397 ); 1398 1399 if (!xml_parse($repeat_parser, $reportxml, true)) { 1400 throw new DomainException(sprintf( 1401 'FactsEHandler XML error: %s at line %d', 1402 xml_error_string(xml_get_error_code($repeat_parser)), 1403 xml_get_current_line_number($repeat_parser) 1404 )); 1405 } 1406 xml_parser_free($repeat_parser); 1407 $i++; 1408 } 1409 // Restore original values 1410 $this->parser = array_pop($this->parser_stack); 1411 $this->gedrec = $oldgedrec; 1412 } 1413 [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); 1414 } 1415 1416 /** 1417 * Setting upp or changing variables in the XML 1418 * The XML variable name and value is stored in $this->vars 1419 * 1420 * @param string[] $attrs an array of key value pairs for the attributes 1421 * 1422 * @return void 1423 */ 1424 protected function setVarStartHandler(array $attrs): void 1425 { 1426 if (empty($attrs['name'])) { 1427 throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file'); 1428 } 1429 1430 $name = $attrs['name']; 1431 $value = $attrs['value']; 1432 $match = []; 1433 // Current GEDCOM record strings 1434 if ($value === '@ID') { 1435 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1436 $value = $match[1]; 1437 } 1438 } elseif ($value === '@fact') { 1439 $value = $this->fact; 1440 } elseif ($value === '@desc') { 1441 $value = $this->desc; 1442 } elseif ($value === '@generation') { 1443 $value = (string) $this->generation; 1444 } elseif (preg_match("/@(\w+)/", $value, $match)) { 1445 $gmatch = []; 1446 if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) { 1447 $value = str_replace('@', '', trim($gmatch[1])); 1448 } 1449 } 1450 if (preg_match("/\\$(\w+)/", $name, $match)) { 1451 $name = $this->vars["'" . $match[1] . "'"]['id']; 1452 } 1453 $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER); 1454 $i = 0; 1455 while ($i < $count) { 1456 $t = $this->vars[$match[$i][1]]['id']; 1457 $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1); 1458 $i++; 1459 } 1460 if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) { 1461 $value = I18N::number((int) $match[1]); 1462 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { 1463 $value = I18N::translate($match[1]); 1464 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { 1465 $value = I18N::translateContext($match[1], $match[2]); 1466 } 1467 1468 // Arithmetic functions 1469 if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) { 1470 // Create an expression language with the functions used by our reports. 1471 $expression_provider = new ReportExpressionLanguageProvider(); 1472 $expression_cache = new NullAdapter(); 1473 $expression_language = new ExpressionLanguage($expression_cache, [$expression_provider]); 1474 1475 $value = (string) $expression_language->evaluate($value); 1476 } 1477 1478 if (strpos($value, '@') !== false) { 1479 $value = ''; 1480 } 1481 $this->vars[$name]['id'] = $value; 1482 } 1483 1484 /** 1485 * XML <if > start element 1486 * 1487 * @param string[] $attrs an array of key value pairs for the attributes 1488 * 1489 * @return void 1490 */ 1491 protected function ifStartHandler(array $attrs): void 1492 { 1493 if ($this->process_ifs > 0) { 1494 $this->process_ifs++; 1495 1496 return; 1497 } 1498 1499 $condition = $attrs['condition']; 1500 $condition = $this->substituteVars($condition, true); 1501 $condition = str_replace([ 1502 ' LT ', 1503 ' GT ', 1504 ], [ 1505 '<', 1506 '>', 1507 ], $condition); 1508 // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT 1509 $condition = str_replace('@fact:', $this->fact . ':', $condition); 1510 $match = []; 1511 $count = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER); 1512 $i = 0; 1513 while ($i < $count) { 1514 $id = $match[$i][1]; 1515 $value = '""'; 1516 if ($id === 'ID') { 1517 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1518 $value = "'" . $match[1] . "'"; 1519 } 1520 } elseif ($id === 'fact') { 1521 $value = '"' . $this->fact . '"'; 1522 } elseif ($id === 'desc') { 1523 $value = '"' . addslashes($this->desc) . '"'; 1524 } elseif ($id === 'generation') { 1525 $value = '"' . $this->generation . '"'; 1526 } else { 1527 $temp = explode(' ', trim($this->gedrec)); 1528 $level = $temp[0]; 1529 if ($level == 0) { 1530 $level++; 1531 } 1532 $value = $this->getGedcomValue($id, $level, $this->gedrec); 1533 if (empty($value)) { 1534 $level++; 1535 $value = $this->getGedcomValue($id, $level, $this->gedrec); 1536 } 1537 $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value); 1538 $value = '"' . addslashes($value) . '"'; 1539 } 1540 $condition = str_replace("@$id", $value, $condition); 1541 $i++; 1542 } 1543 1544 // Create an expression language with the functions used by our reports. 1545 $expression_provider = new ReportExpressionLanguageProvider(); 1546 $expression_cache = new NullAdapter(); 1547 $expression_language = new ExpressionLanguage($expression_cache, [$expression_provider]); 1548 1549 $ret = $expression_language->evaluate($condition); 1550 1551 if (!$ret) { 1552 $this->process_ifs++; 1553 } 1554 } 1555 1556 /** 1557 * XML <if /> end element 1558 * 1559 * @return void 1560 */ 1561 protected function ifEndHandler(): void 1562 { 1563 if ($this->process_ifs > 0) { 1564 $this->process_ifs--; 1565 } 1566 } 1567 1568 /** 1569 * XML <Footnote > start element 1570 * Collect the Footnote links 1571 * GEDCOM Records that are protected by Privacy setting will be ignore 1572 * 1573 * @param string[] $attrs an array of key value pairs for the attributes 1574 * 1575 * @return void 1576 */ 1577 protected function footnoteStartHandler(array $attrs): void 1578 { 1579 $id = ''; 1580 if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) { 1581 $id = $match[2]; 1582 } 1583 $record = GedcomRecord::getInstance($id, $this->tree); 1584 if ($record && $record->canShow()) { 1585 $this->print_data_stack[] = $this->print_data; 1586 $this->print_data = true; 1587 $style = ''; 1588 if (!empty($attrs['style'])) { 1589 $style = $attrs['style']; 1590 } 1591 $this->footnote_element = $this->current_element; 1592 $this->current_element = $this->report_root->createFootnote($style); 1593 } else { 1594 $this->print_data = false; 1595 $this->process_footnote = false; 1596 } 1597 } 1598 1599 /** 1600 * XML <Footnote /> end element 1601 * Print the collected Footnote data 1602 * 1603 * @return void 1604 */ 1605 protected function footnoteEndHandler(): void 1606 { 1607 if ($this->process_footnote) { 1608 $this->print_data = array_pop($this->print_data_stack); 1609 $temp = trim($this->current_element->getValue()); 1610 if (strlen($temp) > 3) { 1611 $this->wt_report->addElement($this->current_element); 1612 } 1613 $this->current_element = $this->footnote_element; 1614 } else { 1615 $this->process_footnote = true; 1616 } 1617 } 1618 1619 /** 1620 * XML <FootnoteTexts /> element 1621 * 1622 * @return void 1623 */ 1624 protected function footnoteTextsStartHandler(): void 1625 { 1626 $temp = 'footnotetexts'; 1627 $this->wt_report->addElement($temp); 1628 } 1629 1630 /** 1631 * XML element Forced line break handler - HTML code 1632 * 1633 * @return void 1634 */ 1635 protected function brStartHandler(): void 1636 { 1637 if ($this->print_data && $this->process_gedcoms === 0) { 1638 $this->current_element->addText('<br>'); 1639 } 1640 } 1641 1642 /** 1643 * XML <sp />element Forced space handler 1644 * 1645 * @return void 1646 */ 1647 protected function spStartHandler(): void 1648 { 1649 if ($this->print_data && $this->process_gedcoms === 0) { 1650 $this->current_element->addText(' '); 1651 } 1652 } 1653 1654 /** 1655 * XML <HighlightedImage/> 1656 * 1657 * @param string[] $attrs an array of key value pairs for the attributes 1658 * 1659 * @return void 1660 */ 1661 protected function highlightedImageStartHandler(array $attrs): void 1662 { 1663 $id = ''; 1664 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1665 $id = $match[1]; 1666 } 1667 1668 // Position the top corner of this box on the page 1669 $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION); 1670 1671 // Position the left corner of this box on the page 1672 $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION); 1673 1674 // string Align the image in left, center, right (or empty to use x/y position). 1675 $align = $attrs['align'] ?? ''; 1676 1677 // string Next Line should be T:next to the image, N:next line 1678 $ln = $attrs['ln'] ?? 'T'; 1679 1680 // Width, height (or both). 1681 $width = (float) ($attrs['width'] ?? 0.0); 1682 $height = (float) ($attrs['height'] ?? 0.0); 1683 1684 $person = Individual::getInstance($id, $this->tree); 1685 $media_file = $person->findHighlightedMediaFile(); 1686 1687 if ($media_file !== null && $media_file->fileExists()) { 1688 $attributes = getimagesize($media_file->getServerFilename()) ?: [ 1689 0, 1690 0, 1691 ]; 1692 if ($width > 0 && $height == 0) { 1693 $perc = $width / $attributes[0]; 1694 $height = round($attributes[1] * $perc); 1695 } elseif ($height > 0 && $width == 0) { 1696 $perc = $height / $attributes[1]; 1697 $width = round($attributes[0] * $perc); 1698 } else { 1699 $width = $attributes[0]; 1700 $height = $attributes[1]; 1701 } 1702 $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln); 1703 $this->wt_report->addElement($image); 1704 } 1705 } 1706 1707 /** 1708 * XML <Image/> 1709 * 1710 * @param string[] $attrs an array of key value pairs for the attributes 1711 * 1712 * @return void 1713 */ 1714 protected function imageStartHandler(array $attrs): void 1715 { 1716 // Position the top corner of this box on the page. the default is the current position 1717 $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION); 1718 1719 // mixed Position the left corner of this box on the page. the default is the current position 1720 $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION); 1721 1722 // string Align the image in left, center, right (or empty to use x/y position). 1723 $align = $attrs['align'] ?? ''; 1724 1725 // string Next Line should be T:next to the image, N:next line 1726 $ln = $attrs['ln'] ?? 'T'; 1727 1728 // Width, height (or both). 1729 $width = (float) ($attrs['width'] ?? 0.0); 1730 $height = (float) ($attrs['height'] ?? 0.0); 1731 1732 $file = $attrs['file'] ?? ''; 1733 1734 if ($file === '@FILE') { 1735 $match = []; 1736 if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) { 1737 $mediaobject = Media::getInstance($match[1], $this->tree); 1738 $media_file = $mediaobject->firstImageFile(); 1739 1740 if ($media_file !== null && $media_file->fileExists()) { 1741 $attributes = getimagesize($media_file->getServerFilename()) ?: [ 1742 0, 1743 0, 1744 ]; 1745 if ($width > 0 && $height == 0) { 1746 $perc = $width / $attributes[0]; 1747 $height = round($attributes[1] * $perc); 1748 } elseif ($height > 0 && $width == 0) { 1749 $perc = $height / $attributes[1]; 1750 $width = round($attributes[0] * $perc); 1751 } else { 1752 $width = $attributes[0]; 1753 $height = $attributes[1]; 1754 } 1755 $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln); 1756 $this->wt_report->addElement($image); 1757 } 1758 } 1759 } else { 1760 if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) { 1761 $size = getimagesize($file); 1762 if ($width > 0 && $height == 0) { 1763 $perc = $width / $size[0]; 1764 $height = round($size[1] * $perc); 1765 } elseif ($height > 0 && $width == 0) { 1766 $perc = $height / $size[1]; 1767 $width = round($size[0] * $perc); 1768 } else { 1769 $width = $size[0]; 1770 $height = $size[1]; 1771 } 1772 $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln); 1773 $this->wt_report->addElement($image); 1774 } 1775 } 1776 } 1777 1778 /** 1779 * XML <Line> element handler 1780 * 1781 * @param string[] $attrs an array of key value pairs for the attributes 1782 * 1783 * @return void 1784 */ 1785 protected function lineStartHandler(array $attrs): void 1786 { 1787 // Start horizontal position, current position (default) 1788 $x1 = ReportBaseElement::CURRENT_POSITION; 1789 if (isset($attrs['x1'])) { 1790 if ($attrs['x1'] === '0') { 1791 $x1 = 0; 1792 } elseif ($attrs['x1'] === '.') { 1793 $x1 = ReportBaseElement::CURRENT_POSITION; 1794 } elseif (!empty($attrs['x1'])) { 1795 $x1 = (float) $attrs['x1']; 1796 } 1797 } 1798 // Start vertical position, current position (default) 1799 $y1 = ReportBaseElement::CURRENT_POSITION; 1800 if (isset($attrs['y1'])) { 1801 if ($attrs['y1'] === '0') { 1802 $y1 = 0; 1803 } elseif ($attrs['y1'] === '.') { 1804 $y1 = ReportBaseElement::CURRENT_POSITION; 1805 } elseif (!empty($attrs['y1'])) { 1806 $y1 = (float) $attrs['y1']; 1807 } 1808 } 1809 // End horizontal position, maximum width (default) 1810 $x2 = ReportBaseElement::CURRENT_POSITION; 1811 if (isset($attrs['x2'])) { 1812 if ($attrs['x2'] === '0') { 1813 $x2 = 0; 1814 } elseif ($attrs['x2'] === '.') { 1815 $x2 = ReportBaseElement::CURRENT_POSITION; 1816 } elseif (!empty($attrs['x2'])) { 1817 $x2 = (float) $attrs['x2']; 1818 } 1819 } 1820 // End vertical position 1821 $y2 = ReportBaseElement::CURRENT_POSITION; 1822 if (isset($attrs['y2'])) { 1823 if ($attrs['y2'] === '0') { 1824 $y2 = 0; 1825 } elseif ($attrs['y2'] === '.') { 1826 $y2 = ReportBaseElement::CURRENT_POSITION; 1827 } elseif (!empty($attrs['y2'])) { 1828 $y2 = (float) $attrs['y2']; 1829 } 1830 } 1831 1832 $line = $this->report_root->createLine($x1, $y1, $x2, $y2); 1833 $this->wt_report->addElement($line); 1834 } 1835 1836 /** 1837 * XML <List> 1838 * 1839 * @param string[] $attrs an array of key value pairs for the attributes 1840 * 1841 * @return void 1842 */ 1843 protected function listStartHandler(array $attrs): void 1844 { 1845 $this->process_repeats++; 1846 if ($this->process_repeats > 1) { 1847 return; 1848 } 1849 1850 $match = []; 1851 if (isset($attrs['sortby'])) { 1852 $sortby = $attrs['sortby']; 1853 if (preg_match("/\\$(\w+)/", $sortby, $match)) { 1854 $sortby = $this->vars[$match[1]]['id']; 1855 $sortby = trim($sortby); 1856 } 1857 } else { 1858 $sortby = 'NAME'; 1859 } 1860 1861 $listname = $attrs['list'] ?? 'individual'; 1862 1863 // Some filters/sorts can be applied using SQL, while others require PHP 1864 switch ($listname) { 1865 case 'pending': 1866 $xrefs = DB::table('change') 1867 ->whereIn('change_id', function (Builder $query): void { 1868 $query->select(new Expression('MAX(change_id)')) 1869 ->from('change') 1870 ->where('gedcom_id', '=', $this->tree->id()) 1871 ->where('status', '=', 'pending') 1872 ->groupBy(['xref']); 1873 }) 1874 ->pluck('xref'); 1875 1876 $this->list = []; 1877 foreach ($xrefs as $xref) { 1878 $this->list[] = GedcomRecord::getInstance($xref, $this->tree); 1879 } 1880 break; 1881 case 'individual': 1882 $query = DB::table('individuals') 1883 ->where('i_file', '=', $this->tree->id()) 1884 ->select(['i_id AS xref', 'i_gedcom AS gedcom']) 1885 ->distinct(); 1886 1887 foreach ($attrs as $attr => $value) { 1888 if (strpos($attr, 'filter') === 0 && $value) { 1889 $value = $this->substituteVars($value, false); 1890 // Convert the various filters into SQL 1891 if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) { 1892 $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void { 1893 $join 1894 ->on($attr . '.d_gid', '=', 'i_id') 1895 ->on($attr . '.d_file', '=', 'i_file'); 1896 }); 1897 1898 $query->where($attr . '.d_fact', '=', $match[1]); 1899 1900 $date = new Date($match[3]); 1901 1902 if ($match[2] === 'LTE') { 1903 $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay()); 1904 } else { 1905 $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay()); 1906 } 1907 1908 // This filter has been fully processed 1909 unset($attrs[$attr]); 1910 } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) { 1911 $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void { 1912 $join 1913 ->on($attr . '.n_id', '=', 'i_id') 1914 ->on($attr . '.n_file', '=', 'i_file'); 1915 }); 1916 // Search the DB only if there is any name supplied 1917 $names = explode(' ', $match[1]); 1918 foreach ($names as $n => $name) { 1919 $query->whereContains($attr . '.n_full', $name); 1920 } 1921 1922 // This filter has been fully processed 1923 unset($attrs[$attr]); 1924 } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) { 1925 // Convert newline escape sequences to actual new lines 1926 $match[1] = str_replace('\n', "\n", $match[1]); 1927 1928 $query->where('i_gedcom', 'LIKE', $match[1]); 1929 1930 // This filter has been fully processed 1931 unset($attrs[$attr]); 1932 } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) { 1933 // Don't unset this filter. This is just initial filtering for performance 1934 $query 1935 ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void { 1936 $join 1937 ->on($attr . 'a.pl_file', '=', 'i_file') 1938 ->on($attr . 'a.pl_gid', '=', 'i_id'); 1939 }) 1940 ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void { 1941 $join 1942 ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file') 1943 ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id'); 1944 }) 1945 ->whereContains($attr . 'b.p_place', $match[1]); 1946 } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) { 1947 // Don't unset this filter. This is just initial filtering for performance 1948 $match[3] = strtr($match[3], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 1949 $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%'; 1950 $query->where('i_gedcom', 'LIKE', $like); 1951 } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) { 1952 // Don't unset this filter. This is just initial filtering for performance 1953 $match[2] = strtr($match[2], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 1954 $like = "%\n1 " . $match[1] . '%' . $match[2] . '%'; 1955 $query->where('i_gedcom', 'LIKE', $like); 1956 } 1957 } 1958 } 1959 1960 $this->list = []; 1961 1962 foreach ($query->get() as $row) { 1963 $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom); 1964 } 1965 break; 1966 1967 case 'family': 1968 $query = DB::table('families') 1969 ->where('f_file', '=', $this->tree->id()) 1970 ->select(['f_id AS xref', 'f_gedcom AS gedcom']) 1971 ->distinct(); 1972 1973 foreach ($attrs as $attr => $value) { 1974 if (strpos($attr, 'filter') === 0 && $value) { 1975 $value = $this->substituteVars($value, false); 1976 // Convert the various filters into SQL 1977 if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) { 1978 $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void { 1979 $join 1980 ->on($attr . '.d_gid', '=', 'f_id') 1981 ->on($attr . '.d_file', '=', 'f_file'); 1982 }); 1983 1984 $query->where($attr . '.d_fact', '=', $match[1]); 1985 1986 $date = new Date($match[3]); 1987 1988 if ($match[2] === 'LTE') { 1989 $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay()); 1990 } else { 1991 $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay()); 1992 } 1993 1994 // This filter has been fully processed 1995 unset($attrs[$attr]); 1996 } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) { 1997 // Convert newline escape sequences to actual new lines 1998 $match[1] = str_replace('\n', "\n", $match[1]); 1999 2000 $query->where('f_gedcom', 'LIKE', $match[1]); 2001 2002 // This filter has been fully processed 2003 unset($attrs[$attr]); 2004 } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) { 2005 if ($sortby === 'NAME' || $match[1] !== '') { 2006 $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void { 2007 $join 2008 ->on($attr . '.n_file', '=', 'f_file') 2009 ->where(static function (Builder $query): void { 2010 $query 2011 ->whereColumn('n_id', '=', 'f_husb') 2012 ->orWhereColumn('n_id', '=', 'f_wife'); 2013 }); 2014 }); 2015 // Search the DB only if there is any name supplied 2016 if ($match[1] != '') { 2017 $names = explode(' ', $match[1]); 2018 foreach ($names as $n => $name) { 2019 $query->whereContains($attr . '.n_full', $name); 2020 } 2021 } 2022 } 2023 2024 // This filter has been fully processed 2025 unset($attrs[$attr]); 2026 } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) { 2027 // Don't unset this filter. This is just initial filtering for performance 2028 $query 2029 ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void { 2030 $join 2031 ->on($attr . 'a.pl_file', '=', 'f_file') 2032 ->on($attr . 'a.pl_gid', '=', 'f_id'); 2033 }) 2034 ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void { 2035 $join 2036 ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file') 2037 ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id'); 2038 }) 2039 ->whereContains($attr . 'b.p_place', $match[1]); 2040 } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) { 2041 // Don't unset this filter. This is just initial filtering for performance 2042 $match[3] = strtr($match[3], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 2043 $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%'; 2044 $query->where('f_gedcom', 'LIKE', $like); 2045 } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) { 2046 // Don't unset this filter. This is just initial filtering for performance 2047 $match[2] = strtr($match[2], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 2048 $like = "%\n1 " . $match[1] . '%' . $match[2] . '%'; 2049 $query->where('f_gedcom', 'LIKE', $like); 2050 } 2051 } 2052 } 2053 2054 $this->list = []; 2055 2056 foreach ($query->get() as $row) { 2057 $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom); 2058 } 2059 break; 2060 2061 default: 2062 throw new DomainException('Invalid list name: ' . $listname); 2063 } 2064 2065 $filters = []; 2066 $filters2 = []; 2067 if (isset($attrs['filter1']) && count($this->list) > 0) { 2068 foreach ($attrs as $key => $value) { 2069 if (preg_match("/filter(\d)/", $key)) { 2070 $condition = $value; 2071 if (preg_match("/@(\w+)/", $condition, $match)) { 2072 $id = $match[1]; 2073 $value = "''"; 2074 if ($id === 'ID') { 2075 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 2076 $value = "'" . $match[1] . "'"; 2077 } 2078 } elseif ($id === 'fact') { 2079 $value = "'" . $this->fact . "'"; 2080 } elseif ($id === 'desc') { 2081 $value = "'" . $this->desc . "'"; 2082 } else { 2083 if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) { 2084 $value = "'" . str_replace('@', '', trim($match[1])) . "'"; 2085 } 2086 } 2087 $condition = preg_replace("/@$id/", $value, $condition); 2088 } 2089 //-- handle regular expressions 2090 if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) { 2091 $tag = trim($match[1]); 2092 $expr = trim($match[2]); 2093 $val = trim($match[3]); 2094 if (preg_match("/\\$(\w+)/", $val, $match)) { 2095 $val = $this->vars[$match[1]]['id']; 2096 $val = trim($val); 2097 } 2098 if ($val) { 2099 $searchstr = ''; 2100 $tags = explode(':', $tag); 2101 //-- only limit to a level number if we are specifically looking at a level 2102 if (count($tags) > 1) { 2103 $level = 1; 2104 foreach ($tags as $t) { 2105 if (!empty($searchstr)) { 2106 $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n"; 2107 } 2108 //-- search for both EMAIL and _EMAIL... silly double gedcom standard 2109 if ($t === 'EMAIL' || $t === '_EMAIL') { 2110 $t = '_?EMAIL'; 2111 } 2112 $searchstr .= $level . ' ' . $t; 2113 $level++; 2114 } 2115 } else { 2116 if ($tag === 'EMAIL' || $tag === '_EMAIL') { 2117 $tag = '_?EMAIL'; 2118 } 2119 $t = $tag; 2120 $searchstr = '1 ' . $tag; 2121 } 2122 switch ($expr) { 2123 case 'CONTAINS': 2124 if ($t === 'PLAC') { 2125 $searchstr .= "[^\n]*[, ]*" . $val; 2126 } else { 2127 $searchstr .= "[^\n]*" . $val; 2128 } 2129 $filters[] = $searchstr; 2130 break; 2131 default: 2132 $filters2[] = [ 2133 'tag' => $tag, 2134 'expr' => $expr, 2135 'val' => $val, 2136 ]; 2137 break; 2138 } 2139 } 2140 } 2141 } 2142 } 2143 } 2144 //-- apply other filters to the list that could not be added to the search string 2145 if ($filters) { 2146 foreach ($this->list as $key => $record) { 2147 foreach ($filters as $filter) { 2148 if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) { 2149 unset($this->list[$key]); 2150 break; 2151 } 2152 } 2153 } 2154 } 2155 if ($filters2) { 2156 $mylist = []; 2157 foreach ($this->list as $indi) { 2158 $key = $indi->xref(); 2159 $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree)); 2160 $keep = true; 2161 foreach ($filters2 as $filter) { 2162 if ($keep) { 2163 $tag = $filter['tag']; 2164 $expr = $filter['expr']; 2165 $val = $filter['val']; 2166 if ($val == "''") { 2167 $val = ''; 2168 } 2169 $tags = explode(':', $tag); 2170 $t = end($tags); 2171 $v = $this->getGedcomValue($tag, 1, $grec); 2172 //-- check for EMAIL and _EMAIL (silly double gedcom standard :P) 2173 if ($t === 'EMAIL' && empty($v)) { 2174 $tag = str_replace('EMAIL', '_EMAIL', $tag); 2175 $tags = explode(':', $tag); 2176 $t = end($tags); 2177 $v = Functions::getSubRecord(1, $tag, $grec); 2178 } 2179 2180 switch ($expr) { 2181 case 'GTE': 2182 if ($t === 'DATE') { 2183 $date1 = new Date($v); 2184 $date2 = new Date($val); 2185 $keep = (Date::compare($date1, $date2) >= 0); 2186 } elseif ($val >= $v) { 2187 $keep = true; 2188 } 2189 break; 2190 case 'LTE': 2191 if ($t === 'DATE') { 2192 $date1 = new Date($v); 2193 $date2 = new Date($val); 2194 $keep = (Date::compare($date1, $date2) <= 0); 2195 } elseif ($val >= $v) { 2196 $keep = true; 2197 } 2198 break; 2199 default: 2200 if ($v == $val) { 2201 $keep = true; 2202 } else { 2203 $keep = false; 2204 } 2205 break; 2206 } 2207 } 2208 } 2209 if ($keep) { 2210 $mylist[$key] = $indi; 2211 } 2212 } 2213 $this->list = $mylist; 2214 } 2215 2216 switch ($sortby) { 2217 case 'NAME': 2218 uasort($this->list, GedcomRecord::nameComparator()); 2219 break; 2220 case 'CHAN': 2221 uasort($this->list, GedcomRecord::lastChangeComparator()); 2222 break; 2223 case 'BIRT:DATE': 2224 uasort($this->list, Individual::birthDateComparator()); 2225 break; 2226 case 'DEAT:DATE': 2227 uasort($this->list, Individual::deathDateComparator()); 2228 break; 2229 case 'MARR:DATE': 2230 uasort($this->list, Family::marriageDateComparator()); 2231 break; 2232 default: 2233 // unsorted or already sorted by SQL 2234 break; 2235 } 2236 2237 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 2238 $this->repeat_bytes = xml_get_current_line_number($this->parser) + 1; 2239 } 2240 2241 /** 2242 * XML <List> 2243 * 2244 * @return void 2245 */ 2246 protected function listEndHandler(): void 2247 { 2248 $this->process_repeats--; 2249 if ($this->process_repeats > 0) { 2250 return; 2251 } 2252 2253 // Check if there is any list 2254 if (count($this->list) > 0) { 2255 $lineoffset = 0; 2256 foreach ($this->repeats_stack as $rep) { 2257 $lineoffset += $rep[1]; 2258 } 2259 //-- read the xml from the file 2260 $lines = file($this->report); 2261 while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) { 2262 $lineoffset--; 2263 } 2264 $lineoffset++; 2265 $reportxml = "<tempdoc>\n"; 2266 $line_nr = $lineoffset + $this->repeat_bytes; 2267 // List Level counter 2268 $count = 1; 2269 while (0 < $count) { 2270 if (strpos($lines[$line_nr], '<List') !== false) { 2271 $count++; 2272 } elseif (strpos($lines[$line_nr], '</List') !== false) { 2273 $count--; 2274 } 2275 if (0 < $count) { 2276 $reportxml .= $lines[$line_nr]; 2277 } 2278 $line_nr++; 2279 } 2280 // No need to drag this 2281 unset($lines); 2282 $reportxml .= '</tempdoc>'; 2283 // Save original values 2284 $this->parser_stack[] = $this->parser; 2285 $oldgedrec = $this->gedrec; 2286 2287 $this->list_total = count($this->list); 2288 $this->list_private = 0; 2289 foreach ($this->list as $record) { 2290 if ($record->canShow()) { 2291 $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree())); 2292 //-- start the sax parser 2293 $repeat_parser = xml_parser_create(); 2294 $this->parser = $repeat_parser; 2295 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 2296 2297 xml_set_element_handler( 2298 $repeat_parser, 2299 function ($parser, string $name, array $attrs): void { 2300 $this->startElement($parser, $name, $attrs); 2301 }, 2302 function ($parser, string $name): void { 2303 $this->endElement($parser, $name); 2304 } 2305 ); 2306 2307 xml_set_character_data_handler( 2308 $repeat_parser, 2309 function ($parser, string $data): void { 2310 $this->characterData($parser, $data); 2311 } 2312 ); 2313 2314 if (!xml_parse($repeat_parser, $reportxml, true)) { 2315 throw new DomainException(sprintf( 2316 'ListEHandler XML error: %s at line %d', 2317 xml_error_string(xml_get_error_code($repeat_parser)), 2318 xml_get_current_line_number($repeat_parser) 2319 )); 2320 } 2321 xml_parser_free($repeat_parser); 2322 } else { 2323 $this->list_private++; 2324 } 2325 } 2326 $this->list = []; 2327 $this->parser = array_pop($this->parser_stack); 2328 $this->gedrec = $oldgedrec; 2329 } 2330 [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); 2331 } 2332 2333 /** 2334 * XML <ListTotal> element handler 2335 * Prints the total number of records in a list 2336 * The total number is collected from 2337 * List and Relatives 2338 * 2339 * @return void 2340 */ 2341 protected function listTotalStartHandler(): void 2342 { 2343 if ($this->list_private == 0) { 2344 $this->current_element->addText((string) $this->list_total); 2345 } else { 2346 $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total); 2347 } 2348 } 2349 2350 /** 2351 * XML <Relatives> 2352 * 2353 * @param string[] $attrs an array of key value pairs for the attributes 2354 * 2355 * @return void 2356 */ 2357 protected function relativesStartHandler(array $attrs): void 2358 { 2359 $this->process_repeats++; 2360 if ($this->process_repeats > 1) { 2361 return; 2362 } 2363 2364 $sortby = $attrs['sortby'] ?? 'NAME'; 2365 2366 $match = []; 2367 if (preg_match("/\\$(\w+)/", $sortby, $match)) { 2368 $sortby = $this->vars[$match[1]]['id']; 2369 $sortby = trim($sortby); 2370 } 2371 2372 $maxgen = -1; 2373 if (isset($attrs['maxgen'])) { 2374 $maxgen = (int) $attrs['maxgen']; 2375 } 2376 2377 $group = $attrs['group'] ?? 'child-family'; 2378 2379 if (preg_match("/\\$(\w+)/", $group, $match)) { 2380 $group = $this->vars[$match[1]]['id']; 2381 $group = trim($group); 2382 } 2383 2384 $id = $attrs['id'] ?? ''; 2385 2386 if (preg_match("/\\$(\w+)/", $id, $match)) { 2387 $id = $this->vars[$match[1]]['id']; 2388 $id = trim($id); 2389 } 2390 2391 $this->list = []; 2392 $person = Individual::getInstance($id, $this->tree); 2393 if ($person instanceof Individual) { 2394 $this->list[$id] = $person; 2395 switch ($group) { 2396 case 'child-family': 2397 foreach ($person->childFamilies() as $family) { 2398 foreach ($family->spouses() as $spouse) { 2399 $this->list[$spouse->xref()] = $spouse; 2400 } 2401 2402 foreach ($family->children() as $child) { 2403 $this->list[$child->xref()] = $child; 2404 } 2405 } 2406 break; 2407 case 'spouse-family': 2408 foreach ($person->spouseFamilies() as $family) { 2409 foreach ($family->spouses() as $spouse) { 2410 $this->list[$spouse->xref()] = $spouse; 2411 } 2412 2413 foreach ($family->children() as $child) { 2414 $this->list[$child->xref()] = $child; 2415 } 2416 } 2417 break; 2418 case 'direct-ancestors': 2419 $this->addAncestors($this->list, $id, false, $maxgen); 2420 break; 2421 case 'ancestors': 2422 $this->addAncestors($this->list, $id, true, $maxgen); 2423 break; 2424 case 'descendants': 2425 $this->list[$id]->generation = 1; 2426 $this->addDescendancy($this->list, $id, false, $maxgen); 2427 break; 2428 case 'all': 2429 $this->addAncestors($this->list, $id, true, $maxgen); 2430 $this->addDescendancy($this->list, $id, true, $maxgen); 2431 break; 2432 } 2433 } 2434 2435 switch ($sortby) { 2436 case 'NAME': 2437 uasort($this->list, GedcomRecord::nameComparator()); 2438 break; 2439 case 'BIRT:DATE': 2440 uasort($this->list, Individual::birthDateComparator()); 2441 break; 2442 case 'DEAT:DATE': 2443 uasort($this->list, Individual::deathDateComparator()); 2444 break; 2445 case 'generation': 2446 $newarray = []; 2447 reset($this->list); 2448 $genCounter = 1; 2449 while (count($newarray) < count($this->list)) { 2450 foreach ($this->list as $key => $value) { 2451 $this->generation = $value->generation; 2452 if ($this->generation == $genCounter) { 2453 $newarray[$key] = new stdClass(); 2454 $newarray[$key]->generation = $this->generation; 2455 } 2456 } 2457 $genCounter++; 2458 } 2459 $this->list = $newarray; 2460 break; 2461 default: 2462 // unsorted 2463 break; 2464 } 2465 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 2466 $this->repeat_bytes = xml_get_current_line_number($this->parser) + 1; 2467 } 2468 2469 /** 2470 * XML </ Relatives> 2471 * 2472 * @return void 2473 */ 2474 protected function relativesEndHandler(): void 2475 { 2476 $this->process_repeats--; 2477 if ($this->process_repeats > 0) { 2478 return; 2479 } 2480 2481 // Check if there is any relatives 2482 if (count($this->list) > 0) { 2483 $lineoffset = 0; 2484 foreach ($this->repeats_stack as $rep) { 2485 $lineoffset += $rep[1]; 2486 } 2487 //-- read the xml from the file 2488 $lines = file($this->report); 2489 while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) { 2490 $lineoffset--; 2491 } 2492 $lineoffset++; 2493 $reportxml = "<tempdoc>\n"; 2494 $line_nr = $lineoffset + $this->repeat_bytes; 2495 // Relatives Level counter 2496 $count = 1; 2497 while (0 < $count) { 2498 if (strpos($lines[$line_nr], '<Relatives') !== false) { 2499 $count++; 2500 } elseif (strpos($lines[$line_nr], '</Relatives') !== false) { 2501 $count--; 2502 } 2503 if (0 < $count) { 2504 $reportxml .= $lines[$line_nr]; 2505 } 2506 $line_nr++; 2507 } 2508 // No need to drag this 2509 unset($lines); 2510 $reportxml .= "</tempdoc>\n"; 2511 // Save original values 2512 $this->parser_stack[] = $this->parser; 2513 $oldgedrec = $this->gedrec; 2514 2515 $this->list_total = count($this->list); 2516 $this->list_private = 0; 2517 foreach ($this->list as $xref => $value) { 2518 if (isset($value->generation)) { 2519 $this->generation = $value->generation; 2520 } 2521 $tmp = GedcomRecord::getInstance((string) $xref, $this->tree); 2522 $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree)); 2523 2524 $repeat_parser = xml_parser_create(); 2525 $this->parser = $repeat_parser; 2526 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 2527 2528 xml_set_element_handler( 2529 $repeat_parser, 2530 function ($parser, string $name, array $attrs): void { 2531 $this->startElement($parser, $name, $attrs); 2532 }, 2533 function ($parser, string $name): void { 2534 $this->endElement($parser, $name); 2535 } 2536 ); 2537 2538 xml_set_character_data_handler( 2539 $repeat_parser, 2540 function ($parser, string $data): void { 2541 $this->characterData($parser, $data); 2542 } 2543 ); 2544 2545 if (!xml_parse($repeat_parser, $reportxml, true)) { 2546 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))); 2547 } 2548 xml_parser_free($repeat_parser); 2549 } 2550 // Clean up the list array 2551 $this->list = []; 2552 $this->parser = array_pop($this->parser_stack); 2553 $this->gedrec = $oldgedrec; 2554 } 2555 [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); 2556 } 2557 2558 /** 2559 * XML <Generation /> element handler 2560 * Prints the number of generations 2561 * 2562 * @return void 2563 */ 2564 protected function generationStartHandler(): void 2565 { 2566 $this->current_element->addText((string) $this->generation); 2567 } 2568 2569 /** 2570 * XML <NewPage /> element handler 2571 * Has to be placed in an element (header, pageheader, body or footer) 2572 * 2573 * @return void 2574 */ 2575 protected function newPageStartHandler(): void 2576 { 2577 $temp = 'addpage'; 2578 $this->wt_report->addElement($temp); 2579 } 2580 2581 /** 2582 * XML <html> 2583 * 2584 * @param string $tag HTML tag name 2585 * @param string[] $attrs an array of key value pairs for the attributes 2586 * 2587 * @return void 2588 */ 2589 protected function htmlStartHandler(string $tag, array $attrs): void 2590 { 2591 if ($tag === 'tempdoc') { 2592 return; 2593 } 2594 $this->wt_report_stack[] = $this->wt_report; 2595 $this->wt_report = $this->report_root->createHTML($tag, $attrs); 2596 $this->current_element = $this->wt_report; 2597 2598 $this->print_data_stack[] = $this->print_data; 2599 $this->print_data = true; 2600 } 2601 2602 /** 2603 * XML </html> 2604 * 2605 * @param string $tag 2606 * 2607 * @return void 2608 */ 2609 protected function htmlEndHandler($tag): void 2610 { 2611 if ($tag === 'tempdoc') { 2612 return; 2613 } 2614 2615 $this->print_data = array_pop($this->print_data_stack); 2616 $this->current_element = $this->wt_report; 2617 $this->wt_report = array_pop($this->wt_report_stack); 2618 if ($this->wt_report !== null) { 2619 $this->wt_report->addElement($this->current_element); 2620 } else { 2621 $this->wt_report = $this->current_element; 2622 } 2623 } 2624 2625 /** 2626 * Handle <Input> 2627 * 2628 * @return void 2629 */ 2630 protected function inputStartHandler(): void 2631 { 2632 // Dummy function, to prevent the default HtmlStartHandler() being called 2633 } 2634 2635 /** 2636 * Handle </Input> 2637 * 2638 * @return void 2639 */ 2640 protected function inputEndHandler(): void 2641 { 2642 // Dummy function, to prevent the default HtmlEndHandler() being called 2643 } 2644 2645 /** 2646 * Handle <Report> 2647 * 2648 * @return void 2649 */ 2650 protected function reportStartHandler(): void 2651 { 2652 // Dummy function, to prevent the default HtmlStartHandler() being called 2653 } 2654 2655 /** 2656 * Handle </Report> 2657 * 2658 * @return void 2659 */ 2660 protected function reportEndHandler(): void 2661 { 2662 // Dummy function, to prevent the default HtmlEndHandler() being called 2663 } 2664 2665 /** 2666 * XML </titleEndHandler> 2667 * 2668 * @return void 2669 */ 2670 protected function titleEndHandler(): void 2671 { 2672 $this->report_root->addTitle($this->text); 2673 } 2674 2675 /** 2676 * XML </descriptionEndHandler> 2677 * 2678 * @return void 2679 */ 2680 protected function descriptionEndHandler(): void 2681 { 2682 $this->report_root->addDescription($this->text); 2683 } 2684 2685 /** 2686 * Create a list of all descendants. 2687 * 2688 * @param string[] $list 2689 * @param string $pid 2690 * @param bool $parents 2691 * @param int $generations 2692 * 2693 * @return void 2694 */ 2695 private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void 2696 { 2697 $person = Individual::getInstance($pid, $this->tree); 2698 if ($person === null) { 2699 return; 2700 } 2701 if (!isset($list[$pid])) { 2702 $list[$pid] = $person; 2703 } 2704 if (!isset($list[$pid]->generation)) { 2705 $list[$pid]->generation = 0; 2706 } 2707 foreach ($person->spouseFamilies() as $family) { 2708 if ($parents) { 2709 $husband = $family->husband(); 2710 $wife = $family->wife(); 2711 if ($husband) { 2712 $list[$husband->xref()] = $husband; 2713 if (isset($list[$pid]->generation)) { 2714 $list[$husband->xref()]->generation = $list[$pid]->generation - 1; 2715 } else { 2716 $list[$husband->xref()]->generation = 1; 2717 } 2718 } 2719 if ($wife) { 2720 $list[$wife->xref()] = $wife; 2721 if (isset($list[$pid]->generation)) { 2722 $list[$wife->xref()]->generation = $list[$pid]->generation - 1; 2723 } else { 2724 $list[$wife->xref()]->generation = 1; 2725 } 2726 } 2727 } 2728 2729 $children = $family->children(); 2730 2731 foreach ($children as $child) { 2732 if ($child) { 2733 $list[$child->xref()] = $child; 2734 2735 if (isset($list[$pid]->generation)) { 2736 $list[$child->xref()]->generation = $list[$pid]->generation + 1; 2737 } else { 2738 $list[$child->xref()]->generation = 2; 2739 } 2740 } 2741 } 2742 if ($generations == -1 || $list[$pid]->generation + 1 < $generations) { 2743 foreach ($children as $child) { 2744 $this->addDescendancy($list, $child->xref(), $parents, $generations); // recurse on the childs family 2745 } 2746 } 2747 } 2748 } 2749 2750 /** 2751 * Create a list of all ancestors. 2752 * 2753 * @param string[] $list 2754 * @param string $pid 2755 * @param bool $children 2756 * @param int $generations 2757 * 2758 * @return void 2759 */ 2760 private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void 2761 { 2762 $genlist = [$pid]; 2763 $list[$pid]->generation = 1; 2764 while (count($genlist) > 0) { 2765 $id = array_shift($genlist); 2766 if (strpos($id, 'empty') === 0) { 2767 continue; // id can be something like “empty7” 2768 } 2769 $person = Individual::getInstance($id, $this->tree); 2770 foreach ($person->childFamilies() as $family) { 2771 $husband = $family->husband(); 2772 $wife = $family->wife(); 2773 if ($husband) { 2774 $list[$husband->xref()] = $husband; 2775 $list[$husband->xref()]->generation = $list[$id]->generation + 1; 2776 } 2777 if ($wife) { 2778 $list[$wife->xref()] = $wife; 2779 $list[$wife->xref()]->generation = $list[$id]->generation + 1; 2780 } 2781 if ($generations == -1 || $list[$id]->generation + 1 < $generations) { 2782 if ($husband) { 2783 $genlist[] = $husband->xref(); 2784 } 2785 if ($wife) { 2786 $genlist[] = $wife->xref(); 2787 } 2788 } 2789 if ($children) { 2790 foreach ($family->children() as $child) { 2791 $list[$child->xref()] = $child; 2792 $list[$child->xref()]->generation = $list[$id]->generation ?? 1; 2793 } 2794 } 2795 } 2796 } 2797 } 2798 2799 /** 2800 * get gedcom tag value 2801 * 2802 * @param string $tag The tag to find, use : to delineate subtags 2803 * @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 2804 * @param string $gedrec The gedcom record to get the value from 2805 * 2806 * @return string the value of a gedcom tag from the given gedcom record 2807 */ 2808 private function getGedcomValue($tag, $level, $gedrec): string 2809 { 2810 if (empty($gedrec)) { 2811 return ''; 2812 } 2813 $tags = explode(':', $tag); 2814 $origlevel = $level; 2815 if ($level == 0) { 2816 $level = $gedrec[0] + 1; 2817 } 2818 2819 $subrec = $gedrec; 2820 foreach ($tags as $t) { 2821 $lastsubrec = $subrec; 2822 $subrec = Functions::getSubRecord($level, "$level $t", $subrec); 2823 if (empty($subrec) && $origlevel == 0) { 2824 $level--; 2825 $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec); 2826 } 2827 if (empty($subrec)) { 2828 if ($t === 'TITL') { 2829 $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec); 2830 if (!empty($subrec)) { 2831 $t = 'ABBR'; 2832 } 2833 } 2834 if (empty($subrec)) { 2835 if ($level > 0) { 2836 $level--; 2837 } 2838 $subrec = Functions::getSubRecord($level, "@ $t", $gedrec); 2839 if (empty($subrec)) { 2840 return ''; 2841 } 2842 } 2843 } 2844 $level++; 2845 } 2846 $level--; 2847 $ct = preg_match("/$level $t(.*)/", $subrec, $match); 2848 if ($ct == 0) { 2849 $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match); 2850 } 2851 if ($ct == 0) { 2852 $ct = preg_match("/@ $t (.+)/", $subrec, $match); 2853 } 2854 if ($ct > 0) { 2855 $value = trim($match[1]); 2856 if ($t === 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) { 2857 $note = Note::getInstance($match[1], $this->tree); 2858 if ($note instanceof Note) { 2859 $value = $note->getNote(); 2860 } else { 2861 //-- set the value to the id without the @ 2862 $value = $match[1]; 2863 } 2864 } 2865 if ($level != 0 || $t != 'NOTE') { 2866 $value .= Functions::getCont($level + 1, $subrec); 2867 } 2868 2869 return $value; 2870 } 2871 2872 return ''; 2873 } 2874 2875 /** 2876 * Replace variable identifiers with their values. 2877 * 2878 * @param string $expression An expression such as "$foo == 123" 2879 * @param bool $quote Whether to add quotation marks 2880 * 2881 * @return string 2882 */ 2883 private function substituteVars($expression, $quote): string 2884 { 2885 return preg_replace_callback( 2886 '/\$(\w+)/', 2887 function (array $matches) use ($quote): string { 2888 if (isset($this->vars[$matches[1]]['id'])) { 2889 if ($quote) { 2890 return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'"; 2891 } 2892 2893 return $this->vars[$matches[1]]['id']; 2894 } 2895 2896 Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1])); 2897 2898 return '$' . $matches[1]; 2899 }, 2900 $expression 2901 ); 2902 } 2903} 2904