xref: /webtrees/app/Family.php (revision d9e083e74a1b6a8c087781e745c9bee5852619d7)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
24d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage;
252e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26820b62dfSGreg Roachuse Illuminate\Support\Collection;
27886b77daSGreg Roachuse stdClass;
282e5b4452SGreg Roach
29a25f0a04SGreg Roach/**
3076692c8bSGreg Roach * A GEDCOM family (FAM) object.
31a25f0a04SGreg Roach */
32c1010edaSGreg Roachclass Family extends GedcomRecord
33c1010edaSGreg Roach{
3416d6367aSGreg Roach    public const RECORD_TYPE = 'FAM';
3516d6367aSGreg Roach
36d7daee59SGreg Roach    protected const ROUTE_NAME = FamilyPage::class;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /** @var Individual|null The husband (or first spouse for same-sex couples) */
39a25f0a04SGreg Roach    private $husb;
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach    /** @var Individual|null The wife (or second spouse for same-sex couples) */
42a25f0a04SGreg Roach    private $wife;
43a25f0a04SGreg Roach
4476692c8bSGreg Roach    /**
4576692c8bSGreg Roach     * Create a GedcomRecord object from raw GEDCOM data.
4676692c8bSGreg Roach     *
4776692c8bSGreg Roach     * @param string      $xref
4876692c8bSGreg Roach     * @param string      $gedcom  an empty string for new/pending records
4976692c8bSGreg Roach     * @param string|null $pending null for a record with no pending edits,
5076692c8bSGreg Roach     *                             empty string for records with pending deletions
5176692c8bSGreg Roach     * @param Tree        $tree
5276692c8bSGreg Roach     */
53e364afe4SGreg Roach    public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree)
54c1010edaSGreg Roach    {
5524ec66ceSGreg Roach        parent::__construct($xref, $gedcom, $pending, $tree);
56a25f0a04SGreg Roach
57395f0fe0SGreg Roach        // Fetch family members
58395f0fe0SGreg Roach        if (preg_match_all('/^1 (?:HUSB|WIFE|CHIL) @(.+)@/m', $gedcom . $pending, $match)) {
59395f0fe0SGreg Roach            Individual::load($tree, $match[1]);
60395f0fe0SGreg Roach        }
61395f0fe0SGreg Roach
62a25f0a04SGreg Roach        if (preg_match('/^1 HUSB @(.+)@/m', $gedcom . $pending, $match)) {
6324ec66ceSGreg Roach            $this->husb = Individual::getInstance($match[1], $tree);
64a25f0a04SGreg Roach        }
65a25f0a04SGreg Roach        if (preg_match('/^1 WIFE @(.+)@/m', $gedcom . $pending, $match)) {
6624ec66ceSGreg Roach            $this->wife = Individual::getInstance($match[1], $tree);
67a25f0a04SGreg Roach        }
68a25f0a04SGreg Roach    }
69a25f0a04SGreg Roach
7076692c8bSGreg Roach    /**
71886b77daSGreg Roach     * A closure which will create a record from a database row.
72886b77daSGreg Roach     *
73d5ad3db0SGreg Roach     * @param Tree $tree
74d5ad3db0SGreg Roach     *
75886b77daSGreg Roach     * @return Closure
76886b77daSGreg Roach     */
77d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
78886b77daSGreg Roach    {
79d5ad3db0SGreg Roach        return static function (stdClass $row) use ($tree): Family {
80d5ad3db0SGreg Roach            return Family::getInstance($row->f_id, $tree, $row->f_gedcom);
81886b77daSGreg Roach        };
82886b77daSGreg Roach    }
83886b77daSGreg Roach
84886b77daSGreg Roach    /**
85c156e8f5SGreg Roach     * A closure which will compare families by marriage date.
86c156e8f5SGreg Roach     *
87c156e8f5SGreg Roach     * @return Closure
88c156e8f5SGreg Roach     */
89c156e8f5SGreg Roach    public static function marriageDateComparator(): Closure
90c156e8f5SGreg Roach    {
916c2179e2SGreg Roach        return static function (Family $x, Family $y): int {
92c156e8f5SGreg Roach            return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
93c156e8f5SGreg Roach        };
94c156e8f5SGreg Roach    }
95c156e8f5SGreg Roach
96c156e8f5SGreg Roach    /**
97e71ef9d2SGreg Roach     * Get an instance of a family object. For single records,
98e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
99e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
100e71ef9d2SGreg Roach     *
101e71ef9d2SGreg Roach     * @param string      $xref
102e71ef9d2SGreg Roach     * @param Tree        $tree
103e71ef9d2SGreg Roach     * @param string|null $gedcom
104e71ef9d2SGreg Roach     *
1056ccdf4f0SGreg Roach     * @throws Exception
106e71ef9d2SGreg Roach     *
107e71ef9d2SGreg Roach     * @return Family|null
108e71ef9d2SGreg Roach     */
109e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
110c1010edaSGreg Roach    {
111e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
112e71ef9d2SGreg Roach
113e364afe4SGreg Roach        if ($record instanceof self) {
114e71ef9d2SGreg Roach            return $record;
115e71ef9d2SGreg Roach        }
116b2ce94c6SRico Sonntag
117b2ce94c6SRico Sonntag        return null;
118e71ef9d2SGreg Roach    }
119e71ef9d2SGreg Roach
120e71ef9d2SGreg Roach    /**
12176692c8bSGreg Roach     * Generate a private version of this record
12276692c8bSGreg Roach     *
12376692c8bSGreg Roach     * @param int $access_level
12476692c8bSGreg Roach     *
12576692c8bSGreg Roach     * @return string
12676692c8bSGreg Roach     */
1273c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
128c1010edaSGreg Roach    {
129*d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
130*d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
131*d9e083e7SGreg Roach        }
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ FAM';
134a25f0a04SGreg Roach        // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data
1358d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
136a25f0a04SGreg Roach        foreach ($matches as $match) {
13724ec66ceSGreg Roach            $rela = Individual::getInstance($match[1], $this->tree);
138*d9e083e7SGreg Roach            if ($rela instanceof Individual && $rela->canShow($access_level)) {
139a25f0a04SGreg Roach                $rec .= $match[0];
140a25f0a04SGreg Roach            }
141a25f0a04SGreg Roach        }
142a25f0a04SGreg Roach
143a25f0a04SGreg Roach        return $rec;
144a25f0a04SGreg Roach    }
145a25f0a04SGreg Roach
14676692c8bSGreg Roach    /**
14776692c8bSGreg Roach     * Fetch data from the database
14876692c8bSGreg Roach     *
14976692c8bSGreg Roach     * @param string $xref
15076692c8bSGreg Roach     * @param int    $tree_id
15176692c8bSGreg Roach     *
152e364afe4SGreg Roach     * @return string|null
15376692c8bSGreg Roach     */
154e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
155c1010edaSGreg Roach    {
1562e5b4452SGreg Roach        return DB::table('families')
1572e5b4452SGreg Roach            ->where('f_id', '=', $xref)
1582e5b4452SGreg Roach            ->where('f_file', '=', $tree_id)
1592e5b4452SGreg Roach            ->value('f_gedcom');
160a25f0a04SGreg Roach    }
161a25f0a04SGreg Roach
162a25f0a04SGreg Roach    /**
163a25f0a04SGreg Roach     * Get the male (or first female) partner of the family
164a25f0a04SGreg Roach     *
165e93111adSRico Sonntag     * @param int|null $access_level
166b3f49e6cSGreg Roach     *
167a25f0a04SGreg Roach     * @return Individual|null
168a25f0a04SGreg Roach     */
16939ca88baSGreg Roach    public function husband($access_level = null): ?Individual
170c1010edaSGreg Roach    {
171*d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
172*d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
173*d9e083e7SGreg Roach        }
174b3f49e6cSGreg Roach
175*d9e083e7SGreg Roach        if ($this->husb instanceof Individual && $this->husb->canShowName($access_level)) {
176a25f0a04SGreg Roach            return $this->husb;
177a25f0a04SGreg Roach        }
178b2ce94c6SRico Sonntag
179b2ce94c6SRico Sonntag        return null;
180a25f0a04SGreg Roach    }
181a25f0a04SGreg Roach
182a25f0a04SGreg Roach    /**
183a25f0a04SGreg Roach     * Get the female (or second male) partner of the family
184a25f0a04SGreg Roach     *
185e93111adSRico Sonntag     * @param int|null $access_level
186b3f49e6cSGreg Roach     *
187a25f0a04SGreg Roach     * @return Individual|null
188a25f0a04SGreg Roach     */
18939ca88baSGreg Roach    public function wife($access_level = null): ?Individual
190c1010edaSGreg Roach    {
191*d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
192*d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
193*d9e083e7SGreg Roach        }
194b3f49e6cSGreg Roach
195*d9e083e7SGreg Roach        if ($this->wife instanceof Individual && $this->wife->canShowName($access_level)) {
196a25f0a04SGreg Roach            return $this->wife;
197a25f0a04SGreg Roach        }
198b2ce94c6SRico Sonntag
199b2ce94c6SRico Sonntag        return null;
200a25f0a04SGreg Roach    }
201a25f0a04SGreg Roach
20276692c8bSGreg Roach    /**
20376692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
20476692c8bSGreg Roach     *
20576692c8bSGreg Roach     * @param int $access_level
20676692c8bSGreg Roach     *
20776692c8bSGreg Roach     * @return bool
20876692c8bSGreg Roach     */
20935584196SGreg Roach    protected function canShowByType(int $access_level): bool
210c1010edaSGreg Roach    {
211a25f0a04SGreg Roach        // Hide a family if any member is private
2128d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches);
213a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
21424ec66ceSGreg Roach            $person = Individual::getInstance($match, $this->tree);
215a25f0a04SGreg Roach            if ($person && !$person->canShow($access_level)) {
216a25f0a04SGreg Roach                return false;
217a25f0a04SGreg Roach            }
218a25f0a04SGreg Roach        }
219a25f0a04SGreg Roach
220a25f0a04SGreg Roach        return true;
221a25f0a04SGreg Roach    }
222a25f0a04SGreg Roach
22376692c8bSGreg Roach    /**
22476692c8bSGreg Roach     * Can the name of this record be shown?
22576692c8bSGreg Roach     *
22676692c8bSGreg Roach     * @param int|null $access_level
22776692c8bSGreg Roach     *
22876692c8bSGreg Roach     * @return bool
22976692c8bSGreg Roach     */
23035584196SGreg Roach    public function canShowName(int $access_level = null): bool
231c1010edaSGreg Roach    {
232a25f0a04SGreg Roach        // We can always see the name (Husband-name + Wife-name), however,
233a25f0a04SGreg Roach        // the name will often be "private + private"
234a25f0a04SGreg Roach        return true;
235a25f0a04SGreg Roach    }
236a25f0a04SGreg Roach
237a25f0a04SGreg Roach    /**
238a25f0a04SGreg Roach     * Find the spouse of a person.
239a25f0a04SGreg Roach     *
240a25f0a04SGreg Roach     * @param Individual $person
24113e3123bSGreg Roach     * @param int|null   $access_level
242a25f0a04SGreg Roach     *
243a25f0a04SGreg Roach     * @return Individual|null
244a25f0a04SGreg Roach     */
245e364afe4SGreg Roach    public function spouse(Individual $person, $access_level = null): ?Individual
246c1010edaSGreg Roach    {
247a25f0a04SGreg Roach        if ($person === $this->wife) {
24839ca88baSGreg Roach            return $this->husband($access_level);
249a25f0a04SGreg Roach        }
250b2ce94c6SRico Sonntag
25139ca88baSGreg Roach        return $this->wife($access_level);
252a25f0a04SGreg Roach    }
253a25f0a04SGreg Roach
254a25f0a04SGreg Roach    /**
255a25f0a04SGreg Roach     * Get the (zero, one or two) spouses from this family.
256a25f0a04SGreg Roach     *
257cbc1590aSGreg Roach     * @param int|null $access_level
258a25f0a04SGreg Roach     *
25954c7f8dfSGreg Roach     * @return Collection
260a25f0a04SGreg Roach     */
261820b62dfSGreg Roach    public function spouses($access_level = null): Collection
262c1010edaSGreg Roach    {
263820b62dfSGreg Roach        $spouses = new Collection([
26439ca88baSGreg Roach            $this->husband($access_level),
26539ca88baSGreg Roach            $this->wife($access_level),
26613abd6f3SGreg Roach        ]);
267820b62dfSGreg Roach
268820b62dfSGreg Roach        return $spouses->filter();
269a25f0a04SGreg Roach    }
270a25f0a04SGreg Roach
271a25f0a04SGreg Roach    /**
272a25f0a04SGreg Roach     * Get a list of this family’s children.
273a25f0a04SGreg Roach     *
274cbc1590aSGreg Roach     * @param int|null $access_level
275a25f0a04SGreg Roach     *
27654c7f8dfSGreg Roach     * @return Collection
277a25f0a04SGreg Roach     */
278820b62dfSGreg Roach    public function children($access_level = null): Collection
279c1010edaSGreg Roach    {
280*d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
2814b9ff166SGreg Roach
282*d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
283*d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
284*d9e083e7SGreg Roach        }
285a25f0a04SGreg Roach
286820b62dfSGreg Roach        $children = new Collection();
287820b62dfSGreg Roach
288*d9e083e7SGreg Roach        foreach ($this->facts(['CHIL'], false, $access_level) as $fact) {
289dc124885SGreg Roach            $child = $fact->target();
290820b62dfSGreg Roach
291*d9e083e7SGreg Roach            if ($child instanceof Individual && $child->canShowName($access_level)) {
292820b62dfSGreg Roach                $children->push($child);
293a25f0a04SGreg Roach            }
294a25f0a04SGreg Roach        }
295a25f0a04SGreg Roach
296a25f0a04SGreg Roach        return $children;
297a25f0a04SGreg Roach    }
298a25f0a04SGreg Roach
299a25f0a04SGreg Roach    /**
300a25f0a04SGreg Roach     * Number of children - for the individual list
301a25f0a04SGreg Roach     *
302cbc1590aSGreg Roach     * @return int
303a25f0a04SGreg Roach     */
30439ca88baSGreg Roach    public function numberOfChildren(): int
305c1010edaSGreg Roach    {
306820b62dfSGreg Roach        $nchi = $this->children()->count();
307820b62dfSGreg Roach
3088d0ebef0SGreg Roach        foreach ($this->facts(['NCHI']) as $fact) {
30984586c02SGreg Roach            $nchi = max($nchi, (int) $fact->value());
310a25f0a04SGreg Roach        }
311a25f0a04SGreg Roach
312a25f0a04SGreg Roach        return $nchi;
313a25f0a04SGreg Roach    }
314a25f0a04SGreg Roach
315a25f0a04SGreg Roach    /**
316a25f0a04SGreg Roach     * get the marriage event
317a25f0a04SGreg Roach     *
31866107289SGreg Roach     * @return Fact|null
319a25f0a04SGreg Roach     */
320820b62dfSGreg Roach    public function getMarriage(): ?Fact
321c1010edaSGreg Roach    {
322820b62dfSGreg Roach        return $this->facts(['MARR'])->first();
323a25f0a04SGreg Roach    }
324a25f0a04SGreg Roach
325a25f0a04SGreg Roach    /**
326a25f0a04SGreg Roach     * Get marriage date
327a25f0a04SGreg Roach     *
328a25f0a04SGreg Roach     * @return Date
329a25f0a04SGreg Roach     */
330820b62dfSGreg Roach    public function getMarriageDate(): Date
331c1010edaSGreg Roach    {
332a25f0a04SGreg Roach        $marriage = $this->getMarriage();
333a25f0a04SGreg Roach        if ($marriage) {
3342decada7SGreg Roach            return $marriage->date();
335a25f0a04SGreg Roach        }
336b2ce94c6SRico Sonntag
337b2ce94c6SRico Sonntag        return new Date('');
338a25f0a04SGreg Roach    }
339a25f0a04SGreg Roach
340a25f0a04SGreg Roach    /**
341a25f0a04SGreg Roach     * Get the marriage year - displayed on lists of families
342a25f0a04SGreg Roach     *
343cbc1590aSGreg Roach     * @return int
344a25f0a04SGreg Roach     */
3458f53f488SRico Sonntag    public function getMarriageYear(): int
346c1010edaSGreg Roach    {
3474a83f5d7SGreg Roach        return $this->getMarriageDate()->minimumDate()->year;
348a25f0a04SGreg Roach    }
349a25f0a04SGreg Roach
350a25f0a04SGreg Roach    /**
351a25f0a04SGreg Roach     * Get the marriage place
352a25f0a04SGreg Roach     *
353a25f0a04SGreg Roach     * @return Place
354a25f0a04SGreg Roach     */
3558f53f488SRico Sonntag    public function getMarriagePlace(): Place
356c1010edaSGreg Roach    {
357a25f0a04SGreg Roach        $marriage = $this->getMarriage();
358a25f0a04SGreg Roach
3597abafad0SGreg Roach        if ($marriage instanceof Fact) {
3604fb14fcbSGreg Roach            return $marriage->place();
361a25f0a04SGreg Roach        }
362a25f0a04SGreg Roach
3637abafad0SGreg Roach        return new Place('', $this->tree);
3647abafad0SGreg Roach    }
3657abafad0SGreg Roach
366a25f0a04SGreg Roach    /**
367a25f0a04SGreg Roach     * Get a list of all marriage dates - for the family lists.
368a25f0a04SGreg Roach     *
369a25f0a04SGreg Roach     * @return Date[]
370a25f0a04SGreg Roach     */
3718f53f488SRico Sonntag    public function getAllMarriageDates(): array
372c1010edaSGreg Roach    {
3738d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3747abafad0SGreg Roach            $array = $this->getAllEventDates([$event]);
3758af3e5c1SGreg Roach
37654c1ab5eSGreg Roach            if ($array !== []) {
377a25f0a04SGreg Roach                return $array;
378a25f0a04SGreg Roach            }
379a25f0a04SGreg Roach        }
380a25f0a04SGreg Roach
38113abd6f3SGreg Roach        return [];
382a25f0a04SGreg Roach    }
383a25f0a04SGreg Roach
384a25f0a04SGreg Roach    /**
385a25f0a04SGreg Roach     * Get a list of all marriage places - for the family lists.
386a25f0a04SGreg Roach     *
3874080d558SGreg Roach     * @return Place[]
388a25f0a04SGreg Roach     */
3898f53f488SRico Sonntag    public function getAllMarriagePlaces(): array
390c1010edaSGreg Roach    {
3918d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3926e83554dSGreg Roach            $places = $this->getAllEventPlaces([$event]);
39354c1ab5eSGreg Roach            if ($places !== []) {
3944080d558SGreg Roach                return $places;
395a25f0a04SGreg Roach            }
396a25f0a04SGreg Roach        }
397a25f0a04SGreg Roach
39813abd6f3SGreg Roach        return [];
399a25f0a04SGreg Roach    }
400a25f0a04SGreg Roach
40176692c8bSGreg Roach    /**
40276692c8bSGreg Roach     * Derived classes should redefine this function, otherwise the object will have no name
40376692c8bSGreg Roach     *
40476692c8bSGreg Roach     * @return string[][]
40576692c8bSGreg Roach     */
4068f53f488SRico Sonntag    public function getAllNames(): array
407c1010edaSGreg Roach    {
4088f038c36SRico Sonntag        if ($this->getAllNames === null) {
409a25f0a04SGreg Roach            // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
410e88674d4SGreg Roach            $husb_names = [];
411a25f0a04SGreg Roach            if ($this->husb) {
4120b5fd0a6SGreg Roach                $husb_names = array_filter($this->husb->getAllNames(), static function (array $x): bool {
4138d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
4148d68cabeSGreg Roach                });
415e88674d4SGreg Roach            }
416e88674d4SGreg Roach            // If the individual only has married names, create a dummy birth name.
41754c1ab5eSGreg Roach            if ($husb_names === []) {
418e88674d4SGreg Roach                $husb_names[] = [
419a25f0a04SGreg Roach                    'type' => 'BIRT',
420a25f0a04SGreg Roach                    'sort' => '@N.N.',
421ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
42213abd6f3SGreg Roach                ];
423a25f0a04SGreg Roach            }
424a25f0a04SGreg Roach            foreach ($husb_names as $n => $husb_name) {
425a25f0a04SGreg Roach                $husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
426a25f0a04SGreg Roach            }
427e88674d4SGreg Roach
428e88674d4SGreg Roach            $wife_names = [];
429a25f0a04SGreg Roach            if ($this->wife) {
4300b5fd0a6SGreg Roach                $wife_names = array_filter($this->wife->getAllNames(), static function (array $x): bool {
4318d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
4328d68cabeSGreg Roach                });
433e88674d4SGreg Roach            }
434e88674d4SGreg Roach            // If the individual only has married names, create a dummy birth name.
43554c1ab5eSGreg Roach            if ($wife_names === []) {
436e88674d4SGreg Roach                $wife_names[] = [
437a25f0a04SGreg Roach                    'type' => 'BIRT',
438a25f0a04SGreg Roach                    'sort' => '@N.N.',
439ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
44013abd6f3SGreg Roach                ];
441a25f0a04SGreg Roach            }
442a25f0a04SGreg Roach            foreach ($wife_names as $n => $wife_name) {
443a25f0a04SGreg Roach                $wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
444a25f0a04SGreg Roach            }
445e88674d4SGreg Roach
446a25f0a04SGreg Roach            // Add the matched names first
447a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
448a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
449e364afe4SGreg Roach                    if ($husb_name['script'] === $wife_name['script']) {
450bdb3725aSGreg Roach                        $this->getAllNames[] = [
451a25f0a04SGreg Roach                            'type' => $husb_name['type'],
452a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
453a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
454a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
45513abd6f3SGreg Roach                        ];
456a25f0a04SGreg Roach                    }
457a25f0a04SGreg Roach                }
458a25f0a04SGreg Roach            }
459e88674d4SGreg Roach
460a25f0a04SGreg Roach            // Add the unmatched names second (there may be no matched names)
461a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
462a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
463e364afe4SGreg Roach                    if ($husb_name['script'] !== $wife_name['script']) {
464bdb3725aSGreg Roach                        $this->getAllNames[] = [
465a25f0a04SGreg Roach                            'type' => $husb_name['type'],
466a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
467a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
468a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
46913abd6f3SGreg Roach                        ];
470a25f0a04SGreg Roach                    }
471a25f0a04SGreg Roach                }
472a25f0a04SGreg Roach            }
473a25f0a04SGreg Roach        }
474a25f0a04SGreg Roach
475bdb3725aSGreg Roach        return $this->getAllNames;
476a25f0a04SGreg Roach    }
477a25f0a04SGreg Roach
47876692c8bSGreg Roach    /**
47976692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
48076692c8bSGreg Roach     * identifying characteristics of this record.
48176692c8bSGreg Roach     *
48276692c8bSGreg Roach     * @return string
48376692c8bSGreg Roach     */
4848f53f488SRico Sonntag    public function formatListDetails(): string
485c1010edaSGreg Roach    {
486a25f0a04SGreg Roach        return
4878d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) .
4888d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1);
489a25f0a04SGreg Roach    }
490a25f0a04SGreg Roach}
491