xref: /webtrees/app/Individual.php (revision dce0740103e59b292d66e287384d4ab36cd3d614)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
2276692c8bSGreg Roach * A GEDCOM individual (INDI) object.
23a25f0a04SGreg Roach */
24a25f0a04SGreg Roachclass Individual extends GedcomRecord {
25a25f0a04SGreg Roach	const RECORD_TYPE = 'INDI';
26a25f0a04SGreg Roach	const URL_PREFIX  = 'individual.php?pid=';
27a25f0a04SGreg Roach
2876692c8bSGreg Roach	/** @var int used in some lists to keep track of this individual’s generation in that list */
2976692c8bSGreg Roach	public $generation;
30a25f0a04SGreg Roach
31a25f0a04SGreg Roach	/** @var Date The estimated date of birth */
32a25f0a04SGreg Roach	private $_getEstimatedBirthDate;
33a25f0a04SGreg Roach
34a25f0a04SGreg Roach	/** @var Date The estimated date of death */
35a25f0a04SGreg Roach	private $_getEstimatedDeathDate;
36a25f0a04SGreg Roach
37a25f0a04SGreg Roach	/**
38395f0fe0SGreg Roach	 * Sometimes, we'll know in advance that we need to load a set of records.
39395f0fe0SGreg Roach	 * Typically when we load families and their members.
40395f0fe0SGreg Roach	 *
41395f0fe0SGreg Roach	 * @param Tree  $tree
424a8aaa00SScrutinizer Auto-Fixer	 * @param string[] $xrefs
43395f0fe0SGreg Roach	 */
44395f0fe0SGreg Roach	public static function load(Tree $tree, array $xrefs) {
4513abd6f3SGreg Roach		$args = [
46395f0fe0SGreg Roach			'tree_id' => $tree->getTreeId(),
4713abd6f3SGreg Roach		];
4813abd6f3SGreg Roach		$placeholders = [];
49395f0fe0SGreg Roach
50395f0fe0SGreg Roach		foreach (array_unique($xrefs) as $n => $xref) {
51395f0fe0SGreg Roach			if (!isset(self::$gedcom_record_cache[$tree->getTreeId()][$xref])) {
52f7247c73SGreg Roach				$placeholders[] = ':x' . $n;
53395f0fe0SGreg Roach				$args['x' . $n] = $xref;
54395f0fe0SGreg Roach			}
55395f0fe0SGreg Roach		}
56395f0fe0SGreg Roach
57f7247c73SGreg Roach		if (!empty($placeholders)) {
58395f0fe0SGreg Roach			$rows = Database::prepare(
59395f0fe0SGreg Roach				"SELECT i_id AS xref, i_gedcom AS gedcom" .
60395f0fe0SGreg Roach				" FROM `##individuals`" .
61f7247c73SGreg Roach				" WHERE i_file = :tree_id AND i_id IN (" . implode(',', $placeholders) . ")"
62395f0fe0SGreg Roach			)->execute(
63395f0fe0SGreg Roach				$args
64395f0fe0SGreg Roach			)->fetchAll();
65395f0fe0SGreg Roach
66395f0fe0SGreg Roach			foreach ($rows as $row) {
67395f0fe0SGreg Roach				self::getInstance($row->xref, $tree, $row->gedcom);
68395f0fe0SGreg Roach			}
69395f0fe0SGreg Roach		}
70395f0fe0SGreg Roach	}
71395f0fe0SGreg Roach
72395f0fe0SGreg Roach	/**
73a25f0a04SGreg Roach	 * Can the name of this record be shown?
74a25f0a04SGreg Roach	 *
7576692c8bSGreg Roach	 * @param int|null $access_level
7676692c8bSGreg Roach	 *
7776692c8bSGreg Roach	 * @return bool
78a25f0a04SGreg Roach	 */
794b9ff166SGreg Roach	public function canShowName($access_level = null) {
804b9ff166SGreg Roach		if ($access_level === null) {
814b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
824b9ff166SGreg Roach		}
834b9ff166SGreg Roach
84518bbdc1SGreg Roach		return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
85a25f0a04SGreg Roach	}
86a25f0a04SGreg Roach
87a25f0a04SGreg Roach	/**
8876692c8bSGreg Roach	 * Can this individual be shown?
89a25f0a04SGreg Roach	 *
9076692c8bSGreg Roach	 * @param int $access_level
9176692c8bSGreg Roach	 *
9276692c8bSGreg Roach	 * @return bool
93a25f0a04SGreg Roach	 */
94a25f0a04SGreg Roach	protected function canShowByType($access_level) {
9524ec66ceSGreg Roach		global $WT_TREE;
9624ec66ceSGreg Roach
97a25f0a04SGreg Roach		// Dead people...
98518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
99a25f0a04SGreg Roach			$keep_alive             = false;
100518bbdc1SGreg Roach			$KEEP_ALIVE_YEARS_BIRTH = $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
101a25f0a04SGreg Roach			if ($KEEP_ALIVE_YEARS_BIRTH) {
102a25f0a04SGreg Roach				preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
103a25f0a04SGreg Roach				foreach ($matches as $match) {
104a25f0a04SGreg Roach					$date = new Date($match[1]);
105a25f0a04SGreg Roach					if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
106a25f0a04SGreg Roach						$keep_alive = true;
107a25f0a04SGreg Roach						break;
108a25f0a04SGreg Roach					}
109a25f0a04SGreg Roach				}
110a25f0a04SGreg Roach			}
111518bbdc1SGreg Roach			$KEEP_ALIVE_YEARS_DEATH = $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
112a25f0a04SGreg Roach			if ($KEEP_ALIVE_YEARS_DEATH) {
113a25f0a04SGreg Roach				preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
114a25f0a04SGreg Roach				foreach ($matches as $match) {
115a25f0a04SGreg Roach					$date = new Date($match[1]);
116a25f0a04SGreg Roach					if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
117a25f0a04SGreg Roach						$keep_alive = true;
118a25f0a04SGreg Roach						break;
119a25f0a04SGreg Roach					}
120a25f0a04SGreg Roach				}
121a25f0a04SGreg Roach			}
122a25f0a04SGreg Roach			if (!$keep_alive) {
123a25f0a04SGreg Roach				return true;
124a25f0a04SGreg Roach			}
125a25f0a04SGreg Roach		}
126a25f0a04SGreg Roach		// Consider relationship privacy (unless an admin is applying download restrictions)
1274b9ff166SGreg Roach		$user_path_length = $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1284b9ff166SGreg Roach		$gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
12924ec66ceSGreg Roach		if ($gedcomid && $user_path_length && $this->tree->getTreeId() == $WT_TREE->getTreeId() && $access_level = Auth::accessLevel($this->tree)) {
1304b9ff166SGreg Roach			return self::isRelated($this, $user_path_length);
131a25f0a04SGreg Roach		}
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach		// No restriction found - show living people to members only:
1344b9ff166SGreg Roach		return Auth::PRIV_USER >= $access_level;
135a25f0a04SGreg Roach	}
136a25f0a04SGreg Roach
137a25f0a04SGreg Roach	/**
138a25f0a04SGreg Roach	 * For relationship privacy calculations - is this individual a close relative?
139a25f0a04SGreg Roach	 *
140a25f0a04SGreg Roach	 * @param Individual $target
141cbc1590aSGreg Roach	 * @param int        $distance
142a25f0a04SGreg Roach	 *
143cbc1590aSGreg Roach	 * @return bool
144a25f0a04SGreg Roach	 */
145a25f0a04SGreg Roach	private static function isRelated(Individual $target, $distance) {
146a25f0a04SGreg Roach		static $cache = null;
147a25f0a04SGreg Roach
14806ef8e02SGreg Roach		$user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
149a25f0a04SGreg Roach		if ($user_individual) {
150a25f0a04SGreg Roach			if (!$cache) {
15113abd6f3SGreg Roach				$cache = [
15213abd6f3SGreg Roach					0 => [$user_individual],
15313abd6f3SGreg Roach					1 => [],
15413abd6f3SGreg Roach				];
1554b9ff166SGreg Roach				foreach ($user_individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
156a25f0a04SGreg Roach					$family = $fact->getTarget();
157a25f0a04SGreg Roach					if ($family) {
158a25f0a04SGreg Roach						$cache[1][] = $family;
159a25f0a04SGreg Roach					}
160a25f0a04SGreg Roach				}
161a25f0a04SGreg Roach			}
162a25f0a04SGreg Roach		} else {
163a25f0a04SGreg Roach			// No individual linked to this account? Cannot use relationship privacy.
164a25f0a04SGreg Roach			return true;
165a25f0a04SGreg Roach		}
166a25f0a04SGreg Roach
167a25f0a04SGreg Roach		// Double the distance, as we count the INDI-FAM and FAM-INDI links separately
168a25f0a04SGreg Roach		$distance *= 2;
169a25f0a04SGreg Roach
170a25f0a04SGreg Roach		// Consider each path length in turn
171a25f0a04SGreg Roach		for ($n = 0; $n <= $distance; ++$n) {
172a25f0a04SGreg Roach			if (array_key_exists($n, $cache)) {
173a25f0a04SGreg Roach				// We have already calculated all records with this length
174a25f0a04SGreg Roach				if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
175a25f0a04SGreg Roach					return true;
176a25f0a04SGreg Roach				}
177a25f0a04SGreg Roach			} else {
178a25f0a04SGreg Roach				// Need to calculate these paths
17913abd6f3SGreg Roach				$cache[$n] = [];
180a25f0a04SGreg Roach				if ($n % 2 == 0) {
181a25f0a04SGreg Roach					// Add FAM->INDI links
182a25f0a04SGreg Roach					foreach ($cache[$n - 1] as $family) {
1834b9ff166SGreg Roach						foreach ($family->getFacts('HUSB|WIFE|CHIL', false, Auth::PRIV_HIDE) as $fact) {
184a25f0a04SGreg Roach							$individual = $fact->getTarget();
185a25f0a04SGreg Roach							// Don’t backtrack
186a25f0a04SGreg Roach							if ($individual && !in_array($individual, $cache[$n - 2], true)) {
187a25f0a04SGreg Roach								$cache[$n][] = $individual;
188a25f0a04SGreg Roach							}
189a25f0a04SGreg Roach						}
190a25f0a04SGreg Roach					}
191a25f0a04SGreg Roach					if (in_array($target, $cache[$n], true)) {
192a25f0a04SGreg Roach						return true;
193a25f0a04SGreg Roach					}
194a25f0a04SGreg Roach				} else {
195a25f0a04SGreg Roach					// Add INDI->FAM links
196a25f0a04SGreg Roach					foreach ($cache[$n - 1] as $individual) {
1974b9ff166SGreg Roach						foreach ($individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
198a25f0a04SGreg Roach							$family = $fact->getTarget();
199a25f0a04SGreg Roach							// Don’t backtrack
200a25f0a04SGreg Roach							if ($family && !in_array($family, $cache[$n - 2], true)) {
201a25f0a04SGreg Roach								$cache[$n][] = $family;
202a25f0a04SGreg Roach							}
203a25f0a04SGreg Roach						}
204a25f0a04SGreg Roach					}
205a25f0a04SGreg Roach				}
206a25f0a04SGreg Roach			}
207a25f0a04SGreg Roach		}
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach		return false;
210a25f0a04SGreg Roach	}
211a25f0a04SGreg Roach
21276692c8bSGreg Roach	/**
21376692c8bSGreg Roach	 * Generate a private version of this record
21476692c8bSGreg Roach	 *
21576692c8bSGreg Roach	 * @param int $access_level
21676692c8bSGreg Roach	 *
21776692c8bSGreg Roach	 * @return string
21876692c8bSGreg Roach	 */
219a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
220518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
221a25f0a04SGreg Roach
222a25f0a04SGreg Roach		$rec = '0 @' . $this->xref . '@ INDI';
223518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
224a25f0a04SGreg Roach			// Show all the NAME tags, including subtags
225a25f0a04SGreg Roach			foreach ($this->getFacts('NAME') as $fact) {
226a25f0a04SGreg Roach				$rec .= "\n" . $fact->getGedcom();
227a25f0a04SGreg Roach			}
228a25f0a04SGreg Roach		}
229a25f0a04SGreg Roach		// Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
230a25f0a04SGreg Roach		preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
231a25f0a04SGreg Roach		foreach ($matches as $match) {
23224ec66ceSGreg Roach			$rela = Family::getInstance($match[1], $this->tree);
233a25f0a04SGreg Roach			if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
234a25f0a04SGreg Roach				$rec .= $match[0];
235a25f0a04SGreg Roach			}
236a25f0a04SGreg Roach		}
237a25f0a04SGreg Roach		// Don’t privatize sex.
238a25f0a04SGreg Roach		if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
239a25f0a04SGreg Roach			$rec .= $match[0];
240a25f0a04SGreg Roach		}
241a25f0a04SGreg Roach
242a25f0a04SGreg Roach		return $rec;
243a25f0a04SGreg Roach	}
244a25f0a04SGreg Roach
24576692c8bSGreg Roach	/**
24676692c8bSGreg Roach	 * Fetch data from the database
24776692c8bSGreg Roach	 *
24876692c8bSGreg Roach	 * @param string $xref
24976692c8bSGreg Roach	 * @param int    $tree_id
25076692c8bSGreg Roach	 *
25176692c8bSGreg Roach	 * @return null|string
25276692c8bSGreg Roach	 */
25364d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
25464d9078aSGreg Roach		return Database::prepare(
25564d9078aSGreg Roach			"SELECT i_gedcom FROM `##individuals` WHERE i_id = :xref AND i_file = :tree_id"
25613abd6f3SGreg Roach		)->execute([
25764d9078aSGreg Roach			'xref'    => $xref,
25864d9078aSGreg Roach			'tree_id' => $tree_id,
25913abd6f3SGreg Roach		])->fetchOne();
260a25f0a04SGreg Roach	}
261a25f0a04SGreg Roach
262a25f0a04SGreg Roach	/**
263a25f0a04SGreg Roach	 * Static helper function to sort an array of people by birth date
264a25f0a04SGreg Roach	 *
265a25f0a04SGreg Roach	 * @param Individual $x
266a25f0a04SGreg Roach	 * @param Individual $y
267a25f0a04SGreg Roach	 *
268cbc1590aSGreg Roach	 * @return int
269a25f0a04SGreg Roach	 */
270a25f0a04SGreg Roach	public static function compareBirthDate(Individual $x, Individual $y) {
271f5b60decSGreg Roach		return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
272a25f0a04SGreg Roach	}
273a25f0a04SGreg Roach
274a25f0a04SGreg Roach	/**
275a25f0a04SGreg Roach	 * Static helper function to sort an array of people by death date
276a25f0a04SGreg Roach	 *
277a25f0a04SGreg Roach	 * @param Individual $x
278a25f0a04SGreg Roach	 * @param Individual $y
279a25f0a04SGreg Roach	 *
280cbc1590aSGreg Roach	 * @return int
281a25f0a04SGreg Roach	 */
282a25f0a04SGreg Roach	public static function compareDeathDate(Individual $x, Individual $y) {
283f5b60decSGreg Roach		return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
284a25f0a04SGreg Roach	}
285a25f0a04SGreg Roach
286a25f0a04SGreg Roach	/**
287a25f0a04SGreg Roach	 * Calculate whether this individual is living or dead.
288a25f0a04SGreg Roach	 * If not known to be dead, then assume living.
289a25f0a04SGreg Roach	 *
290cbc1590aSGreg Roach	 * @return bool
291a25f0a04SGreg Roach	 */
292a25f0a04SGreg Roach	public function isDead() {
293518bbdc1SGreg Roach		$MAX_ALIVE_AGE = $this->tree->getPreference('MAX_ALIVE_AGE');
294a25f0a04SGreg Roach
295a25f0a04SGreg Roach		// "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
296a25f0a04SGreg Roach		if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
297a25f0a04SGreg Roach			return true;
298a25f0a04SGreg Roach		}
299a25f0a04SGreg Roach
300a25f0a04SGreg Roach		// If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
301a25f0a04SGreg Roach		if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
302a25f0a04SGreg Roach			foreach ($date_matches[1] as $date_match) {
303a25f0a04SGreg Roach				$date = new Date($date_match);
304f5b60decSGreg Roach				if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
305a25f0a04SGreg Roach					return true;
306a25f0a04SGreg Roach				}
307a25f0a04SGreg Roach			}
308a25f0a04SGreg Roach			// The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
309a25f0a04SGreg Roach			// If one of these is a birth, the individual must be alive.
310a25f0a04SGreg Roach			if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
311a25f0a04SGreg Roach				return false;
312a25f0a04SGreg Roach			}
313a25f0a04SGreg Roach		}
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach		// If we found no conclusive dates then check the dates of close relatives.
316a25f0a04SGreg Roach
317a25f0a04SGreg Roach		// Check parents (birth and adopted)
3184b9ff166SGreg Roach		foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3194b9ff166SGreg Roach			foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
320a25f0a04SGreg Roach				// Assume parents are no more than 45 years older than their children
321a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
322a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
323a25f0a04SGreg Roach					$date = new Date($date_match);
324f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
325a25f0a04SGreg Roach						return true;
326a25f0a04SGreg Roach					}
327a25f0a04SGreg Roach				}
328a25f0a04SGreg Roach			}
329a25f0a04SGreg Roach		}
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach		// Check spouses
3324b9ff166SGreg Roach		foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
333a25f0a04SGreg Roach			preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
334a25f0a04SGreg Roach			foreach ($date_matches[1] as $date_match) {
335a25f0a04SGreg Roach				$date = new Date($date_match);
336a25f0a04SGreg Roach				// Assume marriage occurs after age of 10
337f5b60decSGreg Roach				if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
338a25f0a04SGreg Roach					return true;
339a25f0a04SGreg Roach				}
340a25f0a04SGreg Roach			}
341a25f0a04SGreg Roach			// Check spouse dates
34213e3123bSGreg Roach			$spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
343a25f0a04SGreg Roach			if ($spouse) {
344a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
345a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
346a25f0a04SGreg Roach					$date = new Date($date_match);
347a25f0a04SGreg Roach					// Assume max age difference between spouses of 40 years
348f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
349a25f0a04SGreg Roach						return true;
350a25f0a04SGreg Roach					}
351a25f0a04SGreg Roach				}
352a25f0a04SGreg Roach			}
353a25f0a04SGreg Roach			// Check child dates
3544b9ff166SGreg Roach			foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
355a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
356a25f0a04SGreg Roach				// Assume children born after age of 15
357a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
358a25f0a04SGreg Roach					$date = new Date($date_match);
359f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
360a25f0a04SGreg Roach						return true;
361a25f0a04SGreg Roach					}
362a25f0a04SGreg Roach				}
363a25f0a04SGreg Roach				// Check grandchildren
3644b9ff166SGreg Roach				foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
3654b9ff166SGreg Roach					foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
366a25f0a04SGreg Roach						preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
367a25f0a04SGreg Roach						// Assume grandchildren born after age of 30
368a25f0a04SGreg Roach						foreach ($date_matches[1] as $date_match) {
369a25f0a04SGreg Roach							$date = new Date($date_match);
370f5b60decSGreg Roach							if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
371a25f0a04SGreg Roach								return true;
372a25f0a04SGreg Roach							}
373a25f0a04SGreg Roach						}
374a25f0a04SGreg Roach					}
375a25f0a04SGreg Roach				}
376a25f0a04SGreg Roach			}
377a25f0a04SGreg Roach		}
378a25f0a04SGreg Roach
379a25f0a04SGreg Roach		return false;
380a25f0a04SGreg Roach	}
381a25f0a04SGreg Roach
382a25f0a04SGreg Roach	/**
383a25f0a04SGreg Roach	 * Find the highlighted media object for an individual
384a25f0a04SGreg Roach	 * 1. Ignore all media objects that are not displayable because of Privacy rules
385a25f0a04SGreg Roach	 * 2. Ignore all media objects with the Highlight option set to "N"
386a25f0a04SGreg Roach	 * 3. Pick the first media object that matches these criteria, in order of preference:
387a25f0a04SGreg Roach	 *    (a) Level 1 object with the Highlight option set to "Y"
388a25f0a04SGreg Roach	 *    (b) Level 1 object with the Highlight option missing or set to other than "Y" or "N"
389a25f0a04SGreg Roach	 *    (c) Level 2 or higher object with the Highlight option set to "Y"
390a25f0a04SGreg Roach	 *
391a25f0a04SGreg Roach	 * @return null|Media
392a25f0a04SGreg Roach	 */
393ffd703eaSGreg Roach	public function findHighlightedMedia() {
394a25f0a04SGreg Roach		$objectA = null;
395a25f0a04SGreg Roach		$objectB = null;
396a25f0a04SGreg Roach		$objectC = null;
397a25f0a04SGreg Roach
398a25f0a04SGreg Roach		// Iterate over all of the media items for the individual
399a25f0a04SGreg Roach		preg_match_all('/\n(\d) OBJE @(' . WT_REGEX_XREF . ')@/', $this->getGedcom(), $matches, PREG_SET_ORDER);
400a25f0a04SGreg Roach		foreach ($matches as $match) {
40124ec66ceSGreg Roach			$media = Media::getInstance($match[2], $this->tree);
402a25f0a04SGreg Roach			if (!$media || !$media->canShow() || $media->isExternal()) {
403a25f0a04SGreg Roach				continue;
404a25f0a04SGreg Roach			}
405a25f0a04SGreg Roach			$level = $match[1];
406a25f0a04SGreg Roach			$prim  = $media->isPrimary();
407a25f0a04SGreg Roach			if ($prim == 'N') {
408a25f0a04SGreg Roach				continue;
409a25f0a04SGreg Roach			}
410a25f0a04SGreg Roach			if ($level == 1) {
411a25f0a04SGreg Roach				if ($prim == 'Y') {
412a25f0a04SGreg Roach					if (empty($objectA)) {
413a25f0a04SGreg Roach						$objectA = $media;
414a25f0a04SGreg Roach					}
415a25f0a04SGreg Roach				} else {
416a25f0a04SGreg Roach					if (empty($objectB)) {
417a25f0a04SGreg Roach						$objectB = $media;
418a25f0a04SGreg Roach					}
419a25f0a04SGreg Roach				}
420a25f0a04SGreg Roach			} else {
421a25f0a04SGreg Roach				if ($prim == 'Y') {
422a25f0a04SGreg Roach					if (empty($objectC)) {
423a25f0a04SGreg Roach						$objectC = $media;
424a25f0a04SGreg Roach					}
425a25f0a04SGreg Roach				}
426a25f0a04SGreg Roach			}
427a25f0a04SGreg Roach		}
428a25f0a04SGreg Roach
429a25f0a04SGreg Roach		if ($objectA) {
430a25f0a04SGreg Roach			return $objectA;
431a25f0a04SGreg Roach		}
432a25f0a04SGreg Roach		if ($objectB) {
433a25f0a04SGreg Roach			return $objectB;
434a25f0a04SGreg Roach		}
435a25f0a04SGreg Roach		if ($objectC) {
436a25f0a04SGreg Roach			return $objectC;
437a25f0a04SGreg Roach		}
438a25f0a04SGreg Roach
439a25f0a04SGreg Roach		return null;
440a25f0a04SGreg Roach	}
441a25f0a04SGreg Roach
442a25f0a04SGreg Roach	/**
443a25f0a04SGreg Roach	 * Display the prefered image for this individual.
444a25f0a04SGreg Roach	 * Use an icon if no image is available.
445a25f0a04SGreg Roach	 *
446*dce07401SGreg Roach	 * @param int      $width      Pixels
447*dce07401SGreg Roach	 * @param int      $height     Pixels
448*dce07401SGreg Roach	 * @param string   $fit        "crop" or "contain"
449*dce07401SGreg Roach	 * @param string[] $attributes Additional HTML attributes
450*dce07401SGreg Roach	 *
451a25f0a04SGreg Roach	 * @return string
452a25f0a04SGreg Roach	 */
453*dce07401SGreg Roach	public function displayImage($width, $height, $fit, $attributes) {
454a25f0a04SGreg Roach		$media = $this->findHighlightedMedia();
455*dce07401SGreg Roach		if ($media !== null) {
456*dce07401SGreg Roach			// Image exists - use it.
457*dce07401SGreg Roach			return $media->displayImage($width, $height, $fit, $attributes);
458518bbdc1SGreg Roach		} elseif ($this->tree->getPreference('USE_SILHOUETTE')) {
459*dce07401SGreg Roach			// No image exists - use an icon
460a25f0a04SGreg Roach			return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
461a25f0a04SGreg Roach		} else {
462a25f0a04SGreg Roach			return '';
463a25f0a04SGreg Roach		}
464a25f0a04SGreg Roach	}
465a25f0a04SGreg Roach
466a25f0a04SGreg Roach	/**
467a25f0a04SGreg Roach	 * Get the date of birth
468a25f0a04SGreg Roach	 *
469a25f0a04SGreg Roach	 * @return Date
470a25f0a04SGreg Roach	 */
471ffd703eaSGreg Roach	public function getBirthDate() {
472a25f0a04SGreg Roach		foreach ($this->getAllBirthDates() as $date) {
473a25f0a04SGreg Roach			if ($date->isOK()) {
474a25f0a04SGreg Roach				return $date;
475a25f0a04SGreg Roach			}
476a25f0a04SGreg Roach		}
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach		return new Date('');
479a25f0a04SGreg Roach	}
480a25f0a04SGreg Roach
481a25f0a04SGreg Roach	/**
482a25f0a04SGreg Roach	 * Get the place of birth
483a25f0a04SGreg Roach	 *
484a25f0a04SGreg Roach	 * @return string
485a25f0a04SGreg Roach	 */
486ffd703eaSGreg Roach	public function getBirthPlace() {
487a25f0a04SGreg Roach		foreach ($this->getAllBirthPlaces() as $place) {
488a25f0a04SGreg Roach			if ($place) {
489a25f0a04SGreg Roach				return $place;
490a25f0a04SGreg Roach			}
491a25f0a04SGreg Roach		}
492a25f0a04SGreg Roach
493a25f0a04SGreg Roach		return '';
494a25f0a04SGreg Roach	}
495a25f0a04SGreg Roach
496a25f0a04SGreg Roach	/**
497a25f0a04SGreg Roach	 * Get the year of birth
498a25f0a04SGreg Roach	 *
499a25f0a04SGreg Roach	 * @return string the year of birth
500a25f0a04SGreg Roach	 */
501ffd703eaSGreg Roach	public function getBirthYear() {
502f5b60decSGreg Roach		return $this->getBirthDate()->minimumDate()->format('%Y');
503a25f0a04SGreg Roach	}
504a25f0a04SGreg Roach
505a25f0a04SGreg Roach	/**
506a25f0a04SGreg Roach	 * Get the date of death
507a25f0a04SGreg Roach	 *
508a25f0a04SGreg Roach	 * @return Date
509a25f0a04SGreg Roach	 */
510ffd703eaSGreg Roach	public function getDeathDate() {
511a25f0a04SGreg Roach		foreach ($this->getAllDeathDates() as $date) {
512a25f0a04SGreg Roach			if ($date->isOK()) {
513a25f0a04SGreg Roach				return $date;
514a25f0a04SGreg Roach			}
515a25f0a04SGreg Roach		}
516a25f0a04SGreg Roach
517a25f0a04SGreg Roach		return new Date('');
518a25f0a04SGreg Roach	}
519a25f0a04SGreg Roach
520a25f0a04SGreg Roach	/**
521a25f0a04SGreg Roach	 * Get the place of death
522a25f0a04SGreg Roach	 *
523a25f0a04SGreg Roach	 * @return string
524a25f0a04SGreg Roach	 */
525ffd703eaSGreg Roach	public function getDeathPlace() {
526a25f0a04SGreg Roach		foreach ($this->getAllDeathPlaces() as $place) {
527a25f0a04SGreg Roach			if ($place) {
528a25f0a04SGreg Roach				return $place;
529a25f0a04SGreg Roach			}
530a25f0a04SGreg Roach		}
531a25f0a04SGreg Roach
532a25f0a04SGreg Roach		return '';
533a25f0a04SGreg Roach	}
534a25f0a04SGreg Roach
535a25f0a04SGreg Roach	/**
536a25f0a04SGreg Roach	 * get the death year
537a25f0a04SGreg Roach	 *
538a25f0a04SGreg Roach	 * @return string the year of death
539a25f0a04SGreg Roach	 */
540ffd703eaSGreg Roach	public function getDeathYear() {
541f5b60decSGreg Roach		return $this->getDeathDate()->minimumDate()->format('%Y');
542a25f0a04SGreg Roach	}
543a25f0a04SGreg Roach
544a25f0a04SGreg Roach	/**
545a25f0a04SGreg Roach	 * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
54615d603e7SGreg Roach	 * Provide the place and full date using a tooltip.
547a25f0a04SGreg Roach	 * For consistent layout in charts, etc., show just a “–” when no dates are known.
548a25f0a04SGreg Roach	 * Note that this is a (non-breaking) en-dash, and not a hyphen.
549a25f0a04SGreg Roach	 *
550a25f0a04SGreg Roach	 * @return string
551a25f0a04SGreg Roach	 */
552a25f0a04SGreg Roach	public function getLifeSpan() {
55315d603e7SGreg Roach		// Just the first part of the place name
55415d603e7SGreg Roach		$birth_place = preg_replace('/,.*/', '', $this->getBirthPlace());
55515d603e7SGreg Roach		$death_place = preg_replace('/,.*/', '', $this->getDeathPlace());
55615d603e7SGreg Roach		// Remove markup from dates
55715d603e7SGreg Roach		$birth_date = strip_tags($this->getBirthDate()->display());
55815d603e7SGreg Roach		$death_date = strip_tags($this->getDeathDate()->display());
55915d603e7SGreg Roach
560a25f0a04SGreg Roach		return
561a25f0a04SGreg Roach			/* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ I18N::translate(
562a25f0a04SGreg Roach				'%1$s–%2$s',
56315d603e7SGreg Roach				'<span title="' . $birth_place . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
56415d603e7SGreg Roach				'<span title="' . $death_place . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
565a25f0a04SGreg Roach			);
566a25f0a04SGreg Roach	}
567a25f0a04SGreg Roach
568a25f0a04SGreg Roach	/**
569a25f0a04SGreg Roach	 * Get all the birth dates - for the individual lists.
570a25f0a04SGreg Roach	 *
571a25f0a04SGreg Roach	 * @return Date[]
572a25f0a04SGreg Roach	 */
573ffd703eaSGreg Roach	public function getAllBirthDates() {
574a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_BIRT) as $event) {
575a25f0a04SGreg Roach			$tmp = $this->getAllEventDates($event);
576a25f0a04SGreg Roach			if ($tmp) {
577a25f0a04SGreg Roach				return $tmp;
578a25f0a04SGreg Roach			}
579a25f0a04SGreg Roach		}
580a25f0a04SGreg Roach
58113abd6f3SGreg Roach		return [];
582a25f0a04SGreg Roach	}
583a25f0a04SGreg Roach
584a25f0a04SGreg Roach	/**
585a25f0a04SGreg Roach	 * Gat all the birth places - for the individual lists.
586a25f0a04SGreg Roach	 *
587a25f0a04SGreg Roach	 * @return string[]
588a25f0a04SGreg Roach	 */
589ffd703eaSGreg Roach	public function getAllBirthPlaces() {
590a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_BIRT) as $event) {
591a25f0a04SGreg Roach			$tmp = $this->getAllEventPlaces($event);
592a25f0a04SGreg Roach			if ($tmp) {
593a25f0a04SGreg Roach				return $tmp;
594a25f0a04SGreg Roach			}
595a25f0a04SGreg Roach		}
596a25f0a04SGreg Roach
59713abd6f3SGreg Roach		return [];
598a25f0a04SGreg Roach	}
599a25f0a04SGreg Roach
600a25f0a04SGreg Roach	/**
601a25f0a04SGreg Roach	 * Get all the death dates - for the individual lists.
602a25f0a04SGreg Roach	 *
603a25f0a04SGreg Roach	 * @return Date[]
604a25f0a04SGreg Roach	 */
605ffd703eaSGreg Roach	public function getAllDeathDates() {
606a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_DEAT) as $event) {
607a25f0a04SGreg Roach			$tmp = $this->getAllEventDates($event);
608a25f0a04SGreg Roach			if ($tmp) {
609a25f0a04SGreg Roach				return $tmp;
610a25f0a04SGreg Roach			}
611a25f0a04SGreg Roach		}
612a25f0a04SGreg Roach
61313abd6f3SGreg Roach		return [];
614a25f0a04SGreg Roach	}
615a25f0a04SGreg Roach
616a25f0a04SGreg Roach	/**
617a25f0a04SGreg Roach	 * Get all the death places - for the individual lists.
618a25f0a04SGreg Roach	 *
619a25f0a04SGreg Roach	 * @return string[]
620a25f0a04SGreg Roach	 */
621ffd703eaSGreg Roach	public function getAllDeathPlaces() {
622a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_DEAT) as $event) {
623a25f0a04SGreg Roach			$tmp = $this->getAllEventPlaces($event);
624a25f0a04SGreg Roach			if ($tmp) {
625a25f0a04SGreg Roach				return $tmp;
626a25f0a04SGreg Roach			}
627a25f0a04SGreg Roach		}
628a25f0a04SGreg Roach
62913abd6f3SGreg Roach		return [];
630a25f0a04SGreg Roach	}
631a25f0a04SGreg Roach
632a25f0a04SGreg Roach	/**
633a25f0a04SGreg Roach	 * Generate an estimate for the date of birth, based on dates of parents/children/spouses
634a25f0a04SGreg Roach	 *
635a25f0a04SGreg Roach	 * @return Date
636a25f0a04SGreg Roach	 */
637ffd703eaSGreg Roach	public function getEstimatedBirthDate() {
638a25f0a04SGreg Roach		if (is_null($this->_getEstimatedBirthDate)) {
639a25f0a04SGreg Roach			foreach ($this->getAllBirthDates() as $date) {
640a25f0a04SGreg Roach				if ($date->isOK()) {
641a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = $date;
642a25f0a04SGreg Roach					break;
643a25f0a04SGreg Roach				}
644a25f0a04SGreg Roach			}
645a25f0a04SGreg Roach			if (is_null($this->_getEstimatedBirthDate)) {
64613abd6f3SGreg Roach				$min = [];
64713abd6f3SGreg Roach				$max = [];
648a25f0a04SGreg Roach				$tmp = $this->getDeathDate();
649f5b60decSGreg Roach				if ($tmp->isOK()) {
650f5b60decSGreg Roach					$min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
651f5b60decSGreg Roach					$max[] = $tmp->maximumJulianDay();
652a25f0a04SGreg Roach				}
653a25f0a04SGreg Roach				foreach ($this->getChildFamilies() as $family) {
654a25f0a04SGreg Roach					$tmp = $family->getMarriageDate();
655f5b60decSGreg Roach					if ($tmp->isOK()) {
656f5b60decSGreg Roach						$min[] = $tmp->maximumJulianDay() - 365 * 1;
657f5b60decSGreg Roach						$max[] = $tmp->minimumJulianDay() + 365 * 30;
658a25f0a04SGreg Roach					}
659a25f0a04SGreg Roach					if ($parent = $family->getHusband()) {
660a25f0a04SGreg Roach						$tmp = $parent->getBirthDate();
661f5b60decSGreg Roach						if ($tmp->isOK()) {
662f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() + 365 * 15;
663f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 65;
664a25f0a04SGreg Roach						}
665a25f0a04SGreg Roach					}
666a25f0a04SGreg Roach					if ($parent = $family->getWife()) {
667a25f0a04SGreg Roach						$tmp = $parent->getBirthDate();
668f5b60decSGreg Roach						if ($tmp->isOK()) {
669f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() + 365 * 15;
670f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 45;
671a25f0a04SGreg Roach						}
672a25f0a04SGreg Roach					}
673a25f0a04SGreg Roach					foreach ($family->getChildren() as $child) {
674a25f0a04SGreg Roach						$tmp = $child->getBirthDate();
675f5b60decSGreg Roach						if ($tmp->isOK()) {
676f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * 30;
677f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 30;
678a25f0a04SGreg Roach						}
679a25f0a04SGreg Roach					}
680a25f0a04SGreg Roach				}
681a25f0a04SGreg Roach				foreach ($this->getSpouseFamilies() as $family) {
682a25f0a04SGreg Roach					$tmp = $family->getMarriageDate();
683f5b60decSGreg Roach					if ($tmp->isOK()) {
684f5b60decSGreg Roach						$min[] = $tmp->maximumJulianDay() - 365 * 45;
685f5b60decSGreg Roach						$max[] = $tmp->minimumJulianDay() - 365 * 15;
686a25f0a04SGreg Roach					}
687a25f0a04SGreg Roach					$spouse = $family->getSpouse($this);
688a25f0a04SGreg Roach					if ($spouse) {
689a25f0a04SGreg Roach						$tmp = $spouse->getBirthDate();
690f5b60decSGreg Roach						if ($tmp->isOK()) {
691f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * 25;
692f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 25;
693a25f0a04SGreg Roach						}
694a25f0a04SGreg Roach					}
695a25f0a04SGreg Roach					foreach ($family->getChildren() as $child) {
696a25f0a04SGreg Roach						$tmp = $child->getBirthDate();
697f5b60decSGreg Roach						if ($tmp->isOK()) {
698f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
699f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() - 365 * 15;
700a25f0a04SGreg Roach						}
701a25f0a04SGreg Roach					}
702a25f0a04SGreg Roach				}
703a25f0a04SGreg Roach				if ($min && $max) {
704a25f0a04SGreg Roach					$gregorian_calendar = new GregorianCalendar;
705a25f0a04SGreg Roach
706a25f0a04SGreg Roach					list($year)                   = $gregorian_calendar->jdToYmd((int) ((max($min) + min($max)) / 2));
707a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = new Date('EST ' . $year);
708a25f0a04SGreg Roach				} else {
709a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = new Date(''); // always return a date object
710a25f0a04SGreg Roach				}
711a25f0a04SGreg Roach			}
712a25f0a04SGreg Roach		}
713a25f0a04SGreg Roach
714a25f0a04SGreg Roach		return $this->_getEstimatedBirthDate;
715a25f0a04SGreg Roach	}
716a25f0a04SGreg Roach
717a25f0a04SGreg Roach	/**
718a25f0a04SGreg Roach	 * Generate an estimated date of death.
719a25f0a04SGreg Roach	 *
720a25f0a04SGreg Roach	 * @return Date
721a25f0a04SGreg Roach	 */
722ffd703eaSGreg Roach	public function getEstimatedDeathDate() {
723a25f0a04SGreg Roach		if ($this->_getEstimatedDeathDate === null) {
724a25f0a04SGreg Roach			foreach ($this->getAllDeathDates() as $date) {
725a25f0a04SGreg Roach				if ($date->isOK()) {
726a25f0a04SGreg Roach					$this->_getEstimatedDeathDate = $date;
727a25f0a04SGreg Roach					break;
728a25f0a04SGreg Roach				}
729a25f0a04SGreg Roach			}
730a25f0a04SGreg Roach			if ($this->_getEstimatedDeathDate === null) {
731f5b60decSGreg Roach				if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
732f5b60decSGreg Roach					$this->_getEstimatedDeathDate = $this->getEstimatedBirthDate()->addYears($this->tree->getPreference('MAX_ALIVE_AGE'), 'BEF');
733a25f0a04SGreg Roach				} else {
734a25f0a04SGreg Roach					$this->_getEstimatedDeathDate = new Date(''); // always return a date object
735a25f0a04SGreg Roach				}
736a25f0a04SGreg Roach			}
737a25f0a04SGreg Roach		}
738a25f0a04SGreg Roach
739a25f0a04SGreg Roach		return $this->_getEstimatedDeathDate;
740a25f0a04SGreg Roach	}
741a25f0a04SGreg Roach
742a25f0a04SGreg Roach	/**
743a25f0a04SGreg Roach	 * Get the sex - M F or U
744a25f0a04SGreg Roach	 * Use the un-privatised gedcom record. We call this function during
745a25f0a04SGreg Roach	 * the privatize-gedcom function, and we are allowed to know this.
746a25f0a04SGreg Roach	 *
747a25f0a04SGreg Roach	 * @return string
748a25f0a04SGreg Roach	 */
749ffd703eaSGreg Roach	public function getSex() {
750a25f0a04SGreg Roach		if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
751a25f0a04SGreg Roach			return $match[1];
752a25f0a04SGreg Roach		} else {
753a25f0a04SGreg Roach			return 'U';
754a25f0a04SGreg Roach		}
755a25f0a04SGreg Roach	}
756a25f0a04SGreg Roach
757a25f0a04SGreg Roach	/**
758a25f0a04SGreg Roach	 * Get the individual’s sex image
759a25f0a04SGreg Roach	 *
760a25f0a04SGreg Roach	 * @param string $size
761a25f0a04SGreg Roach	 *
762a25f0a04SGreg Roach	 * @return string
763a25f0a04SGreg Roach	 */
764ffd703eaSGreg Roach	public function getSexImage($size = 'small') {
765a25f0a04SGreg Roach		return self::sexImage($this->getSex(), $size);
766a25f0a04SGreg Roach	}
767a25f0a04SGreg Roach
768a25f0a04SGreg Roach	/**
769a25f0a04SGreg Roach	 * Generate a sex icon/image
770a25f0a04SGreg Roach	 *
771a25f0a04SGreg Roach	 * @param string $sex
772a25f0a04SGreg Roach	 * @param string $size
773a25f0a04SGreg Roach	 *
774a25f0a04SGreg Roach	 * @return string
775a25f0a04SGreg Roach	 */
776ffd703eaSGreg Roach	public static function sexImage($sex, $size = 'small') {
777a25f0a04SGreg Roach		return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
778a25f0a04SGreg Roach	}
779a25f0a04SGreg Roach
780a25f0a04SGreg Roach	/**
781a25f0a04SGreg Roach	 * Generate the CSS class to be used for drawing this individual
782a25f0a04SGreg Roach	 *
783a25f0a04SGreg Roach	 * @return string
784a25f0a04SGreg Roach	 */
785ffd703eaSGreg Roach	public function getBoxStyle() {
78613abd6f3SGreg Roach		$tmp = ['M' => '', 'F' => 'F', 'U' => 'NN'];
787a25f0a04SGreg Roach
788a25f0a04SGreg Roach		return 'person_box' . $tmp[$this->getSex()];
789a25f0a04SGreg Roach	}
790a25f0a04SGreg Roach
791a25f0a04SGreg Roach	/**
792a25f0a04SGreg Roach	 * Get a list of this individual’s spouse families
793a25f0a04SGreg Roach	 *
794cbc1590aSGreg Roach	 * @param int|null $access_level
795a25f0a04SGreg Roach	 *
796a25f0a04SGreg Roach	 * @return Family[]
797a25f0a04SGreg Roach	 */
7984b9ff166SGreg Roach	public function getSpouseFamilies($access_level = null) {
7994b9ff166SGreg Roach		if ($access_level === null) {
8004b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
8014b9ff166SGreg Roach		}
8024b9ff166SGreg Roach
803518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
804a25f0a04SGreg Roach
80513abd6f3SGreg Roach		$families = [];
806a25f0a04SGreg Roach		foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
807a25f0a04SGreg Roach			$family = $fact->getTarget();
808a25f0a04SGreg Roach			if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
809a25f0a04SGreg Roach				$families[] = $family;
810a25f0a04SGreg Roach			}
811a25f0a04SGreg Roach		}
812a25f0a04SGreg Roach
813a25f0a04SGreg Roach		return $families;
814a25f0a04SGreg Roach	}
815a25f0a04SGreg Roach
816a25f0a04SGreg Roach	/**
817a25f0a04SGreg Roach	 * Get the current spouse of this individual.
818a25f0a04SGreg Roach	 *
819a25f0a04SGreg Roach	 * Where an individual has multiple spouses, assume they are stored
820a25f0a04SGreg Roach	 * in chronological order, and take the last one found.
821a25f0a04SGreg Roach	 *
822a25f0a04SGreg Roach	 * @return Individual|null
823a25f0a04SGreg Roach	 */
824a25f0a04SGreg Roach	public function getCurrentSpouse() {
825a25f0a04SGreg Roach		$tmp    = $this->getSpouseFamilies();
826a25f0a04SGreg Roach		$family = end($tmp);
827a25f0a04SGreg Roach		if ($family) {
828a25f0a04SGreg Roach			return $family->getSpouse($this);
829a25f0a04SGreg Roach		} else {
830a25f0a04SGreg Roach			return null;
831a25f0a04SGreg Roach		}
832a25f0a04SGreg Roach	}
833a25f0a04SGreg Roach
834a25f0a04SGreg Roach	/**
835a25f0a04SGreg Roach	 * Count the children belonging to this individual.
836a25f0a04SGreg Roach	 *
837cbc1590aSGreg Roach	 * @return int
838a25f0a04SGreg Roach	 */
839a25f0a04SGreg Roach	public function getNumberOfChildren() {
840a25f0a04SGreg Roach		if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
841a25f0a04SGreg Roach			return $match[1];
842a25f0a04SGreg Roach		} else {
84313abd6f3SGreg Roach			$children = [];
844a25f0a04SGreg Roach			foreach ($this->getSpouseFamilies() as $fam) {
845a25f0a04SGreg Roach				foreach ($fam->getChildren() as $child) {
846a25f0a04SGreg Roach					$children[$child->getXref()] = true;
847a25f0a04SGreg Roach				}
848a25f0a04SGreg Roach			}
849a25f0a04SGreg Roach
850a25f0a04SGreg Roach			return count($children);
851a25f0a04SGreg Roach		}
852a25f0a04SGreg Roach	}
853a25f0a04SGreg Roach
854a25f0a04SGreg Roach	/**
855a25f0a04SGreg Roach	 * Get a list of this individual’s child families (i.e. their parents).
856a25f0a04SGreg Roach	 *
857cbc1590aSGreg Roach	 * @param int|null $access_level
858a25f0a04SGreg Roach	 *
859a25f0a04SGreg Roach	 * @return Family[]
860a25f0a04SGreg Roach	 */
8614b9ff166SGreg Roach	public function getChildFamilies($access_level = null) {
8624b9ff166SGreg Roach		if ($access_level === null) {
8634b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
8644b9ff166SGreg Roach		}
8654b9ff166SGreg Roach
866518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
867a25f0a04SGreg Roach
86813abd6f3SGreg Roach		$families = [];
869a25f0a04SGreg Roach		foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
870a25f0a04SGreg Roach			$family = $fact->getTarget();
871a25f0a04SGreg Roach			if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
872a25f0a04SGreg Roach				$families[] = $family;
873a25f0a04SGreg Roach			}
874a25f0a04SGreg Roach		}
875a25f0a04SGreg Roach
876a25f0a04SGreg Roach		return $families;
877a25f0a04SGreg Roach	}
878a25f0a04SGreg Roach
879a25f0a04SGreg Roach	/**
880a25f0a04SGreg Roach	 * Get the preferred parents for this individual.
881a25f0a04SGreg Roach	 *
882a25f0a04SGreg Roach	 * An individual may multiple parents (e.g. birth, adopted, disputed).
883a25f0a04SGreg Roach	 * The preferred family record is:
884a25f0a04SGreg Roach	 * (a) the first one with an explicit tag "_PRIMARY Y"
885a25f0a04SGreg Roach	 * (b) the first one with a pedigree of "birth"
886a25f0a04SGreg Roach	 * (c) the first one with no pedigree (default is "birth")
887a25f0a04SGreg Roach	 * (d) the first one found
888a25f0a04SGreg Roach	 *
889a25f0a04SGreg Roach	 * @return Family|null
890a25f0a04SGreg Roach	 */
891a25f0a04SGreg Roach	public function getPrimaryChildFamily() {
892a25f0a04SGreg Roach		$families = $this->getChildFamilies();
893a25f0a04SGreg Roach		switch (count($families)) {
894a25f0a04SGreg Roach		case 0:
895a25f0a04SGreg Roach			return null;
896a25f0a04SGreg Roach		case 1:
89715d603e7SGreg Roach			return $families[0];
898a25f0a04SGreg Roach		default:
899a25f0a04SGreg Roach			// If there is more than one FAMC record, choose the preferred parents:
900a25f0a04SGreg Roach			// a) records with '2 _PRIMARY'
901a25f0a04SGreg Roach			foreach ($families as $famid => $fam) {
902a25f0a04SGreg Roach				if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
903a25f0a04SGreg Roach					return $fam;
904a25f0a04SGreg Roach				}
905a25f0a04SGreg Roach			}
906a25f0a04SGreg Roach			// b) records with '2 PEDI birt'
907a25f0a04SGreg Roach			foreach ($families as $famid => $fam) {
908a25f0a04SGreg Roach				if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
909a25f0a04SGreg Roach					return $fam;
910a25f0a04SGreg Roach				}
911a25f0a04SGreg Roach			}
912a25f0a04SGreg Roach			// c) records with no '2 PEDI'
913a25f0a04SGreg Roach			foreach ($families as $famid => $fam) {
914a25f0a04SGreg Roach				if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
915a25f0a04SGreg Roach					return $fam;
916a25f0a04SGreg Roach				}
917a25f0a04SGreg Roach			}
918a25f0a04SGreg Roach
919a25f0a04SGreg Roach			// d) any record
92015d603e7SGreg Roach			return $families[0];
921a25f0a04SGreg Roach		}
922a25f0a04SGreg Roach	}
923a25f0a04SGreg Roach
924a25f0a04SGreg Roach	/**
925a25f0a04SGreg Roach	 * Get a list of step-parent families.
926a25f0a04SGreg Roach	 *
927a25f0a04SGreg Roach	 * @return Family[]
928a25f0a04SGreg Roach	 */
929ffd703eaSGreg Roach	public function getChildStepFamilies() {
93013abd6f3SGreg Roach		$step_families = [];
931a25f0a04SGreg Roach		$families      = $this->getChildFamilies();
932a25f0a04SGreg Roach		foreach ($families as $family) {
933a25f0a04SGreg Roach			$father = $family->getHusband();
934a25f0a04SGreg Roach			if ($father) {
935a25f0a04SGreg Roach				foreach ($father->getSpouseFamilies() as $step_family) {
936a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
937a25f0a04SGreg Roach						$step_families[] = $step_family;
938a25f0a04SGreg Roach					}
939a25f0a04SGreg Roach				}
940a25f0a04SGreg Roach			}
941a25f0a04SGreg Roach			$mother = $family->getWife();
942a25f0a04SGreg Roach			if ($mother) {
943a25f0a04SGreg Roach				foreach ($mother->getSpouseFamilies() as $step_family) {
944a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
945a25f0a04SGreg Roach						$step_families[] = $step_family;
946a25f0a04SGreg Roach					}
947a25f0a04SGreg Roach				}
948a25f0a04SGreg Roach			}
949a25f0a04SGreg Roach		}
950a25f0a04SGreg Roach
951a25f0a04SGreg Roach		return $step_families;
952a25f0a04SGreg Roach	}
953a25f0a04SGreg Roach
954a25f0a04SGreg Roach	/**
955a25f0a04SGreg Roach	 * Get a list of step-parent families.
956a25f0a04SGreg Roach	 *
957a25f0a04SGreg Roach	 * @return Family[]
958a25f0a04SGreg Roach	 */
959ffd703eaSGreg Roach	public function getSpouseStepFamilies() {
96013abd6f3SGreg Roach		$step_families = [];
961a25f0a04SGreg Roach		$families      = $this->getSpouseFamilies();
962a25f0a04SGreg Roach		foreach ($families as $family) {
963a25f0a04SGreg Roach			$spouse = $family->getSpouse($this);
964a25f0a04SGreg Roach			if ($spouse) {
965a25f0a04SGreg Roach				foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
966a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
967a25f0a04SGreg Roach						$step_families[] = $step_family;
968a25f0a04SGreg Roach					}
969a25f0a04SGreg Roach				}
970a25f0a04SGreg Roach			}
971a25f0a04SGreg Roach		}
972a25f0a04SGreg Roach
973a25f0a04SGreg Roach		return $step_families;
974a25f0a04SGreg Roach	}
975a25f0a04SGreg Roach
976a25f0a04SGreg Roach	/**
977a25f0a04SGreg Roach	 * A label for a parental family group
978a25f0a04SGreg Roach	 *
979a25f0a04SGreg Roach	 * @param Family $family
980a25f0a04SGreg Roach	 *
981a25f0a04SGreg Roach	 * @return string
982a25f0a04SGreg Roach	 */
983ffd703eaSGreg Roach	public function getChildFamilyLabel(Family $family) {
984a25f0a04SGreg Roach		if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
985a25f0a04SGreg Roach			// A specified pedigree
986764a01d9SGreg Roach			return GedcomCodePedi::getChildFamilyLabel($match[1]);
987a25f0a04SGreg Roach		} else {
988a25f0a04SGreg Roach			// Default (birth) pedigree
989764a01d9SGreg Roach			return GedcomCodePedi::getChildFamilyLabel('');
990a25f0a04SGreg Roach		}
991a25f0a04SGreg Roach	}
992a25f0a04SGreg Roach
993a25f0a04SGreg Roach	/**
994a25f0a04SGreg Roach	 * Create a label for a step family
995a25f0a04SGreg Roach	 *
996a25f0a04SGreg Roach	 * @param Family $step_family
997a25f0a04SGreg Roach	 *
998a25f0a04SGreg Roach	 * @return string
999a25f0a04SGreg Roach	 */
1000ffd703eaSGreg Roach	public function getStepFamilyLabel(Family $step_family) {
1001a25f0a04SGreg Roach		foreach ($this->getChildFamilies() as $family) {
1002a25f0a04SGreg Roach			if ($family !== $step_family) {
1003a25f0a04SGreg Roach				// Must be a step-family
1004a25f0a04SGreg Roach				foreach ($family->getSpouses() as $parent) {
1005a25f0a04SGreg Roach					foreach ($step_family->getSpouses() as $step_parent) {
1006a25f0a04SGreg Roach						if ($parent === $step_parent) {
1007a25f0a04SGreg Roach							// One common parent - must be a step family
1008a25f0a04SGreg Roach							if ($parent->getSex() == 'M') {
1009a25f0a04SGreg Roach								// Father’s family with someone else
1010a25f0a04SGreg Roach								if ($step_family->getSpouse($step_parent)) {
1011a25f0a04SGreg Roach									return
1012a25f0a04SGreg Roach										/* I18N: A step-family. %s is an individual’s name */
1013a25f0a04SGreg Roach										I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1014a25f0a04SGreg Roach								} else {
1015a25f0a04SGreg Roach									return
1016a25f0a04SGreg Roach										/* I18N: A step-family. */
1017a25f0a04SGreg Roach										I18N::translate('Father’s family with an unknown individual');
1018a25f0a04SGreg Roach								}
1019a25f0a04SGreg Roach							} else {
1020a25f0a04SGreg Roach								// Mother’s family with someone else
1021a25f0a04SGreg Roach								if ($step_family->getSpouse($step_parent)) {
1022a25f0a04SGreg Roach									return
1023a25f0a04SGreg Roach										/* I18N: A step-family. %s is an individual’s name */
1024a25f0a04SGreg Roach										I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1025a25f0a04SGreg Roach								} else {
1026a25f0a04SGreg Roach									return
1027a25f0a04SGreg Roach										/* I18N: A step-family. */
1028a25f0a04SGreg Roach										I18N::translate('Mother’s family with an unknown individual');
1029a25f0a04SGreg Roach								}
1030a25f0a04SGreg Roach							}
1031a25f0a04SGreg Roach						}
1032a25f0a04SGreg Roach					}
1033a25f0a04SGreg Roach				}
1034a25f0a04SGreg Roach			}
1035a25f0a04SGreg Roach		}
1036a25f0a04SGreg Roach
1037a25f0a04SGreg Roach		// Perahps same parents - but a different family record?
1038a25f0a04SGreg Roach		return I18N::translate('Family with parents');
1039a25f0a04SGreg Roach	}
1040a25f0a04SGreg Roach
1041a25f0a04SGreg Roach	/**
1042a25f0a04SGreg Roach	 * get primary parents names for this individual
1043a25f0a04SGreg Roach	 *
1044a25f0a04SGreg Roach	 * @param string $classname optional css class
1045a25f0a04SGreg Roach	 * @param string $display   optional css style display
1046a25f0a04SGreg Roach	 *
1047a25f0a04SGreg Roach	 * @return string a div block with father & mother names
1048a25f0a04SGreg Roach	 */
1049ffd703eaSGreg Roach	public function getPrimaryParentsNames($classname = '', $display = '') {
1050a25f0a04SGreg Roach		$fam = $this->getPrimaryChildFamily();
1051a25f0a04SGreg Roach		if (!$fam) {
1052a25f0a04SGreg Roach			return '';
1053a25f0a04SGreg Roach		}
1054a25f0a04SGreg Roach		$txt = '<div';
1055a25f0a04SGreg Roach		if ($classname) {
10564c621133SGreg Roach			$txt .= ' class="' . $classname . '"';
1057a25f0a04SGreg Roach		}
1058a25f0a04SGreg Roach		if ($display) {
10594c621133SGreg Roach			$txt .= ' style="display:' . $display . '"';
1060a25f0a04SGreg Roach		}
1061a25f0a04SGreg Roach		$txt .= '>';
1062a25f0a04SGreg Roach		$husb = $fam->getHusband();
1063a25f0a04SGreg Roach		if ($husb) {
1064a25f0a04SGreg Roach			// Temporarily reset the 'prefered' display name, as we always
1065a25f0a04SGreg Roach			// want the default name, not the one selected for display on the indilist.
1066a25f0a04SGreg Roach			$primary = $husb->getPrimaryName();
1067a25f0a04SGreg Roach			$husb->setPrimaryName(null);
1068a25f0a04SGreg Roach			$txt .=
1069a25f0a04SGreg Roach				/* I18N: %s is the name of an individual’s father */
1070a25f0a04SGreg Roach				I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1071a25f0a04SGreg Roach			$husb->setPrimaryName($primary);
1072a25f0a04SGreg Roach		}
1073a25f0a04SGreg Roach		$wife = $fam->getWife();
1074a25f0a04SGreg Roach		if ($wife) {
1075a25f0a04SGreg Roach			// Temporarily reset the 'prefered' display name, as we always
1076a25f0a04SGreg Roach			// want the default name, not the one selected for display on the indilist.
1077a25f0a04SGreg Roach			$primary = $wife->getPrimaryName();
1078a25f0a04SGreg Roach			$wife->setPrimaryName(null);
1079a25f0a04SGreg Roach			$txt .=
1080a25f0a04SGreg Roach				/* I18N: %s is the name of an individual’s mother */
1081a25f0a04SGreg Roach				I18N::translate('Mother: %s', $wife->getFullName());
1082a25f0a04SGreg Roach			$wife->setPrimaryName($primary);
1083a25f0a04SGreg Roach		}
1084a25f0a04SGreg Roach		$txt .= '</div>';
1085a25f0a04SGreg Roach
1086a25f0a04SGreg Roach		return $txt;
1087a25f0a04SGreg Roach	}
1088a25f0a04SGreg Roach
1089a25f0a04SGreg Roach	/** {@inheritdoc} */
1090ffd703eaSGreg Roach	public function getFallBackName() {
1091a25f0a04SGreg Roach		return '@P.N. /@N.N./';
1092a25f0a04SGreg Roach	}
1093a25f0a04SGreg Roach
1094a25f0a04SGreg Roach	/**
1095a25f0a04SGreg Roach	 * Convert a name record into ‘full’ and ‘sort’ versions.
1096a25f0a04SGreg Roach	 * Use the NAME field to generate the ‘full’ version, as the
1097a25f0a04SGreg Roach	 * gedcom spec says that this is the individual’s name, as they would write it.
1098a25f0a04SGreg Roach	 * Use the SURN field to generate the sortable names. Note that this field
1099a25f0a04SGreg Roach	 * may also be used for the ‘true’ surname, perhaps spelt differently to that
1100a25f0a04SGreg Roach	 * recorded in the NAME field. e.g.
1101a25f0a04SGreg Roach	 *
1102a25f0a04SGreg Roach	 * 1 NAME Robert /de Gliderow/
1103a25f0a04SGreg Roach	 * 2 GIVN Robert
1104a25f0a04SGreg Roach	 * 2 SPFX de
1105a25f0a04SGreg Roach	 * 2 SURN CLITHEROW
1106a25f0a04SGreg Roach	 * 2 NICK The Bald
1107a25f0a04SGreg Roach	 *
1108a25f0a04SGreg Roach	 * full=>'Robert de Gliderow 'The Bald''
1109a25f0a04SGreg Roach	 * sort=>'CLITHEROW, ROBERT'
1110a25f0a04SGreg Roach	 *
1111a25f0a04SGreg Roach	 * Handle multiple surnames, either as;
1112a25f0a04SGreg Roach	 *
1113a25f0a04SGreg Roach	 * 1 NAME Carlos /Vasquez/ y /Sante/
1114a25f0a04SGreg Roach	 * or
1115a25f0a04SGreg Roach	 * 1 NAME Carlos /Vasquez y Sante/
1116a25f0a04SGreg Roach	 * 2 GIVN Carlos
1117a25f0a04SGreg Roach	 * 2 SURN Vasquez,Sante
1118a25f0a04SGreg Roach	 *
1119a25f0a04SGreg Roach	 * @param string $type
1120a25f0a04SGreg Roach	 * @param string $full
1121a25f0a04SGreg Roach	 * @param string $gedcom
1122a25f0a04SGreg Roach	 */
1123a25f0a04SGreg Roach	protected function addName($type, $full, $gedcom) {
1124a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1125a25f0a04SGreg Roach		// Extract the structured name parts - use for "sortable" names and indexes
1126a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1127a25f0a04SGreg Roach
1128a25f0a04SGreg Roach		$sublevel = 1 + (int) $gedcom[0];
1129a25f0a04SGreg Roach		$NPFX     = preg_match("/\n{$sublevel} NPFX (.+)/", $gedcom, $match) ? $match[1] : '';
1130a25f0a04SGreg Roach		$GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1131a25f0a04SGreg Roach		$SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1132a25f0a04SGreg Roach		$NSFX     = preg_match("/\n{$sublevel} NSFX (.+)/", $gedcom, $match) ? $match[1] : '';
1133a25f0a04SGreg Roach		$NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1134a25f0a04SGreg Roach
1135a25f0a04SGreg Roach		// SURN is an comma-separated list of surnames...
1136a25f0a04SGreg Roach		if ($SURN) {
1137a25f0a04SGreg Roach			$SURNS = preg_split('/ *, */', $SURN);
1138a25f0a04SGreg Roach		} else {
113913abd6f3SGreg Roach			$SURNS = [];
1140a25f0a04SGreg Roach		}
1141a25f0a04SGreg Roach		// ...so is GIVN - but nobody uses it like that
1142a25f0a04SGreg Roach		$GIVN = str_replace('/ *, */', ' ', $GIVN);
1143a25f0a04SGreg Roach
1144a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1145a25f0a04SGreg Roach		// Extract the components from NAME - use for the "full" names
1146a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1147a25f0a04SGreg Roach
1148a25f0a04SGreg Roach		// Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
1149a25f0a04SGreg Roach		if (substr_count($full, '/') % 2 == 1) {
1150a25f0a04SGreg Roach			$full = $full . '/';
1151a25f0a04SGreg Roach		}
1152a25f0a04SGreg Roach
1153a25f0a04SGreg Roach		// GEDCOM uses "//" to indicate an unknown surname
1154a25f0a04SGreg Roach		$full = preg_replace('/\/\//', '/@N.N./', $full);
1155a25f0a04SGreg Roach
1156a25f0a04SGreg Roach		// Extract the surname.
1157a25f0a04SGreg Roach		// Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1158a25f0a04SGreg Roach		if (preg_match('/\/.*\//', $full, $match)) {
1159a25f0a04SGreg Roach			$surname = str_replace('/', '', $match[0]);
1160a25f0a04SGreg Roach		} else {
1161a25f0a04SGreg Roach			$surname = '';
1162a25f0a04SGreg Roach		}
1163a25f0a04SGreg Roach
1164a25f0a04SGreg Roach		// If we don’t have a SURN record, extract it from the NAME
1165a25f0a04SGreg Roach		if (!$SURNS) {
1166a25f0a04SGreg Roach			if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1167a25f0a04SGreg Roach				// There can be many surnames, each wrapped with '/'
1168a25f0a04SGreg Roach				$SURNS = $matches[1];
1169a25f0a04SGreg Roach				foreach ($SURNS as $n => $SURN) {
1170a25f0a04SGreg Roach					// Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1171a25f0a04SGreg Roach					$SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1172a25f0a04SGreg Roach				}
1173a25f0a04SGreg Roach			} else {
1174a25f0a04SGreg Roach				// It is valid not to have a surname at all
117513abd6f3SGreg Roach				$SURNS = [''];
1176a25f0a04SGreg Roach			}
1177a25f0a04SGreg Roach		}
1178a25f0a04SGreg Roach
1179a25f0a04SGreg Roach		// If we don’t have a GIVN record, extract it from the NAME
1180a25f0a04SGreg Roach		if (!$GIVN) {
1181a25f0a04SGreg Roach			$GIVN = preg_replace(
118213abd6f3SGreg Roach				[
1183a25f0a04SGreg Roach					'/ ?\/.*\/ ?/', // remove surname
1184a25f0a04SGreg Roach					'/ ?".+"/', // remove nickname
1185a25f0a04SGreg Roach					'/ {2,}/', // multiple spaces, caused by the above
1186a25f0a04SGreg Roach					'/^ | $/', // leading/trailing spaces, caused by the above
118713abd6f3SGreg Roach				],
118813abd6f3SGreg Roach				[
1189a25f0a04SGreg Roach					' ',
1190a25f0a04SGreg Roach					' ',
1191a25f0a04SGreg Roach					' ',
1192a25f0a04SGreg Roach					'',
119313abd6f3SGreg Roach				],
1194a25f0a04SGreg Roach				$full
1195a25f0a04SGreg Roach			);
1196a25f0a04SGreg Roach		}
1197a25f0a04SGreg Roach
1198a25f0a04SGreg Roach		// Add placeholder for unknown given name
1199a25f0a04SGreg Roach		if (!$GIVN) {
1200a25f0a04SGreg Roach			$GIVN = '@P.N.';
1201a25f0a04SGreg Roach			$pos  = strpos($full, '/');
1202a25f0a04SGreg Roach			$full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1203a25f0a04SGreg Roach		}
1204a25f0a04SGreg Roach
1205a25f0a04SGreg Roach		// GEDCOM nicknames should be specificied in a NICK field, or in the
1206a25f0a04SGreg Roach		// NAME filed, surrounded by ASCII quotes (or both).
1207c6f196c3SGreg Roach		if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
1208c6f196c3SGreg Roach			// A NICK field is present, but not included in the NAME.
1209a25f0a04SGreg Roach			$pos = strpos($full, '/');
1210a25f0a04SGreg Roach			if ($pos === false) {
1211c6f196c3SGreg Roach				// No surname - just append it
1212c6f196c3SGreg Roach				$full .= ' "' . $NICK . '"';
1213a25f0a04SGreg Roach			} else {
1214a25f0a04SGreg Roach				// Insert before surname
1215c6f196c3SGreg Roach				$full = substr($full, 0, $pos) . '"' . $NICK . '" ' . substr($full, $pos);
1216a25f0a04SGreg Roach			}
1217a25f0a04SGreg Roach		}
1218a25f0a04SGreg Roach
1219a25f0a04SGreg Roach		// Remove slashes - they don’t get displayed
1220a25f0a04SGreg Roach		// $fullNN keeps the @N.N. placeholders, for the database
1221a25f0a04SGreg Roach		// $full is for display on-screen
1222a25f0a04SGreg Roach		$fullNN = str_replace('/', '', $full);
1223a25f0a04SGreg Roach
1224a25f0a04SGreg Roach		// Insert placeholders for any missing/unknown names
1225ad1a1cd2SGreg Roach		$full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1226ad1a1cd2SGreg Roach		$full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1227c6f196c3SGreg Roach		// Format for display
1228a25f0a04SGreg Roach		$full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', Filter::escapeHtml($full)) . '</span>';
1229acc34ea1SGreg Roach		// Localise quotation marks around the nickname
1230acc34ea1SGreg Roach		$full = preg_replace_callback('/&quot;([^&]*)&quot;/', function ($matches) { return I18N::translate('“%s”', $matches[1]); }, $full);
1231a25f0a04SGreg Roach
1232c6f196c3SGreg Roach		// A suffix of “*” indicates a preferred name
1233a25f0a04SGreg Roach		$full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1234a25f0a04SGreg Roach
1235a25f0a04SGreg Roach		// Remove prefered-name indicater - they don’t go in the database
1236a25f0a04SGreg Roach		$GIVN   = str_replace('*', '', $GIVN);
1237a25f0a04SGreg Roach		$fullNN = str_replace('*', '', $fullNN);
1238a25f0a04SGreg Roach
1239ffd703eaSGreg Roach		foreach ($SURNS as $SURN) {
1240a25f0a04SGreg Roach			// Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1241a25f0a04SGreg Roach			if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1242a25f0a04SGreg Roach				$SURN = substr_replace($SURN, 'Mac', 0, 2);
1243a25f0a04SGreg Roach			} elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1244a25f0a04SGreg Roach				$SURN = substr_replace($SURN, 'Mac', 0, 4);
1245a25f0a04SGreg Roach			}
1246a25f0a04SGreg Roach
124713abd6f3SGreg Roach			$this->_getAllNames[] = [
1248a25f0a04SGreg Roach				'type'    => $type,
1249a25f0a04SGreg Roach				'sort'    => $SURN . ',' . $GIVN,
1250a25f0a04SGreg Roach				'full'    => $full, // This is used for display
1251a25f0a04SGreg Roach				'fullNN'  => $fullNN, // This goes into the database
1252a25f0a04SGreg Roach				'surname' => $surname, // This goes into the database
1253a25f0a04SGreg Roach				'givn'    => $GIVN, // This goes into the database
1254a25f0a04SGreg Roach				'surn'    => $SURN, // This goes into the database
125513abd6f3SGreg Roach			];
1256a25f0a04SGreg Roach		}
1257a25f0a04SGreg Roach	}
1258a25f0a04SGreg Roach
1259a25f0a04SGreg Roach	/**
126076692c8bSGreg Roach	 * Extract names from the GEDCOM record.
1261a25f0a04SGreg Roach	 */
1262a25f0a04SGreg Roach	public function extractNames() {
12634b9ff166SGreg Roach		$this->extractNamesFromFacts(1, 'NAME', $this->getFacts('NAME', false, Auth::accessLevel($this->tree), $this->canShowName()));
1264a25f0a04SGreg Roach	}
1265a25f0a04SGreg Roach
1266a25f0a04SGreg Roach	/**
1267a25f0a04SGreg Roach	 * Extra info to display when displaying this record in a list of
1268a25f0a04SGreg Roach	 * selection items or favorites.
1269a25f0a04SGreg Roach	 *
1270a25f0a04SGreg Roach	 * @return string
1271a25f0a04SGreg Roach	 */
1272ffd703eaSGreg Roach	public function formatListDetails() {
1273a25f0a04SGreg Roach		return
1274841014f1SGreg Roach			$this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1275841014f1SGreg Roach			$this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1276a25f0a04SGreg Roach	}
1277a25f0a04SGreg Roach
1278a25f0a04SGreg Roach	/**
1279a25f0a04SGreg Roach	 * Create a short name for compact display on charts
1280a25f0a04SGreg Roach	 *
1281a25f0a04SGreg Roach	 * @return string
1282a25f0a04SGreg Roach	 */
1283a25f0a04SGreg Roach	public function getShortName() {
1284ad1a1cd2SGreg Roach		global $bwidth;
1285a25f0a04SGreg Roach
1286a25f0a04SGreg Roach		// Estimate number of characters that can fit in box. Calulates to 28 characters in webtrees theme, or 34 if no thumbnail used.
1287518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_HIGHLIGHT_IMAGES')) {
1288a25f0a04SGreg Roach			$char = intval(($bwidth - 40) / 6.5);
1289a25f0a04SGreg Roach		} else {
1290a25f0a04SGreg Roach			$char = ($bwidth / 6.5);
1291a25f0a04SGreg Roach		}
1292a25f0a04SGreg Roach		if ($this->canShowName()) {
1293a25f0a04SGreg Roach			$tmp        = $this->getAllNames();
1294a25f0a04SGreg Roach			$givn       = $tmp[$this->getPrimaryName()]['givn'];
1295a25f0a04SGreg Roach			$surn       = $tmp[$this->getPrimaryName()]['surname'];
1296a25f0a04SGreg Roach			$new_givn   = explode(' ', $givn);
1297a25f0a04SGreg Roach			$count_givn = count($new_givn);
1298a25f0a04SGreg Roach			$len_givn   = mb_strlen($givn);
1299a25f0a04SGreg Roach			$len_surn   = mb_strlen($surn);
1300a25f0a04SGreg Roach			$len        = $len_givn + $len_surn;
1301a25f0a04SGreg Roach			$i          = 1;
1302a25f0a04SGreg Roach			while ($len > $char && $i <= $count_givn) {
1303a25f0a04SGreg Roach				$new_givn[$count_givn - $i] = mb_substr($new_givn[$count_givn - $i], 0, 1);
1304a25f0a04SGreg Roach				$givn                       = implode(' ', $new_givn);
1305a25f0a04SGreg Roach				$len_givn                   = mb_strlen($givn);
1306a25f0a04SGreg Roach				$len                        = $len_givn + $len_surn;
1307a25f0a04SGreg Roach				$i++;
1308a25f0a04SGreg Roach			}
1309a25f0a04SGreg Roach			$max_surn = $char - $i * 2;
1310a25f0a04SGreg Roach			if ($len_surn > $max_surn) {
1311a25f0a04SGreg Roach				$surn = substr($surn, 0, $max_surn) . '…';
1312a25f0a04SGreg Roach			}
1313a25f0a04SGreg Roach			$shortname = str_replace(
131413abd6f3SGreg Roach				['@P.N.', '@N.N.'],
131513abd6f3SGreg Roach				[I18N::translateContext('Unknown given name', '…'), I18N::translateContext('Unknown surname', '…')],
1316a25f0a04SGreg Roach				$givn . ' ' . $surn
1317a25f0a04SGreg Roach			);
1318a25f0a04SGreg Roach
1319a25f0a04SGreg Roach			return $shortname;
1320a25f0a04SGreg Roach		} else {
1321a25f0a04SGreg Roach			return I18N::translate('Private');
1322a25f0a04SGreg Roach		}
1323a25f0a04SGreg Roach	}
1324a25f0a04SGreg Roach}
1325