xref: /webtrees/app/Family.php (revision 6c2179e2012ee7bb08bc76647556f957e4eeaa05)
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;
216ccdf4f0SGreg Roachuse Exception;
222e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
23820b62dfSGreg Roachuse Illuminate\Support\Collection;
24886b77daSGreg Roachuse stdClass;
252e5b4452SGreg Roach
26a25f0a04SGreg Roach/**
2776692c8bSGreg Roach * A GEDCOM family (FAM) object.
28a25f0a04SGreg Roach */
29c1010edaSGreg Roachclass Family extends GedcomRecord
30c1010edaSGreg Roach{
3116d6367aSGreg Roach    public const RECORD_TYPE = 'FAM';
3216d6367aSGreg Roach
3316d6367aSGreg Roach    protected const ROUTE_NAME = 'family';
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach    /** @var Individual|null The husband (or first spouse for same-sex couples) */
36a25f0a04SGreg Roach    private $husb;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /** @var Individual|null The wife (or second spouse for same-sex couples) */
39a25f0a04SGreg Roach    private $wife;
40a25f0a04SGreg Roach
4176692c8bSGreg Roach    /**
4276692c8bSGreg Roach     * Create a GedcomRecord object from raw GEDCOM data.
4376692c8bSGreg Roach     *
4476692c8bSGreg Roach     * @param string      $xref
4576692c8bSGreg Roach     * @param string      $gedcom  an empty string for new/pending records
4676692c8bSGreg Roach     * @param string|null $pending null for a record with no pending edits,
4776692c8bSGreg Roach     *                             empty string for records with pending deletions
4876692c8bSGreg Roach     * @param Tree        $tree
4976692c8bSGreg Roach     */
50e364afe4SGreg Roach    public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree)
51c1010edaSGreg Roach    {
5224ec66ceSGreg Roach        parent::__construct($xref, $gedcom, $pending, $tree);
53a25f0a04SGreg Roach
54395f0fe0SGreg Roach        // Fetch family members
55395f0fe0SGreg Roach        if (preg_match_all('/^1 (?:HUSB|WIFE|CHIL) @(.+)@/m', $gedcom . $pending, $match)) {
56395f0fe0SGreg Roach            Individual::load($tree, $match[1]);
57395f0fe0SGreg Roach        }
58395f0fe0SGreg Roach
59a25f0a04SGreg Roach        if (preg_match('/^1 HUSB @(.+)@/m', $gedcom . $pending, $match)) {
6024ec66ceSGreg Roach            $this->husb = Individual::getInstance($match[1], $tree);
61a25f0a04SGreg Roach        }
62a25f0a04SGreg Roach        if (preg_match('/^1 WIFE @(.+)@/m', $gedcom . $pending, $match)) {
6324ec66ceSGreg Roach            $this->wife = Individual::getInstance($match[1], $tree);
64a25f0a04SGreg Roach        }
65a25f0a04SGreg Roach    }
66a25f0a04SGreg Roach
6776692c8bSGreg Roach    /**
68886b77daSGreg Roach     * A closure which will create a record from a database row.
69886b77daSGreg Roach     *
70886b77daSGreg Roach     * @return Closure
71886b77daSGreg Roach     */
72c0804649SGreg Roach    public static function rowMapper(): Closure
73886b77daSGreg Roach    {
74*6c2179e2SGreg Roach        return static function (stdClass $row): Family {
75a7a24840SGreg Roach            return Family::getInstance($row->f_id, Tree::findById((int) $row->f_file), $row->f_gedcom);
76886b77daSGreg Roach        };
77886b77daSGreg Roach    }
78886b77daSGreg Roach
79886b77daSGreg Roach    /**
80c156e8f5SGreg Roach     * A closure which will compare families by marriage date.
81c156e8f5SGreg Roach     *
82c156e8f5SGreg Roach     * @return Closure
83c156e8f5SGreg Roach     */
84c156e8f5SGreg Roach    public static function marriageDateComparator(): Closure
85c156e8f5SGreg Roach    {
86*6c2179e2SGreg Roach        return static function (Family $x, Family $y): int {
87c156e8f5SGreg Roach            return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
88c156e8f5SGreg Roach        };
89c156e8f5SGreg Roach    }
90c156e8f5SGreg Roach
91c156e8f5SGreg Roach    /**
92e71ef9d2SGreg Roach     * Get an instance of a family object. For single records,
93e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
94e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
95e71ef9d2SGreg Roach     *
96e71ef9d2SGreg Roach     * @param string      $xref
97e71ef9d2SGreg Roach     * @param Tree        $tree
98e71ef9d2SGreg Roach     * @param string|null $gedcom
99e71ef9d2SGreg Roach     *
1006ccdf4f0SGreg Roach     * @throws Exception
101e71ef9d2SGreg Roach     *
102e71ef9d2SGreg Roach     * @return Family|null
103e71ef9d2SGreg Roach     */
104e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
105c1010edaSGreg Roach    {
106e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
107e71ef9d2SGreg Roach
108e364afe4SGreg Roach        if ($record instanceof self) {
109e71ef9d2SGreg Roach            return $record;
110e71ef9d2SGreg Roach        }
111b2ce94c6SRico Sonntag
112b2ce94c6SRico Sonntag        return null;
113e71ef9d2SGreg Roach    }
114e71ef9d2SGreg Roach
115e71ef9d2SGreg Roach    /**
11676692c8bSGreg Roach     * Generate a private version of this record
11776692c8bSGreg Roach     *
11876692c8bSGreg Roach     * @param int $access_level
11976692c8bSGreg Roach     *
12076692c8bSGreg Roach     * @return string
12176692c8bSGreg Roach     */
1223c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
123c1010edaSGreg Roach    {
124d86cc606SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
125a25f0a04SGreg Roach
126a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ FAM';
127a25f0a04SGreg Roach        // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data
1288d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
129a25f0a04SGreg Roach        foreach ($matches as $match) {
13024ec66ceSGreg Roach            $rela = Individual::getInstance($match[1], $this->tree);
131a25f0a04SGreg Roach            if ($rela && ($SHOW_PRIVATE_RELATIONSHIPS || $rela->canShow($access_level))) {
132a25f0a04SGreg Roach                $rec .= $match[0];
133a25f0a04SGreg Roach            }
134a25f0a04SGreg Roach        }
135a25f0a04SGreg Roach
136a25f0a04SGreg Roach        return $rec;
137a25f0a04SGreg Roach    }
138a25f0a04SGreg Roach
13976692c8bSGreg Roach    /**
14076692c8bSGreg Roach     * Fetch data from the database
14176692c8bSGreg Roach     *
14276692c8bSGreg Roach     * @param string $xref
14376692c8bSGreg Roach     * @param int    $tree_id
14476692c8bSGreg Roach     *
145e364afe4SGreg Roach     * @return string|null
14676692c8bSGreg Roach     */
147e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
148c1010edaSGreg Roach    {
1492e5b4452SGreg Roach        return DB::table('families')
1502e5b4452SGreg Roach            ->where('f_id', '=', $xref)
1512e5b4452SGreg Roach            ->where('f_file', '=', $tree_id)
1522e5b4452SGreg Roach            ->value('f_gedcom');
153a25f0a04SGreg Roach    }
154a25f0a04SGreg Roach
155a25f0a04SGreg Roach    /**
156a25f0a04SGreg Roach     * Get the male (or first female) partner of the family
157a25f0a04SGreg Roach     *
158e93111adSRico Sonntag     * @param int|null $access_level
159b3f49e6cSGreg Roach     *
160a25f0a04SGreg Roach     * @return Individual|null
161a25f0a04SGreg Roach     */
16239ca88baSGreg Roach    public function husband($access_level = null): ?Individual
163c1010edaSGreg Roach    {
164b3f49e6cSGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
165b3f49e6cSGreg Roach
166b3f49e6cSGreg Roach        if ($this->husb && ($SHOW_PRIVATE_RELATIONSHIPS || $this->husb->canShowName($access_level))) {
167a25f0a04SGreg Roach            return $this->husb;
168a25f0a04SGreg Roach        }
169b2ce94c6SRico Sonntag
170b2ce94c6SRico Sonntag        return null;
171a25f0a04SGreg Roach    }
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach    /**
174a25f0a04SGreg Roach     * Get the female (or second male) partner of the family
175a25f0a04SGreg Roach     *
176e93111adSRico Sonntag     * @param int|null $access_level
177b3f49e6cSGreg Roach     *
178a25f0a04SGreg Roach     * @return Individual|null
179a25f0a04SGreg Roach     */
18039ca88baSGreg Roach    public function wife($access_level = null): ?Individual
181c1010edaSGreg Roach    {
182b3f49e6cSGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
183b3f49e6cSGreg Roach
184b3f49e6cSGreg Roach        if ($this->wife && ($SHOW_PRIVATE_RELATIONSHIPS || $this->wife->canShowName($access_level))) {
185a25f0a04SGreg Roach            return $this->wife;
186a25f0a04SGreg Roach        }
187b2ce94c6SRico Sonntag
188b2ce94c6SRico Sonntag        return null;
189a25f0a04SGreg Roach    }
190a25f0a04SGreg Roach
19176692c8bSGreg Roach    /**
19276692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
19376692c8bSGreg Roach     *
19476692c8bSGreg Roach     * @param int $access_level
19576692c8bSGreg Roach     *
19676692c8bSGreg Roach     * @return bool
19776692c8bSGreg Roach     */
19835584196SGreg Roach    protected function canShowByType(int $access_level): bool
199c1010edaSGreg Roach    {
200a25f0a04SGreg Roach        // Hide a family if any member is private
2018d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches);
202a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
20324ec66ceSGreg Roach            $person = Individual::getInstance($match, $this->tree);
204a25f0a04SGreg Roach            if ($person && !$person->canShow($access_level)) {
205a25f0a04SGreg Roach                return false;
206a25f0a04SGreg Roach            }
207a25f0a04SGreg Roach        }
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach        return true;
210a25f0a04SGreg Roach    }
211a25f0a04SGreg Roach
21276692c8bSGreg Roach    /**
21376692c8bSGreg Roach     * Can the name of this record be shown?
21476692c8bSGreg Roach     *
21576692c8bSGreg Roach     * @param int|null $access_level
21676692c8bSGreg Roach     *
21776692c8bSGreg Roach     * @return bool
21876692c8bSGreg Roach     */
21935584196SGreg Roach    public function canShowName(int $access_level = null): bool
220c1010edaSGreg Roach    {
221a25f0a04SGreg Roach        // We can always see the name (Husband-name + Wife-name), however,
222a25f0a04SGreg Roach        // the name will often be "private + private"
223a25f0a04SGreg Roach        return true;
224a25f0a04SGreg Roach    }
225a25f0a04SGreg Roach
226a25f0a04SGreg Roach    /**
227a25f0a04SGreg Roach     * Find the spouse of a person.
228a25f0a04SGreg Roach     *
229a25f0a04SGreg Roach     * @param Individual $person
23013e3123bSGreg Roach     * @param int|null   $access_level
231a25f0a04SGreg Roach     *
232a25f0a04SGreg Roach     * @return Individual|null
233a25f0a04SGreg Roach     */
234e364afe4SGreg Roach    public function spouse(Individual $person, $access_level = null): ?Individual
235c1010edaSGreg Roach    {
236a25f0a04SGreg Roach        if ($person === $this->wife) {
23739ca88baSGreg Roach            return $this->husband($access_level);
238a25f0a04SGreg Roach        }
239b2ce94c6SRico Sonntag
24039ca88baSGreg Roach        return $this->wife($access_level);
241a25f0a04SGreg Roach    }
242a25f0a04SGreg Roach
243a25f0a04SGreg Roach    /**
244a25f0a04SGreg Roach     * Get the (zero, one or two) spouses from this family.
245a25f0a04SGreg Roach     *
246cbc1590aSGreg Roach     * @param int|null $access_level
247a25f0a04SGreg Roach     *
24854c7f8dfSGreg Roach     * @return Collection
24954c7f8dfSGreg Roach     * @return Individual[]
250a25f0a04SGreg Roach     */
251820b62dfSGreg Roach    public function spouses($access_level = null): Collection
252c1010edaSGreg Roach    {
253820b62dfSGreg Roach        $spouses = new Collection([
25439ca88baSGreg Roach            $this->husband($access_level),
25539ca88baSGreg Roach            $this->wife($access_level),
25613abd6f3SGreg Roach        ]);
257820b62dfSGreg Roach
258820b62dfSGreg Roach        return $spouses->filter();
259a25f0a04SGreg Roach    }
260a25f0a04SGreg Roach
261a25f0a04SGreg Roach    /**
262a25f0a04SGreg Roach     * Get a list of this family’s children.
263a25f0a04SGreg Roach     *
264cbc1590aSGreg Roach     * @param int|null $access_level
265a25f0a04SGreg Roach     *
26654c7f8dfSGreg Roach     * @return Collection
26754c7f8dfSGreg Roach     * @return Individual[]
268a25f0a04SGreg Roach     */
269820b62dfSGreg Roach    public function children($access_level = null): Collection
270c1010edaSGreg Roach    {
2714b9ff166SGreg Roach        if ($access_level === null) {
2724b9ff166SGreg Roach            $access_level = Auth::accessLevel($this->tree);
2734b9ff166SGreg Roach        }
2744b9ff166SGreg Roach
275845c3ce6SGreg Roach        $SHOW_PRIVATE_RELATIONSHIPS = (bool) $this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS');
276a25f0a04SGreg Roach
277820b62dfSGreg Roach        $children = new Collection();
278820b62dfSGreg Roach
2798d0ebef0SGreg Roach        foreach ($this->facts(['CHIL'], false, $access_level, $SHOW_PRIVATE_RELATIONSHIPS) as $fact) {
280dc124885SGreg Roach            $child = $fact->target();
281820b62dfSGreg Roach
282e24444eeSGreg Roach            if ($child instanceof Individual && ($SHOW_PRIVATE_RELATIONSHIPS || $child->canShowName($access_level))) {
283820b62dfSGreg Roach                $children->push($child);
284a25f0a04SGreg Roach            }
285a25f0a04SGreg Roach        }
286a25f0a04SGreg Roach
287a25f0a04SGreg Roach        return $children;
288a25f0a04SGreg Roach    }
289a25f0a04SGreg Roach
290a25f0a04SGreg Roach    /**
291a25f0a04SGreg Roach     * Number of children - for the individual list
292a25f0a04SGreg Roach     *
293cbc1590aSGreg Roach     * @return int
294a25f0a04SGreg Roach     */
29539ca88baSGreg Roach    public function numberOfChildren(): int
296c1010edaSGreg Roach    {
297820b62dfSGreg Roach        $nchi = $this->children()->count();
298820b62dfSGreg Roach
2998d0ebef0SGreg Roach        foreach ($this->facts(['NCHI']) as $fact) {
30084586c02SGreg Roach            $nchi = max($nchi, (int) $fact->value());
301a25f0a04SGreg Roach        }
302a25f0a04SGreg Roach
303a25f0a04SGreg Roach        return $nchi;
304a25f0a04SGreg Roach    }
305a25f0a04SGreg Roach
306a25f0a04SGreg Roach    /**
307a25f0a04SGreg Roach     * get the marriage event
308a25f0a04SGreg Roach     *
30966107289SGreg Roach     * @return Fact|null
310a25f0a04SGreg Roach     */
311820b62dfSGreg Roach    public function getMarriage(): ?Fact
312c1010edaSGreg Roach    {
313820b62dfSGreg Roach        return $this->facts(['MARR'])->first();
314a25f0a04SGreg Roach    }
315a25f0a04SGreg Roach
316a25f0a04SGreg Roach    /**
317a25f0a04SGreg Roach     * Get marriage date
318a25f0a04SGreg Roach     *
319a25f0a04SGreg Roach     * @return Date
320a25f0a04SGreg Roach     */
321820b62dfSGreg Roach    public function getMarriageDate(): Date
322c1010edaSGreg Roach    {
323a25f0a04SGreg Roach        $marriage = $this->getMarriage();
324a25f0a04SGreg Roach        if ($marriage) {
3252decada7SGreg Roach            return $marriage->date();
326a25f0a04SGreg Roach        }
327b2ce94c6SRico Sonntag
328b2ce94c6SRico Sonntag        return new Date('');
329a25f0a04SGreg Roach    }
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach    /**
332a25f0a04SGreg Roach     * Get the marriage year - displayed on lists of families
333a25f0a04SGreg Roach     *
334cbc1590aSGreg Roach     * @return int
335a25f0a04SGreg Roach     */
3368f53f488SRico Sonntag    public function getMarriageYear(): int
337c1010edaSGreg Roach    {
3384a83f5d7SGreg Roach        return $this->getMarriageDate()->minimumDate()->year;
339a25f0a04SGreg Roach    }
340a25f0a04SGreg Roach
341a25f0a04SGreg Roach    /**
342a25f0a04SGreg Roach     * Get the marriage place
343a25f0a04SGreg Roach     *
344a25f0a04SGreg Roach     * @return Place
345a25f0a04SGreg Roach     */
3468f53f488SRico Sonntag    public function getMarriagePlace(): Place
347c1010edaSGreg Roach    {
348a25f0a04SGreg Roach        $marriage = $this->getMarriage();
349a25f0a04SGreg Roach
3507abafad0SGreg Roach        if ($marriage instanceof Fact) {
3514fb14fcbSGreg Roach            return $marriage->place();
352a25f0a04SGreg Roach        }
353a25f0a04SGreg Roach
3547abafad0SGreg Roach        return new Place('', $this->tree);
3557abafad0SGreg Roach    }
3567abafad0SGreg Roach
357a25f0a04SGreg Roach    /**
358a25f0a04SGreg Roach     * Get a list of all marriage dates - for the family lists.
359a25f0a04SGreg Roach     *
360a25f0a04SGreg Roach     * @return Date[]
361a25f0a04SGreg Roach     */
3628f53f488SRico Sonntag    public function getAllMarriageDates(): array
363c1010edaSGreg Roach    {
3648d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3657abafad0SGreg Roach            $array = $this->getAllEventDates([$event]);
3668af3e5c1SGreg Roach
3678af3e5c1SGreg Roach            if (!empty($array)) {
368a25f0a04SGreg Roach                return $array;
369a25f0a04SGreg Roach            }
370a25f0a04SGreg Roach        }
371a25f0a04SGreg Roach
37213abd6f3SGreg Roach        return [];
373a25f0a04SGreg Roach    }
374a25f0a04SGreg Roach
375a25f0a04SGreg Roach    /**
376a25f0a04SGreg Roach     * Get a list of all marriage places - for the family lists.
377a25f0a04SGreg Roach     *
3784080d558SGreg Roach     * @return Place[]
379a25f0a04SGreg Roach     */
3808f53f488SRico Sonntag    public function getAllMarriagePlaces(): array
381c1010edaSGreg Roach    {
3828d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3836e83554dSGreg Roach            $places = $this->getAllEventPlaces([$event]);
3844080d558SGreg Roach            if (!empty($places)) {
3854080d558SGreg Roach                return $places;
386a25f0a04SGreg Roach            }
387a25f0a04SGreg Roach        }
388a25f0a04SGreg Roach
38913abd6f3SGreg Roach        return [];
390a25f0a04SGreg Roach    }
391a25f0a04SGreg Roach
39276692c8bSGreg Roach    /**
39376692c8bSGreg Roach     * Derived classes should redefine this function, otherwise the object will have no name
39476692c8bSGreg Roach     *
39576692c8bSGreg Roach     * @return string[][]
39676692c8bSGreg Roach     */
3978f53f488SRico Sonntag    public function getAllNames(): array
398c1010edaSGreg Roach    {
3998f038c36SRico Sonntag        if ($this->getAllNames === null) {
400a25f0a04SGreg Roach            // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
401e88674d4SGreg Roach            $husb_names = [];
402a25f0a04SGreg Roach            if ($this->husb) {
4030b5fd0a6SGreg Roach                $husb_names = array_filter($this->husb->getAllNames(), static function (array $x): bool {
4048d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
4058d68cabeSGreg Roach                });
406e88674d4SGreg Roach            }
407e88674d4SGreg Roach            // If the individual only has married names, create a dummy birth name.
408e88674d4SGreg Roach            if (empty($husb_names)) {
409e88674d4SGreg Roach                $husb_names[] = [
410a25f0a04SGreg Roach                    'type' => 'BIRT',
411a25f0a04SGreg Roach                    'sort' => '@N.N.',
412ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
41313abd6f3SGreg Roach                ];
414a25f0a04SGreg Roach            }
415a25f0a04SGreg Roach            foreach ($husb_names as $n => $husb_name) {
416a25f0a04SGreg Roach                $husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
417a25f0a04SGreg Roach            }
418e88674d4SGreg Roach
419e88674d4SGreg Roach            $wife_names = [];
420a25f0a04SGreg Roach            if ($this->wife) {
4210b5fd0a6SGreg Roach                $wife_names = array_filter($this->wife->getAllNames(), static function (array $x): bool {
4228d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
4238d68cabeSGreg Roach                });
424e88674d4SGreg Roach            }
425e88674d4SGreg Roach            // If the individual only has married names, create a dummy birth name.
426e88674d4SGreg Roach            if (empty($wife_names)) {
427e88674d4SGreg Roach                $wife_names[] = [
428a25f0a04SGreg Roach                    'type' => 'BIRT',
429a25f0a04SGreg Roach                    'sort' => '@N.N.',
430ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
43113abd6f3SGreg Roach                ];
432a25f0a04SGreg Roach            }
433a25f0a04SGreg Roach            foreach ($wife_names as $n => $wife_name) {
434a25f0a04SGreg Roach                $wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
435a25f0a04SGreg Roach            }
436e88674d4SGreg Roach
437a25f0a04SGreg Roach            // Add the matched names first
438a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
439a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
440e364afe4SGreg Roach                    if ($husb_name['script'] === $wife_name['script']) {
441bdb3725aSGreg Roach                        $this->getAllNames[] = [
442a25f0a04SGreg Roach                            'type' => $husb_name['type'],
443a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
444a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
445a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
44613abd6f3SGreg Roach                        ];
447a25f0a04SGreg Roach                    }
448a25f0a04SGreg Roach                }
449a25f0a04SGreg Roach            }
450e88674d4SGreg Roach
451a25f0a04SGreg Roach            // Add the unmatched names second (there may be no matched names)
452a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
453a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
454e364afe4SGreg Roach                    if ($husb_name['script'] !== $wife_name['script']) {
455bdb3725aSGreg Roach                        $this->getAllNames[] = [
456a25f0a04SGreg Roach                            'type' => $husb_name['type'],
457a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
458a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
459a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
46013abd6f3SGreg Roach                        ];
461a25f0a04SGreg Roach                    }
462a25f0a04SGreg Roach                }
463a25f0a04SGreg Roach            }
464a25f0a04SGreg Roach        }
465a25f0a04SGreg Roach
466bdb3725aSGreg Roach        return $this->getAllNames;
467a25f0a04SGreg Roach    }
468a25f0a04SGreg Roach
46976692c8bSGreg Roach    /**
47076692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
47176692c8bSGreg Roach     * identifying characteristics of this record.
47276692c8bSGreg Roach     *
47376692c8bSGreg Roach     * @return string
47476692c8bSGreg Roach     */
4758f53f488SRico Sonntag    public function formatListDetails(): string
476c1010edaSGreg Roach    {
477a25f0a04SGreg Roach        return
4788d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) .
4798d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1);
480a25f0a04SGreg Roach    }
481a25f0a04SGreg Roach}
482