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