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 /** {@inheritdoc} */ 55 protected function createPrivateGedcomRecord($access_level) { 56 $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 57 58 $rec = '0 @' . $this->xref . '@ FAM'; 59 // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data 60 preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER); 61 foreach ($matches as $match) { 62 $rela = Individual::getInstance($match[1], $this->tree); 63 if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) { 64 $rec .= $match[0]; 65 } 66 } 67 68 return $rec; 69 } 70 71 /** {@inheritdoc} */ 72 protected static function fetchGedcomRecord($xref, $tree_id) { 73 return Database::prepare( 74 "SELECT f_gedcom FROM `##families` WHERE f_id = :xref AND f_file = :tree_id" 75 )->execute(array( 76 'xref' => $xref, 77 'tree_id' => $tree_id, 78 ))->fetchOne(); 79 } 80 81 /** 82 * Get the male (or first female) partner of the family 83 * 84 * @return Individual|null 85 */ 86 public function getHusband() { 87 if ($this->husb && $this->husb->canShowName()) { 88 return $this->husb; 89 } else { 90 return null; 91 } 92 } 93 94 /** 95 * Get the female (or second male) partner of the family 96 * 97 * @return Individual|null 98 */ 99 public function getWife() { 100 if ($this->wife && $this->wife->canShowName()) { 101 return $this->wife; 102 } else { 103 return null; 104 } 105 } 106 107 /** {@inheritdoc} */ 108 protected function canShowByType($access_level) { 109 // Hide a family if any member is private 110 preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches); 111 foreach ($matches[1] as $match) { 112 $person = Individual::getInstance($match, $this->tree); 113 if ($person && !$person->canShow($access_level)) { 114 return false; 115 } 116 } 117 118 return true; 119 } 120 121 /** {@inheritdoc} */ 122 public function canShowName($access_level = null) { 123 // We can always see the name (Husband-name + Wife-name), however, 124 // the name will often be "private + private" 125 return true; 126 } 127 128 /** 129 * Find the spouse of a person. 130 * 131 * @param Individual $person 132 * 133 * @return Individual|null 134 */ 135 public function getSpouse(Individual $person) { 136 if ($person === $this->wife) { 137 return $this->husb; 138 } else { 139 return $this->wife; 140 } 141 } 142 143 /** 144 * Get the (zero, one or two) spouses from this family. 145 * 146 * @param int|null $access_level 147 * 148 * @return Individual[] 149 */ 150 public function getSpouses($access_level = null) { 151 if ($access_level === null) { 152 $access_level = Auth::accessLevel($this->tree); 153 } 154 155 $spouses = array(); 156 if ($this->husb && $this->husb->canShowName($access_level)) { 157 $spouses[] = $this->husb; 158 } 159 if ($this->wife && $this->wife->canShowName($access_level)) { 160 $spouses[] = $this->wife; 161 } 162 163 return $spouses; 164 } 165 166 /** 167 * Get a list of this family’s children. 168 * 169 * @param int|null $access_level 170 * 171 * @return Individual[] 172 */ 173 public function getChildren($access_level = null) { 174 if ($access_level === null) { 175 $access_level = Auth::accessLevel($this->tree); 176 } 177 178 $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS'); 179 180 $children = array(); 181 foreach ($this->getFacts('CHIL', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) { 182 $child = $fact->getTarget(); 183 if ($child && ($SHOW_PRIVATE_RELATIONSHIPS || $child->canShowName($access_level))) { 184 $children[] = $child; 185 } 186 } 187 188 return $children; 189 } 190 191 /** 192 * Static helper function to sort an array of families by marriage date 193 * 194 * @param Family $x 195 * @param Family $y 196 * 197 * @return int 198 */ 199 public static function compareMarrDate(Family $x, Family $y) { 200 return Date::compare($x->getMarriageDate(), $y->getMarriageDate()); 201 } 202 203 /** 204 * Number of children - for the individual list 205 * 206 * @return int 207 */ 208 public function getNumberOfChildren() { 209 $nchi = count($this->getChildren()); 210 foreach ($this->getFacts('NCHI') as $fact) { 211 $nchi = max($nchi, (int) $fact->getValue()); 212 } 213 214 return $nchi; 215 } 216 217 /** 218 * get the marriage event 219 * 220 * @return Fact 221 */ 222 public function getMarriage() { 223 return $this->getFirstFact('MARR'); 224 } 225 226 /** 227 * Get marriage date 228 * 229 * @return Date 230 */ 231 public function getMarriageDate() { 232 $marriage = $this->getMarriage(); 233 if ($marriage) { 234 return $marriage->getDate(); 235 } else { 236 return new Date(''); 237 } 238 } 239 240 /** 241 * Get the marriage year - displayed on lists of families 242 * 243 * @return int 244 */ 245 public function getMarriageYear() { 246 return $this->getMarriageDate()->minimumDate()->y; 247 } 248 249 /** 250 * Get the type for this marriage 251 * 252 * @return string|null 253 */ 254 public function getMarriageType() { 255 $marriage = $this->getMarriage(); 256 if ($marriage) { 257 return $marriage->getAttribute('TYPE'); 258 } else { 259 return null; 260 } 261 } 262 263 /** 264 * Get the marriage place 265 * 266 * @return Place 267 */ 268 public function getMarriagePlace() { 269 $marriage = $this->getMarriage(); 270 271 return $marriage->getPlace(); 272 } 273 274 /** 275 * Get a list of all marriage dates - for the family lists. 276 * 277 * @return Date[] 278 */ 279 public function getAllMarriageDates() { 280 foreach (explode('|', WT_EVENTS_MARR) as $event) { 281 if ($array = $this->getAllEventDates($event)) { 282 return $array; 283 } 284 } 285 286 return array(); 287 } 288 289 /** 290 * Get a list of all marriage places - for the family lists. 291 * 292 * @return string[] 293 */ 294 public function getAllMarriagePlaces() { 295 foreach (explode('|', WT_EVENTS_MARR) as $event) { 296 if ($array = $this->getAllEventPlaces($event)) { 297 return $array; 298 } 299 } 300 301 return array(); 302 } 303 304 /** {@inheritdoc} */ 305 public function getAllNames() { 306 global $UNKNOWN_NN, $UNKNOWN_PN; 307 308 if (is_null($this->_getAllNames)) { 309 // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc. 310 if ($this->husb) { 311 $husb_names = $this->husb->getAllNames(); 312 } else { 313 $husb_names = array( 314 0 => array( 315 'type' => 'BIRT', 316 'sort' => '@N.N.', 317 'full' => $UNKNOWN_PN, ' ', $UNKNOWN_NN, 318 ), 319 ); 320 } 321 foreach ($husb_names as $n => $husb_name) { 322 $husb_names[$n]['script'] = I18N::textScript($husb_name['full']); 323 } 324 if ($this->wife) { 325 $wife_names = $this->wife->getAllNames(); 326 } else { 327 $wife_names = array( 328 0 => array( 329 'type' => 'BIRT', 330 'sort' => '@N.N.', 331 'full' => $UNKNOWN_PN, ' ', $UNKNOWN_NN, 332 ), 333 ); 334 } 335 foreach ($wife_names as $n => $wife_name) { 336 $wife_names[$n]['script'] = I18N::textScript($wife_name['full']); 337 } 338 // Add the matched names first 339 foreach ($husb_names as $husb_name) { 340 foreach ($wife_names as $wife_name) { 341 if ($husb_name['type'] != '_MARNM' && $wife_name['type'] != '_MARNM' && $husb_name['script'] == $wife_name['script']) { 342 $this->_getAllNames[] = array( 343 'type' => $husb_name['type'], 344 'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'], 345 'full' => $husb_name['full'] . ' + ' . $wife_name['full'], 346 // No need for a fullNN entry - we do not currently store FAM names in the database 347 ); 348 } 349 } 350 } 351 // Add the unmatched names second (there may be no matched names) 352 foreach ($husb_names as $husb_name) { 353 foreach ($wife_names as $wife_name) { 354 if ($husb_name['type'] != '_MARNM' && $wife_name['type'] != '_MARNM' && $husb_name['script'] != $wife_name['script']) { 355 $this->_getAllNames[] = array( 356 'type' => $husb_name['type'], 357 'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'], 358 'full' => $husb_name['full'] . ' + ' . $wife_name['full'], 359 // No need for a fullNN entry - we do not currently store FAM names in the database 360 ); 361 } 362 } 363 } 364 } 365 366 return $this->_getAllNames; 367 } 368 369 /** {@inheritdoc} */ 370 public function formatListDetails() { 371 return 372 $this->formatFirstMajorFact(WT_EVENTS_MARR, 1) . 373 $this->formatFirstMajorFact(WT_EVENTS_DIV, 1); 374 } 375} 376