xref: /webtrees/app/Family.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20886b77daSGreg Roachuse Closure;
212e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
22820b62dfSGreg Roachuse Illuminate\Support\Collection;
23886b77daSGreg Roachuse stdClass;
242e5b4452SGreg Roach
25a25f0a04SGreg Roach/**
2676692c8bSGreg Roach * A GEDCOM family (FAM) object.
27a25f0a04SGreg Roach */
28c1010edaSGreg Roachclass Family extends GedcomRecord
29c1010edaSGreg Roach{
3016d6367aSGreg Roach    public const RECORD_TYPE = 'FAM';
3116d6367aSGreg Roach
3216d6367aSGreg Roach    protected const ROUTE_NAME = 'family';
33a25f0a04SGreg Roach
34a25f0a04SGreg Roach    /** @var Individual|null The husband (or first spouse for same-sex couples) */
35a25f0a04SGreg Roach    private $husb;
36a25f0a04SGreg Roach
37a25f0a04SGreg Roach    /** @var Individual|null The wife (or second spouse for same-sex couples) */
38a25f0a04SGreg Roach    private $wife;
39a25f0a04SGreg Roach
4076692c8bSGreg Roach    /**
4176692c8bSGreg Roach     * Create a GedcomRecord object from raw GEDCOM data.
4276692c8bSGreg Roach     *
4376692c8bSGreg Roach     * @param string      $xref
4476692c8bSGreg Roach     * @param string      $gedcom  an empty string for new/pending records
4576692c8bSGreg Roach     * @param string|null $pending null for a record with no pending edits,
4676692c8bSGreg Roach     *                             empty string for records with pending deletions
4776692c8bSGreg Roach     * @param Tree        $tree
4876692c8bSGreg Roach     */
49*e364afe4SGreg Roach    public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree)
50c1010edaSGreg Roach    {
5124ec66ceSGreg Roach        parent::__construct($xref, $gedcom, $pending, $tree);
52a25f0a04SGreg Roach
53395f0fe0SGreg Roach        // Fetch family members
54395f0fe0SGreg Roach        if (preg_match_all('/^1 (?:HUSB|WIFE|CHIL) @(.+)@/m', $gedcom . $pending, $match)) {
55395f0fe0SGreg Roach            Individual::load($tree, $match[1]);
56395f0fe0SGreg Roach        }
57395f0fe0SGreg Roach
58a25f0a04SGreg Roach        if (preg_match('/^1 HUSB @(.+)@/m', $gedcom . $pending, $match)) {
5924ec66ceSGreg Roach            $this->husb = Individual::getInstance($match[1], $tree);
60a25f0a04SGreg Roach        }
61a25f0a04SGreg Roach        if (preg_match('/^1 WIFE @(.+)@/m', $gedcom . $pending, $match)) {
6224ec66ceSGreg Roach            $this->wife = Individual::getInstance($match[1], $tree);
63a25f0a04SGreg Roach        }
64a25f0a04SGreg Roach    }
65a25f0a04SGreg Roach
6676692c8bSGreg Roach    /**
67886b77daSGreg Roach     * A closure which will create a record from a database row.
68886b77daSGreg Roach     *
69886b77daSGreg Roach     * @return Closure
70886b77daSGreg Roach     */
71c0804649SGreg Roach    public static function rowMapper(): Closure
72886b77daSGreg Roach    {
73c0804649SGreg Roach        return function (stdClass $row): Family {
74a7a24840SGreg Roach            return Family::getInstance($row->f_id, Tree::findById((int) $row->f_file), $row->f_gedcom);
75886b77daSGreg Roach        };
76886b77daSGreg Roach    }
77886b77daSGreg Roach
78886b77daSGreg Roach    /**
79c156e8f5SGreg Roach     * A closure which will compare families by marriage date.
80c156e8f5SGreg Roach     *
81c156e8f5SGreg Roach     * @return Closure
82c156e8f5SGreg Roach     */
83c156e8f5SGreg Roach    public static function marriageDateComparator(): Closure
84c156e8f5SGreg Roach    {
85a9199cb2SGreg Roach        return function (Family $x, Family $y): int {
86c156e8f5SGreg Roach            return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
87c156e8f5SGreg Roach        };
88c156e8f5SGreg Roach    }
89c156e8f5SGreg Roach
90c156e8f5SGreg Roach    /**
91e71ef9d2SGreg Roach     * Get an instance of a family object. For single records,
92e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
93e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
94e71ef9d2SGreg Roach     *
95e71ef9d2SGreg Roach     * @param string      $xref
96e71ef9d2SGreg Roach     * @param Tree        $tree
97e71ef9d2SGreg Roach     * @param string|null $gedcom
98e71ef9d2SGreg Roach     *
99e71ef9d2SGreg Roach     * @throws \Exception
100e71ef9d2SGreg Roach     *
101e71ef9d2SGreg Roach     * @return Family|null
102e71ef9d2SGreg Roach     */
103*e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
104c1010edaSGreg Roach    {
105e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
106e71ef9d2SGreg Roach
107*e364afe4SGreg Roach        if ($record instanceof self) {
108e71ef9d2SGreg Roach            return $record;
109e71ef9d2SGreg Roach        }
110b2ce94c6SRico Sonntag
111b2ce94c6SRico Sonntag        return null;
112e71ef9d2SGreg Roach    }
113e71ef9d2SGreg Roach
114e71ef9d2SGreg Roach    /**
11576692c8bSGreg Roach     * Generate a private version of this record
11676692c8bSGreg Roach     *
11776692c8bSGreg Roach     * @param int $access_level
11876692c8bSGreg Roach     *
11976692c8bSGreg Roach     * @return string
12076692c8bSGreg Roach     */
1213c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
122c1010edaSGreg Roach    {
123d86cc606SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
124a25f0a04SGreg Roach
125a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ FAM';
126a25f0a04SGreg Roach        // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data
1278d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
128a25f0a04SGreg Roach        foreach ($matches as $match) {
12924ec66ceSGreg Roach            $rela = Individual::getInstance($match[1], $this->tree);
130a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
131a25f0a04SGreg Roach                $rec .= $match[0];
132a25f0a04SGreg Roach            }
133a25f0a04SGreg Roach        }
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach        return $rec;
136a25f0a04SGreg Roach    }
137a25f0a04SGreg Roach
13876692c8bSGreg Roach    /**
13976692c8bSGreg Roach     * Fetch data from the database
14076692c8bSGreg Roach     *
14176692c8bSGreg Roach     * @param string $xref
14276692c8bSGreg Roach     * @param int    $tree_id
14376692c8bSGreg Roach     *
144*e364afe4SGreg Roach     * @return string|null
14576692c8bSGreg Roach     */
146*e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
147c1010edaSGreg Roach    {
1482e5b4452SGreg Roach        return DB::table('families')
1492e5b4452SGreg Roach            ->where('f_id', '=', $xref)
1502e5b4452SGreg Roach            ->where('f_file', '=', $tree_id)
1512e5b4452SGreg Roach            ->value('f_gedcom');
152a25f0a04SGreg Roach    }
153a25f0a04SGreg Roach
154a25f0a04SGreg Roach    /**
155a25f0a04SGreg Roach     * Get the male (or first female) partner of the family
156a25f0a04SGreg Roach     *
157e93111adSRico Sonntag     * @param int|null $access_level
158b3f49e6cSGreg Roach     *
159a25f0a04SGreg Roach     * @return Individual|null
160a25f0a04SGreg Roach     */
16139ca88baSGreg Roach    public function husband($access_level = null): ?Individual
162c1010edaSGreg Roach    {
163b3f49e6cSGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
164b3f49e6cSGreg Roach
165b3f49e6cSGreg Roach        if ($this->husb && ($SHOW_PRIVATE_RELATIONSHIPS || $this->husb->canShowName($access_level))) {
166a25f0a04SGreg Roach            return $this->husb;
167a25f0a04SGreg Roach        }
168b2ce94c6SRico Sonntag
169b2ce94c6SRico Sonntag        return null;
170a25f0a04SGreg Roach    }
171a25f0a04SGreg Roach
172a25f0a04SGreg Roach    /**
173a25f0a04SGreg Roach     * Get the female (or second male) partner of the family
174a25f0a04SGreg Roach     *
175e93111adSRico Sonntag     * @param int|null $access_level
176b3f49e6cSGreg Roach     *
177a25f0a04SGreg Roach     * @return Individual|null
178a25f0a04SGreg Roach     */
17939ca88baSGreg Roach    public function wife($access_level = null): ?Individual
180c1010edaSGreg Roach    {
181b3f49e6cSGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
182b3f49e6cSGreg Roach
183b3f49e6cSGreg Roach        if ($this->wife && ($SHOW_PRIVATE_RELATIONSHIPS || $this->wife->canShowName($access_level))) {
184a25f0a04SGreg Roach            return $this->wife;
185a25f0a04SGreg Roach        }
186b2ce94c6SRico Sonntag
187b2ce94c6SRico Sonntag        return null;
188a25f0a04SGreg Roach    }
189a25f0a04SGreg Roach
19076692c8bSGreg Roach    /**
19176692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
19276692c8bSGreg Roach     *
19376692c8bSGreg Roach     * @param int $access_level
19476692c8bSGreg Roach     *
19576692c8bSGreg Roach     * @return bool
19676692c8bSGreg Roach     */
19735584196SGreg Roach    protected function canShowByType(int $access_level): bool
198c1010edaSGreg Roach    {
199a25f0a04SGreg Roach        // Hide a family if any member is private
2008d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches);
201a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
20224ec66ceSGreg Roach            $person = Individual::getInstance($match, $this->tree);
203a25f0a04SGreg Roach            if ($person && !$person->canShow($access_level)) {
204a25f0a04SGreg Roach                return false;
205a25f0a04SGreg Roach            }
206a25f0a04SGreg Roach        }
207a25f0a04SGreg Roach
208a25f0a04SGreg Roach        return true;
209a25f0a04SGreg Roach    }
210a25f0a04SGreg Roach
21176692c8bSGreg Roach    /**
21276692c8bSGreg Roach     * Can the name of this record be shown?
21376692c8bSGreg Roach     *
21476692c8bSGreg Roach     * @param int|null $access_level
21576692c8bSGreg Roach     *
21676692c8bSGreg Roach     * @return bool
21776692c8bSGreg Roach     */
21835584196SGreg Roach    public function canShowName(int $access_level = null): bool
219c1010edaSGreg Roach    {
220a25f0a04SGreg Roach        // We can always see the name (Husband-name + Wife-name), however,
221a25f0a04SGreg Roach        // the name will often be "private + private"
222a25f0a04SGreg Roach        return true;
223a25f0a04SGreg Roach    }
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach    /**
226a25f0a04SGreg Roach     * Find the spouse of a person.
227a25f0a04SGreg Roach     *
228a25f0a04SGreg Roach     * @param Individual $person
22913e3123bSGreg Roach     * @param int|null   $access_level
230a25f0a04SGreg Roach     *
231a25f0a04SGreg Roach     * @return Individual|null
232a25f0a04SGreg Roach     */
233*e364afe4SGreg Roach    public function spouse(Individual $person, $access_level = null): ?Individual
234c1010edaSGreg Roach    {
235a25f0a04SGreg Roach        if ($person === $this->wife) {
23639ca88baSGreg Roach            return $this->husband($access_level);
237a25f0a04SGreg Roach        }
238b2ce94c6SRico Sonntag
23939ca88baSGreg Roach        return $this->wife($access_level);
240a25f0a04SGreg Roach    }
241a25f0a04SGreg Roach
242a25f0a04SGreg Roach    /**
243a25f0a04SGreg Roach     * Get the (zero, one or two) spouses from this family.
244a25f0a04SGreg Roach     *
245cbc1590aSGreg Roach     * @param int|null $access_level
246a25f0a04SGreg Roach     *
24754c7f8dfSGreg Roach     * @return Collection
24854c7f8dfSGreg Roach     * @return Individual[]
249a25f0a04SGreg Roach     */
250820b62dfSGreg Roach    public function spouses($access_level = null): Collection
251c1010edaSGreg Roach    {
252820b62dfSGreg Roach        $spouses = new Collection([
25339ca88baSGreg Roach            $this->husband($access_level),
25439ca88baSGreg Roach            $this->wife($access_level),
25513abd6f3SGreg Roach        ]);
256820b62dfSGreg Roach
257820b62dfSGreg Roach        return $spouses->filter();
258a25f0a04SGreg Roach    }
259a25f0a04SGreg Roach
260a25f0a04SGreg Roach    /**
261a25f0a04SGreg Roach     * Get a list of this family’s children.
262a25f0a04SGreg Roach     *
263cbc1590aSGreg Roach     * @param int|null $access_level
264a25f0a04SGreg Roach     *
26554c7f8dfSGreg Roach     * @return Collection
26654c7f8dfSGreg Roach     * @return Individual[]
267a25f0a04SGreg Roach     */
268820b62dfSGreg Roach    public function children($access_level = null): Collection
269c1010edaSGreg Roach    {
2704b9ff166SGreg Roach        if ($access_level === null) {
2714b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
2724b9ff166SGreg Roach        }
2734b9ff166SGreg Roach
274845c3ce6SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
275a25f0a04SGreg Roach
276820b62dfSGreg Roach        $children = new Collection();
277820b62dfSGreg Roach
2788d0ebef0SGreg Roach        foreach ($this->facts(['CHIL'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
279dc124885SGreg Roach            $child = $fact->target();
280820b62dfSGreg Roach
281e24444eeSGreg Roach            if ($child instanceof Individual && ($SHOW_PRIVATE_RELATIONSHIPS || $child->canShowName($access_level))) {
282820b62dfSGreg Roach                $children->push($child);
283a25f0a04SGreg Roach            }
284a25f0a04SGreg Roach        }
285a25f0a04SGreg Roach
286a25f0a04SGreg Roach        return $children;
287a25f0a04SGreg Roach    }
288a25f0a04SGreg Roach
289a25f0a04SGreg Roach    /**
290a25f0a04SGreg Roach     * Number of children - for the individual list
291a25f0a04SGreg Roach     *
292cbc1590aSGreg Roach     * @return int
293a25f0a04SGreg Roach     */
29439ca88baSGreg Roach    public function numberOfChildren(): int
295c1010edaSGreg Roach    {
296820b62dfSGreg Roach        $nchi = $this->children()->count();
297820b62dfSGreg Roach
2988d0ebef0SGreg Roach        foreach ($this->facts(['NCHI']) as $fact) {
29984586c02SGreg Roach            $nchi = max($nchi, (int) $fact->value());
300a25f0a04SGreg Roach        }
301a25f0a04SGreg Roach
302a25f0a04SGreg Roach        return $nchi;
303a25f0a04SGreg Roach    }
304a25f0a04SGreg Roach
305a25f0a04SGreg Roach    /**
306a25f0a04SGreg Roach     * get the marriage event
307a25f0a04SGreg Roach     *
30866107289SGreg Roach     * @return Fact|null
309a25f0a04SGreg Roach     */
310820b62dfSGreg Roach    public function getMarriage(): ?Fact
311c1010edaSGreg Roach    {
312820b62dfSGreg Roach        return $this->facts(['MARR'])->first();
313a25f0a04SGreg Roach    }
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach    /**
316a25f0a04SGreg Roach     * Get marriage date
317a25f0a04SGreg Roach     *
318a25f0a04SGreg Roach     * @return Date
319a25f0a04SGreg Roach     */
320820b62dfSGreg Roach    public function getMarriageDate(): Date
321c1010edaSGreg Roach    {
322a25f0a04SGreg Roach        $marriage = $this->getMarriage();
323a25f0a04SGreg Roach        if ($marriage) {
3242decada7SGreg Roach            return $marriage->date();
325a25f0a04SGreg Roach        }
326b2ce94c6SRico Sonntag
327b2ce94c6SRico Sonntag        return new Date('');
328a25f0a04SGreg Roach    }
329a25f0a04SGreg Roach
330a25f0a04SGreg Roach    /**
331a25f0a04SGreg Roach     * Get the marriage year - displayed on lists of families
332a25f0a04SGreg Roach     *
333cbc1590aSGreg Roach     * @return int
334a25f0a04SGreg Roach     */
3358f53f488SRico Sonntag    public function getMarriageYear(): int
336c1010edaSGreg Roach    {
3374a83f5d7SGreg Roach        return $this->getMarriageDate()->minimumDate()->year;
338a25f0a04SGreg Roach    }
339a25f0a04SGreg Roach
340a25f0a04SGreg Roach    /**
341a25f0a04SGreg Roach     * Get the marriage place
342a25f0a04SGreg Roach     *
343a25f0a04SGreg Roach     * @return Place
344a25f0a04SGreg Roach     */
3458f53f488SRico Sonntag    public function getMarriagePlace(): Place
346c1010edaSGreg Roach    {
347a25f0a04SGreg Roach        $marriage = $this->getMarriage();
348a25f0a04SGreg Roach
3494fb14fcbSGreg Roach        return $marriage->place();
350a25f0a04SGreg Roach    }
351a25f0a04SGreg Roach
352a25f0a04SGreg Roach    /**
353a25f0a04SGreg Roach     * Get a list of all marriage dates - for the family lists.
354a25f0a04SGreg Roach     *
355a25f0a04SGreg Roach     * @return Date[]
356a25f0a04SGreg Roach     */
3578f53f488SRico Sonntag    public function getAllMarriageDates(): array
358c1010edaSGreg Roach    {
3598d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3608d0ebef0SGreg Roach            if ($array = $this->getAllEventDates([$event])) {
361a25f0a04SGreg Roach                return $array;
362a25f0a04SGreg Roach            }
363a25f0a04SGreg Roach        }
364a25f0a04SGreg Roach
36513abd6f3SGreg Roach        return [];
366a25f0a04SGreg Roach    }
367a25f0a04SGreg Roach
368a25f0a04SGreg Roach    /**
369a25f0a04SGreg Roach     * Get a list of all marriage places - for the family lists.
370a25f0a04SGreg Roach     *
3714080d558SGreg Roach     * @return Place[]
372a25f0a04SGreg Roach     */
3738f53f488SRico Sonntag    public function getAllMarriagePlaces(): array
374c1010edaSGreg Roach    {
3758d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3766e83554dSGreg Roach            $places = $this->getAllEventPlaces([$event]);
3774080d558SGreg Roach            if (!empty($places)) {
3784080d558SGreg Roach                return $places;
379a25f0a04SGreg Roach            }
380a25f0a04SGreg Roach        }
381a25f0a04SGreg Roach
38213abd6f3SGreg Roach        return [];
383a25f0a04SGreg Roach    }
384a25f0a04SGreg Roach
38576692c8bSGreg Roach    /**
38676692c8bSGreg Roach     * Derived classes should redefine this function, otherwise the object will have no name
38776692c8bSGreg Roach     *
38876692c8bSGreg Roach     * @return string[][]
38976692c8bSGreg Roach     */
3908f53f488SRico Sonntag    public function getAllNames(): array
391c1010edaSGreg Roach    {
3928f038c36SRico Sonntag        if ($this->getAllNames === null) {
393a25f0a04SGreg Roach            // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
394e88674d4SGreg Roach            $husb_names = [];
395a25f0a04SGreg Roach            if ($this->husb) {
396492c7072SGreg Roach                $husb_names = array_filter($this->husb->getAllNames(), function (array $x): bool {
3978d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
3988d68cabeSGreg Roach                });
399e88674d4SGreg Roach            }
400e88674d4SGreg Roach            // If the individual only has married names, create a dummy birth name.
401e88674d4SGreg Roach            if (empty($husb_names)) {
402e88674d4SGreg Roach                $husb_names[] = [
403a25f0a04SGreg Roach                    'type' => 'BIRT',
404a25f0a04SGreg Roach                    'sort' => '@N.N.',
405ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
40613abd6f3SGreg Roach                ];
407a25f0a04SGreg Roach            }
408a25f0a04SGreg Roach            foreach ($husb_names as $n => $husb_name) {
409a25f0a04SGreg Roach                $husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
410a25f0a04SGreg Roach            }
411e88674d4SGreg Roach
412e88674d4SGreg Roach            $wife_names = [];
413a25f0a04SGreg Roach            if ($this->wife) {
414492c7072SGreg Roach                $wife_names = array_filter($this->wife->getAllNames(), function (array $x): bool {
4158d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
4168d68cabeSGreg Roach                });
417e88674d4SGreg Roach            }
418e88674d4SGreg Roach            // If the individual only has married names, create a dummy birth name.
419e88674d4SGreg Roach            if (empty($wife_names)) {
420e88674d4SGreg Roach                $wife_names[] = [
421a25f0a04SGreg Roach                    'type' => 'BIRT',
422a25f0a04SGreg Roach                    'sort' => '@N.N.',
423ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
42413abd6f3SGreg Roach                ];
425a25f0a04SGreg Roach            }
426a25f0a04SGreg Roach            foreach ($wife_names as $n => $wife_name) {
427a25f0a04SGreg Roach                $wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
428a25f0a04SGreg Roach            }
429e88674d4SGreg Roach
430a25f0a04SGreg Roach            // Add the matched names first
431a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
432a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
433*e364afe4SGreg Roach                    if ($husb_name['script'] === $wife_name['script']) {
434bdb3725aSGreg Roach                        $this->getAllNames[] = [
435a25f0a04SGreg Roach                            'type' => $husb_name['type'],
436a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
437a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
438a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
43913abd6f3SGreg Roach                        ];
440a25f0a04SGreg Roach                    }
441a25f0a04SGreg Roach                }
442a25f0a04SGreg Roach            }
443e88674d4SGreg Roach
444a25f0a04SGreg Roach            // Add the unmatched names second (there may be no matched names)
445a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
446a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
447*e364afe4SGreg Roach                    if ($husb_name['script'] !== $wife_name['script']) {
448bdb3725aSGreg Roach                        $this->getAllNames[] = [
449a25f0a04SGreg Roach                            'type' => $husb_name['type'],
450a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
451a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
452a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
45313abd6f3SGreg Roach                        ];
454a25f0a04SGreg Roach                    }
455a25f0a04SGreg Roach                }
456a25f0a04SGreg Roach            }
457a25f0a04SGreg Roach        }
458a25f0a04SGreg Roach
459bdb3725aSGreg Roach        return $this->getAllNames;
460a25f0a04SGreg Roach    }
461a25f0a04SGreg Roach
46276692c8bSGreg Roach    /**
46376692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
46476692c8bSGreg Roach     * identifying characteristics of this record.
46576692c8bSGreg Roach     *
46676692c8bSGreg Roach     * @return string
46776692c8bSGreg Roach     */
4688f53f488SRico Sonntag    public function formatListDetails(): string
469c1010edaSGreg Roach    {
470a25f0a04SGreg Roach        return
4718d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) .
4728d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1);
473a25f0a04SGreg Roach    }
474a25f0a04SGreg Roach}
475