xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
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    private 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    private 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 string $type
250     *
251     * @return Fact
252     */
253    private function convertEvent(Fact $fact, string $type): Fact
254    {
255        $gedcom = $fact->gedcom();
256        $gedcom = preg_replace('/\n2 TYPE .*/', '', $gedcom);
257        $gedcom = preg_replace('/^1 .*/', "1 EVEN CLOSE_RELATIVE\n2 TYPE " . $type, $gedcom);
258
259        $converted = new Fact($gedcom, $fact->record(), $fact->id());
260
261        if ($fact->isPendingAddition()) {
262            $converted->setPendingAddition();
263        }
264
265        if ($fact->isPendingDeletion()) {
266            $converted->setPendingDeletion();
267        }
268
269        return $converted;
270    }
271
272    /**
273     * Get the events of children and grandchildren.
274     *
275     * @param Individual $person
276     * @param Family     $family
277     * @param string     $option
278     * @param string     $relation
279     * @param Date       $min_date
280     * @param Date       $max_date
281     *
282     * @return Collection<int,Fact>
283     */
284    private function childFacts(Individual $person, Family $family, string $option, string $relation, Date $min_date, Date $max_date): Collection
285    {
286        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
287
288        $birth_of_a_child = [
289            'INDI:BIRT' => [
290                'M' => I18N::translate('Birth of a son'),
291                'F' => I18N::translate('Birth of a daughter'),
292                'U' => I18N::translate('Birth of a child'),
293            ],
294            'INDI:CHR'  => [
295                'M' => I18N::translate('Christening of a son'),
296                'F' => I18N::translate('Christening of a daughter'),
297                'U' => I18N::translate('Christening of a child'),
298            ],
299            'INDI:BAPM' => [
300                'M' => I18N::translate('Baptism of a son'),
301                'F' => I18N::translate('Baptism of a daughter'),
302                'U' => I18N::translate('Baptism of a child'),
303            ],
304            'INDI:ADOP' => [
305                'M' => I18N::translate('Adoption of a son'),
306                'F' => I18N::translate('Adoption of a daughter'),
307                'U' => I18N::translate('Adoption of a child'),
308            ],
309        ];
310
311        $birth_of_a_sibling = [
312            'INDI:BIRT' => [
313                'M' => I18N::translate('Birth of a brother'),
314                'F' => I18N::translate('Birth of a sister'),
315                'U' => I18N::translate('Birth of a sibling'),
316            ],
317            'INDI:CHR'  => [
318                'M' => I18N::translate('Christening of a brother'),
319                'F' => I18N::translate('Christening of a sister'),
320                'U' => I18N::translate('Christening of a sibling'),
321            ],
322            'INDI:BAPM' => [
323                'M' => I18N::translate('Baptism of a brother'),
324                'F' => I18N::translate('Baptism of a sister'),
325                'U' => I18N::translate('Baptism of a sibling'),
326            ],
327            'INDI:ADOP' => [
328                'M' => I18N::translate('Adoption of a brother'),
329                'F' => I18N::translate('Adoption of a sister'),
330                'U' => I18N::translate('Adoption of a sibling'),
331            ],
332        ];
333
334        $birth_of_a_half_sibling = [
335            'INDI:BIRT' => [
336                'M' => I18N::translate('Birth of a half-brother'),
337                'F' => I18N::translate('Birth of a half-sister'),
338                'U' => I18N::translate('Birth of a half-sibling'),
339            ],
340            'INDI:CHR'  => [
341                'M' => I18N::translate('Christening of a half-brother'),
342                'F' => I18N::translate('Christening of a half-sister'),
343                'U' => I18N::translate('Christening of a half-sibling'),
344            ],
345            'INDI:BAPM' => [
346                'M' => I18N::translate('Baptism of a half-brother'),
347                'F' => I18N::translate('Baptism of a half-sister'),
348                'U' => I18N::translate('Baptism of a half-sibling'),
349            ],
350            'INDI:ADOP' => [
351                'M' => I18N::translate('Adoption of a half-brother'),
352                'F' => I18N::translate('Adoption of a half-sister'),
353                'U' => I18N::translate('Adoption of a half-sibling'),
354            ],
355        ];
356
357        $birth_of_a_grandchild = [
358            'INDI:BIRT' => [
359                'M' => I18N::translate('Birth of a grandson'),
360                'F' => I18N::translate('Birth of a granddaughter'),
361                'U' => I18N::translate('Birth of a grandchild'),
362            ],
363            'INDI:CHR'  => [
364                'M' => I18N::translate('Christening of a grandson'),
365                'F' => I18N::translate('Christening of a granddaughter'),
366                'U' => I18N::translate('Christening of a grandchild'),
367            ],
368            'INDI:BAPM' => [
369                'M' => I18N::translate('Baptism of a grandson'),
370                'F' => I18N::translate('Baptism of a granddaughter'),
371                'U' => I18N::translate('Baptism of a grandchild'),
372            ],
373            'INDI:ADOP' => [
374                'M' => I18N::translate('Adoption of a grandson'),
375                'F' => I18N::translate('Adoption of a granddaughter'),
376                'U' => I18N::translate('Adoption of a grandchild'),
377            ],
378        ];
379
380        $birth_of_a_grandchild1 = [
381            'INDI:BIRT' => [
382                'M' => I18N::translateContext('daughter’s son', 'Birth of a grandson'),
383                'F' => I18N::translateContext('daughter’s daughter', 'Birth of a granddaughter'),
384                'U' => I18N::translate('Birth of a grandchild'),
385            ],
386            'INDI:CHR'  => [
387                'M' => I18N::translateContext('daughter’s son', 'Christening of a grandson'),
388                'F' => I18N::translateContext('daughter’s daughter', 'Christening of a granddaughter'),
389                'U' => I18N::translate('Christening of a grandchild'),
390            ],
391            'INDI:BAPM' => [
392                'M' => I18N::translateContext('daughter’s son', 'Baptism of a grandson'),
393                'F' => I18N::translateContext('daughter’s daughter', 'Baptism of a granddaughter'),
394                'U' => I18N::translate('Baptism of a grandchild'),
395            ],
396            'INDI:ADOP' => [
397                'M' => I18N::translateContext('daughter’s son', 'Adoption of a grandson'),
398                'F' => I18N::translateContext('daughter’s daughter', 'Adoption of a granddaughter'),
399                'U' => I18N::translate('Adoption of a grandchild'),
400            ],
401        ];
402
403        $birth_of_a_grandchild2 = [
404            'INDI:BIRT' => [
405                'M' => I18N::translateContext('son’s son', 'Birth of a grandson'),
406                'F' => I18N::translateContext('son’s daughter', 'Birth of a granddaughter'),
407                'U' => I18N::translate('Birth of a grandchild'),
408            ],
409            'INDI:CHR'  => [
410                'M' => I18N::translateContext('son’s son', 'Christening of a grandson'),
411                'F' => I18N::translateContext('son’s daughter', 'Christening of a granddaughter'),
412                'U' => I18N::translate('Christening of a grandchild'),
413            ],
414            'INDI:BAPM' => [
415                'M' => I18N::translateContext('son’s son', 'Baptism of a grandson'),
416                'F' => I18N::translateContext('son’s daughter', 'Baptism of a granddaughter'),
417                'U' => I18N::translate('Baptism of a grandchild'),
418            ],
419            'INDI:ADOP' => [
420                'M' => I18N::translateContext('son’s son', 'Adoption of a grandson'),
421                'F' => I18N::translateContext('son’s daughter', 'Adoption of a granddaughter'),
422                'U' => I18N::translate('Adoption of a grandchild'),
423            ],
424        ];
425
426        $death_of_a_child = [
427            'INDI:DEAT' => [
428                'M' => I18N::translate('Death of a son'),
429                'F' => I18N::translate('Death of a daughter'),
430                'U' => I18N::translate('Death of a child'),
431            ],
432            'INDI:BURI' => [
433                'M' => I18N::translate('Burial of a son'),
434                'F' => I18N::translate('Burial of a daughter'),
435                'U' => I18N::translate('Burial of a child'),
436            ],
437            'INDI:CREM' => [
438                'M' => I18N::translate('Cremation of a son'),
439                'F' => I18N::translate('Cremation of a daughter'),
440                'U' => I18N::translate('Cremation of a child'),
441            ],
442        ];
443
444        $death_of_a_sibling = [
445            'INDI:DEAT' => [
446                'M' => I18N::translate('Death of a brother'),
447                'F' => I18N::translate('Death of a sister'),
448                'U' => I18N::translate('Death of a sibling'),
449            ],
450            'INDI:BURI' => [
451                'M' => I18N::translate('Burial of a brother'),
452                'F' => I18N::translate('Burial of a sister'),
453                'U' => I18N::translate('Burial of a sibling'),
454            ],
455            'INDI:CREM' => [
456                'M' => I18N::translate('Cremation of a brother'),
457                'F' => I18N::translate('Cremation of a sister'),
458                'U' => I18N::translate('Cremation of a sibling'),
459            ],
460        ];
461
462        $death_of_a_half_sibling = [
463            'INDI:DEAT' => [
464                'M' => I18N::translate('Death of a half-brother'),
465                'F' => I18N::translate('Death of a half-sister'),
466                'U' => I18N::translate('Death of a half-sibling'),
467            ],
468            'INDI:BURI' => [
469                'M' => I18N::translate('Burial of a half-brother'),
470                'F' => I18N::translate('Burial of a half-sister'),
471                'U' => I18N::translate('Burial of a half-sibling'),
472            ],
473            'INDI:CREM' => [
474                'M' => I18N::translate('Cremation of a half-brother'),
475                'F' => I18N::translate('Cremation of a half-sister'),
476                'U' => I18N::translate('Cremation of a half-sibling'),
477            ],
478        ];
479
480        $death_of_a_grandchild = [
481            'INDI:DEAT' => [
482                'M' => I18N::translate('Death of a grandson'),
483                'F' => I18N::translate('Death of a granddaughter'),
484                'U' => I18N::translate('Death of a grandchild'),
485            ],
486            'INDI:BURI' => [
487                'M' => I18N::translate('Burial of a grandson'),
488                'F' => I18N::translate('Burial of a granddaughter'),
489                'U' => I18N::translate('Burial of a grandchild'),
490            ],
491            'INDI:CREM' => [
492                'M' => I18N::translate('Cremation of a grandson'),
493                'F' => I18N::translate('Cremation of a granddaughter'),
494                'U' => I18N::translate('Baptism of a grandchild'),
495            ],
496        ];
497
498        $death_of_a_grandchild1 = [
499            'INDI:DEAT' => [
500                'M' => I18N::translateContext('daughter’s son', 'Death of a grandson'),
501                'F' => I18N::translateContext('daughter’s daughter', 'Death of a granddaughter'),
502                'U' => I18N::translate('Death of a grandchild'),
503            ],
504            'INDI:BURI' => [
505                'M' => I18N::translateContext('daughter’s son', 'Burial of a grandson'),
506                'F' => I18N::translateContext('daughter’s daughter', 'Burial of a granddaughter'),
507                'U' => I18N::translate('Burial of a grandchild'),
508            ],
509            'INDI:CREM' => [
510                'M' => I18N::translateContext('daughter’s son', 'Cremation of a grandson'),
511                'F' => I18N::translateContext('daughter’s daughter', 'Cremation of a granddaughter'),
512                'U' => I18N::translate('Baptism of a grandchild'),
513            ],
514        ];
515
516        $death_of_a_grandchild2 = [
517            'INDI:DEAT' => [
518                'M' => I18N::translateContext('son’s son', 'Death of a grandson'),
519                'F' => I18N::translateContext('son’s daughter', 'Death of a granddaughter'),
520                'U' => I18N::translate('Death of a grandchild'),
521            ],
522            'INDI:BURI' => [
523                'M' => I18N::translateContext('son’s son', 'Burial of a grandson'),
524                'F' => I18N::translateContext('son’s daughter', 'Burial of a granddaughter'),
525                'U' => I18N::translate('Burial of a grandchild'),
526            ],
527            'INDI:CREM' => [
528                'M' => I18N::translateContext('son’s son', 'Cremation of a grandson'),
529                'F' => I18N::translateContext('son’s daughter', 'Cremation of a granddaughter'),
530                'U' => I18N::translate('Cremation of a grandchild'),
531            ],
532        ];
533
534        $marriage_of_a_child = [
535            'M' => I18N::translate('Marriage of a son'),
536            'F' => I18N::translate('Marriage of a daughter'),
537            'U' => I18N::translate('Marriage of a child'),
538        ];
539
540        $marriage_of_a_grandchild = [
541            'M' => I18N::translate('Marriage of a grandson'),
542            'F' => I18N::translate('Marriage of a granddaughter'),
543            'U' => I18N::translate('Marriage of a grandchild'),
544        ];
545
546        $marriage_of_a_grandchild1 = [
547            'M' => I18N::translateContext('daughter’s son', 'Marriage of a grandson'),
548            'F' => I18N::translateContext('daughter’s daughter', 'Marriage of a granddaughter'),
549            'U' => I18N::translate('Marriage of a grandchild'),
550        ];
551
552        $marriage_of_a_grandchild2 = [
553            'M' => I18N::translateContext('son’s son', 'Marriage of a grandson'),
554            'F' => I18N::translateContext('son’s daughter', 'Marriage of a granddaughter'),
555            'U' => I18N::translate('Marriage of a grandchild'),
556        ];
557
558        $marriage_of_a_sibling = [
559            'M' => I18N::translate('Marriage of a brother'),
560            'F' => I18N::translate('Marriage of a sister'),
561            'U' => I18N::translate('Marriage of a sibling'),
562        ];
563
564        $marriage_of_a_half_sibling = [
565            'M' => I18N::translate('Marriage of a half-brother'),
566            'F' => I18N::translate('Marriage of a half-sister'),
567            'U' => I18N::translate('Marriage of a half-sibling'),
568        ];
569
570        $facts = new Collection();
571
572        // Deal with recursion.
573        if ($option === '_CHIL') {
574            // Add grandchildren
575            foreach ($family->children() as $child) {
576                foreach ($child->spouseFamilies() as $cfamily) {
577                    switch ($child->sex()) {
578                        case 'M':
579                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
580                                $facts[] = $fact;
581                            }
582                            break;
583                        case 'F':
584                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
585                                $facts[] = $fact;
586                            }
587                            break;
588                        default:
589                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
590                                $facts[] = $fact;
591                            }
592                            break;
593                    }
594                }
595            }
596        }
597
598        // For each child in the family
599        foreach ($family->children() as $child) {
600            if ($child->xref() === $person->xref()) {
601                // We are not our own sibling!
602                continue;
603            }
604            // add child’s birth
605            if (str_contains($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option))) {
606                foreach ($child->facts(['BIRT', 'CHR', 'BAPM', 'ADOP']) as $fact) {
607                    // Always show _BIRT_CHIL, even if the dates are not known
608                    if ($option === '_CHIL' || $this->includeFact($fact, $min_date, $max_date)) {
609                        switch ($option) {
610                            case '_GCHI':
611                                switch ($relation) {
612                                    case 'dau':
613                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild1[$fact->tag()][$fact->record()->sex()]);
614                                        break;
615                                    case 'son':
616                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild2[$fact->tag()][$fact->record()->sex()]);
617                                        break;
618                                    case 'chil':
619                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild[$fact->tag()][$fact->record()->sex()]);
620                                        break;
621                                }
622                                break;
623                            case '_SIBL':
624                                $facts[] = $this->convertEvent($fact, $birth_of_a_sibling[$fact->tag()][$fact->record()->sex()]);
625                                break;
626                            case '_HSIB':
627                                $facts[] = $this->convertEvent($fact, $birth_of_a_half_sibling[$fact->tag()][$fact->record()->sex()]);
628                                break;
629                            case '_CHIL':
630                                $facts[] = $this->convertEvent($fact, $birth_of_a_child[$fact->tag()][$fact->record()->sex()]);
631                                break;
632                        }
633                    }
634                }
635            }
636            // add child’s death
637            if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option))) {
638                foreach ($child->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
639                    if ($this->includeFact($fact, $min_date, $max_date)) {
640                        switch ($option) {
641                            case '_GCHI':
642                                switch ($relation) {
643                                    case 'dau':
644                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild1[$fact->tag()][$fact->record()->sex()]);
645                                        break;
646                                    case 'son':
647                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild2[$fact->tag()][$fact->record()->sex()]);
648                                        break;
649                                    case 'chi':
650                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild[$fact->tag()][$fact->record()->sex()]);
651                                        break;
652                                }
653                                break;
654                            case '_SIBL':
655                                $facts[] = $this->convertEvent($fact, $death_of_a_sibling[$fact->tag()][$fact->record()->sex()]);
656                                break;
657                            case '_HSIB':
658                                $facts[] = $this->convertEvent($fact, $death_of_a_half_sibling[$fact->tag()][$fact->record()->sex()]);
659                                break;
660                            case '_CHIL':
661                                $facts[] = $this->convertEvent($fact, $death_of_a_child[$fact->tag()][$fact->record()->sex()]);
662                                break;
663                        }
664                    }
665                }
666            }
667
668            // add child’s marriage
669            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
670                foreach ($child->spouseFamilies() as $sfamily) {
671                    foreach ($sfamily->facts(['MARR']) as $fact) {
672                        if ($this->includeFact($fact, $min_date, $max_date)) {
673                            switch ($option) {
674                                case '_GCHI':
675                                    switch ($relation) {
676                                        case 'dau':
677                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild1[$child->sex()]);
678                                            break;
679                                        case 'son':
680                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild2[$child->sex()]);
681                                            break;
682                                        case 'chi':
683                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild[$child->sex()]);
684                                            break;
685                                    }
686                                    break;
687                                case '_SIBL':
688                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_sibling[$child->sex()]);
689                                    break;
690                                case '_HSIB':
691                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_half_sibling[$child->sex()]);
692                                    break;
693                                case '_CHIL':
694                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_child[$child->sex()]);
695                                    break;
696                            }
697                        }
698                    }
699                }
700            }
701        }
702
703        return $facts;
704    }
705
706    /**
707     * Get the events of parents and grandparents.
708     *
709     * @param Individual $person
710     * @param int        $sosa
711     * @param Date       $min_date
712     * @param Date       $max_date
713     *
714     * @return Collection<int,Fact>
715     */
716    private function parentFacts(Individual $person, int $sosa, Date $min_date, Date $max_date): Collection
717    {
718        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
719
720        $death_of_a_parent = [
721            'INDI:DEAT' => [
722                'M' => I18N::translate('Death of a father'),
723                'F' => I18N::translate('Death of a mother'),
724                'U' => I18N::translate('Death of a parent'),
725            ],
726            'INDI:BURI' => [
727                'M' => I18N::translate('Burial of a father'),
728                'F' => I18N::translate('Burial of a mother'),
729                'U' => I18N::translate('Burial of a parent'),
730            ],
731            'INDI:CREM' => [
732                'M' => I18N::translate('Cremation of a father'),
733                'F' => I18N::translate('Cremation of a mother'),
734                'U' => I18N::translate('Cremation of a parent'),
735            ],
736        ];
737
738        $death_of_a_grandparent = [
739            'INDI:DEAT' => [
740                'M' => I18N::translate('Death of a grandfather'),
741                'F' => I18N::translate('Death of a grandmother'),
742                'U' => I18N::translate('Death of a grandparent'),
743            ],
744            'INDI:BURI' => [
745                'M' => I18N::translate('Burial of a grandfather'),
746                'F' => I18N::translate('Burial of a grandmother'),
747                'U' => I18N::translate('Burial of a grandparent'),
748            ],
749            'INDI:CREM' => [
750                'M' => I18N::translate('Cremation of a grandfather'),
751                'F' => I18N::translate('Cremation of a grandmother'),
752                'U' => I18N::translate('Cremation of a grandparent'),
753            ],
754        ];
755
756        $death_of_a_maternal_grandparent = [
757            'INDI:DEAT' => [
758                'M' => I18N::translate('Death of a maternal grandfather'),
759                'F' => I18N::translate('Death of a maternal grandmother'),
760                'U' => I18N::translate('Death of a grandparent'),
761            ],
762            'INDI:BURI' => [
763                'M' => I18N::translate('Burial of a maternal grandfather'),
764                'F' => I18N::translate('Burial of a maternal grandmother'),
765                'U' => I18N::translate('Burial of a grandparent'),
766            ],
767            'INDI:CREM' => [
768                'M' => I18N::translate('Cremation of a maternal grandfather'),
769                'F' => I18N::translate('Cremation of a maternal grandmother'),
770                'U' => I18N::translate('Cremation of a grandparent'),
771            ],
772        ];
773
774        $death_of_a_paternal_grandparent = [
775            'INDI:DEAT' => [
776                'M' => I18N::translate('Death of a paternal grandfather'),
777                'F' => I18N::translate('Death of a paternal grandmother'),
778                'U' => I18N::translate('Death of a grandparent'),
779            ],
780            'INDI:BURI' => [
781                'M' => I18N::translate('Burial of a paternal grandfather'),
782                'F' => I18N::translate('Burial of a paternal grandmother'),
783                'U' => I18N::translate('Burial of a grandparent'),
784            ],
785            'INDI:CREM' => [
786                'M' => I18N::translate('Cremation of a paternal grandfather'),
787                'F' => I18N::translate('Cremation of a paternal grandmother'),
788                'U' => I18N::translate('Cremation of a grandparent'),
789            ],
790        ];
791
792        $marriage_of_a_parent = [
793            'M' => I18N::translate('Marriage of a father'),
794            'F' => I18N::translate('Marriage of a mother'),
795            'U' => I18N::translate('Marriage of a parent'),
796        ];
797
798        $facts = new Collection();
799
800        if ($sosa === 1) {
801            foreach ($person->childFamilies() as $family) {
802                // Add siblings
803                foreach ($this->childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
804                    $facts[] = $fact;
805                }
806                foreach ($family->spouses() as $spouse) {
807                    foreach ($spouse->spouseFamilies() as $sfamily) {
808                        if ($family !== $sfamily) {
809                            // Add half-siblings
810                            foreach ($this->childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
811                                $facts[] = $fact;
812                            }
813                        }
814                    }
815                    // Add grandparents
816                    foreach ($this->parentFacts($spouse, $spouse->sex() === 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
817                        $facts[] = $fact;
818                    }
819                }
820            }
821
822            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
823                // add father/mother marriages
824                foreach ($person->childFamilies() as $sfamily) {
825                    foreach ($sfamily->facts(['MARR']) as $fact) {
826                        if ($this->includeFact($fact, $min_date, $max_date)) {
827                            // marriage of parents (to each other)
828                            $facts[] = $this->convertEvent($fact, I18N::translate('Marriage of parents'));
829                        }
830                    }
831                }
832                foreach ($person->childStepFamilies() as $sfamily) {
833                    foreach ($sfamily->facts(['MARR']) as $fact) {
834                        if ($this->includeFact($fact, $min_date, $max_date)) {
835                            // marriage of a parent (to another spouse)
836                            $facts[] = $this->convertEvent($fact, $marriage_of_a_parent['U']);
837                        }
838                    }
839                }
840            }
841        }
842
843        foreach ($person->childFamilies() as $family) {
844            foreach ($family->spouses() as $parent) {
845                if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa === 1 ? '_PARE' : '_GPAR'))) {
846                    foreach ($parent->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
847                        // Show death of parent when it happened prior to birth
848                        if ($sosa === 1 && Date::compare($fact->date(), $min_date) < 0 || $this->includeFact($fact, $min_date, $max_date)) {
849                            switch ($sosa) {
850                                case 1:
851                                    $facts[] = $this->convertEvent($fact, $death_of_a_parent[$fact->tag()][$fact->record()->sex()]);
852                                    break;
853                                case 2:
854                                case 3:
855                                    switch ($person->sex()) {
856                                        case 'M':
857                                            $facts[] = $this->convertEvent($fact, $death_of_a_paternal_grandparent[$fact->tag()][$fact->record()->sex()]);
858                                            break;
859                                        case 'F':
860                                            $facts[] = $this->convertEvent($fact, $death_of_a_maternal_grandparent[$fact->tag()][$fact->record()->sex()]);
861                                            break;
862                                        default:
863                                            $facts[] = $this->convertEvent($fact, $death_of_a_grandparent[$fact->tag()][$fact->record()->sex()]);
864                                            break;
865                                    }
866                            }
867                        }
868                    }
869                }
870            }
871        }
872
873        return $facts;
874    }
875
876    /**
877     * Get the events of associates.
878     *
879     * @param Individual $person
880     *
881     * @return Collection<int,Fact>
882     */
883    private function associateFacts(Individual $person): Collection
884    {
885        $facts = [];
886
887        $asso1 = $this->linked_record_service->linkedIndividuals($person, 'ASSO');
888        $asso2 = $this->linked_record_service->linkedIndividuals($person, '_ASSO');
889        $asso3 = $this->linked_record_service->linkedFamilies($person, 'ASSO');
890        $asso4 = $this->linked_record_service->linkedFamilies($person, '_ASSO');
891
892        $associates = $asso1->merge($asso2)->merge($asso3)->merge($asso4);
893
894        foreach ($associates as $associate) {
895            foreach ($associate->facts() as $fact) {
896                if (preg_match('/\n\d _?ASSO @' . $person->xref() . '@/', $fact->gedcom())) {
897                    // Extract the important details from the fact
898                    $factrec = explode("\n", $fact->gedcom(), 2)[0];
899                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
900                        $factrec .= $match[0];
901                    }
902                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
903                        $factrec .= $match[0];
904                    }
905                    if ($associate instanceof Family) {
906                        foreach ($associate->spouses() as $spouse) {
907                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
908                        }
909                    } else {
910                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
911                    }
912                    $facts[] = new Fact($factrec, $associate, 'asso');
913                }
914            }
915        }
916
917        return new Collection($facts);
918    }
919
920    /**
921     * Get any historical events.
922     *
923     * @param Individual $individual
924     *
925     * @return Collection<int,Fact>
926     */
927    private function historicFacts(Individual $individual): Collection
928    {
929        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
930            ->map(static function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
931                return $module->historicEventsForIndividual($individual);
932            })
933            ->flatten();
934    }
935
936    /**
937     * Is this tab empty? If so, we don't always need to display it.
938     *
939     * @param Individual $individual
940     *
941     * @return bool
942     */
943    public function hasTabContent(Individual $individual): bool
944    {
945        return true;
946    }
947
948    /**
949     * Can this tab load asynchronously?
950     *
951     * @return bool
952     */
953    public function canLoadAjax(): bool
954    {
955        return false;
956    }
957
958    /**
959     * This module handles the following facts - so don't show them on the "Facts and events" tab.
960     *
961     * @return Collection<int,string>
962     */
963    public function supportedFacts(): Collection
964    {
965        // We don't actually displaye these facts, but they are displayed
966        // outside the tabs/sidebar systems. This just forces them to be excluded here.
967        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
968    }
969}
970