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 Illuminate\Database\Capsule\Manager as DB; 25use Illuminate\Support\Collection; 26use stdClass; 27 28/** 29 * A GEDCOM family (FAM) object. 30 */ 31class Family extends GedcomRecord 32{ 33 public const RECORD_TYPE = 'FAM'; 34 35 protected const ROUTE_NAME = 'family'; 36 37 /** @var Individual|null The husband (or first spouse for same-sex couples) */ 38 private $husb; 39 40 /** @var Individual|null The wife (or second spouse for same-sex couples) */ 41 private $wife; 42 43 /** 44 * Create a GedcomRecord object from raw GEDCOM data. 45 * 46 * @param string $xref 47 * @param string $gedcom an empty string for new/pending records 48 * @param string|null $pending null for a record with no pending edits, 49 * empty string for records with pending deletions 50 * @param Tree $tree 51 */ 52 public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree) 53 { 54 parent::__construct($xref, $gedcom, $pending, $tree); 55 56 // Fetch family members 57 if (preg_match_all('/^1 (?:HUSB|WIFE|CHIL) @(.+)@/m', $gedcom . $pending, $match)) { 58 Individual::load($tree, $match[1]); 59 } 60 61 if (preg_match('/^1 HUSB @(.+)@/m', $gedcom . $pending, $match)) { 62 $this->husb = Individual::getInstance($match[1], $tree); 63 } 64 if (preg_match('/^1 WIFE @(.+)@/m', $gedcom . $pending, $match)) { 65 $this->wife = Individual::getInstance($match[1], $tree); 66 } 67 } 68 69 /** 70 * A closure which will create a record from a database row. 71 * 72 * @return Closure 73 */ 74 public static function rowMapper(): Closure 75 { 76 return static function (stdClass $row): Family { 77 return Family::getInstance($row->f_id, Tree::findById((int) $row->f_file), $row->f_gedcom); 78 }; 79 } 80 81 /** 82 * A closure which will compare families by marriage date. 83 * 84 * @return Closure 85 */ 86 public static function marriageDateComparator(): Closure 87 { 88 return static function (Family $x, Family $y): int { 89 return Date::compare($x->getMarriageDate(), $y->getMarriageDate()); 90 }; 91 } 92 93 /** 94 * Get an instance of a family object. For single records, 95 * we just receive the XREF. For bulk records (such as lists 96 * and search results) we can receive the GEDCOM data as well. 97 * 98 * @param string $xref 99 * @param Tree $tree 100 * @param string|null $gedcom 101 * 102 * @throws Exception 103 * 104 * @return Family|null 105 */ 106 public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self 107 { 108 $record = parent::getInstance($xref, $tree, $gedcom); 109 110 if ($record instanceof self) { 111 return $record; 112 } 113 114 return null; 115 } 116 117 /** 118 * Generate a private version of this record 119 * 120 * @param int $access_level 121 * 122 * @return string 123 */ 124 protected function createPrivateGedcomRecord(int $access_level): string 125 { 126 $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 127 128 $rec = '0 @' . $this->xref . '@ FAM'; 129 // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data 130 preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 131 foreach ($matches as $match) { 132 $rela = Individual::getInstance($match[1], $this->tree); 133 if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 134 $rec .= $match[0]; 135 } 136 } 137 138 return $rec; 139 } 140 141 /** 142 * Fetch data from the database 143 * 144 * @param string $xref 145 * @param int $tree_id 146 * 147 * @return string|null 148 */ 149 protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 150 { 151 return DB::table('families') 152 ->where('f_id', '=', $xref) 153 ->where('f_file', '=', $tree_id) 154 ->value('f_gedcom'); 155 } 156 157 /** 158 * Get the male (or first female) partner of the family 159 * 160 * @param int|null $access_level 161 * 162 * @return Individual|null 163 */ 164 public function husband($access_level = null): ?Individual 165 { 166 $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 167 168 if ($this->husb && ($SHOW_PRIVATE_RELATIONSHIPS || $this->husb->canShowName($access_level))) { 169 return $this->husb; 170 } 171 172 return null; 173 } 174 175 /** 176 * Get the female (or second male) partner of the family 177 * 178 * @param int|null $access_level 179 * 180 * @return Individual|null 181 */ 182 public function wife($access_level = null): ?Individual 183 { 184 $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 185 186 if ($this->wife && ($SHOW_PRIVATE_RELATIONSHIPS || $this->wife->canShowName($access_level))) { 187 return $this->wife; 188 } 189 190 return null; 191 } 192 193 /** 194 * Each object type may have its own special rules, and re-implement this function. 195 * 196 * @param int $access_level 197 * 198 * @return bool 199 */ 200 protected function canShowByType(int $access_level): bool 201 { 202 // Hide a family if any member is private 203 preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches); 204 foreach ($matches[1] as $match) { 205 $person = Individual::getInstance($match, $this->tree); 206 if ($person && !$person->canShow($access_level)) { 207 return false; 208 } 209 } 210 211 return true; 212 } 213 214 /** 215 * Can the name of this record be shown? 216 * 217 * @param int|null $access_level 218 * 219 * @return bool 220 */ 221 public function canShowName(int $access_level = null): bool 222 { 223 // We can always see the name (Husband-name + Wife-name), however, 224 // the name will often be "private + private" 225 return true; 226 } 227 228 /** 229 * Find the spouse of a person. 230 * 231 * @param Individual $person 232 * @param int|null $access_level 233 * 234 * @return Individual|null 235 */ 236 public function spouse(Individual $person, $access_level = null): ?Individual 237 { 238 if ($person === $this->wife) { 239 return $this->husband($access_level); 240 } 241 242 return $this->wife($access_level); 243 } 244 245 /** 246 * Get the (zero, one or two) spouses from this family. 247 * 248 * @param int|null $access_level 249 * 250 * @return Collection 251 */ 252 public function spouses($access_level = null): Collection 253 { 254 $spouses = new Collection([ 255 $this->husband($access_level), 256 $this->wife($access_level), 257 ]); 258 259 return $spouses->filter(); 260 } 261 262 /** 263 * Get a list of this family’s children. 264 * 265 * @param int|null $access_level 266 * 267 * @return Collection 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