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