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