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