xref: /webtrees/app/Individual.php (revision 54ba7dc5b209b08d4d8fd3d0f360c3e5ae1e079f)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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';
26225e381fSGreg Roach	const ROUTE_NAME  = 'individual';
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	/**
38e71ef9d2SGreg Roach	 * Get an instance of an individual object. For single records,
39e71ef9d2SGreg Roach	 * we just receive the XREF. For bulk records (such as lists
40e71ef9d2SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
41e71ef9d2SGreg Roach	 *
42e71ef9d2SGreg Roach	 * @param string      $xref
43e71ef9d2SGreg Roach	 * @param Tree        $tree
44e71ef9d2SGreg Roach	 * @param string|null $gedcom
45e71ef9d2SGreg Roach	 *
46e71ef9d2SGreg Roach	 * @throws \Exception
47e71ef9d2SGreg Roach	 *
48e71ef9d2SGreg Roach	 * @return Individual|null
49e71ef9d2SGreg Roach	 */
50e71ef9d2SGreg Roach	public static function getInstance($xref, Tree $tree, $gedcom = null) {
51e71ef9d2SGreg Roach		$record = parent::getInstance($xref, $tree, $gedcom);
52e71ef9d2SGreg Roach
53e71ef9d2SGreg Roach		if ($record instanceof Individual) {
54e71ef9d2SGreg Roach			return $record;
55e71ef9d2SGreg Roach		} else {
56e71ef9d2SGreg Roach			return null;
57e71ef9d2SGreg Roach		}
58e71ef9d2SGreg Roach	}
59e71ef9d2SGreg Roach
60e71ef9d2SGreg Roach	/**
61395f0fe0SGreg Roach	 * Sometimes, we'll know in advance that we need to load a set of records.
62395f0fe0SGreg Roach	 * Typically when we load families and their members.
63395f0fe0SGreg Roach	 *
64395f0fe0SGreg Roach	 * @param Tree  $tree
654a8aaa00SScrutinizer Auto-Fixer	 * @param string[] $xrefs
66395f0fe0SGreg Roach	 */
67395f0fe0SGreg Roach	public static function load(Tree $tree, array $xrefs) {
6813abd6f3SGreg Roach		$args = [
69395f0fe0SGreg Roach			'tree_id' => $tree->getTreeId(),
7013abd6f3SGreg Roach		];
7113abd6f3SGreg Roach		$placeholders = [];
72395f0fe0SGreg Roach
73395f0fe0SGreg Roach		foreach (array_unique($xrefs) as $n => $xref) {
74395f0fe0SGreg Roach			if (!isset(self::$gedcom_record_cache[$tree->getTreeId()][$xref])) {
75f7247c73SGreg Roach				$placeholders[] = ':x' . $n;
76395f0fe0SGreg Roach				$args['x' . $n] = $xref;
77395f0fe0SGreg Roach			}
78395f0fe0SGreg Roach		}
79395f0fe0SGreg Roach
80f7247c73SGreg Roach		if (!empty($placeholders)) {
81395f0fe0SGreg Roach			$rows = Database::prepare(
82395f0fe0SGreg Roach				"SELECT i_id AS xref, i_gedcom AS gedcom" .
83395f0fe0SGreg Roach				" FROM `##individuals`" .
84f7247c73SGreg Roach				" WHERE i_file = :tree_id AND i_id IN (" . implode(',', $placeholders) . ")"
85395f0fe0SGreg Roach			)->execute(
86395f0fe0SGreg Roach				$args
87395f0fe0SGreg Roach			)->fetchAll();
88395f0fe0SGreg Roach
89395f0fe0SGreg Roach			foreach ($rows as $row) {
90395f0fe0SGreg Roach				self::getInstance($row->xref, $tree, $row->gedcom);
91395f0fe0SGreg Roach			}
92395f0fe0SGreg Roach		}
93395f0fe0SGreg Roach	}
94395f0fe0SGreg Roach
95395f0fe0SGreg Roach	/**
96a25f0a04SGreg Roach	 * Can the name of this record be shown?
97a25f0a04SGreg Roach	 *
9876692c8bSGreg Roach	 * @param int|null $access_level
9976692c8bSGreg Roach	 *
10076692c8bSGreg Roach	 * @return bool
101a25f0a04SGreg Roach	 */
1024b9ff166SGreg Roach	public function canShowName($access_level = null) {
1034b9ff166SGreg Roach		if ($access_level === null) {
1044b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
1054b9ff166SGreg Roach		}
1064b9ff166SGreg Roach
107518bbdc1SGreg Roach		return $this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level || $this->canShow($access_level);
108a25f0a04SGreg Roach	}
109a25f0a04SGreg Roach
110a25f0a04SGreg Roach	/**
11176692c8bSGreg Roach	 * Can this individual be shown?
112a25f0a04SGreg Roach	 *
11376692c8bSGreg Roach	 * @param int $access_level
11476692c8bSGreg Roach	 *
11576692c8bSGreg Roach	 * @return bool
116a25f0a04SGreg Roach	 */
117a25f0a04SGreg Roach	protected function canShowByType($access_level) {
118a25f0a04SGreg Roach		// Dead people...
119518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_DEAD_PEOPLE') >= $access_level && $this->isDead()) {
120a25f0a04SGreg Roach			$keep_alive             = false;
121*54ba7dc5SGreg Roach			$KEEP_ALIVE_YEARS_BIRTH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_BIRTH');
122a25f0a04SGreg Roach			if ($KEEP_ALIVE_YEARS_BIRTH) {
123a25f0a04SGreg Roach				preg_match_all('/\n1 (?:' . WT_EVENTS_BIRT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
124a25f0a04SGreg Roach				foreach ($matches as $match) {
125a25f0a04SGreg Roach					$date = new Date($match[1]);
126a25f0a04SGreg Roach					if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_BIRTH > date('Y')) {
127a25f0a04SGreg Roach						$keep_alive = true;
128a25f0a04SGreg Roach						break;
129a25f0a04SGreg Roach					}
130a25f0a04SGreg Roach				}
131a25f0a04SGreg Roach			}
132*54ba7dc5SGreg Roach			$KEEP_ALIVE_YEARS_DEATH = (int) $this->tree->getPreference('KEEP_ALIVE_YEARS_DEATH');
133a25f0a04SGreg Roach			if ($KEEP_ALIVE_YEARS_DEATH) {
134a25f0a04SGreg Roach				preg_match_all('/\n1 (?:' . WT_EVENTS_DEAT . ').*(?:\n[2-9].*)*(?:\n2 DATE (.+))/', $this->gedcom, $matches, PREG_SET_ORDER);
135a25f0a04SGreg Roach				foreach ($matches as $match) {
136a25f0a04SGreg Roach					$date = new Date($match[1]);
137a25f0a04SGreg Roach					if ($date->isOK() && $date->gregorianYear() + $KEEP_ALIVE_YEARS_DEATH > date('Y')) {
138a25f0a04SGreg Roach						$keep_alive = true;
139a25f0a04SGreg Roach						break;
140a25f0a04SGreg Roach					}
141a25f0a04SGreg Roach				}
142a25f0a04SGreg Roach			}
143a25f0a04SGreg Roach			if (!$keep_alive) {
144a25f0a04SGreg Roach				return true;
145a25f0a04SGreg Roach			}
146a25f0a04SGreg Roach		}
147a25f0a04SGreg Roach		// Consider relationship privacy (unless an admin is applying download restrictions)
148*54ba7dc5SGreg Roach		$user_path_length = (int) $this->tree->getUserPreference(Auth::user(), 'RELATIONSHIP_PATH_LENGTH');
1494b9ff166SGreg Roach		$gedcomid         = $this->tree->getUserPreference(Auth::user(), 'gedcomid');
150*54ba7dc5SGreg Roach		if ($gedcomid !== '' && $user_path_length > 0) {
1514b9ff166SGreg Roach			return self::isRelated($this, $user_path_length);
152a25f0a04SGreg Roach		}
153a25f0a04SGreg Roach
154a25f0a04SGreg Roach		// No restriction found - show living people to members only:
1554b9ff166SGreg Roach		return Auth::PRIV_USER >= $access_level;
156a25f0a04SGreg Roach	}
157a25f0a04SGreg Roach
158a25f0a04SGreg Roach	/**
159a25f0a04SGreg Roach	 * For relationship privacy calculations - is this individual a close relative?
160a25f0a04SGreg Roach	 *
161a25f0a04SGreg Roach	 * @param Individual $target
162cbc1590aSGreg Roach	 * @param int        $distance
163a25f0a04SGreg Roach	 *
164cbc1590aSGreg Roach	 * @return bool
165a25f0a04SGreg Roach	 */
166a25f0a04SGreg Roach	private static function isRelated(Individual $target, $distance) {
167a25f0a04SGreg Roach		static $cache = null;
168a25f0a04SGreg Roach
16906ef8e02SGreg Roach		$user_individual = self::getInstance($target->tree->getUserPreference(Auth::user(), 'gedcomid'), $target->tree);
170a25f0a04SGreg Roach		if ($user_individual) {
171a25f0a04SGreg Roach			if (!$cache) {
17213abd6f3SGreg Roach				$cache = [
17313abd6f3SGreg Roach					0 => [$user_individual],
17413abd6f3SGreg Roach					1 => [],
17513abd6f3SGreg Roach				];
1764b9ff166SGreg Roach				foreach ($user_individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
177a25f0a04SGreg Roach					$family = $fact->getTarget();
178a25f0a04SGreg Roach					if ($family) {
179a25f0a04SGreg Roach						$cache[1][] = $family;
180a25f0a04SGreg Roach					}
181a25f0a04SGreg Roach				}
182a25f0a04SGreg Roach			}
183a25f0a04SGreg Roach		} else {
184a25f0a04SGreg Roach			// No individual linked to this account? Cannot use relationship privacy.
185a25f0a04SGreg Roach			return true;
186a25f0a04SGreg Roach		}
187a25f0a04SGreg Roach
188a25f0a04SGreg Roach		// Double the distance, as we count the INDI-FAM and FAM-INDI links separately
189a25f0a04SGreg Roach		$distance *= 2;
190a25f0a04SGreg Roach
191a25f0a04SGreg Roach		// Consider each path length in turn
192a25f0a04SGreg Roach		for ($n = 0; $n <= $distance; ++$n) {
193a25f0a04SGreg Roach			if (array_key_exists($n, $cache)) {
194a25f0a04SGreg Roach				// We have already calculated all records with this length
195a25f0a04SGreg Roach				if ($n % 2 == 0 && in_array($target, $cache[$n], true)) {
196a25f0a04SGreg Roach					return true;
197a25f0a04SGreg Roach				}
198a25f0a04SGreg Roach			} else {
199a25f0a04SGreg Roach				// Need to calculate these paths
20013abd6f3SGreg Roach				$cache[$n] = [];
201a25f0a04SGreg Roach				if ($n % 2 == 0) {
202a25f0a04SGreg Roach					// Add FAM->INDI links
203a25f0a04SGreg Roach					foreach ($cache[$n - 1] as $family) {
2044b9ff166SGreg Roach						foreach ($family->getFacts('HUSB|WIFE|CHIL', false, Auth::PRIV_HIDE) as $fact) {
205a25f0a04SGreg Roach							$individual = $fact->getTarget();
206a25f0a04SGreg Roach							// Don’t backtrack
207a25f0a04SGreg Roach							if ($individual && !in_array($individual, $cache[$n - 2], true)) {
208a25f0a04SGreg Roach								$cache[$n][] = $individual;
209a25f0a04SGreg Roach							}
210a25f0a04SGreg Roach						}
211a25f0a04SGreg Roach					}
212a25f0a04SGreg Roach					if (in_array($target, $cache[$n], true)) {
213a25f0a04SGreg Roach						return true;
214a25f0a04SGreg Roach					}
215a25f0a04SGreg Roach				} else {
216a25f0a04SGreg Roach					// Add INDI->FAM links
217a25f0a04SGreg Roach					foreach ($cache[$n - 1] as $individual) {
2184b9ff166SGreg Roach						foreach ($individual->getFacts('FAM[CS]', false, Auth::PRIV_HIDE) as $fact) {
219a25f0a04SGreg Roach							$family = $fact->getTarget();
220a25f0a04SGreg Roach							// Don’t backtrack
221a25f0a04SGreg Roach							if ($family && !in_array($family, $cache[$n - 2], true)) {
222a25f0a04SGreg Roach								$cache[$n][] = $family;
223a25f0a04SGreg Roach							}
224a25f0a04SGreg Roach						}
225a25f0a04SGreg Roach					}
226a25f0a04SGreg Roach				}
227a25f0a04SGreg Roach			}
228a25f0a04SGreg Roach		}
229a25f0a04SGreg Roach
230a25f0a04SGreg Roach		return false;
231a25f0a04SGreg Roach	}
232a25f0a04SGreg Roach
23376692c8bSGreg Roach	/**
23476692c8bSGreg Roach	 * Generate a private version of this record
23576692c8bSGreg Roach	 *
23676692c8bSGreg Roach	 * @param int $access_level
23776692c8bSGreg Roach	 *
23876692c8bSGreg Roach	 * @return string
23976692c8bSGreg Roach	 */
240a25f0a04SGreg Roach	protected function createPrivateGedcomRecord($access_level) {
241518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
242a25f0a04SGreg Roach
243a25f0a04SGreg Roach		$rec = '0 @' . $this->xref . '@ INDI';
244518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_LIVING_NAMES') >= $access_level) {
245a25f0a04SGreg Roach			// Show all the NAME tags, including subtags
246a25f0a04SGreg Roach			foreach ($this->getFacts('NAME') as $fact) {
247a25f0a04SGreg Roach				$rec .= "\n" . $fact->getGedcom();
248a25f0a04SGreg Roach			}
249a25f0a04SGreg Roach		}
250a25f0a04SGreg Roach		// Just show the 1 FAMC/FAMS tag, not any subtags, which may contain private data
251a25f0a04SGreg Roach		preg_match_all('/\n1 (?:FAMC|FAMS) @(' . WT_REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
252a25f0a04SGreg Roach		foreach ($matches as $match) {
25324ec66ceSGreg Roach			$rela = Family::getInstance($match[1], $this->tree);
254a25f0a04SGreg Roach			if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
255a25f0a04SGreg Roach				$rec .= $match[0];
256a25f0a04SGreg Roach			}
257a25f0a04SGreg Roach		}
258a25f0a04SGreg Roach		// Don’t privatize sex.
259a25f0a04SGreg Roach		if (preg_match('/\n1 SEX [MFU]/', $this->gedcom, $match)) {
260a25f0a04SGreg Roach			$rec .= $match[0];
261a25f0a04SGreg Roach		}
262a25f0a04SGreg Roach
263a25f0a04SGreg Roach		return $rec;
264a25f0a04SGreg Roach	}
265a25f0a04SGreg Roach
26676692c8bSGreg Roach	/**
26776692c8bSGreg Roach	 * Fetch data from the database
26876692c8bSGreg Roach	 *
26976692c8bSGreg Roach	 * @param string $xref
27076692c8bSGreg Roach	 * @param int    $tree_id
27176692c8bSGreg Roach	 *
27276692c8bSGreg Roach	 * @return null|string
27376692c8bSGreg Roach	 */
27464d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
27564d9078aSGreg Roach		return Database::prepare(
27664d9078aSGreg Roach			"SELECT i_gedcom FROM `##individuals` WHERE i_id = :xref AND i_file = :tree_id"
27713abd6f3SGreg Roach		)->execute([
27864d9078aSGreg Roach			'xref'    => $xref,
27964d9078aSGreg Roach			'tree_id' => $tree_id,
28013abd6f3SGreg Roach		])->fetchOne();
281a25f0a04SGreg Roach	}
282a25f0a04SGreg Roach
283a25f0a04SGreg Roach	/**
284a25f0a04SGreg Roach	 * Static helper function to sort an array of people by birth date
285a25f0a04SGreg Roach	 *
286a25f0a04SGreg Roach	 * @param Individual $x
287a25f0a04SGreg Roach	 * @param Individual $y
288a25f0a04SGreg Roach	 *
289cbc1590aSGreg Roach	 * @return int
290a25f0a04SGreg Roach	 */
291a25f0a04SGreg Roach	public static function compareBirthDate(Individual $x, Individual $y) {
292f5b60decSGreg Roach		return Date::compare($x->getEstimatedBirthDate(), $y->getEstimatedBirthDate());
293a25f0a04SGreg Roach	}
294a25f0a04SGreg Roach
295a25f0a04SGreg Roach	/**
296a25f0a04SGreg Roach	 * Static helper function to sort an array of people by death date
297a25f0a04SGreg Roach	 *
298a25f0a04SGreg Roach	 * @param Individual $x
299a25f0a04SGreg Roach	 * @param Individual $y
300a25f0a04SGreg Roach	 *
301cbc1590aSGreg Roach	 * @return int
302a25f0a04SGreg Roach	 */
303a25f0a04SGreg Roach	public static function compareDeathDate(Individual $x, Individual $y) {
304f5b60decSGreg Roach		return Date::compare($x->getEstimatedDeathDate(), $y->getEstimatedDeathDate());
305a25f0a04SGreg Roach	}
306a25f0a04SGreg Roach
307a25f0a04SGreg Roach	/**
308a25f0a04SGreg Roach	 * Calculate whether this individual is living or dead.
309a25f0a04SGreg Roach	 * If not known to be dead, then assume living.
310a25f0a04SGreg Roach	 *
311cbc1590aSGreg Roach	 * @return bool
312a25f0a04SGreg Roach	 */
313a25f0a04SGreg Roach	public function isDead() {
314c4b3e5a2SGreg Roach		$MAX_ALIVE_AGE = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
315a25f0a04SGreg Roach
316a25f0a04SGreg Roach		// "1 DEAT Y" or "1 DEAT/2 DATE" or "1 DEAT/2 PLAC"
317a25f0a04SGreg Roach		if (preg_match('/\n1 (?:' . WT_EVENTS_DEAT . ')(?: Y|(?:\n[2-9].+)*\n2 (DATE|PLAC) )/', $this->gedcom)) {
318a25f0a04SGreg Roach			return true;
319a25f0a04SGreg Roach		}
320a25f0a04SGreg Roach
321a25f0a04SGreg Roach		// If any event occured more than $MAX_ALIVE_AGE years ago, then assume the individual is dead
322a25f0a04SGreg Roach		if (preg_match_all('/\n2 DATE (.+)/', $this->gedcom, $date_matches)) {
323a25f0a04SGreg Roach			foreach ($date_matches[1] as $date_match) {
324a25f0a04SGreg Roach				$date = new Date($date_match);
325f5b60decSGreg Roach				if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * $MAX_ALIVE_AGE) {
326a25f0a04SGreg Roach					return true;
327a25f0a04SGreg Roach				}
328a25f0a04SGreg Roach			}
329a25f0a04SGreg Roach			// The individual has one or more dated events. All are less than $MAX_ALIVE_AGE years ago.
330a25f0a04SGreg Roach			// If one of these is a birth, the individual must be alive.
331a25f0a04SGreg Roach			if (preg_match('/\n1 BIRT(?:\n[2-9].+)*\n2 DATE /', $this->gedcom)) {
332a25f0a04SGreg Roach				return false;
333a25f0a04SGreg Roach			}
334a25f0a04SGreg Roach		}
335a25f0a04SGreg Roach
336a25f0a04SGreg Roach		// If we found no conclusive dates then check the dates of close relatives.
337a25f0a04SGreg Roach
338a25f0a04SGreg Roach		// Check parents (birth and adopted)
3394b9ff166SGreg Roach		foreach ($this->getChildFamilies(Auth::PRIV_HIDE) as $family) {
3404b9ff166SGreg Roach			foreach ($family->getSpouses(Auth::PRIV_HIDE) as $parent) {
341a25f0a04SGreg Roach				// Assume parents are no more than 45 years older than their children
342a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $parent->gedcom, $date_matches);
343a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
344a25f0a04SGreg Roach					$date = new Date($date_match);
345f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 45)) {
346a25f0a04SGreg Roach						return true;
347a25f0a04SGreg Roach					}
348a25f0a04SGreg Roach				}
349a25f0a04SGreg Roach			}
350a25f0a04SGreg Roach		}
351a25f0a04SGreg Roach
352a25f0a04SGreg Roach		// Check spouses
3534b9ff166SGreg Roach		foreach ($this->getSpouseFamilies(Auth::PRIV_HIDE) as $family) {
354a25f0a04SGreg Roach			preg_match_all('/\n2 DATE (.+)/', $family->gedcom, $date_matches);
355a25f0a04SGreg Roach			foreach ($date_matches[1] as $date_match) {
356a25f0a04SGreg Roach				$date = new Date($date_match);
357a25f0a04SGreg Roach				// Assume marriage occurs after age of 10
358f5b60decSGreg Roach				if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 10)) {
359a25f0a04SGreg Roach					return true;
360a25f0a04SGreg Roach				}
361a25f0a04SGreg Roach			}
362a25f0a04SGreg Roach			// Check spouse dates
36313e3123bSGreg Roach			$spouse = $family->getSpouse($this, Auth::PRIV_HIDE);
364a25f0a04SGreg Roach			if ($spouse) {
365a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $spouse->gedcom, $date_matches);
366a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
367a25f0a04SGreg Roach					$date = new Date($date_match);
368a25f0a04SGreg Roach					// Assume max age difference between spouses of 40 years
369f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE + 40)) {
370a25f0a04SGreg Roach						return true;
371a25f0a04SGreg Roach					}
372a25f0a04SGreg Roach				}
373a25f0a04SGreg Roach			}
374a25f0a04SGreg Roach			// Check child dates
3754b9ff166SGreg Roach			foreach ($family->getChildren(Auth::PRIV_HIDE) as $child) {
376a25f0a04SGreg Roach				preg_match_all('/\n2 DATE (.+)/', $child->gedcom, $date_matches);
377a25f0a04SGreg Roach				// Assume children born after age of 15
378a25f0a04SGreg Roach				foreach ($date_matches[1] as $date_match) {
379a25f0a04SGreg Roach					$date = new Date($date_match);
380f5b60decSGreg Roach					if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 15)) {
381a25f0a04SGreg Roach						return true;
382a25f0a04SGreg Roach					}
383a25f0a04SGreg Roach				}
384a25f0a04SGreg Roach				// Check grandchildren
3854b9ff166SGreg Roach				foreach ($child->getSpouseFamilies(Auth::PRIV_HIDE) as $child_family) {
3864b9ff166SGreg Roach					foreach ($child_family->getChildren(Auth::PRIV_HIDE) as $grandchild) {
387a25f0a04SGreg Roach						preg_match_all('/\n2 DATE (.+)/', $grandchild->gedcom, $date_matches);
388a25f0a04SGreg Roach						// Assume grandchildren born after age of 30
389a25f0a04SGreg Roach						foreach ($date_matches[1] as $date_match) {
390a25f0a04SGreg Roach							$date = new Date($date_match);
391f5b60decSGreg Roach							if ($date->isOK() && $date->maximumJulianDay() <= WT_CLIENT_JD - 365 * ($MAX_ALIVE_AGE - 30)) {
392a25f0a04SGreg Roach								return true;
393a25f0a04SGreg Roach							}
394a25f0a04SGreg Roach						}
395a25f0a04SGreg Roach					}
396a25f0a04SGreg Roach				}
397a25f0a04SGreg Roach			}
398a25f0a04SGreg Roach		}
399a25f0a04SGreg Roach
400a25f0a04SGreg Roach		return false;
401a25f0a04SGreg Roach	}
402a25f0a04SGreg Roach
403a25f0a04SGreg Roach	/**
404a25f0a04SGreg Roach	 * Find the highlighted media object for an individual
405a25f0a04SGreg Roach	 *
4064a9f750fSGreg Roach	 * @return null|MediaFile
407a25f0a04SGreg Roach	 */
4084a9f750fSGreg Roach	public function findHighlightedMediaFile() {
409c4fdfd2dSGreg Roach		foreach ($this->getFacts('OBJE') as $fact) {
410c4fdfd2dSGreg Roach			$media = $fact->getTarget();
411a213a63cSGreg Roach			if ($media instanceof Media) {
4124a9f750fSGreg Roach				foreach ($media->mediaFiles() as $media_file) {
413a213a63cSGreg Roach					if ($media_file->isImage() && !$media_file->isExternal()) {
4144a9f750fSGreg Roach						return $media_file;
4154a9f750fSGreg Roach					}
4164a9f750fSGreg Roach				}
417a25f0a04SGreg Roach			}
418a25f0a04SGreg Roach		}
419a25f0a04SGreg Roach
420a25f0a04SGreg Roach		return null;
421a25f0a04SGreg Roach	}
422a25f0a04SGreg Roach
423a25f0a04SGreg Roach	/**
424a25f0a04SGreg Roach	 * Display the prefered image for this individual.
425a25f0a04SGreg Roach	 * Use an icon if no image is available.
426a25f0a04SGreg Roach	 *
427dce07401SGreg Roach	 * @param int      $width      Pixels
428dce07401SGreg Roach	 * @param int      $height     Pixels
429dce07401SGreg Roach	 * @param string   $fit        "crop" or "contain"
430dce07401SGreg Roach	 * @param string[] $attributes Additional HTML attributes
431dce07401SGreg Roach	 *
432a25f0a04SGreg Roach	 * @return string
433a25f0a04SGreg Roach	 */
434dce07401SGreg Roach	public function displayImage($width, $height, $fit, $attributes) {
4354a9f750fSGreg Roach		$media_file = $this->findHighlightedMediaFile();
4364a9f750fSGreg Roach
4374a9f750fSGreg Roach		if ($media_file !== null) {
4384a9f750fSGreg Roach			return $media_file->displayImage($width, $height, $fit, $attributes);
439a25f0a04SGreg Roach		}
4404a9f750fSGreg Roach
4414a9f750fSGreg Roach		if ($this->tree->getPreference('USE_SILHOUETTE')) {
4424a9f750fSGreg Roach			return '<i class="icon-silhouette-' . $this->getSex() . '"></i>';
4434a9f750fSGreg Roach		}
4444a9f750fSGreg Roach
4454a9f750fSGreg Roach		return '';
446a25f0a04SGreg Roach	}
447a25f0a04SGreg Roach
448a25f0a04SGreg Roach	/**
449a25f0a04SGreg Roach	 * Get the date of birth
450a25f0a04SGreg Roach	 *
451a25f0a04SGreg Roach	 * @return Date
452a25f0a04SGreg Roach	 */
453ffd703eaSGreg Roach	public function getBirthDate() {
454a25f0a04SGreg Roach		foreach ($this->getAllBirthDates() as $date) {
455a25f0a04SGreg Roach			if ($date->isOK()) {
456a25f0a04SGreg Roach				return $date;
457a25f0a04SGreg Roach			}
458a25f0a04SGreg Roach		}
459a25f0a04SGreg Roach
460a25f0a04SGreg Roach		return new Date('');
461a25f0a04SGreg Roach	}
462a25f0a04SGreg Roach
463a25f0a04SGreg Roach	/**
464a25f0a04SGreg Roach	 * Get the place of birth
465a25f0a04SGreg Roach	 *
46616d0b7f7SRico Sonntag	 * @return Place
467a25f0a04SGreg Roach	 */
468ffd703eaSGreg Roach	public function getBirthPlace() {
469a25f0a04SGreg Roach		foreach ($this->getAllBirthPlaces() as $place) {
470a25f0a04SGreg Roach			if ($place) {
471a25f0a04SGreg Roach				return $place;
472a25f0a04SGreg Roach			}
473a25f0a04SGreg Roach		}
474a25f0a04SGreg Roach
475b20ddbf9SGreg Roach		return new Place('', $this->tree);
476a25f0a04SGreg Roach	}
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach	/**
479a25f0a04SGreg Roach	 * Get the year of birth
480a25f0a04SGreg Roach	 *
481a25f0a04SGreg Roach	 * @return string the year of birth
482a25f0a04SGreg Roach	 */
483ffd703eaSGreg Roach	public function getBirthYear() {
484f5b60decSGreg Roach		return $this->getBirthDate()->minimumDate()->format('%Y');
485a25f0a04SGreg Roach	}
486a25f0a04SGreg Roach
487a25f0a04SGreg Roach	/**
488a25f0a04SGreg Roach	 * Get the date of death
489a25f0a04SGreg Roach	 *
490a25f0a04SGreg Roach	 * @return Date
491a25f0a04SGreg Roach	 */
492ffd703eaSGreg Roach	public function getDeathDate() {
493a25f0a04SGreg Roach		foreach ($this->getAllDeathDates() as $date) {
494a25f0a04SGreg Roach			if ($date->isOK()) {
495a25f0a04SGreg Roach				return $date;
496a25f0a04SGreg Roach			}
497a25f0a04SGreg Roach		}
498a25f0a04SGreg Roach
499a25f0a04SGreg Roach		return new Date('');
500a25f0a04SGreg Roach	}
501a25f0a04SGreg Roach
502a25f0a04SGreg Roach	/**
503a25f0a04SGreg Roach	 * Get the place of death
504a25f0a04SGreg Roach	 *
50516d0b7f7SRico Sonntag	 * @return Place
506a25f0a04SGreg Roach	 */
507ffd703eaSGreg Roach	public function getDeathPlace() {
508a25f0a04SGreg Roach		foreach ($this->getAllDeathPlaces() as $place) {
509a25f0a04SGreg Roach			if ($place) {
510a25f0a04SGreg Roach				return $place;
511a25f0a04SGreg Roach			}
512a25f0a04SGreg Roach		}
513a25f0a04SGreg Roach
514b20ddbf9SGreg Roach		return new Place('', $this->tree);
515a25f0a04SGreg Roach	}
516a25f0a04SGreg Roach
517a25f0a04SGreg Roach	/**
518a25f0a04SGreg Roach	 * get the death year
519a25f0a04SGreg Roach	 *
520a25f0a04SGreg Roach	 * @return string the year of death
521a25f0a04SGreg Roach	 */
522ffd703eaSGreg Roach	public function getDeathYear() {
523f5b60decSGreg Roach		return $this->getDeathDate()->minimumDate()->format('%Y');
524a25f0a04SGreg Roach	}
525a25f0a04SGreg Roach
526a25f0a04SGreg Roach	/**
527a25f0a04SGreg Roach	 * Get the range of years in which a individual lived. e.g. “1870–”, “1870–1920”, “–1920”.
52815d603e7SGreg Roach	 * Provide the place and full date using a tooltip.
529a25f0a04SGreg Roach	 * For consistent layout in charts, etc., show just a “–” when no dates are known.
530a25f0a04SGreg Roach	 * Note that this is a (non-breaking) en-dash, and not a hyphen.
531a25f0a04SGreg Roach	 *
532a25f0a04SGreg Roach	 * @return string
533a25f0a04SGreg Roach	 */
534a25f0a04SGreg Roach	public function getLifeSpan() {
53515d603e7SGreg Roach		// Just the first part of the place name
5361e9c2c49SGreg Roach		$birth_place = strip_tags($this->getBirthPlace()->getShortName());
5371e9c2c49SGreg Roach		$death_place = strip_tags($this->getDeathPlace()->getShortName());
53815d603e7SGreg Roach		// Remove markup from dates
53915d603e7SGreg Roach		$birth_date = strip_tags($this->getBirthDate()->display());
54015d603e7SGreg Roach		$death_date = strip_tags($this->getDeathDate()->display());
54115d603e7SGreg Roach
542a25f0a04SGreg Roach		return
543a25f0a04SGreg Roach			/* I18N: A range of years, e.g. “1870–”, “1870–1920”, “–1920” */ I18N::translate(
544a25f0a04SGreg Roach				'%1$s–%2$s',
545d53324c9SGreg Roach				'<span title="' . e($birth_place) . ' ' . $birth_date . '">' . $this->getBirthYear() . '</span>',
546d53324c9SGreg Roach				'<span title="' . e($death_place) . ' ' . $death_date . '">' . $this->getDeathYear() . '</span>'
547a25f0a04SGreg Roach			);
548a25f0a04SGreg Roach	}
549a25f0a04SGreg Roach
550a25f0a04SGreg Roach	/**
551a25f0a04SGreg Roach	 * Get all the birth dates - for the individual lists.
552a25f0a04SGreg Roach	 *
553a25f0a04SGreg Roach	 * @return Date[]
554a25f0a04SGreg Roach	 */
555ffd703eaSGreg Roach	public function getAllBirthDates() {
556a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_BIRT) as $event) {
557a25f0a04SGreg Roach			$tmp = $this->getAllEventDates($event);
558a25f0a04SGreg Roach			if ($tmp) {
559a25f0a04SGreg Roach				return $tmp;
560a25f0a04SGreg Roach			}
561a25f0a04SGreg Roach		}
562a25f0a04SGreg Roach
56313abd6f3SGreg Roach		return [];
564a25f0a04SGreg Roach	}
565a25f0a04SGreg Roach
566a25f0a04SGreg Roach	/**
567a25f0a04SGreg Roach	 * Gat all the birth places - for the individual lists.
568a25f0a04SGreg Roach	 *
5694080d558SGreg Roach	 * @return Place[]
570a25f0a04SGreg Roach	 */
571ffd703eaSGreg Roach	public function getAllBirthPlaces() {
572a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_BIRT) as $event) {
5734080d558SGreg Roach			$places = $this->getAllEventPlaces($event);
5744080d558SGreg Roach			if (!empty($places)) {
5754080d558SGreg Roach				return $places;
576a25f0a04SGreg Roach			}
577a25f0a04SGreg Roach		}
578a25f0a04SGreg Roach
57913abd6f3SGreg Roach		return [];
580a25f0a04SGreg Roach	}
581a25f0a04SGreg Roach
582a25f0a04SGreg Roach	/**
583a25f0a04SGreg Roach	 * Get all the death dates - for the individual lists.
584a25f0a04SGreg Roach	 *
585a25f0a04SGreg Roach	 * @return Date[]
586a25f0a04SGreg Roach	 */
587ffd703eaSGreg Roach	public function getAllDeathDates() {
588a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_DEAT) as $event) {
589a25f0a04SGreg Roach			$tmp = $this->getAllEventDates($event);
590a25f0a04SGreg Roach			if ($tmp) {
591a25f0a04SGreg Roach				return $tmp;
592a25f0a04SGreg Roach			}
593a25f0a04SGreg Roach		}
594a25f0a04SGreg Roach
59513abd6f3SGreg Roach		return [];
596a25f0a04SGreg Roach	}
597a25f0a04SGreg Roach
598a25f0a04SGreg Roach	/**
599a25f0a04SGreg Roach	 * Get all the death places - for the individual lists.
600a25f0a04SGreg Roach	 *
6014080d558SGreg Roach	 * @return Place[]
602a25f0a04SGreg Roach	 */
603ffd703eaSGreg Roach	public function getAllDeathPlaces() {
604a25f0a04SGreg Roach		foreach (explode('|', WT_EVENTS_DEAT) as $event) {
6054080d558SGreg Roach			$places = $this->getAllEventPlaces($event);
6064080d558SGreg Roach			if (!empty($places)) {
6074080d558SGreg Roach				return $places;
608a25f0a04SGreg Roach			}
609a25f0a04SGreg Roach		}
610a25f0a04SGreg Roach
61113abd6f3SGreg Roach		return [];
612a25f0a04SGreg Roach	}
613a25f0a04SGreg Roach
614a25f0a04SGreg Roach	/**
615a25f0a04SGreg Roach	 * Generate an estimate for the date of birth, based on dates of parents/children/spouses
616a25f0a04SGreg Roach	 *
617a25f0a04SGreg Roach	 * @return Date
618a25f0a04SGreg Roach	 */
619ffd703eaSGreg Roach	public function getEstimatedBirthDate() {
620a25f0a04SGreg Roach		if (is_null($this->_getEstimatedBirthDate)) {
621a25f0a04SGreg Roach			foreach ($this->getAllBirthDates() as $date) {
622a25f0a04SGreg Roach				if ($date->isOK()) {
623a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = $date;
624a25f0a04SGreg Roach					break;
625a25f0a04SGreg Roach				}
626a25f0a04SGreg Roach			}
627a25f0a04SGreg Roach			if (is_null($this->_getEstimatedBirthDate)) {
62813abd6f3SGreg Roach				$min = [];
62913abd6f3SGreg Roach				$max = [];
630a25f0a04SGreg Roach				$tmp = $this->getDeathDate();
631f5b60decSGreg Roach				if ($tmp->isOK()) {
632f5b60decSGreg Roach					$min[] = $tmp->minimumJulianDay() - $this->tree->getPreference('MAX_ALIVE_AGE') * 365;
633f5b60decSGreg Roach					$max[] = $tmp->maximumJulianDay();
634a25f0a04SGreg Roach				}
635a25f0a04SGreg Roach				foreach ($this->getChildFamilies() as $family) {
636a25f0a04SGreg Roach					$tmp = $family->getMarriageDate();
637f5b60decSGreg Roach					if ($tmp->isOK()) {
638f5b60decSGreg Roach						$min[] = $tmp->maximumJulianDay() - 365 * 1;
639f5b60decSGreg Roach						$max[] = $tmp->minimumJulianDay() + 365 * 30;
640a25f0a04SGreg Roach					}
641a25f0a04SGreg Roach					if ($parent = $family->getHusband()) {
642a25f0a04SGreg Roach						$tmp = $parent->getBirthDate();
643f5b60decSGreg Roach						if ($tmp->isOK()) {
644f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() + 365 * 15;
645f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 65;
646a25f0a04SGreg Roach						}
647a25f0a04SGreg Roach					}
648a25f0a04SGreg Roach					if ($parent = $family->getWife()) {
649a25f0a04SGreg Roach						$tmp = $parent->getBirthDate();
650f5b60decSGreg Roach						if ($tmp->isOK()) {
651f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() + 365 * 15;
652f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 45;
653a25f0a04SGreg Roach						}
654a25f0a04SGreg Roach					}
655a25f0a04SGreg Roach					foreach ($family->getChildren() as $child) {
656a25f0a04SGreg Roach						$tmp = $child->getBirthDate();
657f5b60decSGreg Roach						if ($tmp->isOK()) {
658f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * 30;
659f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 30;
660a25f0a04SGreg Roach						}
661a25f0a04SGreg Roach					}
662a25f0a04SGreg Roach				}
663a25f0a04SGreg Roach				foreach ($this->getSpouseFamilies() as $family) {
664a25f0a04SGreg Roach					$tmp = $family->getMarriageDate();
665f5b60decSGreg Roach					if ($tmp->isOK()) {
666f5b60decSGreg Roach						$min[] = $tmp->maximumJulianDay() - 365 * 45;
667f5b60decSGreg Roach						$max[] = $tmp->minimumJulianDay() - 365 * 15;
668a25f0a04SGreg Roach					}
669a25f0a04SGreg Roach					$spouse = $family->getSpouse($this);
670a25f0a04SGreg Roach					if ($spouse) {
671a25f0a04SGreg Roach						$tmp = $spouse->getBirthDate();
672f5b60decSGreg Roach						if ($tmp->isOK()) {
673f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * 25;
674f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() + 365 * 25;
675a25f0a04SGreg Roach						}
676a25f0a04SGreg Roach					}
677a25f0a04SGreg Roach					foreach ($family->getChildren() as $child) {
678a25f0a04SGreg Roach						$tmp = $child->getBirthDate();
679f5b60decSGreg Roach						if ($tmp->isOK()) {
680f5b60decSGreg Roach							$min[] = $tmp->maximumJulianDay() - 365 * ($this->getSex() == 'F' ? 45 : 65);
681f5b60decSGreg Roach							$max[] = $tmp->minimumJulianDay() - 365 * 15;
682a25f0a04SGreg Roach						}
683a25f0a04SGreg Roach					}
684a25f0a04SGreg Roach				}
685a25f0a04SGreg Roach				if ($min && $max) {
686a25f0a04SGreg Roach					$gregorian_calendar = new GregorianCalendar;
687a25f0a04SGreg Roach
688a25f0a04SGreg Roach					list($year)                   = $gregorian_calendar->jdToYmd((int) ((max($min) + min($max)) / 2));
689a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = new Date('EST ' . $year);
690a25f0a04SGreg Roach				} else {
691a25f0a04SGreg Roach					$this->_getEstimatedBirthDate = new Date(''); // always return a date object
692a25f0a04SGreg Roach				}
693a25f0a04SGreg Roach			}
694a25f0a04SGreg Roach		}
695a25f0a04SGreg Roach
696a25f0a04SGreg Roach		return $this->_getEstimatedBirthDate;
697a25f0a04SGreg Roach	}
698a25f0a04SGreg Roach
699a25f0a04SGreg Roach	/**
700a25f0a04SGreg Roach	 * Generate an estimated date of death.
701a25f0a04SGreg Roach	 *
702a25f0a04SGreg Roach	 * @return Date
703a25f0a04SGreg Roach	 */
704ffd703eaSGreg Roach	public function getEstimatedDeathDate() {
705a25f0a04SGreg Roach		if ($this->_getEstimatedDeathDate === null) {
706a25f0a04SGreg Roach			foreach ($this->getAllDeathDates() as $date) {
707a25f0a04SGreg Roach				if ($date->isOK()) {
708a25f0a04SGreg Roach					$this->_getEstimatedDeathDate = $date;
709a25f0a04SGreg Roach					break;
710a25f0a04SGreg Roach				}
711a25f0a04SGreg Roach			}
712a25f0a04SGreg Roach			if ($this->_getEstimatedDeathDate === null) {
713f5b60decSGreg Roach				if ($this->getEstimatedBirthDate()->minimumJulianDay()) {
714c4b3e5a2SGreg Roach					$max_alive_age                = (int) $this->tree->getPreference('MAX_ALIVE_AGE');
715c4b3e5a2SGreg Roach					$this->_getEstimatedDeathDate = $this->getEstimatedBirthDate()->addYears($max_alive_age, 'BEF');
716a25f0a04SGreg Roach				} else {
717a25f0a04SGreg Roach					$this->_getEstimatedDeathDate = new Date(''); // always return a date object
718a25f0a04SGreg Roach				}
719a25f0a04SGreg Roach			}
720a25f0a04SGreg Roach		}
721a25f0a04SGreg Roach
722a25f0a04SGreg Roach		return $this->_getEstimatedDeathDate;
723a25f0a04SGreg Roach	}
724a25f0a04SGreg Roach
725a25f0a04SGreg Roach	/**
726a25f0a04SGreg Roach	 * Get the sex - M F or U
727a25f0a04SGreg Roach	 * Use the un-privatised gedcom record. We call this function during
728a25f0a04SGreg Roach	 * the privatize-gedcom function, and we are allowed to know this.
729a25f0a04SGreg Roach	 *
730a25f0a04SGreg Roach	 * @return string
731a25f0a04SGreg Roach	 */
732ffd703eaSGreg Roach	public function getSex() {
733a25f0a04SGreg Roach		if (preg_match('/\n1 SEX ([MF])/', $this->gedcom . $this->pending, $match)) {
734a25f0a04SGreg Roach			return $match[1];
735a25f0a04SGreg Roach		} else {
736a25f0a04SGreg Roach			return 'U';
737a25f0a04SGreg Roach		}
738a25f0a04SGreg Roach	}
739a25f0a04SGreg Roach
740a25f0a04SGreg Roach	/**
741a25f0a04SGreg Roach	 * Get the individual’s sex image
742a25f0a04SGreg Roach	 *
743a25f0a04SGreg Roach	 * @param string $size
744a25f0a04SGreg Roach	 *
745a25f0a04SGreg Roach	 * @return string
746a25f0a04SGreg Roach	 */
747ffd703eaSGreg Roach	public function getSexImage($size = 'small') {
748a25f0a04SGreg Roach		return self::sexImage($this->getSex(), $size);
749a25f0a04SGreg Roach	}
750a25f0a04SGreg Roach
751a25f0a04SGreg Roach	/**
752a25f0a04SGreg Roach	 * Generate a sex icon/image
753a25f0a04SGreg Roach	 *
754a25f0a04SGreg Roach	 * @param string $sex
755a25f0a04SGreg Roach	 * @param string $size
756a25f0a04SGreg Roach	 *
757a25f0a04SGreg Roach	 * @return string
758a25f0a04SGreg Roach	 */
759ffd703eaSGreg Roach	public static function sexImage($sex, $size = 'small') {
760a25f0a04SGreg Roach		return '<i class="icon-sex_' . strtolower($sex) . '_' . ($size == 'small' ? '9x9' : '15x15') . '"></i>';
761a25f0a04SGreg Roach	}
762a25f0a04SGreg Roach
763a25f0a04SGreg Roach	/**
764a25f0a04SGreg Roach	 * Generate the CSS class to be used for drawing this individual
765a25f0a04SGreg Roach	 *
766a25f0a04SGreg Roach	 * @return string
767a25f0a04SGreg Roach	 */
768ffd703eaSGreg Roach	public function getBoxStyle() {
76913abd6f3SGreg Roach		$tmp = ['M' => '', 'F' => 'F', 'U' => 'NN'];
770a25f0a04SGreg Roach
771a25f0a04SGreg Roach		return 'person_box' . $tmp[$this->getSex()];
772a25f0a04SGreg Roach	}
773a25f0a04SGreg Roach
774a25f0a04SGreg Roach	/**
775a25f0a04SGreg Roach	 * Get a list of this individual’s spouse families
776a25f0a04SGreg Roach	 *
777cbc1590aSGreg Roach	 * @param int|null $access_level
778a25f0a04SGreg Roach	 *
779a25f0a04SGreg Roach	 * @return Family[]
780a25f0a04SGreg Roach	 */
7814b9ff166SGreg Roach	public function getSpouseFamilies($access_level = null) {
7824b9ff166SGreg Roach		if ($access_level === null) {
7834b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
7844b9ff166SGreg Roach		}
7854b9ff166SGreg Roach
786518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
787a25f0a04SGreg Roach
78813abd6f3SGreg Roach		$families = [];
789a25f0a04SGreg Roach		foreach ($this->getFacts('FAMS', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
790a25f0a04SGreg Roach			$family = $fact->getTarget();
791a25f0a04SGreg Roach			if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
792a25f0a04SGreg Roach				$families[] = $family;
793a25f0a04SGreg Roach			}
794a25f0a04SGreg Roach		}
795a25f0a04SGreg Roach
796a25f0a04SGreg Roach		return $families;
797a25f0a04SGreg Roach	}
798a25f0a04SGreg Roach
799a25f0a04SGreg Roach	/**
800a25f0a04SGreg Roach	 * Get the current spouse of this individual.
801a25f0a04SGreg Roach	 *
802a25f0a04SGreg Roach	 * Where an individual has multiple spouses, assume they are stored
803a25f0a04SGreg Roach	 * in chronological order, and take the last one found.
804a25f0a04SGreg Roach	 *
805a25f0a04SGreg Roach	 * @return Individual|null
806a25f0a04SGreg Roach	 */
807a25f0a04SGreg Roach	public function getCurrentSpouse() {
808a25f0a04SGreg Roach		$tmp    = $this->getSpouseFamilies();
809a25f0a04SGreg Roach		$family = end($tmp);
810a25f0a04SGreg Roach		if ($family) {
811a25f0a04SGreg Roach			return $family->getSpouse($this);
812a25f0a04SGreg Roach		} else {
813a25f0a04SGreg Roach			return null;
814a25f0a04SGreg Roach		}
815a25f0a04SGreg Roach	}
816a25f0a04SGreg Roach
817a25f0a04SGreg Roach	/**
818a25f0a04SGreg Roach	 * Count the children belonging to this individual.
819a25f0a04SGreg Roach	 *
820cbc1590aSGreg Roach	 * @return int
821a25f0a04SGreg Roach	 */
822a25f0a04SGreg Roach	public function getNumberOfChildren() {
823a25f0a04SGreg Roach		if (preg_match('/\n1 NCHI (\d+)(?:\n|$)/', $this->getGedcom(), $match)) {
824a25f0a04SGreg Roach			return $match[1];
825a25f0a04SGreg Roach		} else {
82613abd6f3SGreg Roach			$children = [];
827a25f0a04SGreg Roach			foreach ($this->getSpouseFamilies() as $fam) {
828a25f0a04SGreg Roach				foreach ($fam->getChildren() as $child) {
829a25f0a04SGreg Roach					$children[$child->getXref()] = true;
830a25f0a04SGreg Roach				}
831a25f0a04SGreg Roach			}
832a25f0a04SGreg Roach
833a25f0a04SGreg Roach			return count($children);
834a25f0a04SGreg Roach		}
835a25f0a04SGreg Roach	}
836a25f0a04SGreg Roach
837a25f0a04SGreg Roach	/**
838a25f0a04SGreg Roach	 * Get a list of this individual’s child families (i.e. their parents).
839a25f0a04SGreg Roach	 *
840cbc1590aSGreg Roach	 * @param int|null $access_level
841a25f0a04SGreg Roach	 *
842a25f0a04SGreg Roach	 * @return Family[]
843a25f0a04SGreg Roach	 */
8444b9ff166SGreg Roach	public function getChildFamilies($access_level = null) {
8454b9ff166SGreg Roach		if ($access_level === null) {
8464b9ff166SGreg Roach			$access_level = Auth::accessLevel($this->tree);
8474b9ff166SGreg Roach		}
8484b9ff166SGreg Roach
849518bbdc1SGreg Roach		$SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
850a25f0a04SGreg Roach
85113abd6f3SGreg Roach		$families = [];
852a25f0a04SGreg Roach		foreach ($this->getFacts('FAMC', false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
853a25f0a04SGreg Roach			$family = $fact->getTarget();
854a25f0a04SGreg Roach			if ($family && ($SHOW_PRIVATE_RELATIONSHIPS || $family->canShow($access_level))) {
855a25f0a04SGreg Roach				$families[] = $family;
856a25f0a04SGreg Roach			}
857a25f0a04SGreg Roach		}
858a25f0a04SGreg Roach
859a25f0a04SGreg Roach		return $families;
860a25f0a04SGreg Roach	}
861a25f0a04SGreg Roach
862a25f0a04SGreg Roach	/**
863a25f0a04SGreg Roach	 * Get the preferred parents for this individual.
864a25f0a04SGreg Roach	 *
865a25f0a04SGreg Roach	 * An individual may multiple parents (e.g. birth, adopted, disputed).
866a25f0a04SGreg Roach	 * The preferred family record is:
867a25f0a04SGreg Roach	 * (a) the first one with an explicit tag "_PRIMARY Y"
868a25f0a04SGreg Roach	 * (b) the first one with a pedigree of "birth"
869a25f0a04SGreg Roach	 * (c) the first one with no pedigree (default is "birth")
870a25f0a04SGreg Roach	 * (d) the first one found
871a25f0a04SGreg Roach	 *
872a25f0a04SGreg Roach	 * @return Family|null
873a25f0a04SGreg Roach	 */
874a25f0a04SGreg Roach	public function getPrimaryChildFamily() {
875a25f0a04SGreg Roach		$families = $this->getChildFamilies();
876a25f0a04SGreg Roach		switch (count($families)) {
877a25f0a04SGreg Roach			case 0:
878a25f0a04SGreg Roach				return null;
879a25f0a04SGreg Roach			case 1:
88015d603e7SGreg Roach				return $families[0];
881a25f0a04SGreg Roach			default:
882a25f0a04SGreg Roach				// If there is more than one FAMC record, choose the preferred parents:
883a25f0a04SGreg Roach				// a) records with '2 _PRIMARY'
884a25f0a04SGreg Roach				foreach ($families as $famid => $fam) {
885a25f0a04SGreg Roach					if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->getGedcom())) {
886a25f0a04SGreg Roach						return $fam;
887a25f0a04SGreg Roach					}
888a25f0a04SGreg Roach				}
889a25f0a04SGreg Roach				// b) records with '2 PEDI birt'
890a25f0a04SGreg Roach				foreach ($families as $famid => $fam) {
891a25f0a04SGreg Roach					if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->getGedcom())) {
892a25f0a04SGreg Roach						return $fam;
893a25f0a04SGreg Roach					}
894a25f0a04SGreg Roach				}
895a25f0a04SGreg Roach				// c) records with no '2 PEDI'
896a25f0a04SGreg Roach				foreach ($families as $famid => $fam) {
897a25f0a04SGreg Roach					if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->getGedcom())) {
898a25f0a04SGreg Roach						return $fam;
899a25f0a04SGreg Roach					}
900a25f0a04SGreg Roach				}
901a25f0a04SGreg Roach
902a25f0a04SGreg Roach				// d) any record
90315d603e7SGreg Roach				return $families[0];
904a25f0a04SGreg Roach		}
905a25f0a04SGreg Roach	}
906a25f0a04SGreg Roach
907a25f0a04SGreg Roach	/**
908a25f0a04SGreg Roach	 * Get a list of step-parent families.
909a25f0a04SGreg Roach	 *
910a25f0a04SGreg Roach	 * @return Family[]
911a25f0a04SGreg Roach	 */
912ffd703eaSGreg Roach	public function getChildStepFamilies() {
91313abd6f3SGreg Roach		$step_families = [];
914a25f0a04SGreg Roach		$families      = $this->getChildFamilies();
915a25f0a04SGreg Roach		foreach ($families as $family) {
916a25f0a04SGreg Roach			$father = $family->getHusband();
917a25f0a04SGreg Roach			if ($father) {
918a25f0a04SGreg Roach				foreach ($father->getSpouseFamilies() as $step_family) {
919a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
920a25f0a04SGreg Roach						$step_families[] = $step_family;
921a25f0a04SGreg Roach					}
922a25f0a04SGreg Roach				}
923a25f0a04SGreg Roach			}
924a25f0a04SGreg Roach			$mother = $family->getWife();
925a25f0a04SGreg Roach			if ($mother) {
926a25f0a04SGreg Roach				foreach ($mother->getSpouseFamilies() as $step_family) {
927a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
928a25f0a04SGreg Roach						$step_families[] = $step_family;
929a25f0a04SGreg Roach					}
930a25f0a04SGreg Roach				}
931a25f0a04SGreg Roach			}
932a25f0a04SGreg Roach		}
933a25f0a04SGreg Roach
934a25f0a04SGreg Roach		return $step_families;
935a25f0a04SGreg Roach	}
936a25f0a04SGreg Roach
937a25f0a04SGreg Roach	/**
938a25f0a04SGreg Roach	 * Get a list of step-parent families.
939a25f0a04SGreg Roach	 *
940a25f0a04SGreg Roach	 * @return Family[]
941a25f0a04SGreg Roach	 */
942ffd703eaSGreg Roach	public function getSpouseStepFamilies() {
94313abd6f3SGreg Roach		$step_families = [];
944a25f0a04SGreg Roach		$families      = $this->getSpouseFamilies();
945a25f0a04SGreg Roach		foreach ($families as $family) {
946a25f0a04SGreg Roach			$spouse = $family->getSpouse($this);
947a25f0a04SGreg Roach			if ($spouse) {
948a25f0a04SGreg Roach				foreach ($family->getSpouse($this)->getSpouseFamilies() as $step_family) {
949a25f0a04SGreg Roach					if (!in_array($step_family, $families, true)) {
950a25f0a04SGreg Roach						$step_families[] = $step_family;
951a25f0a04SGreg Roach					}
952a25f0a04SGreg Roach				}
953a25f0a04SGreg Roach			}
954a25f0a04SGreg Roach		}
955a25f0a04SGreg Roach
956a25f0a04SGreg Roach		return $step_families;
957a25f0a04SGreg Roach	}
958a25f0a04SGreg Roach
959a25f0a04SGreg Roach	/**
960a25f0a04SGreg Roach	 * A label for a parental family group
961a25f0a04SGreg Roach	 *
962a25f0a04SGreg Roach	 * @param Family $family
963a25f0a04SGreg Roach	 *
964a25f0a04SGreg Roach	 * @return string
965a25f0a04SGreg Roach	 */
966ffd703eaSGreg Roach	public function getChildFamilyLabel(Family $family) {
967a25f0a04SGreg Roach		if (preg_match('/\n1 FAMC @' . $family->getXref() . '@(?:\n[2-9].*)*\n2 PEDI (.+)/', $this->getGedcom(), $match)) {
968a25f0a04SGreg Roach			// A specified pedigree
969764a01d9SGreg Roach			return GedcomCodePedi::getChildFamilyLabel($match[1]);
970a25f0a04SGreg Roach		} else {
971a25f0a04SGreg Roach			// Default (birth) pedigree
972764a01d9SGreg Roach			return GedcomCodePedi::getChildFamilyLabel('');
973a25f0a04SGreg Roach		}
974a25f0a04SGreg Roach	}
975a25f0a04SGreg Roach
976a25f0a04SGreg Roach	/**
977a25f0a04SGreg Roach	 * Create a label for a step family
978a25f0a04SGreg Roach	 *
979a25f0a04SGreg Roach	 * @param Family $step_family
980a25f0a04SGreg Roach	 *
981a25f0a04SGreg Roach	 * @return string
982a25f0a04SGreg Roach	 */
983ffd703eaSGreg Roach	public function getStepFamilyLabel(Family $step_family) {
984a25f0a04SGreg Roach		foreach ($this->getChildFamilies() as $family) {
985a25f0a04SGreg Roach			if ($family !== $step_family) {
986a25f0a04SGreg Roach				// Must be a step-family
987a25f0a04SGreg Roach				foreach ($family->getSpouses() as $parent) {
988a25f0a04SGreg Roach					foreach ($step_family->getSpouses() as $step_parent) {
989a25f0a04SGreg Roach						if ($parent === $step_parent) {
990a25f0a04SGreg Roach							// One common parent - must be a step family
991a25f0a04SGreg Roach							if ($parent->getSex() == 'M') {
992a25f0a04SGreg Roach								// Father’s family with someone else
993a25f0a04SGreg Roach								if ($step_family->getSpouse($step_parent)) {
994a25f0a04SGreg Roach									return
995a25f0a04SGreg Roach										/* I18N: A step-family. %s is an individual’s name */
996a25f0a04SGreg Roach										I18N::translate('Father’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
997a25f0a04SGreg Roach								} else {
998a25f0a04SGreg Roach									return
999a25f0a04SGreg Roach										/* I18N: A step-family. */
1000a25f0a04SGreg Roach										I18N::translate('Father’s family with an unknown individual');
1001a25f0a04SGreg Roach								}
1002a25f0a04SGreg Roach							} else {
1003a25f0a04SGreg Roach								// Mother’s family with someone else
1004a25f0a04SGreg Roach								if ($step_family->getSpouse($step_parent)) {
1005a25f0a04SGreg Roach									return
1006a25f0a04SGreg Roach										/* I18N: A step-family. %s is an individual’s name */
1007a25f0a04SGreg Roach										I18N::translate('Mother’s family with %s', $step_family->getSpouse($step_parent)->getFullName());
1008a25f0a04SGreg Roach								} else {
1009a25f0a04SGreg Roach									return
1010a25f0a04SGreg Roach										/* I18N: A step-family. */
1011a25f0a04SGreg Roach										I18N::translate('Mother’s family with an unknown individual');
1012a25f0a04SGreg Roach								}
1013a25f0a04SGreg Roach							}
1014a25f0a04SGreg Roach						}
1015a25f0a04SGreg Roach					}
1016a25f0a04SGreg Roach				}
1017a25f0a04SGreg Roach			}
1018a25f0a04SGreg Roach		}
1019a25f0a04SGreg Roach
1020a25f0a04SGreg Roach		// Perahps same parents - but a different family record?
1021a25f0a04SGreg Roach		return I18N::translate('Family with parents');
1022a25f0a04SGreg Roach	}
1023a25f0a04SGreg Roach
1024225e381fSGreg Roach
1025225e381fSGreg Roach	/**
1026225e381fSGreg Roach	 * Get the description for the family.
1027225e381fSGreg Roach	 *
1028225e381fSGreg Roach	 * For example, "XXX's family with new wife".
1029225e381fSGreg Roach	 *
1030225e381fSGreg Roach	 * @param Family     $family
1031225e381fSGreg Roach	 *
1032225e381fSGreg Roach	 * @return string
1033225e381fSGreg Roach	 */
1034225e381fSGreg Roach	public function getSpouseFamilyLabel(Family $family) {
1035225e381fSGreg Roach		$spouse = $family->getSpouse($this);
1036225e381fSGreg Roach		if ($spouse) {
1037225e381fSGreg Roach			return
1038225e381fSGreg Roach				/* I18N: %s is the spouse name */
1039225e381fSGreg Roach				I18N::translate('Family with %s', $spouse->getFullName());
1040225e381fSGreg Roach		} else {
1041225e381fSGreg Roach			return $family->getFullName();
1042225e381fSGreg Roach		}
1043225e381fSGreg Roach	}
1044225e381fSGreg Roach
1045a25f0a04SGreg Roach	/**
1046a25f0a04SGreg Roach	 * get primary parents names for this individual
1047a25f0a04SGreg Roach	 *
1048a25f0a04SGreg Roach	 * @param string $classname optional css class
1049a25f0a04SGreg Roach	 * @param string $display   optional css style display
1050a25f0a04SGreg Roach	 *
1051a25f0a04SGreg Roach	 * @return string a div block with father & mother names
1052a25f0a04SGreg Roach	 */
1053ffd703eaSGreg Roach	public function getPrimaryParentsNames($classname = '', $display = '') {
1054a25f0a04SGreg Roach		$fam = $this->getPrimaryChildFamily();
1055a25f0a04SGreg Roach		if (!$fam) {
1056a25f0a04SGreg Roach			return '';
1057a25f0a04SGreg Roach		}
1058a25f0a04SGreg Roach		$txt = '<div';
1059a25f0a04SGreg Roach		if ($classname) {
10604c621133SGreg Roach			$txt .= ' class="' . $classname . '"';
1061a25f0a04SGreg Roach		}
1062a25f0a04SGreg Roach		if ($display) {
10634c621133SGreg Roach			$txt .= ' style="display:' . $display . '"';
1064a25f0a04SGreg Roach		}
1065a25f0a04SGreg Roach		$txt .= '>';
1066a25f0a04SGreg Roach		$husb = $fam->getHusband();
1067a25f0a04SGreg Roach		if ($husb) {
1068a25f0a04SGreg Roach			// Temporarily reset the 'prefered' display name, as we always
1069a25f0a04SGreg Roach			// want the default name, not the one selected for display on the indilist.
1070a25f0a04SGreg Roach			$primary = $husb->getPrimaryName();
1071a25f0a04SGreg Roach			$husb->setPrimaryName(null);
1072a25f0a04SGreg Roach			$txt .=
1073a25f0a04SGreg Roach				/* I18N: %s is the name of an individual’s father */
1074a25f0a04SGreg Roach				I18N::translate('Father: %s', $husb->getFullName()) . '<br>';
1075a25f0a04SGreg Roach			$husb->setPrimaryName($primary);
1076a25f0a04SGreg Roach		}
1077a25f0a04SGreg Roach		$wife = $fam->getWife();
1078a25f0a04SGreg Roach		if ($wife) {
1079a25f0a04SGreg Roach			// Temporarily reset the 'prefered' display name, as we always
1080a25f0a04SGreg Roach			// want the default name, not the one selected for display on the indilist.
1081a25f0a04SGreg Roach			$primary = $wife->getPrimaryName();
1082a25f0a04SGreg Roach			$wife->setPrimaryName(null);
1083a25f0a04SGreg Roach			$txt .=
1084a25f0a04SGreg Roach				/* I18N: %s is the name of an individual’s mother */
1085a25f0a04SGreg Roach				I18N::translate('Mother: %s', $wife->getFullName());
1086a25f0a04SGreg Roach			$wife->setPrimaryName($primary);
1087a25f0a04SGreg Roach		}
1088a25f0a04SGreg Roach		$txt .= '</div>';
1089a25f0a04SGreg Roach
1090a25f0a04SGreg Roach		return $txt;
1091a25f0a04SGreg Roach	}
1092a25f0a04SGreg Roach
1093a25f0a04SGreg Roach	/** {@inheritdoc} */
1094ffd703eaSGreg Roach	public function getFallBackName() {
1095a25f0a04SGreg Roach		return '@P.N. /@N.N./';
1096a25f0a04SGreg Roach	}
1097a25f0a04SGreg Roach
1098a25f0a04SGreg Roach	/**
1099a25f0a04SGreg Roach	 * Convert a name record into ‘full’ and ‘sort’ versions.
1100a25f0a04SGreg Roach	 * Use the NAME field to generate the ‘full’ version, as the
1101a25f0a04SGreg Roach	 * gedcom spec says that this is the individual’s name, as they would write it.
1102a25f0a04SGreg Roach	 * Use the SURN field to generate the sortable names. Note that this field
1103a25f0a04SGreg Roach	 * may also be used for the ‘true’ surname, perhaps spelt differently to that
1104a25f0a04SGreg Roach	 * recorded in the NAME field. e.g.
1105a25f0a04SGreg Roach	 *
1106a25f0a04SGreg Roach	 * 1 NAME Robert /de Gliderow/
1107a25f0a04SGreg Roach	 * 2 GIVN Robert
1108a25f0a04SGreg Roach	 * 2 SPFX de
1109a25f0a04SGreg Roach	 * 2 SURN CLITHEROW
1110a25f0a04SGreg Roach	 * 2 NICK The Bald
1111a25f0a04SGreg Roach	 *
1112a25f0a04SGreg Roach	 * full=>'Robert de Gliderow 'The Bald''
1113a25f0a04SGreg Roach	 * sort=>'CLITHEROW, ROBERT'
1114a25f0a04SGreg Roach	 *
1115a25f0a04SGreg Roach	 * Handle multiple surnames, either as;
1116a25f0a04SGreg Roach	 *
1117a25f0a04SGreg Roach	 * 1 NAME Carlos /Vasquez/ y /Sante/
1118a25f0a04SGreg Roach	 * or
1119a25f0a04SGreg Roach	 * 1 NAME Carlos /Vasquez y Sante/
1120a25f0a04SGreg Roach	 * 2 GIVN Carlos
1121a25f0a04SGreg Roach	 * 2 SURN Vasquez,Sante
1122a25f0a04SGreg Roach	 *
1123a25f0a04SGreg Roach	 * @param string $type
1124a25f0a04SGreg Roach	 * @param string $full
1125a25f0a04SGreg Roach	 * @param string $gedcom
1126a25f0a04SGreg Roach	 */
1127a25f0a04SGreg Roach	protected function addName($type, $full, $gedcom) {
1128a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1129a25f0a04SGreg Roach		// Extract the structured name parts - use for "sortable" names and indexes
1130a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1131a25f0a04SGreg Roach
1132a25f0a04SGreg Roach		$sublevel = 1 + (int) $gedcom[0];
1133a25f0a04SGreg Roach		$NPFX     = preg_match("/\n{$sublevel} NPFX (.+)/", $gedcom, $match) ? $match[1] : '';
1134a25f0a04SGreg Roach		$GIVN     = preg_match("/\n{$sublevel} GIVN (.+)/", $gedcom, $match) ? $match[1] : '';
1135a25f0a04SGreg Roach		$SURN     = preg_match("/\n{$sublevel} SURN (.+)/", $gedcom, $match) ? $match[1] : '';
1136a25f0a04SGreg Roach		$NSFX     = preg_match("/\n{$sublevel} NSFX (.+)/", $gedcom, $match) ? $match[1] : '';
1137a25f0a04SGreg Roach		$NICK     = preg_match("/\n{$sublevel} NICK (.+)/", $gedcom, $match) ? $match[1] : '';
1138a25f0a04SGreg Roach
1139a25f0a04SGreg Roach		// SURN is an comma-separated list of surnames...
1140a25f0a04SGreg Roach		if ($SURN) {
1141a25f0a04SGreg Roach			$SURNS = preg_split('/ *, */', $SURN);
1142a25f0a04SGreg Roach		} else {
114313abd6f3SGreg Roach			$SURNS = [];
1144a25f0a04SGreg Roach		}
1145a25f0a04SGreg Roach		// ...so is GIVN - but nobody uses it like that
1146a25f0a04SGreg Roach		$GIVN = str_replace('/ *, */', ' ', $GIVN);
1147a25f0a04SGreg Roach
1148a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1149a25f0a04SGreg Roach		// Extract the components from NAME - use for the "full" names
1150a25f0a04SGreg Roach		////////////////////////////////////////////////////////////////////////////
1151a25f0a04SGreg Roach
1152a25f0a04SGreg Roach		// Fix bad slashes. e.g. 'John/Smith' => 'John/Smith/'
1153a25f0a04SGreg Roach		if (substr_count($full, '/') % 2 == 1) {
1154a25f0a04SGreg Roach			$full = $full . '/';
1155a25f0a04SGreg Roach		}
1156a25f0a04SGreg Roach
1157a25f0a04SGreg Roach		// GEDCOM uses "//" to indicate an unknown surname
1158a25f0a04SGreg Roach		$full = preg_replace('/\/\//', '/@N.N./', $full);
1159a25f0a04SGreg Roach
1160a25f0a04SGreg Roach		// Extract the surname.
1161a25f0a04SGreg Roach		// Note, there may be multiple surnames, e.g. Jean /Vasquez/ y /Cortes/
1162a25f0a04SGreg Roach		if (preg_match('/\/.*\//', $full, $match)) {
1163a25f0a04SGreg Roach			$surname = str_replace('/', '', $match[0]);
1164a25f0a04SGreg Roach		} else {
1165a25f0a04SGreg Roach			$surname = '';
1166a25f0a04SGreg Roach		}
1167a25f0a04SGreg Roach
1168a25f0a04SGreg Roach		// If we don’t have a SURN record, extract it from the NAME
1169a25f0a04SGreg Roach		if (!$SURNS) {
1170a25f0a04SGreg Roach			if (preg_match_all('/\/([^\/]*)\//', $full, $matches)) {
1171a25f0a04SGreg Roach				// There can be many surnames, each wrapped with '/'
1172a25f0a04SGreg Roach				$SURNS = $matches[1];
1173a25f0a04SGreg Roach				foreach ($SURNS as $n => $SURN) {
1174a25f0a04SGreg Roach					// Remove surname prefixes, such as "van de ", "d'" and "'t " (lower case only)
1175a25f0a04SGreg Roach					$SURNS[$n] = preg_replace('/^(?:[a-z]+ |[a-z]+\' ?|\'[a-z]+ )+/', '', $SURN);
1176a25f0a04SGreg Roach				}
1177a25f0a04SGreg Roach			} else {
1178a25f0a04SGreg Roach				// It is valid not to have a surname at all
117913abd6f3SGreg Roach				$SURNS = [''];
1180a25f0a04SGreg Roach			}
1181a25f0a04SGreg Roach		}
1182a25f0a04SGreg Roach
1183a25f0a04SGreg Roach		// If we don’t have a GIVN record, extract it from the NAME
1184a25f0a04SGreg Roach		if (!$GIVN) {
1185a25f0a04SGreg Roach			$GIVN = preg_replace(
118613abd6f3SGreg Roach				[
1187a25f0a04SGreg Roach					'/ ?\/.*\/ ?/', // remove surname
1188a25f0a04SGreg Roach					'/ ?".+"/', // remove nickname
1189a25f0a04SGreg Roach					'/ {2,}/', // multiple spaces, caused by the above
1190a25f0a04SGreg Roach					'/^ | $/', // leading/trailing spaces, caused by the above
119113abd6f3SGreg Roach				],
119213abd6f3SGreg Roach				[
1193a25f0a04SGreg Roach					' ',
1194a25f0a04SGreg Roach					' ',
1195a25f0a04SGreg Roach					' ',
1196a25f0a04SGreg Roach					'',
119713abd6f3SGreg Roach				],
1198a25f0a04SGreg Roach				$full
1199a25f0a04SGreg Roach			);
1200a25f0a04SGreg Roach		}
1201a25f0a04SGreg Roach
1202a25f0a04SGreg Roach		// Add placeholder for unknown given name
1203a25f0a04SGreg Roach		if (!$GIVN) {
1204a25f0a04SGreg Roach			$GIVN = '@P.N.';
1205a25f0a04SGreg Roach			$pos  = strpos($full, '/');
1206a25f0a04SGreg Roach			$full = substr($full, 0, $pos) . '@P.N. ' . substr($full, $pos);
1207a25f0a04SGreg Roach		}
1208a25f0a04SGreg Roach
12097b0e71c3SGreg Roach		// GEDCOM 5.5.1 nicknames should be specificied in a NICK field
12107b0e71c3SGreg Roach		// GEDCOM 5.5   nicknames should be specified in the NAME field, surrounded by quotes
1211c6f196c3SGreg Roach		if ($NICK && strpos($full, '"' . $NICK . '"') === false) {
12127b0e71c3SGreg Roach			// A NICK field is present, but not included in the NAME.  Show it at the end.
1213c6f196c3SGreg Roach			$full .= ' "' . $NICK . '"';
1214a25f0a04SGreg Roach		}
1215a25f0a04SGreg Roach
1216a25f0a04SGreg Roach		// Remove slashes - they don’t get displayed
1217a25f0a04SGreg Roach		// $fullNN keeps the @N.N. placeholders, for the database
1218a25f0a04SGreg Roach		// $full is for display on-screen
1219a25f0a04SGreg Roach		$fullNN = str_replace('/', '', $full);
1220a25f0a04SGreg Roach
1221a25f0a04SGreg Roach		// Insert placeholders for any missing/unknown names
1222ad1a1cd2SGreg Roach		$full = str_replace('@N.N.', I18N::translateContext('Unknown surname', '…'), $full);
1223ad1a1cd2SGreg Roach		$full = str_replace('@P.N.', I18N::translateContext('Unknown given name', '…'), $full);
1224c6f196c3SGreg Roach		// Format for display
1225d53324c9SGreg Roach		$full = '<span class="NAME" dir="auto" translate="no">' . preg_replace('/\/([^\/]*)\//', '<span class="SURN">$1</span>', e($full)) . '</span>';
1226acc34ea1SGreg Roach		// Localise quotation marks around the nickname
12278d68cabeSGreg Roach		$full = preg_replace_callback('/&quot;([^&]*)&quot;/', function ($matches) {
12288d68cabeSGreg Roach			return I18N::translate('“%s”', $matches[1]);
12298d68cabeSGreg Roach		}, $full);
1230a25f0a04SGreg Roach
1231c6f196c3SGreg Roach		// A suffix of “*” indicates a preferred name
1232a25f0a04SGreg Roach		$full = preg_replace('/([^ >]*)\*/', '<span class="starredname">\\1</span>', $full);
1233a25f0a04SGreg Roach
1234a25f0a04SGreg Roach		// Remove prefered-name indicater - they don’t go in the database
1235a25f0a04SGreg Roach		$GIVN   = str_replace('*', '', $GIVN);
1236a25f0a04SGreg Roach		$fullNN = str_replace('*', '', $fullNN);
1237a25f0a04SGreg Roach
1238ffd703eaSGreg Roach		foreach ($SURNS as $SURN) {
1239a25f0a04SGreg Roach			// Scottish 'Mc and Mac ' prefixes both sort under 'Mac'
1240a25f0a04SGreg Roach			if (strcasecmp(substr($SURN, 0, 2), 'Mc') == 0) {
1241a25f0a04SGreg Roach				$SURN = substr_replace($SURN, 'Mac', 0, 2);
1242a25f0a04SGreg Roach			} elseif (strcasecmp(substr($SURN, 0, 4), 'Mac ') == 0) {
1243a25f0a04SGreg Roach				$SURN = substr_replace($SURN, 'Mac', 0, 4);
1244a25f0a04SGreg Roach			}
1245a25f0a04SGreg Roach
124613abd6f3SGreg Roach			$this->_getAllNames[] = [
1247a25f0a04SGreg Roach				'type'    => $type,
1248a25f0a04SGreg Roach				'sort'    => $SURN . ',' . $GIVN,
1249a25f0a04SGreg Roach				'full'    => $full, // This is used for display
1250a25f0a04SGreg Roach				'fullNN'  => $fullNN, // This goes into the database
1251a25f0a04SGreg Roach				'surname' => $surname, // This goes into the database
1252a25f0a04SGreg Roach				'givn'    => $GIVN, // This goes into the database
1253a25f0a04SGreg Roach				'surn'    => $SURN, // This goes into the database
125413abd6f3SGreg Roach			];
1255a25f0a04SGreg Roach		}
1256a25f0a04SGreg Roach	}
1257a25f0a04SGreg Roach
1258a25f0a04SGreg Roach	/**
125976692c8bSGreg Roach	 * Extract names from the GEDCOM record.
1260a25f0a04SGreg Roach	 */
1261a25f0a04SGreg Roach	public function extractNames() {
12624b9ff166SGreg Roach		$this->extractNamesFromFacts(1, 'NAME', $this->getFacts('NAME', false, Auth::accessLevel($this->tree), $this->canShowName()));
1263a25f0a04SGreg Roach	}
1264a25f0a04SGreg Roach
1265a25f0a04SGreg Roach	/**
1266a25f0a04SGreg Roach	 * Extra info to display when displaying this record in a list of
1267a25f0a04SGreg Roach	 * selection items or favorites.
1268a25f0a04SGreg Roach	 *
1269a25f0a04SGreg Roach	 * @return string
1270a25f0a04SGreg Roach	 */
1271ffd703eaSGreg Roach	public function formatListDetails() {
1272a25f0a04SGreg Roach		return
1273841014f1SGreg Roach			$this->formatFirstMajorFact(WT_EVENTS_BIRT, 1) .
1274841014f1SGreg Roach			$this->formatFirstMajorFact(WT_EVENTS_DEAT, 1);
1275a25f0a04SGreg Roach	}
1276a25f0a04SGreg Roach
1277a25f0a04SGreg Roach	/**
1278a25f0a04SGreg Roach	 * Create a short name for compact display on charts
1279a25f0a04SGreg Roach	 *
1280a25f0a04SGreg Roach	 * @return string
1281a25f0a04SGreg Roach	 */
1282a25f0a04SGreg Roach	public function getShortName() {
1283ad1a1cd2SGreg Roach		global $bwidth;
1284a25f0a04SGreg Roach
1285a25f0a04SGreg Roach		// Estimate number of characters that can fit in box. Calulates to 28 characters in webtrees theme, or 34 if no thumbnail used.
1286518bbdc1SGreg Roach		if ($this->tree->getPreference('SHOW_HIGHLIGHT_IMAGES')) {
1287a25f0a04SGreg Roach			$char = intval(($bwidth - 40) / 6.5);
1288a25f0a04SGreg Roach		} else {
1289a25f0a04SGreg Roach			$char = ($bwidth / 6.5);
1290a25f0a04SGreg Roach		}
1291a25f0a04SGreg Roach		if ($this->canShowName()) {
1292a25f0a04SGreg Roach			$tmp        = $this->getAllNames();
1293a25f0a04SGreg Roach			$givn       = $tmp[$this->getPrimaryName()]['givn'];
1294a25f0a04SGreg Roach			$surn       = $tmp[$this->getPrimaryName()]['surname'];
1295a25f0a04SGreg Roach			$new_givn   = explode(' ', $givn);
1296a25f0a04SGreg Roach			$count_givn = count($new_givn);
1297a25f0a04SGreg Roach			$len_givn   = mb_strlen($givn);
1298a25f0a04SGreg Roach			$len_surn   = mb_strlen($surn);
1299a25f0a04SGreg Roach			$len        = $len_givn + $len_surn;
1300a25f0a04SGreg Roach			$i          = 1;
1301a25f0a04SGreg Roach			while ($len > $char && $i <= $count_givn) {
1302a25f0a04SGreg Roach				$new_givn[$count_givn - $i] = mb_substr($new_givn[$count_givn - $i], 0, 1);
1303a25f0a04SGreg Roach				$givn                       = implode(' ', $new_givn);
1304a25f0a04SGreg Roach				$len_givn                   = mb_strlen($givn);
1305a25f0a04SGreg Roach				$len                        = $len_givn + $len_surn;
1306a25f0a04SGreg Roach				$i++;
1307a25f0a04SGreg Roach			}
1308a25f0a04SGreg Roach			$max_surn = $char - $i * 2;
1309a25f0a04SGreg Roach			if ($len_surn > $max_surn) {
1310a25f0a04SGreg Roach				$surn = substr($surn, 0, $max_surn) . '…';
1311a25f0a04SGreg Roach			}
1312a25f0a04SGreg Roach			$shortname = str_replace(
131313abd6f3SGreg Roach				['@P.N.', '@N.N.'],
131413abd6f3SGreg Roach				[I18N::translateContext('Unknown given name', '…'), I18N::translateContext('Unknown surname', '…')],
1315a25f0a04SGreg Roach				$givn . ' ' . $surn
1316a25f0a04SGreg Roach			);
1317a25f0a04SGreg Roach
1318a25f0a04SGreg Roach			return $shortname;
1319a25f0a04SGreg Roach		} else {
1320a25f0a04SGreg Roach			return I18N::translate('Private');
1321a25f0a04SGreg Roach		}
1322a25f0a04SGreg Roach	}
1323a25f0a04SGreg Roach}
1324