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