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