xref: /webtrees/app/Individual.php (revision 764a01d9433bb50d2ecbce7851cba4793c7674e2)
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 Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
22a25f0a04SGreg Roach * Class Individual - Class file for an individual
23a25f0a04SGreg Roach */
24a25f0a04SGreg Roachclass Individual extends GedcomRecord {
25a25f0a04SGreg Roach	const RECORD_TYPE = 'INDI';
26a25f0a04SGreg Roach	const URL_PREFIX = 'individual.php?pid=';
27a25f0a04SGreg Roach
28a25f0a04SGreg Roach	var $generation; // used in some lists to keep track of this individual’s generation in that list
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach	/** @var Date The estimated date of birth */
31a25f0a04SGreg Roach	private $_getEstimatedBirthDate;
32a25f0a04SGreg Roach
33a25f0a04SGreg Roach	/** @var Date The estimated date of death */
34a25f0a04SGreg Roach	private $_getEstimatedDeathDate;
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach	/**
37a25f0a04SGreg Roach	 * Get an instance of an individual object.  For single records,
38a25f0a04SGreg Roach	 * we just receive the XREF.  For bulk records (such as lists
39a25f0a04SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
40a25f0a04SGreg Roach	 *
41a25f0a04SGreg Roach	 * @param string       $xref
42a25f0a04SGreg Roach	 * @param integer|null $gedcom_id
43a25f0a04SGreg Roach	 * @param string|null  $gedcom
44a25f0a04SGreg Roach	 *
45a25f0a04SGreg Roach	 * @return Individual|null
46a25f0a04SGreg Roach	 */
47a25f0a04SGreg Roach	public static function getInstance($xref, $gedcom_id = WT_GED_ID, $gedcom = null) {
48a25f0a04SGreg Roach		$record = parent::getInstance($xref, $gedcom_id, $gedcom);
49a25f0a04SGreg Roach
50a25f0a04SGreg Roach		if ($record instanceof Individual) {
51a25f0a04SGreg Roach			return $record;
52a25f0a04SGreg Roach		} else {
53a25f0a04SGreg Roach			return null;
54a25f0a04SGreg Roach		}
55a25f0a04SGreg Roach	}
56a25f0a04SGreg Roach
57a25f0a04SGreg Roach	/**
58a25f0a04SGreg Roach	 * Can the name of this record be shown?
59a25f0a04SGreg Roach	 *
60a25f0a04SGreg Roach	 * {@inheritdoc}
61a25f0a04SGreg Roach	 */
62a25f0a04SGreg Roach	public function canShowName($access_level = WT_USER_ACCESS_LEVEL) {
63518bbdc1SGreg Roach		return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
64a25f0a04SGreg Roach	}
65a25f0a04SGreg Roach
66a25f0a04SGreg Roach	/**
67a25f0a04SGreg Roach	 * Implement individual-specific privacy logic
68a25f0a04SGreg Roach	 *
69a25f0a04SGreg Roach	 * {@inheritdoc}
70a25f0a04SGreg Roach	 */
71a25f0a04SGreg Roach	protected function canShowByType($access_level) {
72a25f0a04SGreg Roach		// Dead people...
73518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
74a25f0a04SGreg Roach			$keep_alive = false;
75518bbdc1SGreg Roach			$KEEP_ALIVE_YEARS_BIRTH = $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
76a25f0a04SGreg Roach			if ($KEEP_ALIVE_YEARS_BIRTH) {
77a25f0a04SGreg Roach				preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
78a25f0a04SGreg Roach				foreach ($matches as $match) {
79a25f0a04SGreg Roach					$date = new Date($match[1]);
80a25f0a04SGreg Roach					if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
81a25f0a04SGreg Roach						$keep_alive = true;
82a25f0a04SGreg Roach						break;
83a25f0a04SGreg Roach					}
84a25f0a04SGreg Roach				}
85a25f0a04SGreg Roach			}
86518bbdc1SGreg Roach			$KEEP_ALIVE_YEARS_DEATH = $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
87a25f0a04SGreg Roach			if ($KEEP_ALIVE_YEARS_DEATH) {
88a25f0a04SGreg Roach				preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
89a25f0a04SGreg Roach				foreach ($matches as $match) {
90a25f0a04SGreg Roach					$date = new Date($match[1]);
91a25f0a04SGreg Roach					if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
92a25f0a04SGreg Roach						$keep_alive = true;
93a25f0a04SGreg Roach						break;
94a25f0a04SGreg Roach					}
95a25f0a04SGreg Roach				}
96a25f0a04SGreg Roach			}
97a25f0a04SGreg Roach			if (!$keep_alive) {
98a25f0a04SGreg Roach				return true;
99a25f0a04SGreg Roach			}
100a25f0a04SGreg Roach		}
101a25f0a04SGreg Roach		// Consider relationship privacy (unless an admin is applying download restrictions)
102b496965eSGreg Roach		if (WT_USER_GEDCOM_ID && WT_USER_PATH_LENGTH && $this->tree->getTreeId() == WT_GED_ID && $access_level = WT_USER_ACCESS_LEVEL) {
103a25f0a04SGreg Roach			return self::isRelated($this, WT_USER_PATH_LENGTH);
104a25f0a04SGreg Roach		}
105a25f0a04SGreg Roach
106a25f0a04SGreg Roach		// No restriction found - show living people to members only:
107a25f0a04SGreg Roach		return WT_PRIV_USER >= $access_level;
108a25f0a04SGreg Roach	}
109a25f0a04SGreg Roach
110a25f0a04SGreg Roach	/**
111a25f0a04SGreg Roach	 * For relationship privacy calculations - is this individual a close relative?
112a25f0a04SGreg Roach	 *
113a25f0a04SGreg Roach	 * @param Individual $target
114a25f0a04SGreg Roach	 * @param integer       $distance
115a25f0a04SGreg Roach	 *
116a25f0a04SGreg Roach	 * @return boolean
117a25f0a04SGreg Roach	 */
118a25f0a04SGreg Roach	private static function isRelated(Individual $target, $distance) {
119a25f0a04SGreg Roach		static $cache = null;
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach		$user_individual = Individual::getInstance(WT_USER_GEDCOM_ID);
122a25f0a04SGreg Roach		if ($user_individual) {
123a25f0a04SGreg Roach			if (!$cache) {
124a25f0a04SGreg Roach				$cache = array(
125a25f0a04SGreg Roach					0 => array($user_individual),
126a25f0a04SGreg Roach					1 => array(),
127a25f0a04SGreg Roach				);
128a25f0a04SGreg Roach				foreach ($user_individual->getFacts('FAM[CS]', false, WT_PRIV_HIDE) as $fact) {
129a25f0a04SGreg Roach					$family = $fact->getTarget();
130a25f0a04SGreg Roach					if ($family) {
131a25f0a04SGreg Roach						$cache[1][] = $family;
132a25f0a04SGreg Roach					}
133a25f0a04SGreg Roach				}
134a25f0a04SGreg Roach			}
135a25f0a04SGreg Roach		} else {
136a25f0a04SGreg Roach			// No individual linked to this account?  Cannot use relationship privacy.
137a25f0a04SGreg Roach			return true;
138a25f0a04SGreg Roach		}
139a25f0a04SGreg Roach
140a25f0a04SGreg Roach		// Double the distance, as we count the INDI-FAM and FAM-INDI links separately
141a25f0a04SGreg Roach		$distance *= 2;
142a25f0a04SGreg Roach
143a25f0a04SGreg Roach		// Consider each path length in turn
144a25f0a04SGreg Roach		for ($n = 0; $n <= $distance; ++$n) {
145a25f0a04SGreg Roach			if (array_key_exists($n, $cache)) {
146a25f0a04SGreg Roach				// We have already calculated all records with this length
147a25f0a04SGreg Roach				if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
148a25f0a04SGreg Roach					return true;
149a25f0a04SGreg Roach				}
150a25f0a04SGreg Roach			} else {
151a25f0a04SGreg Roach				// Need to calculate these paths
152a25f0a04SGreg Roach				$cache[$n] = array();
153a25f0a04SGreg Roach				if ($n % 2 == 0) {
154a25f0a04SGreg Roach					// Add FAM->INDI links
155a25f0a04SGreg Roach					foreach ($cache[$n - 1] as $family) {
156a25f0a04SGreg Roach						foreach ($family->getFacts('HUSB|WIFE|CHIL', false, WT_PRIV_HIDE) as $fact) {
157a25f0a04SGreg Roach							$individual = $fact->getTarget();
158a25f0a04SGreg Roach							// Don’t backtrack
159a25f0a04SGreg Roach							if ($individual && !in_array($individual, $cache[$n - 2], true)) {
160a25f0a04SGreg Roach								$cache[$n][] = $individual;
161a25f0a04SGreg Roach							}
162a25f0a04SGreg Roach						}
163a25f0a04SGreg Roach					}
164a25f0a04SGreg Roach					if (in_array($target, $cache[$n], true)) {
165a25f0a04SGreg Roach						return true;
166a25f0a04SGreg Roach					}
167a25f0a04SGreg Roach				} else {
168a25f0a04SGreg Roach					// Add INDI->FAM links
169a25f0a04SGreg Roach					foreach ($cache[$n - 1] as $individual) {
170a25f0a04SGreg Roach						foreach ($individual->getFacts('FAM[CS]', false, WT_PRIV_HIDE) as $fact) {
171a25f0a04SGreg Roach							$family = $fact->getTarget();
172a25f0a04SGreg Roach							// Don’t backtrack
173a25f0a04SGreg Roach							if ($family && !in_array($family, $cache[$n - 2], true)) {
174a25f0a04SGreg Roach								$cache[$n][] = $family;
175a25f0a04SGreg Roach							}
176a25f0a04SGreg Roach						}
177a25f0a04SGreg Roach					}
178a25f0a04SGreg Roach				}
179a25f0a04SGreg Roach			}
180a25f0a04SGreg Roach		}
181a25f0a04SGreg Roach
182a25f0a04SGreg Roach		return false;
183a25f0a04SGreg Roach	}
184a25f0a04SGreg Roach
185a25f0a04SGreg Roach	/** {@inheritdoc} */
186a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
187518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
188a25f0a04SGreg Roach
189a25f0a04SGreg Roach		$rec = '0 @' . $this->xref . '@ INDI';
190518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
191a25f0a04SGreg Roach			// Show all the NAME tags, including subtags
192a25f0a04SGreg Roach			foreach ($this->getFacts('NAME') as $fact) {
193a25f0a04SGreg Roach				$rec .= "\n" . $fact->getGedcom();
194a25f0a04SGreg Roach			}
195a25f0a04SGreg Roach		}
196a25f0a04SGreg Roach		// Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
197a25f0a04SGreg Roach		preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
198a25f0a04SGreg Roach		foreach ($matches as $match) {
199a25f0a04SGreg Roach			$rela = Family::getInstance($match[1]);
200a25f0a04SGreg Roach			if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
201a25f0a04SGreg Roach				$rec .= $match[0];
202a25f0a04SGreg Roach			}
203a25f0a04SGreg Roach		}
204a25f0a04SGreg Roach		// Don’t privatize sex.
205a25f0a04SGreg Roach		if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
206a25f0a04SGreg Roach			$rec .= $match[0];
207a25f0a04SGreg Roach		}
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach		return $rec;
210a25f0a04SGreg Roach	}
211a25f0a04SGreg Roach
212a25f0a04SGreg Roach	/** {@inheritdoc} */
213a25f0a04SGreg Roach	protected static function fetchGedcomRecord($xref, $gedcom_id) {
214a25f0a04SGreg Roach		static $statement = null;
215a25f0a04SGreg Roach
216a25f0a04SGreg Roach		if ($statement === null) {
217a25f0a04SGreg Roach			$statement = Database::prepare("SELECT i_gedcom FROM `##individuals` WHERE i_id=? AND i_file=?");
218a25f0a04SGreg Roach		}
219a25f0a04SGreg Roach
220a25f0a04SGreg Roach		return $statement->execute(array($xref, $gedcom_id))->fetchOne();
221a25f0a04SGreg Roach	}
222a25f0a04SGreg Roach
223a25f0a04SGreg Roach	/**
224a25f0a04SGreg Roach	 * Static helper function to sort an array of people by birth date
225a25f0a04SGreg Roach	 *
226a25f0a04SGreg Roach	 * @param Individual $x
227a25f0a04SGreg Roach	 * @param Individual $y
228a25f0a04SGreg Roach	 *
229a25f0a04SGreg Roach	 * @return integer
230a25f0a04SGreg Roach	 */
231a25f0a04SGreg Roach	public static function compareBirthDate(Individual $x, Individual $y) {
232f5b60decSGreg Roach		return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
233a25f0a04SGreg Roach	}
234a25f0a04SGreg Roach
235a25f0a04SGreg Roach	/**
236a25f0a04SGreg Roach	 * Static helper function to sort an array of people by death date
237a25f0a04SGreg Roach	 *
238a25f0a04SGreg Roach	 * @param Individual $x
239a25f0a04SGreg Roach	 * @param Individual $y
240a25f0a04SGreg Roach	 *
241a25f0a04SGreg Roach	 * @return integer
242a25f0a04SGreg Roach	 */
243a25f0a04SGreg Roach	public static function compareDeathDate(Individual $x, Individual $y) {
244f5b60decSGreg Roach		return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
245a25f0a04SGreg Roach	}
246a25f0a04SGreg Roach
247a25f0a04SGreg Roach	/**
248a25f0a04SGreg Roach	 * Calculate whether this individual is living or dead.
249a25f0a04SGreg Roach	 * If not known to be dead, then assume living.
250a25f0a04SGreg Roach	 *
251a25f0a04SGreg Roach	 * @return boolean
252a25f0a04SGreg Roach	 */
253a25f0a04SGreg Roach	public function isDead() {
254518bbdc1SGreg Roach		$MAX_ALIVE_AGE = $this->tree->getPreference('MAX_ALIVE_AGE');
255a25f0a04SGreg Roach
256a25f0a04SGreg Roach		// "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
257a25f0a04SGreg Roach		if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
258a25f0a04SGreg Roach			return true;
259a25f0a04SGreg Roach		}
260a25f0a04SGreg Roach
261a25f0a04SGreg Roach		// If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
262a25f0a04SGreg Roach		if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
263a25f0a04SGreg Roach			foreach ($date_matches[1] as $date_match) {
264a25f0a04SGreg Roach				$date = new Date($date_match);
265f5b60decSGreg Roach				if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
266a25f0a04SGreg Roach					return true;
267a25f0a04SGreg Roach				}
268a25f0a04SGreg Roach			}
269a25f0a04SGreg Roach			// The individual has one or more dated events.  All are less than $MAX_ALIVE_AGE years ago.
270a25f0a04SGreg Roach			// If one of these is a birth, the individual must be alive.
271a25f0a04SGreg Roach			if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
272a25f0a04SGreg Roach				return false;
273a25f0a04SGreg Roach			}
274a25f0a04SGreg Roach		}
275a25f0a04SGreg Roach
276a25f0a04SGreg Roach		// If we found no conclusive dates then check the dates of close relatives.
277a25f0a04SGreg Roach
278a25f0a04SGreg Roach		// Check parents (birth and adopted)
279a25f0a04SGreg Roach		foreach ($this->getChildFamilies(WT_PRIV_HIDE) as $family) {
280a25f0a04SGreg Roach			foreach ($family->getSpouses(WT_PRIV_HIDE) as $parent) {
281a25f0a04SGreg Roach				// Assume parents are no more than 45 years older than their children
282a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
283a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
284a25f0a04SGreg Roach					$date = new Date($date_match);
285f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
286a25f0a04SGreg Roach						return true;
287a25f0a04SGreg Roach					}
288a25f0a04SGreg Roach				}
289a25f0a04SGreg Roach			}
290a25f0a04SGreg Roach		}
291a25f0a04SGreg Roach
292a25f0a04SGreg Roach		// Check spouses
293a25f0a04SGreg Roach		foreach ($this->getSpouseFamilies(WT_PRIV_HIDE) as $family) {
294a25f0a04SGreg Roach			preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
295a25f0a04SGreg Roach			foreach ($date_matches[1] as $date_match) {
296a25f0a04SGreg Roach				$date = new Date($date_match);
297a25f0a04SGreg Roach				// Assume marriage occurs after age of 10
298f5b60decSGreg Roach				if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
299a25f0a04SGreg Roach					return true;
300a25f0a04SGreg Roach				}
301a25f0a04SGreg Roach			}
302a25f0a04SGreg Roach			// Check spouse dates
303a25f0a04SGreg Roach			$spouse = $family->getSpouse($this);
304a25f0a04SGreg Roach			if ($spouse) {
305a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
306a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
307a25f0a04SGreg Roach					$date = new Date($date_match);
308a25f0a04SGreg Roach					// Assume max age difference between spouses of 40 years
309f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
310a25f0a04SGreg Roach						return true;
311a25f0a04SGreg Roach					}
312a25f0a04SGreg Roach				}
313a25f0a04SGreg Roach			}
314a25f0a04SGreg Roach			// Check child dates
315a25f0a04SGreg Roach			foreach ($family->getChildren(WT_PRIV_HIDE) as $child) {
316a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
317a25f0a04SGreg Roach				// Assume children born after age of 15
318a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
319a25f0a04SGreg Roach					$date = new Date($date_match);
320f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
321a25f0a04SGreg Roach						return true;
322a25f0a04SGreg Roach					}
323a25f0a04SGreg Roach				}
324a25f0a04SGreg Roach				// Check grandchildren
325a25f0a04SGreg Roach				foreach ($child->getSpouseFamilies(WT_PRIV_HIDE) as $child_family) {
326a25f0a04SGreg Roach					foreach ($child_family->getChildren(WT_PRIV_HIDE) as $grandchild) {
327a25f0a04SGreg Roach						preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
328a25f0a04SGreg Roach						// Assume grandchildren born after age of 30
329a25f0a04SGreg Roach						foreach ($date_matches[1] as $date_match) {
330a25f0a04SGreg Roach							$date = new Date($date_match);
331f5b60decSGreg Roach							if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
332a25f0a04SGreg Roach								return true;
333a25f0a04SGreg Roach							}
334a25f0a04SGreg Roach						}
335a25f0a04SGreg Roach					}
336a25f0a04SGreg Roach				}
337a25f0a04SGreg Roach			}
338a25f0a04SGreg Roach		}
339a25f0a04SGreg Roach
340a25f0a04SGreg Roach		return false;
341a25f0a04SGreg Roach	}
342a25f0a04SGreg Roach
343a25f0a04SGreg Roach	/**
344a25f0a04SGreg Roach	 * Find the highlighted media object for an individual
345a25f0a04SGreg Roach	 * 1. Ignore all media objects that are not displayable because of Privacy rules
346a25f0a04SGreg Roach	 * 2. Ignore all media objects with the Highlight option set to "N"
347a25f0a04SGreg Roach	 * 3. Pick the first media object that matches these criteria, in order of preference:
348a25f0a04SGreg Roach	 *    (a) Level 1 object with the Highlight option set to "Y"
349a25f0a04SGreg Roach	 *    (b) Level 1 object with the Highlight option missing or set to other than "Y" or "N"
350a25f0a04SGreg Roach	 *    (c) Level 2 or higher object with the Highlight option set to "Y"
351a25f0a04SGreg Roach	 *
352a25f0a04SGreg Roach	 * @return null|Media
353a25f0a04SGreg Roach	 */
354a25f0a04SGreg Roach	function findHighlightedMedia() {
355a25f0a04SGreg Roach		$objectA = null;
356a25f0a04SGreg Roach		$objectB = null;
357a25f0a04SGreg Roach		$objectC = null;
358a25f0a04SGreg Roach
359a25f0a04SGreg Roach		// Iterate over all of the media items for the individual
360a25f0a04SGreg Roach		preg_match_all('/\n(\d) OBJE @(' . WT_REGEX_XREF . ')@/', $this->getGedcom(), $matches, PREG_SET_ORDER);
361a25f0a04SGreg Roach		foreach ($matches as $match) {
362a25f0a04SGreg Roach			$media = Media::getInstance($match[2]);
363a25f0a04SGreg Roach			if (!$media || !$media->canShow() || $media->isExternal()) {
364a25f0a04SGreg Roach				continue;
365a25f0a04SGreg Roach			}
366a25f0a04SGreg Roach			$level = $match[1];
367a25f0a04SGreg Roach			$prim = $media->isPrimary();
368a25f0a04SGreg Roach			if ($prim == 'N') {
369a25f0a04SGreg Roach				continue;
370a25f0a04SGreg Roach			}
371a25f0a04SGreg Roach			if ($level == 1) {
372a25f0a04SGreg Roach				if ($prim == 'Y') {
373a25f0a04SGreg Roach					if (empty($objectA)) {
374a25f0a04SGreg Roach						$objectA = $media;
375a25f0a04SGreg Roach					}
376a25f0a04SGreg Roach				} else {
377a25f0a04SGreg Roach					if (empty($objectB)) {
378a25f0a04SGreg Roach						$objectB = $media;
379a25f0a04SGreg Roach					}
380a25f0a04SGreg Roach				}
381a25f0a04SGreg Roach			} else {
382a25f0a04SGreg Roach				if ($prim == 'Y') {
383a25f0a04SGreg Roach					if (empty($objectC)) {
384a25f0a04SGreg Roach						$objectC = $media;
385a25f0a04SGreg Roach					}
386a25f0a04SGreg Roach				}
387a25f0a04SGreg Roach			}
388a25f0a04SGreg Roach		}
389a25f0a04SGreg Roach
390a25f0a04SGreg Roach		if ($objectA) {
391a25f0a04SGreg Roach			return $objectA;
392a25f0a04SGreg Roach		}
393a25f0a04SGreg Roach		if ($objectB) {
394a25f0a04SGreg Roach			return $objectB;
395a25f0a04SGreg Roach		}
396a25f0a04SGreg Roach		if ($objectC) {
397a25f0a04SGreg Roach			return $objectC;
398a25f0a04SGreg Roach		}
399a25f0a04SGreg Roach
400a25f0a04SGreg Roach		return null;
401a25f0a04SGreg Roach	}
402a25f0a04SGreg Roach
403a25f0a04SGreg Roach	/**
404a25f0a04SGreg Roach	 * Display the prefered image for this individual.
405a25f0a04SGreg Roach	 * Use an icon if no image is available.
406a25f0a04SGreg Roach	 *
407a25f0a04SGreg Roach	 * @return string
408a25f0a04SGreg Roach	 */
409a25f0a04SGreg Roach	public function displayImage() {
410a25f0a04SGreg Roach		$media = $this->findHighlightedMedia();
411a25f0a04SGreg Roach		if ($media) {
412a25f0a04SGreg Roach			// Thumbnail exists - use it.
413a25f0a04SGreg Roach			return $media->displayImage();
414518bbdc1SGreg Roach		} elseif ($this->tree->getPreference('USE_SILHOUETTE')) {
415a25f0a04SGreg Roach			// No thumbnail exists - use an icon
416a25f0a04SGreg Roach			return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
417a25f0a04SGreg Roach		} else {
418a25f0a04SGreg Roach			return '';
419a25f0a04SGreg Roach		}
420a25f0a04SGreg Roach	}
421a25f0a04SGreg Roach
422a25f0a04SGreg Roach	/**
423a25f0a04SGreg Roach	 * Get the date of birth
424a25f0a04SGreg Roach	 *
425a25f0a04SGreg Roach	 * @return Date
426a25f0a04SGreg Roach	 */
427a25f0a04SGreg Roach	function getBirthDate() {
428a25f0a04SGreg Roach		foreach ($this->getAllBirthDates() as $date) {
429a25f0a04SGreg Roach			if ($date->isOK()) {
430a25f0a04SGreg Roach				return $date;
431a25f0a04SGreg Roach			}
432a25f0a04SGreg Roach		}
433a25f0a04SGreg Roach
434a25f0a04SGreg Roach		return new Date('');
435a25f0a04SGreg Roach	}
436a25f0a04SGreg Roach
437a25f0a04SGreg Roach	/**
438a25f0a04SGreg Roach	 * Get the place of birth
439a25f0a04SGreg Roach	 *
440a25f0a04SGreg Roach	 * @return string
441a25f0a04SGreg Roach	 */
442a25f0a04SGreg Roach	function getBirthPlace() {
443a25f0a04SGreg Roach		foreach ($this->getAllBirthPlaces() as $place) {
444a25f0a04SGreg Roach			if ($place) {
445a25f0a04SGreg Roach				return $place;
446a25f0a04SGreg Roach			}
447a25f0a04SGreg Roach		}
448a25f0a04SGreg Roach
449a25f0a04SGreg Roach		return '';
450a25f0a04SGreg Roach	}
451a25f0a04SGreg Roach
452a25f0a04SGreg Roach	/**
453a25f0a04SGreg Roach	 * Get the year of birth
454a25f0a04SGreg Roach	 *
455a25f0a04SGreg Roach	 * @return string the year of birth
456a25f0a04SGreg Roach	 */
457a25f0a04SGreg Roach	function getBirthYear() {
458f5b60decSGreg Roach		return $this->getBirthDate()->minimumDate()->format('%Y');
459a25f0a04SGreg Roach	}
460a25f0a04SGreg Roach
461a25f0a04SGreg Roach	/**
462a25f0a04SGreg Roach	 * Get the date of death
463a25f0a04SGreg Roach	 *
464a25f0a04SGreg Roach	 * @return Date
465a25f0a04SGreg Roach	 */
466a25f0a04SGreg Roach	function getDeathDate() {
467a25f0a04SGreg Roach		foreach ($this->getAllDeathDates() as $date) {
468a25f0a04SGreg Roach			if ($date->isOK()) {
469a25f0a04SGreg Roach				return $date;
470a25f0a04SGreg Roach			}
471a25f0a04SGreg Roach		}
472a25f0a04SGreg Roach
473a25f0a04SGreg Roach		return new Date('');
474a25f0a04SGreg Roach	}
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach	/**
477a25f0a04SGreg Roach	 * Get the place of death
478a25f0a04SGreg Roach	 *
479a25f0a04SGreg Roach	 * @return string
480a25f0a04SGreg Roach	 */
481a25f0a04SGreg Roach	function getDeathPlace() {
482a25f0a04SGreg Roach		foreach ($this->getAllDeathPlaces() as $place) {
483a25f0a04SGreg Roach			if ($place) {
484a25f0a04SGreg Roach				return $place;
485a25f0a04SGreg Roach			}
486a25f0a04SGreg Roach		}
487a25f0a04SGreg Roach
488a25f0a04SGreg Roach		return '';
489a25f0a04SGreg Roach	}
490a25f0a04SGreg Roach
491a25f0a04SGreg Roach	/**
492a25f0a04SGreg Roach	 * get the death year
493a25f0a04SGreg Roach	 *
494a25f0a04SGreg Roach	 * @return string the year of death
495a25f0a04SGreg Roach	 */
496a25f0a04SGreg Roach	function getDeathYear() {
497f5b60decSGreg Roach		return $this->getDeathDate()->minimumDate()->format('%Y');
498a25f0a04SGreg Roach	}
499a25f0a04SGreg Roach
500a25f0a04SGreg Roach	/**
501a25f0a04SGreg Roach	 * Get the range of years in which a individual lived.  e.g. “1870–”, “1870–1920”, “–1920”.
502a25f0a04SGreg Roach	 * Provide the full date using a tooltip.
503a25f0a04SGreg Roach	 * For consistent layout in charts, etc., show just a “–” when no dates are known.
504a25f0a04SGreg Roach	 * Note that this is a (non-breaking) en-dash, and not a hyphen.
505a25f0a04SGreg Roach	 *
506a25f0a04SGreg Roach	 * @return string
507a25f0a04SGreg Roach	 */
508a25f0a04SGreg Roach	public function getLifeSpan() {
509a25f0a04SGreg Roach		return
510a25f0a04SGreg Roach			/* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ I18N::translate(
511a25f0a04SGreg Roach				'%1$s–%2$s',
512f5b60decSGreg Roach				'<span title="' . strip_tags($this->getBirthDate()->display()) . '">' . $this->getBirthDate()->minimumDate()->format('%Y') . '</span>',
513f5b60decSGreg Roach				'<span title="' . strip_tags($this->getDeathDate()->display()) . '">' . $this->getDeathDate()->minimumDate()->format('%Y') . '</span>'
514a25f0a04SGreg Roach			);
515a25f0a04SGreg Roach	}
516a25f0a04SGreg Roach
517a25f0a04SGreg Roach	/**
518a25f0a04SGreg Roach	 * Get all the birth dates - for the individual lists.
519a25f0a04SGreg Roach	 *
520a25f0a04SGreg Roach	 * @return Date[]
521a25f0a04SGreg Roach	 */
522a25f0a04SGreg Roach	function getAllBirthDates() {
523a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_BIRT) as $event) {
524a25f0a04SGreg Roach			$tmp = $this->getAllEventDates($event);
525a25f0a04SGreg Roach			if ($tmp) {
526a25f0a04SGreg Roach				return $tmp;
527a25f0a04SGreg Roach			}
528a25f0a04SGreg Roach		}
529a25f0a04SGreg Roach
530a25f0a04SGreg Roach		return array();
531a25f0a04SGreg Roach	}
532a25f0a04SGreg Roach
533a25f0a04SGreg Roach	/**
534a25f0a04SGreg Roach	 * Gat all the birth places - for the individual lists.
535a25f0a04SGreg Roach	 *
536a25f0a04SGreg Roach	 * @return string[]
537a25f0a04SGreg Roach	 */
538a25f0a04SGreg Roach	function getAllBirthPlaces() {
539a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_BIRT) as $event) {
540a25f0a04SGreg Roach			$tmp = $this->getAllEventPlaces($event);
541a25f0a04SGreg Roach			if ($tmp) {
542a25f0a04SGreg Roach				return $tmp;
543a25f0a04SGreg Roach			}
544a25f0a04SGreg Roach		}
545a25f0a04SGreg Roach
546a25f0a04SGreg Roach		return array();
547a25f0a04SGreg Roach	}
548a25f0a04SGreg Roach
549a25f0a04SGreg Roach	/**
550a25f0a04SGreg Roach	 * Get all the death dates - for the individual lists.
551a25f0a04SGreg Roach	 *
552a25f0a04SGreg Roach	 * @return Date[]
553a25f0a04SGreg Roach	 */
554a25f0a04SGreg Roach	function getAllDeathDates() {
555a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_DEAT) as $event) {
556a25f0a04SGreg Roach			$tmp = $this->getAllEventDates($event);
557a25f0a04SGreg Roach			if ($tmp) {
558a25f0a04SGreg Roach				return $tmp;
559a25f0a04SGreg Roach			}
560a25f0a04SGreg Roach		}
561a25f0a04SGreg Roach
562a25f0a04SGreg Roach		return array();
563a25f0a04SGreg Roach	}
564a25f0a04SGreg Roach
565a25f0a04SGreg Roach	/**
566a25f0a04SGreg Roach	 * Get all the death places - for the individual lists.
567a25f0a04SGreg Roach	 *
568a25f0a04SGreg Roach	 * @return string[]
569a25f0a04SGreg Roach	 */
570a25f0a04SGreg Roach	function getAllDeathPlaces() {
571a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_DEAT) as $event) {
572a25f0a04SGreg Roach			$tmp = $this->getAllEventPlaces($event);
573a25f0a04SGreg Roach			if ($tmp) {
574a25f0a04SGreg Roach				return $tmp;
575a25f0a04SGreg Roach			}
576a25f0a04SGreg Roach		}
577a25f0a04SGreg Roach
578a25f0a04SGreg Roach		return array();
579a25f0a04SGreg Roach	}
580a25f0a04SGreg Roach
581a25f0a04SGreg Roach	/**
582a25f0a04SGreg Roach	 * Generate an estimate for the date of birth, based on dates of parents/children/spouses
583a25f0a04SGreg Roach	 *
584a25f0a04SGreg Roach	 * @return Date
585a25f0a04SGreg Roach	 */
586a25f0a04SGreg Roach	function getEstimatedBirthDate() {
587a25f0a04SGreg Roach		if (is_null($this->_getEstimatedBirthDate)) {
588a25f0a04SGreg Roach			foreach ($this->getAllBirthDates() as $date) {
589a25f0a04SGreg Roach				if ($date->isOK()) {
590a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = $date;
591a25f0a04SGreg Roach					break;
592a25f0a04SGreg Roach				}
593a25f0a04SGreg Roach			}
594a25f0a04SGreg Roach			if (is_null($this->_getEstimatedBirthDate)) {
595a25f0a04SGreg Roach				$min = array();
596a25f0a04SGreg Roach				$max = array();
597a25f0a04SGreg Roach				$tmp = $this->getDeathDate();
598f5b60decSGreg Roach				if ($tmp->isOK()) {
599f5b60decSGreg Roach					$min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
600f5b60decSGreg Roach					$max[] = $tmp->maximumJulianDay();
601a25f0a04SGreg Roach				}
602a25f0a04SGreg Roach				foreach ($this->getChildFamilies() as $family) {
603a25f0a04SGreg Roach					$tmp = $family->getMarriageDate();
604f5b60decSGreg Roach					if ($tmp->isOK()) {
605f5b60decSGreg Roach						$min[] = $tmp->maximumJulianDay() - 365 * 1;
606f5b60decSGreg Roach						$max[] = $tmp->minimumJulianDay() + 365 * 30;
607a25f0a04SGreg Roach					}
608a25f0a04SGreg Roach					if ($parent = $family->getHusband()) {
609a25f0a04SGreg Roach						$tmp = $parent->getBirthDate();
610f5b60decSGreg Roach						if ($tmp->isOK()) {
611f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() + 365 * 15;
612f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 65;
613a25f0a04SGreg Roach						}
614a25f0a04SGreg Roach					}
615a25f0a04SGreg Roach					if ($parent = $family->getWife()) {
616a25f0a04SGreg Roach						$tmp = $parent->getBirthDate();
617f5b60decSGreg Roach						if ($tmp->isOK()) {
618f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() + 365 * 15;
619f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 45;
620a25f0a04SGreg Roach						}
621a25f0a04SGreg Roach					}
622a25f0a04SGreg Roach					foreach ($family->getChildren() as $child) {
623a25f0a04SGreg Roach						$tmp = $child->getBirthDate();
624f5b60decSGreg Roach						if ($tmp->isOK()) {
625f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * 30;
626f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 30;
627a25f0a04SGreg Roach						}
628a25f0a04SGreg Roach					}
629a25f0a04SGreg Roach				}
630a25f0a04SGreg Roach				foreach ($this->getSpouseFamilies() as $family) {
631a25f0a04SGreg Roach					$tmp = $family->getMarriageDate();
632f5b60decSGreg Roach					if ($tmp->isOK()) {
633f5b60decSGreg Roach						$min[] = $tmp->maximumJulianDay() - 365 * 45;
634f5b60decSGreg Roach						$max[] = $tmp->minimumJulianDay() - 365 * 15;
635a25f0a04SGreg Roach					}
636a25f0a04SGreg Roach					$spouse = $family->getSpouse($this);
637a25f0a04SGreg Roach					if ($spouse) {
638a25f0a04SGreg Roach						$tmp = $spouse->getBirthDate();
639f5b60decSGreg Roach						if ($tmp->isOK()) {
640f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * 25;
641f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 25;
642a25f0a04SGreg Roach						}
643a25f0a04SGreg Roach					}
644a25f0a04SGreg Roach					foreach ($family->getChildren() as $child) {
645a25f0a04SGreg Roach						$tmp = $child->getBirthDate();
646f5b60decSGreg Roach						if ($tmp->isOK()) {
647f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
648f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() - 365 * 15;
649a25f0a04SGreg Roach						}
650a25f0a04SGreg Roach					}
651a25f0a04SGreg Roach				}
652a25f0a04SGreg Roach				if ($min && $max) {
653a25f0a04SGreg Roach					$gregorian_calendar = new GregorianCalendar;
654a25f0a04SGreg Roach
655a25f0a04SGreg Roach					list($year) = $gregorian_calendar->jdToYmd((int) ((max($min) + min($max)) / 2));
656a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = new Date('EST ' . $year);
657a25f0a04SGreg Roach				} else {
658a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = new Date(''); // always return a date object
659a25f0a04SGreg Roach				}
660a25f0a04SGreg Roach			}
661a25f0a04SGreg Roach		}
662a25f0a04SGreg Roach
663a25f0a04SGreg Roach		return $this->_getEstimatedBirthDate;
664a25f0a04SGreg Roach	}
665a25f0a04SGreg Roach
666a25f0a04SGreg Roach	/**
667a25f0a04SGreg Roach	 * Generate an estimated date of death.
668a25f0a04SGreg Roach	 *
669a25f0a04SGreg Roach	 * @return Date
670a25f0a04SGreg Roach	 */
671a25f0a04SGreg Roach	function getEstimatedDeathDate() {
672a25f0a04SGreg Roach		if ($this->_getEstimatedDeathDate === null) {
673a25f0a04SGreg Roach			foreach ($this->getAllDeathDates() as $date) {
674a25f0a04SGreg Roach				if ($date->isOK()) {
675a25f0a04SGreg Roach					$this->_getEstimatedDeathDate = $date;
676a25f0a04SGreg Roach					break;
677a25f0a04SGreg Roach				}
678a25f0a04SGreg Roach			}
679a25f0a04SGreg Roach			if ($this->_getEstimatedDeathDate === null) {
680f5b60decSGreg Roach				if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
681f5b60decSGreg Roach					$this->_getEstimatedDeathDate = $this->getEstimatedBirthDate()->addYears($this->tree->getPreference('MAX_ALIVE_AGE'), 'BEF');
682a25f0a04SGreg Roach				} else {
683a25f0a04SGreg Roach					$this->_getEstimatedDeathDate = new Date(''); // always return a date object
684a25f0a04SGreg Roach				}
685a25f0a04SGreg Roach			}
686a25f0a04SGreg Roach		}
687a25f0a04SGreg Roach
688a25f0a04SGreg Roach		return $this->_getEstimatedDeathDate;
689a25f0a04SGreg Roach	}
690a25f0a04SGreg Roach
691a25f0a04SGreg Roach	/**
692a25f0a04SGreg Roach	 * Get the sex - M F or U
693a25f0a04SGreg Roach	 * Use the un-privatised gedcom record.  We call this function during
694a25f0a04SGreg Roach	 * the privatize-gedcom function, and we are allowed to know this.
695a25f0a04SGreg Roach	 *
696a25f0a04SGreg Roach	 * @return string
697a25f0a04SGreg Roach	 */
698a25f0a04SGreg Roach	function getSex() {
699a25f0a04SGreg Roach		if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
700a25f0a04SGreg Roach			return $match[1];
701a25f0a04SGreg Roach		} else {
702a25f0a04SGreg Roach			return 'U';
703a25f0a04SGreg Roach		}
704a25f0a04SGreg Roach	}
705a25f0a04SGreg Roach
706a25f0a04SGreg Roach	/**
707a25f0a04SGreg Roach	 * Get the individual’s sex image
708a25f0a04SGreg Roach	 *
709a25f0a04SGreg Roach	 * @param string $size
710a25f0a04SGreg Roach	 *
711a25f0a04SGreg Roach	 * @return string
712a25f0a04SGreg Roach	 */
713a25f0a04SGreg Roach	function getSexImage($size = 'small') {
714a25f0a04SGreg Roach		return self::sexImage($this->getSex(), $size);
715a25f0a04SGreg Roach	}
716a25f0a04SGreg Roach
717a25f0a04SGreg Roach	/**
718a25f0a04SGreg Roach	 * Generate a sex icon/image
719a25f0a04SGreg Roach	 *
720a25f0a04SGreg Roach	 * @param string $sex
721a25f0a04SGreg Roach	 * @param string $size
722a25f0a04SGreg Roach	 *
723a25f0a04SGreg Roach	 * @return string
724a25f0a04SGreg Roach	 */
725a25f0a04SGreg Roach	static function sexImage($sex, $size = 'small') {
726a25f0a04SGreg Roach		return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
727a25f0a04SGreg Roach	}
728a25f0a04SGreg Roach
729a25f0a04SGreg Roach	/**
730a25f0a04SGreg Roach	 * Generate the CSS class to be used for drawing this individual
731a25f0a04SGreg Roach	 *
732a25f0a04SGreg Roach	 * @return string
733a25f0a04SGreg Roach	 */
734a25f0a04SGreg Roach	function getBoxStyle() {
735a25f0a04SGreg Roach		$tmp = array('M' => '', 'F' => 'F', 'U' => 'NN');
736a25f0a04SGreg Roach
737a25f0a04SGreg Roach		return 'person_box' . $tmp[$this->getSex()];
738a25f0a04SGreg Roach	}
739a25f0a04SGreg Roach
740a25f0a04SGreg Roach	/**
741a25f0a04SGreg Roach	 * Get a list of this individual’s spouse families
742a25f0a04SGreg Roach	 *
743a25f0a04SGreg Roach	 * @param integer $access_level
744a25f0a04SGreg Roach	 *
745a25f0a04SGreg Roach	 * @return Family[]
746a25f0a04SGreg Roach	 */
747a25f0a04SGreg Roach	public function getSpouseFamilies($access_level = WT_USER_ACCESS_LEVEL) {
748518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
749a25f0a04SGreg Roach
750a25f0a04SGreg Roach		$families = array();
751a25f0a04SGreg Roach		foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
752a25f0a04SGreg Roach			$family = $fact->getTarget();
753a25f0a04SGreg Roach			if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
754a25f0a04SGreg Roach				$families[] = $family;
755a25f0a04SGreg Roach			}
756a25f0a04SGreg Roach		}
757a25f0a04SGreg Roach
758a25f0a04SGreg Roach		return $families;
759a25f0a04SGreg Roach	}
760a25f0a04SGreg Roach
761a25f0a04SGreg Roach	/**
762a25f0a04SGreg Roach	 * Get the current spouse of this individual.
763a25f0a04SGreg Roach	 *
764a25f0a04SGreg Roach	 * Where an individual has multiple spouses, assume they are stored
765a25f0a04SGreg Roach	 * in chronological order, and take the last one found.
766a25f0a04SGreg Roach	 *
767a25f0a04SGreg Roach	 * @return Individual|null
768a25f0a04SGreg Roach	 */
769a25f0a04SGreg Roach	public function getCurrentSpouse() {
770a25f0a04SGreg Roach		$tmp = $this->getSpouseFamilies();
771a25f0a04SGreg Roach		$family = end($tmp);
772a25f0a04SGreg Roach		if ($family) {
773a25f0a04SGreg Roach			return $family->getSpouse($this);
774a25f0a04SGreg Roach		} else {
775a25f0a04SGreg Roach			return null;
776a25f0a04SGreg Roach		}
777a25f0a04SGreg Roach	}
778a25f0a04SGreg Roach
779a25f0a04SGreg Roach	/**
780a25f0a04SGreg Roach	 * Count the children belonging to this individual.
781a25f0a04SGreg Roach	 *
782a25f0a04SGreg Roach	 * @return integer
783a25f0a04SGreg Roach	 */
784a25f0a04SGreg Roach	public function getNumberOfChildren() {
785a25f0a04SGreg Roach		if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
786a25f0a04SGreg Roach			return $match[1];
787a25f0a04SGreg Roach		} else {
788a25f0a04SGreg Roach			$children = array();
789a25f0a04SGreg Roach			foreach ($this->getSpouseFamilies() as $fam) {
790a25f0a04SGreg Roach				foreach ($fam->getChildren() as $child) {
791a25f0a04SGreg Roach					$children[$child->getXref()] = true;
792a25f0a04SGreg Roach				}
793a25f0a04SGreg Roach			}
794a25f0a04SGreg Roach
795a25f0a04SGreg Roach			return count($children);
796a25f0a04SGreg Roach		}
797a25f0a04SGreg Roach	}
798a25f0a04SGreg Roach
799a25f0a04SGreg Roach	/**
800a25f0a04SGreg Roach	 * Get a list of this individual’s child families (i.e. their parents).
801a25f0a04SGreg Roach	 *
802a25f0a04SGreg Roach	 * @param integer $access_level
803a25f0a04SGreg Roach	 *
804a25f0a04SGreg Roach	 * @return Family[]
805a25f0a04SGreg Roach	 */
806a25f0a04SGreg Roach	public function getChildFamilies($access_level = WT_USER_ACCESS_LEVEL) {
807518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
808a25f0a04SGreg Roach
809a25f0a04SGreg Roach		$families = array();
810a25f0a04SGreg Roach		foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
811a25f0a04SGreg Roach			$family = $fact->getTarget();
812a25f0a04SGreg Roach			if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
813a25f0a04SGreg Roach				$families[] = $family;
814a25f0a04SGreg Roach			}
815a25f0a04SGreg Roach		}
816a25f0a04SGreg Roach
817a25f0a04SGreg Roach		return $families;
818a25f0a04SGreg Roach	}
819a25f0a04SGreg Roach
820a25f0a04SGreg Roach	/**
821a25f0a04SGreg Roach	 * Get the preferred parents for this individual.
822a25f0a04SGreg Roach	 *
823a25f0a04SGreg Roach	 * An individual may multiple parents (e.g. birth, adopted, disputed).
824a25f0a04SGreg Roach	 * The preferred family record is:
825a25f0a04SGreg Roach	 * (a) the first one with an explicit tag "_PRIMARY Y"
826a25f0a04SGreg Roach	 * (b) the first one with a pedigree of "birth"
827a25f0a04SGreg Roach	 * (c) the first one with no pedigree (default is "birth")
828a25f0a04SGreg Roach	 * (d) the first one found
829a25f0a04SGreg Roach	 *
830a25f0a04SGreg Roach	 * @return Family|null
831a25f0a04SGreg Roach	 */
832a25f0a04SGreg Roach	public function getPrimaryChildFamily() {
833a25f0a04SGreg Roach		$families = $this->getChildFamilies();
834a25f0a04SGreg Roach		switch (count($families)) {
835a25f0a04SGreg Roach		case 0:
836a25f0a04SGreg Roach			return null;
837a25f0a04SGreg Roach		case 1:
838a25f0a04SGreg Roach			return reset($families);
839a25f0a04SGreg Roach		default:
840a25f0a04SGreg Roach			// If there is more than one FAMC record, choose the preferred parents:
841a25f0a04SGreg Roach			// a) records with '2 _PRIMARY'
842a25f0a04SGreg Roach			foreach ($families as $famid => $fam) {
843a25f0a04SGreg Roach				if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
844a25f0a04SGreg Roach					return $fam;
845a25f0a04SGreg Roach				}
846a25f0a04SGreg Roach			}
847a25f0a04SGreg Roach			// b) records with '2 PEDI birt'
848a25f0a04SGreg Roach			foreach ($families as $famid => $fam) {
849a25f0a04SGreg Roach				if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
850a25f0a04SGreg Roach					return $fam;
851a25f0a04SGreg Roach				}
852a25f0a04SGreg Roach			}
853a25f0a04SGreg Roach			// c) records with no '2 PEDI'
854a25f0a04SGreg Roach			foreach ($families as $famid => $fam) {
855a25f0a04SGreg Roach				if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
856a25f0a04SGreg Roach					return $fam;
857a25f0a04SGreg Roach				}
858a25f0a04SGreg Roach			}
859a25f0a04SGreg Roach
860a25f0a04SGreg Roach			// d) any record
861a25f0a04SGreg Roach			return reset($families);
862a25f0a04SGreg Roach		}
863a25f0a04SGreg Roach	}
864a25f0a04SGreg Roach
865a25f0a04SGreg Roach	/**
866a25f0a04SGreg Roach	 * Get a list of step-parent families.
867a25f0a04SGreg Roach	 *
868a25f0a04SGreg Roach	 * @return Family[]
869a25f0a04SGreg Roach	 */
870a25f0a04SGreg Roach	function getChildStepFamilies() {
871a25f0a04SGreg Roach		$step_families = array();
872a25f0a04SGreg Roach		$families = $this->getChildFamilies();
873a25f0a04SGreg Roach		foreach ($families as $family) {
874a25f0a04SGreg Roach			$father = $family->getHusband();
875a25f0a04SGreg Roach			if ($father) {
876a25f0a04SGreg Roach				foreach ($father->getSpouseFamilies() as $step_family) {
877a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
878a25f0a04SGreg Roach						$step_families[] = $step_family;
879a25f0a04SGreg Roach					}
880a25f0a04SGreg Roach				}
881a25f0a04SGreg Roach			}
882a25f0a04SGreg Roach			$mother = $family->getWife();
883a25f0a04SGreg Roach			if ($mother) {
884a25f0a04SGreg Roach				foreach ($mother->getSpouseFamilies() as $step_family) {
885a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
886a25f0a04SGreg Roach						$step_families[] = $step_family;
887a25f0a04SGreg Roach					}
888a25f0a04SGreg Roach				}
889a25f0a04SGreg Roach			}
890a25f0a04SGreg Roach		}
891a25f0a04SGreg Roach
892a25f0a04SGreg Roach		return $step_families;
893a25f0a04SGreg Roach	}
894a25f0a04SGreg Roach
895a25f0a04SGreg Roach	/**
896a25f0a04SGreg Roach	 * Get a list of step-parent families.
897a25f0a04SGreg Roach	 *
898a25f0a04SGreg Roach	 * @return Family[]
899a25f0a04SGreg Roach	 */
900a25f0a04SGreg Roach	function getSpouseStepFamilies() {
901a25f0a04SGreg Roach		$step_families = array();
902a25f0a04SGreg Roach		$families = $this->getSpouseFamilies();
903a25f0a04SGreg Roach		foreach ($families as $family) {
904a25f0a04SGreg Roach			$spouse = $family->getSpouse($this);
905a25f0a04SGreg Roach			if ($spouse) {
906a25f0a04SGreg Roach				foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
907a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
908a25f0a04SGreg Roach						$step_families[] = $step_family;
909a25f0a04SGreg Roach					}
910a25f0a04SGreg Roach				}
911a25f0a04SGreg Roach			}
912a25f0a04SGreg Roach		}
913a25f0a04SGreg Roach
914a25f0a04SGreg Roach		return $step_families;
915a25f0a04SGreg Roach	}
916a25f0a04SGreg Roach
917a25f0a04SGreg Roach	/**
918a25f0a04SGreg Roach	 * A label for a parental family group
919a25f0a04SGreg Roach	 *
920a25f0a04SGreg Roach	 * @param Family $family
921a25f0a04SGreg Roach	 *
922a25f0a04SGreg Roach	 * @return string
923a25f0a04SGreg Roach	 */
924a25f0a04SGreg Roach	function getChildFamilyLabel(Family $family) {
925a25f0a04SGreg Roach		if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
926a25f0a04SGreg Roach			// A specified pedigree
927*764a01d9SGreg Roach			return GedcomCodePedi::getChildFamilyLabel($match[1]);
928a25f0a04SGreg Roach		} else {
929a25f0a04SGreg Roach			// Default (birth) pedigree
930*764a01d9SGreg Roach			return GedcomCodePedi::getChildFamilyLabel('');
931a25f0a04SGreg Roach		}
932a25f0a04SGreg Roach	}
933a25f0a04SGreg Roach
934a25f0a04SGreg Roach	/**
935a25f0a04SGreg Roach	 * Create a label for a step family
936a25f0a04SGreg Roach	 *
937a25f0a04SGreg Roach	 * @param Family $step_family
938a25f0a04SGreg Roach	 *
939a25f0a04SGreg Roach	 * @return string
940a25f0a04SGreg Roach	 */
941a25f0a04SGreg Roach	function getStepFamilyLabel(Family $step_family) {
942a25f0a04SGreg Roach		foreach ($this->getChildFamilies() as $family) {
943a25f0a04SGreg Roach			if ($family !== $step_family) {
944a25f0a04SGreg Roach				// Must be a step-family
945a25f0a04SGreg Roach				foreach ($family->getSpouses() as $parent) {
946a25f0a04SGreg Roach					foreach ($step_family->getSpouses() as $step_parent) {
947a25f0a04SGreg Roach						if ($parent === $step_parent) {
948a25f0a04SGreg Roach							// One common parent - must be a step family
949a25f0a04SGreg Roach							if ($parent->getSex() == 'M') {
950a25f0a04SGreg Roach								// Father’s family with someone else
951a25f0a04SGreg Roach								if ($step_family->getSpouse($step_parent)) {
952a25f0a04SGreg Roach									return
953a25f0a04SGreg Roach										/* I18N: A step-family.  %s is an individual’s name */
954a25f0a04SGreg Roach										I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
955a25f0a04SGreg Roach								} else {
956a25f0a04SGreg Roach									return
957a25f0a04SGreg Roach										/* I18N: A step-family. */
958a25f0a04SGreg Roach										I18N::translate('Father’s family with an unknown individual');
959a25f0a04SGreg Roach								}
960a25f0a04SGreg Roach							} else {
961a25f0a04SGreg Roach								// Mother’s family with someone else
962a25f0a04SGreg Roach								if ($step_family->getSpouse($step_parent)) {
963a25f0a04SGreg Roach									return
964a25f0a04SGreg Roach										/* I18N: A step-family.  %s is an individual’s name */
965a25f0a04SGreg Roach										I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
966a25f0a04SGreg Roach								} else {
967a25f0a04SGreg Roach									return
968a25f0a04SGreg Roach										/* I18N: A step-family. */
969a25f0a04SGreg Roach										I18N::translate('Mother’s family with an unknown individual');
970a25f0a04SGreg Roach								}
971a25f0a04SGreg Roach							}
972a25f0a04SGreg Roach						}
973a25f0a04SGreg Roach					}
974a25f0a04SGreg Roach				}
975a25f0a04SGreg Roach			}
976a25f0a04SGreg Roach		}
977a25f0a04SGreg Roach
978a25f0a04SGreg Roach		// Perahps same parents - but a different family record?
979a25f0a04SGreg Roach		return I18N::translate('Family with parents');
980a25f0a04SGreg Roach	}
981a25f0a04SGreg Roach
982a25f0a04SGreg Roach	/**
983a25f0a04SGreg Roach	 *
984a25f0a04SGreg Roach	 * @todo this function does not belong in this class
985a25f0a04SGreg Roach	 *
986a25f0a04SGreg Roach	 * @param Family $family
987a25f0a04SGreg Roach	 *
988a25f0a04SGreg Roach	 * @return string
989a25f0a04SGreg Roach	 */
990a25f0a04SGreg Roach	function getSpouseFamilyLabel(Family $family) {
991a25f0a04SGreg Roach		$spouse = $family->getSpouse($this);
992a25f0a04SGreg Roach		if ($spouse) {
993a25f0a04SGreg Roach			return
994a25f0a04SGreg Roach				/* I18N: %s is the spouse name */
995a25f0a04SGreg Roach				I18N::translate('Family with %s', $spouse->getFullName());
996a25f0a04SGreg Roach		} else {
997a25f0a04SGreg Roach			return $family->getFullName();
998a25f0a04SGreg Roach		}
999a25f0a04SGreg Roach	}
1000a25f0a04SGreg Roach
1001a25f0a04SGreg Roach	/**
1002a25f0a04SGreg Roach	 * get primary parents names for this individual
1003a25f0a04SGreg Roach	 *
1004a25f0a04SGreg Roach	 * @param string $classname optional css class
1005a25f0a04SGreg Roach	 * @param string $display   optional css style display
1006a25f0a04SGreg Roach	 *
1007a25f0a04SGreg Roach	 * @return string a div block with father & mother names
1008a25f0a04SGreg Roach	 */
1009a25f0a04SGreg Roach	function getPrimaryParentsNames($classname = '', $display = '') {
1010a25f0a04SGreg Roach		$fam = $this->getPrimaryChildFamily();
1011a25f0a04SGreg Roach		if (!$fam) {
1012a25f0a04SGreg Roach			return '';
1013a25f0a04SGreg Roach		}
1014a25f0a04SGreg Roach		$txt = '<div';
1015a25f0a04SGreg Roach		if ($classname) {
1016a25f0a04SGreg Roach			$txt .= " class=\"$classname\"";
1017a25f0a04SGreg Roach		}
1018a25f0a04SGreg Roach		if ($display) {
1019a25f0a04SGreg Roach			$txt .= " style=\"display:$display\"";
1020a25f0a04SGreg Roach		}
1021a25f0a04SGreg Roach		$txt .= '>';
1022a25f0a04SGreg Roach		$husb = $fam->getHusband();
1023a25f0a04SGreg Roach		if ($husb) {
1024a25f0a04SGreg Roach			// Temporarily reset the 'prefered' display name, as we always
1025a25f0a04SGreg Roach			// want the default name, not the one selected for display on the indilist.
1026a25f0a04SGreg Roach			$primary = $husb->getPrimaryName();
1027a25f0a04SGreg Roach			$husb->setPrimaryName(null);
1028a25f0a04SGreg Roach			$txt .=
1029a25f0a04SGreg Roach				/* I18N: %s is the name of an individual’s father */
1030a25f0a04SGreg Roach				I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1031a25f0a04SGreg Roach			$husb->setPrimaryName($primary);
1032a25f0a04SGreg Roach		}
1033a25f0a04SGreg Roach		$wife = $fam->getWife();
1034a25f0a04SGreg Roach		if ($wife) {
1035a25f0a04SGreg Roach			// Temporarily reset the 'prefered' display name, as we always
1036a25f0a04SGreg Roach			// want the default name, not the one selected for display on the indilist.
1037a25f0a04SGreg Roach			$primary = $wife->getPrimaryName();
1038a25f0a04SGreg Roach			$wife->setPrimaryName(null);
1039a25f0a04SGreg Roach			$txt .=
1040a25f0a04SGreg Roach				/* I18N: %s is the name of an individual’s mother */
1041a25f0a04SGreg Roach				I18N::translate('Mother: %s', $wife->getFullName());
1042a25f0a04SGreg Roach			$wife->setPrimaryName($primary);
1043a25f0a04SGreg Roach		}
1044a25f0a04SGreg Roach		$txt .= '</div>';
1045a25f0a04SGreg Roach
1046a25f0a04SGreg Roach		return $txt;
1047a25f0a04SGreg Roach	}
1048a25f0a04SGreg Roach
1049a25f0a04SGreg Roach	/** {@inheritdoc} */
1050a25f0a04SGreg Roach	function getFallBackName() {
1051a25f0a04SGreg Roach		return '@P.N. /@N.N./';
1052a25f0a04SGreg Roach	}
1053a25f0a04SGreg Roach
1054a25f0a04SGreg Roach	/**
1055a25f0a04SGreg Roach	 * Convert a name record into ‘full’ and ‘sort’ versions.
1056a25f0a04SGreg Roach	 * Use the NAME field to generate the ‘full’ version, as the
1057a25f0a04SGreg Roach	 * gedcom spec says that this is the individual’s name, as they would write it.
1058a25f0a04SGreg Roach	 * Use the SURN field to generate the sortable names.  Note that this field
1059a25f0a04SGreg Roach	 * may also be used for the ‘true’ surname, perhaps spelt differently to that
1060a25f0a04SGreg Roach	 * recorded in the NAME field. e.g.
1061a25f0a04SGreg Roach	 *
1062a25f0a04SGreg Roach	 * 1 NAME Robert /de Gliderow/
1063a25f0a04SGreg Roach	 * 2 GIVN Robert
1064a25f0a04SGreg Roach	 * 2 SPFX de
1065a25f0a04SGreg Roach	 * 2 SURN CLITHEROW
1066a25f0a04SGreg Roach	 * 2 NICK The Bald
1067a25f0a04SGreg Roach	 *
1068a25f0a04SGreg Roach	 * full=>'Robert de Gliderow 'The Bald''
1069a25f0a04SGreg Roach	 * sort=>'CLITHEROW, ROBERT'
1070a25f0a04SGreg Roach	 *
1071a25f0a04SGreg Roach	 * Handle multiple surnames, either as;
1072a25f0a04SGreg Roach	 *
1073a25f0a04SGreg Roach	 * 1 NAME Carlos /Vasquez/ y /Sante/
1074a25f0a04SGreg Roach	 * or
1075a25f0a04SGreg Roach	 * 1 NAME Carlos /Vasquez y Sante/
1076a25f0a04SGreg Roach	 * 2 GIVN Carlos
1077a25f0a04SGreg Roach	 * 2 SURN Vasquez,Sante
1078a25f0a04SGreg Roach	 *
1079a25f0a04SGreg Roach	 * @param string $type
1080a25f0a04SGreg Roach	 * @param string $full
1081a25f0a04SGreg Roach	 * @param string $gedcom
1082a25f0a04SGreg Roach	 */
1083a25f0a04SGreg Roach	protected function addName($type, $full, $gedcom) {
1084a25f0a04SGreg Roach		global $UNKNOWN_NN, $UNKNOWN_PN;
1085a25f0a04SGreg Roach
1086a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1087a25f0a04SGreg Roach		// Extract the structured name parts - use for "sortable" names and indexes
1088a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1089a25f0a04SGreg Roach
1090a25f0a04SGreg Roach		$sublevel = 1 + (int) $gedcom[0];
1091a25f0a04SGreg Roach		$NPFX = preg_match("/\n{$sublevel} NPFX (.+)/", $gedcom, $match) ? $match[1] : '';
1092a25f0a04SGreg Roach		$GIVN = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1093a25f0a04SGreg Roach		$SURN = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1094a25f0a04SGreg Roach		$NSFX = preg_match("/\n{$sublevel} NSFX (.+)/", $gedcom, $match) ? $match[1] : '';
1095a25f0a04SGreg Roach		$NICK = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1096a25f0a04SGreg Roach
1097a25f0a04SGreg Roach		// SURN is an comma-separated list of surnames...
1098a25f0a04SGreg Roach		if ($SURN) {
1099a25f0a04SGreg Roach			$SURNS = preg_split('/ *, */', $SURN);
1100a25f0a04SGreg Roach		} else {
1101a25f0a04SGreg Roach			$SURNS = array();
1102a25f0a04SGreg Roach		}
1103a25f0a04SGreg Roach		// ...so is GIVN - but nobody uses it like that
1104a25f0a04SGreg Roach		$GIVN = str_replace('/ *, */', ' ', $GIVN);
1105a25f0a04SGreg Roach
1106a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1107a25f0a04SGreg Roach		// Extract the components from NAME - use for the "full" names
1108a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1109a25f0a04SGreg Roach
1110a25f0a04SGreg Roach		// Fix bad slashes.  e.g. 'John/Smith' => 'John/Smith/'
1111a25f0a04SGreg Roach		if (substr_count($full, '/') % 2 == 1) {
1112a25f0a04SGreg Roach			$full = $full . '/';
1113a25f0a04SGreg Roach		}
1114a25f0a04SGreg Roach
1115a25f0a04SGreg Roach		// GEDCOM uses "//" to indicate an unknown surname
1116a25f0a04SGreg Roach		$full = preg_replace('/\/\//', '/@N.N./', $full);
1117a25f0a04SGreg Roach
1118a25f0a04SGreg Roach		// Extract the surname.
1119a25f0a04SGreg Roach		// Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1120a25f0a04SGreg Roach		if (preg_match('/\/.*\//', $full, $match)) {
1121a25f0a04SGreg Roach			$surname = str_replace('/', '', $match[0]);
1122a25f0a04SGreg Roach		} else {
1123a25f0a04SGreg Roach			$surname = '';
1124a25f0a04SGreg Roach		}
1125a25f0a04SGreg Roach
1126a25f0a04SGreg Roach		// If we don’t have a SURN record, extract it from the NAME
1127a25f0a04SGreg Roach		if (!$SURNS) {
1128a25f0a04SGreg Roach			if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1129a25f0a04SGreg Roach				// There can be many surnames, each wrapped with '/'
1130a25f0a04SGreg Roach				$SURNS = $matches[1];
1131a25f0a04SGreg Roach				foreach ($SURNS as $n => $SURN) {
1132a25f0a04SGreg Roach					// Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1133a25f0a04SGreg Roach					$SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1134a25f0a04SGreg Roach				}
1135a25f0a04SGreg Roach			} else {
1136a25f0a04SGreg Roach				// It is valid not to have a surname at all
1137a25f0a04SGreg Roach				$SURNS = array('');
1138a25f0a04SGreg Roach			}
1139a25f0a04SGreg Roach		}
1140a25f0a04SGreg Roach
1141a25f0a04SGreg Roach		// If we don’t have a GIVN record, extract it from the NAME
1142a25f0a04SGreg Roach		if (!$GIVN) {
1143a25f0a04SGreg Roach			$GIVN = preg_replace(
1144a25f0a04SGreg Roach				array(
1145a25f0a04SGreg Roach					'/ ?\/.*\/ ?/', // remove surname
1146a25f0a04SGreg Roach					'/ ?".+"/', // remove nickname
1147a25f0a04SGreg Roach					'/ {2,}/', // multiple spaces, caused by the above
1148a25f0a04SGreg Roach					'/^ | $/', // leading/trailing spaces, caused by the above
1149a25f0a04SGreg Roach				),
1150a25f0a04SGreg Roach				array(
1151a25f0a04SGreg Roach					' ',
1152a25f0a04SGreg Roach					' ',
1153a25f0a04SGreg Roach					' ',
1154a25f0a04SGreg Roach					'',
1155a25f0a04SGreg Roach				),
1156a25f0a04SGreg Roach				$full
1157a25f0a04SGreg Roach			);
1158a25f0a04SGreg Roach		}
1159a25f0a04SGreg Roach
1160a25f0a04SGreg Roach		// Add placeholder for unknown given name
1161a25f0a04SGreg Roach		if (!$GIVN) {
1162a25f0a04SGreg Roach			$GIVN = '@P.N.';
1163a25f0a04SGreg Roach			$pos = strpos($full, '/');
1164a25f0a04SGreg Roach			$full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1165a25f0a04SGreg Roach		}
1166a25f0a04SGreg Roach
1167a25f0a04SGreg Roach		// The NPFX field might be present, but not appear in the NAME
1168a25f0a04SGreg Roach		if ($NPFX && strpos($full, "$NPFX ") !== 0) {
1169a25f0a04SGreg Roach			$full = "$NPFX $full";
1170a25f0a04SGreg Roach		}
1171a25f0a04SGreg Roach
1172a25f0a04SGreg Roach		// The NSFX field might be present, but not appear in the NAME
1173a25f0a04SGreg Roach		if ($NSFX && strrpos($full, " $NSFX") !== strlen($full) - strlen(" $NSFX")) {
1174a25f0a04SGreg Roach			$full = "$full $NSFX";
1175a25f0a04SGreg Roach		}
1176a25f0a04SGreg Roach
1177a25f0a04SGreg Roach		// GEDCOM nicknames should be specificied in a NICK field, or in the
1178a25f0a04SGreg Roach		// NAME filed, surrounded by ASCII quotes (or both).
1179a25f0a04SGreg Roach		if ($NICK) {
1180a92f322dSGreg Roach			// A NICK field is present.
1181a92f322dSGreg Roach			$QNICK = /* I18N: Place a nickname in quotation marks */ I18N::translate('“%s”', $NICK);
1182a25f0a04SGreg Roach
1183a92f322dSGreg Roach			if (strpos($full, '"' . $NICK . '"') !== false) {
1184a92f322dSGreg Roach				// NICK present in name.  Localise ASCII quotes.
1185a92f322dSGreg Roach				$full = str_replace('"' . $NICK . '"', $QNICK, $full);
1186a25f0a04SGreg Roach			} else {
1187a25f0a04SGreg Roach				// NICK not present in NAME.
1188a25f0a04SGreg Roach				$pos = strpos($full, '/');
1189a25f0a04SGreg Roach				if ($pos === false) {
1190a25f0a04SGreg Roach					// No surname - append it
1191a25f0a04SGreg Roach					$full .= ' ' . $QNICK;
1192a25f0a04SGreg Roach				} else {
1193a25f0a04SGreg Roach					// Insert before surname
1194a25f0a04SGreg Roach					$full = substr($full, 0, $pos) . $QNICK . ' ' . substr($full, $pos);
1195a25f0a04SGreg Roach				}
1196a25f0a04SGreg Roach			}
1197a92f322dSGreg Roach		} else {
1198a92f322dSGreg Roach			// No NICK field - but nickname may be included in NAME (in quotes)
1199a92f322dSGreg Roach			$full = preg_replace_callback('/"([^"]*)"/', function($matches) { return I18N::translate('“%s”', $matches[1]); }, $full);
1200a25f0a04SGreg Roach		}
1201a25f0a04SGreg Roach
1202a25f0a04SGreg Roach		// Remove slashes - they don’t get displayed
1203a25f0a04SGreg Roach		// $fullNN keeps the @N.N. placeholders, for the database
1204a25f0a04SGreg Roach		// $full is for display on-screen
1205a25f0a04SGreg Roach		$fullNN = str_replace('/', '', $full);
1206a25f0a04SGreg Roach
1207a25f0a04SGreg Roach		// Insert placeholders for any missing/unknown names
1208a25f0a04SGreg Roach		if (strpos($full, '@N.N.') !== false) {
1209a25f0a04SGreg Roach			$full = str_replace('@N.N.', $UNKNOWN_NN, $full);
1210a25f0a04SGreg Roach		}
1211a25f0a04SGreg Roach		if (strpos($full, '@P.N.') !== false) {
1212a25f0a04SGreg Roach			$full = str_replace('@P.N.', $UNKNOWN_PN, $full);
1213a25f0a04SGreg Roach		}
1214a25f0a04SGreg Roach		$full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', Filter::escapeHtml($full)) . '</span>';
1215a25f0a04SGreg Roach
1216a25f0a04SGreg Roach		// The standards say you should use a suffix of '*' for preferred name
1217a25f0a04SGreg Roach		$full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1218a25f0a04SGreg Roach
1219a25f0a04SGreg Roach		// Remove prefered-name indicater - they don’t go in the database
1220a25f0a04SGreg Roach		$GIVN = str_replace('*', '', $GIVN);
1221a25f0a04SGreg Roach		$fullNN = str_replace('*', '', $fullNN);
1222a25f0a04SGreg Roach
1223a25f0a04SGreg Roach		foreach ($SURNS AS $SURN) {
1224a25f0a04SGreg Roach			// Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1225a25f0a04SGreg Roach			if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1226a25f0a04SGreg Roach				$SURN = substr_replace($SURN, 'Mac', 0, 2);
1227a25f0a04SGreg Roach			} elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1228a25f0a04SGreg Roach				$SURN = substr_replace($SURN, 'Mac', 0, 4);
1229a25f0a04SGreg Roach			}
1230a25f0a04SGreg Roach
1231a25f0a04SGreg Roach			$this->_getAllNames[] = array(
1232a25f0a04SGreg Roach				'type'    => $type,
1233a25f0a04SGreg Roach				'sort'    => $SURN . ',' . $GIVN,
1234a25f0a04SGreg Roach				'full'    => $full, // This is used for display
1235a25f0a04SGreg Roach				'fullNN'  => $fullNN, // This goes into the database
1236a25f0a04SGreg Roach				'surname' => $surname, // This goes into the database
1237a25f0a04SGreg Roach				'givn'    => $GIVN, // This goes into the database
1238a25f0a04SGreg Roach				'surn'    => $SURN, // This goes into the database
1239a25f0a04SGreg Roach			);
1240a25f0a04SGreg Roach		}
1241a25f0a04SGreg Roach	}
1242a25f0a04SGreg Roach
1243a25f0a04SGreg Roach	/**
1244a25f0a04SGreg Roach	 * Get an array of structures containing all the names in the record
1245a25f0a04SGreg Roach	 */
1246a25f0a04SGreg Roach	public function extractNames() {
124736a0c51dSGreg Roach		$this->extractNamesFromFacts(1, 'NAME', $this->getFacts('NAME', false, WT_USER_ACCESS_LEVEL, $this->canShowName()));
1248a25f0a04SGreg Roach	}
1249a25f0a04SGreg Roach
1250a25f0a04SGreg Roach	/**
1251a25f0a04SGreg Roach	 * Extra info to display when displaying this record in a list of
1252a25f0a04SGreg Roach	 * selection items or favorites.
1253a25f0a04SGreg Roach	 *
1254a25f0a04SGreg Roach	 * @return string
1255a25f0a04SGreg Roach	 */
1256a25f0a04SGreg Roach	function formatListDetails() {
1257a25f0a04SGreg Roach		return
1258841014f1SGreg Roach			$this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1259841014f1SGreg Roach			$this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1260a25f0a04SGreg Roach	}
1261a25f0a04SGreg Roach
1262a25f0a04SGreg Roach	/**
1263a25f0a04SGreg Roach	 * Create a short name for compact display on charts
1264a25f0a04SGreg Roach	 *
1265a25f0a04SGreg Roach	 * @return string
1266a25f0a04SGreg Roach	 */
1267a25f0a04SGreg Roach	public function getShortName() {
1268518bbdc1SGreg Roach		global $bwidth, $UNKNOWN_NN, $UNKNOWN_PN;
1269a25f0a04SGreg Roach
1270a25f0a04SGreg Roach		// Estimate number of characters that can fit in box. Calulates to 28 characters in webtrees theme, or 34 if no thumbnail used.
1271518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_HIGHLIGHT_IMAGES')) {
1272a25f0a04SGreg Roach			$char = intval(($bwidth - 40) / 6.5);
1273a25f0a04SGreg Roach		} else {
1274a25f0a04SGreg Roach			$char = ($bwidth / 6.5);
1275a25f0a04SGreg Roach		}
1276a25f0a04SGreg Roach		if ($this->canShowName()) {
1277a25f0a04SGreg Roach			$tmp = $this->getAllNames();
1278a25f0a04SGreg Roach			$givn = $tmp[$this->getPrimaryName()]['givn'];
1279a25f0a04SGreg Roach			$surn = $tmp[$this->getPrimaryName()]['surname'];
1280a25f0a04SGreg Roach			$new_givn = explode(' ', $givn);
1281a25f0a04SGreg Roach			$count_givn = count($new_givn);
1282a25f0a04SGreg Roach			$len_givn = mb_strlen($givn);
1283a25f0a04SGreg Roach			$len_surn = mb_strlen($surn);
1284a25f0a04SGreg Roach			$len = $len_givn + $len_surn;
1285a25f0a04SGreg Roach			$i = 1;
1286a25f0a04SGreg Roach			while ($len > $char && $i <= $count_givn) {
1287a25f0a04SGreg Roach				$new_givn[$count_givn - $i] = mb_substr($new_givn[$count_givn - $i], 0, 1);
1288a25f0a04SGreg Roach				$givn = implode(' ', $new_givn);
1289a25f0a04SGreg Roach				$len_givn = mb_strlen($givn);
1290a25f0a04SGreg Roach				$len = $len_givn + $len_surn;
1291a25f0a04SGreg Roach				$i++;
1292a25f0a04SGreg Roach			}
1293a25f0a04SGreg Roach			$max_surn = $char - $i * 2;
1294a25f0a04SGreg Roach			if ($len_surn > $max_surn) {
1295a25f0a04SGreg Roach				$surn = substr($surn, 0, $max_surn) . '…';
1296a25f0a04SGreg Roach			}
1297a25f0a04SGreg Roach			$shortname = str_replace(
1298a25f0a04SGreg Roach				array('@P.N.', '@N.N.'),
1299a25f0a04SGreg Roach				array($UNKNOWN_PN, $UNKNOWN_NN),
1300a25f0a04SGreg Roach				$givn . ' ' . $surn
1301a25f0a04SGreg Roach			);
1302a25f0a04SGreg Roach
1303a25f0a04SGreg Roach			return $shortname;
1304a25f0a04SGreg Roach		} else {
1305a25f0a04SGreg Roach			return I18N::translate('Private');
1306a25f0a04SGreg Roach		}
1307a25f0a04SGreg Roach	}
1308a25f0a04SGreg Roach}
1309