| Conditions | 113 |
| Paths | > 20000 |
| Total Lines | 388 |
| Code Lines | 285 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 96 | public static function printFact(Fact $fact, GedcomRecord $record): void |
||
| 97 | { |
||
| 98 | $parent = $fact->record(); |
||
| 99 | $tree = $parent->tree(); |
||
| 100 | $tag = $fact->getTag(); |
||
| 101 | $label = $fact->label(); |
||
| 102 | $value = $fact->value(); |
||
| 103 | $type = $fact->attribute('TYPE'); |
||
| 104 | $id = $fact->id(); |
||
| 105 | |||
| 106 | // This preference is named HIDE instead of SHOW |
||
| 107 | $hide_errors = $tree->getPreference('HIDE_GEDCOM_ERRORS') === '0'; |
||
| 108 | |||
| 109 | // Some facts don't get printed here ... |
||
| 110 | switch ($tag) { |
||
| 111 | case 'NOTE': |
||
| 112 | self::printMainNotes($fact, 1); |
||
| 113 | |||
| 114 | return; |
||
| 115 | case 'SOUR': |
||
| 116 | self::printMainSources($fact, 1); |
||
| 117 | |||
| 118 | return; |
||
| 119 | case 'OBJE': |
||
| 120 | self::printMainMedia($fact, 1); |
||
| 121 | |||
| 122 | return; |
||
| 123 | case 'FAMC': |
||
| 124 | case 'FAMS': |
||
| 125 | case 'CHIL': |
||
| 126 | case 'HUSB': |
||
| 127 | case 'WIFE': |
||
| 128 | // These are internal links, not facts |
||
| 129 | return; |
||
| 130 | case '_WT_OBJE_SORT': |
||
| 131 | // These links were once used internally to record the sort order. |
||
| 132 | return; |
||
| 133 | default: |
||
| 134 | // Hide unrecognized/custom tags? |
||
| 135 | if ($hide_errors && !GedcomTag::isTag($tag)) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | |||
| 141 | // New or deleted facts need different styling |
||
| 142 | $styles = []; |
||
| 143 | if ($fact->isPendingAddition()) { |
||
| 144 | $styles[] = 'wt-new'; |
||
| 145 | } |
||
| 146 | if ($fact->isPendingDeletion()) { |
||
| 147 | $styles[] = 'wt-old'; |
||
| 148 | } |
||
| 149 | |||
| 150 | // Event of close relative |
||
| 151 | if ($tag === 'EVEN' && $value === 'CLOSE_RELATIVE') { |
||
| 152 | $styles[] = 'wt-relation-fact collapse'; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Event of close associates |
||
| 156 | if ($id === 'asso') { |
||
| 157 | $styles[] = 'wt-relation-fact collapse'; |
||
| 158 | } |
||
| 159 | |||
| 160 | // historical facts |
||
| 161 | if ($id === 'histo') { |
||
| 162 | $styles[] = 'wt-historic-fact collapse'; |
||
| 163 | } |
||
| 164 | |||
| 165 | // Use marriage type as the label. |
||
| 166 | if ($tag === 'MARR' && $type !== '') { |
||
| 167 | switch (strtoupper($type)) { |
||
| 168 | case 'CIVIL': |
||
| 169 | $label = I18N::translate('Civil marriage'); |
||
| 170 | $type = ''; // Do not print this again |
||
| 171 | break; |
||
| 172 | case 'PARTNERS': |
||
| 173 | $label = I18N::translate('Registered partnership'); |
||
| 174 | $type = ''; // Do not print this again |
||
| 175 | break; |
||
| 176 | case 'RELIGIOUS': |
||
| 177 | $label = I18N::translate('Religious marriage'); |
||
| 178 | $type = ''; // Do not print this again |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | echo '<tr class="', implode(' ', $styles), '">'; |
||
| 184 | echo '<th scope="row">'; |
||
| 185 | echo $label; |
||
| 186 | |||
| 187 | if ($id !== 'histo' && $id !== 'asso' && $fact->canEdit()) { |
||
| 188 | echo '<div class="editfacts nowrap">'; |
||
| 189 | echo view('edit/icon-fact-edit', ['fact' => $fact]); |
||
| 190 | echo view('edit/icon-fact-copy', ['fact' => $fact]); |
||
| 191 | echo view('edit/icon-fact-delete', ['fact' => $fact]); |
||
| 192 | echo '</div>'; |
||
| 193 | } |
||
| 194 | |||
| 195 | if ($tree->getPreference('SHOW_FACT_ICONS')) { |
||
| 196 | echo '<span class="wt-fact-icon wt-fact-icon-' . $tag . '" title="' . strip_tags($label) . '"></span>'; |
||
| 197 | } |
||
| 198 | |||
| 199 | echo '</th>'; |
||
| 200 | echo '<td>'; |
||
| 201 | |||
| 202 | // Event from another record? |
||
| 203 | if ($parent !== $record) { |
||
| 204 | if ($parent instanceof Family) { |
||
| 205 | foreach ($parent->spouses() as $spouse) { |
||
| 206 | if ($record !== $spouse) { |
||
| 207 | echo '<a href="', e($spouse->url()), '">', $spouse->fullName(), '</a> — '; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | echo '<a href="', e($parent->url()), '">', I18N::translate('View this family'), '</a><br>'; |
||
| 211 | } elseif ($parent instanceof Individual) { |
||
| 212 | echo '<a href="', e($parent->url()), '">', $parent->fullName(), '</a><br>'; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | // Print the value of this fact/event |
||
| 217 | switch ($tag) { |
||
| 218 | case 'ADDR': |
||
| 219 | echo e($value); |
||
| 220 | break; |
||
| 221 | case 'AFN': |
||
| 222 | echo '<div class="field"><a href="https://familysearch.org/search/tree/results#count=20&query=afn:', rawurlencode($value), '">', e($value), '</a></div>'; |
||
| 223 | break; |
||
| 224 | case 'ASSO': |
||
| 225 | // we handle this later, in format_asso_rela_record() |
||
| 226 | break; |
||
| 227 | case 'EMAIL': |
||
| 228 | case 'EMAI': |
||
| 229 | case '_EMAIL': |
||
| 230 | echo '<div class="field"><a href="mailto:', e($value), '">', e($value), '</a></div>'; |
||
| 231 | break; |
||
| 232 | case 'LANG': |
||
| 233 | echo GedcomCodeLang::getValue($value); |
||
| 234 | break; |
||
| 235 | case 'RESN': |
||
| 236 | echo '<div class="field">'; |
||
| 237 | switch ($value) { |
||
| 238 | case 'none': |
||
| 239 | // Note: "1 RESN none" is not valid gedcom. |
||
| 240 | // However, webtrees privacy rules will interpret it as "show an otherwise private record to public". |
||
| 241 | echo '<i class="icon-resn-none"></i> ', I18N::translate('Show to visitors'); |
||
| 242 | break; |
||
| 243 | case 'privacy': |
||
| 244 | echo '<i class="icon-class-none"></i> ', I18N::translate('Show to members'); |
||
| 245 | break; |
||
| 246 | case 'confidential': |
||
| 247 | echo '<i class="icon-confidential-none"></i> ', I18N::translate('Show to managers'); |
||
| 248 | break; |
||
| 249 | case 'locked': |
||
| 250 | echo '<i class="icon-locked-none"></i> ', I18N::translate('Only managers can edit'); |
||
| 251 | break; |
||
| 252 | default: |
||
| 253 | echo e($value); |
||
| 254 | break; |
||
| 255 | } |
||
| 256 | echo '</div>'; |
||
| 257 | break; |
||
| 258 | case 'PUBL': // Publication details might contain URLs. |
||
| 259 | echo '<div class="field">', Filter::expandUrls($value, $tree), '</div>'; |
||
| 260 | break; |
||
| 261 | case 'REPO': |
||
| 262 | $repository = $fact->target(); |
||
| 263 | if ($repository instanceof Repository) { |
||
| 264 | echo '<div><a class="field" href="', e($repository->url()), '">', $repository->fullName(), '</a></div>'; |
||
| 265 | } else { |
||
| 266 | echo '<div class="error">', e($value), '</div>'; |
||
| 267 | } |
||
| 268 | break; |
||
| 269 | case 'SUBM': |
||
| 270 | $submitter = $fact->target(); |
||
| 271 | if ($submitter instanceof Submitter) { |
||
| 272 | echo '<div><a class="field" href="', e($submitter->url()), '">', $submitter->fullName(), '</a></div>'; |
||
| 273 | } else { |
||
| 274 | echo '<div class="error">', e($value), '</div>'; |
||
| 275 | } |
||
| 276 | break; |
||
| 277 | case 'SUBN': |
||
| 278 | $submission = $fact->target(); |
||
| 279 | if ($submission instanceof Submission) { |
||
| 280 | echo '<div><a class="field" href="', e($submission->url()), '">', $submission->fullName(), '</a></div>'; |
||
| 281 | } else { |
||
| 282 | echo '<div class="error">', e($value), '</div>'; |
||
| 283 | } |
||
| 284 | break; |
||
| 285 | case 'URL': |
||
| 286 | case '_URL': |
||
| 287 | case 'WWW': |
||
| 288 | echo '<div class="field"><a href="', e($value), '">', e($value), '</a></div>'; |
||
| 289 | break; |
||
| 290 | case 'TEXT': // 0 SOUR / 1 TEXT |
||
| 291 | echo Filter::formatText($value, $tree); |
||
| 292 | break; |
||
| 293 | case '_GOV': |
||
| 294 | echo '<div class="field"><a href="https://gov.genealogy.net/item/show/', e($value), '">', e($value), '</a></div>'; |
||
| 295 | break; |
||
| 296 | default: |
||
| 297 | // Display the value for all other facts/events |
||
| 298 | switch ($value) { |
||
| 299 | case '': |
||
| 300 | case 'CLOSE_RELATIVE': |
||
| 301 | // Nothing to display |
||
| 302 | break; |
||
| 303 | case 'N': |
||
| 304 | // Not valid GEDCOM |
||
| 305 | echo '<div class="field">', I18N::translate('No'), '</div>'; |
||
| 306 | break; |
||
| 307 | case 'Y': |
||
| 308 | // Do not display "Yes". |
||
| 309 | break; |
||
| 310 | default: |
||
| 311 | if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match)) { |
||
| 312 | $target = $fact->target(); |
||
| 313 | if ($target instanceof GedcomRecord) { |
||
| 314 | echo '<div><a href="', e($target->url()), '">', $target->fullName(), '</a></div>'; |
||
| 315 | } else { |
||
| 316 | echo '<div class="error">', e($value), '</div>'; |
||
| 317 | } |
||
| 318 | } else { |
||
| 319 | echo '<div class="field"><span dir="auto">', e($value), '</span></div>'; |
||
| 320 | } |
||
| 321 | break; |
||
| 322 | } |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | |||
| 326 | // Print the type of this fact/event |
||
| 327 | if ($type !== '' && $tag !== 'EVEN' && $tag !== 'FACT') { |
||
| 328 | // Allow (custom) translations for other types |
||
| 329 | $type = I18N::translate($type); |
||
| 330 | echo GedcomTag::getLabelValue('TYPE', e($type)); |
||
| 331 | } |
||
| 332 | |||
| 333 | // Print the date of this fact/event |
||
| 334 | echo FunctionsPrint::formatFactDate($fact, $record, true, true); |
||
| 335 | |||
| 336 | // Print the place of this fact/event |
||
| 337 | echo '<div class="place">', FunctionsPrint::formatFactPlace($fact, true, true, true), '</div>'; |
||
| 338 | // A blank line between the primary attributes (value, date, place) and the secondary ones |
||
| 339 | echo '<br>'; |
||
| 340 | |||
| 341 | $addr = $fact->attribute('ADDR'); |
||
| 342 | if ($addr !== '') { |
||
| 343 | echo GedcomTag::getLabelValue($record::RECORD_TYPE . ':' . $fact->getTag() . ':ADDR', $addr); |
||
| 344 | } |
||
| 345 | |||
| 346 | // Print the associates of this fact/event |
||
| 347 | if ($id !== 'asso') { |
||
| 348 | echo self::formatAssociateRelationship($fact); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Print any other "2 XXXX" attributes, in the order in which they appear. |
||
| 352 | preg_match_all('/\n2 (' . Gedcom::REGEX_TAG . ') (.+)/', $fact->gedcom(), $matches, PREG_SET_ORDER); |
||
| 353 | |||
| 354 | //0 SOUR / 1 DATA / 2 EVEN / 3 DATE and 3 PLAC must be collected separately |
||
| 355 | preg_match_all('/\n2 EVEN .*((\n[3].*)*)/', $fact->gedcom(), $evenMatches, PREG_SET_ORDER); |
||
| 356 | $currentEvenMatch = 0; |
||
| 357 | |||
| 358 | foreach ($matches as $match) { |
||
| 359 | switch ($match[1]) { |
||
| 360 | case 'DATE': |
||
| 361 | case 'TIME': |
||
| 362 | case 'AGE': |
||
| 363 | case 'PLAC': |
||
| 364 | case 'ADDR': |
||
| 365 | case 'ALIA': |
||
| 366 | case 'ASSO': |
||
| 367 | case '_ASSO': |
||
| 368 | case 'DESC': |
||
| 369 | case 'RELA': |
||
| 370 | case 'STAT': |
||
| 371 | case 'TEMP': |
||
| 372 | case 'TYPE': |
||
| 373 | case 'FAMS': |
||
| 374 | case 'CONT': |
||
| 375 | // These were already shown at the beginning |
||
| 376 | break; |
||
| 377 | case 'NOTE': |
||
| 378 | case 'OBJE': |
||
| 379 | case 'SOUR': |
||
| 380 | // These will be shown at the end |
||
| 381 | break; |
||
| 382 | case '_UID': |
||
| 383 | case 'RIN': |
||
| 384 | // These don't belong at level 2, so do not display them. |
||
| 385 | // They are only shown when editing. |
||
| 386 | break; |
||
| 387 | case 'EVEN': // 0 SOUR / 1 DATA / 2 EVEN / 3 DATE / 3 PLAC |
||
| 388 | $events = []; |
||
| 389 | foreach (preg_split('/ *, */', $match[2]) as $event) { |
||
| 390 | $events[] = GedcomTag::getLabel($event); |
||
| 391 | } |
||
| 392 | echo GedcomTag::getLabelValue('EVEN', implode(I18N::$list_separator, $events)); |
||
| 393 | |||
| 394 | if (preg_match('/\n3 DATE (.+)/', $evenMatches[$currentEvenMatch][0], $date_match)) { |
||
| 395 | $date = new Date($date_match[1]); |
||
| 396 | echo GedcomTag::getLabelValue('DATE', $date->display()); |
||
| 397 | } |
||
| 398 | if (preg_match('/\n3 PLAC (.+)/', $evenMatches[$currentEvenMatch][0], $plac_match)) { |
||
| 399 | echo GedcomTag::getLabelValue('PLAC', $plac_match[1]); |
||
| 400 | } |
||
| 401 | $currentEvenMatch++; |
||
| 402 | |||
| 403 | break; |
||
| 404 | case 'FAMC': // 0 INDI / 1 ADOP / 2 FAMC / 3 ADOP |
||
| 405 | $family = Factory::family()->make(str_replace('@', '', $match[2]), $tree); |
||
| 406 | if ($family instanceof Family) { |
||
| 407 | echo GedcomTag::getLabelValue('FAM', '<a href="' . e($family->url()) . '">' . $family->fullName() . '</a>'); |
||
| 408 | if (preg_match('/\n3 ADOP (HUSB|WIFE|BOTH)/', $fact->gedcom(), $adop_match)) { |
||
| 409 | echo GedcomTag::getLabelValue('ADOP', GedcomCodeAdop::getValue($adop_match[1])); |
||
| 410 | } |
||
| 411 | } else { |
||
| 412 | echo GedcomTag::getLabelValue('FAM', '<span class="error">' . $match[2] . '</span>'); |
||
| 413 | } |
||
| 414 | break; |
||
| 415 | case '_WT_USER': |
||
| 416 | if (Auth::check()) { |
||
| 417 | $user = (new UserService())->findByIdentifier($match[2]); // may not exist |
||
| 418 | if ($user instanceof UserInterface) { |
||
| 419 | echo GedcomTag::getLabelValue('_WT_USER', '<span dir="auto">' . e($user->realName()) . '</span>'); |
||
| 420 | } else { |
||
| 421 | echo GedcomTag::getLabelValue('_WT_USER', e($match[2])); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | break; |
||
| 425 | case 'RESN': |
||
| 426 | switch ($match[2]) { |
||
| 427 | case 'none': |
||
| 428 | // Note: "2 RESN none" is not valid gedcom. |
||
| 429 | // However, webtrees privacy rules will interpret it as "show an otherwise private fact to public". |
||
| 430 | echo GedcomTag::getLabelValue('RESN', '<i class="icon-resn-none"></i> ' . I18N::translate('Show to visitors')); |
||
| 431 | break; |
||
| 432 | case 'privacy': |
||
| 433 | echo GedcomTag::getLabelValue('RESN', '<i class="icon-resn-privacy"></i> ' . I18N::translate('Show to members')); |
||
| 434 | break; |
||
| 435 | case 'confidential': |
||
| 436 | echo GedcomTag::getLabelValue('RESN', '<i class="icon-resn-confidential"></i> ' . I18N::translate('Show to managers')); |
||
| 437 | break; |
||
| 438 | case 'locked': |
||
| 439 | echo GedcomTag::getLabelValue('RESN', '<i class="icon-resn-locked"></i> ' . I18N::translate('Only managers can edit')); |
||
| 440 | break; |
||
| 441 | default: |
||
| 442 | echo GedcomTag::getLabelValue('RESN', e($match[2])); |
||
| 443 | break; |
||
| 444 | } |
||
| 445 | break; |
||
| 446 | case 'CALN': |
||
| 447 | echo GedcomTag::getLabelValue('CALN', Filter::expandUrls($match[2], $tree)); |
||
| 448 | break; |
||
| 449 | case 'FORM': // 0 OBJE / 1 FILE / 2 FORM / 3 TYPE |
||
| 450 | echo GedcomTag::getLabelValue('FORM', $match[2]); |
||
| 451 | if (preg_match('/\n3 TYPE (.+)/', $fact->gedcom(), $type_match)) { |
||
| 452 | echo GedcomTag::getLabelValue('TYPE', GedcomTag::getFileFormTypeValue($type_match[1])); |
||
| 453 | } |
||
| 454 | break; |
||
| 455 | case 'URL': |
||
| 456 | case '_URL': |
||
| 457 | case 'WWW': |
||
| 458 | $link = '<a href="' . e($match[2]) . '">' . e($match[2]) . '</a>'; |
||
| 459 | echo GedcomTag::getLabelValue($tag . ':' . $match[1], $link); |
||
| 460 | break; |
||
| 461 | default: |
||
| 462 | if (!$hide_errors || GedcomTag::isTag($match[1])) { |
||
| 463 | if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $match[2], $xmatch)) { |
||
| 464 | // Links |
||
| 465 | $linked_record = Factory::gedcomRecord()->make($xmatch[1], $tree); |
||
| 466 | if ($linked_record) { |
||
| 467 | $link = '<a href="' . e($linked_record->url()) . '">' . $linked_record->fullName() . '</a>'; |
||
| 468 | echo GedcomTag::getLabelValue($tag . ':' . $match[1], $link); |
||
| 469 | } else { |
||
| 470 | echo GedcomTag::getLabelValue($tag . ':' . $match[1], e($match[2])); |
||
| 471 | } |
||
| 472 | } else { |
||
| 473 | // Non links |
||
| 474 | echo GedcomTag::getLabelValue($tag . ':' . $match[1], e($match[2])); |
||
| 475 | } |
||
| 476 | } |
||
| 477 | break; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | echo self::printFactSources($tree, $fact->gedcom(), 2); |
||
| 481 | echo FunctionsPrint::printFactNotes($tree, $fact->gedcom(), 2); |
||
| 482 | self::printMediaLinks($tree, $fact->gedcom(), 2); |
||
| 483 | echo '</td></tr>'; |
||
| 484 | } |
||
| 1119 |