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