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