xref: /webtrees/app/Family.php (revision e873f434551745f888937263ff89e80db3b0f785)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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;
24820b62dfSGreg Roachuse Illuminate\Support\Collection;
252e5b4452SGreg Roach
26a25f0a04SGreg Roach/**
2776692c8bSGreg Roach * A GEDCOM family (FAM) object.
28a25f0a04SGreg Roach */
29c1010edaSGreg Roachclass Family extends GedcomRecord
30c1010edaSGreg Roach{
31*e873f434SGreg Roach    public const string RECORD_TYPE = 'FAM';
3216d6367aSGreg Roach
33*e873f434SGreg Roach    protected const string ROUTE_NAME = FamilyPage::class;
34a25f0a04SGreg Roach
3533c746f1SGreg Roach    // The husband (or first spouse for same-sex couples)
361ff45046SGreg Roach    private Individual|null $husb = null;
37a25f0a04SGreg Roach
3833c746f1SGreg Roach    // The wife (or second spouse for same-sex couples)
391ff45046SGreg Roach    private Individual|null $wife = null;
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     */
501ff45046SGreg Roach    public function __construct(string $xref, string $gedcom, string|null $pending, Tree $tree)
51c1010edaSGreg Roach    {
5224ec66ceSGreg Roach        parent::__construct($xref, $gedcom, $pending, $tree);
53a25f0a04SGreg Roach
5414239cc9SGreg Roach        // Make sure we find records in pending records.
5514239cc9SGreg Roach        $gedcom_pending = $gedcom . "\n" . $pending;
5614239cc9SGreg Roach
5714239cc9SGreg Roach        if (preg_match('/\n1 HUSB @(.+)@/', $gedcom_pending, $match)) {
586b9cb339SGreg Roach            $this->husb = Registry::individualFactory()->make($match[1], $tree);
59a25f0a04SGreg Roach        }
6014239cc9SGreg Roach        if (preg_match('/\n1 WIFE @(.+)@/', $gedcom_pending, $match)) {
616b9cb339SGreg Roach            $this->wife = Registry::individualFactory()->make($match[1], $tree);
62a25f0a04SGreg Roach        }
63a25f0a04SGreg Roach    }
64a25f0a04SGreg Roach
6576692c8bSGreg Roach    /**
66c156e8f5SGreg Roach     * A closure which will compare families by marriage date.
67c156e8f5SGreg Roach     *
68c6921a17SGreg Roach     * @return Closure(Family,Family):int
69c156e8f5SGreg Roach     */
70c156e8f5SGreg Roach    public static function marriageDateComparator(): Closure
71c156e8f5SGreg Roach    {
72f25fc0f9SGreg Roach        return static fn (Family $x, Family $y): int => Date::compare($x->getMarriageDate(), $y->getMarriageDate());
73c156e8f5SGreg Roach    }
74c156e8f5SGreg Roach
75c156e8f5SGreg Roach    /**
76a25f0a04SGreg Roach     * Get the male (or first female) partner of the family
77a25f0a04SGreg Roach     *
78e93111adSRico Sonntag     * @param int|null $access_level
79b3f49e6cSGreg Roach     *
80a25f0a04SGreg Roach     * @return Individual|null
81a25f0a04SGreg Roach     */
821ff45046SGreg Roach    public function husband(int|null $access_level = null): Individual|null
83c1010edaSGreg Roach    {
84d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
85d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
86d9e083e7SGreg Roach        }
87b3f49e6cSGreg Roach
88d9e083e7SGreg Roach        if ($this->husb instanceof Individual && $this->husb->canShowName($access_level)) {
89a25f0a04SGreg Roach            return $this->husb;
90a25f0a04SGreg Roach        }
91b2ce94c6SRico Sonntag
92b2ce94c6SRico Sonntag        return null;
93a25f0a04SGreg Roach    }
94a25f0a04SGreg Roach
95a25f0a04SGreg Roach    /**
96a25f0a04SGreg Roach     * Get the female (or second male) partner of the family
97a25f0a04SGreg Roach     *
98e93111adSRico Sonntag     * @param int|null $access_level
99b3f49e6cSGreg Roach     *
100a25f0a04SGreg Roach     * @return Individual|null
101a25f0a04SGreg Roach     */
1021ff45046SGreg Roach    public function wife(int|null $access_level = null): Individual|null
103c1010edaSGreg Roach    {
104d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
105d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
106d9e083e7SGreg Roach        }
107b3f49e6cSGreg Roach
108d9e083e7SGreg Roach        if ($this->wife instanceof Individual && $this->wife->canShowName($access_level)) {
109a25f0a04SGreg Roach            return $this->wife;
110a25f0a04SGreg Roach        }
111b2ce94c6SRico Sonntag
112b2ce94c6SRico Sonntag        return null;
113a25f0a04SGreg Roach    }
114a25f0a04SGreg Roach
11576692c8bSGreg Roach    /**
11676692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
11776692c8bSGreg Roach     *
11876692c8bSGreg Roach     * @param int $access_level
11976692c8bSGreg Roach     *
12076692c8bSGreg Roach     * @return bool
12176692c8bSGreg Roach     */
12235584196SGreg Roach    protected function canShowByType(int $access_level): bool
123c1010edaSGreg Roach    {
124a25f0a04SGreg Roach        // Hide a family if any member is private
1258d0ebef0SGreg Roach        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches);
126a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
1276b9cb339SGreg Roach            $person = Registry::individualFactory()->make($match, $this->tree);
128a25f0a04SGreg Roach            if ($person && !$person->canShow($access_level)) {
129a25f0a04SGreg Roach                return false;
130a25f0a04SGreg Roach            }
131a25f0a04SGreg Roach        }
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach        return true;
134a25f0a04SGreg Roach    }
135a25f0a04SGreg Roach
13676692c8bSGreg Roach    /**
13776692c8bSGreg Roach     * Can the name of this record be shown?
13876692c8bSGreg Roach     *
13976692c8bSGreg Roach     * @param int|null $access_level
14076692c8bSGreg Roach     *
14176692c8bSGreg Roach     * @return bool
14276692c8bSGreg Roach     */
1432c6f1bd5SGreg Roach    public function canShowName(int|null $access_level = null): bool
144c1010edaSGreg Roach    {
145a25f0a04SGreg Roach        // We can always see the name (Husband-name + Wife-name), however,
146a25f0a04SGreg Roach        // the name will often be "private + private"
147a25f0a04SGreg Roach        return true;
148a25f0a04SGreg Roach    }
149a25f0a04SGreg Roach
150a25f0a04SGreg Roach    /**
151a25f0a04SGreg Roach     * Find the spouse of a person.
152a25f0a04SGreg Roach     *
153a25f0a04SGreg Roach     * @param Individual $person
15413e3123bSGreg Roach     * @param int|null   $access_level
155a25f0a04SGreg Roach     *
156a25f0a04SGreg Roach     * @return Individual|null
157a25f0a04SGreg Roach     */
1581ff45046SGreg Roach    public function spouse(Individual $person, int|null $access_level = null): Individual|null
159c1010edaSGreg Roach    {
160a25f0a04SGreg Roach        if ($person === $this->wife) {
16139ca88baSGreg Roach            return $this->husband($access_level);
162a25f0a04SGreg Roach        }
163b2ce94c6SRico Sonntag
16439ca88baSGreg Roach        return $this->wife($access_level);
165a25f0a04SGreg Roach    }
166a25f0a04SGreg Roach
167a25f0a04SGreg Roach    /**
168a25f0a04SGreg Roach     * Get the (zero, one or two) spouses from this family.
169a25f0a04SGreg Roach     *
170cbc1590aSGreg Roach     * @param int|null $access_level
171a25f0a04SGreg Roach     *
17236779af1SGreg Roach     * @return Collection<int,Individual>
173a25f0a04SGreg Roach     */
1742c6f1bd5SGreg Roach    public function spouses(int|null $access_level = null): Collection
175c1010edaSGreg Roach    {
176820b62dfSGreg Roach        $spouses = new Collection([
17739ca88baSGreg Roach            $this->husband($access_level),
17839ca88baSGreg Roach            $this->wife($access_level),
17913abd6f3SGreg Roach        ]);
180820b62dfSGreg Roach
181820b62dfSGreg Roach        return $spouses->filter();
182a25f0a04SGreg Roach    }
183a25f0a04SGreg Roach
184a25f0a04SGreg Roach    /**
185a25f0a04SGreg Roach     * Get a list of this family’s children.
186a25f0a04SGreg Roach     *
187cbc1590aSGreg Roach     * @param int|null $access_level
188a25f0a04SGreg Roach     *
18936779af1SGreg Roach     * @return Collection<int,Individual>
190a25f0a04SGreg Roach     */
1912c6f1bd5SGreg Roach    public function children(int|null $access_level = null): Collection
192c1010edaSGreg Roach    {
1933529c469SGreg Roach        $access_level ??= Auth::accessLevel($this->tree);
1944b9ff166SGreg Roach
195d9e083e7SGreg Roach        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
196d9e083e7SGreg Roach            $access_level = Auth::PRIV_HIDE;
197d9e083e7SGreg Roach        }
198a25f0a04SGreg Roach
199820b62dfSGreg Roach        $children = new Collection();
200820b62dfSGreg Roach
201d9e083e7SGreg Roach        foreach ($this->facts(['CHIL'], false, $access_level) as $fact) {
202dc124885SGreg Roach            $child = $fact->target();
203820b62dfSGreg Roach
204d9e083e7SGreg Roach            if ($child instanceof Individual && $child->canShowName($access_level)) {
205820b62dfSGreg Roach                $children->push($child);
206a25f0a04SGreg Roach            }
207a25f0a04SGreg Roach        }
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach        return $children;
210a25f0a04SGreg Roach    }
211a25f0a04SGreg Roach
212a25f0a04SGreg Roach    /**
213a25f0a04SGreg Roach     * Number of children - for the individual list
214a25f0a04SGreg Roach     *
215cbc1590aSGreg Roach     * @return int
216a25f0a04SGreg Roach     */
21739ca88baSGreg Roach    public function numberOfChildren(): int
218c1010edaSGreg Roach    {
219820b62dfSGreg Roach        $nchi = $this->children()->count();
220820b62dfSGreg Roach
2218d0ebef0SGreg Roach        foreach ($this->facts(['NCHI']) as $fact) {
22284586c02SGreg Roach            $nchi = max($nchi, (int) $fact->value());
223a25f0a04SGreg Roach        }
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach        return $nchi;
226a25f0a04SGreg Roach    }
227a25f0a04SGreg Roach
228a25f0a04SGreg Roach    /**
229a25f0a04SGreg Roach     * get the marriage event
230a25f0a04SGreg Roach     */
2311ff45046SGreg Roach    public function getMarriage(): Fact|null
232c1010edaSGreg Roach    {
233820b62dfSGreg Roach        return $this->facts(['MARR'])->first();
234a25f0a04SGreg Roach    }
235a25f0a04SGreg Roach
236a25f0a04SGreg Roach    /**
237a25f0a04SGreg Roach     * Get marriage date
238a25f0a04SGreg Roach     *
239a25f0a04SGreg Roach     * @return Date
240a25f0a04SGreg Roach     */
241820b62dfSGreg Roach    public function getMarriageDate(): Date
242c1010edaSGreg Roach    {
243a25f0a04SGreg Roach        $marriage = $this->getMarriage();
244b6ec1ccfSGreg Roach        if ($marriage instanceof Fact) {
2452decada7SGreg Roach            return $marriage->date();
246a25f0a04SGreg Roach        }
247b2ce94c6SRico Sonntag
248b2ce94c6SRico Sonntag        return new Date('');
249a25f0a04SGreg Roach    }
250a25f0a04SGreg Roach
251a25f0a04SGreg Roach    /**
252a25f0a04SGreg Roach     * Get the marriage year - displayed on lists of families
253a25f0a04SGreg Roach     *
254cbc1590aSGreg Roach     * @return int
255a25f0a04SGreg Roach     */
2568f53f488SRico Sonntag    public function getMarriageYear(): int
257c1010edaSGreg Roach    {
2584a83f5d7SGreg Roach        return $this->getMarriageDate()->minimumDate()->year;
259a25f0a04SGreg Roach    }
260a25f0a04SGreg Roach
261a25f0a04SGreg Roach    /**
262a25f0a04SGreg Roach     * Get the marriage place
263a25f0a04SGreg Roach     *
264a25f0a04SGreg Roach     * @return Place
265a25f0a04SGreg Roach     */
2668f53f488SRico Sonntag    public function getMarriagePlace(): Place
267c1010edaSGreg Roach    {
268a25f0a04SGreg Roach        $marriage = $this->getMarriage();
269a25f0a04SGreg Roach
2707abafad0SGreg Roach        if ($marriage instanceof Fact) {
2714fb14fcbSGreg Roach            return $marriage->place();
272a25f0a04SGreg Roach        }
273a25f0a04SGreg Roach
2747abafad0SGreg Roach        return new Place('', $this->tree);
2757abafad0SGreg Roach    }
2767abafad0SGreg Roach
277a25f0a04SGreg Roach    /**
278a25f0a04SGreg Roach     * Get a list of all marriage dates - for the family lists.
279a25f0a04SGreg Roach     *
28009482a55SGreg Roach     * @return array<Date>
281a25f0a04SGreg Roach     */
2828f53f488SRico Sonntag    public function getAllMarriageDates(): array
283c1010edaSGreg Roach    {
2848d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
2857abafad0SGreg Roach            $array = $this->getAllEventDates([$event]);
2868af3e5c1SGreg Roach
28754c1ab5eSGreg Roach            if ($array !== []) {
288a25f0a04SGreg Roach                return $array;
289a25f0a04SGreg Roach            }
290a25f0a04SGreg Roach        }
291a25f0a04SGreg Roach
29213abd6f3SGreg Roach        return [];
293a25f0a04SGreg Roach    }
294a25f0a04SGreg Roach
295a25f0a04SGreg Roach    /**
296a25f0a04SGreg Roach     * Get a list of all marriage places - for the family lists.
297a25f0a04SGreg Roach     *
29809482a55SGreg Roach     * @return array<Place>
299a25f0a04SGreg Roach     */
3008f53f488SRico Sonntag    public function getAllMarriagePlaces(): array
301c1010edaSGreg Roach    {
3028d0ebef0SGreg Roach        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
3036e83554dSGreg Roach            $places = $this->getAllEventPlaces([$event]);
30454c1ab5eSGreg Roach            if ($places !== []) {
3054080d558SGreg Roach                return $places;
306a25f0a04SGreg Roach            }
307a25f0a04SGreg Roach        }
308a25f0a04SGreg Roach
30913abd6f3SGreg Roach        return [];
310a25f0a04SGreg Roach    }
311a25f0a04SGreg Roach
31276692c8bSGreg Roach    /**
31376692c8bSGreg Roach     * Derived classes should redefine this function, otherwise the object will have no name
31476692c8bSGreg Roach     *
315870ec5a3SGreg Roach     * @return array<int,array<string,string>>
31676692c8bSGreg Roach     */
3178f53f488SRico Sonntag    public function getAllNames(): array
318c1010edaSGreg Roach    {
3199599771eSGreg Roach        if ($this->getAllNames === []) {
320a25f0a04SGreg Roach            // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
321e88674d4SGreg Roach            $husb_names = [];
322b6ec1ccfSGreg Roach            if ($this->husb instanceof Individual) {
323f25fc0f9SGreg Roach                $husb_names = array_filter($this->husb->getAllNames(), static fn (array $x): bool => $x['type'] !== '_MARNM');
324e88674d4SGreg Roach            }
3251f244d77SGreg Roach            // If the individual only has married names, create a fake birth name.
32654c1ab5eSGreg Roach            if ($husb_names === []) {
327e88674d4SGreg Roach                $husb_names[] = [
328a25f0a04SGreg Roach                    'type' => 'BIRT',
3298fb4e87cSGreg Roach                    'sort' => Individual::NOMEN_NESCIO,
330ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
33113abd6f3SGreg Roach                ];
332a25f0a04SGreg Roach            }
333a25f0a04SGreg Roach            foreach ($husb_names as $n => $husb_name) {
334a25f0a04SGreg Roach                $husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
335a25f0a04SGreg Roach            }
336e88674d4SGreg Roach
337e88674d4SGreg Roach            $wife_names = [];
338b6ec1ccfSGreg Roach            if ($this->wife instanceof Individual) {
339f25fc0f9SGreg Roach                $wife_names = array_filter($this->wife->getAllNames(), static fn (array $x): bool => $x['type'] !== '_MARNM');
340e88674d4SGreg Roach            }
3411f244d77SGreg Roach            // If the individual only has married names, create a fake birth name.
34254c1ab5eSGreg Roach            if ($wife_names === []) {
343e88674d4SGreg Roach                $wife_names[] = [
344a25f0a04SGreg Roach                    'type' => 'BIRT',
3458fb4e87cSGreg Roach                    'sort' => Individual::NOMEN_NESCIO,
346ad1a1cd2SGreg Roach                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
34713abd6f3SGreg Roach                ];
348a25f0a04SGreg Roach            }
349a25f0a04SGreg Roach            foreach ($wife_names as $n => $wife_name) {
350a25f0a04SGreg Roach                $wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
351a25f0a04SGreg Roach            }
352e88674d4SGreg Roach
353a25f0a04SGreg Roach            // Add the matched names first
354a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
355a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
356e364afe4SGreg Roach                    if ($husb_name['script'] === $wife_name['script']) {
357bdb3725aSGreg Roach                        $this->getAllNames[] = [
358a25f0a04SGreg Roach                            'type' => $husb_name['type'],
359a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
360a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
361a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
36213abd6f3SGreg Roach                        ];
363a25f0a04SGreg Roach                    }
364a25f0a04SGreg Roach                }
365a25f0a04SGreg Roach            }
366e88674d4SGreg Roach
367a25f0a04SGreg Roach            // Add the unmatched names second (there may be no matched names)
368a25f0a04SGreg Roach            foreach ($husb_names as $husb_name) {
369a25f0a04SGreg Roach                foreach ($wife_names as $wife_name) {
370e364afe4SGreg Roach                    if ($husb_name['script'] !== $wife_name['script']) {
371bdb3725aSGreg Roach                        $this->getAllNames[] = [
372a25f0a04SGreg Roach                            'type' => $husb_name['type'],
373a25f0a04SGreg Roach                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
374a25f0a04SGreg Roach                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
375a25f0a04SGreg Roach                            // No need for a fullNN entry - we do not currently store FAM names in the database
37613abd6f3SGreg Roach                        ];
377a25f0a04SGreg Roach                    }
378a25f0a04SGreg Roach                }
379a25f0a04SGreg Roach            }
380a25f0a04SGreg Roach        }
381a25f0a04SGreg Roach
382bdb3725aSGreg Roach        return $this->getAllNames;
383a25f0a04SGreg Roach    }
384a25f0a04SGreg Roach
38576692c8bSGreg Roach    /**
38676692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
38776692c8bSGreg Roach     * identifying characteristics of this record.
38876692c8bSGreg Roach     *
38976692c8bSGreg Roach     * @return string
39076692c8bSGreg Roach     */
3918f53f488SRico Sonntag    public function formatListDetails(): string
392c1010edaSGreg Roach    {
393a25f0a04SGreg Roach        return
3948d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) .
3958d0ebef0SGreg Roach            $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1);
396a25f0a04SGreg Roach    }
3978091bfd1SGreg Roach
3988091bfd1SGreg Roach    /**
3998091bfd1SGreg Roach     * Lock the database row, to prevent concurrent edits.
4008091bfd1SGreg Roach     */
4018091bfd1SGreg Roach    public function lock(): void
4028091bfd1SGreg Roach    {
4038091bfd1SGreg Roach        DB::table('families')
4048091bfd1SGreg Roach            ->where('f_file', '=', $this->tree->id())
4058091bfd1SGreg Roach            ->where('f_id', '=', $this->xref())
4068091bfd1SGreg Roach            ->lockForUpdate()
4078091bfd1SGreg Roach            ->get();
4088091bfd1SGreg Roach    }
409a25f0a04SGreg Roach}
410