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