xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 05babb969dd66fd95325613c7360d51ed3aa19fc)
13763c3f2SGreg Roach<?php
23976b470SGreg Roach
33763c3f2SGreg Roach/**
43763c3f2SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
63763c3f2SGreg Roach * This program is free software: you can redistribute it and/or modify
73763c3f2SGreg Roach * it under the terms of the GNU General Public License as published by
83763c3f2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
93763c3f2SGreg Roach * (at your option) any later version.
103763c3f2SGreg Roach * This program is distributed in the hope that it will be useful,
113763c3f2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123763c3f2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133763c3f2SGreg Roach * GNU General Public License for more details.
143763c3f2SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
163763c3f2SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
2263276d8fSGreg Roachuse Fisharebest\Webtrees\Auth;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
282adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
3017c50b57SGreg Roachuse Illuminate\Support\Collection;
313763c3f2SGreg Roach
32d0889c63SGreg Roachuse function explode;
33d0889c63SGreg Roachuse function preg_match;
34d0889c63SGreg Roachuse function preg_replace;
35dec352c1SGreg Roachuse function str_contains;
36d0889c63SGreg Roachuse function str_replace;
37d0889c63SGreg Roachuse function view;
38dec352c1SGreg Roach
393763c3f2SGreg Roach/**
403763c3f2SGreg Roach * Class IndividualFactsTabModule
413763c3f2SGreg Roach */
4237eb8894SGreg Roachclass IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleTabTrait;
4549a243cbSGreg Roach
4691a257a4SGreg Roach    private ModuleService $module_service;
474ca7e03cSGreg Roach
4891a257a4SGreg Roach    private ClipboardService $clipboard_service;
492adcbd9aSGreg Roach
504ca7e03cSGreg Roach    /**
510874af26SRichard Cissée     * IndividualFactsTabModule constructor.
524ca7e03cSGreg Roach     *
534ca7e03cSGreg Roach     * @param ModuleService    $module_service
542adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
554ca7e03cSGreg Roach     */
562adcbd9aSGreg Roach    public function __construct(ModuleService $module_service, ClipboardService $clipboard_service)
575bdbe281SGreg Roach    {
584ca7e03cSGreg Roach        $this->module_service    = $module_service;
592adcbd9aSGreg Roach        $this->clipboard_service = $clipboard_service;
604ca7e03cSGreg Roach    }
614ca7e03cSGreg Roach
624ca7e03cSGreg Roach    /**
630cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
64961ec755SGreg Roach     *
65961ec755SGreg Roach     * @return string
66961ec755SGreg Roach     */
6749a243cbSGreg Roach    public function title(): string
68c1010edaSGreg Roach    {
69bbb76c12SGreg Roach        /* I18N: Name of a module/tab on the individual page. */
70bbb76c12SGreg Roach        return I18N::translate('Facts and events');
713763c3f2SGreg Roach    }
723763c3f2SGreg Roach
73961ec755SGreg Roach    /**
74961ec755SGreg Roach     * A sentence describing what this module does.
75961ec755SGreg Roach     *
76961ec755SGreg Roach     * @return string
77961ec755SGreg Roach     */
7849a243cbSGreg Roach    public function description(): string
79c1010edaSGreg Roach    {
80bbb76c12SGreg Roach        /* I18N: Description of the “Facts and events” module */
81bbb76c12SGreg Roach        return I18N::translate('A tab showing the facts and events of an individual.');
823763c3f2SGreg Roach    }
833763c3f2SGreg Roach
8449a243cbSGreg Roach    /**
8549a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
8649a243cbSGreg Roach     *
8749a243cbSGreg Roach     * @return int
8849a243cbSGreg Roach     */
89cbf4b7faSGreg Roach    public function defaultTabOrder(): int
90cbf4b7faSGreg Roach    {
91fb7a0427SGreg Roach        return 1;
923763c3f2SGreg Roach    }
933763c3f2SGreg Roach
943caaa4d2SGreg Roach    /**
953caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
963caaa4d2SGreg Roach     * options to create content.
973caaa4d2SGreg Roach     *
983caaa4d2SGreg Roach     * @param Individual $individual
993caaa4d2SGreg Roach     *
1003caaa4d2SGreg Roach     * @return bool
1013caaa4d2SGreg Roach     */
1028f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
103c1010edaSGreg Roach    {
1043763c3f2SGreg Roach        return false;
1053763c3f2SGreg Roach    }
1063763c3f2SGreg Roach
1073caaa4d2SGreg Roach    /**
1083caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1093caaa4d2SGreg Roach     *
1103caaa4d2SGreg Roach     * @param Individual $individual
1113caaa4d2SGreg Roach     *
1123caaa4d2SGreg Roach     * @return string
1133caaa4d2SGreg Roach     */
1149b34404bSGreg Roach    public function getTabContent(Individual $individual): string
115c1010edaSGreg Roach    {
116ee727175SGreg Roach        // Only include events of close relatives that are between birth and death
117ee727175SGreg Roach        $min_date = $individual->getEstimatedBirthDate();
118ee727175SGreg Roach        $max_date = $individual->getEstimatedDeathDate();
119ee727175SGreg Roach
1208eaf8709SGreg Roach        // Which facts and events are handled by other modules?
1218eaf8709SGreg Roach        $sidebar_facts = $this->module_service
12287cca37cSGreg Roach            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
123d0889c63SGreg Roach            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts());
1248eaf8709SGreg Roach
1258eaf8709SGreg Roach        $tab_facts = $this->module_service
12687cca37cSGreg Roach            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
127d0889c63SGreg Roach            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts());
1288eaf8709SGreg Roach
1298eaf8709SGreg Roach        $exclude_facts = $sidebar_facts->merge($tab_facts)->flatten();
1308eaf8709SGreg Roach
1313763c3f2SGreg Roach        // The individual’s own facts
13291a257a4SGreg Roach        $individual_facts = $individual->facts()
133d0889c63SGreg Roach            ->filter(fn (Fact $fact): bool => !$exclude_facts->contains($fact->tag()));
1343763c3f2SGreg Roach
13591a257a4SGreg Roach        $relative_facts = new Collection();
13691a257a4SGreg Roach
1373763c3f2SGreg Roach        // Add spouse-family facts
13839ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
13930158ae7SGreg Roach            foreach ($family->facts() as $fact) {
140d0889c63SGreg Roach                if (!$exclude_facts->contains($fact->tag()) && $fact->tag() !== 'FAM:CHAN') {
14191a257a4SGreg Roach                    $relative_facts->push($fact);
1423763c3f2SGreg Roach                }
1433763c3f2SGreg Roach            }
144ee727175SGreg Roach
14539ca88baSGreg Roach            $spouse = $family->spouse($individual);
146ee727175SGreg Roach
147ee727175SGreg Roach            if ($spouse instanceof Individual) {
1488b9cfadbSGreg Roach                $spouse_facts   = $this->spouseFacts($individual, $spouse, $min_date, $max_date);
14991a257a4SGreg Roach                $relative_facts = $relative_facts->merge($spouse_facts);
1503763c3f2SGreg Roach            }
1513763c3f2SGreg Roach
1528b9cfadbSGreg Roach            $child_facts    = $this->childFacts($individual, $family, '_CHIL', '', $min_date, $max_date);
15391a257a4SGreg Roach            $relative_facts = $relative_facts->merge($child_facts);
1543763c3f2SGreg Roach        }
155225e381fSGreg Roach
1568b9cfadbSGreg Roach        $parent_facts    = $this->parentFacts($individual, 1, $min_date, $max_date);
15791a257a4SGreg Roach        $relative_facts  = $relative_facts->merge($parent_facts);
1588b9cfadbSGreg Roach        $associate_facts = $this->associateFacts($individual);
15991a257a4SGreg Roach        $historic_facts  = $this->historicFacts($individual);
160225e381fSGreg Roach
16191a257a4SGreg Roach        $individual_facts = $individual_facts
16239ca88baSGreg Roach            ->merge($associate_facts)
16391a257a4SGreg Roach            ->merge($historic_facts)
16491a257a4SGreg Roach            ->merge($relative_facts);
1653763c3f2SGreg Roach
16691a257a4SGreg Roach        $individual_facts = Fact::sortFacts($individual_facts);
1673763c3f2SGreg Roach
168a8cd57e1SGreg Roach        return view('modules/personal_facts/tab', [
169225e381fSGreg Roach            'can_edit'            => $individual->canEdit(),
17069cdf014SGreg Roach            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
17191a257a4SGreg Roach            'has_associate_facts' => $associate_facts->isNotEmpty(),
17291a257a4SGreg Roach            'has_historic_facts'  => $historic_facts->isNotEmpty(),
17391a257a4SGreg Roach            'has_relative_facts'  => $relative_facts->isNotEmpty(),
174225e381fSGreg Roach            'individual'          => $individual,
17591a257a4SGreg Roach            'facts'               => $individual_facts,
176225e381fSGreg Roach        ]);
1773763c3f2SGreg Roach    }
1783763c3f2SGreg Roach
179ee727175SGreg Roach    /**
18091a257a4SGreg Roach     * Spouse facts that are shown on an individual’s page.
18191a257a4SGreg Roach     *
18291a257a4SGreg Roach     * @param Individual $individual Show events that occured during the lifetime of this individual
18391a257a4SGreg Roach     * @param Individual $spouse     Show events of this individual
18491a257a4SGreg Roach     * @param Date       $min_date
18591a257a4SGreg Roach     * @param Date       $max_date
18691a257a4SGreg Roach     *
18791a257a4SGreg Roach     * @return Collection<Fact>
18891a257a4SGreg Roach     */
18991a257a4SGreg Roach    private function spouseFacts(Individual $individual, Individual $spouse, Date $min_date, Date $max_date): Collection
19091a257a4SGreg Roach    {
19191a257a4SGreg Roach        $SHOW_RELATIVES_EVENTS = $individual->tree()->getPreference('SHOW_RELATIVES_EVENTS');
19291a257a4SGreg Roach
19391a257a4SGreg Roach        $death_of_a_spouse = [
194f7f3cc73SGreg Roach            'INDI:DEAT' => [
19591a257a4SGreg Roach                'M' => I18N::translate('Death of a husband'),
19691a257a4SGreg Roach                'F' => I18N::translate('Death of a wife'),
19791a257a4SGreg Roach                'U' => I18N::translate('Death of a spouse'),
19891a257a4SGreg Roach            ],
199f7f3cc73SGreg Roach            'INDI:BURI' => [
20091a257a4SGreg Roach                'M' => I18N::translate('Burial of a husband'),
20191a257a4SGreg Roach                'F' => I18N::translate('Burial of a wife'),
20291a257a4SGreg Roach                'U' => I18N::translate('Burial of a spouse'),
20391a257a4SGreg Roach            ],
204f7f3cc73SGreg Roach            'INDI:CREM' => [
20591a257a4SGreg Roach                'M' => I18N::translate('Cremation of a husband'),
20691a257a4SGreg Roach                'F' => I18N::translate('Cremation of a wife'),
20791a257a4SGreg Roach                'U' => I18N::translate('Cremation of a spouse'),
20891a257a4SGreg Roach            ],
20991a257a4SGreg Roach        ];
21091a257a4SGreg Roach
21191a257a4SGreg Roach        $facts = new Collection();
21291a257a4SGreg Roach
21391a257a4SGreg Roach        if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT_SPOU')) {
21491a257a4SGreg Roach            foreach ($spouse->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
21591a257a4SGreg Roach                if ($this->includeFact($fact, $min_date, $max_date)) {
216d0889c63SGreg Roach                    $facts[] = $this->convertEvent($fact, $death_of_a_spouse[$fact->tag()][$fact->record()->sex()]);
21791a257a4SGreg Roach                }
21891a257a4SGreg Roach            }
21991a257a4SGreg Roach        }
22091a257a4SGreg Roach
22191a257a4SGreg Roach        return $facts;
22291a257a4SGreg Roach    }
22391a257a4SGreg Roach
22491a257a4SGreg Roach    /**
225ee727175SGreg Roach     * Does a relative event occur within a date range (i.e. the individual's lifetime)?
226ee727175SGreg Roach     *
227ee727175SGreg Roach     * @param Fact $fact
228ee727175SGreg Roach     * @param Date $min_date
229ee727175SGreg Roach     * @param Date $max_date
230ee727175SGreg Roach     *
231ee727175SGreg Roach     * @return bool
232ee727175SGreg Roach     */
2338b9cfadbSGreg Roach    private function includeFact(Fact $fact, Date $min_date, Date $max_date): bool
234b3b1d905SGreg Roach    {
2352decada7SGreg Roach        $fact_date = $fact->date();
236ee727175SGreg Roach
237ee727175SGreg Roach        return $fact_date->isOK() && Date::compare($min_date, $fact_date) <= 0 && Date::compare($fact_date, $max_date) <= 0;
238ee727175SGreg Roach    }
239ee727175SGreg Roach
2403caaa4d2SGreg Roach    /**
2417bf6ca81SGreg Roach     * Convert an event into a special "event of a close relative".
2427bf6ca81SGreg Roach     *
2437bf6ca81SGreg Roach     * @param Fact   $fact
2449e65d053SGreg Roach     * @param string $type
2457bf6ca81SGreg Roach     *
2467bf6ca81SGreg Roach     * @return Fact
2477bf6ca81SGreg Roach     */
2489e65d053SGreg Roach    private function convertEvent(Fact $fact, string $type): Fact
24999ed8541SGreg Roach    {
2507bf6ca81SGreg Roach        $gedcom = $fact->gedcom();
2517bf6ca81SGreg Roach        $gedcom = preg_replace('/\n2 TYPE .*/', '', $gedcom);
2527bf6ca81SGreg Roach        $gedcom = preg_replace('/^1 .*/', "1 EVEN CLOSE_RELATIVE\n2 TYPE " . $type, $gedcom);
2537bf6ca81SGreg Roach
2549cda3358SGreg Roach        $converted = new Fact($gedcom, $fact->record(), $fact->id());
2559cda3358SGreg Roach
2569cda3358SGreg Roach        if ($fact->isPendingAddition()) {
2579cda3358SGreg Roach            $converted->setPendingAddition();
2589cda3358SGreg Roach        }
2599cda3358SGreg Roach
2609cda3358SGreg Roach        if ($fact->isPendingDeletion()) {
2619cda3358SGreg Roach            $converted->setPendingDeletion();
2629cda3358SGreg Roach        }
2639cda3358SGreg Roach
2649cda3358SGreg Roach        return $converted;
2657bf6ca81SGreg Roach    }
2667bf6ca81SGreg Roach
2677bf6ca81SGreg Roach    /**
2683763c3f2SGreg Roach     * Get the events of children and grandchildren.
2693763c3f2SGreg Roach     *
2703763c3f2SGreg Roach     * @param Individual $person
2713763c3f2SGreg Roach     * @param Family     $family
2723763c3f2SGreg Roach     * @param string     $option
2733763c3f2SGreg Roach     * @param string     $relation
274ee727175SGreg Roach     * @param Date       $min_date
275ee727175SGreg Roach     * @param Date       $max_date
2763763c3f2SGreg Roach     *
27791a257a4SGreg Roach     * @return Collection<Fact>
2783763c3f2SGreg Roach     */
27991a257a4SGreg Roach    private function childFacts(Individual $person, Family $family, string $option, string $relation, Date $min_date, Date $max_date): Collection
280c1010edaSGreg Roach    {
281f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
2823763c3f2SGreg Roach
2837bf6ca81SGreg Roach        $birth_of_a_child = [
284d0889c63SGreg Roach            'INDI:BIRT' => [
2857bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a son'),
2867bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a daughter'),
2877bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a child'),
2887bf6ca81SGreg Roach            ],
289d0889c63SGreg Roach            'INDI:CHR'  => [
2907bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a son'),
2917bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a daughter'),
2927bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a child'),
2937bf6ca81SGreg Roach            ],
294d0889c63SGreg Roach            'INDI:BAPM' => [
2957bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a son'),
2967bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a daughter'),
2977bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a child'),
2987bf6ca81SGreg Roach            ],
299d0889c63SGreg Roach            'INDI:ADOP' => [
3007bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a son'),
3017bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a daughter'),
3027bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a child'),
3037bf6ca81SGreg Roach            ],
3047bf6ca81SGreg Roach        ];
3057bf6ca81SGreg Roach
3067bf6ca81SGreg Roach        $birth_of_a_sibling = [
307d0889c63SGreg Roach            'INDI:BIRT' => [
3087bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a brother'),
3097bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a sister'),
3107bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a sibling'),
3117bf6ca81SGreg Roach            ],
312d0889c63SGreg Roach            'INDI:CHR'  => [
3137bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a brother'),
3147bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a sister'),
3157bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a sibling'),
3167bf6ca81SGreg Roach            ],
317d0889c63SGreg Roach            'INDI:BAPM' => [
3187bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a brother'),
3197bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a sister'),
3207bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a sibling'),
3217bf6ca81SGreg Roach            ],
322d0889c63SGreg Roach            'INDI:ADOP' => [
3237bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a brother'),
3247bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a sister'),
3257bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a sibling'),
3267bf6ca81SGreg Roach            ],
3277bf6ca81SGreg Roach        ];
3287bf6ca81SGreg Roach
3297bf6ca81SGreg Roach        $birth_of_a_half_sibling = [
330d0889c63SGreg Roach            'INDI:BIRT' => [
3317bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a half-brother'),
3327bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a half-sister'),
3337bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a half-sibling'),
3347bf6ca81SGreg Roach            ],
335d0889c63SGreg Roach            'INDI:CHR'  => [
3367bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a half-brother'),
3377bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a half-sister'),
3387bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a half-sibling'),
3397bf6ca81SGreg Roach            ],
340d0889c63SGreg Roach            'INDI:BAPM' => [
3417bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a half-brother'),
3427bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a half-sister'),
3437bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a half-sibling'),
3447bf6ca81SGreg Roach            ],
345d0889c63SGreg Roach            'INDI:ADOP' => [
3467bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a half-brother'),
3477bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a half-sister'),
3487bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a half-sibling'),
3497bf6ca81SGreg Roach            ],
3507bf6ca81SGreg Roach        ];
3517bf6ca81SGreg Roach
3527bf6ca81SGreg Roach        $birth_of_a_grandchild = [
353d0889c63SGreg Roach            'INDI:BIRT' => [
3547bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a grandson'),
3557bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a granddaughter'),
3567bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a grandchild'),
3577bf6ca81SGreg Roach            ],
358d0889c63SGreg Roach            'INDI:CHR'  => [
3597bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a grandson'),
3607bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a granddaughter'),
3617bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a grandchild'),
3627bf6ca81SGreg Roach            ],
363d0889c63SGreg Roach            'INDI:BAPM' => [
3647bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a grandson'),
3657bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a granddaughter'),
3667bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
3677bf6ca81SGreg Roach            ],
368d0889c63SGreg Roach            'INDI:ADOP' => [
3697bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a grandson'),
3707bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a granddaughter'),
3717bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a grandchild'),
3727bf6ca81SGreg Roach            ],
3737bf6ca81SGreg Roach        ];
3747bf6ca81SGreg Roach
3757bf6ca81SGreg Roach        $birth_of_a_grandchild1 = [
376d0889c63SGreg Roach            'INDI:BIRT' => [
3777bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Birth of a grandson'),
3787bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Birth of a granddaughter'),
3797bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a grandchild'),
3807bf6ca81SGreg Roach            ],
381d0889c63SGreg Roach            'INDI:CHR'  => [
3827bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Christening of a grandson'),
3837bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Christening of a granddaughter'),
3847bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a grandchild'),
3857bf6ca81SGreg Roach            ],
386d0889c63SGreg Roach            'INDI:BAPM' => [
3877bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Baptism of a grandson'),
3887bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Baptism of a granddaughter'),
3897bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
3907bf6ca81SGreg Roach            ],
391d0889c63SGreg Roach            'INDI:ADOP' => [
3927bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Adoption of a grandson'),
3937bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Adoption of a granddaughter'),
3947bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a grandchild'),
3957bf6ca81SGreg Roach            ],
3967bf6ca81SGreg Roach        ];
3977bf6ca81SGreg Roach
3987bf6ca81SGreg Roach        $birth_of_a_grandchild2 = [
399d0889c63SGreg Roach            'INDI:BIRT' => [
4003d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Birth of a grandson'),
4013d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Birth of a granddaughter'),
4027bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a grandchild'),
4037bf6ca81SGreg Roach            ],
404d0889c63SGreg Roach            'INDI:CHR'  => [
4053d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Christening of a grandson'),
4063d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Christening of a granddaughter'),
4077bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a grandchild'),
4087bf6ca81SGreg Roach            ],
409d0889c63SGreg Roach            'INDI:BAPM' => [
4103d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Baptism of a grandson'),
4113d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Baptism of a granddaughter'),
4127bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
4137bf6ca81SGreg Roach            ],
414d0889c63SGreg Roach            'INDI:ADOP' => [
4153d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Adoption of a grandson'),
4163d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Adoption of a granddaughter'),
4177bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a grandchild'),
4187bf6ca81SGreg Roach            ],
4197bf6ca81SGreg Roach        ];
4207bf6ca81SGreg Roach
4217bf6ca81SGreg Roach        $death_of_a_child = [
422d0889c63SGreg Roach            'INDI:DEAT' => [
4237bf6ca81SGreg Roach                'M' => I18N::translate('Death of a son'),
4247bf6ca81SGreg Roach                'F' => I18N::translate('Death of a daughter'),
4257bf6ca81SGreg Roach                'U' => I18N::translate('Death of a child'),
4267bf6ca81SGreg Roach            ],
427d0889c63SGreg Roach            'INDI:BURI' => [
4287bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a son'),
4297bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a daughter'),
4307bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a child'),
4317bf6ca81SGreg Roach            ],
432d0889c63SGreg Roach            'INDI:CREM' => [
4337bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a son'),
4347bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a daughter'),
4357bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a child'),
4367bf6ca81SGreg Roach            ],
4377bf6ca81SGreg Roach        ];
4387bf6ca81SGreg Roach
4397bf6ca81SGreg Roach        $death_of_a_sibling = [
440d0889c63SGreg Roach            'INDI:DEAT' => [
4417bf6ca81SGreg Roach                'M' => I18N::translate('Death of a brother'),
4427bf6ca81SGreg Roach                'F' => I18N::translate('Death of a sister'),
4437bf6ca81SGreg Roach                'U' => I18N::translate('Death of a sibling'),
4447bf6ca81SGreg Roach            ],
445d0889c63SGreg Roach            'INDI:BURI' => [
4467bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a brother'),
4477bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a sister'),
4487bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a sibling'),
4497bf6ca81SGreg Roach            ],
450d0889c63SGreg Roach            'INDI:CREM' => [
4517bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a brother'),
4527bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a sister'),
4537bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a sibling'),
4547bf6ca81SGreg Roach            ],
4557bf6ca81SGreg Roach        ];
4567bf6ca81SGreg Roach
4577bf6ca81SGreg Roach        $death_of_a_half_sibling = [
458d0889c63SGreg Roach            'INDI:DEAT' => [
4597bf6ca81SGreg Roach                'M' => I18N::translate('Death of a half-brother'),
4607bf6ca81SGreg Roach                'F' => I18N::translate('Death of a half-sister'),
4617bf6ca81SGreg Roach                'U' => I18N::translate('Death of a half-sibling'),
4627bf6ca81SGreg Roach            ],
463d0889c63SGreg Roach            'INDI:BURI' => [
4647bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a half-brother'),
4657bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a half-sister'),
4667bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a half-sibling'),
4677bf6ca81SGreg Roach            ],
468d0889c63SGreg Roach            'INDI:CREM' => [
4697bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a half-brother'),
4707bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a half-sister'),
4717bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a half-sibling'),
4727bf6ca81SGreg Roach            ],
4737bf6ca81SGreg Roach        ];
4747bf6ca81SGreg Roach
4757bf6ca81SGreg Roach        $death_of_a_grandchild = [
476d0889c63SGreg Roach            'INDI:DEAT' => [
4777bf6ca81SGreg Roach                'M' => I18N::translate('Death of a grandson'),
4787bf6ca81SGreg Roach                'F' => I18N::translate('Death of a granddaughter'),
4797bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandchild'),
4807bf6ca81SGreg Roach            ],
481d0889c63SGreg Roach            'INDI:BURI' => [
4827bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a grandson'),
4837bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a granddaughter'),
4847bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandchild'),
4857bf6ca81SGreg Roach            ],
486d0889c63SGreg Roach            'INDI:CREM' => [
4877bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a grandson'),
4887bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a granddaughter'),
4897bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
4907bf6ca81SGreg Roach            ],
4917bf6ca81SGreg Roach        ];
4927bf6ca81SGreg Roach
4937bf6ca81SGreg Roach        $death_of_a_grandchild1 = [
494d0889c63SGreg Roach            'INDI:DEAT' => [
4957bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Death of a grandson'),
4967bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Death of a granddaughter'),
4977bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandchild'),
4987bf6ca81SGreg Roach            ],
499d0889c63SGreg Roach            'INDI:BURI' => [
5007bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Burial of a grandson'),
5017bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Burial of a granddaughter'),
5027bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandchild'),
5037bf6ca81SGreg Roach            ],
504d0889c63SGreg Roach            'INDI:CREM' => [
5057bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Cremation of a grandson'),
5067bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Cremation of a granddaughter'),
5077bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
5087bf6ca81SGreg Roach            ],
5097bf6ca81SGreg Roach        ];
5107bf6ca81SGreg Roach
5117bf6ca81SGreg Roach        $death_of_a_grandchild2 = [
512d0889c63SGreg Roach            'INDI:DEAT' => [
5133d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Death of a grandson'),
5143d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Death of a granddaughter'),
5157bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandchild'),
5167bf6ca81SGreg Roach            ],
517d0889c63SGreg Roach            'INDI:BURI' => [
5183d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Burial of a grandson'),
5193d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Burial of a granddaughter'),
5207bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandchild'),
5217bf6ca81SGreg Roach            ],
522d0889c63SGreg Roach            'INDI:CREM' => [
5233d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Cremation of a grandson'),
5243d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Cremation of a granddaughter'),
5257bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a grandchild'),
5267bf6ca81SGreg Roach            ],
5277bf6ca81SGreg Roach        ];
5287bf6ca81SGreg Roach
5297bf6ca81SGreg Roach        $marriage_of_a_child = [
5307bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a son'),
5317bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a daughter'),
5327bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a child'),
5337bf6ca81SGreg Roach        ];
5347bf6ca81SGreg Roach
5357bf6ca81SGreg Roach        $marriage_of_a_grandchild = [
5367bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a grandson'),
5377bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a granddaughter'),
5387bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a grandchild'),
5397bf6ca81SGreg Roach        ];
5407bf6ca81SGreg Roach
5417bf6ca81SGreg Roach        $marriage_of_a_grandchild1 = [
5427bf6ca81SGreg Roach            'M' => I18N::translateContext('daughter’s son', 'Marriage of a grandson'),
5437bf6ca81SGreg Roach            'F' => I18N::translateContext('daughter’s daughter', 'Marriage of a granddaughter'),
5447bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a grandchild'),
5457bf6ca81SGreg Roach        ];
5467bf6ca81SGreg Roach
5477bf6ca81SGreg Roach        $marriage_of_a_grandchild2 = [
5483d394ce7SGreg Roach            'M' => I18N::translateContext('son’s son', 'Marriage of a grandson'),
5493d394ce7SGreg Roach            'F' => I18N::translateContext('son’s daughter', 'Marriage of a granddaughter'),
5507bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a grandchild'),
5517bf6ca81SGreg Roach        ];
5527bf6ca81SGreg Roach
5537bf6ca81SGreg Roach        $marriage_of_a_sibling = [
5547bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a brother'),
5557bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a sister'),
5567bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a sibling'),
5577bf6ca81SGreg Roach        ];
5587bf6ca81SGreg Roach
5597bf6ca81SGreg Roach        $marriage_of_a_half_sibling = [
5607bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a half-brother'),
5617bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a half-sister'),
5627bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a half-sibling'),
5637bf6ca81SGreg Roach        ];
5647bf6ca81SGreg Roach
56591a257a4SGreg Roach        $facts = new Collection();
5663763c3f2SGreg Roach
5673763c3f2SGreg Roach        // Deal with recursion.
568*05babb96SGreg Roach        if ($option === '_CHIL') {
5693763c3f2SGreg Roach            // Add grandchildren
57039ca88baSGreg Roach            foreach ($family->children() as $child) {
57139ca88baSGreg Roach                foreach ($child->spouseFamilies() as $cfamily) {
57239ca88baSGreg Roach                    switch ($child->sex()) {
5733763c3f2SGreg Roach                        case 'M':
5748b9cfadbSGreg Roach                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
5753763c3f2SGreg Roach                                $facts[] = $fact;
5763763c3f2SGreg Roach                            }
5773763c3f2SGreg Roach                            break;
5783763c3f2SGreg Roach                        case 'F':
5798b9cfadbSGreg Roach                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
5803763c3f2SGreg Roach                                $facts[] = $fact;
5813763c3f2SGreg Roach                            }
5823763c3f2SGreg Roach                            break;
5833763c3f2SGreg Roach                        default:
5848b9cfadbSGreg Roach                            foreach ($this->childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
5853763c3f2SGreg Roach                                $facts[] = $fact;
5863763c3f2SGreg Roach                            }
5873763c3f2SGreg Roach                            break;
5883763c3f2SGreg Roach                    }
5893763c3f2SGreg Roach                }
5903763c3f2SGreg Roach            }
5913763c3f2SGreg Roach        }
5923763c3f2SGreg Roach
5933763c3f2SGreg Roach        // For each child in the family
59439ca88baSGreg Roach        foreach ($family->children() as $child) {
59522d65e5aSGreg Roach            if ($child->xref() === $person->xref()) {
5963763c3f2SGreg Roach                // We are not our own sibling!
5973763c3f2SGreg Roach                continue;
5983763c3f2SGreg Roach            }
5993763c3f2SGreg Roach            // add child’s birth
600dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option))) {
6017bf6ca81SGreg Roach                foreach ($child->facts(['BIRT', 'CHR', 'BAPM', 'ADOP']) as $fact) {
6023763c3f2SGreg Roach                    // Always show _BIRT_CHIL, even if the dates are not known
60322d65e5aSGreg Roach                    if ($option === '_CHIL' || $this->includeFact($fact, $min_date, $max_date)) {
6047bf6ca81SGreg Roach                        switch ($option) {
6057bf6ca81SGreg Roach                            case '_GCHI':
6067bf6ca81SGreg Roach                                switch ($relation) {
6077bf6ca81SGreg Roach                                    case 'dau':
608d0889c63SGreg Roach                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild1[$fact->tag()][$fact->record()->sex()]);
6097bf6ca81SGreg Roach                                        break;
6107bf6ca81SGreg Roach                                    case 'son':
611d0889c63SGreg Roach                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild2[$fact->tag()][$fact->record()->sex()]);
6127bf6ca81SGreg Roach                                        break;
6137bf6ca81SGreg Roach                                    case 'chil':
614d0889c63SGreg Roach                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild[$fact->tag()][$fact->record()->sex()]);
6157bf6ca81SGreg Roach                                        break;
6167bf6ca81SGreg Roach                                }
6177bf6ca81SGreg Roach                                break;
6187bf6ca81SGreg Roach                            case '_SIBL':
619d0889c63SGreg Roach                                $facts[] = $this->convertEvent($fact, $birth_of_a_sibling[$fact->tag()][$fact->record()->sex()]);
6207bf6ca81SGreg Roach                                break;
6217bf6ca81SGreg Roach                            case '_HSIB':
622d0889c63SGreg Roach                                $facts[] = $this->convertEvent($fact, $birth_of_a_half_sibling[$fact->tag()][$fact->record()->sex()]);
6237bf6ca81SGreg Roach                                break;
6247bf6ca81SGreg Roach                            case '_CHIL':
625d0889c63SGreg Roach                                $facts[] = $this->convertEvent($fact, $birth_of_a_child[$fact->tag()][$fact->record()->sex()]);
6267bf6ca81SGreg Roach                                break;
6273763c3f2SGreg Roach                        }
6283763c3f2SGreg Roach                    }
6293763c3f2SGreg Roach                }
6303763c3f2SGreg Roach            }
6313763c3f2SGreg Roach            // add child’s death
632dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option))) {
6337bf6ca81SGreg Roach                foreach ($child->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
6348b9cfadbSGreg Roach                    if ($this->includeFact($fact, $min_date, $max_date)) {
6357bf6ca81SGreg Roach                        switch ($option) {
6367bf6ca81SGreg Roach                            case '_GCHI':
6377bf6ca81SGreg Roach                                switch ($relation) {
6387bf6ca81SGreg Roach                                    case 'dau':
639d0889c63SGreg Roach                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild1[$fact->tag()][$fact->record()->sex()]);
6407bf6ca81SGreg Roach                                        break;
6417bf6ca81SGreg Roach                                    case 'son':
642d0889c63SGreg Roach                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild2[$fact->tag()][$fact->record()->sex()]);
6437bf6ca81SGreg Roach                                        break;
6447bf6ca81SGreg Roach                                    case 'chi':
645d0889c63SGreg Roach                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild[$fact->tag()][$fact->record()->sex()]);
6467bf6ca81SGreg Roach                                        break;
6477bf6ca81SGreg Roach                                }
6487bf6ca81SGreg Roach                                break;
6497bf6ca81SGreg Roach                            case '_SIBL':
650d0889c63SGreg Roach                                $facts[] = $this->convertEvent($fact, $death_of_a_sibling[$fact->tag()][$fact->record()->sex()]);
6517bf6ca81SGreg Roach                                break;
6527bf6ca81SGreg Roach                            case '_HSIB':
653d0889c63SGreg Roach                                $facts[] = $this->convertEvent($fact, $death_of_a_half_sibling[$fact->tag()][$fact->record()->sex()]);
6547bf6ca81SGreg Roach                                break;
655718b3bc2SGreg Roach                            case '_CHIL':
656d0889c63SGreg Roach                                $facts[] = $this->convertEvent($fact, $death_of_a_child[$fact->tag()][$fact->record()->sex()]);
6577bf6ca81SGreg Roach                                break;
6583763c3f2SGreg Roach                        }
6593763c3f2SGreg Roach                    }
6603763c3f2SGreg Roach                }
6613763c3f2SGreg Roach            }
6627bf6ca81SGreg Roach
6633763c3f2SGreg Roach            // add child’s marriage
664dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
66539ca88baSGreg Roach                foreach ($child->spouseFamilies() as $sfamily) {
6668d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
6678b9cfadbSGreg Roach                        if ($this->includeFact($fact, $min_date, $max_date)) {
6687bf6ca81SGreg Roach                            switch ($option) {
6697bf6ca81SGreg Roach                                case '_GCHI':
6707bf6ca81SGreg Roach                                    switch ($relation) {
6717bf6ca81SGreg Roach                                        case 'dau':
6725d15adbbSJonathan Jaubart                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild1[$child->sex()]);
6737bf6ca81SGreg Roach                                            break;
6747bf6ca81SGreg Roach                                        case 'son':
6755d15adbbSJonathan Jaubart                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild2[$child->sex()]);
6767bf6ca81SGreg Roach                                            break;
6777bf6ca81SGreg Roach                                        case 'chi':
6785d15adbbSJonathan Jaubart                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild[$child->sex()]);
6797bf6ca81SGreg Roach                                            break;
6807bf6ca81SGreg Roach                                    }
6817bf6ca81SGreg Roach                                    break;
6827bf6ca81SGreg Roach                                case '_SIBL':
6835d15adbbSJonathan Jaubart                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_sibling[$child->sex()]);
6847bf6ca81SGreg Roach                                    break;
6857bf6ca81SGreg Roach                                case '_HSIB':
6865d15adbbSJonathan Jaubart                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_half_sibling[$child->sex()]);
6877bf6ca81SGreg Roach                                    break;
6887bf6ca81SGreg Roach                                case '_CHIL':
6895d15adbbSJonathan Jaubart                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_child[$child->sex()]);
6907bf6ca81SGreg Roach                                    break;
6913763c3f2SGreg Roach                            }
6923763c3f2SGreg Roach                        }
6933763c3f2SGreg Roach                    }
6943763c3f2SGreg Roach                }
6953763c3f2SGreg Roach            }
6963763c3f2SGreg Roach        }
6973763c3f2SGreg Roach
6983763c3f2SGreg Roach        return $facts;
6993763c3f2SGreg Roach    }
7003763c3f2SGreg Roach
7013763c3f2SGreg Roach    /**
7023763c3f2SGreg Roach     * Get the events of parents and grandparents.
7033763c3f2SGreg Roach     *
7043763c3f2SGreg Roach     * @param Individual $person
705cbc1590aSGreg Roach     * @param int        $sosa
706ee727175SGreg Roach     * @param Date       $min_date
707ee727175SGreg Roach     * @param Date       $max_date
7083763c3f2SGreg Roach     *
70991a257a4SGreg Roach     * @return Collection<Fact>
7103763c3f2SGreg Roach     */
71191a257a4SGreg Roach    private function parentFacts(Individual $person, int $sosa, Date $min_date, Date $max_date): Collection
712c1010edaSGreg Roach    {
713f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
7143763c3f2SGreg Roach
7157bf6ca81SGreg Roach        $death_of_a_parent = [
716d0889c63SGreg Roach            'INDI:DEAT' => [
7177bf6ca81SGreg Roach                'M' => I18N::translate('Death of a father'),
7187bf6ca81SGreg Roach                'F' => I18N::translate('Death of a mother'),
7197bf6ca81SGreg Roach                'U' => I18N::translate('Death of a parent'),
7207bf6ca81SGreg Roach            ],
721d0889c63SGreg Roach            'INDI:BURI' => [
7227bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a father'),
7237bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a mother'),
7247bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a parent'),
7257bf6ca81SGreg Roach            ],
726d0889c63SGreg Roach            'INDI:CREM' => [
7277bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a father'),
7287bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a mother'),
7297bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a parent'),
7307bf6ca81SGreg Roach            ],
7317bf6ca81SGreg Roach        ];
7327bf6ca81SGreg Roach
7337bf6ca81SGreg Roach        $death_of_a_grandparent = [
734d0889c63SGreg Roach            'INDI:DEAT' => [
7357bf6ca81SGreg Roach                'M' => I18N::translate('Death of a grandfather'),
7367bf6ca81SGreg Roach                'F' => I18N::translate('Death of a grandmother'),
7377bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandparent'),
7387bf6ca81SGreg Roach            ],
739d0889c63SGreg Roach            'INDI:BURI' => [
7407bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a grandfather'),
7417bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a grandmother'),
7427bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandparent'),
7437bf6ca81SGreg Roach            ],
744d0889c63SGreg Roach            'INDI:CREM' => [
7457bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a grandfather'),
7467bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a grandmother'),
7470b73ecfcSGreg Roach                'U' => I18N::translate('Cremation of a grandparent'),
7487bf6ca81SGreg Roach            ],
7497bf6ca81SGreg Roach        ];
7507bf6ca81SGreg Roach
7517bf6ca81SGreg Roach        $death_of_a_maternal_grandparent = [
752d0889c63SGreg Roach            'INDI:DEAT' => [
7530b73ecfcSGreg Roach                'M' => I18N::translate('Death of a maternal grandfather'),
7540b73ecfcSGreg Roach                'F' => I18N::translate('Death of a maternal grandmother'),
7557bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandparent'),
7567bf6ca81SGreg Roach            ],
757d0889c63SGreg Roach            'INDI:BURI' => [
7580b73ecfcSGreg Roach                'M' => I18N::translate('Burial of a maternal grandfather'),
7590b73ecfcSGreg Roach                'F' => I18N::translate('Burial of a maternal grandmother'),
7607bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandparent'),
7617bf6ca81SGreg Roach            ],
762d0889c63SGreg Roach            'INDI:CREM' => [
7630b73ecfcSGreg Roach                'M' => I18N::translate('Cremation of a maternal grandfather'),
7640b73ecfcSGreg Roach                'F' => I18N::translate('Cremation of a maternal grandmother'),
7650b73ecfcSGreg Roach                'U' => I18N::translate('Cremation of a grandparent'),
7667bf6ca81SGreg Roach            ],
7677bf6ca81SGreg Roach        ];
7687bf6ca81SGreg Roach
7697bf6ca81SGreg Roach        $death_of_a_paternal_grandparent = [
770d0889c63SGreg Roach            'INDI:DEAT' => [
7710b73ecfcSGreg Roach                'M' => I18N::translate('Death of a paternal grandfather'),
7720b73ecfcSGreg Roach                'F' => I18N::translate('Death of a paternal grandmother'),
7737bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandparent'),
7747bf6ca81SGreg Roach            ],
775d0889c63SGreg Roach            'INDI:BURI' => [
7760b73ecfcSGreg Roach                'M' => I18N::translate('Burial of a paternal grandfather'),
7770b73ecfcSGreg Roach                'F' => I18N::translate('Burial of a paternal grandmother'),
7787bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandparent'),
7797bf6ca81SGreg Roach            ],
780d0889c63SGreg Roach            'INDI:CREM' => [
7810b73ecfcSGreg Roach                'M' => I18N::translate('Cremation of a paternal grandfather'),
7820b73ecfcSGreg Roach                'F' => I18N::translate('Cremation of a paternal grandmother'),
7837bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a grandparent'),
7847bf6ca81SGreg Roach            ],
7857bf6ca81SGreg Roach        ];
7867bf6ca81SGreg Roach
7877bf6ca81SGreg Roach        $marriage_of_a_parent = [
7887bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a father'),
7897bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a mother'),
7907bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a parent'),
7917bf6ca81SGreg Roach        ];
7927bf6ca81SGreg Roach
79391a257a4SGreg Roach        $facts = new Collection();
7943763c3f2SGreg Roach
7950b73ecfcSGreg Roach        if ($sosa === 1) {
79639ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
7973763c3f2SGreg Roach                // Add siblings
7988b9cfadbSGreg Roach                foreach ($this->childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
7993763c3f2SGreg Roach                    $facts[] = $fact;
8003763c3f2SGreg Roach                }
80139ca88baSGreg Roach                foreach ($family->spouses() as $spouse) {
80239ca88baSGreg Roach                    foreach ($spouse->spouseFamilies() as $sfamily) {
8033763c3f2SGreg Roach                        if ($family !== $sfamily) {
8043763c3f2SGreg Roach                            // Add half-siblings
8058b9cfadbSGreg Roach                            foreach ($this->childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
8063763c3f2SGreg Roach                                $facts[] = $fact;
8073763c3f2SGreg Roach                            }
8083763c3f2SGreg Roach                        }
8093763c3f2SGreg Roach                    }
8103763c3f2SGreg Roach                    // Add grandparents
81122d65e5aSGreg Roach                    foreach ($this->parentFacts($spouse, $spouse->sex() === 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
8123763c3f2SGreg Roach                        $facts[] = $fact;
8133763c3f2SGreg Roach                    }
8143763c3f2SGreg Roach                }
8153763c3f2SGreg Roach            }
8163763c3f2SGreg Roach
817dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
8183763c3f2SGreg Roach                // add father/mother marriages
81939ca88baSGreg Roach                foreach ($person->childFamilies() as $sfamily) {
8208d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
8218b9cfadbSGreg Roach                        if ($this->includeFact($fact, $min_date, $max_date)) {
8223763c3f2SGreg Roach                            // marriage of parents (to each other)
8237bf6ca81SGreg Roach                            $facts[] = $this->convertEvent($fact, I18N::translate('Marriage of parents'));
8243763c3f2SGreg Roach                        }
8253763c3f2SGreg Roach                    }
8263763c3f2SGreg Roach                }
82739ca88baSGreg Roach                foreach ($person->childStepFamilies() as $sfamily) {
8288d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
8298b9cfadbSGreg Roach                        if ($this->includeFact($fact, $min_date, $max_date)) {
8303763c3f2SGreg Roach                            // marriage of a parent (to another spouse)
8317bf6ca81SGreg Roach                            $facts[] = $this->convertEvent($fact, $marriage_of_a_parent['U']);
8323763c3f2SGreg Roach                        }
8333763c3f2SGreg Roach                    }
8343763c3f2SGreg Roach                }
8353763c3f2SGreg Roach            }
8363763c3f2SGreg Roach        }
8373763c3f2SGreg Roach
83839ca88baSGreg Roach        foreach ($person->childFamilies() as $family) {
83939ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
840dec352c1SGreg Roach                if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa === 1 ? '_PARE' : '_GPAR'))) {
8417bf6ca81SGreg Roach                    foreach ($parent->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
842e4cf93e3SGreg Roach                        // Show death of parent when it happened prior to birth
843e4cf93e3SGreg Roach                        if ($sosa === 1 && Date::compare($fact->date(), $min_date) < 0 || $this->includeFact($fact, $min_date, $max_date)) {
8443763c3f2SGreg Roach                            switch ($sosa) {
8453763c3f2SGreg Roach                                case 1:
846d0889c63SGreg Roach                                    $facts[] = $this->convertEvent($fact, $death_of_a_parent[$fact->tag()][$fact->record()->sex()]);
8473763c3f2SGreg Roach                                    break;
8483763c3f2SGreg Roach                                case 2:
8493763c3f2SGreg Roach                                case 3:
85069c89463SGreg Roach                                    switch ($person->sex()) {
8517bf6ca81SGreg Roach                                        case 'M':
852d0889c63SGreg Roach                                            $facts[] = $this->convertEvent($fact, $death_of_a_paternal_grandparent[$fact->tag()][$fact->record()->sex()]);
8533763c3f2SGreg Roach                                            break;
8547bf6ca81SGreg Roach                                        case 'F':
855d0889c63SGreg Roach                                            $facts[] = $this->convertEvent($fact, $death_of_a_maternal_grandparent[$fact->tag()][$fact->record()->sex()]);
8567bf6ca81SGreg Roach                                            break;
8577bf6ca81SGreg Roach                                        default:
858d0889c63SGreg Roach                                            $facts[] = $this->convertEvent($fact, $death_of_a_grandparent[$fact->tag()][$fact->record()->sex()]);
8597bf6ca81SGreg Roach                                            break;
8607bf6ca81SGreg Roach                                    }
8613763c3f2SGreg Roach                            }
8623763c3f2SGreg Roach                        }
8633763c3f2SGreg Roach                    }
8643763c3f2SGreg Roach                }
8653763c3f2SGreg Roach            }
8663763c3f2SGreg Roach        }
8673763c3f2SGreg Roach
8683763c3f2SGreg Roach        return $facts;
8693763c3f2SGreg Roach    }
8703763c3f2SGreg Roach
8713763c3f2SGreg Roach    /**
8723763c3f2SGreg Roach     * Get the events of associates.
8733763c3f2SGreg Roach     *
8743763c3f2SGreg Roach     * @param Individual $person
8753763c3f2SGreg Roach     *
87691a257a4SGreg Roach     * @return Collection<Fact>
8773763c3f2SGreg Roach     */
87891a257a4SGreg Roach    private function associateFacts(Individual $person): Collection
879c1010edaSGreg Roach    {
88013abd6f3SGreg Roach        $facts = [];
8813763c3f2SGreg Roach
882907c1109SGreg Roach        $asso1 = $person->linkedIndividuals('ASSO');
883907c1109SGreg Roach        $asso2 = $person->linkedIndividuals('_ASSO');
884907c1109SGreg Roach        $asso3 = $person->linkedFamilies('ASSO');
885907c1109SGreg Roach        $asso4 = $person->linkedFamilies('_ASSO');
886907c1109SGreg Roach
887907c1109SGreg Roach        $associates = $asso1->merge($asso2)->merge($asso3)->merge($asso4);
888907c1109SGreg Roach
8893763c3f2SGreg Roach        foreach ($associates as $associate) {
89030158ae7SGreg Roach            foreach ($associate->facts() as $fact) {
891907c1109SGreg Roach                if (preg_match('/\n\d _?ASSO @' . $person->xref() . '@/', $fact->gedcom())) {
8923763c3f2SGreg Roach                    // Extract the important details from the fact
893d0889c63SGreg Roach                    $factrec = explode("\n", $fact->gedcom(), 2)[0];
894138ca96cSGreg Roach                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
8953763c3f2SGreg Roach                        $factrec .= $match[0];
8963763c3f2SGreg Roach                    }
897138ca96cSGreg Roach                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
8983763c3f2SGreg Roach                        $factrec .= $match[0];
8993763c3f2SGreg Roach                    }
9003763c3f2SGreg Roach                    if ($associate instanceof Family) {
90139ca88baSGreg Roach                        foreach ($associate->spouses() as $spouse) {
902c0935879SGreg Roach                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
9033763c3f2SGreg Roach                        }
9043763c3f2SGreg Roach                    } else {
905c0935879SGreg Roach                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
9063763c3f2SGreg Roach                    }
9073763c3f2SGreg Roach                    $facts[] = new Fact($factrec, $associate, 'asso');
9083763c3f2SGreg Roach                }
9093763c3f2SGreg Roach            }
9103763c3f2SGreg Roach        }
9113763c3f2SGreg Roach
91291a257a4SGreg Roach        return new Collection($facts);
91391a257a4SGreg Roach    }
91491a257a4SGreg Roach
91591a257a4SGreg Roach    /**
91691a257a4SGreg Roach     * Get any historical events.
91791a257a4SGreg Roach     *
91891a257a4SGreg Roach     * @param Individual $individual
91991a257a4SGreg Roach     *
92091a257a4SGreg Roach     * @return Collection<Fact>
92191a257a4SGreg Roach     */
92291a257a4SGreg Roach    private function historicFacts(Individual $individual): Collection
92391a257a4SGreg Roach    {
92491a257a4SGreg Roach        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
92591a257a4SGreg Roach            ->map(static function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
92691a257a4SGreg Roach                return $module->historicEventsForIndividual($individual);
92791a257a4SGreg Roach            })
92891a257a4SGreg Roach            ->flatten();
92991a257a4SGreg Roach    }
93091a257a4SGreg Roach
93191a257a4SGreg Roach    /**
93291a257a4SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
93391a257a4SGreg Roach     *
93491a257a4SGreg Roach     * @param Individual $individual
93591a257a4SGreg Roach     *
93691a257a4SGreg Roach     * @return bool
93791a257a4SGreg Roach     */
93891a257a4SGreg Roach    public function hasTabContent(Individual $individual): bool
93991a257a4SGreg Roach    {
94091a257a4SGreg Roach        return true;
94191a257a4SGreg Roach    }
94291a257a4SGreg Roach
94391a257a4SGreg Roach    /**
94491a257a4SGreg Roach     * Can this tab load asynchronously?
94591a257a4SGreg Roach     *
94691a257a4SGreg Roach     * @return bool
94791a257a4SGreg Roach     */
94891a257a4SGreg Roach    public function canLoadAjax(): bool
94991a257a4SGreg Roach    {
95091a257a4SGreg Roach        return false;
9513763c3f2SGreg Roach    }
9528eaf8709SGreg Roach
9538eaf8709SGreg Roach    /**
9548eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
9558eaf8709SGreg Roach     *
956b5c8fd7eSGreg Roach     * @return Collection<string>
9578eaf8709SGreg Roach     */
9588eaf8709SGreg Roach    public function supportedFacts(): Collection
9598eaf8709SGreg Roach    {
9608eaf8709SGreg Roach        // We don't actually displaye these facts, but they are displayed
9618eaf8709SGreg Roach        // outside the tabs/sidebar systems. This just forces them to be excluded here.
962d0889c63SGreg Roach        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
9638eaf8709SGreg Roach    }
9643763c3f2SGreg Roach}
965