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