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