xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 91a257a4cd149099de2291e3d62a1b61f2f06bcd)
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
32dec352c1SGreg Roachuse function str_contains;
33dec352c1SGreg Roach
343763c3f2SGreg Roach/**
353763c3f2SGreg Roach * Class IndividualFactsTabModule
363763c3f2SGreg Roach */
3737eb8894SGreg Roachclass IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
38c1010edaSGreg Roach{
3949a243cbSGreg Roach    use ModuleTabTrait;
4049a243cbSGreg Roach
41*91a257a4SGreg Roach    private ModuleService $module_service;
424ca7e03cSGreg Roach
43*91a257a4SGreg Roach    private ClipboardService $clipboard_service;
442adcbd9aSGreg Roach
454ca7e03cSGreg Roach    /**
460874af26SRichard Cissée     * IndividualFactsTabModule constructor.
474ca7e03cSGreg Roach     *
484ca7e03cSGreg Roach     * @param ModuleService    $module_service
492adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
504ca7e03cSGreg Roach     */
512adcbd9aSGreg Roach    public function __construct(ModuleService $module_service, ClipboardService $clipboard_service)
525bdbe281SGreg Roach    {
534ca7e03cSGreg Roach        $this->module_service    = $module_service;
542adcbd9aSGreg Roach        $this->clipboard_service = $clipboard_service;
554ca7e03cSGreg Roach    }
564ca7e03cSGreg Roach
574ca7e03cSGreg Roach    /**
580cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
59961ec755SGreg Roach     *
60961ec755SGreg Roach     * @return string
61961ec755SGreg Roach     */
6249a243cbSGreg Roach    public function title(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Name of a module/tab on the individual page. */
65bbb76c12SGreg Roach        return I18N::translate('Facts and events');
663763c3f2SGreg Roach    }
673763c3f2SGreg Roach
68961ec755SGreg Roach    /**
69961ec755SGreg Roach     * A sentence describing what this module does.
70961ec755SGreg Roach     *
71961ec755SGreg Roach     * @return string
72961ec755SGreg Roach     */
7349a243cbSGreg Roach    public function description(): string
74c1010edaSGreg Roach    {
75bbb76c12SGreg Roach        /* I18N: Description of the “Facts and events” module */
76bbb76c12SGreg Roach        return I18N::translate('A tab showing the facts and events of an individual.');
773763c3f2SGreg Roach    }
783763c3f2SGreg Roach
7949a243cbSGreg Roach    /**
8049a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
8149a243cbSGreg Roach     *
8249a243cbSGreg Roach     * @return int
8349a243cbSGreg Roach     */
84cbf4b7faSGreg Roach    public function defaultTabOrder(): int
85cbf4b7faSGreg Roach    {
86fb7a0427SGreg Roach        return 1;
873763c3f2SGreg Roach    }
883763c3f2SGreg Roach
893caaa4d2SGreg Roach    /**
903caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
913caaa4d2SGreg Roach     * options to create content.
923caaa4d2SGreg Roach     *
933caaa4d2SGreg Roach     * @param Individual $individual
943caaa4d2SGreg Roach     *
953caaa4d2SGreg Roach     * @return bool
963caaa4d2SGreg Roach     */
978f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
98c1010edaSGreg Roach    {
993763c3f2SGreg Roach        return false;
1003763c3f2SGreg Roach    }
1013763c3f2SGreg Roach
1023caaa4d2SGreg Roach    /**
1033caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1043caaa4d2SGreg Roach     *
1053caaa4d2SGreg Roach     * @param Individual $individual
1063caaa4d2SGreg Roach     *
1073caaa4d2SGreg Roach     * @return string
1083caaa4d2SGreg Roach     */
1099b34404bSGreg Roach    public function getTabContent(Individual $individual): string
110c1010edaSGreg Roach    {
111ee727175SGreg Roach        // Only include events of close relatives that are between birth and death
112ee727175SGreg Roach        $min_date = $individual->getEstimatedBirthDate();
113ee727175SGreg Roach        $max_date = $individual->getEstimatedDeathDate();
114ee727175SGreg Roach
1158eaf8709SGreg Roach        // Which facts and events are handled by other modules?
1168eaf8709SGreg Roach        $sidebar_facts = $this->module_service
11787cca37cSGreg Roach            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
1180b5fd0a6SGreg Roach            ->map(static function (ModuleSidebarInterface $sidebar): Collection {
1198eaf8709SGreg Roach                return $sidebar->supportedFacts();
1208eaf8709SGreg Roach            });
1218eaf8709SGreg Roach
1228eaf8709SGreg Roach        $tab_facts = $this->module_service
12387cca37cSGreg Roach            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
1240b5fd0a6SGreg Roach            ->map(static function (ModuleTabInterface $sidebar): Collection {
1258eaf8709SGreg Roach                return $sidebar->supportedFacts();
1268eaf8709SGreg Roach            });
1278eaf8709SGreg Roach
1288eaf8709SGreg Roach        $exclude_facts = $sidebar_facts->merge($tab_facts)->flatten();
1298eaf8709SGreg Roach
1303763c3f2SGreg Roach        // The individual’s own facts
131*91a257a4SGreg Roach        $individual_facts = $individual->facts()
1320b5fd0a6SGreg Roach            ->filter(static function (Fact $fact) use ($exclude_facts): bool {
1337fe676e5SGreg Roach                return !$exclude_facts->contains($fact->getTag());
13439ca88baSGreg Roach            });
1353763c3f2SGreg Roach
136*91a257a4SGreg Roach        $relative_facts = new Collection();
137*91a257a4SGreg Roach
1383763c3f2SGreg Roach        // Add spouse-family facts
13939ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
14030158ae7SGreg Roach            foreach ($family->facts() as $fact) {
1417fe676e5SGreg Roach                if (!$exclude_facts->contains($fact->getTag()) && $fact->getTag() !== 'CHAN') {
142*91a257a4SGreg Roach                    $relative_facts->push($fact);
1433763c3f2SGreg Roach                }
1443763c3f2SGreg Roach            }
145ee727175SGreg Roach
14639ca88baSGreg Roach            $spouse = $family->spouse($individual);
147ee727175SGreg Roach
148ee727175SGreg Roach            if ($spouse instanceof Individual) {
1498b9cfadbSGreg Roach                $spouse_facts   = $this->spouseFacts($individual, $spouse, $min_date, $max_date);
150*91a257a4SGreg Roach                $relative_facts = $relative_facts->merge($spouse_facts);
1513763c3f2SGreg Roach            }
1523763c3f2SGreg Roach
1538b9cfadbSGreg Roach            $child_facts    = $this->childFacts($individual, $family, '_CHIL', '', $min_date, $max_date);
154*91a257a4SGreg Roach            $relative_facts = $relative_facts->merge($child_facts);
1553763c3f2SGreg Roach        }
156225e381fSGreg Roach
1578b9cfadbSGreg Roach        $parent_facts    = $this->parentFacts($individual, 1, $min_date, $max_date);
158*91a257a4SGreg Roach        $relative_facts  = $relative_facts->merge($parent_facts);
1598b9cfadbSGreg Roach        $associate_facts = $this->associateFacts($individual);
160*91a257a4SGreg Roach        $historic_facts  = $this->historicFacts($individual);
161225e381fSGreg Roach
162*91a257a4SGreg Roach        $individual_facts = $individual_facts
16339ca88baSGreg Roach            ->merge($associate_facts)
164*91a257a4SGreg Roach            ->merge($historic_facts)
165*91a257a4SGreg Roach            ->merge($relative_facts);
1663763c3f2SGreg Roach
167*91a257a4SGreg Roach        $individual_facts = Fact::sortFacts($individual_facts);
1683763c3f2SGreg Roach
169a8cd57e1SGreg Roach        return view('modules/personal_facts/tab', [
170225e381fSGreg Roach            'can_edit'            => $individual->canEdit(),
17169cdf014SGreg Roach            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
172*91a257a4SGreg Roach            'has_associate_facts' => $associate_facts->isNotEmpty(),
173*91a257a4SGreg Roach            'has_historic_facts'  => $historic_facts->isNotEmpty(),
174*91a257a4SGreg Roach            'has_relative_facts'  => $relative_facts->isNotEmpty(),
175225e381fSGreg Roach            'individual'          => $individual,
176*91a257a4SGreg Roach            'facts'               => $individual_facts,
177225e381fSGreg Roach        ]);
1783763c3f2SGreg Roach    }
1793763c3f2SGreg Roach
180ee727175SGreg Roach    /**
181*91a257a4SGreg Roach     * Spouse facts that are shown on an individual’s page.
182*91a257a4SGreg Roach     *
183*91a257a4SGreg Roach     * @param Individual $individual Show events that occured during the lifetime of this individual
184*91a257a4SGreg Roach     * @param Individual $spouse     Show events of this individual
185*91a257a4SGreg Roach     * @param Date       $min_date
186*91a257a4SGreg Roach     * @param Date       $max_date
187*91a257a4SGreg Roach     *
188*91a257a4SGreg Roach     * @return Collection<Fact>
189*91a257a4SGreg Roach     */
190*91a257a4SGreg Roach    private function spouseFacts(Individual $individual, Individual $spouse, Date $min_date, Date $max_date): Collection
191*91a257a4SGreg Roach    {
192*91a257a4SGreg Roach        $SHOW_RELATIVES_EVENTS = $individual->tree()->getPreference('SHOW_RELATIVES_EVENTS');
193*91a257a4SGreg Roach
194*91a257a4SGreg Roach        $death_of_a_spouse = [
195*91a257a4SGreg Roach            'DEAT' => [
196*91a257a4SGreg Roach                'M' => I18N::translate('Death of a husband'),
197*91a257a4SGreg Roach                'F' => I18N::translate('Death of a wife'),
198*91a257a4SGreg Roach                'U' => I18N::translate('Death of a spouse'),
199*91a257a4SGreg Roach            ],
200*91a257a4SGreg Roach            'BURI' => [
201*91a257a4SGreg Roach                'M' => I18N::translate('Burial of a husband'),
202*91a257a4SGreg Roach                'F' => I18N::translate('Burial of a wife'),
203*91a257a4SGreg Roach                'U' => I18N::translate('Burial of a spouse'),
204*91a257a4SGreg Roach            ],
205*91a257a4SGreg Roach            'CREM' => [
206*91a257a4SGreg Roach                'M' => I18N::translate('Cremation of a husband'),
207*91a257a4SGreg Roach                'F' => I18N::translate('Cremation of a wife'),
208*91a257a4SGreg Roach                'U' => I18N::translate('Cremation of a spouse'),
209*91a257a4SGreg Roach            ],
210*91a257a4SGreg Roach        ];
211*91a257a4SGreg Roach
212*91a257a4SGreg Roach        $facts = new Collection();
213*91a257a4SGreg Roach
214*91a257a4SGreg Roach        if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT_SPOU')) {
215*91a257a4SGreg Roach            foreach ($spouse->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
216*91a257a4SGreg Roach                if ($this->includeFact($fact, $min_date, $max_date)) {
217*91a257a4SGreg Roach                    $facts[] = $this->convertEvent($fact, $death_of_a_spouse[$fact->getTag()][$fact->record()->sex()]);
218*91a257a4SGreg Roach                }
219*91a257a4SGreg Roach            }
220*91a257a4SGreg Roach        }
221*91a257a4SGreg Roach
222*91a257a4SGreg Roach        return $facts;
223*91a257a4SGreg Roach    }
224*91a257a4SGreg Roach
225*91a257a4SGreg Roach    /**
226ee727175SGreg Roach     * Does a relative event occur within a date range (i.e. the individual's lifetime)?
227ee727175SGreg Roach     *
228ee727175SGreg Roach     * @param Fact $fact
229ee727175SGreg Roach     * @param Date $min_date
230ee727175SGreg Roach     * @param Date $max_date
231ee727175SGreg Roach     *
232ee727175SGreg Roach     * @return bool
233ee727175SGreg Roach     */
2348b9cfadbSGreg Roach    private function includeFact(Fact $fact, Date $min_date, Date $max_date): bool
235b3b1d905SGreg Roach    {
2362decada7SGreg Roach        $fact_date = $fact->date();
237ee727175SGreg Roach
238ee727175SGreg Roach        return $fact_date->isOK() && Date::compare($min_date, $fact_date) <= 0 && Date::compare($fact_date, $max_date) <= 0;
239ee727175SGreg Roach    }
240ee727175SGreg Roach
2413caaa4d2SGreg Roach    /**
2427bf6ca81SGreg Roach     * Convert an event into a special "event of a close relative".
2437bf6ca81SGreg Roach     *
2447bf6ca81SGreg Roach     * @param Fact   $fact
2459e65d053SGreg Roach     * @param string $type
2467bf6ca81SGreg Roach     *
2477bf6ca81SGreg Roach     * @return Fact
2487bf6ca81SGreg Roach     */
2499e65d053SGreg Roach    private function convertEvent(Fact $fact, string $type): Fact
25099ed8541SGreg Roach    {
2517bf6ca81SGreg Roach        $gedcom = $fact->gedcom();
2527bf6ca81SGreg Roach        $gedcom = preg_replace('/\n2 TYPE .*/', '', $gedcom);
2537bf6ca81SGreg Roach        $gedcom = preg_replace('/^1 .*/', "1 EVEN CLOSE_RELATIVE\n2 TYPE " . $type, $gedcom);
2547bf6ca81SGreg Roach
2559cda3358SGreg Roach        $converted = new Fact($gedcom, $fact->record(), $fact->id());
2569cda3358SGreg Roach
2579cda3358SGreg Roach        if ($fact->isPendingAddition()) {
2589cda3358SGreg Roach            $converted->setPendingAddition();
2599cda3358SGreg Roach        }
2609cda3358SGreg Roach
2619cda3358SGreg Roach        if ($fact->isPendingDeletion()) {
2629cda3358SGreg Roach            $converted->setPendingDeletion();
2639cda3358SGreg Roach        }
2649cda3358SGreg Roach
2659cda3358SGreg Roach        return $converted;
2667bf6ca81SGreg Roach    }
2677bf6ca81SGreg Roach
2687bf6ca81SGreg Roach    /**
2693763c3f2SGreg Roach     * Get the events of children and grandchildren.
2703763c3f2SGreg Roach     *
2713763c3f2SGreg Roach     * @param Individual $person
2723763c3f2SGreg Roach     * @param Family     $family
2733763c3f2SGreg Roach     * @param string     $option
2743763c3f2SGreg Roach     * @param string     $relation
275ee727175SGreg Roach     * @param Date       $min_date
276ee727175SGreg Roach     * @param Date       $max_date
2773763c3f2SGreg Roach     *
278*91a257a4SGreg Roach     * @return Collection<Fact>
2793763c3f2SGreg Roach     */
280*91a257a4SGreg Roach    private function childFacts(Individual $person, Family $family, string $option, string $relation, Date $min_date, Date $max_date): Collection
281c1010edaSGreg Roach    {
282f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
2833763c3f2SGreg Roach
2847bf6ca81SGreg Roach        $birth_of_a_child = [
2857bf6ca81SGreg Roach            'BIRT' => [
2867bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a son'),
2877bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a daughter'),
2887bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a child'),
2897bf6ca81SGreg Roach            ],
2907bf6ca81SGreg Roach            'CHR'  => [
2917bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a son'),
2927bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a daughter'),
2937bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a child'),
2947bf6ca81SGreg Roach            ],
2957bf6ca81SGreg Roach            'BAPM' => [
2967bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a son'),
2977bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a daughter'),
2987bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a child'),
2997bf6ca81SGreg Roach            ],
3007bf6ca81SGreg Roach            'ADOP' => [
3017bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a son'),
3027bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a daughter'),
3037bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a child'),
3047bf6ca81SGreg Roach            ],
3057bf6ca81SGreg Roach        ];
3067bf6ca81SGreg Roach
3077bf6ca81SGreg Roach        $birth_of_a_sibling = [
3087bf6ca81SGreg Roach            'BIRT' => [
3097bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a brother'),
3107bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a sister'),
3117bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a sibling'),
3127bf6ca81SGreg Roach            ],
3137bf6ca81SGreg Roach            'CHR'  => [
3147bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a brother'),
3157bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a sister'),
3167bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a sibling'),
3177bf6ca81SGreg Roach            ],
3187bf6ca81SGreg Roach            'BAPM' => [
3197bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a brother'),
3207bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a sister'),
3217bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a sibling'),
3227bf6ca81SGreg Roach            ],
3237bf6ca81SGreg Roach            'ADOP' => [
3247bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a brother'),
3257bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a sister'),
3267bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a sibling'),
3277bf6ca81SGreg Roach            ],
3287bf6ca81SGreg Roach        ];
3297bf6ca81SGreg Roach
3307bf6ca81SGreg Roach        $birth_of_a_half_sibling = [
3317bf6ca81SGreg Roach            'BIRT' => [
3327bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a half-brother'),
3337bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a half-sister'),
3347bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a half-sibling'),
3357bf6ca81SGreg Roach            ],
3367bf6ca81SGreg Roach            'CHR'  => [
3377bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a half-brother'),
3387bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a half-sister'),
3397bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a half-sibling'),
3407bf6ca81SGreg Roach            ],
3417bf6ca81SGreg Roach            'BAPM' => [
3427bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a half-brother'),
3437bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a half-sister'),
3447bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a half-sibling'),
3457bf6ca81SGreg Roach            ],
3467bf6ca81SGreg Roach            'ADOP' => [
3477bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a half-brother'),
3487bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a half-sister'),
3497bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a half-sibling'),
3507bf6ca81SGreg Roach            ],
3517bf6ca81SGreg Roach        ];
3527bf6ca81SGreg Roach
3537bf6ca81SGreg Roach        $birth_of_a_grandchild = [
3547bf6ca81SGreg Roach            'BIRT' => [
3557bf6ca81SGreg Roach                'M' => I18N::translate('Birth of a grandson'),
3567bf6ca81SGreg Roach                'F' => I18N::translate('Birth of a granddaughter'),
3577bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a grandchild'),
3587bf6ca81SGreg Roach            ],
3597bf6ca81SGreg Roach            'CHR'  => [
3607bf6ca81SGreg Roach                'M' => I18N::translate('Christening of a grandson'),
3617bf6ca81SGreg Roach                'F' => I18N::translate('Christening of a granddaughter'),
3627bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a grandchild'),
3637bf6ca81SGreg Roach            ],
3647bf6ca81SGreg Roach            'BAPM' => [
3657bf6ca81SGreg Roach                'M' => I18N::translate('Baptism of a grandson'),
3667bf6ca81SGreg Roach                'F' => I18N::translate('Baptism of a granddaughter'),
3677bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
3687bf6ca81SGreg Roach            ],
3697bf6ca81SGreg Roach            'ADOP' => [
3707bf6ca81SGreg Roach                'M' => I18N::translate('Adoption of a grandson'),
3717bf6ca81SGreg Roach                'F' => I18N::translate('Adoption of a granddaughter'),
3727bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a grandchild'),
3737bf6ca81SGreg Roach            ],
3747bf6ca81SGreg Roach        ];
3757bf6ca81SGreg Roach
3767bf6ca81SGreg Roach        $birth_of_a_grandchild1 = [
3777bf6ca81SGreg Roach            'BIRT' => [
3787bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Birth of a grandson'),
3797bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Birth of a granddaughter'),
3807bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a grandchild'),
3817bf6ca81SGreg Roach            ],
3827bf6ca81SGreg Roach            'CHR'  => [
3837bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Christening of a grandson'),
3847bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Christening of a granddaughter'),
3857bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a grandchild'),
3867bf6ca81SGreg Roach            ],
3877bf6ca81SGreg Roach            'BAPM' => [
3887bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Baptism of a grandson'),
3897bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Baptism of a granddaughter'),
3907bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
3917bf6ca81SGreg Roach            ],
3927bf6ca81SGreg Roach            'ADOP' => [
3937bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Adoption of a grandson'),
3947bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Adoption of a granddaughter'),
3957bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a grandchild'),
3967bf6ca81SGreg Roach            ],
3977bf6ca81SGreg Roach        ];
3987bf6ca81SGreg Roach
3997bf6ca81SGreg Roach        $birth_of_a_grandchild2 = [
4007bf6ca81SGreg Roach            'BIRT' => [
4013d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Birth of a grandson'),
4023d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Birth of a granddaughter'),
4037bf6ca81SGreg Roach                'U' => I18N::translate('Birth of a grandchild'),
4047bf6ca81SGreg Roach            ],
4057bf6ca81SGreg Roach            'CHR'  => [
4063d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Christening of a grandson'),
4073d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Christening of a granddaughter'),
4087bf6ca81SGreg Roach                'U' => I18N::translate('Christening of a grandchild'),
4097bf6ca81SGreg Roach            ],
4107bf6ca81SGreg Roach            'BAPM' => [
4113d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Baptism of a grandson'),
4123d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Baptism of a granddaughter'),
4137bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
4147bf6ca81SGreg Roach            ],
4157bf6ca81SGreg Roach            'ADOP' => [
4163d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Adoption of a grandson'),
4173d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Adoption of a granddaughter'),
4187bf6ca81SGreg Roach                'U' => I18N::translate('Adoption of a grandchild'),
4197bf6ca81SGreg Roach            ],
4207bf6ca81SGreg Roach        ];
4217bf6ca81SGreg Roach
4227bf6ca81SGreg Roach        $death_of_a_child = [
4237bf6ca81SGreg Roach            'DEAT' => [
4247bf6ca81SGreg Roach                'M' => I18N::translate('Death of a son'),
4257bf6ca81SGreg Roach                'F' => I18N::translate('Death of a daughter'),
4267bf6ca81SGreg Roach                'U' => I18N::translate('Death of a child'),
4277bf6ca81SGreg Roach            ],
4287bf6ca81SGreg Roach            'BURI' => [
4297bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a son'),
4307bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a daughter'),
4317bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a child'),
4327bf6ca81SGreg Roach            ],
4337bf6ca81SGreg Roach            'CREM' => [
4347bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a son'),
4357bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a daughter'),
4367bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a child'),
4377bf6ca81SGreg Roach            ],
4387bf6ca81SGreg Roach        ];
4397bf6ca81SGreg Roach
4407bf6ca81SGreg Roach        $death_of_a_sibling = [
4417bf6ca81SGreg Roach            'DEAT' => [
4427bf6ca81SGreg Roach                'M' => I18N::translate('Death of a brother'),
4437bf6ca81SGreg Roach                'F' => I18N::translate('Death of a sister'),
4447bf6ca81SGreg Roach                'U' => I18N::translate('Death of a sibling'),
4457bf6ca81SGreg Roach            ],
4467bf6ca81SGreg Roach            'BURI' => [
4477bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a brother'),
4487bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a sister'),
4497bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a sibling'),
4507bf6ca81SGreg Roach            ],
4517bf6ca81SGreg Roach            'CREM' => [
4527bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a brother'),
4537bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a sister'),
4547bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a sibling'),
4557bf6ca81SGreg Roach            ],
4567bf6ca81SGreg Roach        ];
4577bf6ca81SGreg Roach
4587bf6ca81SGreg Roach        $death_of_a_half_sibling = [
4597bf6ca81SGreg Roach            'DEAT' => [
4607bf6ca81SGreg Roach                'M' => I18N::translate('Death of a half-brother'),
4617bf6ca81SGreg Roach                'F' => I18N::translate('Death of a half-sister'),
4627bf6ca81SGreg Roach                'U' => I18N::translate('Death of a half-sibling'),
4637bf6ca81SGreg Roach            ],
4647bf6ca81SGreg Roach            'BURI' => [
4657bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a half-brother'),
4667bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a half-sister'),
4677bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a half-sibling'),
4687bf6ca81SGreg Roach            ],
4697bf6ca81SGreg Roach            'CREM' => [
4707bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a half-brother'),
4717bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a half-sister'),
4727bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a half-sibling'),
4737bf6ca81SGreg Roach            ],
4747bf6ca81SGreg Roach        ];
4757bf6ca81SGreg Roach
4767bf6ca81SGreg Roach        $death_of_a_grandchild = [
4777bf6ca81SGreg Roach            'DEAT' => [
4787bf6ca81SGreg Roach                'M' => I18N::translate('Death of a grandson'),
4797bf6ca81SGreg Roach                'F' => I18N::translate('Death of a granddaughter'),
4807bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandchild'),
4817bf6ca81SGreg Roach            ],
4827bf6ca81SGreg Roach            'BURI' => [
4837bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a grandson'),
4847bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a granddaughter'),
4857bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandchild'),
4867bf6ca81SGreg Roach            ],
4877bf6ca81SGreg Roach            'CREM' => [
4887bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a grandson'),
4897bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a granddaughter'),
4907bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
4917bf6ca81SGreg Roach            ],
4927bf6ca81SGreg Roach        ];
4937bf6ca81SGreg Roach
4947bf6ca81SGreg Roach        $death_of_a_grandchild1 = [
4957bf6ca81SGreg Roach            'DEAT' => [
4967bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Death of a grandson'),
4977bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Death of a granddaughter'),
4987bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandchild'),
4997bf6ca81SGreg Roach            ],
5007bf6ca81SGreg Roach            'BURI' => [
5017bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Burial of a grandson'),
5027bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Burial of a granddaughter'),
5037bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandchild'),
5047bf6ca81SGreg Roach            ],
5057bf6ca81SGreg Roach            'CREM' => [
5067bf6ca81SGreg Roach                'M' => I18N::translateContext('daughter’s son', 'Cremation of a grandson'),
5077bf6ca81SGreg Roach                'F' => I18N::translateContext('daughter’s daughter', 'Cremation of a granddaughter'),
5087bf6ca81SGreg Roach                'U' => I18N::translate('Baptism of a grandchild'),
5097bf6ca81SGreg Roach            ],
5107bf6ca81SGreg Roach        ];
5117bf6ca81SGreg Roach
5127bf6ca81SGreg Roach        $death_of_a_grandchild2 = [
5137bf6ca81SGreg Roach            'DEAT' => [
5143d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Death of a grandson'),
5153d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Death of a granddaughter'),
5167bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandchild'),
5177bf6ca81SGreg Roach            ],
5187bf6ca81SGreg Roach            'BURI' => [
5193d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Burial of a grandson'),
5203d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Burial of a granddaughter'),
5217bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandchild'),
5227bf6ca81SGreg Roach            ],
5237bf6ca81SGreg Roach            'CREM' => [
5243d394ce7SGreg Roach                'M' => I18N::translateContext('son’s son', 'Cremation of a grandson'),
5253d394ce7SGreg Roach                'F' => I18N::translateContext('son’s daughter', 'Cremation of a granddaughter'),
5267bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a grandchild'),
5277bf6ca81SGreg Roach            ],
5287bf6ca81SGreg Roach        ];
5297bf6ca81SGreg Roach
5307bf6ca81SGreg Roach        $marriage_of_a_child = [
5317bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a son'),
5327bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a daughter'),
5337bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a child'),
5347bf6ca81SGreg Roach        ];
5357bf6ca81SGreg Roach
5367bf6ca81SGreg Roach        $marriage_of_a_grandchild = [
5377bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a grandson'),
5387bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a granddaughter'),
5397bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a grandchild'),
5407bf6ca81SGreg Roach        ];
5417bf6ca81SGreg Roach
5427bf6ca81SGreg Roach        $marriage_of_a_grandchild1 = [
5437bf6ca81SGreg Roach            'M' => I18N::translateContext('daughter’s son', 'Marriage of a grandson'),
5447bf6ca81SGreg Roach            'F' => I18N::translateContext('daughter’s daughter', 'Marriage of a granddaughter'),
5457bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a grandchild'),
5467bf6ca81SGreg Roach        ];
5477bf6ca81SGreg Roach
5487bf6ca81SGreg Roach        $marriage_of_a_grandchild2 = [
5493d394ce7SGreg Roach            'M' => I18N::translateContext('son’s son', 'Marriage of a grandson'),
5503d394ce7SGreg Roach            'F' => I18N::translateContext('son’s daughter', 'Marriage of a granddaughter'),
5517bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a grandchild'),
5527bf6ca81SGreg Roach        ];
5537bf6ca81SGreg Roach
5547bf6ca81SGreg Roach        $marriage_of_a_sibling = [
5557bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a brother'),
5567bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a sister'),
5577bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a sibling'),
5587bf6ca81SGreg Roach        ];
5597bf6ca81SGreg Roach
5607bf6ca81SGreg Roach        $marriage_of_a_half_sibling = [
5617bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a half-brother'),
5627bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a half-sister'),
5637bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a half-sibling'),
5647bf6ca81SGreg Roach        ];
5657bf6ca81SGreg Roach
566*91a257a4SGreg Roach        $facts = new Collection();
5673763c3f2SGreg Roach
5683763c3f2SGreg Roach        // Deal with recursion.
5693763c3f2SGreg Roach        switch ($option) {
5703763c3f2SGreg Roach            case '_CHIL':
5713763c3f2SGreg Roach                // Add grandchildren
57239ca88baSGreg Roach                foreach ($family->children() as $child) {
57339ca88baSGreg Roach                    foreach ($child->spouseFamilies() as $cfamily) {
57439ca88baSGreg Roach                        switch ($child->sex()) {
5753763c3f2SGreg Roach                            case 'M':
5768b9cfadbSGreg Roach                                foreach ($this->childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
5773763c3f2SGreg Roach                                    $facts[] = $fact;
5783763c3f2SGreg Roach                                }
5793763c3f2SGreg Roach                                break;
5803763c3f2SGreg Roach                            case 'F':
5818b9cfadbSGreg Roach                                foreach ($this->childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
5823763c3f2SGreg Roach                                    $facts[] = $fact;
5833763c3f2SGreg Roach                                }
5843763c3f2SGreg Roach                                break;
5853763c3f2SGreg Roach                            default:
5868b9cfadbSGreg Roach                                foreach ($this->childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
5873763c3f2SGreg Roach                                    $facts[] = $fact;
5883763c3f2SGreg Roach                                }
5893763c3f2SGreg Roach                                break;
5903763c3f2SGreg Roach                        }
5913763c3f2SGreg Roach                    }
5923763c3f2SGreg Roach                }
5933763c3f2SGreg Roach                break;
5943763c3f2SGreg Roach        }
5953763c3f2SGreg Roach
5963763c3f2SGreg Roach        // For each child in the family
59739ca88baSGreg Roach        foreach ($family->children() as $child) {
59822d65e5aSGreg Roach            if ($child->xref() === $person->xref()) {
5993763c3f2SGreg Roach                // We are not our own sibling!
6003763c3f2SGreg Roach                continue;
6013763c3f2SGreg Roach            }
6023763c3f2SGreg Roach            // add child’s birth
603dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option))) {
6047bf6ca81SGreg Roach                foreach ($child->facts(['BIRT', 'CHR', 'BAPM', 'ADOP']) as $fact) {
6053763c3f2SGreg Roach                    // Always show _BIRT_CHIL, even if the dates are not known
60622d65e5aSGreg Roach                    if ($option === '_CHIL' || $this->includeFact($fact, $min_date, $max_date)) {
6077bf6ca81SGreg Roach                        switch ($option) {
6087bf6ca81SGreg Roach                            case '_GCHI':
6097bf6ca81SGreg Roach                                switch ($relation) {
6107bf6ca81SGreg Roach                                    case 'dau':
6117fe676e5SGreg Roach                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild1[$fact->getTag()][$fact->record()->sex()]);
6127bf6ca81SGreg Roach                                        break;
6137bf6ca81SGreg Roach                                    case 'son':
6147fe676e5SGreg Roach                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild2[$fact->getTag()][$fact->record()->sex()]);
6157bf6ca81SGreg Roach                                        break;
6167bf6ca81SGreg Roach                                    case 'chil':
6177fe676e5SGreg Roach                                        $facts[] = $this->convertEvent($fact, $birth_of_a_grandchild[$fact->getTag()][$fact->record()->sex()]);
6187bf6ca81SGreg Roach                                        break;
6197bf6ca81SGreg Roach                                }
6207bf6ca81SGreg Roach                                break;
6217bf6ca81SGreg Roach                            case '_SIBL':
6227fe676e5SGreg Roach                                $facts[] = $this->convertEvent($fact, $birth_of_a_sibling[$fact->getTag()][$fact->record()->sex()]);
6237bf6ca81SGreg Roach                                break;
6247bf6ca81SGreg Roach                            case '_HSIB':
6257fe676e5SGreg Roach                                $facts[] = $this->convertEvent($fact, $birth_of_a_half_sibling[$fact->getTag()][$fact->record()->sex()]);
6267bf6ca81SGreg Roach                                break;
6277bf6ca81SGreg Roach                            case '_CHIL':
6287fe676e5SGreg Roach                                $facts[] = $this->convertEvent($fact, $birth_of_a_child[$fact->getTag()][$fact->record()->sex()]);
6297bf6ca81SGreg Roach                                break;
6303763c3f2SGreg Roach                        }
6313763c3f2SGreg Roach                    }
6323763c3f2SGreg Roach                }
6333763c3f2SGreg Roach            }
6343763c3f2SGreg Roach            // add child’s death
635dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option))) {
6367bf6ca81SGreg Roach                foreach ($child->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
6378b9cfadbSGreg Roach                    if ($this->includeFact($fact, $min_date, $max_date)) {
6387bf6ca81SGreg Roach                        switch ($option) {
6397bf6ca81SGreg Roach                            case '_GCHI':
6407bf6ca81SGreg Roach                                switch ($relation) {
6417bf6ca81SGreg Roach                                    case 'dau':
6427fe676e5SGreg Roach                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild1[$fact->getTag()][$fact->record()->sex()]);
6437bf6ca81SGreg Roach                                        break;
6447bf6ca81SGreg Roach                                    case 'son':
6457fe676e5SGreg Roach                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild2[$fact->getTag()][$fact->record()->sex()]);
6467bf6ca81SGreg Roach                                        break;
6477bf6ca81SGreg Roach                                    case 'chi':
6487fe676e5SGreg Roach                                        $facts[] = $this->convertEvent($fact, $death_of_a_grandchild[$fact->getTag()][$fact->record()->sex()]);
6497bf6ca81SGreg Roach                                        break;
6507bf6ca81SGreg Roach                                }
6517bf6ca81SGreg Roach                                break;
6527bf6ca81SGreg Roach                            case '_SIBL':
6537fe676e5SGreg Roach                                $facts[] = $this->convertEvent($fact, $death_of_a_sibling[$fact->getTag()][$fact->record()->sex()]);
6547bf6ca81SGreg Roach                                break;
6557bf6ca81SGreg Roach                            case '_HSIB':
6567fe676e5SGreg Roach                                $facts[] = $this->convertEvent($fact, $death_of_a_half_sibling[$fact->getTag()][$fact->record()->sex()]);
6577bf6ca81SGreg Roach                                break;
658718b3bc2SGreg Roach                            case '_CHIL':
6597fe676e5SGreg Roach                                $facts[] = $this->convertEvent($fact, $death_of_a_child[$fact->getTag()][$fact->record()->sex()]);
6607bf6ca81SGreg Roach                                break;
6613763c3f2SGreg Roach                        }
6623763c3f2SGreg Roach                    }
6633763c3f2SGreg Roach                }
6643763c3f2SGreg Roach            }
6657bf6ca81SGreg Roach
6663763c3f2SGreg Roach            // add child’s marriage
667dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
66839ca88baSGreg Roach                foreach ($child->spouseFamilies() as $sfamily) {
6698d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
6708b9cfadbSGreg Roach                        if ($this->includeFact($fact, $min_date, $max_date)) {
6717bf6ca81SGreg Roach                            switch ($option) {
6727bf6ca81SGreg Roach                                case '_GCHI':
6737bf6ca81SGreg Roach                                    switch ($relation) {
6747bf6ca81SGreg Roach                                        case 'dau':
6757bf6ca81SGreg Roach                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild1['F']);
6767bf6ca81SGreg Roach                                            break;
6777bf6ca81SGreg Roach                                        case 'son':
6787bf6ca81SGreg Roach                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild2['M']);
6797bf6ca81SGreg Roach                                            break;
6807bf6ca81SGreg Roach                                        case 'chi':
6817bf6ca81SGreg Roach                                            $facts[] = $this->convertEvent($fact, $marriage_of_a_grandchild['U']);
6827bf6ca81SGreg Roach                                            break;
6837bf6ca81SGreg Roach                                    }
6847bf6ca81SGreg Roach                                    break;
6857bf6ca81SGreg Roach                                case '_SIBL':
6867bf6ca81SGreg Roach                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_sibling['U']);
6877bf6ca81SGreg Roach                                    break;
6887bf6ca81SGreg Roach                                case '_HSIB':
6897bf6ca81SGreg Roach                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_half_sibling['U']);
6907bf6ca81SGreg Roach                                    break;
6917bf6ca81SGreg Roach                                case '_CHIL':
6927bf6ca81SGreg Roach                                    $facts[] = $this->convertEvent($fact, $marriage_of_a_child['U']);
6937bf6ca81SGreg Roach                                    break;
6943763c3f2SGreg Roach                            }
6953763c3f2SGreg Roach                        }
6963763c3f2SGreg Roach                    }
6973763c3f2SGreg Roach                }
6983763c3f2SGreg Roach            }
6993763c3f2SGreg Roach        }
7003763c3f2SGreg Roach
7013763c3f2SGreg Roach        return $facts;
7023763c3f2SGreg Roach    }
7033763c3f2SGreg Roach
7043763c3f2SGreg Roach    /**
7053763c3f2SGreg Roach     * Get the events of parents and grandparents.
7063763c3f2SGreg Roach     *
7073763c3f2SGreg Roach     * @param Individual $person
708cbc1590aSGreg Roach     * @param int        $sosa
709ee727175SGreg Roach     * @param Date       $min_date
710ee727175SGreg Roach     * @param Date       $max_date
7113763c3f2SGreg Roach     *
712*91a257a4SGreg Roach     * @return Collection<Fact>
7133763c3f2SGreg Roach     */
714*91a257a4SGreg Roach    private function parentFacts(Individual $person, int $sosa, Date $min_date, Date $max_date): Collection
715c1010edaSGreg Roach    {
716f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
7173763c3f2SGreg Roach
7187bf6ca81SGreg Roach        $death_of_a_parent = [
7197bf6ca81SGreg Roach            'DEAT' => [
7207bf6ca81SGreg Roach                'M' => I18N::translate('Death of a father'),
7217bf6ca81SGreg Roach                'F' => I18N::translate('Death of a mother'),
7227bf6ca81SGreg Roach                'U' => I18N::translate('Death of a parent'),
7237bf6ca81SGreg Roach            ],
7247bf6ca81SGreg Roach            'BURI' => [
7257bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a father'),
7267bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a mother'),
7277bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a parent'),
7287bf6ca81SGreg Roach            ],
7297bf6ca81SGreg Roach            'CREM' => [
7307bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a father'),
7317bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a mother'),
7327bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a parent'),
7337bf6ca81SGreg Roach            ],
7347bf6ca81SGreg Roach        ];
7357bf6ca81SGreg Roach
7367bf6ca81SGreg Roach        $death_of_a_grandparent = [
7377bf6ca81SGreg Roach            'DEAT' => [
7387bf6ca81SGreg Roach                'M' => I18N::translate('Death of a grandfather'),
7397bf6ca81SGreg Roach                'F' => I18N::translate('Death of a grandmother'),
7407bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandparent'),
7417bf6ca81SGreg Roach            ],
7427bf6ca81SGreg Roach            'BURI' => [
7437bf6ca81SGreg Roach                'M' => I18N::translate('Burial of a grandfather'),
7447bf6ca81SGreg Roach                'F' => I18N::translate('Burial of a grandmother'),
7457bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandparent'),
7467bf6ca81SGreg Roach            ],
7477bf6ca81SGreg Roach            'CREM' => [
7487bf6ca81SGreg Roach                'M' => I18N::translate('Cremation of a grandfather'),
7497bf6ca81SGreg Roach                'F' => I18N::translate('Cremation of a grandmother'),
7500b73ecfcSGreg Roach                'U' => I18N::translate('Cremation of a grandparent'),
7517bf6ca81SGreg Roach            ],
7527bf6ca81SGreg Roach        ];
7537bf6ca81SGreg Roach
7547bf6ca81SGreg Roach        $death_of_a_maternal_grandparent = [
7557bf6ca81SGreg Roach            'DEAT' => [
7560b73ecfcSGreg Roach                'M' => I18N::translate('Death of a maternal grandfather'),
7570b73ecfcSGreg Roach                'F' => I18N::translate('Death of a maternal grandmother'),
7587bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandparent'),
7597bf6ca81SGreg Roach            ],
7607bf6ca81SGreg Roach            'BURI' => [
7610b73ecfcSGreg Roach                'M' => I18N::translate('Burial of a maternal grandfather'),
7620b73ecfcSGreg Roach                'F' => I18N::translate('Burial of a maternal grandmother'),
7637bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandparent'),
7647bf6ca81SGreg Roach            ],
7657bf6ca81SGreg Roach            'CREM' => [
7660b73ecfcSGreg Roach                'M' => I18N::translate('Cremation of a maternal grandfather'),
7670b73ecfcSGreg Roach                'F' => I18N::translate('Cremation of a maternal grandmother'),
7680b73ecfcSGreg Roach                'U' => I18N::translate('Cremation of a grandparent'),
7697bf6ca81SGreg Roach            ],
7707bf6ca81SGreg Roach        ];
7717bf6ca81SGreg Roach
7727bf6ca81SGreg Roach        $death_of_a_paternal_grandparent = [
7737bf6ca81SGreg Roach            'DEAT' => [
7740b73ecfcSGreg Roach                'M' => I18N::translate('Death of a paternal grandfather'),
7750b73ecfcSGreg Roach                'F' => I18N::translate('Death of a paternal grandmother'),
7767bf6ca81SGreg Roach                'U' => I18N::translate('Death of a grandparent'),
7777bf6ca81SGreg Roach            ],
7787bf6ca81SGreg Roach            'BURI' => [
7790b73ecfcSGreg Roach                'M' => I18N::translate('Burial of a paternal grandfather'),
7800b73ecfcSGreg Roach                'F' => I18N::translate('Burial of a paternal grandmother'),
7817bf6ca81SGreg Roach                'U' => I18N::translate('Burial of a grandparent'),
7827bf6ca81SGreg Roach            ],
7837bf6ca81SGreg Roach            'CREM' => [
7840b73ecfcSGreg Roach                'M' => I18N::translate('Cremation of a paternal grandfather'),
7850b73ecfcSGreg Roach                'F' => I18N::translate('Cremation of a paternal grandmother'),
7867bf6ca81SGreg Roach                'U' => I18N::translate('Cremation of a grandparent'),
7877bf6ca81SGreg Roach            ],
7887bf6ca81SGreg Roach        ];
7897bf6ca81SGreg Roach
7907bf6ca81SGreg Roach        $marriage_of_a_parent = [
7917bf6ca81SGreg Roach            'M' => I18N::translate('Marriage of a father'),
7927bf6ca81SGreg Roach            'F' => I18N::translate('Marriage of a mother'),
7937bf6ca81SGreg Roach            'U' => I18N::translate('Marriage of a parent'),
7947bf6ca81SGreg Roach        ];
7957bf6ca81SGreg Roach
796*91a257a4SGreg Roach        $facts = new Collection();
7973763c3f2SGreg Roach
7980b73ecfcSGreg Roach        if ($sosa === 1) {
79939ca88baSGreg Roach            foreach ($person->childFamilies() as $family) {
8003763c3f2SGreg Roach                // Add siblings
8018b9cfadbSGreg Roach                foreach ($this->childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
8023763c3f2SGreg Roach                    $facts[] = $fact;
8033763c3f2SGreg Roach                }
80439ca88baSGreg Roach                foreach ($family->spouses() as $spouse) {
80539ca88baSGreg Roach                    foreach ($spouse->spouseFamilies() as $sfamily) {
8063763c3f2SGreg Roach                        if ($family !== $sfamily) {
8073763c3f2SGreg Roach                            // Add half-siblings
8088b9cfadbSGreg Roach                            foreach ($this->childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
8093763c3f2SGreg Roach                                $facts[] = $fact;
8103763c3f2SGreg Roach                            }
8113763c3f2SGreg Roach                        }
8123763c3f2SGreg Roach                    }
8133763c3f2SGreg Roach                    // Add grandparents
81422d65e5aSGreg Roach                    foreach ($this->parentFacts($spouse, $spouse->sex() === 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
8153763c3f2SGreg Roach                        $facts[] = $fact;
8163763c3f2SGreg Roach                    }
8173763c3f2SGreg Roach                }
8183763c3f2SGreg Roach            }
8193763c3f2SGreg Roach
820dec352c1SGreg Roach            if (str_contains($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
8213763c3f2SGreg Roach                // add father/mother marriages
82239ca88baSGreg Roach                foreach ($person->childFamilies() as $sfamily) {
8238d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
8248b9cfadbSGreg Roach                        if ($this->includeFact($fact, $min_date, $max_date)) {
8253763c3f2SGreg Roach                            // marriage of parents (to each other)
8267bf6ca81SGreg Roach                            $facts[] = $this->convertEvent($fact, I18N::translate('Marriage of parents'));
8273763c3f2SGreg Roach                        }
8283763c3f2SGreg Roach                    }
8293763c3f2SGreg Roach                }
83039ca88baSGreg Roach                foreach ($person->childStepFamilies() as $sfamily) {
8318d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
8328b9cfadbSGreg Roach                        if ($this->includeFact($fact, $min_date, $max_date)) {
8333763c3f2SGreg Roach                            // marriage of a parent (to another spouse)
8347bf6ca81SGreg Roach                            $facts[] = $this->convertEvent($fact, $marriage_of_a_parent['U']);
8353763c3f2SGreg Roach                        }
8363763c3f2SGreg Roach                    }
8373763c3f2SGreg Roach                }
8383763c3f2SGreg Roach            }
8393763c3f2SGreg Roach        }
8403763c3f2SGreg Roach
84139ca88baSGreg Roach        foreach ($person->childFamilies() as $family) {
84239ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
843dec352c1SGreg Roach                if (str_contains($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa === 1 ? '_PARE' : '_GPAR'))) {
8447bf6ca81SGreg Roach                    foreach ($parent->facts(['DEAT', 'BURI', 'CREM']) as $fact) {
845e4cf93e3SGreg Roach                        // Show death of parent when it happened prior to birth
846e4cf93e3SGreg Roach                        if ($sosa === 1 && Date::compare($fact->date(), $min_date) < 0 || $this->includeFact($fact, $min_date, $max_date)) {
8473763c3f2SGreg Roach                            switch ($sosa) {
8483763c3f2SGreg Roach                                case 1:
8497fe676e5SGreg Roach                                    $facts[] = $this->convertEvent($fact, $death_of_a_parent[$fact->getTag()][$fact->record()->sex()]);
8503763c3f2SGreg Roach                                    break;
8513763c3f2SGreg Roach                                case 2:
8523763c3f2SGreg Roach                                case 3:
85369c89463SGreg Roach                                    switch ($person->sex()) {
8547bf6ca81SGreg Roach                                        case 'M':
8557fe676e5SGreg Roach                                            $facts[] = $this->convertEvent($fact, $death_of_a_paternal_grandparent[$fact->getTag()][$fact->record()->sex()]);
8563763c3f2SGreg Roach                                            break;
8577bf6ca81SGreg Roach                                        case 'F':
8587fe676e5SGreg Roach                                            $facts[] = $this->convertEvent($fact, $death_of_a_maternal_grandparent[$fact->getTag()][$fact->record()->sex()]);
8597bf6ca81SGreg Roach                                            break;
8607bf6ca81SGreg Roach                                        default:
8617fe676e5SGreg Roach                                            $facts[] = $this->convertEvent($fact, $death_of_a_grandparent[$fact->getTag()][$fact->record()->sex()]);
8627bf6ca81SGreg Roach                                            break;
8637bf6ca81SGreg Roach                                    }
8643763c3f2SGreg Roach                            }
8653763c3f2SGreg Roach                        }
8663763c3f2SGreg Roach                    }
8673763c3f2SGreg Roach                }
8683763c3f2SGreg Roach            }
8693763c3f2SGreg Roach        }
8703763c3f2SGreg Roach
8713763c3f2SGreg Roach        return $facts;
8723763c3f2SGreg Roach    }
8733763c3f2SGreg Roach
8743763c3f2SGreg Roach    /**
8753763c3f2SGreg Roach     * Get the events of associates.
8763763c3f2SGreg Roach     *
8773763c3f2SGreg Roach     * @param Individual $person
8783763c3f2SGreg Roach     *
879*91a257a4SGreg Roach     * @return Collection<Fact>
8803763c3f2SGreg Roach     */
881*91a257a4SGreg Roach    private function associateFacts(Individual $person): Collection
882c1010edaSGreg Roach    {
88313abd6f3SGreg Roach        $facts = [];
8843763c3f2SGreg Roach
885ee727175SGreg Roach        /** @var Individual[] $associates */
886907c1109SGreg Roach        $asso1 = $person->linkedIndividuals('ASSO');
887907c1109SGreg Roach        $asso2 = $person->linkedIndividuals('_ASSO');
888907c1109SGreg Roach        $asso3 = $person->linkedFamilies('ASSO');
889907c1109SGreg Roach        $asso4 = $person->linkedFamilies('_ASSO');
890907c1109SGreg Roach
891907c1109SGreg Roach        $associates = $asso1->merge($asso2)->merge($asso3)->merge($asso4);
892907c1109SGreg Roach
8933763c3f2SGreg Roach        foreach ($associates as $associate) {
89430158ae7SGreg Roach            foreach ($associate->facts() as $fact) {
895907c1109SGreg Roach                if (preg_match('/\n\d _?ASSO @' . $person->xref() . '@/', $fact->gedcom())) {
8963763c3f2SGreg Roach                    // Extract the important details from the fact
8977fe676e5SGreg Roach                    $factrec = '1 ' . $fact->getTag();
898138ca96cSGreg Roach                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
8993763c3f2SGreg Roach                        $factrec .= $match[0];
9003763c3f2SGreg Roach                    }
901138ca96cSGreg Roach                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
9023763c3f2SGreg Roach                        $factrec .= $match[0];
9033763c3f2SGreg Roach                    }
9043763c3f2SGreg Roach                    if ($associate instanceof Family) {
90539ca88baSGreg Roach                        foreach ($associate->spouses() as $spouse) {
906c0935879SGreg Roach                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
9073763c3f2SGreg Roach                        }
9083763c3f2SGreg Roach                    } else {
909c0935879SGreg Roach                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
9103763c3f2SGreg Roach                    }
9113763c3f2SGreg Roach                    $facts[] = new Fact($factrec, $associate, 'asso');
9123763c3f2SGreg Roach                }
9133763c3f2SGreg Roach            }
9143763c3f2SGreg Roach        }
9153763c3f2SGreg Roach
916*91a257a4SGreg Roach        return new Collection($facts);
917*91a257a4SGreg Roach    }
918*91a257a4SGreg Roach
919*91a257a4SGreg Roach    /**
920*91a257a4SGreg Roach     * Get any historical events.
921*91a257a4SGreg Roach     *
922*91a257a4SGreg Roach     * @param Individual $individual
923*91a257a4SGreg Roach     *
924*91a257a4SGreg Roach     * @return Collection<Fact>
925*91a257a4SGreg Roach     */
926*91a257a4SGreg Roach    private function historicFacts(Individual $individual): Collection
927*91a257a4SGreg Roach    {
928*91a257a4SGreg Roach        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
929*91a257a4SGreg Roach            ->map(static function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
930*91a257a4SGreg Roach                return $module->historicEventsForIndividual($individual);
931*91a257a4SGreg Roach            })
932*91a257a4SGreg Roach            ->flatten();
933*91a257a4SGreg Roach    }
934*91a257a4SGreg Roach
935*91a257a4SGreg Roach    /**
936*91a257a4SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
937*91a257a4SGreg Roach     *
938*91a257a4SGreg Roach     * @param Individual $individual
939*91a257a4SGreg Roach     *
940*91a257a4SGreg Roach     * @return bool
941*91a257a4SGreg Roach     */
942*91a257a4SGreg Roach    public function hasTabContent(Individual $individual): bool
943*91a257a4SGreg Roach    {
944*91a257a4SGreg Roach        return true;
945*91a257a4SGreg Roach    }
946*91a257a4SGreg Roach
947*91a257a4SGreg Roach    /**
948*91a257a4SGreg Roach     * Can this tab load asynchronously?
949*91a257a4SGreg Roach     *
950*91a257a4SGreg Roach     * @return bool
951*91a257a4SGreg Roach     */
952*91a257a4SGreg Roach    public function canLoadAjax(): bool
953*91a257a4SGreg Roach    {
954*91a257a4SGreg Roach        return false;
9553763c3f2SGreg Roach    }
9568eaf8709SGreg Roach
9578eaf8709SGreg Roach    /**
9588eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
9598eaf8709SGreg Roach     *
960b5c8fd7eSGreg Roach     * @return Collection<string>
9618eaf8709SGreg Roach     */
9628eaf8709SGreg Roach    public function supportedFacts(): Collection
9638eaf8709SGreg Roach    {
9648eaf8709SGreg Roach        // We don't actually displaye these facts, but they are displayed
9658eaf8709SGreg Roach        // outside the tabs/sidebar systems. This just forces them to be excluded here.
9660666a894SGreg Roach        return new Collection(['NAME', 'SEX', 'OBJE']);
9678eaf8709SGreg Roach    }
9683763c3f2SGreg Roach}
969