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