Issues (2491)

app/Family.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees;
21
22
use Closure;
23
use Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage;
24
use Illuminate\Support\Collection;
25
26
/**
27
 * A GEDCOM family (FAM) object.
28
 */
29
class Family extends GedcomRecord
30
{
31
    public const string RECORD_TYPE = 'FAM';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 31 at column 24
Loading history...
32
33
    protected const string ROUTE_NAME = FamilyPage::class;
34
35
    // The husband (or first spouse for same-sex couples)
36
    private Individual|null $husb = null;
37
38
    // The wife (or second spouse for same-sex couples)
39
    private Individual|null $wife = null;
40
41
    /**
42
     * Create a GedcomRecord object from raw GEDCOM data.
43
     *
44
     * @param string      $xref
45
     * @param string      $gedcom  an empty string for new/pending records
46
     * @param string|null $pending null for a record with no pending edits,
47
     *                             empty string for records with pending deletions
48
     * @param Tree        $tree
49
     */
50
    public function __construct(string $xref, string $gedcom, string|null $pending, Tree $tree)
51
    {
52
        parent::__construct($xref, $gedcom, $pending, $tree);
53
54
        // Make sure we find records in pending records.
55
        $gedcom_pending = $gedcom . "\n" . $pending;
56
57
        if (preg_match('/\n1 HUSB @(.+)@/', $gedcom_pending, $match)) {
58
            $this->husb = Registry::individualFactory()->make($match[1], $tree);
59
        }
60
        if (preg_match('/\n1 WIFE @(.+)@/', $gedcom_pending, $match)) {
61
            $this->wife = Registry::individualFactory()->make($match[1], $tree);
62
        }
63
    }
64
65
    /**
66
     * A closure which will compare families by marriage date.
67
     *
68
     * @return Closure(Family,Family):int
69
     */
70
    public static function marriageDateComparator(): Closure
71
    {
72
        return static fn (Family $x, Family $y): int => Date::compare($x->getMarriageDate(), $y->getMarriageDate());
73
    }
74
75
    /**
76
     * Get the male (or first female) partner of the family
77
     *
78
     * @param int|null $access_level
79
     *
80
     * @return Individual|null
81
     */
82
    public function husband(int|null $access_level = null): Individual|null
83
    {
84
        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
85
            $access_level = Auth::PRIV_HIDE;
86
        }
87
88
        if ($this->husb instanceof Individual && $this->husb->canShowName($access_level)) {
89
            return $this->husb;
90
        }
91
92
        return null;
93
    }
94
95
    /**
96
     * Get the female (or second male) partner of the family
97
     *
98
     * @param int|null $access_level
99
     *
100
     * @return Individual|null
101
     */
102
    public function wife(int|null $access_level = null): Individual|null
103
    {
104
        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
105
            $access_level = Auth::PRIV_HIDE;
106
        }
107
108
        if ($this->wife instanceof Individual && $this->wife->canShowName($access_level)) {
109
            return $this->wife;
110
        }
111
112
        return null;
113
    }
114
115
    /**
116
     * Each object type may have its own special rules, and re-implement this function.
117
     *
118
     * @param int $access_level
119
     *
120
     * @return bool
121
     */
122
    protected function canShowByType(int $access_level): bool
123
    {
124
        // Hide a family if any member is private
125
        preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches);
126
        foreach ($matches[1] as $match) {
127
            $individual = Registry::individualFactory()->make($match, $this->tree);
128
129
            if ($individual instanceof Individual && !$individual->canShow($access_level)) {
130
                return false;
131
            }
132
        }
133
134
        return true;
135
    }
136
137
    /**
138
     * Can the name of this record be shown?
139
     *
140
     * @param int|null $access_level
141
     *
142
     * @return bool
143
     */
144
    public function canShowName(int|null $access_level = null): bool
145
    {
146
        // We can always see the name (Husband-name + Wife-name), however,
147
        // the name will often be "private + private"
148
        return true;
149
    }
150
151
    /**
152
     * Find the spouse of a person.
153
     *
154
     * @param Individual $person
155
     * @param int|null   $access_level
156
     *
157
     * @return Individual|null
158
     */
159
    public function spouse(Individual $person, int|null $access_level = null): Individual|null
160
    {
161
        if ($person === $this->wife) {
162
            return $this->husband($access_level);
163
        }
164
165
        return $this->wife($access_level);
166
    }
167
168
    /**
169
     * Get the (zero, one or two) spouses from this family.
170
     *
171
     * @param int|null $access_level
172
     *
173
     * @return Collection<int,Individual>
174
     */
175
    public function spouses(int|null $access_level = null): Collection
176
    {
177
        $spouses = new Collection([
178
            $this->husband($access_level),
179
            $this->wife($access_level),
180
        ]);
181
182
        return $spouses->filter();
183
    }
184
185
    /**
186
     * Get a list of this family’s children.
187
     *
188
     * @param int|null $access_level
189
     *
190
     * @return Collection<int,Individual>
191
     */
192
    public function children(int|null $access_level = null): Collection
193
    {
194
        $access_level ??= Auth::accessLevel($this->tree);
195
196
        if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
197
            $access_level = Auth::PRIV_HIDE;
198
        }
199
200
        $children = new Collection();
201
202
        foreach ($this->facts(['CHIL'], false, $access_level) as $fact) {
203
            $child = $fact->target();
204
205
            if ($child instanceof Individual && $child->canShowName($access_level)) {
206
                $children->push($child);
207
            }
208
        }
209
210
        return $children;
211
    }
212
213
    /**
214
     * Number of children - for the individual list
215
     *
216
     * @return int
217
     */
