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