xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 3f810b5b8127b30ab770dcf66ea1875ff8823633)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Date;
24use Fisharebest\Webtrees\Fact;
25use Fisharebest\Webtrees\Family;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Services\ClipboardService;
29use Fisharebest\Webtrees\Services\LinkedRecordService;
30use Fisharebest\Webtrees\Services\ModuleService;
31use Illuminate\Support\Collection;
32
33use function explode;
34use function preg_match;
35use function preg_replace;
36use function str_contains;
37use function str_replace;
38use function view;
39
40/**
41 * Class IndividualFactsTabModule
42 */
43class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
44{
45    use ModuleTabTrait;
46
47    private ClipboardService $clipboard_service;
48
49    private LinkedRecordService $linked_record_service;
50
51    private ModuleService $module_service;
52
53    /**
54     * IndividualFactsTabModule constructor.
55     *
56     * @param ModuleService       $module_service
57     * @param LinkedRecordService $linked_record_service
58     * @param ClipboardService    $clipboard_service
59     */
60    public function __construct(ModuleService $module_service, LinkedRecordService $linked_record_service, ClipboardService $clipboard_service)
61    {
62        $this->clipboard_service     = $clipboard_service;
63        $this->linked_record_service = $linked_record_service;
64        $this->module_service        = $module_service;
65    }
66
67    /**
68     * How should this module be identified in the control panel, etc.?
69     *
70     * @return string
71     */
72    public function title(): string
73    {
74        /* I18N: Name of a module/tab on the individual page. */
75        return I18N::translate('Facts and events');
76    }
77
78    /**
79     * A sentence describing what this module does.
80     *
81     * @return string
82     */
83    public function description(): string
84    {
85        /* I18N: Description of the “Facts and events” module */
86        return I18N::translate('A tab showing the facts and events of an individual.');
87    }
88
89    /**
90     * The default position for this tab.  It can be changed in the control panel.
91     *
92     * @return int
93     */
94    public function defaultTabOrder(): int
95    {
96        return 1;
97    }
98
99    /**
100     * A greyed out tab has no actual content, but may perhaps have
101     * options to create content.
102     *
103     * @param Individual $individual
104     *
105     * @return bool
106     */
107    public function isGrayedOut(Individual $individual): bool
108    {
109        return false;
110    }
111
112    /**
113     * Generate the HTML content of this tab.
114     *
115     * @param Individual $individual
116     *
117     * @return string
118     */
119    public function getTabContent(Individual $individual): string
120    {
121        // Only include events of close relatives that are between birth and death
122        $min_date = $individual->getEstimatedBirthDate();
123        $max_date = $individual->getEstimatedDeathDate();
124
125        // Which facts and events are handled by other modules?
126        $sidebar_facts = $this->module_service
127            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
128            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts());
129
130        $tab_facts = $this->module_service
131            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
132            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts());
133
134        $exclude_facts = $sidebar_facts->merge($tab_facts)->flatten();
135
136        // The individual’s own facts
137        $individual_facts = $individual->facts()
138            ->filter(fn (Fact $fact): bool => !$exclude_facts->contains($fact->tag()));
139
140        $relative_facts = new Collection();
141
142        // Add spouse-family facts
143        foreach ($individual->spouseFamilies() as $family) {
144            foreach ($family->facts() as $fact) {
145                if (!$exclude_facts->contains($fact->tag()) && $fact->tag() !== 'FAM:CHAN') {
146                    $relative_facts->push($fact);
147                }
148            }
149
150            $spouse = $family->spouse($individual);
151
152            if ($spouse instanceof Individual) {
153                $spouse_facts   = $this->spouseFacts($individual, $spouse, $min_date, $max_date);
154                $relative_facts = $relative_facts->merge($spouse_facts);
155            }
156
157            $child_facts    = $this->childFacts($individual, $family, '_CHIL', '', $min_date, $max_date);
158            $relative_facts = $relative_facts->merge($child_facts);
159        }
160
161        $parent_facts    = $this->parentFacts($individual, 1, $min_date, $max_date);
162        $relative_facts  = $relative_facts->merge($parent_facts);
163        $associate_facts = $this->associateFacts($individual);
164        $historic_facts  = $this->historicFacts($individual);
165
166        $individual_facts = $individual_facts
167            ->merge($associate_facts)
168            ->merge($historic_facts)
169            ->merge($relative_facts);
170
171        $individual_facts = Fact::sortFacts($individual_facts);
172
173        return view('modules/personal_facts/tab', [
174            'can_edit'            => $individual->canEdit(),
175            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
176            'has_associate_facts' => $associate_facts->isNotEmpty(),
177            'has_historic_facts'  => $historic_facts->isNotEmpty(),
178            'has_relative_facts'  => $relative_facts->isNotEmpty(),
179            'individual'          => $individual,
180            'facts'               => $individual_facts,
181        ]);
182    }
183
184    /**
185     * Spouse facts that are shown on an individual’s page.
186     *
187     * @param Individual $individual Show events that occured during the lifetime of this individual
188     * @param Individual $spouse     Show events of this individual
189     * @param Date       $min_date
190     * @param Date       $max_date
191     *
192     * @return Collection<int,Fact>
193     */
194    protected function spouseFacts(Individual $individual, Individual $spouse, Date $min_date, Date $max_date): Collection
195    {
196        $SHOW_RELATIVES_EVENTS = $individual->tree()->getPreference('SHOW_RELATIVES_EVENTS');
197
198        $death_of_a_spouse = [
199            'INDI:DEAT' => [
200                'M' => I18N::translate('Death of a husband'),
201                'F' => I18N::translate('Death of a wife'),
202                'U' => I18N::translate('Death of a spouse'),
203            ],
204            'INDI:BURI' => [
205                'M' => I18N::translate('Burial of a husband'),
206                'F' => I18N::translate('Burial of a wife'),
207                'U' => I18N::translate('Burial of a spouse'),
208            ],
209            'INDI:CREM' => [
210                'M' => I18N::translate('Cremation of a husband'),
211                'F' => I18N::translate('Cremation of a wife'),
212                'U' => I18N::translate('Cremation of a spouse'),
213            ],
214        ];
215
216        $facts = new Collection();
217
218        if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT_SPOU')) {
219            foreach ($spouse->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
220                if ($this->includeFact($fact, $min_date, $max_date)) {
221                    $facts[] = $this->convertEvent($fact, $death_of_a_spouse[$fact->tag()], $fact->record()->sex());
222                }
223            }
224        }
225
226        return $facts;
227    }
228
229    /**
230     * Does a relative event occur within a date range (i.e. the individual's lifetime)?
231     *
232     * @param Fact $fact
233     * @param Date $min_date
234     * @param Date $max_date
235     *
236     * @return bool
237     */
238    protected function includeFact(Fact $fact, Date $min_date, Date $max_date): bool
239    {
240        $fact_date = $fact->date();
241
242        return $fact_date->isOK() && Date::compare($min_date, $fact_date) <= 0 && Date::compare($fact_date, $max_date) <= 0;
243    }
244
245    /**
246     * Convert an event into a special "event of a close relative".
247     *
248     * @param Fact          $fact
249     * @param array<string> $types
250     * @param string        $sex
251     *
252     * @return Fact
253     */
254    protected function convertEvent(Fact $fact, array $types, string $sex): Fact
255    {
256        $type = $types[$sex] ?? $types['U'];
257
258        $gedcom = $fact->gedcom();
259        $gedcom = preg_replace('/\n2 TYPE .*/', '', $gedcom);
260        $gedcom = preg_replace('/^1 .*/', "1 EVEN CLOSE_RELATIVE\n2 TYPE " . $type, $gedcom);
261
262        $converted = new Fact($gedcom, $fact->record(), $fact->id());
263
264        if ($fact->isPendingAddition()) {
265            $converted->setPendingAddition();
266        }
267
268        if ($fact->isPendingDeletion()) {
269            $converted->setPendingDeletion();
270        }
271
272        return $converted;
273    }
274
275    /**
276     * Get the events of children and grandchildren.
277     *
278     * @param Individual $person
279     * @param Family     $family
280     * @param string     $option
281     * @param string     $relation
282     * @param Date       $min_date
283     * @param Date       $max_date
284     *
285     * @return Collection<int,Fact>
286     */
287    protected function childFacts(Individual $person, Family $family, string $option, string $relation, Date $min_date, Date $max_date): Collection
288    {
289        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
290
291        $birth_of_a_child = [
292            'INDI:BIRT' => [
293                'M' => I18N::translate('Birth of a son'),
294                'F' => I18N::translate('Birth of a daughter'),
295                'U' => I18N::translate('Birth of a child'),
296            ],
297            'INDI:CHR'  => [
298                'M' => I18N::translate('Christening of a son'),
299                'F' => I18N::translate('Christening of a daughter'),
300                'U' => I18N::translate('Christening of a child'),
301            ],
302            'INDI:BAPM' => [
303                'M' => I18N::translate('Baptism of a son'),
304                'F' => I18N::translate('Baptism of a daughter'),
305                'U' => I18N::translate('Baptism of a child'),
306            ],
307            'INDI:ADOP' => [
308                'M' => I18N::translate('Adoption of a son'),
309                'F' => I18N::translate('Adoption of a daughter'),
310                'U' => I18N::translate('Adoption of a child'),
311            ],
312        ];
313
314        $birth_of_a_sibling = [
315            'INDI:BIRT' => [
316                'M' => I18N::translate('Birth of a brother'),
317                'F' => I18N::translate('Birth of a sister'),
318                'U' => I18N::translate('Birth of a sibling'),
319            ],
320            'INDI:CHR'  => [
321                'M' => I18N::translate('Christening of a brother'),
322                'F' => I18N::translate('Christening of a sister'),
323                'U' => I18N::translate('Christening of a sibling'),
324            ],
325            'INDI:BAPM' => [
326                'M' => I18N::translate('Baptism of a brother'),
327                'F' => I18N::translate('Baptism of a sister'),
328                'U' => I18N::translate('Baptism of a sibling'),
329            ],
330            'INDI:ADOP' => [
331                'M' => I18N::translate('Adoption of a brother'),
332                'F' => I18N::translate('Adoption of a sister'),
333                'U' => I18N::translate('Adoption of a sibling'),
334            ],
335        ];
336
337        $birth_of_a_half_sibling = [
338            'INDI:BIRT' => [
339                'M' => I18N::translate('Birth of a half-brother'),
340                'F' => I18N::translate('Birth of a half-sister'),
341                'U' => I18N::translate('Birth of a half-sibling'),
342            ],
343            'INDI:CHR'  => [
344                'M' => I18N::translate('Christening of a half-brother'),
345                'F' => I18N::translate('Christening of a half-sister'),
346                'U' => I18N::translate('Christening of a half-sibling'),
347            ],
348            'INDI:BAPM' => [
349                'M' => I18N::translate('Baptism of a half-brother'),
350                'F' => I18N::translate('Baptism of a half-sister'),
351                'U' => I18N::translate('Baptism of a half-sibling'),
352            ],
353            'INDI:ADOP' => [
354                'M' => I18N::translate('Adoption of a half-brother'),
355                'F' => I18N::translate('Adoption of a half-sister'),
356                'U' => I18N::translate('Adoption of a half-sibling'),
357            ],
358        ];
359
360        $birth_of_a_grandchild = [
361            'INDI:BIRT' => [
362                'M' => I18N::translate('Birth of a grandson'),
363                'F' => I18N::translate('Birth of a granddaughter'),
364                'U' => I18N::translate('Birth of a grandchild'),
365            ],
366            'INDI:CHR'  => [
367                'M' => I18N::translate('Christening of a grandson'),
368                'F' => I18N::translate('Christening of a granddaughter'),
369                'U' => I18N::translate('Christening of a grandchild'),
370            ],
371            'INDI:BAPM' => [
372                'M' => I18N::translate('Baptism of a grandson'),
373                'F' => I18N::translate('Baptism of a granddaughter'),
374                'U' => I18N::translate('Baptism of a grandchild'),
375            ],
376            'INDI:ADOP' => [
377                'M' => I18N::translate('Adoption of a grandson'),
378                'F' => I18N::translate('Adoption of a granddaughter'),
379                'U' => I18N::translate('Adoption of a grandchild'),
380            ],
381        ];
382
383        $birth_of_a_grandchild1 = [
384            'INDI:BIRT' => [
385                'M' => I18N::translateContext('daughter’s son', 'Birth of a grandson'),
386                'F' => I18N::translateContext('daughter’s daughter', 'Birth of a granddaughter'),
387                'U' => I18N::translate('Birth of a grandchild'),
388            ],
389            'INDI:CHR'  => [
390                'M' => I18N::translateContext('daughter’s son', 'Christening of a grandson'),
391                'F' => I18N::translateContext('daughter’s daughter', 'Christening of a granddaughter'),
392                'U' => I18N::translate('Christening of a grandchild'),
393            ],
394            'INDI:BAPM' => [
395                'M' => I18N::translateContext('daughter’s son', 'Baptism of a grandson'),
396                'F' => I18N::translateContext('daughter’s daughter', 'Baptism of a granddaughter'),
397                'U' => I18N::translate('Baptism of a grandchild'),
398            ],
399            'INDI:ADOP' => [
400                'M' => I18N::translateContext('daughter’s son', 'Adoption of a grandson'),
401                'F' => I18N::translateContext('daughter’s daughter', 'Adoption of a granddaughter'),
402                'U' => I18N::translate('Adoption of a grandchild'),
403            ],
404        ];
405
406        $birth_of_a_grandchild2 = [
407            'INDI:BIRT' => [
408                'M' => I18N::translateContext('son’s son', 'Birth of a grandson'),
409                'F' => I18N::translateContext('son’s daughter', 'Birth of a granddaughter'),
410                'U' => I18N::translate('Birth of a grandchild'),
411            ],
412            'INDI:CHR'  => [
413                'M' => I18N::translateContext('son’s son', 'Christening of a grandson'),
414                'F' => I18N::translateContext('son’s daughter', 'Christening of a granddaughter'),
415                'U' => I18N::translate('Christening of a grandchild'),
416            ],
417            'INDI:BAPM' => [
418                'M' => I18N::translateContext('son’s son', 'Baptism of a grandson'),
419                'F' => I18N::translateContext('son’s daughter', 'Baptism of a granddaughter'),
420                'U' => I18N::translate('Baptism of a grandchild'),
421            ],
422            'INDI:ADOP' => [
423                'M' => I18N::translateContext('son’s son', 'Adoption of a grandson'),
424                'F' => I18N::translateContext('son’s daughter', 'Adoption of a granddaughter'),
425                'U' => I18N::translate('Adoption of a grandchild'),
426            ],
427        ];
428
429        $death_of_a_child = [
430            'INDI:DEAT' => [
431                'M' => I18N::translate('Death of a son'),
432                'F' => I18N::translate('Death of a daughter'),
433                'U' => I18N::translate('Death of a child'),
434            ],
435            'INDI:BURI' => [
436                'M' => I18N::translate('Burial of a son'),
437                'F' => I18N::translate('Burial of a daughter'),
438                'U' => I18N::translate('Burial of a child'),
439            ],
440            'INDI:CREM' => [
441                'M' => I18N::translate('Cremation of a son'),
442                'F' => I18N::translate('Cremation of a daughter'),
443                'U' => I18N::translate('Cremation of a child'),
444            ],
445        ];
446
447        $death_of_a_sibling = [
448            'INDI:DEAT' => [
449                'M' => I18N::translate('Death of a brother'),
450                'F' => I18N::translate('Death of a sister'),
451                'U' => I18N::translate('Death of a sibling'),
452            ],
453            'INDI:BURI' => [
454                'M' => I18N::translate('Burial of a brother'),
455                'F' => I18N::translate('Burial of a sister'),
456                'U' => I18N::translate('Burial of a sibling'),
457            ],
458            'INDI:CREM' => [
459                'M' => I18N::translate('Cremation of a brother'),
460                'F' => I18N::translate('Cremation of a sister'),
461                'U' => I18N::translate('Cremation of a sibling'),
462            ],
463        ];
464
465        $death_of_a_half_sibling = [
466            'INDI:DEAT' => [
467                'M' => I18N::translate('Death of a half-brother'),
468                'F' => I18N::translate('Death of a half-sister'),
469                'U' => I18N::translate('Death of a half-sibling'),
470            ],
471            'INDI:BURI' => [
472                'M' => I18N::translate('Burial of a half-brother'),
473                'F' => I18N::translate('Burial of a half-sister'),
474                'U' => I18N::translate('Burial of a half-sibling'),
475            ],
476            'INDI:CREM' => [
477                'M' => I18N::translate('Cremation of a half-brother'),
478                'F' => I18N::translate('Cremation of a half-sister'),
479                'U' => I18N::translate('Cremation of a half-sibling'),
480            ],
481        ];
482
483        $death_of_a_grandchild = [
484            'INDI:DEAT' => [
485                'M' => I18N::translate('Death of a grandson'),
486                'F' => I18N::translate('Death of a granddaughter'),
487                'U' => I18N::translate('Death of a grandchild'),
488            ],
489            'INDI:BURI' => [
490                'M' => I18N::translate('Burial of a grandson'),
491                'F' => I18N::translate('Burial of a granddaughter'),
492                'U' => I18N::translate('Burial of a grandchild'),
493            ],
494            'INDI:CREM' => [
495                'M' => I18N::translate('Cremation of a grandson'),
496                'F' => I18N::translate('Cremation of a granddaughter'),
497                'U' => I18N::translate('Baptism of a grandchild'),
498            ],
499        ];
500
501        $death_of_a_grandchild1 = [
502            'INDI:DEAT' => [
503                'M' => I18N::translateContext('daughter’s son', 'Death of a grandson'),
504                'F' => I18N::translateContext('daughter’s daughter', 'Death of a granddaughter'),
505                'U' => I18N::translate('Death of a grandchild'),
506            ],
507            'INDI:BURI' => [
508                'M' => I18N::translateContext('daughter’s son', 'Burial of a grandson'),
509                'F' => I18N::translateContext('daughter’s daughter', 'Burial of a granddaughter'),
510                'U' => I18N::translate('Burial of a grandchild'),
511            ],
512            'INDI:CREM' => [
513                'M' => I18N::translateContext('daughter’s son', 'Cremation of a grandson'),
514                'F' => I18N::translateContext('daughter’s daughter', 'Cremation of a granddaughter'),
515                'U' => I18N::translate('Baptism of a grandchild'),
516            ],
517        ];
518
519        $death_of_a_grandchild2 = [
520            'INDI:DEAT' => [
521                'M' => I18N::translateContext('son’s son', 'Death of a grandson'),
522                'F' => I18N::translateContext('son’s daughter', 'Death of a granddaughter'),
523                'U' => I18N::translate('Death of a grandchild'),
524            ],
525            'INDI:BURI' => [
526                'M' => I18N::translateContext('son’s son', 'Burial of a grandson'),
527                'F' => I18N::translateContext('son’s daughter', 'Burial of a granddaughter'),
528                'U' => I18N::translate('Burial of a grandchild'),
529            ],
530            'INDI:CREM' => [
531                'M' => I18N::translateContext('son’s son', 'Cremation of a grandson'),
532                'F' => I18N::translateContext('son’s daughter', 'Cremation of a granddaughter'),
533                'U' => I18N::translate('Cremation of a grandchild'),
534            ],
535        ];
536
537        $marriage_of_a_child = [
538            'M' => I18N::translate('Marriage of a son'),
539            'F' => I18N::translate('Marriage of a daughter'),
540            'U' => I18N::translate('Marriage of a child'),
541        ];
542
543        $marriage_of_a_grandchild = [
544            'M' => I18N::translate('Marriage of a grandson'),
545            'F' => I18N::translate('Marriage of a granddaughter'),
546            'U' => I18N::translate('Marriage of a grandchild'),
547        ];
548
549        $marriage_of_a_grandchild1 = [
550            'M' => I18N::translateContext('daughter’s son', 'Marriage of a grandson'),
551            'F' => I18N::translateContext('daughter’s daughter', 'Marriage of a granddaughter'),
552            'U' => I18N::translate('Marriage of a grandchild'),
553        ];
554
555        $marriage_of_a_grandchild2 = [
556            'M' => I18N::translateContext('son’s son', 'Marriage of a grandson'),
557            'F' => I18N::translateContext('son’s daughter', 'Marriage of a granddaughter'),
558            'U' => I18N::translate('Marriage of a grandchild'),
559        ];
560
561        $marriage_of_a_sibling = [
562            'M' => I18N::translate('Marriage of a brother'),
563            'F' => I18N::translate('Marriage of a sister'),
564            'U' => I18N::translate('Marriage of a sibling'),
565        ];
566
567        $marriage_of_a_half_sibling = [
568            'M' => I18N::translate('Marriage of a half-brother'),
569            'F' => I18N::translate('Marriage of a half-sister'),
570            'U' => I18N::translate('Marriage of a half-sibling'),
571        ];
572
573        $facts = new Collection();
574
575        // Deal with recursion.
576        if ($option === '_CHIL') {
577            // Add grandchildren
578            foreach ($family->children() as $child) {
579                foreach ($child->spouseFamilies() as $cfamily) {
580                    switch ($child->sex()) {
581                        case 'M':
582                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
583                                $facts[] = $fact;
584                            }
585                            break;
586                        case 'F':
587                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
588                                $facts[] = $fact;
589                            }
590                            break;
591                        default:
592                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
593                                $facts[] = $fact;
594                            }
595                            break;
596                    }
597                }
598            }
599        }
600
601        // For each child in the family
602        foreach ($family->children() as $child) {
603            if ($child->xref() === $person->xref()) {
604                // We are not our own sibling!
605                continue;
606            }
607            // add child’s birth
608            if (str_contains($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option))) {
609                foreach ($child->facts(['BIRT', 'CHR', 'BAPM', 'ADOP']) as $fact) {
610                    // Always show _BIRT_CHIL, even if the dates are not known
611                    if ($option === '_CHIL' || $this->includeFact($fact, $min_date, $max_date)) {
612                        switch ($option) {
613                            case '_GCHI':
614                                switch ($relation) {
615                                    case 'dau':
616                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild1[$fact->tag()], $fact->record()->sex());
617                                        break;
618                                    case 'son':
619                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild2[$fact->tag()], $fact->record()->sex());
620                                        break;
621                                    case 'chil':
622                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild[$fact->tag()], $fact->record()->sex());
623                                        break;
624                                }
625                                break;
626                            case '_SIBL':
627                                $facts[] = $this->convertEvent($fact, $birth_of_a_sibling[$fact->tag()], $fact->record()->sex());
628                                break;
629                            case '_HSIB':
630                                $facts[] = $this->convertEvent($fact, $birth_of_a_half_sibling[$fact->tag()], $fact->record()->sex());
631                                break;
632                            case '_CHIL':
633                                $facts[] = $this->convertEvent($fact, $birth_of_a_child[$fact->tag()], $fact->record()->sex());
634                                break;
635                        }
636                    }
637                }
638            }
639            // add child’s death
640            if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option))) {
641                foreach ($child->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
642                    if ($this->includeFact($fact, $min_date, $max_date)) {
643                        switch ($option) {
644                            case '_GCHI':
645                                switch ($relation) {
646                                    case 'dau':
647                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild1[$fact->tag()], $fact->record()->sex());
648                                        break;
649                                    case 'son':
650                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild2[$fact->tag()], $fact->record()->sex());
651                                        break;
652                                    case 'chi':
653                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild[$fact->tag()], $fact->record()->sex());
654                                        break;
655                                }
656                                break;
657                            case '_SIBL':
658                                $facts[] = $this->convertEvent($fact, $death_of_a_sibling[$fact->tag()], $fact->record()->sex());
659                                break;
660                            case '_HSIB':
661                                $facts[] = $this->convertEvent($fact, $death_of_a_half_sibling[$fact->tag()], $fact->record()->sex());
662                                break;
663                            case '_CHIL':
664                                $facts[] = $this->convertEvent($fact, $death_of_a_child[$fact->tag()], $fact->record()->sex());
665                                break;
666                        }
667                    }
668                }
669            }
670
671            // add child’s marriage
672            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
673                foreach ($child->spouseFamilies() as $sfamily) {
674                    foreach ($sfamily->facts(['MARR']) as $fact) {
675                        if ($this->includeFact($fact, $min_date, $max_date)) {
676                            switch ($option) {
677                                case '_GCHI':
678                                    switch ($relation) {
679                                        case 'dau':
680                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild1, $child->sex());
681                                            break;
682                                        case 'son':
683                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild2, $child->sex());
684                                            break;
685                                        case 'chi':
686                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild, $child->sex());
687                                            break;
688                                    }
689                                    break;
690                                case '_SIBL':
691                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_sibling, $child->sex());
692                                    break;
693                                case '_HSIB':
694                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_half_sibling, $child->sex());
695                                    break;
696                                case '_CHIL':
697                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_child, $child->sex());
698                                    break;
699                            }
700                        }
701                    }
702                }
703            }
704        }
705
706        return $facts;
707    }
708
709    /**
710     * Get the events of parents and grandparents.
711     *
712     * @param Individual $person
713     * @param int        $sosa
714     * @param Date       $min_date
715     * @param Date       $max_date
716     *
717     * @return Collection<int,Fact>
718     */
719    protected function parentFacts(Individual $person, int $sosa, Date $min_date, Date $max_date): Collection
720    {
721        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
722
723        $death_of_a_parent = [
724            'INDI:DEAT' => [
725                'M' => I18N::translate('Death of a father'),
726                'F' => I18N::translate('Death of a mother'),
727                'U' => I18N::translate('Death of a parent'),
728            ],
729            'INDI:BURI' => [
730                'M' => I18N::translate('Burial of a father'),
731                'F' => I18N::translate('Burial of a mother'),
732                'U' => I18N::translate('Burial of a parent'),
733            ],
734            'INDI:CREM' => [
735                'M' => I18N::translate('Cremation of a father'),
736                'F' => I18N::translate('Cremation of a mother'),
737                'U' => I18N::translate('Cremation of a parent'),
738            ],
739        ];
740
741        $death_of_a_grandparent = [
742            'INDI:DEAT' => [
743                'M' => I18N::translate('Death of a grandfather'),
744                'F' => I18N::translate('Death of a grandmother'),
745                'U' => I18N::translate('Death of a grandparent'),
746            ],
747            'INDI:BURI' => [
748                'M' => I18N::translate('Burial of a grandfather'),
749                'F' => I18N::translate('Burial of a grandmother'),
750                'U' => I18N::translate('Burial of a grandparent'),
751            ],
752            'INDI:CREM' => [
753                'M' => I18N::translate('Cremation of a grandfather'),
754                'F' => I18N::translate('Cremation of a grandmother'),
755                'U' => I18N::translate('Cremation of a grandparent'),
756            ],
757        ];
758
759        $death_of_a_maternal_grandparent = [
760            'INDI:DEAT' => [
761                'M' => I18N::translate('Death of a maternal grandfather'),
762                'F' => I18N::translate('Death of a maternal grandmother'),
763                'U' => I18N::translate('Death of a grandparent'),
764            ],
765            'INDI:BURI' => [
766                'M' => I18N::translate('Burial of a maternal grandfather'),
767                'F' => I18N::translate('Burial of a maternal grandmother'),
768                'U' => I18N::translate('Burial of a grandparent'),
769            ],
770            'INDI:CREM' => [
771                'M' => I18N::translate('Cremation of a maternal grandfather'),
772                'F' => I18N::translate('Cremation of a maternal grandmother'),
773                'U' => I18N::translate('Cremation of a grandparent'),
774            ],
775        ];
776
777        $death_of_a_paternal_grandparent = [
778            'INDI:DEAT' => [
779                'M' => I18N::translate('Death of a paternal grandfather'),
780                'F' => I18N::translate('Death of a paternal grandmother'),
781                'U' => I18N::translate('Death of a grandparent'),
782            ],
783            'INDI:BURI' => [
784                'M' => I18N::translate('Burial of a paternal grandfather'),
785                'F' => I18N::translate('Burial of a paternal grandmother'),
786                'U' => I18N::translate('Burial of a grandparent'),
787            ],
788            'INDI:CREM' => [
789                'M' => I18N::translate('Cremation of a paternal grandfather'),
790                'F' => I18N::translate('Cremation of a paternal grandmother'),
791                'U' => I18N::translate('Cremation of a grandparent'),
792            ],
793        ];
794
795        $marriage_of_a_parent = [
796            'M' => I18N::translate('Marriage of a father'),
797            'F' => I18N::translate('Marriage of a mother'),
798            'U' => I18N::translate('Marriage of a parent'),
799        ];
800
801        $facts = new Collection();
802
803        if ($sosa === 1) {
804            foreach ($person->childFamilies() as $family) {
805                // Add siblings
806                foreach ($this->childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
807                    $facts[] = $fact;
808                }
809                foreach ($family->spouses() as $spouse) {
810                    foreach ($spouse->spouseFamilies() as $sfamily) {
811                        if ($family !== $sfamily) {
812                            // Add half-siblings
813                            foreach ($this->childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
814                                $facts[] = $fact;
815                            }
816                        }
817                    }
818                    // Add grandparents
819                    foreach ($this->parentFacts($spouse, $spouse->sex() === 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
820                        $facts[] = $fact;
821                    }
822                }
823            }
824
825            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
826                // add father/mother marriages
827                foreach ($person->childFamilies() as $sfamily) {
828                    foreach ($sfamily->facts(['MARR']) as $fact) {
829                        if ($this->includeFact($fact, $min_date, $max_date)) {
830                            // marriage of parents (to each other)
831                            $facts[] = $this->convertEvent($fact, ['U' => I18N::translate('Marriage of parents')], 'U');
832                        }
833                    }
834                }
835                foreach ($person->childStepFamilies() as $sfamily) {
836                    foreach ($sfamily->facts(['MARR']) as $fact) {
837                        if ($this->includeFact($fact, $min_date, $max_date)) {
838                            // marriage of a parent (to another spouse)
839                            $facts[] = $this->convertEvent($fact, $marriage_of_a_parent, 'U');
840                        }
841                    }
842                }
843            }
844        }
845
846        foreach ($person->childFamilies() as $family) {
847            foreach ($family->spouses() as $parent) {
848                if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa === 1 ? '_PARE' : '_GPAR'))) {
849                    foreach ($parent->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
850                        // Show death of parent when it happened prior to birth
851                        if ($sosa === 1 && Date::compare($fact->date(), $min_date) < 0 || $this->includeFact($fact, $min_date, $max_date)) {
852                            switch ($sosa) {
853                                case 1:
854                                    $facts[] = $this->convertEvent($fact, $death_of_a_parent[$fact->tag()], $fact->record()->sex());
855                                    break;
856                                case 2:
857                                case 3:
858                                    switch ($person->sex()) {
859                                        case 'M':
860                                            $facts[] = $this->convertEvent($fact, $death_of_a_paternal_grandparent[$fact->tag()], $fact->record()->sex());
861                                            break;
862                                        case 'F':
863                                            $facts[] = $this->convertEvent($fact, $death_of_a_maternal_grandparent[$fact->tag()], $fact->record()->sex());
864                                            break;
865                                        default:
866                                            $facts[] = $this->convertEvent($fact, $death_of_a_grandparent[$fact->tag()], $fact->record()->sex());
867                                            break;
868                                    }
869                            }
870                        }
871                    }
872                }
873            }
874        }
875
876        return $facts;
877    }
878
879    /**
880     * Get the events of associates.
881     *
882     * @param Individual $person
883     *
884     * @return Collection<int,Fact>
885     */
886    protected function associateFacts(Individual $person): Collection
887    {
888        $facts = [];
889
890        $asso1 = $this->linked_record_service->linkedIndividuals($person, 'ASSO');
891        $asso2 = $this->linked_record_service->linkedIndividuals($person, '_ASSO');
892        $asso3 = $this->linked_record_service->linkedFamilies($person, 'ASSO');
893        $asso4 = $this->linked_record_service->linkedFamilies($person, '_ASSO');
894
895        $associates = $asso1->merge($asso2)->merge($asso3)->merge($asso4);
896
897        foreach ($associates as $associate) {
898            foreach ($associate->facts() as $fact) {
899                if (preg_match('/\n\d _?ASSO @' . $person->xref() . '@/', $fact->gedcom())) {
900                    // Extract the important details from the fact
901                    $factrec = explode("\n", $fact->gedcom(), 2)[0];
902                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
903                        $factrec .= $match[0];
904                    }
905                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
906                        $factrec .= $match[0];
907                    }
908                    if ($associate instanceof Family) {
909                        foreach ($associate->spouses() as $spouse) {
910                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
911                        }
912                    } else {
913                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
914                    }
915                    $facts[] = new Fact($factrec, $associate, 'asso');
916                }
917            }
918        }
919
920        return new Collection($facts);
921    }
922
923    /**
924     * Get any historical events.
925     *
926     * @param Individual $individual
927     *
928     * @return Collection<int,Fact>
929     */
930    protected function historicFacts(Individual $individual): Collection
931    {
932        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
933            ->map(static function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
934                return $module->historicEventsForIndividual($individual);
935            })
936            ->flatten();
937    }
938
939    /**
940     * Is this tab empty? If so, we don't always need to display it.
941     *
942     * @param Individual $individual
943     *
944     * @return bool
945     */
946    public function hasTabContent(Individual $individual): bool
947    {
948        return true;
949    }
950
951    /**
952     * Can this tab load asynchronously?
953     *
954     * @return bool
955     */
956    public function canLoadAjax(): bool
957    {
958        return false;
959    }
960
961    /**
962     * This module handles the following facts - so don't show them on the "Facts and events" tab.
963     *
964     * @return Collection<int,string>
965     */
966    public function supportedFacts(): Collection
967    {
968        // We don't actually displaye these facts, but they are displayed
969        // outside the tabs/sidebar systems. This just forces them to be excluded here.
970        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
971    }
972}
973