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