xref: /webtrees/app/Family.php (revision 8c2e82270a639a3acf607b432e54721116dae723)
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, $gedcom_id) {
34		parent::__construct($xref, $gedcom, $pending, $gedcom_id);
35
36		// Fetch husband and wife
37		if (preg_match('/^1 HUSB @(.+)@/m', $gedcom . $pending, $match)) {
38			$this->husb = Individual::getInstance($match[1], $gedcom_id);
39		}
40		if (preg_match('/^1 WIFE @(.+)@/m', $gedcom . $pending, $match)) {
41			$this->wife = Individual::getInstance($match[1], $gedcom_id);
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 integer|null $gedcom_id
57	 * @param string|null  $gedcom
58	 *
59	 * @return Family|null
60	 */
61	public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) {
62		$record = parent::getInstance($xref, $gedcom_id, $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]);
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);
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 = WT_USER_ACCESS_LEVEL) {
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 $access_level
165	 *
166	 * @return Individual[]
167	 */
168	function getSpouses($access_level = WT_USER_ACCESS_LEVEL) {
169		$spouses = array();
170		if ($this->husb && $this->husb->canShowName($access_level)) {
171			$spouses[] = $this->husb;
172		}
173		if ($this->wife && $this->wife->canShowName($access_level)) {
174			$spouses[] = $this->wife;
175		}
176
177		return $spouses;
178	}
179
180	/**
181	 * Get a list of this family’s children.
182	 *
183	 * @param integer $access_level
184	 *
185	 * @return Individual[]
186	 */
187	function getChildren($access_level = WT_USER_ACCESS_LEVEL) {
188		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
189
190		$children = array();
191		foreach ($this->getFacts('CHIL', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
192			$child = $fact->getTarget();
193			if ($child && ($SHOW_PRIVATE_RELATIONSHIPS || $child->canShowName($access_level))) {
194				$children[] = $child;
195			}
196		}
197
198		return $children;
199	}
200
201	/**
202	 * Static helper function to sort an array of families by marriage date
203	 *
204	 * @param Family $x
205	 * @param Family $y
206	 *
207	 * @return integer
208	 */
209	public static function compareMarrDate(Family $x, Family $y) {
210		return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
211	}
212
213	/**
214	 * Number of children - for the individual list
215	 *
216	 * @return integer
217	 */
218	public function getNumberOfChildren() {
219		$nchi = count($this->getChildren());
220		foreach ($this->getFacts('NCHI') as $fact) {
221			$nchi = max($nchi, (int) $fact->getValue());
222		}
223
224		return $nchi;
225	}
226
227	/**
228	 * get the marriage event
229	 *
230	 * @return Fact
231	 */
232	public function getMarriage() {
233		return $this->getFirstFact('MARR');
234	}
235
236	/**
237	 * Get marriage date
238	 *
239	 * @return Date
240	 */
241	public function getMarriageDate() {
242		$marriage = $this->getMarriage();
243		if ($marriage) {
244			return $marriage->getDate();
245		} else {
246			return new Date('');
247		}
248	}
249
250	/**
251	 * Get the marriage year - displayed on lists of families
252	 *
253	 * @return integer
254	 */
255	public function getMarriageYear() {
256		return $this->getMarriageDate()->minimumDate()->y;
257	}
258
259	/**
260	 * Get the type for this marriage
261	 *
262	 * @return string|null
263	 */
264	public function getMarriageType() {
265		$marriage = $this->getMarriage();
266		if ($marriage) {
267			return $marriage->getAttribute('TYPE');
268		} else {
269			return null;
270		}
271	}
272
273	/**
274	 * Get the marriage place
275	 *
276	 * @return Place
277	 */
278	public function getMarriagePlace() {
279		$marriage = $this->getMarriage();
280
281		return $marriage->getPlace();
282	}
283
284	/**
285	 * Get a list of all marriage dates - for the family lists.
286	 *
287	 * @return Date[]
288	 */
289	public function getAllMarriageDates() {
290		foreach (explode('|', WT_EVENTS_MARR) as $event) {
291			if ($array = $this->getAllEventDates($event)) {
292				return $array;
293			}
294		}
295
296		return array();
297	}
298
299	/**
300	 * Get a list of all marriage places - for the family lists.
301	 *
302	 * @return string[]
303	 */
304	public function getAllMarriagePlaces() {
305		foreach (explode('|', WT_EVENTS_MARR) as $event) {
306			if ($array = $this->getAllEventPlaces($event)) {
307				return $array;
308			}
309		}
310
311		return array();
312	}
313
314	/** {@inheritdoc} */
315	public function getAllNames() {
316		global $UNKNOWN_NN, $UNKNOWN_PN;
317
318		if (is_null($this->_getAllNames)) {
319			// Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
320			if ($this->husb) {
321				$husb_names = $this->husb->getAllNames();
322			} else {
323				$husb_names = array(
324					0 => array(
325						'type' => 'BIRT',
326						'sort' => '@N.N.',
327						'full' => $UNKNOWN_PN, ' ', $UNKNOWN_NN,
328					),
329				);
330			}
331			foreach ($husb_names as $n => $husb_name) {
332				$husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
333			}
334			if ($this->wife) {
335				$wife_names = $this->wife->getAllNames();
336			} else {
337				$wife_names = array(
338					0 => array(
339						'type' => 'BIRT',
340						'sort' => '@N.N.',
341						'full' => $UNKNOWN_PN, ' ', $UNKNOWN_NN,
342					),
343				);
344			}
345			foreach ($wife_names as $n => $wife_name) {
346				$wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
347			}
348			// Add the matched names first
349			foreach ($husb_names as $husb_name) {
350				foreach ($wife_names as $wife_name) {
351					if ($husb_name['type'] != '_MARNM' && $wife_name['type'] != '_MARNM' && $husb_name['script'] == $wife_name['script']) {
352						$this->_getAllNames[] = array(
353							'type' => $husb_name['type'],
354							'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
355							'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
356							// No need for a fullNN entry - we do not currently store FAM names in the database
357						);
358					}
359				}
360			}
361			// Add the unmatched names second (there may be no matched names)
362			foreach ($husb_names as $husb_name) {
363				foreach ($wife_names as $wife_name) {
364					if ($husb_name['type'] != '_MARNM' && $wife_name['type'] != '_MARNM' && $husb_name['script'] != $wife_name['script']) {
365						$this->_getAllNames[] = array(
366							'type' => $husb_name['type'],
367							'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
368							'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
369							// No need for a fullNN entry - we do not currently store FAM names in the database
370						);
371					}
372				}
373			}
374		}
375
376		return $this->_getAllNames;
377	}
378
379	/** {@inheritdoc} */
380	function formatListDetails() {
381		return
382			$this->formatFirstMajorFact(WT_EVENTS_MARR, 1) .
383			$this->formatFirstMajorFact(WT_EVENTS_DIV, 1);
384	}
385}
386