| Total Complexity | 624 |
| Total Lines | 3100 |
| 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 |
||
| 1171 | { |
||
| 1172 | $this->process_repeats++; |
||
| 1173 | if ($this->process_repeats > 1) { |
||
| 1174 | return; |
||
| 1175 | } |
||
| 1176 | |||
| 1177 | $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; |
||
| 1178 | $this->repeats = []; |
||
| 1179 | $this->repeat_bytes = xml_get_current_line_number($this->parser); |
||
| 1180 | |||
| 1181 | $tag = $attrs['tag'] ?? ''; |
||
| 1182 | if (!empty($tag)) { |
||
| 1183 | if ($tag === '@desc') { |
||
| 1184 | $value = $this->desc; |
||
| 1185 | $value = trim($value); |
||
| 1186 | $this->current_element->addText($value); |
||
| 1187 | } else { |
||
| 1188 | $tag = str_replace('@fact', $this->fact, $tag); |
||
| 1189 | $tags = explode(':', $tag); |
||
| 1190 | $level = (int) explode(' ', trim($this->gedrec))[0]; |
||
| 1191 | if ($level === 0) { |
||
| 1192 | $level++; |
||
| 1193 | } |
||
| 1194 | $subrec = $this->gedrec; |
||
| 1195 | $t = $tag; |
||
| 1196 | $count = count($tags); |
||
| 1197 | $i = 0; |
||
| 1198 | while ($i < $count) { |
||
| 1199 | $t = $tags[$i]; |
||
| 1200 | if (!empty($t)) { |
||
| 1201 | if ($i < ($count - 1)) { |
||
| 1202 | $subrec = self::getSubRecord($level, "$level $t", $subrec); |
||
| 1203 | if (empty($subrec)) { |
||
| 1204 | $level--; |
||
| 1205 | $subrec = self::getSubRecord($level, "@ $t", $this->gedrec); |
||
| 1206 | if (empty($subrec)) { |
||
| 1207 | return; |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | } |
||
| 1211 | $level++; |
||
| 1212 | } |
||
| 1213 | $i++; |
||
| 1214 | } |
||
| 1215 | $level--; |
||
| 1216 | $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER); |
||
| 1217 | $i = 0; |
||
| 1218 | while ($i < $count) { |
||
| 1219 | $i++; |
||
| 1220 | // Privacy check - is this a link, and are we allowed to view the linked object? |
||
| 1221 | $subrecord = self::getSubRecord($level, "$level $t", $subrec, $i); |
||
| 1222 | if (preg_match('/^\d ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@/', $subrecord, $xref_match)) { |
||
| 1223 | $linked_object = Registry::gedcomRecordFactory()->make($xref_match[1], $this->tree); |
||
| 1224 | if ($linked_object && !$linked_object->canShow()) { |
||
| 1225 | //continue; |
||
| 1226 | } |
||
| 1227 | } |
||
| 1228 | $this->repeats[] = $subrecord; |
||
| 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 = $lineoffset + $rep[1] - 1; |
||
| 1257 | } |
||
| 1258 | while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag')) { |
||
| 1259 | $lineoffset--; |
||
| 1260 | } |
||
| 1261 | $lineoffset++; |
||
| 1262 | $reportxml = "<tempdoc>\n"; |
||
| 1263 | $line_nr = $lineoffset + $this->repeat_bytes; |
||
| 1264 | $lnnn = $line_nr; |
||
| 1265 | // RepeatTag Level counter |
||
| 1266 | $count = 1; |
||
| 1267 | while (0 < $count) { |
||
| 1268 | if (str_contains($lines[$line_nr], '<RepeatTag')) { |
||
| 1269 | $count++; |
||
| 1270 | } elseif (str_contains($lines[$line_nr], '</RepeatTag')) { |
||
| 1271 | $count--; |
||
| 1272 | } |
||
| 1273 | if (0 < $count) { |
||
| 1274 | $reportxml .= $lines[$line_nr]; |
||
| 1275 | } |
||
| 1276 | $line_nr++; |
||
| 1277 | } |
||
| 1278 | // No need to drag this |
||
| 1279 | unset($lines); |
||
| 1280 | $reportxml .= "</tempdoc>\n"; |
||
| 1281 | // Save original values |
||
| 1282 | $this->parser_stack[] = $this->parser; |
||
| 1283 | $oldgedrec = $this->gedrec; |
||
| 1284 | foreach ($this->repeats as $gedrec) { |
||
| 1285 | $this->gedrec = $gedrec; |
||
| 1286 | $repeat_parser = xml_parser_create(); |
||
| 1287 | $this->parser = $repeat_parser; |
||
| 1288 | xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0); |
||
| 1289 | |||
| 1290 | xml_set_element_handler( |
||
| 1291 | $repeat_parser, |
||
| 1292 | function ($parser, string $name, array $attrs): void { |
||
| 1293 | $this->startElement($parser, $name, $attrs); |
||
| 1294 | }, |
||
| 1295 | function ($parser, string $name): void { |
||
| 1296 | $this->endElement($parser, $name); |
||
| 1297 | } |
||
| 1298 | ); |
||
| 1299 | |||
| 1300 | xml_set_character_data_handler( |
||
| 1301 | $repeat_parser, |
||
| 1302 | function ($parser, string $data): void { |
||
| 1303 | $this->characterData($parser, $data); |
||
| 1304 | } |
||
| 1305 | ); |
||
| 1306 | |||
| 1307 | if (!xml_parse($repeat_parser, $reportxml, true)) { |
||
| 1308 | throw new DomainException(sprintf( |
||
| 1309 | 'RepeatTagEHandler XML error: %s at line %d', |
||
| 1310 | xml_error_string(xml_get_error_code($repeat_parser)), |
||
| 1311 | xml_get_current_line_number($repeat_parser) |
||
| 1312 | )); |
||
| 1313 | } |
||
| 1314 | xml_parser_free($repeat_parser); |
||
| 1315 | } |
||
| 1316 | // Restore original values |
||
| 1317 | $this->gedrec = $oldgedrec; |
||
| 1318 | $this->parser = array_pop($this->parser_stack); |
||
| 1319 | } |
||
| 1320 | [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * Variable lookup |
||
| 1325 | * Retrieve predefined variables : |
||
| 1326 | * @ desc GEDCOM fact description, example: |
||
| 1327 | * 1 EVEN This is a description |
||
| 1328 | * @ fact GEDCOM fact tag, such as BIRT, DEAT etc. |
||
| 1329 | * $ I18N::translate('....') |
||
| 1330 | * $ language_settings[] |
||
| 1331 | * |
||
| 1332 | * @param array<string> $attrs an array of key value pairs for the attributes |
||
| 1333 | * |
||
| 1334 | * @return void |
||
| 1335 | */ |
||
| 1336 | protected function varStartHandler(array $attrs): void |
||
| 1337 | { |
||
| 1338 | if (!isset($attrs['var'])) { |
||
| 1339 | 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)); |
||
| 1340 | } |
||
| 1341 | |||
| 1342 | $var = $attrs['var']; |
||
| 1343 | // SetVar element preset variables |
||
| 1344 | if (!empty($this->vars[$var]['id'])) { |
||
| 1345 | $var = $this->vars[$var]['id']; |
||
| 1346 | } else { |
||
| 1347 | $tfact = $this->fact; |
||
| 1348 | if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== '') { |
||
| 1349 | // Use : |
||
| 1350 | // n TYPE This text if string |
||
| 1351 | $tfact = $this->type; |
||
| 1352 | } else { |
||
| 1353 | foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) { |
||
| 1354 | $element = Registry::elementFactory()->make($record_type . ':' . $this->fact); |
||
| 1355 | |||
| 1356 | if (!$element instanceof UnknownElement) { |
||
| 1357 | $tfact = $element->label(); |
||
| 1358 | break; |
||
| 1359 | } |
||
| 1360 | } |
||
| 1361 | } |
||
| 1362 | |||
| 1363 | $var = strtr($var, ['@desc' => $this->desc, '@fact' => $tfact]); |
||
| 1364 | |||
| 1365 | if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) { |
||
| 1366 | $var = I18N::number((int) $match[1]); |
||
| 1367 | } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) { |
||
| 1368 | $var = I18N::translate($match[1]); |
||
| 1369 | } elseif (preg_match('/^I18N::translate\(\$(.+)\)$/', $var, $match)) { |
||
| 1370 | $var = I18N::translate($this->vars[$match[1]]['id']); |
||
| 1371 | } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) { |
||
| 1372 | $var = I18N::translateContext($match[1], $match[2]); |
||
| 1373 | } |
||
| 1374 | } |
||
| 1375 | // Check if variable is set as a date and reformat the date |
||
| 1376 | if (isset($attrs['date'])) { |
||
| 1377 | if ($attrs['date'] === '1') { |
||
| 1378 | $g = new Date($var); |
||
| 1379 | $var = $g->display(); |
||
| 1380 | } |
||
| 1381 | } |
||
| 1382 | if (isset($attrs['amp'])) { |
||
| 1383 | $var = str_replace("%26", '&', $var); |
||
| 1384 | } |
||
| 1385 | if (isset($attrs['cut'])) { |
||
| 1386 | $cut = (int) $attrs['cut']; |
||
| 1387 | $var = $cut > 0 ? substr($var, 0, $cut) : substr($var, $cut); |
||
| 1388 | if ($cut == 0) { |
||
| 1389 | $var = ""; |
||
| 1390 | } |
||
| 1391 | } |
||
| 1392 | if (isset($attrs['lcfirst'])) { |
||
| 1393 | $var = lcfirst($var); |
||
| 1394 | } |
||
| 1395 | $this->current_element->addText($var); |
||
| 1396 | $this->text = $var; // Used for title/description |
||
| 1397 | } |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * Handle <facts> |
||
| 1401 | * |
||
| 1402 | * @param array<string> $attrs |
||
| 1403 | * |
||
| 1404 | * @return void |
||
| 1405 | */ |
||
| 1406 | protected function factsStartHandler(array $attrs): void |
||
| 1407 | { |
||
| 1408 | $this->process_repeats++; |
||
| 1409 | if ($this->process_repeats > 1) { |
||
| 1410 | return; |
||
| 1411 | } |
||
| 1412 | |||
| 1413 | $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; |
||
| 1414 | $this->repeats = []; |
||
| 1415 | $this->repeat_bytes = xml_get_current_line_number($this->parser); |
||
| 1416 | |||
| 1417 | $id = ''; |
||
| 1418 | $match = []; |
||
| 1419 | if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { |
||
| 1420 | $id = $match[1]; |
||
| 1421 | } |
||
| 1422 | $tag = ''; |
||
| 1423 | if (isset($attrs['ignore'])) { |
||
| 1424 | $tag .= $attrs['ignore']; |
||
| 1425 | } |
||
| 1426 | if (preg_match('/\$(.+)/', $tag, $match)) { |
||
| 1427 | $tag = $this->vars[$match[1]]['id']; |
||
| 1428 | } |
||
| 1429 | |||
| 1430 | $record = Registry::gedcomRecordFactory()->make($id, $this->tree); |
||
| 1431 | if (empty($attrs['diff']) && !empty($id)) { |
||
| 1432 | $facts = $record->facts([], true); |
||
| 1433 | $this->repeats = []; |
||
| 1434 | $nonfacts = explode(',', $tag); |
||
| 1435 | foreach ($facts as $fact) { |
||
| 1436 | $tag = explode(':', $fact->tag())[1]; |
||
| 1437 | |||
| 1438 | if (!in_array($tag, $nonfacts, true)) { |
||
| 1439 | $this->repeats[] = $fact->gedcom(); |
||
| 1440 | } |
||
| 1441 | } |
||
| 1442 | } else { |
||
| 1443 | foreach ($record->facts() as $fact) { |
||
| 1444 | if (($fact->isPendingAddition() || $fact->isPendingDeletion()) && !str_ends_with($fact->tag(), ':CHAN')) { |
||
| 1445 | $this->repeats[] = $fact->gedcom(); |
||
| 1446 | } |
||
| 1447 | } |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | // Add fact/event for FAM:DIV and for death of spouse |
||
| 1451 | foreach ($this->repeats as $key => $fact) { |
||
| 1452 | $jdarr[$key] = 0; |
||
| 1453 | if (preg_match('/1 FAMS @(.+)@/', $fact, $match)) { |
||
| 1454 | $famid = $match[1]; |
||
| 1455 | $fam = Registry::familyFactory()->make($match[1], $this->tree); |
||
| 1456 | if ($fam === null) { |
||
| 1457 | continue; |
||
| 1458 | } |
||
| 1459 | $dt = $this->getGedcomValue("MARR:DATE", 0, $fam->gedcom()); |
||
| 1460 | if ($dt == "") { |
||
| 1461 | $dt = $this->getGedcomValue("ENGA:DATE", 0, $fam->gedcom()); |
||
| 1462 | } |
||
| 1463 | if ($dt == "" && $this->getGedcomValue("EVEN:TYPE", 0, $fam->gedcom()) == "Sambo") { |
||
| 1464 | $dt = $this->getGedcomValue("EVEN:DATE", 0, $fam->gedcom()); |
||
| 1465 | } |
||
| 1466 | $date = new Date($dt); |
||
| 1467 | $jd = $date->julianDay(); |
||
| 1468 | $jdarr[$key] = $jd; |
||
| 1469 | // Divorce |
||
| 1470 | $dt = $this->getGedcomValue("DIV:DATE", 0, $fam->gedcom()); |
||
| 1471 | if ($dt != "") { |
||
| 1472 | $this->repeats[] = "1 DIV\n2 DATE " . $dt . "\n"; |
||
| 1473 | } |
||
| 1474 | // Separation // Doesn't work!! getGedComValue only reports the first event!! I.e. no match here |
||
| 1475 | if ($this->getGedcomValue("EVEN:TYPE", 0, $fam->gedcom()) == "Separation") { |
||
| 1476 | $dt = $this->getGedcomValue("EVEN:DATE", 0, $fam->gedcom()); |
||
| 1477 | if ($dt != "") { |
||
| 1478 | $this->repeats[] = "1 EVEN\n2 TYPE Separation\n2 DATE " . $dt . "\n"; |
||
| 1479 | } |
||
| 1480 | } |
||
| 1481 | // death of husband / wife |
||
| 1482 | $husb = $fam->husband(); |
||
| 1483 | $wife = $fam->wife(); |
||
| 1484 | if ($this->getGedcomValue("SEX", 0, $this->gedrec) == "M") { |
||
| 1485 | $spouse = $wife; |
||
| 1486 | } else { |
||
| 1487 | $spouse = $husb; |
||
| 1488 | } |
||
| 1489 | if ($spouse) { |
||
| 1490 | $dt = $this->getGedcomValue("DEAT:DATE", 0, $spouse->gedcom()); |
||
| 1491 | } else { |
||
| 1492 | $dt = ""; |
||
| 1493 | } |
||
| 1494 | if ($dt != "") { |
||
| 1495 | $this->repeats[] = "1 _SP_DEAT\n2 DATE " . $dt . "\n2 _O_FAM " . $famid . "\n"; |
||
| 1496 | } |
||
| 1497 | } |
||
| 1498 | } |
||
| 1499 | // Find the dates for the facts that are found |
||
| 1500 | foreach ($this->repeats as $key => $fact) { |
||
| 1501 | if (preg_match('/[234] DATE ([^\n]+)/', $fact, $match)) { |
||
| 1502 | $date = new Date($match[1]); |
||
| 1503 | $jd = $date->julianDay(); |
||
| 1504 | $jdarr[$key] = $jd; |
||
| 1505 | } |
||
| 1506 | } |
||
| 1507 | |||
| 1508 | // Sort facts in chronological order, if possible |
||
| 1509 | $m = count($this->repeats) - 1; |
||
| 1510 | $prevd = 0; |
||
| 1511 | for ($i = 0; $i <= $m; $i++) { // keep undated events after previous dated event |
||
| 1512 | if ($jdarr[$i] === 0) { |
||
| 1513 | $jdarr[$i] = $prevd; |
||
| 1514 | } else { |
||
| 1515 | $prevd = $jdarr[$i]; |
||
| 1516 | } |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | while ($m > 1) { |
||
| 1520 | $n = count($this->repeats); |
||
| 1521 | while ($n > 1) { |
||
| 1522 | if ($jdarr[$n - 2] > $jdarr[$n - 1] && $jdarr[$n - 1] !== 0) { |
||
| 1523 | $s = $this->repeats[$n - 1]; |
||
| 1524 | $this->repeats[$n - 1] = $this->repeats[$n - 2]; |
||
| 1525 | $this->repeats[$n - 2] = $s; |
||
| 1526 | $s = $jdarr[$n - 1]; |
||
| 1527 | $jdarr[$n - 1] = $jdarr[$n - 2]; |
||
| 1528 | $jdarr[$n - 2] = $s; |
||
| 1529 | } |
||
| 1530 | $n -= 1; |
||
| 1531 | } |
||
| 1532 | $m -= 1; |
||
| 1533 | } |
||
| 1534 | |||
| 1535 | // Remove spouse deaths that are too late: after new marriage or own death |
||
| 1536 | $currfam = ""; |
||
| 1537 | for ($i = 0; $i <= count($this->repeats) - 1; $i++) { |
||
| 1538 | if (preg_match('/[1234] FAMS @(.+)@/', $this->repeats[$i], $match)) { |
||
| 1539 | $currfam = $match[1]; |
||
| 1540 | } |
||
| 1541 | if (preg_match('/_SP_DEAT.*\n2 DATE (.*)\n.*_O_FAM (.+)\n/', $this->repeats[$i], $match)) { |
||
| 1542 | if ($currfam != $match[2] || $i == count($this->repeats) - 1) { |
||
| 1543 | $this->repeats[$i] = "1 _XXX\n"; |
||
| 1544 | } // ignore fact |
||
| 1545 | } |
||
| 1546 | } |
||
| 1547 | } |
||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Handle </facts> |
||
| 1551 | * |
||
| 1552 | * @return void |
||
| 1553 | */ |
||
| 1554 | protected function factsEndHandler(): void |
||
| 1555 | { |
||
| 1556 | $this->process_repeats--; |
||
| 1557 | if ($this->process_repeats > 0) { |
||
| 1558 | return; |
||
| 1559 | } |
||
| 1560 | |||
| 1561 | // Check if there is anything to repeat |
||
| 1562 | if (count($this->repeats) > 0) { |
||
| 1563 | $line = xml_get_current_line_number($this->parser) - 1; |
||
| 1564 | $lineoffset = 0; |
||
| 1565 | foreach ($this->repeats_stack as $rep) { |
||
| 1566 | $lineoffset = $lineoffset + $rep[1] - 1; |
||
| 1567 | } |
||
| 1568 | |||
| 1569 | //-- read the xml from the file |
||
| 1570 | $lines = file($this->report); |
||
| 1571 | while ($lineoffset + $this->repeat_bytes > 0 && !str_contains($lines[$lineoffset + $this->repeat_bytes], '<Facts ')) { |
||
| 1572 | $lineoffset--; |
||
| 1573 | } |
||
| 1574 | $lineoffset++; |
||
| 1575 | $reportxml = "<tempdoc>\n"; |
||
| 1576 | $i = $line + $lineoffset; |
||
| 1577 | $line_nr = $this->repeat_bytes + $lineoffset; |
||
| 1578 | while ($line_nr < $i) { |
||
| 1579 | $reportxml .= $lines[$line_nr]; |
||
| 1580 | $line_nr++; |
||
| 1581 | } |
||
| 1582 | // No need to drag this |
||
| 1583 | unset($lines); |
||
| 1584 | $reportxml .= "</tempdoc>\n"; |
||
| 1585 | // Save original values |
||
| 1586 | $this->parser_stack[] = $this->parser; |
||
| 1587 | $oldgedrec = $this->gedrec; |
||
| 1588 | $count = count($this->repeats); |
||
| 1589 | $i = 0; |
||
| 1590 | while ($i < $count) { |
||
| 1591 | if (!isset($this->repeats[$i])) { |
||
| 1592 | $i++; |
||
| 1593 | continue; // this fact has been removed above, occured too late |
||
| 1594 | } |
||
| 1595 | $this->gedrec = $this->repeats[$i]; |
||
| 1596 | $this->fact = ''; |
||
| 1597 | $this->desc = ''; |
||
| 1598 | if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) { |
||
| 1599 | $this->fact = $match[1]; |
||
| 1600 | if ($this->fact === 'EVEN' || $this->fact === 'FACT') { |
||
| 1601 | $tmatch = []; |
||
| 1602 | if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) { |
||
| 1603 | $this->type = trim($tmatch[1]); |
||
| 1604 | } else { |
||
| 1605 | $this->type = ' '; |
||
| 1606 | } |
||
| 1607 | } |
||
| 1608 | $this->desc = trim($match[2]); |
||
| 1609 | $this->desc .= self::getCont(2, $this->gedrec); |
||
| 1610 | } |
||
| 1611 | $repeat_parser = xml_parser_create(); |
||
| 1612 | $this->parser = $repeat_parser; |
||
| 1613 | xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0); |
||
| 1614 | |||
| 1615 | xml_set_element_handler( |
||
| 1616 | $repeat_parser, |
||
| 1617 | function ($parser, string $name, array $attrs): void { |
||
| 1618 | $this->startElement($parser, $name, $attrs); |
||
| 1619 | }, |
||
| 1620 | function ($parser, string $name): void { |
||
| 1621 | $this->endElement($parser, $name); |
||
| 1622 | } |
||
| 1623 | ); |
||
| 1624 | |||
| 1625 | xml_set_character_data_handler( |
||
| 1626 | $repeat_parser, |
||
| 1627 | function ($parser, string $data): void { |
||
| 1628 | $this->characterData($parser, $data); |
||
| 1629 | } |
||
| 1630 | ); |
||
| 1631 | |||
| 1632 | if (!xml_parse($repeat_parser, $reportxml, true)) { |
||
| 1633 | throw new DomainException(sprintf( |
||
| 1634 | 'FactsEHandler XML error: %s at line %d', |
||
| 1635 | xml_error_string(xml_get_error_code($repeat_parser)), |
||
| 1636 | xml_get_current_line_number($repeat_parser) |
||
| 1637 | )); |
||
| 1638 | } |
||
| 1639 | xml_parser_free($repeat_parser); |
||
| 1640 | $i++; |
||
| 1641 | } |
||
| 1642 | // Restore original values |
||
| 1643 | $this->parser = array_pop($this->parser_stack); |
||
| 1644 | $this->gedrec = $oldgedrec; |
||
| 1645 | } |
||
| 1646 | [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); |
||
| 1647 | } |
||
| 1648 | |||
| 1649 | /** |
||
| 1650 | * Setting upp or changing variables in the XML |
||
| 1651 | * The XML variable name and value is stored in $this->vars |
||
| 1652 | * |
||
| 1653 | * @param array<string> $attrs an array of key value pairs for the attributes |
||
| 1654 | * |
||
| 1655 | * @return void |
||
| 1656 | */ |
||
| 1657 | protected function setVarStartHandler(array $attrs): void |
||
| 1658 | { |
||
| 1659 | if (empty($attrs['name'])) { |
||
| 1660 | throw new DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file'); |
||
| 1661 | } |
||
| 1662 | |||
| 1663 | $name = $attrs['name']; |
||
| 1664 | $value = $attrs['value']; |
||
| 1665 | if (isset($attrs['dumpvar'])) { |
||
| 1666 | $dumpvar = $attrs['dumpvar']; |
||
| 1667 | } else { |
||
| 1668 | $dumpvar = ""; |
||
| 1669 | } |
||
| 1670 | $curr_id = ""; |
||
| 1671 | $match = []; |
||
| 1672 | if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { |
||
| 1673 | $curr_id = $match[1]; |
||
| 1674 | } |
||
| 1675 | $match = []; |
||
| 1676 | // Current GEDCOM record strings |
||
| 1677 | if ($value === '@ID') { |
||
| 1678 | if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { |
||
| 1679 | $value = $match[1]; |
||
| 1680 | } |
||
| 1681 | } elseif ($value === '@fact') { |
||
| 1682 | $value = $this->fact; |
||
| 1683 | } elseif ($value === '@desc') { |
||
| 1684 | $value = $this->desc; |
||
| 1685 | } elseif ($value === '@format') { |
||
| 1686 | if (isset($_GET["format"])) { |
||
| 1687 | $value = $_GET["format"]; |
||
| 1688 | } else { |
||
| 1689 | $value = ""; |
||
| 1690 | } |
||
| 1691 | } elseif ($value === '@generation') { |
||
| 1692 | $value = (string) $this->generation; |
||
| 1693 | } elseif ($value === '@base_url') { |
||
| 1694 | $value = ""; |
||
| 1695 | if (array_key_exists("route", $_GET)) { |
||
| 1696 | $value = $_GET["route"]; |
||
| 1697 | } |
||
| 1698 | $i = strpos($value, "%2Freport"); |
||
| 1699 | if ($i === false) { |
||
| 1700 | $i = strpos($value, "/report"); |
||
| 1701 | } |
||
| 1702 | if ($i !== false) { |
||
| 1703 | $value = substr($value, 0, $i); |
||
| 1704 | } |
||
| 1705 | $value = "index.php?route=" . $value; |
||
| 1706 | } elseif ($value === '@relation') { |
||
| 1707 | if (isset($this->mfrelation[$curr_id]) && $curr_id != "") { |
||
| 1708 | $value = (string) $this->mfrelation[$curr_id]; |
||
| 1709 | } else { |
||
| 1710 | $value = ""; |
||
| 1711 | } |
||
| 1712 | } elseif (preg_match("/@(\w+)/", $value, $match)) { |
||
| 1713 | $gmatch = []; |
||
| 1714 | if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) { |
||
| 1715 | $value = str_replace('@', '', trim($gmatch[1])); |
||
| 1716 | } |
||
| 1717 | } elseif (preg_match("/@\\$(\w+)/", $value, $match)) { |
||
| 1718 | if ($match[1] == "dump" && $this->vars['dval']['id'] > 0) { |
||
| 1719 | // if ($this->vars[ 'dval' ]['id'] == 1001) |
||
| 1720 | if ($dumpvar == "gedrec") { |
||
| 1721 | error_log("\n---- setvar start " . date("Y-m-d H:i:s") . " RPG " . __LINE__ . " " . $name . " gedcom=\n" . $this->gedrec . "\n", 3, "my-errors.log"); |
||
| 1722 | } elseif ($dumpvar != "") { |
||
| 1723 | error_log("var: " . $dumpvar . " = " . $this->vars[$dumpvar]['id'] . "\n", 3, "my-errors.log"); |
||
| 1724 | } else { |
||
| 1725 | if (array_key_exists('dval', $this->vars)) { |
||
| 1726 | $nnn = $this->vars['dval']['id']; |
||
| 1727 | } else { |
||
| 1728 | $nnn = 0; |
||
| 1729 | } |
||
| 1730 | error_log("\n---- setvar start " . date("Y-m-d H:i:s") . " RPG " . __LINE__ . " " . $name . " -----\n", 3, "my-errors.log"); |
||
| 1731 | foreach ($this->vars as $key => $val) { |
||
| 1732 | if ($nnn-- < 0) { |
||
| 1733 | error_log($key . "='" . $val['id'] . "'\n", 3, "my-errors.log"); |
||
| 1734 | } |
||
| 1735 | } |
||
| 1736 | } |
||
| 1737 | } |
||
| 1738 | $value = $this->vars[$match[1]]['id']; |
||
| 1739 | if (isset($this->vars[$value]['id'])) { |
||
| 1740 | $value = '$' . $this->vars[$match[1]]['id']; |
||
| 1741 | } else { |
||
| 1742 | $value = "0"; |
||
| 1743 | } |
||
| 1744 | } |
||
| 1745 | if (isset($attrs['trim'])) { |
||
| 1746 | $value = str_replace($attrs['trim'], '', $value); |
||
| 1747 | } |
||
| 1748 | if (preg_match("/\\$(\w+)/", $name, $match)) { |
||
| 1749 | $name = $this->vars["'" . $match[1] . "'"]['id']; |
||
| 1750 | } |
||
| 1751 | $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER); |
||
| 1752 | $i = 0; |
||
| 1753 | while ($i < $count) { |
||
| 1754 | $t = $this->vars[$match[$i][1]]['id']; |
||
| 1755 | $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1); |
||
| 1756 | $i++; |
||
| 1757 | } |
||
| 1758 | if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) { |
||
| 1759 | $value = I18N::number((int) $match[1]); |
||
| 1760 | } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { |
||
| 1761 | $value = I18N::translate($match[1]); |
||
| 1762 | } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { |
||
| 1763 | $value = I18N::translateContext($match[1], $match[2]); |
||
| 1764 | } |
||
| 1765 | if (isset($attrs['lcfirst'])) { // set 1st char to lower case |
||
| 1766 | $value = lcfirst($value); |
||
| 1767 | } |
||
| 1768 | |||
| 1769 | // Arithmetic functions |
||
| 1770 | if (preg_match("/(\d+)\s*([-+*\/])\s*(\d+)/", $value, $match)) { |
||
| 1771 | // Create an expression language with the functions used by our reports. |
||
| 1772 | $expression_provider = new ReportExpressionLanguageProvider(); |
||
| 1773 | $expression_cache = new NullAdapter(); |
||
| 1774 | $expression_language = new ExpressionLanguage($expression_cache, [$expression_provider]); |
||
| 1775 | |||
| 1776 | $value = (string) $expression_language->evaluate($value); |
||
| 1777 | } |
||
| 1778 | |||
| 1779 | if (str_contains($value, '@')) { |
||
| 1780 | $value = ''; |
||
| 1781 | } |
||
| 1782 | $this->vars[$name]['id'] = $value; |
||
| 1783 | if ($name == 'title') { |
||
| 1784 | $this->wt_report->title = $value; |
||
| 1785 | } |
||
| 1786 | } |
||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Handle <if> |
||
| 1790 | * |
||
| 1791 | * @param array<string> $attrs |
||
| 1792 | * |
||
| 1793 | * @return void |
||
| 1794 | */ |
||
| 1795 | protected function ifStartHandler(array $attrs): void |
||
| 1796 | { |
||
| 1797 | if ($this->process_ifs > 0) { |
||
| 1798 | $this->process_ifs++; |
||
| 1799 | |||
| 1800 | return; |
||
| 1801 | } |
||
| 1802 | |||
| 1803 | $condition = $attrs['condition']; |
||
| 1804 | $condition = $this->substituteVars($condition, true); |
||
| 1805 | $condition = str_replace([ |
||
| 1806 | ' LT ', |
||
| 1807 | ' GT ', |
||
| 1808 | ], [ |
||
| 1809 | '<', |
||
| 1810 | '>', |
||
| 1811 | ], $condition); |
||
| 1812 | // Replace the first occurrence only once of @fact:DATE or in any other combinations to the current fact, such as BIRT |
||
| 1813 | $condition = str_replace('@fact:', $this->fact . ':', $condition); |
||
| 1814 | $match = []; |
||
| 1815 | $count = preg_match_all("/@([\w:.]+)/", $condition, $match, PREG_SET_ORDER); |
||
| 1816 | $i = 0; |
||
| 1817 | while ($i < $count) { |
||
| 1818 | $id = $match[$i][1]; |
||
| 1819 | $value = '""'; |
||
| 1820 | if ($id === 'ID') { |
||
| 1821 | if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { |
||
| 1822 | $value = "'" . $match[1] . "'"; |
||
| 1823 | } |
||
| 1824 | } elseif ($id === 'fact') { |
||
| 1825 | $value = '"' . $this->fact . '"'; |
||
| 1826 | } elseif ($id === 'desc') { |
||
| 1827 | $value = '"' . addslashes($this->desc) . '"'; |
||
| 1828 | } elseif ($id === 'generation') { |
||
| 1829 | $value = '"' . $this->generation . '"'; |
||
| 1830 | } else { |
||
| 1831 | $level = (int) explode(' ', trim($this->gedrec))[0]; |
||
| 1832 | if ($level === 0) { |
||
| 1833 | $level++; |
||
| 1834 | } |
||
| 1835 | $value = $this->getGedcomValue($id, $level, $this->gedrec); |
||
| 1836 | if (empty($value)) { |
||
| 1837 | $level++; |
||
| 1838 | $value = $this->getGedcomValue($id, $level, $this->gedrec); |
||
| 1839 | } |
||
| 1840 | $value = preg_replace('/^@(' . Gedcom::REGEX_XREF . ')@$/', '$1', $value); |
||
| 1841 | $value = '"' . addslashes($value) . '"'; |
||
| 1842 | } |
||
| 1843 | $condition = str_replace("@$id", $value, $condition); |
||
| 1844 | $i++; |
||
| 1845 | } |
||
| 1846 | |||
| 1847 | // Create an expression language with the functions used by our reports. |
||
| 1848 | $expression_provider = new ReportExpressionLanguageProvider(); |
||
| 1849 | $expression_cache = new NullAdapter(); |
||
| 1850 | $expression_language = new ExpressionLanguage($expression_cache, [$expression_provider]); |
||
| 1851 | |||
| 1852 | $ret = $expression_language->evaluate($condition); |
||
| 1853 | |||
| 1854 | if (!$ret) { |
||
| 1855 | $this->process_ifs++; |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 | |||
| 1859 | /** |
||
| 1860 | * Handle </if> |
||
| 1861 | * |
||
| 1862 | * @return void |
||
| 1863 | */ |
||
| 1864 | protected function ifEndHandler(): void |
||
| 1865 | { |
||
| 1866 | if ($this->process_ifs > 0) { |
||
| 1867 | $this->process_ifs--; |
||
| 1868 | } |
||
| 1869 | } |
||
| 1870 | |||
| 1871 | /** |
||
| 1872 | * Handle <footnote> |
||
| 1873 | * Collect the Footnote links |
||
| 1874 | * GEDCOM Records that are protected by Privacy setting will be ignored |
||
| 1875 | * |
||
| 1876 | * @param array<string> $attrs |
||
| 1877 | * |
||
| 1878 | * @return void |
||
| 1879 | */ |
||
| 1880 | protected function footnoteStartHandler(array $attrs): void |
||
| 1881 | { |
||
| 1882 | $id = ''; |
||
| 1883 | if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) { |
||
| 1884 | $id = $match[2]; |
||
| 1885 | } |
||
| 1886 | $record = Registry::gedcomRecordFactory()->make($id, $this->tree); |
||
| 1887 | if ($record && $record->canShow()) { |
||
| 1888 | $this->print_data_stack[] = $this->print_data; |
||
| 1889 | $this->print_data = true; |
||
| 1890 | $style = ''; |
||
| 1891 | if (!empty($attrs['style'])) { |
||
| 1892 | $style = $attrs['style']; |
||
| 1893 | } |
||
| 1894 | $this->footnote_element = $this->current_element; |
||
| 1895 | $this->current_element = $this->report_root->createFootnote($style); |
||
| 1896 | } else { |
||
| 1897 | $this->print_data = false; |
||
| 1898 | $this->process_footnote = false; |
||
| 1899 | } |
||
| 1900 | } |
||
| 1901 | |||
| 1902 | /** |
||
| 1903 | * Handle </footnote> |
||
| 1904 | * Print the collected Footnote data |
||
| 1905 | * |
||
| 1906 | * @return void |
||
| 1907 | */ |
||
| 1908 | protected function footnoteEndHandler(): void |
||
| 1909 | { |
||
| 1910 | if ($this->process_footnote) { |
||
| 1911 | $this->print_data = array_pop($this->print_data_stack); |
||
| 1912 | $temp = trim($this->current_element->getValue()); |
||
| 1913 | if (strlen($temp) > 3) { |
||
| 1914 | $this->wt_report->addElement($this->current_element); |
||
| 1915 | } |
||
| 1916 | $this->current_element = $this->footnote_element; |
||
| 1917 | } else { |
||
| 1918 | $this->process_footnote = true; |
||
| 1919 | } |
||
| 1920 | } |
||
| 1921 | |||
| 1922 | /** |
||
| 1923 | * Handle <footnoteTexts /> |
||
| 1924 | * |
||
| 1925 | * @return void |
||
| 1926 | */ |
||
| 1927 | protected function footnoteTextsStartHandler(): void |
||
| 1928 | { |
||
| 1929 | $temp = 'footnotetexts'; |
||
| 1930 | $this->wt_report->addElement($temp); |
||
| 1931 | } |
||
| 1932 | |||
| 1933 | /** |
||
| 1934 | * XML element Forced line break handler - HTML code |
||
| 1935 | * |
||
| 1936 | * @return void |
||
| 1937 | */ |
||
| 1938 | protected function brStartHandler(): void |
||
| 1939 | { |
||
| 1940 | if ($this->print_data && $this->process_gedcoms === 0) { |
||
| 1941 | $this->current_element->addText('<br>'); |
||
| 1942 | } |
||
| 1943 | } |
||
| 1944 | |||
| 1945 | /** |
||
| 1946 | * Handle <sp /> |
||
| 1947 | * Forced space |
||
| 1948 | * |
||
| 1949 | * @return void |
||
| 1950 | */ |
||
| 1951 | protected function spStartHandler(): void |
||
| 1952 | { |
||
| 1953 | if ($this->print_data && $this->process_gedcoms === 0) { |
||
| 1954 | $this->current_element->addText(' '); |
||
| 1955 | } |
||
| 1956 | } |
||
| 1957 | |||
| 1958 | /** |
||
| 1959 | * Handle <highlightedImage /> |
||
| 1960 | * |
||
| 1961 | * @param array<string> $attrs |
||
| 1962 | * |
||
| 1963 | * @return void |
||
| 1964 | */ |
||
| 1965 | protected function highlightedImageStartHandler(array $attrs): void |
||
| 1966 | { |
||
| 1967 | $id = ''; |
||
| 1968 | if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { |
||
| 1969 | $id = $match[1]; |
||
| 1970 | } |
||
| 1971 | |||
| 1972 | // Position the top corner of this box on the page |
||
| 1973 | $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION); |
||
| 1974 | |||
| 1975 | // Position the left corner of this box on the page |
||
| 1976 | $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION); |
||
| 1977 | |||
| 1978 | // string Align the image in left, center, right (or empty to use x/y position). |
||
| 1979 | $align = $attrs['align'] ?? ''; |
||
| 1980 | |||
| 1981 | // string Next Line should be T:next to the image, N:next line |
||
| 1982 | $ln = $attrs['ln'] ?? 'T'; |
||
| 1983 | |||
| 1984 | // Width, height (or both). |
||
| 1985 | $width = (float) ($attrs['width'] ?? 0.0); |
||
| 1986 | $height = (float) ($attrs['height'] ?? 0.0); |
||
| 1987 | |||
| 1988 | $person = Registry::individualFactory()->make($id, $this->tree); |
||
| 1989 | $media_file = $person->findHighlightedMediaFile(); |
||
| 1990 | |||
| 1991 | if ($media_file instanceof MediaFile && $media_file->fileExists()) { |
||
| 1992 | $image = imagecreatefromstring($media_file->fileContents()); |
||
| 1993 | $attributes = [imagesx($image), imagesy($image)]; |
||
| 1994 | |||
| 1995 | if ($width > 0 && $height == 0) { |
||
| 1996 | $perc = $width / $attributes[0]; |
||
| 1997 | $height = round($attributes[1] * $perc); |
||
| 1998 | } elseif ($height > 0 && $width == 0) { |
||
| 1999 | $perc = $height / $attributes[1]; |
||
| 2000 | $width = round($attributes[0] * $perc); |
||
| 2001 | } else { |
||
| 2002 | $width = (float) $attributes[0]; |
||
| 2003 | $height = (float) $attributes[1]; |
||
| 2004 | } |
||
| 2005 | $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln); |
||
| 2006 | $this->wt_report->addElement($image); |
||
| 2007 | } |
||
| 2008 | } |
||
| 2009 | |||
| 2010 | /** |
||
| 2011 | * Handle <image/> |
||
| 2012 | * |
||
| 2013 | * @param array<string> $attrs |
||
| 2014 | * |
||
| 2015 | * @return void |
||
| 2016 | */ |
||
| 2017 | protected function imageStartHandler(array $attrs): void |
||
| 2018 | { |
||
| 2019 | // Position the top corner of this box on the page. the default is the current position |
||
| 2020 | $top = (float) ($attrs['top'] ?? ReportBaseElement::CURRENT_POSITION); |
||
| 2021 | |||
| 2022 | // mixed Position the left corner of this box on the page. the default is the current position |
||
| 2023 | $left = (float) ($attrs['left'] ?? ReportBaseElement::CURRENT_POSITION); |
||
| 2024 | |||
| 2025 | // string Align the image in left, center, right (or empty to use x/y position). |
||
| 2026 | $align = $attrs['align'] ?? ''; |
||
| 2027 | |||
| 2028 | // string Next Line should be T:next to the image, N:next line |
||
| 2029 | $ln = $attrs['ln'] ?? 'T'; |
||
| 2030 | |||
| 2031 | // Width, height (or both). |
||
| 2032 | $width = (float) ($attrs['width'] ?? 0.0); |
||
| 2033 | $height = (float) ($attrs['height'] ?? 0.0); |
||
| 2034 | |||
| 2035 | $file = $attrs['file'] ?? ''; |
||
| 2036 | |||
| 2037 | if ($file === '@FILE') { |
||
| 2038 | $match = []; |
||
| 2039 | if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) { |
||
| 2040 | $mediaobject = Registry::mediaFactory()->make($match[1], $this->tree); |
||
| 2041 | $media_file = $mediaobject->firstImageFile(); |
||
| 2042 | |||
| 2043 | if ($media_file instanceof MediaFile && $media_file->fileExists()) { |
||
| 2044 | $image = imagecreatefromstring($media_file->fileContents()); |
||
| 2045 | $attributes = [imagesx($image), imagesy($image)]; |
||
| 2046 | |||
| 2047 | if ($width > 0 && $height == 0) { |
||
| 2048 | $perc = $width / $attributes[0]; |
||
| 2049 | $height = round($attributes[1] * $perc); |
||
| 2050 | } elseif ($height > 0 && $width == 0) { |
||
| 2051 | $perc = $height / $attributes[1]; |
||
| 2052 | $width = round($attributes[0] * $perc); |
||
| 2053 | } else { |
||
| 2054 | $width = (float) $attributes[0]; |
||
| 2055 | $height = (float) $attributes[1]; |
||
| 2056 | } |
||
| 2057 | $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln); |
||
| 2058 | $this->wt_report->addElement($image); |
||
| 2059 | } |
||
| 2060 | } |
||
| 2061 | } else { |
||
| 2062 | if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) { |
||
| 2063 | $size = getimagesize($file); |
||
| 2064 | if ($width > 0 && $height == 0) { |
||
| 2065 | $perc = $width / $size[0]; |
||
| 2066 | $height = round($size[1] * $perc); |
||
| 2067 | } elseif ($height > 0 && $width == 0) { |
||
| 2068 | $perc = $height / $size[1]; |
||
| 2069 | $width = round($size[0] * $perc); |
||
| 2070 | } else { |
||
| 2071 | $width = $size[0]; |
||
| 2072 | $height = $size[1]; |
||
| 2073 | } |
||
| 2074 | $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln); |
||
| 2075 | $this->wt_report->addElement($image); |
||
| 2076 | } |
||
| 2077 | } |
||
| 2078 | } |
||
| 2079 | |||
| 2080 | /** |
||
| 2081 | * Handle <line> |
||
| 2082 | * |
||
| 2083 | * @param array<string> $attrs |
||
| 2084 | * |
||
| 2085 | * @return void |
||
| 2086 | */ |
||
| 2087 | protected function lineStartHandler(array $attrs): void |
||
| 2088 | { |
||
| 2089 | // Start horizontal position, current position (default) |
||
| 2090 | $x1 = ReportBaseElement::CURRENT_POSITION; |
||
| 2091 | if (isset($attrs['x1'])) { |
||
| 2092 | if ($attrs['x1'] === '0') { |
||
| 2093 | $x1 = 0; |
||
| 2094 | } elseif ($attrs['x1'] === '.') { |
||
| 2095 | $x1 = ReportBaseElement::CURRENT_POSITION; |
||
| 2096 | } elseif (!empty($attrs['x1'])) { |
||
| 2097 | $x1 = (float) $attrs['x1']; |
||
| 2098 | } |
||
| 2099 | } |
||
| 2100 | // Start vertical position, current position (default) |
||
| 2101 | $y1 = ReportBaseElement::CURRENT_POSITION; |
||
| 2102 | if (isset($attrs['y1'])) { |
||
| 2103 | if ($attrs['y1'] === '0') { |
||
| 2104 | $y1 = 0; |
||
| 2105 | } elseif ($attrs['y1'] === '.') { |
||
| 2106 | $y1 = ReportBaseElement::CURRENT_POSITION; |
||
| 2107 | } elseif (!empty($attrs['y1'])) { |
||
| 2108 | $y1 = (float) $attrs['y1']; |
||
| 2109 | } |
||
| 2110 | } |
||
| 2111 | // End horizontal position, maximum width (default) |
||
| 2112 | $x2 = ReportBaseElement::CURRENT_POSITION; |
||
| 2113 | if (isset($attrs['x2'])) { |
||
| 2114 | if ($attrs['x2'] === '0') { |
||
| 2115 | $x2 = 0; |
||
| 2116 | } elseif ($attrs['x2'] === '.') { |
||
| 2117 | $x2 = ReportBaseElement::CURRENT_POSITION; |
||
| 2118 | } elseif (!empty($attrs['x2'])) { |
||
| 2119 | $x2 = (float) $attrs['x2']; |
||
| 2120 | } |
||
| 2121 | } |
||
| 2122 | // End vertical position |
||
| 2123 | $y2 = ReportBaseElement::CURRENT_POSITION; |
||
| 2124 | if (isset($attrs['y2'])) { |
||
| 2125 | if ($attrs['y2'] === '0') { |
||
| 2126 | $y2 = 0; |
||
| 2127 | } elseif ($attrs['y2'] === '.') { |
||
| 2128 | $y2 = ReportBaseElement::CURRENT_POSITION; |
||
| 2129 | } elseif (!empty($attrs['y2'])) { |
||
| 2130 | $y2 = (float) $attrs['y2']; |
||
| 2131 | } |
||
| 2132 | } |
||
| 2133 | |||
| 2134 | $line = $this->report_root->createLine($x1, $y1, $x2, $y2); |
||
| 2135 | $this->wt_report->addElement($line); |
||
| 2136 | } |
||
| 2137 | |||
| 2138 | /** |
||
| 2139 | * Handle <list> |
||
| 2140 | * |
||
| 2141 | * @param array<string> $attrs |
||
| 2142 | * |
||
| 2143 | * @return void |
||
| 2144 | */ |
||
| 2145 | protected function listStartHandler(array $attrs): void |
||
| 2146 | { |
||
| 2147 | $this->process_repeats++; |
||
| 2148 | if ($this->process_repeats > 1) { |
||
| 2149 | return; |
||
| 2150 | } |
||
| 2151 | |||
| 2152 | $match = []; |
||
| 2153 | if (isset($attrs['sortby'])) { |
||
| 2154 | $sortby = $attrs['sortby']; |
||
| 2155 | if (preg_match("/\\$(\w+)/", $sortby, $match)) { |
||
| 2156 | $sortby = $this->vars[$match[1]]['id']; |
||
| 2157 | $sortby = trim($sortby); |
||
| 2158 | } |
||
| 2159 | } else { |
||
| 2160 | $sortby = 'NAME'; |
||
| 2161 | } |
||
| 2162 | |||
| 2163 | $listname = $attrs['list'] ?? 'individual'; |
||
| 2164 | |||
| 2165 | // Some filters/sorts can be applied using SQL, while others require PHP |
||
| 2166 | switch ($listname) { |
||
| 2167 | case 'pending': |
||
| 2168 | $this->list = DB::table('change') |
||
| 2169 | ->whereIn('change_id', function (Builder $query): void { |
||
| 2170 | $query->select([new Expression('MAX(change_id)')]) |
||
| 2171 | ->from('change') |
||
| 2172 | ->where('gedcom_id', '=', $this->tree->id()) |
||
| 2173 | ->where('status', '=', 'pending') |
||
| 2174 | ->groupBy(['xref']); |
||
| 2175 | }) |
||
| 2176 | ->get() |
||
| 2177 | ->map(fn (object $row): ?GedcomRecord => Registry::gedcomRecordFactory()->make($row->xref, $this->tree, $row->new_gedcom ?: $row->old_gedcom)) |
||
| 2178 | ->filter() |
||
| 2179 | ->all(); |
||
| 2180 | break; |
||
| 2181 | |||
| 2182 | case 'individual': |
||
| 2183 | $query = DB::table('individuals') |
||
| 2184 | ->where('i_file', '=', $this->tree->id()) |
||
| 2185 | ->select(['i_id AS xref', 'i_gedcom AS gedcom']) |
||
| 2186 | ->distinct(); |
||
| 2187 | |||
| 2188 | foreach ($attrs as $attr => $value) { |
||
| 2189 | if (str_starts_with($attr, 'filter') && $value !== '') { |
||
| 2190 | $value = $this->substituteVars($value, false); |
||
| 2191 | // Convert the various filters into SQL |
||
| 2192 | if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) { |
||
| 2193 | $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void { |
||
| 2194 | $join |
||
| 2195 | ->on($attr . '.d_gid', '=', 'i_id') |
||
| 2196 | ->on($attr . '.d_file', '=', 'i_file'); |
||
| 2197 | }); |
||
| 2198 | |||
| 2199 | $query->where($attr . '.d_fact', '=', $match[1]); |
||
| 2200 | |||
| 2201 | $date = new Date($match[3]); |
||
| 2202 | |||
| 2203 | if ($match[2] === 'LTE') { |
||
| 2204 | $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay()); |
||
| 2205 | } else { |
||
| 2206 | $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay()); |
||
| 2207 | } |
||
| 2208 | |||
| 2209 | // This filter has been fully processed |
||
| 2210 | unset($attrs[$attr]); |
||
| 2211 | } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) { |
||
| 2212 | $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void { |
||
| 2213 | $join |
||
| 2214 | ->on($attr . '.n_id', '=', 'i_id') |
||
| 2215 | ->on($attr . '.n_file', '=', 'i_file'); |
||
| 2216 | }); |
||
| 2217 | // Search the DB only if there is any name supplied |
||
| 2218 | $names = explode(' ', $match[1]); |
||
| 2219 | foreach ($names as $name) { |
||
| 2220 | $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%'); |
||
| 2221 | } |
||
| 2222 | |||
| 2223 | // This filter has been fully processed |
||
| 2224 | unset($attrs[$attr]); |
||
| 2225 | } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) { |
||
| 2226 | // Convert newline escape sequences to actual new lines |
||
| 2227 | $match[1] = str_replace('\n', "\n", $match[1]); |
||
| 2228 | |||
| 2229 | $query->where('i_gedcom', 'LIKE', $match[1]); |
||
| 2230 | |||
| 2231 | // This filter has been fully processed |
||
| 2232 | unset($attrs[$attr]); |
||
| 2233 | } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) { |
||
| 2234 | // Don't unset this filter. This is just initial filtering for performance |
||
| 2235 | $query |
||
| 2236 | ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void { |
||
| 2237 | $join |
||
| 2238 | ->on($attr . 'a.pl_file', '=', 'i_file') |
||
| 2239 | ->on($attr . 'a.pl_gid', '=', 'i_id'); |
||
| 2240 | }) |
||
| 2241 | ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void { |
||
| 2242 | $join |
||
| 2243 | ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file') |
||
| 2244 | ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id'); |
||
| 2245 | }) |
||
| 2246 | ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%'); |
||
| 2247 | } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) { |
||
| 2248 | // Don't unset this filter. This is just initial filtering for performance |
||
| 2249 | $match[3] = strtr($match[3], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); |
||
| 2250 | $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%'; |
||
| 2251 | $query->where('i_gedcom', 'LIKE', $like); |
||
| 2252 | } elseif (preg_match('/^(\w+) CONTAINS (.*)$/', $value, $match)) { |
||
| 2253 | // Don't unset this filter. This is just initial filtering for performance |
||
| 2254 | $match[2] = strtr($match[2], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); |
||
| 2255 | $like = "%\n1 " . $match[1] . '%' . $match[2] . '%'; |
||
| 2256 | $query->where('i_gedcom', 'LIKE', $like); |
||
| 2257 | } |
||
| 2258 | } |
||
| 2259 | } |
||
| 2260 | |||
| 2261 | $this->list = []; |
||
| 2262 | |||
| 2263 | foreach ($query->get() as $row) { |
||
| 2264 | $this->list[$row->xref] = Registry::individualFactory()->make($row->xref, $this->tree, $row->gedcom); |
||
| 2265 | } |
||
| 2266 | break; |
||
| 2267 | |||
| 2268 | case 'family': |
||
| 2269 | $query = DB::table('families') |
||
| 2270 | ->where('f_file', '=', $this->tree->id()) |
||
| 2271 | ->select(['f_id AS xref', 'f_gedcom AS gedcom']) |
||
| 2272 | ->distinct(); |
||
| 2273 | |||
| 2274 | foreach ($attrs as $attr => $value) { |
||
| 2275 | if (str_starts_with($attr, 'filter') && $value !== '') { |
||
| 2276 | $value = $this->substituteVars($value, false); |
||
| 2277 | // Convert the various filters into SQL |
||
| 2278 | if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) { |
||
| 2279 | $query->join('dates AS ' . $attr, static function (JoinClause $join) use ($attr): void { |
||
| 2280 | $join |
||
| 2281 | ->on($attr . '.d_gid', '=', 'f_id') |
||
| 2282 | ->on($attr . '.d_file', '=', 'f_file'); |
||
| 2283 | }); |
||
| 2284 | |||
| 2285 | $query->where($attr . '.d_fact', '=', $match[1]); |
||
| 2286 | |||
| 2287 | $date = new Date($match[3]); |
||
| 2288 | |||
| 2289 | if ($match[2] === 'LTE') { |
||
| 2290 | $query->where($attr . '.d_julianday2', '<=', $date->maximumJulianDay()); |
||
| 2291 | } else { |
||
| 2292 | $query->where($attr . '.d_julianday1', '>=', $date->minimumJulianDay()); |
||
| 2293 | } |
||
| 2294 | |||
| 2295 | // This filter has been fully processed |
||
| 2296 | unset($attrs[$attr]); |
||
| 2297 | } elseif (preg_match('/^LIKE \/(.+)\/$/', $value, $match)) { |
||
| 2298 | // Convert newline escape sequences to actual new lines |
||
| 2299 | $match[1] = str_replace('\n', "\n", $match[1]); |
||
| 2300 | |||
| 2301 | $query->where('f_gedcom', 'LIKE', $match[1]); |
||
| 2302 | |||
| 2303 | // This filter has been fully processed |
||
| 2304 | unset($attrs[$attr]); |
||
| 2305 | } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) { |
||
| 2306 | if ($sortby === 'NAME' || $match[1] !== '') { |
||
| 2307 | $query->join('name AS ' . $attr, static function (JoinClause $join) use ($attr): void { |
||
| 2308 | $join |
||
| 2309 | ->on($attr . '.n_file', '=', 'f_file') |
||
| 2310 | ->where(static function (Builder $query): void { |
||
| 2311 | $query |
||
| 2312 | ->whereColumn('n_id', '=', 'f_husb') |
||
| 2313 | ->orWhereColumn('n_id', '=', 'f_wife'); |
||
| 2314 | }); |
||
| 2315 | }); |
||
| 2316 | // Search the DB only if there is any name supplied |
||
| 2317 | if ($match[1] != '') { |
||
| 2318 | $names = explode(' ', $match[1]); |
||
| 2319 | foreach ($names as $name) { |
||
| 2320 | $query->where($attr . '.n_full', 'LIKE', '%' . addcslashes($name, '\\%_') . '%'); |
||
| 2321 | } |
||
| 2322 | } |
||
| 2323 | } |
||
| 2324 | |||
| 2325 | // This filter has been fully processed |
||
| 2326 | unset($attrs[$attr]); |
||
| 2327 | } elseif (preg_match('/^(?:\w*):PLAC CONTAINS (.+)$/', $value, $match)) { |
||
| 2328 | // Don't unset this filter. This is just initial filtering for performance |
||
| 2329 | $query |
||
| 2330 | ->join('placelinks AS ' . $attr . 'a', static function (JoinClause $join) use ($attr): void { |
||
| 2331 | $join |
||
| 2332 | ->on($attr . 'a.pl_file', '=', 'f_file') |
||
| 2333 | ->on($attr . 'a.pl_gid', '=', 'f_id'); |
||
| 2334 | }) |
||
| 2335 | ->join('places AS ' . $attr . 'b', static function (JoinClause $join) use ($attr): void { |
||
| 2336 | $join |
||
| 2337 | ->on($attr . 'b.p_file', '=', $attr . 'a.pl_file') |
||
| 2338 | ->on($attr . 'b.p_id', '=', $attr . 'a.pl_p_id'); |
||
| 2339 | }) |
||
| 2340 | ->where($attr . 'b.p_place', 'LIKE', '%' . addcslashes($match[1], '\\%_') . '%'); |
||
| 2341 | } elseif (preg_match('/^(\w*):(\w+) CONTAINS (.+)$/', $value, $match)) { |
||
| 2342 | // Don't unset this filter. This is just initial filtering for performance |
||
| 2343 | $match[3] = strtr($match[3], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); |
||
| 2344 | $like = "%\n1 " . $match[1] . "%\n2 " . $match[2] . '%' . $match[3] . '%'; |
||
| 2345 | $query->where('f_gedcom', 'LIKE', $like); |
||
| 2346 | } elseif (preg_match('/^(\w+) CONTAINS (.+)$/', $value, $match)) { |
||
| 2347 | // Don't unset this filter. This is just initial filtering for performance |
||
| 2348 | $match[2] = strtr($match[2], ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); |
||
| 2349 | $like = "%\n1 " . $match[1] . '%' . $match[2] . '%'; |
||
| 2350 | $query->where('f_gedcom', 'LIKE', $like); |
||
| 2351 | } |
||
| 2352 | } |
||
| 2353 | } |
||
| 2354 | |||
| 2355 | $this->list = []; |
||
| 2356 | |||
| 2357 | foreach ($query->get() as $row) { |
||
| 2358 | $this->list[$row->xref] = Registry::familyFactory()->make($row->xref, $this->tree, $row->gedcom); |
||
| 2359 | } |
||
| 2360 | break; |
||
| 2361 | |||
| 2362 | default: |
||
| 2363 | throw new DomainException('Invalid list name: ' . $listname); |
||
| 2364 | } |
||
| 2365 | |||
| 2366 | $filters = []; |
||
| 2367 | $filters2 = []; |
||
| 2368 | if (isset($attrs['filter1']) && count($this->list) > 0) { |
||
| 2369 | foreach ($attrs as $key => $value) { |
||
| 2370 | if (preg_match("/filter(\d)/", $key)) { |
||
| 2371 | $condition = $value; |
||
| 2372 | if (preg_match("/@(\w+)/", $condition, $match)) { |
||
| 2373 | $id = $match[1]; |
||
| 2374 | $value = "''"; |
||
| 2375 | if ($id === 'ID') { |
||
| 2376 | if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { |
||
| 2377 | $value = "'" . $match[1] . "'"; |
||
| 2378 | } |
||
| 2379 | } elseif ($id === 'fact') { |
||
| 2380 | $value = "'" . $this->fact . "'"; |
||
| 2381 | } elseif ($id === 'desc') { |
||
| 2382 | $value = "'" . $this->desc . "'"; |
||
| 2383 | } else { |
||
| 2384 | if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) { |
||
| 2385 | $value = "'" . str_replace('@', '', trim($match[1])) . "'"; |
||
| 2386 | } |
||
| 2387 | } |
||
| 2388 | $condition = preg_replace("/@$id/", $value, $condition); |
||
| 2389 | } |
||
| 2390 | //-- handle regular expressions |
||
| 2391 | if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) { |
||
| 2392 | $tag = trim($match[1]); |
||
| 2393 | $expr = trim($match[2]); |
||
| 2394 | $val = trim($match[3]); |
||
| 2395 | if (preg_match("/\\$(\w+)/", $val, $match)) { |
||
| 2396 | $val = $this->vars[$match[1]]['id']; |
||
| 2397 | $val = trim($val); |
||
| 2398 | } |
||
| 2399 | if ($val !== '') { |
||
| 2400 | $searchstr = ''; |
||
| 2401 | $tags = explode(':', $tag); |
||
| 2402 | //-- only limit to a level number if we are specifically looking at a level |
||
| 2403 | if (count($tags) > 1) { |
||
| 2404 | $level = 1; |
||
| 2405 | $t = 'XXXX'; |
||
| 2406 | foreach ($tags as $t) { |
||
| 2407 | if (!empty($searchstr)) { |
||
| 2408 | $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n"; |
||
| 2409 | } |
||
| 2410 | //-- search for both EMAIL and _EMAIL... silly double gedcom standard |
||
| 2411 | if ($t === 'EMAIL' || $t === '_EMAIL') { |
||
| 2412 | $t = '_?EMAIL'; |
||
| 2413 | } |
||
| 2414 | $searchstr .= $level . ' ' . $t; |
||
| 2415 | $level++; |
||
| 2416 | } |
||
| 2417 | } else { |
||
| 2418 | if ($tag === 'EMAIL' || $tag === '_EMAIL') { |
||
| 2419 | $tag = '_?EMAIL'; |
||
| 2420 | } |
||
| 2421 | $t = $tag; |
||
| 2422 | $searchstr = '1 ' . $tag; |
||
| 2423 | } |
||
| 2424 | switch ($expr) { |
||
| 2425 | case 'CONTAINS': |
||
| 2426 | if ($t === 'PLAC') { |
||
| 2427 | $searchstr .= "[^\n]*[, ]*" . $val; |
||
| 2428 | } else { |
||
| 2429 | $searchstr .= "[^\n]*" . $val; |
||
| 2430 | } |
||
| 2431 | $filters[] = $searchstr; |
||
| 2432 | break; |
||
| 2433 | default: |
||
| 2434 | $filters2[] = [ |
||
| 2435 | 'tag' => $tag, |
||
| 2436 | 'expr' => $expr, |
||
| 2437 | 'val' => $val, |
||
| 2438 | ]; |
||
| 2439 | break; |
||
| 2440 | } |
||
| 2441 | } |
||
| 2442 | } |
||
| 2443 | } |
||
| 2444 | } |
||
| 2445 | } |
||
| 2446 | //-- apply other filters to the list that could not be added to the search string |
||
| 2447 | if ($filters !== []) { |
||
| 2448 | foreach ($this->list as $key => $record) { |
||
| 2449 | foreach ($filters as $filter) { |
||
| 2450 | if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) { |
||
| 2451 | unset($this->list[$key]); |
||
| 2452 | break; |
||
| 2453 | } |
||
| 2454 | } |
||
| 2455 | } |
||
| 2456 | } |
||
| 2457 | if ($filters2 !== []) { |
||
| 2458 | $mylist = []; |
||
| 2459 | foreach ($this->list as $indi) { |
||
| 2460 | $key = $indi->xref(); |
||
| 2461 | $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree)); |
||
| 2462 | $keep = true; |
||
| 2463 | foreach ($filters2 as $filter) { |
||
| 2464 | if ($keep) { |
||
| 2465 | $tag = $filter['tag']; |
||
| 2466 | $expr = $filter['expr']; |
||
| 2467 | $val = $filter['val']; |
||
| 2468 | if ($val === "''") { |
||
| 2469 | $val = ''; |
||
| 2470 | } |
||
| 2471 | $tags = explode(':', $tag); |
||
| 2472 | $t = end($tags); |
||
| 2473 | $v = $this->getGedcomValue($tag, 1, $grec); |
||
| 2474 | //-- check for EMAIL and _EMAIL (silly double gedcom standard :P) |
||
| 2475 | if ($t === 'EMAIL' && empty($v)) { |
||
| 2476 | $tag = str_replace('EMAIL', '_EMAIL', $tag); |
||
| 2477 | $tags = explode(':', $tag); |
||
| 2478 | $t = end($tags); |
||
| 2479 | $v = self::getSubRecord(1, $tag, $grec); |
||
| 2480 | } |
||
| 2481 | |||
| 2482 | switch ($expr) { |
||
| 2483 | case 'GTE': |
||
| 2484 | if ($t === 'DATE') { |
||
| 2485 | $date1 = new Date($v); |
||
| 2486 | $date2 = new Date($val); |
||
| 2487 | $keep = (Date::compare($date1, $date2) >= 0); |
||
| 2488 | } elseif ($val >= $v) { |
||
| 2489 | $keep = true; |
||
| 2490 | } |
||
| 2491 | break; |
||
| 2492 | case 'LTE': |
||
| 2493 | if ($t === 'DATE') { |
||
| 2494 | $date1 = new Date($v); |
||
| 2495 | $date2 = new Date($val); |
||
| 2496 | $keep = (Date::compare($date1, $date2) <= 0); |
||
| 2497 | } elseif ($val >= $v) { |
||
| 2498 | $keep = true; |
||
| 2499 | } |
||
| 2500 | break; |
||
| 2501 | default: |
||
| 2502 | if ($v == $val) { |
||
| 2503 | $keep = true; |
||
| 2504 | } else { |
||
| 2505 | $keep = false; |
||
| 2506 | } |
||
| 2507 | break; |
||
| 2508 | } |
||
| 2509 | } |
||
| 2510 | } |
||
| 2511 | if ($keep) { |
||
| 2512 | $mylist[$key] = $indi; |
||
| 2513 | } |
||
| 2514 | } |
||
| 2515 | $this->list = $mylist; |
||
| 2516 | } |
||
| 2517 | |||
| 2518 | switch ($sortby) { |
||
| 2519 | case 'NAME': |
||
| 2520 | uasort($this->list, GedcomRecord::nameComparator()); |
||
| 2521 | break; |
||
| 2522 | case 'CHAN': |
||
| 2523 | uasort($this->list, GedcomRecord::lastChangeComparator()); |
||
| 2524 | break; |
||
| 2525 | case 'BIRT:DATE': |
||
| 2526 | uasort($this->list, Individual::birthDateComparator()); |
||
| 2527 | break; |
||
| 2528 | case 'DEAT:DATE': |
||
| 2529 | uasort($this->list, Individual::deathDateComparator()); |
||
| 2530 | break; |
||
| 2531 | case 'MARR:DATE': |
||
| 2532 | uasort($this->list, Family::marriageDateComparator()); |
||
| 2533 | break; |
||
| 2534 | default: |
||
| 2535 | // unsorted or already sorted by SQL |
||
| 2536 | break; |
||
| 2537 | } |
||
| 2538 | |||
| 2539 | $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; |
||
| 2540 | $this->repeat_bytes = xml_get_current_line_number($this->parser) + 1; |
||
| 2541 | } |
||
| 2542 | |||
| 2543 | /** |
||
| 2544 | * Handle </list> |
||
| 2545 | * |
||
| 2546 | * @return void |
||
| 2547 | */ |
||
| 2548 | protected function listEndHandler(): void |
||
| 2549 | { |
||
| 2550 | $this->process_repeats--; |
||
| 2551 | if ($this->process_repeats > 0) { |
||
| 2552 | return; |
||
| 2553 | } |
||
| 2554 | |||
| 2555 | // Check if there is any list |
||
| 2556 | if (count($this->list) > 0) { |
||
| 2557 | $lineoffset = 0; |
||
| 2558 | foreach ($this->repeats_stack as $rep) { |
||
| 2559 | $lineoffset = $lineoffset + $rep[1] - 1; |
||
| 2560 | } |
||
| 2561 | //-- read the xml from the file |
||
| 2562 | $lines = file($this->report); |
||
| 2563 | while ((!str_contains($lines[$lineoffset + $this->repeat_bytes], '<List')) && (($lineoffset + $this->repeat_bytes) > 0)) { |
||
| 2564 | $lineoffset--; |
||
| 2565 | } |
||
| 2566 | $lineoffset++; |
||
| 2567 | $reportxml = "<tempdoc>\n"; |
||
| 2568 | $line_nr = $lineoffset + $this->repeat_bytes; |
||
| 2569 | // List Level counter |
||
| 2570 | $count = 1; |
||
| 2571 | while (0 < $count) { |
||
| 2572 | if (str_contains($lines[$line_nr], '<List')) { |
||
| 2573 | $count++; |
||
| 2574 | } elseif (str_contains($lines[$line_nr], '</List')) { |
||
| 2575 | $count--; |
||
| 2576 | } |
||
| 2577 | if (0 < $count) { |
||
| 2578 | $reportxml .= $lines[$line_nr]; |
||
| 2579 | } |
||
| 2580 | $line_nr++; |
||
| 2581 | } |
||
| 2582 | // No need to drag this |
||
| 2583 | unset($lines); |
||
| 2584 | $reportxml .= '</tempdoc>'; |
||
| 2585 | // Save original values |
||
| 2586 | $this->parser_stack[] = $this->parser; |
||
| 2587 | $oldgedrec = $this->gedrec; |
||
| 2588 | |||
| 2589 | $this->list_total = count($this->list); |
||
| 2590 | $this->list_private = 0; |
||
| 2591 | foreach ($this->list as $record) { |
||
| 2592 | if ($record->canShow()) { |
||
| 2593 | $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->tree())); |
||
| 2594 | //-- start the sax parser |
||
| 2595 | $repeat_parser = xml_parser_create(); |
||
| 2596 | $this->parser = $repeat_parser; |
||
| 2597 | xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0); |
||
| 2598 | |||
| 2599 | xml_set_element_handler( |
||
| 2600 | $repeat_parser, |
||
| 2601 | function ($parser, string $name, array $attrs): void { |
||
| 2602 | $this->startElement($parser, $name, $attrs); |
||
| 2603 | }, |
||
| 2604 | function ($parser, string $name): void { |
||
| 2605 | $this->endElement($parser, $name); |
||
| 2606 | } |
||
| 2607 | ); |
||
| 2608 | |||
| 2609 | xml_set_character_data_handler( |
||
| 2610 | $repeat_parser, |
||
| 2611 | function ($parser, string $data): void { |
||
| 2612 | $this->characterData($parser, $data); |
||
| 2613 | } |
||
| 2614 | ); |
||
| 2615 | |||
| 2616 | if (!xml_parse($repeat_parser, $reportxml, true)) { |
||
| 2617 | throw new DomainException(sprintf( |
||
| 2618 | 'ListEHandler XML error: %s at line %d', |
||
| 2619 | xml_error_string(xml_get_error_code($repeat_parser)), |
||
| 2620 | xml_get_current_line_number($repeat_parser) |
||
| 2621 | )); |
||
| 2622 | } |
||
| 2623 | xml_parser_free($repeat_parser); |
||
| 2624 | } else { |
||
| 2625 | $this->list_private++; |
||
| 2626 | } |
||
| 2627 | } |
||
| 2628 | $this->list = []; |
||
| 2629 | $this->parser = array_pop($this->parser_stack); |
||
| 2630 | $this->gedrec = $oldgedrec; |
||
| 2631 | } |
||
| 2632 | [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); |
||
| 2633 | } |
||
| 2634 | |||
| 2635 | /** |
||
| 2636 | * Handle <listTotal> |
||
| 2637 | * Prints the total number of records in a list |
||
| 2638 | * The total number is collected from <list> and <relatives> |
||
| 2639 | * |
||
| 2640 | * @return void |
||
| 2641 | */ |
||
| 2642 | protected function listTotalStartHandler(): void |
||
| 2643 | { |
||
| 2644 | if ($this->list_private == 0) { |
||
| 2645 | $this->current_element->addText((string) $this->list_total); |
||
| 2646 | } else { |
||
| 2647 | $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total); |
||
| 2648 | } |
||
| 2649 | } |
||
| 2650 | |||
| 2651 | /** |
||
| 2652 | * Handle <relatives> |
||
| 2653 | * |
||
| 2654 | * @param array<string> $attrs |
||
| 2655 | * |
||
| 2656 | * @return void |
||
| 2657 | */ |
||
| 2658 | protected function relativesStartHandler(array $attrs): void |
||
| 2659 | { |
||
| 2660 | $this->process_repeats++; |
||
| 2661 | if ($this->process_repeats > 1) { |
||
| 2662 | return; |
||
| 2663 | } |
||
| 2664 | |||
| 2665 | $sortby = $attrs['sortby'] ?? 'NAME'; |
||
| 2666 | |||
| 2667 | $match = []; |
||
| 2668 | if (preg_match("/\\$(\w+)/", $sortby, $match)) { |
||
| 2669 | $sortby = $this->vars[$match[1]]['id']; |
||
| 2670 | $sortby = trim($sortby); |
||
| 2671 | } |
||
| 2672 | |||
| 2673 | $maxgen = -1; |
||
| 2674 | if (isset($attrs['maxgen'])) { |
||
| 2675 | $maxgen = (int) $attrs['maxgen']; |
||
| 2676 | } |
||
| 2677 | |||
| 2678 | $group = $attrs['group'] ?? 'child-family'; |
||
| 2679 | |||
| 2680 | if (preg_match("/\\$(\w+)/", $group, $match)) { |
||
| 2681 | $group = $this->vars[$match[1]]['id']; |
||
| 2682 | $group = trim($group); |
||
| 2683 | } |
||
| 2684 | |||
| 2685 | $id = $attrs['id'] ?? ''; |
||
| 2686 | |||
| 2687 | if (preg_match("/\\$(\w+)/", $id, $match)) { |
||
| 2688 | $id = $this->vars[$match[1]]['id']; |
||
| 2689 | $id = trim($id); |
||
| 2690 | } |
||
| 2691 | |||
| 2692 | $this->list = []; |
||
| 2693 | $person = Registry::individualFactory()->make($id, $this->tree); |
||
| 2694 | if ($person instanceof Individual) { |
||
| 2695 | $this->list[$id] = $person; |
||
| 2696 | $this->mfrelation[$id] = ""; |
||
| 2697 | $nam = $person->getAllNames()[0]['fullNN']; |
||
| 2698 | switch ($group) { |
||
| 2699 | case 'child-family': |
||
| 2700 | foreach ($person->childFamilies() as $family) { |
||
| 2701 | foreach ($family->spouses() as $spouse) { |
||
| 2702 | $this->list[$spouse->xref()] = $spouse; |
||
| 2703 | } |
||
| 2704 | |||
| 2705 | foreach ($family->children() as $child) { |
||
| 2706 | $this->list[$child->xref()] = $child; |
||
| 2707 | } |
||
| 2708 | } |
||
| 2709 | break; |
||
| 2710 | case 'spouse-family': |
||
| 2711 | foreach ($person->spouseFamilies() as $family) { |
||
| 2712 | foreach ($family->spouses() as $spouse) { |
||
| 2713 | $this->list[$spouse->xref()] = $spouse; |
||
| 2714 | } |
||
| 2715 | |||
| 2716 | foreach ($family->children() as $child) { |
||
| 2717 | $this->list[$child->xref()] = $child; |
||
| 2718 | } |
||
| 2719 | } |
||
| 2720 | break; |
||
| 2721 | case 'direct-ancestors': |
||
| 2722 | $this->addAncestors($this->list, $id, false, $maxgen); |
||
| 2723 | break; |
||
| 2724 | case 'ancestors': |
||
| 2725 | $this->addAncestors($this->list, $id, true, $maxgen); |
||
| 2726 | break; |
||
| 2727 | case 'descendants': |
||
| 2728 | $this->list[$id]->generation = 1; |
||
| 2729 | $this->addDescendancy($this->list, $id, false, $maxgen); |
||
| 2730 | break; |
||
| 2731 | case 'all': |
||
| 2732 | $this->addAncestors($this->list, $id, true, $maxgen); |
||
| 2733 | $this->addDescendancy($this->list, $id, true, $maxgen); |
||
| 2734 | break; |
||
| 2735 | } |
||
| 2736 | } |
||
| 2737 | |||
| 2738 | switch ($sortby) { |
||
| 2739 | case 'NAME': |
||
| 2740 | uasort($this->list, GedcomRecord::nameComparator()); |
||
| 2741 | break; |
||
| 2742 | case 'BIRT:DATE': |
||
| 2743 | uasort($this->list, Individual::birthDateComparator()); |
||
| 2744 | break; |
||
| 2745 | case 'DEAT:DATE': |
||
| 2746 | uasort($this->list, Individual::deathDateComparator()); |
||
| 2747 | break; |
||
| 2748 | case 'generation': |
||
| 2749 | $newarray = []; |
||
| 2750 | reset($this->list); |
||
| 2751 | $genCounter = 1; |
||
| 2752 | while (count($newarray) < count($this->list)) { |
||
| 2753 | foreach ($this->list as $key => $value) { |
||
| 2754 | if ($value->generation < 0) { |
||
| 2755 | // indication of husband or wife |
||
| 2756 | $this->generation = -$value->generation; |
||
| 2757 | } else { |
||
| 2758 | $this->generation = $value->generation; |
||
| 2759 | } |
||
| 2760 | if ($this->generation == $genCounter) { |
||
| 2761 | $newarray[$key] = (object) ['generation' => $this->generation]; |
||
| 2762 | } |
||
| 2763 | } |
||
| 2764 | $genCounter++; |
||
| 2765 | } |
||
| 2766 | $this->list = $newarray; |
||
| 2767 | break; |
||
| 2768 | default: |
||
| 2769 | // unsorted |
||
| 2770 | break; |
||
| 2771 | } |
||
| 2772 | $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; |
||
| 2773 | $this->repeat_bytes = xml_get_current_line_number($this->parser) + 1; |
||
| 2774 | } |
||
| 2775 | |||
| 2776 | /** |
||
| 2777 | * Handle </relatives> |
||
| 2778 | * |
||
| 2779 | * @return void |
||
| 2780 | */ |
||
| 2781 | protected function relativesEndHandler(): void |
||
| 2782 | { |
||
| 2783 | $this->process_repeats--; |
||
| 2784 | if ($this->process_repeats > 0) { |
||
| 2785 | return; |
||
| 2786 | } |
||
| 2787 | |||
| 2788 | // Check if there is any relatives |
||
| 2789 | if (count($this->list) > 0) { |
||
| 2790 | $lineoffset = 0; |
||
| 2791 | foreach ($this->repeats_stack as $rep) { |
||
| 2792 | $lineoffset = $lineoffset + $rep[1] - 1; |
||
| 2793 | } |
||
| 2794 | //-- read the xml from the file |
||
| 2795 | $lines = file($this->report); |
||
| 2796 | while (!str_contains($lines[$lineoffset + $this->repeat_bytes], '<Relatives') && $lineoffset + $this->repeat_bytes > 0) { |
||
| 2797 | $lineoffset--; |
||
| 2798 | } |
||
| 2799 | $lineoffset++; |
||
| 2800 | $reportxml = "<tempdoc>\n"; |
||
| 2801 | $line_nr = $lineoffset + $this->repeat_bytes; |
||
| 2802 | // Relatives Level counter |
||
| 2803 | $count = 1; |
||
| 2804 | while (0 < $count) { |
||
| 2805 | if (str_contains($lines[$line_nr], '<Relatives')) { |
||
| 2806 | $count++; |
||
| 2807 | } elseif (str_contains($lines[$line_nr], '</Relatives')) { |
||
| 2808 | $count--; |
||
| 2809 | } |
||
| 2810 | if (0 < $count) { |
||
| 2811 | $reportxml .= $lines[$line_nr]; |
||
| 2812 | } |
||
| 2813 | $line_nr++; |
||
| 2814 | } |
||
| 2815 | // No need to drag this |
||
| 2816 | unset($lines); |
||
| 2817 | $reportxml .= "</tempdoc>\n"; |
||
| 2818 | // Save original values |
||
| 2819 | $this->parser_stack[] = $this->parser; |
||
| 2820 | $oldgedrec = $this->gedrec; |
||
| 2821 | |||
| 2822 | $this->list_total = count($this->list); |
||
| 2823 | $this->list_private = 0; |
||
| 2824 | foreach ($this->list as $key => $value) { |
||
| 2825 | if (isset($value->generation)) { |
||
| 2826 | $this->generation = $value->generation; |
||
| 2827 | } |
||
| 2828 | $xref = $key; |
||
| 2829 | $this->vars["dupl"]["id"] = "no"; |
||
| 2830 | if (substr($key, 0, 2) == "D_") { |
||
| 2831 | $xref = substr($key, strrpos($key, "_") + 1); |
||
| 2832 | $this->vars["dupl"]["id"] = "yes"; |
||
| 2833 | } |
||
| 2834 | $tmp = Registry::gedcomRecordFactory()->make((string) $xref, $this->tree); |
||
| 2835 | $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree)); |
||
| 2836 | |||
| 2837 | $repeat_parser = xml_parser_create(); |
||
| 2838 | $this->parser = $repeat_parser; |
||
| 2839 | xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, 0); |
||
| 2840 | |||
| 2841 | xml_set_element_handler( |
||
| 2842 | $repeat_parser, |
||
| 2843 | function ($parser, string $name, array $attrs): void { |
||
| 2844 | $this->startElement($parser, $name, $attrs); |
||
| 2845 | }, |
||
| 2846 | function ($parser, string $name): void { |
||
| 2847 | $this->endElement($parser, $name); |
||
| 2848 | } |
||
| 2849 | ); |
||
| 2850 | |||
| 2851 | xml_set_character_data_handler( |
||
| 2852 | $repeat_parser, |
||
| 2853 | function ($parser, string $data): void { |
||
| 2854 | $this->characterData($parser, $data); |
||
| 2855 | } |
||
| 2856 | ); |
||
| 2857 | |||
| 2858 | if (!xml_parse($repeat_parser, $reportxml, true)) { |
||
| 2859 | 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))); |
||
| 2860 | } |
||
| 2861 | xml_parser_free($repeat_parser); |
||
| 2862 | } |
||
| 2863 | // Clean up the list array |
||
| 2864 | $this->list = []; |
||
| 2865 | $this->parser = array_pop($this->parser_stack); |
||
| 2866 | $this->gedrec = $oldgedrec; |
||
| 2867 | } |
||
| 2868 | [$this->repeats, $this->repeat_bytes] = array_pop($this->repeats_stack); |
||
| 2869 | } |
||
| 2870 | |||
| 2871 | /** |
||
| 2872 | * Handle <generation /> |
||
| 2873 | * Prints the number of generations |
||
| 2874 | * |
||
| 2875 | * @return void |
||
| 2876 | */ |
||
| 2877 | protected function generationStartHandler(): void |
||
| 2878 | { |
||
| 2879 | $this->current_element->addText((string) $this->generation); |
||
| 2880 | } |
||
| 2881 | |||
| 2882 | /** |
||
| 2883 | * Handle <newPage /> |
||
| 2884 | * Has to be placed in an element (header, body or footer) |
||
| 2885 | * |
||
| 2886 | * @return void |
||
| 2887 | */ |
||
| 2888 | protected function newPageStartHandler(): void |
||
| 2889 | { |
||
| 2890 | $temp = 'addpage'; |
||
| 2891 | $this->wt_report->addElement($temp); |
||
| 2892 | } |
||
| 2893 | |||
| 2894 | /** |
||
| 2895 | * Handle </title> |
||
| 2896 | * |
||
| 2897 | * @return void |
||
| 2898 | */ |
||
| 2899 | protected function titleEndHandler(): void |
||
| 2900 | { |
||
| 2901 | $this->report_root->addTitle($this->text); |
||
| 2902 | } |
||
| 2903 | |||
| 2904 | /** |
||
| 2905 | * Handle </description> |
||
| 2906 | * |
||
| 2907 | * @return void |
||
| 2908 | */ |
||
| 2909 | protected function descriptionEndHandler(): void |
||
| 2912 | } |
||
| 2913 | |||
| 2914 | /** |
||
| 2915 | * Create a list of all descendants. |
||
| 2916 | * |
||
| 2917 | * @param array<Individual> $list |
||
| 2918 | * @param string $pid |
||
| 2919 | * @param bool $parents |
||
| 2920 | * @param int $generations |
||
| 2921 | * |
||
| 2922 | * @return void |
||
| 2923 | */ |
||
| 2924 | private function addDescendancy(&$list, $pid, $parents = false, $generations = -1): void |
||
| 2925 | { |
||
| 2926 | $person = Registry::individualFactory()->make($pid, $this->tree); |
||
| 2927 | if ($person === null) { |
||
| 2928 | return; |
||
| 2929 | } |
||
| 2930 | |||
| 2931 | static $focusperson = true; |
||
| 2932 | static $dupl = 1; |
||
| 2933 | $sx = $person->sex(); |
||
| 2934 | $rl = "x"; // unknown |
||
| 2935 | if ($sx == "M") { |
||
| 2936 | $rl = "s"; |
||
| 2937 | } // son |
||
| 2938 | if ($sx == "F") { |
||
| 2939 | $rl = "d"; |
||
| 2940 | } // daughter |
||
| 2941 | if ($focusperson) { |
||
| 2942 | $this->mfrelation[$pid] = ""; |
||
| 2943 | } |
||
| 2944 | $nam = $person->getAllNames()[0]['fullNN']; |
||
| 2945 | |||
| 2946 | $newpid = $pid; |
||
| 2947 | if (!isset($list[$pid])) { |
||
| 2948 | $list[$pid] = $person; |
||
| 2949 | } elseif (!$focusperson) { |
||
| 2950 | $newpid = "D_" . $dupl . "_" . $pid; |
||
| 2951 | $list[$newpid] = $person; |
||
| 2952 | } |
||
| 2953 | if (!isset($list[$newpid]->generation)) { |
||
| 2954 | $list[$newpid]->generation = 0; |
||
| 2955 | } |
||
| 2956 | $focusperson = false; |
||
| 2957 | foreach ($person->spouseFamilies() as $family) { |
||
| 2958 | if ($parents) { |
||
| 2959 | $husband = $family->husband(); |
||
| 2960 | $wife = $family->wife(); |
||
| 2961 | if ($husband) { |
||
| 2962 | $list[$husband->xref()] = $husband; |
||
| 2963 | if (isset($list[$pid]->generation)) { |
||
| 2964 | $list[$husband->xref()]->generation = $list[$pid]->generation - 1; |
||
| 2965 | } else { |
||
| 2966 | $list[$husband->xref()]->generation = 1; |
||
| 2967 | } |
||
| 2968 | } |
||
| 2969 | if ($wife) { |
||
| 2970 | $list[$wife->xref()] = $wife; |
||
| 2971 | if (isset($list[$pid]->generation)) { |
||
| 2972 | $list[$wife->xref()]->generation = $list[$pid]->generation - 1; |
||
| 2973 | } else { |
||
| 2974 | $list[$wife->xref()]->generation = 1; |
||
| 2975 | } |
||
| 2976 | } |
||
| 2977 | } |
||
| 2978 | $husband = $family->husband(); |
||
| 2979 | $wife = $family->wife(); |
||
| 2980 | |||
| 2981 | if ($husband && $wife) { |
||
| 2982 | if ($husband->xref() == $person->xref()) { |
||
| 2983 | $this->mfrelation[$wife->xref()] = $this->mfrelation[$person->xref()] . "x"; |
||
| 2984 | if ($wife->canShow()) { |
||
| 2985 | $list[$wife->xref()] = $wife; |
||
| 2986 | } |
||
| 2987 | if (!isset($wife->generation)) { |
||
| 2988 | $wife->generation = $person->generation; |
||
| 2989 | } |
||
| 2990 | $nam = $wife->getAllNames()[0]['fullNN']; |
||
| 2991 | } else { |
||
| 2992 | $this->mfrelation[$husband->xref()] = $this->mfrelation[$person->xref()] . "x"; |
||
| 2993 | if ($husband->canShow()) { |
||
| 2994 | $list[$husband->xref()] = $husband; |
||
| 2995 | } |
||
| 2996 | if (!isset($husband->generation)) { |
||
| 2997 | $husband->generation = $person->generation; |
||
| 2998 | } |
||
| 2999 | $nam = $husband->getAllNames()[0]['fullNN']; |
||
| 3000 | } |
||
| 3001 | } |
||
| 3002 | |||
| 3003 | $children = $family->children(); |
||
| 3004 | foreach ($children as $child) { |
||
| 3005 | if ($child) { |
||
| 3006 | $sx = $child->sex(); |
||
| 3007 | $rl = "x"; // unknown |
||
| 3008 | if ($sx == "M") { |
||
| 3009 | $rl = "s"; |
||
| 3010 | } // son |
||
| 3011 | if ($sx == "F") { |
||
| 3012 | $rl = "d"; |
||
| 3013 | } // daughter |
||
| 3014 | $rl = $this->mfrelation[$person->xref()] . $rl; |
||
| 3015 | $this->mfrelation[$child->xref()] = $rl; |
||
| 3016 | if (isset($list[$pid]->generation)) { |
||
| 3017 | $child->generation = $list[$pid]->generation + 1; |
||
| 3018 | } else { |
||
| 3019 | $child->generation = 2; |
||
| 3020 | } |
||
| 3021 | } |
||
| 3022 | } |
||
| 3023 | if ($generations == -1 || $list[$pid]->generation < $generations) { |
||
| 3024 | foreach ($children as $child) { |
||
| 3025 | if ($child->canShow()) { |
||
| 3026 | $this->addDescendancy($list, $child->xref(), $parents, $generations); |
||
| 3027 | } // recurse on the childs family |
||
| 3028 | } |
||
| 3029 | } |
||
| 3030 | } |
||
| 3031 | $focusperson = false; |
||
| 3032 | } |
||
| 3033 | |||
| 3034 | /** |
||
| 3035 | * Create a list of all ancestors. |
||
| 3036 | * |
||
| 3037 | * @param array<Individual> $list |
||
| 3038 | * @param string $pid |
||
| 3039 | * @param bool $children |
||
| 3040 | * @param int $generations |
||
| 3041 | * |
||
| 3042 | * @return void |
||
| 3043 | */ |
||
| 3044 | private function addAncestors(array &$list, string $pid, bool $children = false, int $generations = -1): void |
||
| 3084 | } |
||
| 3085 | } |
||
| 3086 | } |
||
| 3087 | } |
||
| 3088 | } |
||
| 3089 | } |
||
| 3090 | |||
| 3091 | /** |
||
| 3092 | * get gedcom tag value |
||
| 3093 | * |
||
| 3094 | * @param string $tag The tag to find, use : to delineate subtags |
||
| 3095 | * @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 |
||
| 3096 | * @param string $gedrec The gedcom record to get the value from |
||
| 3097 | * |
||
| 3098 | * @return string the value of a gedcom tag from the given gedcom record |
||
| 3099 | */ |
||
| 3100 | private function getGedcomValue(string $tag, int $level, string $gedrec): string |
||
| 3174 | } |
||
| 3175 | |||
| 3176 | /** |
||
| 3177 | * Replace variable identifiers with their values. |
||
| 3178 | * |
||
| 3179 | * @param string $expression An expression such as "$foo == 123" |
||
| 3180 | * @param bool $quote Whether to add quotation marks |
||
| 3181 | * |
||
| 3182 | * @return string |
||
| 3183 | */ |
||
| 3184 | private function substituteVars($expression, $quote): string |
||
| 3202 | ); |
||
| 3203 | } |
||
| 3204 | } |
||
| 3205 |