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