xref: /webtrees/app/Family.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
23d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage;
248091bfd1SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
25820b62dfSGreg Roachuse Illuminate\Support\Collection;
262e5b4452SGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * A GEDCOM family (FAM) object.
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass Family extends GedcomRecord
31c1010edaSGreg Roach{
3216d6367aSGreg Roach    public const RECORD_TYPE = 'FAM';
3316d6367aSGreg Roach
34d7daee59SGreg Roach    protected const ROUTE_NAME = FamilyPage::class;
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach    /** @var Individual|null The husband (or first spouse for same-sex couples) */
37a25f0a04SGreg Roach    private $husb;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach    /** @var Individual|null The wife (or second spouse for same-sex couples) */
40a25f0a04SGreg Roach    private $wife;
41a25f0a04SGreg Roach
4276692c8bSGreg Roach    /**
4376692c8bSGreg Roach     * Create a GedcomRecord object from raw GEDCOM data.
4476692c8bSGreg Roach     *
4576692c8bSGreg Roach     * @param string      $xref
4676692c8bSGreg Roach     * @param string      $gedcom  an empty string for new/pending records
4776692c8bSGreg Roach     * @param string|null $pending null for a record with no pending edits,
4876692c8bSGreg Roach     *                             empty string for records with pending deletions
4976692c8bSGreg Roach     * @param Tree        $tree
5076692c8bSGreg Roach     */
51e364afe4SGreg Roach    public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree)
52c1010edaSGreg Roach    {
5324ec66ceSGreg Roach        parent::__construct($xref, $gedcom, $pending, $tree);
54a25f0a04SGreg Roach
5514239cc9SGreg Roach        // Make sure we find records in pending records.
5614239cc9SGreg Roach        $gedcom_pending = $gedcom . "\n" . $pending;
5714239cc9SGreg Roach
5814239cc9SGreg Roach        if (preg_match('/\n1 HUSB @(.+)@/', $gedcom_pending, $match)) {
596b9cb339SGreg Roach            $this->husb = Registry::individualFactory()->make($match[1], $tree);
60a25f0a04SGreg Roach        }
6114239cc9SGreg Roach        if (preg_match('/\n1 WIFE @(.+)@/', $gedcom_pending, $match)) {
626b9cb339SGreg Roach            $this->wife = Registry::individualFactory()->make($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     *
69bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::family()
70bb03c9f0SGreg Roach     *
71d5ad3db0SGreg Roach     * @param Tree $tree
72d5ad3db0SGreg Roach     *
73886b77daSGreg Roach     * @return Closure
74886b77daSGreg Roach     */
75d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
76886b77daSGreg Roach    {
776b9cb339SGreg Roach        return Registry::familyFactory()->mapper($tree);
78886b77daSGreg Roach    }
79886b77daSGreg Roach
80886b77daSGreg Roach    /**
81c156e8f5SGreg Roach     * A closure which will compare families by marriage date.
82c156e8f5SGreg Roach     *
83c156e8f5SGreg Roach     * @return Closure
84c156e8f5SGreg Roach     */
85c156e8f5SGreg Roach    public static function marriageDateComparator(): Closure
86c156e8f5SGreg Roach    {
876c2179e2SGreg Roach        return static function (Family $x, Family $y): int {
88c156e8f5SGreg Roach            return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
89c156e8f5SGreg Roach        };
90c156e8f5SGreg Roach    }
91c156e8f5SGreg Roach
92c156e8f5SGreg Roach    /**
93e71ef9d2SGreg Roach     * Get an instance of a family object. For single records,
94e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
95e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
96e71ef9d2SGreg Roach     *
97bb03c9f0SGreg Roach     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::family()
98bb03c9f0SGreg Roach     *
99e71ef9d2SGreg Roach     * @param string      $xref
100e71ef9d2SGreg Roach     * @param Tree        $tree
101e71ef9d2SGreg Roach     * @param string|null $gedcom
102e71ef9d2SGreg Roach     *
103e71ef9d2SGreg Roach     * @return Family|null
104e71ef9d2SGreg Roach     */
105ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Family
106c1010edaSGreg Roach    {
1076b9cb339SGreg Roach        return Registry::familyFactory()->make($xref, $tree, $gedcom);
108e71ef9d2SGreg Roach    }
109e71ef9d2SGreg Roach
110e71ef9d2SGreg Roach    /**
11176692c8bSGreg Roach     * Generate a private version of this record
11276692c8bSGreg Roach     *
11376692c8bSGreg Roach     * @param int $access_level
11476692c8bSGreg Roach     *
11576692c8bSGreg Roach     * @return string
11676692c8bSGreg Roach     */
1173c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
118c1010edaSGreg Roach    {
119d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
120d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
121d9e083e7SGreg Roach        }
122a25f0a04SGreg Roach
123a25f0a04SGreg Roach        $rec = '0 @' . $this->xref . '@ FAM';
124a25f0a04SGreg Roach        // Just show the 1 CHIL/HUSB/WIFE tag, not any subtags, which may contain private data
1258d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches, PREG_SET_ORDER);
126a25f0a04SGreg Roach        foreach ($matches as $match) {
1276b9cb339SGreg Roach            $rela = Registry::individualFactory()->make($match[1], $this->tree);
128d9e083e7SGreg Roach            if ($rela instanceof Individual && $rela->canShow($access_level)) {
129a25f0a04SGreg Roach                $rec .= $match[0];
130a25f0a04SGreg Roach            }
131a25f0a04SGreg Roach        }
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach        return $rec;
134a25f0a04SGreg Roach    }
135a25f0a04SGreg Roach
13676692c8bSGreg Roach    /**
137a25f0a04SGreg Roach     * Get the male (or first female) partner of the family
138a25f0a04SGreg Roach     *
139e93111adSRico Sonntag     * @param int|null $access_level
140b3f49e6cSGreg Roach     *
141a25f0a04SGreg Roach     * @return Individual|null
142a25f0a04SGreg Roach     */
14339ca88baSGreg Roach    public function husband($access_level = null): ?Individual
144c1010edaSGreg Roach    {
145d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
146d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
147d9e083e7SGreg Roach        }
148b3f49e6cSGreg Roach
149d9e083e7SGreg Roach        if ($this->husb instanceof Individual && $this->husb->canShowName($access_level)) {
150a25f0a04SGreg Roach            return $this->husb;
151a25f0a04SGreg Roach        }
152b2ce94c6SRico Sonntag
153b2ce94c6SRico Sonntag        return null;
154a25f0a04SGreg Roach    }
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach    /**
157a25f0a04SGreg Roach     * Get the female (or second male) partner of the family
158a25f0a04SGreg Roach     *
159e93111adSRico Sonntag     * @param int|null $access_level
160b3f49e6cSGreg Roach     *
161a25f0a04SGreg Roach     * @return Individual|null
162a25f0a04SGreg Roach     */
16339ca88baSGreg Roach    public function wife($access_level = null): ?Individual
164c1010edaSGreg Roach    {
165d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
166d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
167d9e083e7SGreg Roach        }
168b3f49e6cSGreg Roach
169d9e083e7SGreg Roach        if ($this->wife instanceof Individual && $this->wife->canShowName($access_level)) {
170a25f0a04SGreg Roach            return $this->wife;
171a25f0a04SGreg Roach        }
172b2ce94c6SRico Sonntag
173b2ce94c6SRico Sonntag        return null;
174a25f0a04SGreg Roach    }
175a25f0a04SGreg Roach
17676692c8bSGreg Roach    /**
17776692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
17876692c8bSGreg Roach     *
17976692c8bSGreg Roach     * @param int $access_level
18076692c8bSGreg Roach     *
18176692c8bSGreg Roach     * @return bool
18276692c8bSGreg Roach     */
18335584196SGreg Roach    protected function canShowByType(int $access_level): bool
184c1010edaSGreg Roach    {
185a25f0a04SGreg Roach        // Hide a family if any member is private
1868d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches);
187a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
1886b9cb339SGreg Roach            $person = Registry::individualFactory()->make($match, $this->tree);
189a25f0a04SGreg Roach            if ($person && !$person->canShow($access_level)) {
190a25f0a04SGreg Roach                return false;
191a25f0a04SGreg Roach            }
192a25f0a04SGreg Roach        }
193a25f0a04SGreg Roach
194a25f0a04SGreg Roach        return true;
195a25f0a04SGreg Roach    }
196a25f0a04SGreg Roach
19776692c8bSGreg Roach    /**
19876692c8bSGreg Roach     * Can the name of this record be shown?
19976692c8bSGreg Roach     *
20076692c8bSGreg Roach     * @param int|null $access_level
20176692c8bSGreg Roach     *
20276692c8bSGreg Roach     * @return bool
20376692c8bSGreg Roach     */
20435584196SGreg Roach    public function canShowName(int $access_level = null): bool
205c1010edaSGreg Roach    {
206a25f0a04SGreg Roach        // We can always see the name (Husband-name + Wife-name), however,
207a25f0a04SGreg Roach        // the name will often be "private + private"
208a25f0a04SGreg Roach        return true;
209a25f0a04SGreg Roach    }
210a25f0a04SGreg Roach
211a25f0a04SGreg Roach    /**
212a25f0a04SGreg Roach     * Find the spouse of a person.
213a25f0a04SGreg Roach     *
214a25f0a04SGreg Roach     * @param Individual $person
21513e3123bSGreg Roach     * @param int|null   $access_level
216a25f0a04SGreg Roach     *
217a25f0a04SGreg Roach     * @return Individual|null
218a25f0a04SGreg Roach     */
219e364afe4SGreg Roach    public function spouse(Individual $person, $access_level = null): ?Individual
220c1010edaSGreg Roach    {
221a25f0a04SGreg Roach        if ($person === $this->wife) {
22239ca88baSGreg Roach            return $this->husband($access_level);
223a25f0a04SGreg Roach        }
224b2ce94c6SRico Sonntag
22539ca88baSGreg Roach        return $this->wife($access_level);
226a25f0a04SGreg Roach    }
227a25f0a04SGreg Roach
228a25f0a04SGreg Roach    /**
229a25f0a04SGreg Roach     * Get the (zero, one or two) spouses from this family.
230a25f0a04SGreg Roach     *
231cbc1590aSGreg Roach     * @param int|null $access_level
232a25f0a04SGreg Roach     *
233b5c8fd7eSGreg Roach     * @return Collection<Individual>
234a25f0a04SGreg Roach     */
235820b62dfSGreg Roach    public function spouses($access_level = null): Collection
236c1010edaSGreg Roach    {
237820b62dfSGreg Roach        $spouses = new Collection([
23839ca88baSGreg Roach            $this->husband($access_level),
23939ca88baSGreg Roach            $this->wife($access_level),
24013abd6f3SGreg Roach        ]);
241820b62dfSGreg Roach
242820b62dfSGreg Roach        return $spouses->filter();
243a25f0a04SGreg Roach    }
244a25f0a04SGreg Roach
245a25f0a04SGreg Roach    /**
246a25f0a04SGreg Roach     * Get a list of this family’s children.
247a25f0a04SGreg Roach     *
248cbc1590aSGreg Roach     * @param int|null $access_level
249a25f0a04SGreg Roach     *
250b5c8fd7eSGreg Roach     * @return Collection<Individual>
251a25f0a04SGreg Roach     */
252820b62dfSGreg Roach    public function children($access_level = null): Collection
253c1010edaSGreg Roach    {
254d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->tree);
2554b9ff166SGreg Roach
256d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
257d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
258d9e083e7SGreg Roach        }
259a25f0a04SGreg Roach
260820b62dfSGreg Roach        $children = new Collection();
261820b62dfSGreg Roach
262d9e083e7SGreg Roach        foreach ($this->facts(['CHIL'], false, $access_level) as $fact) {
263dc124885SGreg Roach            $child = $fact->target();
264820b62dfSGreg Roach
265d9e083e7SGreg Roach            if ($child instanceof Individual && $child->canShowName($access_level)) {
266820b62dfSGreg Roach                $children->push($child);
267a25f0a04SGreg Roach            }
268a25f0a04SGreg Roach        }
269a25f0a04SGreg Roach
270a25f0a04SGreg Roach        return $children;
271a25f0a04SGreg Roach    }
272a25f0a04SGreg Roach
273a25f0a04SGreg Roach    /**
274a25f0a04SGreg Roach     * Number of children - for the individual list
275a25f0a04SGreg Roach     *
276cbc1590aSGreg Roach     * @return int
277a25f0a04SGreg Roach     */
27839ca88baSGreg Roach    public function numberOfChildren(): int
279c1010edaSGreg Roach    {
280820b62dfSGreg Roach        $nchi = $this->children()->count();
281820b62dfSGreg Roach
2828d0ebef0SGreg Roach        foreach ($this->facts(['NCHI']) as $fact) {
28384586c02SGreg Roach            $nchi = max($nchi, (int) $fact->value());
284a25f0a04SGreg Roach        }
285a25f0a04SGreg Roach
286a25f0a04SGreg Roach        return $nchi;
287a25f0a04SGreg Roach    }
288a25f0a04SGreg Roach
289a25f0a04SGreg Roach    /**
290a25f0a04SGreg Roach     * get the marriage event
291a25f0a04SGreg Roach     *
29266107289SGreg Roach     * @return Fact|null
293a25f0a04SGreg Roach     */
294820b62dfSGreg Roach    public function getMarriage(): ?Fact
295c1010edaSGreg Roach    {
296820b62dfSGreg Roach        return $this->facts(['MARR'])->first();
297a25f0a04SGreg Roach    }
298a25f0a04SGreg Roach
299a25f0a04SGreg Roach    /**
300a25f0a04SGreg Roach     * Get marriage date
301a25f0a04SGreg Roach     *
302a25f0a04SGreg Roach     * @return Date
303a25f0a04SGreg Roach     */
304820b62dfSGreg Roach    public function getMarriageDate(): Date
305c1010edaSGreg Roach    {
306a25f0a04SGreg Roach        $marriage = $this->getMarriage();
307a25f0a04SGreg Roach        if ($marriage) {
3082decada7SGreg Roach            return $marriage->date();
309a25f0a04SGreg Roach        }
310b2ce94c6SRico Sonntag
311b2ce94c6SRico Sonntag        return new Date('');
312a25f0a04SGreg Roach    }
313a25f0a04SGreg Roach
314a25f0a04SGreg Roach    /**
315a25f0a04SGreg Roach     * Get the marriage year - displayed on lists of families
316a25f0a04SGreg Roach     *
317cbc1590aSGreg Roach     * @return int
318a25f0a04SGreg Roach     */
3198f53f488SRico Sonntag    public function getMarriageYear(): int
320c1010edaSGreg Roach    {
3214a83f5d7SGreg Roach        return $this->getMarriageDate()->minimumDate()->year;
322a25f0a04SGreg Roach    }
323a25f0a04SGreg Roach
324a25f0a04SGreg Roach    /**
325a25f0a04SGreg Roach     * Get the marriage place
326a25f0a04SGreg Roach     *
327a25f0a04SGreg Roach     * @return Place
328a25f0a04SGreg Roach     */
3298f53f488SRico Sonntag    public function getMarriagePlace(): Place
330c1010edaSGreg Roach    {
331a25f0a04SGreg Roach        $marriage = $this->getMarriage();
332a25f0a04SGreg Roach
3337abafad0SGreg Roach        if ($marriage instanceof Fact) {
3344fb14fcbSGreg Roach            return $marriage->place();
335a25f0a04SGreg Roach        }
336a25f0a04SGreg Roach
3377abafad0SGreg Roach        return new Place('', $this->tree);
3387abafad0SGreg Roach    }
3397abafad0SGreg Roach
340a25f0a04SGreg Roach    /**
341a25f0a04SGreg Roach     * Get a list of all marriage dates - for the family lists.
342a25f0a04SGreg Roach     *
343a25f0a04SGreg Roach     * @return Date[]
344a25f0a04SGreg Roach     */
3458f53f488SRico Sonntag    public function getAllMarriageDates(): array
346c1010edaSGreg Roach    {
3478d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3487abafad0SGreg Roach            $array = $this->getAllEventDates([$event]);
3498af3e5c1SGreg Roach
35054c1ab5eSGreg Roach            if ($array !== []) {
351a25f0a04SGreg Roach                return $array;
352a25f0a04SGreg Roach            }
353a25f0a04SGreg Roach        }
354a25f0a04SGreg Roach
35513abd6f3SGreg Roach        return [];
356a25f0a04SGreg Roach    }
357a25f0a04SGreg Roach
358a25f0a04SGreg Roach    /**
359a25f0a04SGreg Roach     * Get a list of all marriage places - for the family lists.
360a25f0a04SGreg Roach     *
3614080d558SGreg Roach     * @return Place[]
362a25f0a04SGreg Roach     */
3638f53f488SRico Sonntag    public function getAllMarriagePlaces(): array
364c1010edaSGreg Roach    {
3658d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3666e83554dSGreg Roach            $places = $this->getAllEventPlaces([$event]);
36754c1ab5eSGreg Roach            if ($places !== []) {
3684080d558SGreg Roach                return $places;
369a25f0a04SGreg Roach            }
370a25f0a04SGreg Roach        }
371a25f0a04SGreg Roach
37213abd6f3SGreg Roach        return [];
373a25f0a04SGreg Roach    }
374a25f0a04SGreg Roach
37576692c8bSGreg Roach    /**
37676692c8bSGreg Roach     * Derived classes should redefine this function, otherwise the object will have no name
37776692c8bSGreg Roach     *
37876692c8bSGreg Roach     * @return string[][]
37976692c8bSGreg Roach     */
3808f53f488SRico Sonntag    public function getAllNames(): array
381c1010edaSGreg Roach    {
3828f038c36SRico Sonntag        if ($this->getAllNames === null) {
383a25f0a04SGreg Roach            // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
384e88674d4SGreg Roach            $husb_names = [];
385a25f0a04SGreg Roach            if ($this->husb) {
3860b5fd0a6SGreg Roach                $husb_names = array_filter($this->husb->getAllNames(), static function (array $x): bool {
3878d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
3888d68cabeSGreg Roach                });
389e88674d4SGreg Roach            }
3901f244d77SGreg Roach            // If the individual only has married names, create a fake birth name.
39154c1ab5eSGreg Roach            if ($husb_names === []) {
392e88674d4SGreg Roach                $husb_names[] = [
393a25f0a04SGreg Roach                    'type' => 'BIRT',
3948fb4e87cSGreg Roach                    'sort' => Individual::NOMEN_NESCIO,
395ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
39613abd6f3SGreg Roach                ];
397a25f0a04SGreg Roach            }
398a25f0a04SGreg Roach            foreach ($husb_names as $n => $husb_name) {
399a25f0a04SGreg Roach                $husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
400a25f0a04SGreg Roach            }
401e88674d4SGreg Roach
402e88674d4SGreg Roach            $wife_names = [];
403a25f0a04SGreg Roach            if ($this->wife) {
4040b5fd0a6SGreg Roach                $wife_names = array_filter($this->wife->getAllNames(), static function (array $x): bool {
4058d68cabeSGreg Roach                    return $x['type'] !== '_MARNM';
4068d68cabeSGreg Roach                });
407e88674d4SGreg Roach            }
4081f244d77SGreg Roach            // If the individual only has married names, create a fake birth name.
40954c1ab5eSGreg Roach            if ($wife_names === []) {
410e88674d4SGreg Roach                $wife_names[] = [
411a25f0a04SGreg Roach                    'type' => 'BIRT',
4128fb4e87cSGreg Roach                    'sort' => Individual::NOMEN_NESCIO,
413ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
41413abd6f3SGreg Roach                ];
415a25f0a04SGreg Roach            }
416a25f0a04SGreg Roach            foreach ($wife_names as $n => $wife_name) {
417a25f0a04SGreg Roach                $wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
418a25f0a04SGreg Roach            }
419e88674d4SGreg Roach
420a25f0a04SGreg Roach            // Add the matched names first
421a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
422a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
423e364afe4SGreg Roach                    if ($husb_name['script'] === $wife_name['script']) {
424bdb3725aSGreg Roach                        $this->getAllNames[] = [
425a25f0a04SGreg Roach                            'type' => $husb_name['type'],
426a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
427a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
428a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
42913abd6f3SGreg Roach                        ];
430a25f0a04SGreg Roach                    }
431a25f0a04SGreg Roach                }
432a25f0a04SGreg Roach            }
433e88674d4SGreg Roach
434a25f0a04SGreg Roach            // Add the unmatched names second (there may be no matched names)
435a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
436a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
437e364afe4SGreg Roach                    if ($husb_name['script'] !== $wife_name['script']) {
438bdb3725aSGreg Roach                        $this->getAllNames[] = [
439a25f0a04SGreg Roach                            'type' => $husb_name['type'],
440a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
441a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
442a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
44313abd6f3SGreg Roach                        ];
444a25f0a04SGreg Roach                    }
445a25f0a04SGreg Roach                }
446a25f0a04SGreg Roach            }
447a25f0a04SGreg Roach        }
448a25f0a04SGreg Roach
449bdb3725aSGreg Roach        return $this->getAllNames;
450a25f0a04SGreg Roach    }
451a25f0a04SGreg Roach
45276692c8bSGreg Roach    /**
45376692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
45476692c8bSGreg Roach     * identifying characteristics of this record.
45576692c8bSGreg Roach     *
45676692c8bSGreg Roach     * @return string
45776692c8bSGreg Roach     */
4588f53f488SRico Sonntag    public function formatListDetails(): string
459c1010edaSGreg Roach    {
460a25f0a04SGreg Roach        return
4618d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) .
4628d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1);
463a25f0a04SGreg Roach    }
4648091bfd1SGreg Roach
4658091bfd1SGreg Roach    /**
4668091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
4678091bfd1SGreg Roach     */
4688091bfd1SGreg Roach    public function lock(): void
4698091bfd1SGreg Roach    {
4708091bfd1SGreg Roach        DB::table('families')
4718091bfd1SGreg Roach            ->where('f_file', '=', $this->tree->id())
4728091bfd1SGreg Roach            ->where('f_id', '=', $this->xref())
4738091bfd1SGreg Roach            ->lockForUpdate()
4748091bfd1SGreg Roach            ->get();
4758091bfd1SGreg Roach    }
476a25f0a04SGreg Roach}
477