1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Date; 22use Fisharebest\Webtrees\Fact; 23use Fisharebest\Webtrees\Family; 24use Fisharebest\Webtrees\Functions\Functions; 25use Fisharebest\Webtrees\Gedcom; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Services\ModuleService; 29use Illuminate\Support\Collection; 30 31/** 32 * Class IndividualFactsTabModule 33 */ 34class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface 35{ 36 use ModuleTabTrait; 37 38 /** 39 * @var ModuleService 40 */ 41 private $module_service; 42 43 /** 44 * UserWelcomeModule constructor. 45 * 46 * @param ModuleService $module_service 47 */ 48 public function __construct(ModuleService $module_service) 49 { 50 $this->module_service = $module_service; 51 } 52 53 /** 54 * How should this module be labelled on tabs, menus, etc.? 55 * 56 * @return string 57 */ 58 public function title(): string 59 { 60 /* I18N: Name of a module/tab on the individual page. */ 61 return I18N::translate('Facts and events'); 62 } 63 64 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function description(): string 70 { 71 /* I18N: Description of the “Facts and events” module */ 72 return I18N::translate('A tab showing the facts and events of an individual.'); 73 } 74 75 /** 76 * The default position for this tab. It can be changed in the control panel. 77 * 78 * @return int 79 */ 80 public function defaultTabOrder(): int 81 { 82 return 2; 83 } 84 85 /** {@inheritdoc} */ 86 public function isGrayedOut(Individual $individual): bool 87 { 88 return false; 89 } 90 91 /** {@inheritdoc} */ 92 public function getTabContent(Individual $individual): string 93 { 94 // Only include events of close relatives that are between birth and death 95 $min_date = $individual->getEstimatedBirthDate(); 96 $max_date = $individual->getEstimatedDeathDate(); 97 98 // Which facts and events are handled by other modules? 99 $sidebar_facts = $this->module_service 100 ->findByComponent('sidebar', $individual->tree(), Auth::user()) 101 ->map(function (ModuleSidebarInterface $sidebar): Collection { 102 return $sidebar->supportedFacts(); 103 }); 104 105 $tab_facts = $this->module_service 106 ->findByComponent('tab', $individual->tree(), Auth::user()) 107 ->map(function (ModuleTabInterface $sidebar): Collection { 108 return $sidebar->supportedFacts(); 109 }); 110 111 $exclude_facts = $sidebar_facts->merge($tab_facts)->flatten(); 112 113 $indifacts = []; 114 115 // The individual’s own facts 116 foreach ($individual->facts() as $fact) { 117 if (!$exclude_facts->contains($fact->getTag())) { 118 $indifacts[] = $fact; 119 } 120 } 121 122 // Add spouse-family facts 123 foreach ($individual->getSpouseFamilies() as $family) { 124 foreach ($family->facts() as $fact) { 125 if (!$exclude_facts->contains($fact->getTag()) && $fact->getTag() !== 'CHAN') { 126 $indifacts[] = $fact; 127 } 128 } 129 130 $spouse = $family->getSpouse($individual); 131 132 if ($spouse instanceof Individual) { 133 $spouse_facts = $this->spouseFacts($individual, $spouse, $min_date, $max_date); 134 $indifacts = array_merge($indifacts, $spouse_facts); 135 } 136 137 $child_facts = $this->childFacts($individual, $family, '_CHIL', '', $min_date, $max_date); 138 $indifacts = array_merge($indifacts, $child_facts); 139 } 140 141 $parent_facts = $this->parentFacts($individual, 1, $min_date, $max_date); 142 $associate_facts = $this->associateFacts($individual); 143 $historical_facts = $this->historicalFacts($individual); 144 145 $indifacts = array_merge($indifacts, $parent_facts, $associate_facts, $historical_facts); 146 147 Functions::sortFacts($indifacts); 148 149 return view('modules/personal_facts/tab', [ 150 'can_edit' => $individual->canEdit(), 151 'has_historical_facts' => !empty($historical_facts), 152 'individual' => $individual, 153 'facts' => $indifacts, 154 ]); 155 } 156 157 /** 158 * Does a relative event occur within a date range (i.e. the individual's lifetime)? 159 * 160 * @param Fact $fact 161 * @param Date $min_date 162 * @param Date $max_date 163 * 164 * @return bool 165 */ 166 private function includeFact(Fact $fact, Date $min_date, Date $max_date): bool 167 { 168 $fact_date = $fact->date(); 169 170 return $fact_date->isOK() && Date::compare($min_date, $fact_date) <= 0 && Date::compare($fact_date, $max_date) <= 0; 171 } 172 173 /** {@inheritdoc} */ 174 public function hasTabContent(Individual $individual): bool 175 { 176 return true; 177 } 178 179 /** {@inheritdoc} */ 180 public function canLoadAjax(): bool 181 { 182 return false; 183 } 184 185 /** 186 * Spouse facts that are shown on an individual’s page. 187 * 188 * @param Individual $individual Show events that occured during the lifetime of this individual 189 * @param Individual $spouse Show events of this individual 190 * @param Date $min_date 191 * @param Date $max_date 192 * 193 * @return Fact[] 194 */ 195 private function spouseFacts(Individual $individual, Individual $spouse, Date $min_date, Date $max_date): array 196 { 197 $SHOW_RELATIVES_EVENTS = $individual->tree()->getPreference('SHOW_RELATIVES_EVENTS'); 198 199 $facts = []; 200 if (strstr($SHOW_RELATIVES_EVENTS, '_DEAT_SPOU')) { 201 foreach ($spouse->facts(Gedcom::DEATH_EVENTS) as $fact) { 202 if ($this->includeFact($fact, $min_date, $max_date)) { 203 // Convert the event to a close relatives event. 204 $rela_fact = clone($fact); 205 $rela_fact->setTag('_' . $fact->getTag() . '_SPOU'); 206 $facts[] = $rela_fact; 207 } 208 } 209 } 210 211 return $facts; 212 } 213 214 /** 215 * Get the events of children and grandchildren. 216 * 217 * @param Individual $person 218 * @param Family $family 219 * @param string $option 220 * @param string $relation 221 * @param Date $min_date 222 * @param Date $max_date 223 * 224 * @return Fact[] 225 */ 226 private function childFacts(Individual $person, Family $family, $option, $relation, Date $min_date, Date $max_date): array 227 { 228 $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS'); 229 230 $facts = []; 231 232 // Deal with recursion. 233 switch ($option) { 234 case '_CHIL': 235 // Add grandchildren 236 foreach ($family->getChildren() as $child) { 237 foreach ($child->getSpouseFamilies() as $cfamily) { 238 switch ($child->getSex()) { 239 case 'M': 240 foreach ($this->childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) { 241 $facts[] = $fact; 242 } 243 break; 244 case 'F': 245 foreach ($this->childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) { 246 $facts[] = $fact; 247 } 248 break; 249 default: 250 foreach ($this->childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) { 251 $facts[] = $fact; 252 } 253 break; 254 } 255 } 256 } 257 break; 258 } 259 260 // For each child in the family 261 foreach ($family->getChildren() as $child) { 262 if ($child->xref() == $person->xref()) { 263 // We are not our own sibling! 264 continue; 265 } 266 // add child’s birth 267 if (strpos($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option)) !== false) { 268 foreach ($child->facts(Gedcom::BIRTH_EVENTS) as $fact) { 269 // Always show _BIRT_CHIL, even if the dates are not known 270 if ($option == '_CHIL' || $this->includeFact($fact, $min_date, $max_date)) { 271 if ($option == '_GCHI' && $relation == 'dau') { 272 // Convert the event to a close relatives event. 273 $rela_fact = clone($fact); 274 $rela_fact->setTag('_' . $fact->getTag() . '_GCH1'); 275 $facts[] = $rela_fact; 276 } elseif ($option == '_GCHI' && $relation == 'son') { 277 // Convert the event to a close relatives event. 278 $rela_fact = clone($fact); 279 $rela_fact->setTag('_' . $fact->getTag() . '_GCH2'); 280 $facts[] = $rela_fact; 281 } else { 282 // Convert the event to a close relatives event. 283 $rela_fact = clone($fact); 284 $rela_fact->setTag('_' . $fact->getTag() . $option); 285 $facts[] = $rela_fact; 286 } 287 } 288 } 289 } 290 // add child’s death 291 if (strpos($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option)) !== false) { 292 foreach ($child->facts(Gedcom::DEATH_EVENTS) as $fact) { 293 if ($this->includeFact($fact, $min_date, $max_date)) { 294 if ($option == '_GCHI' && $relation == 'dau') { 295 // Convert the event to a close relatives event. 296 $rela_fact = clone($fact); 297 $rela_fact->setTag('_' . $fact->getTag() . '_GCH1'); 298 $facts[] = $rela_fact; 299 } elseif ($option == '_GCHI' && $relation == 'son') { 300 // Convert the event to a close relatives event. 301 $rela_fact = clone($fact); 302 $rela_fact->setTag('_' . $fact->getTag() . '_GCH2'); 303 $facts[] = $rela_fact; 304 } else { 305 // Convert the event to a close relatives event. 306 $rela_fact = clone($fact); 307 $rela_fact->setTag('_' . $fact->getTag() . $option); 308 $facts[] = $rela_fact; 309 } 310 } 311 } 312 } 313 // add child’s marriage 314 if (strstr($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) { 315 foreach ($child->getSpouseFamilies() as $sfamily) { 316 foreach ($sfamily->facts(['MARR']) as $fact) { 317 if ($this->includeFact($fact, $min_date, $max_date)) { 318 if ($option == '_GCHI' && $relation == 'dau') { 319 // Convert the event to a close relatives event. 320 $rela_fact = clone($fact); 321 $rela_fact->setTag('_' . $fact->getTag() . '_GCH1'); 322 $facts[] = $rela_fact; 323 } elseif ($option == '_GCHI' && $relation == 'son') { 324 // Convert the event to a close relatives event. 325 $rela_fact = clone($fact); 326 $rela_fact->setTag('_' . $fact->getTag() . '_GCH2'); 327 $facts[] = $rela_fact; 328 } else { 329 // Convert the event to a close relatives event. 330 $rela_fact = clone($fact); 331 $rela_fact->setTag('_' . $fact->getTag() . $option); 332 $facts[] = $rela_fact; 333 } 334 } 335 } 336 } 337 } 338 } 339 340 return $facts; 341 } 342 343 /** 344 * Get the events of parents and grandparents. 345 * 346 * @param Individual $person 347 * @param int $sosa 348 * @param Date $min_date 349 * @param Date $max_date 350 * 351 * @return Fact[] 352 */ 353 private function parentFacts(Individual $person, $sosa, Date $min_date, Date $max_date): array 354 { 355 $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS'); 356 357 $facts = []; 358 359 if ($sosa == 1) { 360 foreach ($person->getChildFamilies() as $family) { 361 // Add siblings 362 foreach ($this->childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) { 363 $facts[] = $fact; 364 } 365 foreach ($family->getSpouses() as $spouse) { 366 foreach ($spouse->getSpouseFamilies() as $sfamily) { 367 if ($family !== $sfamily) { 368 // Add half-siblings 369 foreach ($this->childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) { 370 $facts[] = $fact; 371 } 372 } 373 } 374 // Add grandparents 375 foreach ($this->parentFacts($spouse, $spouse->getSex() == 'F' ? 3 : 2, $min_date, $max_date) as $fact) { 376 $facts[] = $fact; 377 } 378 } 379 } 380 381 if (strstr($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) { 382 // add father/mother marriages 383 foreach ($person->getChildFamilies() as $sfamily) { 384 foreach ($sfamily->facts(['MARR']) as $fact) { 385 if ($this->includeFact($fact, $min_date, $max_date)) { 386 // marriage of parents (to each other) 387 $rela_fact = clone($fact); 388 $rela_fact->setTag('_' . $fact->getTag() . '_FAMC'); 389 $facts[] = $rela_fact; 390 } 391 } 392 } 393 foreach ($person->getChildStepFamilies() as $sfamily) { 394 foreach ($sfamily->facts(['MARR']) as $fact) { 395 if ($this->includeFact($fact, $min_date, $max_date)) { 396 // marriage of a parent (to another spouse) 397 // Convert the event to a close relatives event 398 $rela_fact = clone($fact); 399 $rela_fact->setTag('_' . $fact->getTag() . '_PARE'); 400 $facts[] = $rela_fact; 401 } 402 } 403 } 404 } 405 } 406 407 foreach ($person->getChildFamilies() as $family) { 408 foreach ($family->getSpouses() as $parent) { 409 if (strstr($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa == 1 ? '_PARE' : '_GPAR'))) { 410 foreach ($parent->facts(Gedcom::DEATH_EVENTS) as $fact) { 411 if ($this->includeFact($fact, $min_date, $max_date)) { 412 switch ($sosa) { 413 case 1: 414 // Convert the event to a close relatives event. 415 $rela_fact = clone($fact); 416 $rela_fact->setTag('_' . $fact->getTag() . '_PARE'); 417 $facts[] = $rela_fact; 418 break; 419 case 2: 420 // Convert the event to a close relatives event 421 $rela_fact = clone($fact); 422 $rela_fact->setTag('_' . $fact->getTag() . '_GPA1'); 423 $facts[] = $rela_fact; 424 break; 425 case 3: 426 // Convert the event to a close relatives event 427 $rela_fact = clone($fact); 428 $rela_fact->setTag('_' . $fact->getTag() . '_GPA2'); 429 $facts[] = $rela_fact; 430 break; 431 } 432 } 433 } 434 } 435 } 436 } 437 438 return $facts; 439 } 440 441 /** 442 * Get any historical events. 443 * 444 * @param Individual $individual 445 * 446 * @return Fact[] 447 */ 448 private function historicalFacts(Individual $individual): array 449 { 450 return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class) 451 ->map(function (ModuleHistoricEventsInterface $module) use ($individual): Collection { 452 return $module->historicEventsForIndividual($individual); 453 }) 454 ->flatten() 455 ->all(); 456 } 457 458 /** 459 * Get the events of associates. 460 * 461 * @param Individual $person 462 * 463 * @return Fact[] 464 */ 465 private function associateFacts(Individual $person): array 466 { 467 $facts = []; 468 469 /** @var Individual[] $associates */ 470 $associates = array_merge( 471 $person->linkedIndividuals('ASSO'), 472 $person->linkedIndividuals('_ASSO'), 473 $person->linkedFamilies('ASSO'), 474 $person->linkedFamilies('_ASSO') 475 ); 476 foreach ($associates as $associate) { 477 foreach ($associate->facts() as $fact) { 478 $arec = $fact->attribute('_ASSO'); 479 if (!$arec) { 480 $arec = $fact->attribute('ASSO'); 481 } 482 if ($arec && trim($arec, '@') === $person->xref()) { 483 // Extract the important details from the fact 484 $factrec = '1 ' . $fact->getTag(); 485 if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) { 486 $factrec .= $match[0]; 487 } 488 if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) { 489 $factrec .= $match[0]; 490 } 491 if ($associate instanceof Family) { 492 foreach ($associate->getSpouses() as $spouse) { 493 $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@'; 494 } 495 } else { 496 $factrec .= "\n2 _ASSO @" . $associate->xref() . '@'; 497 } 498 $facts[] = new Fact($factrec, $associate, 'asso'); 499 } 500 } 501 } 502 503 return $facts; 504 } 505 506 /** 507 * This module handles the following facts - so don't show them on the "Facts and events" tab. 508 * 509 * @return Collection|string[] 510 */ 511 public function supportedFacts(): Collection 512 { 513 // We don't actually displaye these facts, but they are displayed 514 // outside the tabs/sidebar systems. This just forces them to be excluded here. 515 return new Collection(['NAME', 'SEX']); 516 } 517} 518