xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 94db7d2d27c3ad6a04fceebbdcb98e28e97649b3)
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<int,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<int,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        if ($option === '_CHIL') {
569            // Add grandchildren
570            foreach ($family->children() as $child) {
571                foreach ($child->spouseFamilies() as $cfamily) {
572                    switch ($child->sex()) {
573                        case 'M':
574                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
575                                $facts[] = $fact;
576                            }
577                            break;
578                        case 'F':
579                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
580                                $facts[] = $fact;
581                            }
582                            break;
583                        default:
584                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
585                                $facts[] = $fact;
586                            }
587                            break;
588                    }
589                }
590            }
591        }
592
593        // For each child in the family
594        foreach ($family->children() as $child) {
595            if ($child->xref() === $person->xref()) {
596                // We are not our own sibling!
597                continue;
598            }
599            // add child’s birth
600            if (str_contains($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option))) {
601                foreach ($child->facts(['BIRT', 'CHR', 'BAPM', 'ADOP']) as $fact) {
602                    // Always show _BIRT_CHIL, even if the dates are not known
603                    if ($option === '_CHIL' || $this->includeFact($fact, $min_date, $max_date)) {
604                        switch ($option) {
605                            case '_GCHI':
606                                switch ($relation) {
607                                    case 'dau':
608                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild1[$fact->tag()][$fact->record()->sex()]);
609                                        break;
610                                    case 'son':
611                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild2[$fact->tag()][$fact->record()->sex()]);
612                                        break;
613                                    case 'chil':
614                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild[$fact->tag()][$fact->record()->sex()]);
615                                        break;
616                                }
617                                break;
618                            case '_SIBL':
619                                $facts[] = $this->convertEvent($fact, $birth_of_a_sibling[$fact->tag()][$fact->record()->sex()]);
620                                break;
621                            case '_HSIB':
622                                $facts[] = $this->convertEvent($fact, $birth_of_a_half_sibling[$fact->tag()][$fact->record()->sex()]);
623                                break;
624                            case '_CHIL':
625                                $facts[] = $this->convertEvent($fact, $birth_of_a_child[$fact->tag()][$fact->record()->sex()]);
626                                break;
627                        }
628                    }
629                }
630            }
631            // add child’s death
632            if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option))) {
633                foreach ($child->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
634                    if ($this->includeFact($fact, $min_date, $max_date)) {
635                        switch ($option) {
636                            case '_GCHI':
637                                switch ($relation) {
638                                    case 'dau':
639                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild1[$fact->tag()][$fact->record()->sex()]);
640                                        break;
641                                    case 'son':
642                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild2[$fact->tag()][$fact->record()->sex()]);
643                                        break;
644                                    case 'chi':
645                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild[$fact->tag()][$fact->record()->sex()]);
646                                        break;
647                                }
648                                break;
649                            case '_SIBL':
650                                $facts[] = $this->convertEvent($fact, $death_of_a_sibling[$fact->tag()][$fact->record()->sex()]);
651                                break;
652                            case '_HSIB':
653                                $facts[] = $this->convertEvent($fact, $death_of_a_half_sibling[$fact->tag()][$fact->record()->sex()]);
654                                break;
655                            case '_CHIL':
656                                $facts[] = $this->convertEvent($fact, $death_of_a_child[$fact->tag()][$fact->record()->sex()]);
657                                break;
658                        }
659                    }
660                }
661            }
662
663            // add child’s marriage
664            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
665                foreach ($child->spouseFamilies() as $sfamily) {
666                    foreach ($sfamily->facts(['MARR']) as $fact) {
667                        if ($this->includeFact($fact, $min_date, $max_date)) {
668                            switch ($option) {
669                                case '_GCHI':
670                                    switch ($relation) {
671                                        case 'dau':
672                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild1[$child->sex()]);
673                                            break;
674                                        case 'son':
675                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild2[$child->sex()]);
676                                            break;
677                                        case 'chi':
678                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild[$child->sex()]);
679                                            break;
680                                    }
681                                    break;
682                                case '_SIBL':
683                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_sibling[$child->sex()]);
684                                    break;
685                                case '_HSIB':
686                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_half_sibling[$child->sex()]);
687                                    break;
688                                case '_CHIL':
689                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_child[$child->sex()]);
690                                    break;
691                            }
692                        }
693                    }
694                }
695            }
696        }
697
698        return $facts;
699    }
700
701    /**
702     * Get the events of parents and grandparents.
703     *
704     * @param Individual $person
705     * @param int        $sosa
706     * @param Date       $min_date
707     * @param Date       $max_date
708     *
709     * @return Collection<int,Fact>
710     */
711    private function parentFacts(Individual $person, int $sosa, Date $min_date, Date $max_date): Collection
712    {
713        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
714
715        $death_of_a_parent = [
716            'INDI:DEAT' => [
717                'M' => I18N::translate('Death of a father'),
718                'F' => I18N::translate('Death of a mother'),
719                'U' => I18N::translate('Death of a parent'),
720            ],
721            'INDI:BURI' => [
722                'M' => I18N::translate('Burial of a father'),
723                'F' => I18N::translate('Burial of a mother'),
724                'U' => I18N::translate('Burial of a parent'),
725            ],
726            'INDI:CREM' => [
727                'M' => I18N::translate('Cremation of a father'),
728                'F' => I18N::translate('Cremation of a mother'),
729                'U' => I18N::translate('Cremation of a parent'),
730            ],
731        ];
732
733        $death_of_a_grandparent = [
734            'INDI:DEAT' => [
735                'M' => I18N::translate('Death of a grandfather'),
736                'F' => I18N::translate('Death of a grandmother'),
737                'U' => I18N::translate('Death of a grandparent'),
738            ],
739            'INDI:BURI' => [
740                'M' => I18N::translate('Burial of a grandfather'),
741                'F' => I18N::translate('Burial of a grandmother'),
742                'U' => I18N::translate('Burial of a grandparent'),
743            ],
744            'INDI:CREM' => [
745                'M' => I18N::translate('Cremation of a grandfather'),
746                'F' => I18N::translate('Cremation of a grandmother'),
747                'U' => I18N::translate('Cremation of a grandparent'),
748            ],
749        ];
750
751        $death_of_a_maternal_grandparent = [
752            'INDI:DEAT' => [
753                'M' => I18N::translate('Death of a maternal grandfather'),
754                'F' => I18N::translate('Death of a maternal grandmother'),
755                'U' => I18N::translate('Death of a grandparent'),
756            ],
757            'INDI:BURI' => [
758                'M' => I18N::translate('Burial of a maternal grandfather'),
759                'F' => I18N::translate('Burial of a maternal grandmother'),
760                'U' => I18N::translate('Burial of a grandparent'),
761            ],
762            'INDI:CREM' => [
763                'M' => I18N::translate('Cremation of a maternal grandfather'),
764                'F' => I18N::translate('Cremation of a maternal grandmother'),
765                'U' => I18N::translate('Cremation of a grandparent'),
766            ],
767        ];
768
769        $death_of_a_paternal_grandparent = [
770            'INDI:DEAT' => [
771                'M' => I18N::translate('Death of a paternal grandfather'),
772                'F' => I18N::translate('Death of a paternal grandmother'),
773                'U' => I18N::translate('Death of a grandparent'),
774            ],
775            'INDI:BURI' => [
776                'M' => I18N::translate('Burial of a paternal grandfather'),
777                'F' => I18N::translate('Burial of a paternal grandmother'),
778                'U' => I18N::translate('Burial of a grandparent'),
779            ],
780            'INDI:CREM' => [
781                'M' => I18N::translate('Cremation of a paternal grandfather'),
782                'F' => I18N::translate('Cremation of a paternal grandmother'),
783                'U' => I18N::translate('Cremation of a grandparent'),
784            ],
785        ];
786
787        $marriage_of_a_parent = [
788            'M' => I18N::translate('Marriage of a father'),
789            'F' => I18N::translate('Marriage of a mother'),
790            'U' => I18N::translate('Marriage of a parent'),
791        ];
792
793        $facts = new Collection();
794
795        if ($sosa === 1) {
796            foreach ($person->childFamilies() as $family) {
797                // Add siblings
798                foreach ($this->childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
799                    $facts[] = $fact;
800                }
801                foreach ($family->spouses() as $spouse) {
802                    foreach ($spouse->spouseFamilies() as $sfamily) {
803                        if ($family !== $sfamily) {
804                            // Add half-siblings
805                            foreach ($this->childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
806                                $facts[] = $fact;
807                            }
808                        }
809                    }
810                    // Add grandparents
811                    foreach ($this->parentFacts($spouse, $spouse->sex() === 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
812                        $facts[] = $fact;
813                    }
814                }
815            }
816
817            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
818                // add father/mother marriages
819                foreach ($person->childFamilies() as $sfamily) {
820                    foreach ($sfamily->facts(['MARR']) as $fact) {
821                        if ($this->includeFact($fact, $min_date, $max_date)) {
822                            // marriage of parents (to each other)
823                            $facts[] = $this->convertEvent($fact, I18N::translate('Marriage of parents'));
824                        }
825                    }
826                }
827                foreach ($person->childStepFamilies() as $sfamily) {
828                    foreach ($sfamily->facts(['MARR']) as $fact) {
829                        if ($this->includeFact($fact, $min_date, $max_date)) {
830                            // marriage of a parent (to another spouse)
831                            $facts[] = $this->convertEvent($fact, $marriage_of_a_parent['U']);
832                        }
833                    }
834                }
835            }
836        }
837
838        foreach ($person->childFamilies() as $family) {
839            foreach ($family->spouses() as $parent) {
840                if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa === 1 ? '_PARE' : '_GPAR'))) {
841                    foreach ($parent->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
842                        // Show death of parent when it happened prior to birth
843                        if ($sosa === 1 && Date::compare($fact->date(), $min_date) < 0 || $this->includeFact($fact, $min_date, $max_date)) {
844                            switch ($sosa) {
845                                case 1:
846                                    $facts[] = $this->convertEvent($fact, $death_of_a_parent[$fact->tag()][$fact->record()->sex()]);
847                                    break;
848                                case 2:
849                                case 3:
850                                    switch ($person->sex()) {
851                                        case 'M':
852                                            $facts[] = $this->convertEvent($fact, $death_of_a_paternal_grandparent[$fact->tag()][$fact->record()->sex()]);
853                                            break;
854                                        case 'F':
855                                            $facts[] = $this->convertEvent($fact, $death_of_a_maternal_grandparent[$fact->tag()][$fact->record()->sex()]);
856                                            break;
857                                        default:
858                                            $facts[] = $this->convertEvent($fact, $death_of_a_grandparent[$fact->tag()][$fact->record()->sex()]);
859                                            break;
860                                    }
861                            }
862                        }
863                    }
864                }
865            }
866        }
867
868        return $facts;
869    }
870
871    /**
872     * Get the events of associates.
873     *
874     * @param Individual $person
875     *
876     * @return Collection<int,Fact>
877     */
878    private function associateFacts(Individual $person): Collection
879    {
880        $facts = [];
881
882        $asso1 = $person->linkedIndividuals('ASSO');
883        $asso2 = $person->linkedIndividuals('_ASSO');
884        $asso3 = $person->linkedFamilies('ASSO');
885        $asso4 = $person->linkedFamilies('_ASSO');
886
887        $associates = $asso1->merge($asso2)->merge($asso3)->merge($asso4);
888
889        foreach ($associates as $associate) {
890            foreach ($associate->facts() as $fact) {
891                if (preg_match('/\n\d _?ASSO @' . $person->xref() . '@/', $fact->gedcom())) {
892                    // Extract the important details from the fact
893                    $factrec = explode("\n", $fact->gedcom(), 2)[0];
894                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
895                        $factrec .= $match[0];
896                    }
897                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
898                        $factrec .= $match[0];
899                    }
900                    if ($associate instanceof Family) {
901                        foreach ($associate->spouses() as $spouse) {
902                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
903                        }
904                    } else {
905                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
906                    }
907                    $facts[] = new Fact($factrec, $associate, 'asso');
908                }
909            }
910        }
911
912        return new Collection($facts);
913    }
914
915    /**
916     * Get any historical events.
917     *
918     * @param Individual $individual
919     *
920     * @return Collection<int,Fact>
921     */
922    private function historicFacts(Individual $individual): Collection
923    {
924        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
925            ->map(static function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
926                return $module->historicEventsForIndividual($individual);
927            })
928            ->flatten();
929    }
930
931    /**
932     * Is this tab empty? If so, we don't always need to display it.
933     *
934     * @param Individual $individual
935     *
936     * @return bool
937     */
938    public function hasTabContent(Individual $individual): bool
939    {
940        return true;
941    }
942
943    /**
944     * Can this tab load asynchronously?
945     *
946     * @return bool
947     */
948    public function canLoadAjax(): bool
949    {
950        return false;
951    }
952
953    /**
954     * This module handles the following facts - so don't show them on the "Facts and events" tab.
955     *
956     * @return Collection<int,string>
957     */
958    public function supportedFacts(): Collection
959    {
960        // We don't actually displaye these facts, but they are displayed
961        // outside the tabs/sidebar systems. This just forces them to be excluded here.
962        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
963    }
964}
965