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