218
    public function numberOfChildren(): int
219
    {
220
        $nchi = $this->children()->count();
221
222
        foreach ($this->facts(['NCHI']) as $fact) {
223
            $nchi = max($nchi, (int) $fact->value());
224
        }
225
226
        return $nchi;
227
    }
228
229
    /**
230
     * get the marriage event
231
     */
232
    public function getMarriage(): Fact|null
233
    {
234
        return $this->facts(['MARR'])->first();
235
    }
236
237
    /**
238
     * Get marriage date
239
     *
240
     * @return Date
241
     */
242
    public function getMarriageDate(): Date
243
    {
244
        $marriage = $this->getMarriage();
245
        if ($marriage instanceof Fact) {
246
            return $marriage->date();
247
        }
248
249
        return new Date('');
250
    }
251
252
    /**
253
     * Get the marriage year - displayed on lists of families
254
     *
255
     * @return int
256
     */
257
    public function getMarriageYear(): int
258
    {
259
        return $this->getMarriageDate()->minimumDate()->year;
260
    }
261
262
    /**
263
     * Get the marriage place
264
     *
265
     * @return Place
266
     */
267
    public function getMarriagePlace(): Place
268
    {
269
        $marriage = $this->getMarriage();
270
271
        if ($marriage instanceof Fact) {
272
            return $marriage->place();
273
        }
274
275
        return new Place('', $this->tree);
276
    }
277
278
    /**
279
     * Get a list of all marriage dates - for the family lists.
280
     *
281
     * @return array<Date>
282
     */
283
    public function getAllMarriageDates(): array
284
    {
285
        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
286
            $array = $this->getAllEventDates([$event]);
287
288
            if ($array !== []) {
289
                return $array;
290
            }
291
        }
292
293
        return [];
294
    }
295
296
    /**
297
     * Get a list of all marriage places - for the family lists.
298
     *
299
     * @return array<Place>
300
     */
301
    public function getAllMarriagePlaces(): array
302
    {
303
        foreach (Gedcom::MARRIAGE_EVENTS as $event) {
304
            $places = $this->getAllEventPlaces([$event]);
305
            if ($places !== []) {
306
                return $places;
307
            }
308
        }
309
310
        return [];
311
    }
312
313
    /**
314
     * Derived classes should redefine this function, otherwise the object will have no name
315
     *
316
     * @return array<int,array<string,string>>
317
     */
318
    public function getAllNames(): array
319
    {
320
        if ($this->getAllNames === []) {
321
            // Check the script used by each name, so we can match cyrillic with cyrillic, greek with greek, etc.
322
            $husb_names = [];
323
            if ($this->husb instanceof Individual) {
324
                $husb_names = $this->husb->getAllNames();
325
            }
326
            // use Nomen Nescio when no name is known
327
            if ($husb_names === []) {
328
                $husb_names[] = [
329
                    'type' => 'BIRT',
330
                    'sort' => Individual::NOMEN_NESCIO,
331
                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
332
                ];
333
            }
334
            foreach ($husb_names as $n => $husb_name) {
335
                $husb_names[$n]['script'] = I18N::textScript($husb_name['full']);
336
            }
337
338
            $wife_names = [];
339
            if ($this->wife instanceof Individual) {
340
                $wife_names = $this->wife->getAllNames();
341
            }
342
            // use Nomen Nescio when no name is known
343
            if ($wife_names === []) {
344
                $wife_names[] = [
345
                    'type' => 'BIRT',
346
                    'sort' => Individual::NOMEN_NESCIO,
347
                    'full' => I18N::translateContext('Unknown given name', '…') . ' ' . I18N::translateContext('Unknown surname', '…'),
348
                ];
349
            }
350
            foreach ($wife_names as $n => $wife_name) {
351
                $wife_names[$n]['script'] = I18N::textScript($wife_name['full']);
352
            }
353
354
            // Add the matched names first
355
            foreach ($husb_names as $husb_name) {
356
                foreach ($wife_names as $wife_name) {
357
                    if ($husb_name['script'] === $wife_name['script']) {
358
                        $this->getAllNames[] = [
359
                            'type' => $husb_name['type'],
360
                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
361
                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
362
                            // No need for a fullNN entry - we do not currently store FAM names in the database
363
                        ];
364
                    }
365
                }
366
            }
367
368
            // Add the unmatched names second (there may be no matched names)
369
            foreach ($husb_names as $husb_name) {
370
                foreach ($wife_names as $wife_name) {
371
                    if ($husb_name['script'] !== $wife_name['script']) {
372
                        $this->getAllNames[] = [
373
                            'type' => $husb_name['type'],
374
                            'sort' => $husb_name['sort'] . ' + ' . $wife_name['sort'],
375
                            'full' => $husb_name['full'] . ' + ' . $wife_name['full'],
376
                            // No need for a fullNN entry - we do not currently store FAM names in the database
377
                        ];
378
                    }
379
                }
380
            }
381
        }
382
383
        return $this->getAllNames;
384
    }
385
386
    /**
387
     * This function should be redefined in derived classes to show any major
388
     * identifying characteristics of this record.
389
     *
390
     * @return string
391
     */
392
    public function formatListDetails(): string
393
    {
394
        return
395
            $this->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 1) .
396
            $this->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 1);
397
    }
398
399
    /**
400
     * Lock the database row, to prevent concurrent edits.
401
     */
402
    public function lock(): void
403
    {
404
        DB::table('families')
405
            ->where('f_file', '=', $this->tree->id())
406
            ->where('f_id', '=', $this->xref())
407
            ->lockForUpdate()
408
            ->get();
409
    }
410
}
411