1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Closure; 23use Exception; 24use Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage; 25use Illuminate\Database\Capsule\Manager as DB; 26use Illuminate\Support\Collection; 27use stdClass; 28 29/** 30 * A GEDCOM family (FAM) object. 31 */ 32class Family extends GedcomRecord 33{ 34 public const RECORD_TYPE = 'FAM'; 35 36 protected const ROUTE_NAME = FamilyPage::class; 37 38 /** @var Individual|null The husband (or first spouse for same-sex couples) */ 39 private $husb; 40 41 /** @var Individual|null The wife (or second spouse for same-sex couples) */ 42 private $wife; 43 44 /** 45 * Create a GedcomRecord object from raw GEDCOM data. 46 * 47 * @param string $xref 48 * @param string $gedcom an empty string for new/pending records 49 * @param string|null $pending null for a record with no pending edits, 50 * empty string for records with pending deletions 51 * @param Tree $tree 52 */ 53 public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 54 { 55 parent::__construct($xref, $gedcom, $pending, $tree); 56 57 // Make sure we find records in pending records. 58 $gedcom_pending = $gedcom . "\n" . $pending; 59 60 // Fetch family members 61 if (preg_match_all('/\n1 (?:HUSB|WIFE|CHIL) @(.+)@/', $gedcom_pending, $match)) { 62 Individual::load($tree, $match[1]); 63 } 64 65 if (preg_match('/\n1 HUSB @(.+)@/', $gedcom_pending, $match)) { 66 $this->husb = Individual::getInstance($match[1], $tree); 67 } 68 if (preg_match('/\n1 WIFE @(.+)@/', $gedcom_pending, $match)) { 69 $this->wife = Individual::getInstance($match[1], $tree); 70 } 71 } 72 73 /** 74 * A closure which will create a record from a database row. 75 * 76 * @param Tree $tree 77 * 78 * @return Closure 79 */ 80 public static function rowMapper(Tree $tree): Closure 81 { 82 return static function (stdClass $row) use ($tree): Family { 83 $family = Family::getInstance($row->f_id, $tree, $row->f_gedcom); 84 assert($family instanceof Family); 85 86 return $family; 87 }; 88 } 89 90 /** 91 * A closure which will compare families by marriage date. 92 * 93 * @return Closure 94 */ 95 public static function marriageDateComparator(): Closure 96 { 97 return static function (Family $x, Family $y): int { 98 return Date::compare($x->getMarriageDate(), $y->getMarriageDate()); 99 }; 100 } 101 102 /** 103 * Get an instance of a family object. For single records, 104 * we just receive the XREF. For bulk records (such as lists 105 * and search results) we can receive the GEDCOM data as well. 106 * 107 * @param string $xref 108 * @param Tree $tree 109 * @param string|null $gedcom 110 * 111 * @throws Exception 112 * 113 * @return Family|null 114 */ 115 public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Family 116 { 117 $record = parent::getInstance($xref, $tree, $gedcom); 118 119 if ($record instanceof self) { 120 return $record; 121 } 122 123 return null; 124 } 125 126 /** 127 * Generate a private version of this record 128 * 129 * @param int $access_level 130 * 131 * @return string 132 */ 133 protected function createPrivateGedcomRecord(int $access_level): string 134 { 135 if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 136 $access_level = Auth::PRIV_HIDE; 137 } 138 139 $rec = '0 @' . $this->xref . '@ FAM'; 140 // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data 141 preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 142 foreach ($matches as $match) { 143 $rela = Individual::getInstance($match[1], $this->tree); 144 if ($rela instanceof Individual && $rela->canShow($access_level)) { 145 $rec .= $match[0]; 146 } 147 } 148 149 return $rec; 150 } 151 152 /** 153 * Fetch data from the database 154 * 155 * @param string $xref 156 * @param int $tree_id 157 * 158 * @return string|null 159 */ 160 protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 161 { 162 return DB::table('families') 163 ->where('f_id', '=', $xref) 164 ->where('f_file', '=', $tree_id) 165 ->value('f_gedcom'); 166 } 167 168 /** 169 * Get the male (or first female) partner of the family 170 * 171 * @param int|null $access_level 172 * 173 * @return Individual|null 174 */ 175 public function husband($access_level = null): ?Individual 176 { 177 if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 178 $access_level = Auth::PRIV_HIDE; 179 } 180 181 if ($this->husb instanceof Individual && $this->husb->canShowName($access_level)) { 182 return $this->husb; 183 } 184 185 return null; 186 } 187 188 /** 189 * Get the female (or second male) partner of the family 190 * 191 * @param int|null $access_level 192 * 193 * @return Individual|null 194 */ 195 public function wife($access_level = null): ?Individual 196 { 197 if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 198 $access_level = Auth::PRIV_HIDE; 199 } 200 201 if ($this->wife instanceof Individual && $this->wife->canShowName($access_level)) { 202 return $this->wife; 203 } 204 205 return null; 206 } 207 208 /** 209 * Each object type may have its own special rules, and re-implement this function. 210 * 211 * @param int $access_level 212 * 213 * @return bool 214 */ 215 protected function canShowByType(int $access_level): bool 216 { 217 // Hide a family if any member is private 218 preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches); 219 foreach ($matches[1] as $match) { 220 $person = Individual::getInstance($match, $this->tree); 221 if ($person && !$person->canShow($access_level)) { 222 return false; 223 } 224 } 225 226 return true; 227 } 228 229 /** 230 * Can the name of this record be shown? 231 * 232 * @param int|null $access_level 233 * 234 * @return bool 235 */ 236 public function canShowName(int $access_level = null): bool 237 { 238 // We can always see the name (Husband-name + Wife-name), however, 239 // the name will often be "private + private" 240 return true; 241 } 242 243 /** 244 * Find the spouse of a person. 245 * 246 * @param Individual $person 247 * @param int|null $access_level 248 * 249 * @return Individual|null 250 */ 251 public function spouse(Individual $person, $access_level = null): ?Individual 252 { 253 if ($person === $this->wife) { 254 return $this->husband($access_level); 255 } 256 257 return $this->wife($access_level); 258 } 259 260 /** 261 * Get the (zero, one or two) spouses from this family. 262 * 263 * @param int|null $access_level 264 * 265 * @return Collection<Individual> 266 */ 267 public function spouses($access_level = null): Collection 268 { 269 $spouses = new Collection([ 270 $this->husband($access_level), 271 $this->wife($access_level), 272 ]); 273 274 return $spouses->filter(); 275 } 276 277 /** 278 * Get a list of this family’s children. 279 * 280 * @param int|null $access_level 281 * 282 * @return Collection<Individual> 283 */ 284 public function children($access_level = null): Collection 285 { 286 $access_level = $access_level ?? Auth::accessLevel($this->tree); 287 288 if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 289 $access_level = Auth::PRIV_HIDE; 290 } 291 292 $children = new Collection(); 293 294 foreach ($this->facts(['CHIL'], false, $access_level) as $fact) { 295 $child = $fact->target(); 296 297 if ($child instanceof Individual && $child->canShowName($access_level)) { 298 $children->push($child); 299 } 300 } 301 302 return $children; 303 } 304 305 /** 306 * Number of children - for the individual list 307 * 308 * @return int 309 */ 310 public function numberOfChildren(): int 311 { 312 $nchi = $this->children()->count(); 313 314 foreach ($this->facts(['NCHI']) as $fact) { 315 $nchi = max($nchi, (int) $fact->value()); 316 } 317 318 return $nchi; 319 } 320 321 /** 322 * get the marriage event 323 * 324 * @return Fact|null 325 */ 326 public function getMarriage(): ?Fact 327 { 328 return $this->facts(['MARR'])->first(); 329 } 330 331 /** 332 * Get marriage date 333 * 334 * @return Date 335 */ 336 public function getMarriageDate(): Date 337 { 338 $marriage = $this->getMarriage(); 339 if ($marriage) { 340 return $marriage->date(); 341 } 342 343 return new Date(''); 344 } 345 346 /** 347 * Get the marriage year - displayed on lists of families 348 * 349 * @return int 350 */ 351 public function getMarriageYear(): int 352 { 353 return $this->getMarriageDate()->minimumDate()->year; 354 } 355 356 /** 357 * Get the marriage place 358 * 359 * @return Place 360 */ 361 public function getMarriagePlace(): Place 362 { 363 $marriage = $this->getMarriage(); 364 365 if ($marriage instanceof Fact) { 366 return $marriage->place(); 367 } 368 369 return new Place('', $this->tree); 370 } 371 372 /** 373 * Get a list of all marriage dates - for the family lists. 374 * 375 * @return Date[] 376 */ 377 public function getAllMarriageDates(): array 378 { 379 foreach (Gedcom::MARRIAGE_EVENTS as $event) { 380 $array = $this->getAllEventDates([$event]); 381 382 if ($array !== []) { 383 return $array; 384 } 385 } 386 387 return []; 388 } 389 390 /** 391 * Get a list of all marriage places - for the family lists. 392 * 393 * @return Place[] 394 */ 395 public function getAllMarriagePlaces(): array 396 { 397 foreach (Gedcom::MARRIAGE_EVENTS as $event) { 398 $places = $this->getAllEventPlaces([$event]); 399 if ($places !== []) { 400 return $places; 401 } 402 } 403 404 return []; 405 } 406 407 /** 408 * Derived classes should redefine this function, otherwise the object will have no name 409 * 410 * @return string[][] 411 */ 412 public function getAllNames(): array 413 { 414 if ($this->getAllNames === null) { 415 // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc. 416 $husb_names = []; 417 if ($this->husb) { 418 $husb_names = array_filter($this->husb->getAllNames(), static function (array $x): bool { 419 return $x['type'] !== '_MARNM'; 420 }); 421 } 422 // If the individual only has married names, create a dummy birth name. 423 if ($husb_names === []) { 424 $husb_names[] = [ 425 'type' => 'BIRT', 426 'sort' => '@N.N.', 427 'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'), 428 ]; 429 } 430 foreach ($husb_names as $n => $husb_name) { 431 $husb_names[$n]['script'] = I18N::textScript($husb_name['full']); 432 } 433 434 $wife_names = []; 435 if ($this->wife) { 436 $wife_names = array_filter($this->wife->getAllNames(), static function (array $x): bool { 437 return $x['type'] !== '_MARNM'; 438 }); 439 } 440 // If the individual only has married names, create a dummy birth name. 441 if ($wife_names === []) { 442 $wife_names[] = [ 443 'type' => 'BIRT', 444 'sort' => '@N.N.', 445 'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'), 446 ]; 447 } 448 foreach ($wife_names as $n => $wife_name) { 449 $wife_names[$n]['script'] = I18N::textScript($wife_name['full']); 450 } 451 452 // Add the matched names first 453 foreach ($husb_names as $husb_name) { 454 foreach ($wife_names as $wife_name) { 455 if ($husb_name['script'] === $wife_name['script']) { 456 $this->getAllNames[] = [ 457 'type' => $husb_name['type'], 458 'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'], 459 'full' => $husb_name['full'] . ' + ' . $wife_name['full'], 460 // No need for a fullNN entry - we do not currently store FAM names in the database 461 ]; 462 } 463 } 464 } 465 466 // Add the unmatched names second (there may be no matched names) 467 foreach ($husb_names as $husb_name) { 468 foreach ($wife_names as $wife_name) { 469 if ($husb_name['script'] !== $wife_name['script']) { 470 $this->getAllNames[] = [ 471 'type' => $husb_name['type'], 472 'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'], 473 'full' => $husb_name['full'] . ' + ' . $wife_name['full'], 474 // No need for a fullNN entry - we do not currently store FAM names in the database 475 ]; 476 } 477 } 478 } 479 } 480 481 return $this->getAllNames; 482 } 483 484 /** 485 * This function should be redefined in derived classes to show any major 486 * identifying characteristics of this record. 487 * 488 * @return string 489 */ 490 public function formatListDetails(): string 491 { 492 return 493 $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) . 494 $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1); 495 } 496} 497