xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 5bdbe281716f5b59896b1aae8ba03e4572b315bb)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
53763c3f2SGreg Roach * This program is free software: you can redistribute it and/or modify
63763c3f2SGreg Roach * it under the terms of the GNU General Public License as published by
73763c3f2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
83763c3f2SGreg Roach * (at your option) any later version.
93763c3f2SGreg Roach * This program is distributed in the hope that it will be useful,
103763c3f2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
113763c3f2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123763c3f2SGreg Roach * GNU General Public License for more details.
133763c3f2SGreg Roach * You should have received a copy of the GNU General Public License
143763c3f2SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
153763c3f2SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
2063276d8fSGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
258d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module;
294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
3117c50b57SGreg Roachuse Illuminate\Support\Collection;
323763c3f2SGreg Roach
333763c3f2SGreg Roach/**
343763c3f2SGreg Roach * Class IndividualFactsTabModule
353763c3f2SGreg Roach */
3637eb8894SGreg Roachclass IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleTabTrait;
3949a243cbSGreg Roach
40961ec755SGreg Roach    /**
414ca7e03cSGreg Roach     * @var ModuleService
424ca7e03cSGreg Roach     */
434ca7e03cSGreg Roach    private $module_service;
444ca7e03cSGreg Roach
454ca7e03cSGreg Roach    /**
464ca7e03cSGreg Roach     * UserWelcomeModule constructor.
474ca7e03cSGreg Roach     *
484ca7e03cSGreg Roach     * @param ModuleService $module_service
494ca7e03cSGreg Roach     */
50*5bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
51*5bdbe281SGreg Roach    {
524ca7e03cSGreg Roach        $this->module_service = $module_service;
534ca7e03cSGreg Roach    }
544ca7e03cSGreg Roach
554ca7e03cSGreg Roach    /**
56961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
57961ec755SGreg Roach     *
58961ec755SGreg Roach     * @return string
59961ec755SGreg Roach     */
6049a243cbSGreg Roach    public function title(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Name of a module/tab on the individual page. */
63bbb76c12SGreg Roach        return I18N::translate('Facts and events');
643763c3f2SGreg Roach    }
653763c3f2SGreg Roach
66961ec755SGreg Roach    /**
67961ec755SGreg Roach     * A sentence describing what this module does.
68961ec755SGreg Roach     *
69961ec755SGreg Roach     * @return string
70961ec755SGreg Roach     */
7149a243cbSGreg Roach    public function description(): string
72c1010edaSGreg Roach    {
73bbb76c12SGreg Roach        /* I18N: Description of the “Facts and events” module */
74bbb76c12SGreg Roach        return I18N::translate('A tab showing the facts and events of an individual.');
753763c3f2SGreg Roach    }
763763c3f2SGreg Roach
7749a243cbSGreg Roach    /**
7849a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
7949a243cbSGreg Roach     *
8049a243cbSGreg Roach     * @return int
8149a243cbSGreg Roach     */
82cbf4b7faSGreg Roach    public function defaultTabOrder(): int
83cbf4b7faSGreg Roach    {
84353b36abSGreg Roach        return 2;
853763c3f2SGreg Roach    }
863763c3f2SGreg Roach
873763c3f2SGreg Roach    /** {@inheritdoc} */
888f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
89c1010edaSGreg Roach    {
903763c3f2SGreg Roach        return false;
913763c3f2SGreg Roach    }
923763c3f2SGreg Roach
933763c3f2SGreg Roach    /** {@inheritdoc} */
949b34404bSGreg Roach    public function getTabContent(Individual $individual): string
95c1010edaSGreg Roach    {
96ee727175SGreg Roach        // Only include events of close relatives that are between birth and death
97ee727175SGreg Roach        $min_date = $individual->getEstimatedBirthDate();
98ee727175SGreg Roach        $max_date = $individual->getEstimatedDeathDate();
99ee727175SGreg Roach
10013abd6f3SGreg Roach        $indifacts = [];
1013763c3f2SGreg Roach        // The individual’s own facts
10230158ae7SGreg Roach        foreach ($individual->facts() as $fact) {
1033763c3f2SGreg Roach            switch ($fact->getTag()) {
1043763c3f2SGreg Roach                case 'SEX':
1053763c3f2SGreg Roach                case 'NAME':
1063763c3f2SGreg Roach                case 'SOUR':
1073763c3f2SGreg Roach                case 'OBJE':
1083763c3f2SGreg Roach                case 'NOTE':
1093763c3f2SGreg Roach                case 'FAMC':
1103763c3f2SGreg Roach                case 'FAMS':
1113763c3f2SGreg Roach                    break;
11249a243cbSGreg Roach
1133763c3f2SGreg Roach                default:
1144ca7e03cSGreg Roach                    $use_extra_info_module = $this->module_service->findByComponent('sidebar', $individual->tree(), Auth::user())
11549a243cbSGreg Roach                        ->filter(function (ModuleInterface $module): bool {
11649a243cbSGreg Roach                            return $module instanceof ExtraInformationModule;
11749a243cbSGreg Roach                        })->isNotEmpty();
11849a243cbSGreg Roach
11949a243cbSGreg Roach                    if (!$use_extra_info_module || !ExtraInformationModule::showFact($fact)) {
1203763c3f2SGreg Roach                        $indifacts[] = $fact;
1213763c3f2SGreg Roach                    }
1223763c3f2SGreg Roach                    break;
1233763c3f2SGreg Roach            }
1243763c3f2SGreg Roach        }
1253763c3f2SGreg Roach
1263763c3f2SGreg Roach        // Add spouse-family facts
127225e381fSGreg Roach        foreach ($individual->getSpouseFamilies() as $family) {
12830158ae7SGreg Roach            foreach ($family->facts() as $fact) {
1293763c3f2SGreg Roach                switch ($fact->getTag()) {
1303763c3f2SGreg Roach                    case 'SOUR':
1313763c3f2SGreg Roach                    case 'NOTE':
1323763c3f2SGreg Roach                    case 'OBJE':
1333763c3f2SGreg Roach                    case 'CHAN':
1343763c3f2SGreg Roach                    case '_UID':
1353763c3f2SGreg Roach                    case 'RIN':
1363763c3f2SGreg Roach                    case 'HUSB':
1373763c3f2SGreg Roach                    case 'WIFE':
1383763c3f2SGreg Roach                    case 'CHIL':
1393763c3f2SGreg Roach                        break;
1403763c3f2SGreg Roach                    default:
1413763c3f2SGreg Roach                        $indifacts[] = $fact;
1423763c3f2SGreg Roach                        break;
1433763c3f2SGreg Roach                }
1443763c3f2SGreg Roach            }
145ee727175SGreg Roach
146225e381fSGreg Roach            $spouse = $family->getSpouse($individual);
147ee727175SGreg Roach
148ee727175SGreg Roach            if ($spouse instanceof Individual) {
149ee727175SGreg Roach                $spouse_facts = self::spouseFacts($individual, $spouse, $min_date, $max_date);
150ee727175SGreg Roach                $indifacts    = array_merge($indifacts, $spouse_facts);
1513763c3f2SGreg Roach            }
1523763c3f2SGreg Roach
153ee727175SGreg Roach            $child_facts = self::childFacts($individual, $family, '_CHIL', '', $min_date, $max_date);
154ee727175SGreg Roach            $indifacts   = array_merge($indifacts, $child_facts);
1553763c3f2SGreg Roach        }
156225e381fSGreg Roach
157ee727175SGreg Roach        $parent_facts     = self::parentFacts($individual, 1, $min_date, $max_date);
158ee727175SGreg Roach        $associate_facts  = self::associateFacts($individual);
15917c50b57SGreg Roach        $historical_facts = self::historicalFacts($individual);
160225e381fSGreg Roach
161ee727175SGreg Roach        $indifacts = array_merge($indifacts, $parent_facts, $associate_facts, $historical_facts);
1623763c3f2SGreg Roach
1633d7a8a4cSGreg Roach        Functions::sortFacts($indifacts);
1643763c3f2SGreg Roach
165a8cd57e1SGreg Roach        return view('modules/personal_facts/tab', [
166225e381fSGreg Roach            'can_edit'             => $individual->canEdit(),
167ee727175SGreg Roach            'has_historical_facts' => !empty($historical_facts),
168225e381fSGreg Roach            'individual'           => $individual,
169225e381fSGreg Roach            'facts'                => $indifacts,
170225e381fSGreg Roach        ]);
1713763c3f2SGreg Roach    }
1723763c3f2SGreg Roach
173ee727175SGreg Roach    /**
174ee727175SGreg Roach     * Does a relative event occur within a date range (i.e. the individual's lifetime)?
175ee727175SGreg Roach     *
176ee727175SGreg Roach     * @param Fact $fact
177ee727175SGreg Roach     * @param Date $min_date
178ee727175SGreg Roach     * @param Date $max_date
179ee727175SGreg Roach     *
180ee727175SGreg Roach     * @return bool
181ee727175SGreg Roach     */
182b3b1d905SGreg Roach    private static function includeFact(Fact $fact, Date $min_date, Date $max_date): bool
183b3b1d905SGreg Roach    {
1842decada7SGreg Roach        $fact_date = $fact->date();
185ee727175SGreg Roach
186ee727175SGreg Roach        return $fact_date->isOK() && Date::compare($min_date, $fact_date) <= 0 && Date::compare($fact_date, $max_date) <= 0;
187ee727175SGreg Roach    }
188ee727175SGreg Roach
1893763c3f2SGreg Roach    /** {@inheritdoc} */
1908f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
191c1010edaSGreg Roach    {
1923763c3f2SGreg Roach        return true;
1933763c3f2SGreg Roach    }
1943763c3f2SGreg Roach
1953763c3f2SGreg Roach    /** {@inheritdoc} */
1968f53f488SRico Sonntag    public function canLoadAjax(): bool
197c1010edaSGreg Roach    {
19815d603e7SGreg Roach        return false;
1993763c3f2SGreg Roach    }
2003763c3f2SGreg Roach
2013763c3f2SGreg Roach    /**
2023763c3f2SGreg Roach     * Spouse facts that are shown on an individual’s page.
2033763c3f2SGreg Roach     *
2043763c3f2SGreg Roach     * @param Individual $individual Show events that occured during the lifetime of this individual
2053763c3f2SGreg Roach     * @param Individual $spouse     Show events of this individual
206ee727175SGreg Roach     * @param Date       $min_date
207ee727175SGreg Roach     * @param Date       $max_date
2083763c3f2SGreg Roach     *
2093763c3f2SGreg Roach     * @return Fact[]
2103763c3f2SGreg Roach     */
211ee727175SGreg Roach    private static function spouseFacts(Individual $individual, Individual $spouse, Date $min_date, Date $max_date): array
212c1010edaSGreg Roach    {
213f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $individual->tree()->getPreference('SHOW_RELATIVES_EVENTS');
2143763c3f2SGreg Roach
21513abd6f3SGreg Roach        $facts = [];
2163763c3f2SGreg Roach        if (strstr($SHOW_RELATIVES_EVENTS, '_DEAT_SPOU')) {
2178d0ebef0SGreg Roach            foreach ($spouse->facts(Gedcom::DEATH_EVENTS) as $fact) {
218ee727175SGreg Roach                if (self::includeFact($fact, $min_date, $max_date)) {
2193763c3f2SGreg Roach                    // Convert the event to a close relatives event.
2203763c3f2SGreg Roach                    $rela_fact = clone($fact);
2213763c3f2SGreg Roach                    $rela_fact->setTag('_' . $fact->getTag() . '_SPOU');
2223763c3f2SGreg Roach                    $facts[] = $rela_fact;
2233763c3f2SGreg Roach                }
2243763c3f2SGreg Roach            }
2253763c3f2SGreg Roach        }
2263763c3f2SGreg Roach
2273763c3f2SGreg Roach        return $facts;
2283763c3f2SGreg Roach    }
2293763c3f2SGreg Roach
2303763c3f2SGreg Roach    /**
2313763c3f2SGreg Roach     * Get the events of children and grandchildren.
2323763c3f2SGreg Roach     *
2333763c3f2SGreg Roach     * @param Individual $person
2343763c3f2SGreg Roach     * @param Family     $family
2353763c3f2SGreg Roach     * @param string     $option
2363763c3f2SGreg Roach     * @param string     $relation
237ee727175SGreg Roach     * @param Date       $min_date
238ee727175SGreg Roach     * @param Date       $max_date
2393763c3f2SGreg Roach     *
2403763c3f2SGreg Roach     * @return Fact[]
2413763c3f2SGreg Roach     */
242ee727175SGreg Roach    private static function childFacts(Individual $person, Family $family, $option, $relation, Date $min_date, Date $max_date): array
243c1010edaSGreg Roach    {
244f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
2453763c3f2SGreg Roach
24613abd6f3SGreg Roach        $facts = [];
2473763c3f2SGreg Roach
2483763c3f2SGreg Roach        // Deal with recursion.
2493763c3f2SGreg Roach        switch ($option) {
2503763c3f2SGreg Roach            case '_CHIL':
2513763c3f2SGreg Roach                // Add grandchildren
2523763c3f2SGreg Roach                foreach ($family->getChildren() as $child) {
2533763c3f2SGreg Roach                    foreach ($child->getSpouseFamilies() as $cfamily) {
2543763c3f2SGreg Roach                        switch ($child->getSex()) {
2553763c3f2SGreg Roach                            case 'M':
256ee727175SGreg Roach                                foreach (self::childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
2573763c3f2SGreg Roach                                    $facts[] = $fact;
2583763c3f2SGreg Roach                                }
2593763c3f2SGreg Roach                                break;
2603763c3f2SGreg Roach                            case 'F':
261ee727175SGreg Roach                                foreach (self::childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
2623763c3f2SGreg Roach                                    $facts[] = $fact;
2633763c3f2SGreg Roach                                }
2643763c3f2SGreg Roach                                break;
2653763c3f2SGreg Roach                            default:
266ee727175SGreg Roach                                foreach (self::childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
2673763c3f2SGreg Roach                                    $facts[] = $fact;
2683763c3f2SGreg Roach                                }
2693763c3f2SGreg Roach                                break;
2703763c3f2SGreg Roach                        }
2713763c3f2SGreg Roach                    }
2723763c3f2SGreg Roach                }
2733763c3f2SGreg Roach                break;
2743763c3f2SGreg Roach        }
2753763c3f2SGreg Roach
2763763c3f2SGreg Roach        // For each child in the family
2773763c3f2SGreg Roach        foreach ($family->getChildren() as $child) {
278c0935879SGreg Roach            if ($child->xref() == $person->xref()) {
2793763c3f2SGreg Roach                // We are not our own sibling!
2803763c3f2SGreg Roach                continue;
2813763c3f2SGreg Roach            }
2823763c3f2SGreg Roach            // add child’s birth
2833763c3f2SGreg Roach            if (strpos($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option)) !== false) {
2848d0ebef0SGreg Roach                foreach ($child->facts(Gedcom::BIRTH_EVENTS) as $fact) {
2853763c3f2SGreg Roach                    // Always show _BIRT_CHIL, even if the dates are not known
286ee727175SGreg Roach                    if ($option == '_CHIL' || self::includeFact($fact, $min_date, $max_date)) {
2873763c3f2SGreg Roach                        if ($option == '_GCHI' && $relation == 'dau') {
2883763c3f2SGreg Roach                            // Convert the event to a close relatives event.
2893763c3f2SGreg Roach                            $rela_fact = clone($fact);
2903763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH1');
2913763c3f2SGreg Roach                            $facts[] = $rela_fact;
2923763c3f2SGreg Roach                        } elseif ($option == '_GCHI' && $relation == 'son') {
2933763c3f2SGreg Roach                            // Convert the event to a close relatives event.
2943763c3f2SGreg Roach                            $rela_fact = clone($fact);
2953763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH2');
2963763c3f2SGreg Roach                            $facts[] = $rela_fact;
2973763c3f2SGreg Roach                        } else {
2983763c3f2SGreg Roach                            // Convert the event to a close relatives event.
2993763c3f2SGreg Roach                            $rela_fact = clone($fact);
3003763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . $option);
3013763c3f2SGreg Roach                            $facts[] = $rela_fact;
3023763c3f2SGreg Roach                        }
3033763c3f2SGreg Roach                    }
3043763c3f2SGreg Roach                }
3053763c3f2SGreg Roach            }
3063763c3f2SGreg Roach            // add child’s death
3073763c3f2SGreg Roach            if (strpos($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option)) !== false) {
3088d0ebef0SGreg Roach                foreach ($child->facts(Gedcom::DEATH_EVENTS) as $fact) {
309ee727175SGreg Roach                    if (self::includeFact($fact, $min_date, $max_date)) {
3103763c3f2SGreg Roach                        if ($option == '_GCHI' && $relation == 'dau') {
3113763c3f2SGreg Roach                            // Convert the event to a close relatives event.
3123763c3f2SGreg Roach                            $rela_fact = clone($fact);
3133763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH1');
3143763c3f2SGreg Roach                            $facts[] = $rela_fact;
3153763c3f2SGreg Roach                        } elseif ($option == '_GCHI' && $relation == 'son') {
3163763c3f2SGreg Roach                            // Convert the event to a close relatives event.
3173763c3f2SGreg Roach                            $rela_fact = clone($fact);
3183763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH2');
3193763c3f2SGreg Roach                            $facts[] = $rela_fact;
3203763c3f2SGreg Roach                        } else {
3213763c3f2SGreg Roach                            // Convert the event to a close relatives event.
3223763c3f2SGreg Roach                            $rela_fact = clone($fact);
3233763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . $option);
3243763c3f2SGreg Roach                            $facts[] = $rela_fact;
3253763c3f2SGreg Roach                        }
3263763c3f2SGreg Roach                    }
3273763c3f2SGreg Roach                }
3283763c3f2SGreg Roach            }
3293763c3f2SGreg Roach            // add child’s marriage
3303763c3f2SGreg Roach            if (strstr($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
3313763c3f2SGreg Roach                foreach ($child->getSpouseFamilies() as $sfamily) {
3328d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
333ee727175SGreg Roach                        if (self::includeFact($fact, $min_date, $max_date)) {
3343763c3f2SGreg Roach                            if ($option == '_GCHI' && $relation == 'dau') {
3353763c3f2SGreg Roach                                // Convert the event to a close relatives event.
3363763c3f2SGreg Roach                                $rela_fact = clone($fact);
3373763c3f2SGreg Roach                                $rela_fact->setTag('_' . $fact->getTag() . '_GCH1');
3383763c3f2SGreg Roach                                $facts[] = $rela_fact;
3393763c3f2SGreg Roach                            } elseif ($option == '_GCHI' && $relation == 'son') {
3403763c3f2SGreg Roach                                // Convert the event to a close relatives event.
3413763c3f2SGreg Roach                                $rela_fact = clone($fact);
3423763c3f2SGreg Roach                                $rela_fact->setTag('_' . $fact->getTag() . '_GCH2');
3433763c3f2SGreg Roach                                $facts[] = $rela_fact;
3443763c3f2SGreg Roach                            } else {
3453763c3f2SGreg Roach                                // Convert the event to a close relatives event.
3463763c3f2SGreg Roach                                $rela_fact = clone($fact);
3473763c3f2SGreg Roach                                $rela_fact->setTag('_' . $fact->getTag() . $option);
3483763c3f2SGreg Roach                                $facts[] = $rela_fact;
3493763c3f2SGreg Roach                            }
3503763c3f2SGreg Roach                        }
3513763c3f2SGreg Roach                    }
3523763c3f2SGreg Roach                }
3533763c3f2SGreg Roach            }
3543763c3f2SGreg Roach        }
3553763c3f2SGreg Roach
3563763c3f2SGreg Roach        return $facts;
3573763c3f2SGreg Roach    }
3583763c3f2SGreg Roach
3593763c3f2SGreg Roach    /**
3603763c3f2SGreg Roach     * Get the events of parents and grandparents.
3613763c3f2SGreg Roach     *
3623763c3f2SGreg Roach     * @param Individual $person
363cbc1590aSGreg Roach     * @param int        $sosa
364ee727175SGreg Roach     * @param Date       $min_date
365ee727175SGreg Roach     * @param Date       $max_date
3663763c3f2SGreg Roach     *
3673763c3f2SGreg Roach     * @return Fact[]
3683763c3f2SGreg Roach     */
369ee727175SGreg Roach    private static function parentFacts(Individual $person, $sosa, Date $min_date, Date $max_date): array
370c1010edaSGreg Roach    {
371f4afa648SGreg Roach        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
3723763c3f2SGreg Roach
37313abd6f3SGreg Roach        $facts = [];
3743763c3f2SGreg Roach
3753763c3f2SGreg Roach        if ($sosa == 1) {
3763763c3f2SGreg Roach            foreach ($person->getChildFamilies() as $family) {
3773763c3f2SGreg Roach                // Add siblings
378ee727175SGreg Roach                foreach (self::childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
3793763c3f2SGreg Roach                    $facts[] = $fact;
3803763c3f2SGreg Roach                }
3813763c3f2SGreg Roach                foreach ($family->getSpouses() as $spouse) {
3823763c3f2SGreg Roach                    foreach ($spouse->getSpouseFamilies() as $sfamily) {
3833763c3f2SGreg Roach                        if ($family !== $sfamily) {
3843763c3f2SGreg Roach                            // Add half-siblings
385ee727175SGreg Roach                            foreach (self::childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
3863763c3f2SGreg Roach                                $facts[] = $fact;
3873763c3f2SGreg Roach                            }
3883763c3f2SGreg Roach                        }
3893763c3f2SGreg Roach                    }
3903763c3f2SGreg Roach                    // Add grandparents
391ee727175SGreg Roach                    foreach (self::parentFacts($spouse, $spouse->getSex() == 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
3923763c3f2SGreg Roach                        $facts[] = $fact;
3933763c3f2SGreg Roach                    }
3943763c3f2SGreg Roach                }
3953763c3f2SGreg Roach            }
3963763c3f2SGreg Roach
3973763c3f2SGreg Roach            if (strstr($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
3983763c3f2SGreg Roach                // add father/mother marriages
3993763c3f2SGreg Roach                foreach ($person->getChildFamilies() as $sfamily) {
4008d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
401ee727175SGreg Roach                        if (self::includeFact($fact, $min_date, $max_date)) {
4023763c3f2SGreg Roach                            // marriage of parents (to each other)
4033763c3f2SGreg Roach                            $rela_fact = clone($fact);
4043763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . '_FAMC');
4053763c3f2SGreg Roach                            $facts[] = $rela_fact;
4063763c3f2SGreg Roach                        }
4073763c3f2SGreg Roach                    }
4083763c3f2SGreg Roach                }
4093763c3f2SGreg Roach                foreach ($person->getChildStepFamilies() as $sfamily) {
4108d0ebef0SGreg Roach                    foreach ($sfamily->facts(['MARR']) as $fact) {
411ee727175SGreg Roach                        if (self::includeFact($fact, $min_date, $max_date)) {
4123763c3f2SGreg Roach                            // marriage of a parent (to another spouse)
4133763c3f2SGreg Roach                            // Convert the event to a close relatives event
4143763c3f2SGreg Roach                            $rela_fact = clone($fact);
4153763c3f2SGreg Roach                            $rela_fact->setTag('_' . $fact->getTag() . '_PARE');
4163763c3f2SGreg Roach                            $facts[] = $rela_fact;
4173763c3f2SGreg Roach                        }
4183763c3f2SGreg Roach                    }
4193763c3f2SGreg Roach                }
4203763c3f2SGreg Roach            }
4213763c3f2SGreg Roach        }
4223763c3f2SGreg Roach
4233763c3f2SGreg Roach        foreach ($person->getChildFamilies() as $family) {
4243763c3f2SGreg Roach            foreach ($family->getSpouses() as $parent) {
4253763c3f2SGreg Roach                if (strstr($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa == 1 ? '_PARE' : '_GPAR'))) {
4268d0ebef0SGreg Roach                    foreach ($parent->facts(Gedcom::DEATH_EVENTS) as $fact) {
427ee727175SGreg Roach                        if (self::includeFact($fact, $min_date, $max_date)) {
4283763c3f2SGreg Roach                            switch ($sosa) {
4293763c3f2SGreg Roach                                case 1:
4303763c3f2SGreg Roach                                    // Convert the event to a close relatives event.
4313763c3f2SGreg Roach                                    $rela_fact = clone($fact);
4323763c3f2SGreg Roach                                    $rela_fact->setTag('_' . $fact->getTag() . '_PARE');
4333763c3f2SGreg Roach                                    $facts[] = $rela_fact;
4343763c3f2SGreg Roach                                    break;
4353763c3f2SGreg Roach                                case 2:
4363763c3f2SGreg Roach                                    // Convert the event to a close relatives event
4373763c3f2SGreg Roach                                    $rela_fact = clone($fact);
4383763c3f2SGreg Roach                                    $rela_fact->setTag('_' . $fact->getTag() . '_GPA1');
4393763c3f2SGreg Roach                                    $facts[] = $rela_fact;
4403763c3f2SGreg Roach                                    break;
4413763c3f2SGreg Roach                                case 3:
4423763c3f2SGreg Roach                                    // Convert the event to a close relatives event
4433763c3f2SGreg Roach                                    $rela_fact = clone($fact);
4443763c3f2SGreg Roach                                    $rela_fact->setTag('_' . $fact->getTag() . '_GPA2');
4453763c3f2SGreg Roach                                    $facts[] = $rela_fact;
4463763c3f2SGreg Roach                                    break;
4473763c3f2SGreg Roach                            }
4483763c3f2SGreg Roach                        }
4493763c3f2SGreg Roach                    }
4503763c3f2SGreg Roach                }
4513763c3f2SGreg Roach            }
4523763c3f2SGreg Roach        }
4533763c3f2SGreg Roach
4543763c3f2SGreg Roach        return $facts;
4553763c3f2SGreg Roach    }
4563763c3f2SGreg Roach
4573763c3f2SGreg Roach    /**
4583763c3f2SGreg Roach     * Get any historical events.
4593763c3f2SGreg Roach     *
46017c50b57SGreg Roach     * @param Individual $individual
4613763c3f2SGreg Roach     *
4623763c3f2SGreg Roach     * @return Fact[]
4633763c3f2SGreg Roach     */
4644ca7e03cSGreg Roach    private function historicalFacts(Individual $individual): array
465c1010edaSGreg Roach    {
4664ca7e03cSGreg Roach        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
46717c50b57SGreg Roach            ->map(function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
46817c50b57SGreg Roach                return $module->historicEventsForIndividual($individual);
46917c50b57SGreg Roach            })
47017c50b57SGreg Roach            ->flatten()
47117c50b57SGreg Roach            ->all();
4723763c3f2SGreg Roach    }
4733763c3f2SGreg Roach
4743763c3f2SGreg Roach    /**
4753763c3f2SGreg Roach     * Get the events of associates.
4763763c3f2SGreg Roach     *
4773763c3f2SGreg Roach     * @param Individual $person
4783763c3f2SGreg Roach     *
4793763c3f2SGreg Roach     * @return Fact[]
4803763c3f2SGreg Roach     */
4818f53f488SRico Sonntag    private static function associateFacts(Individual $person): array
482c1010edaSGreg Roach    {
48313abd6f3SGreg Roach        $facts = [];
4843763c3f2SGreg Roach
485ee727175SGreg Roach        /** @var Individual[] $associates */
4863763c3f2SGreg Roach        $associates = array_merge(
4873763c3f2SGreg Roach            $person->linkedIndividuals('ASSO'),
4883763c3f2SGreg Roach            $person->linkedIndividuals('_ASSO'),
4893763c3f2SGreg Roach            $person->linkedFamilies('ASSO'),
4903763c3f2SGreg Roach            $person->linkedFamilies('_ASSO')
4913763c3f2SGreg Roach        );
4923763c3f2SGreg Roach        foreach ($associates as $associate) {
49330158ae7SGreg Roach            foreach ($associate->facts() as $fact) {
4943425616eSGreg Roach                $arec = $fact->attribute('_ASSO');
4953763c3f2SGreg Roach                if (!$arec) {
4963425616eSGreg Roach                    $arec = $fact->attribute('ASSO');
4973763c3f2SGreg Roach                }
498c0935879SGreg Roach                if ($arec && trim($arec, '@') === $person->xref()) {
4993763c3f2SGreg Roach                    // Extract the important details from the fact
5003763c3f2SGreg Roach                    $factrec = '1 ' . $fact->getTag();
501138ca96cSGreg Roach                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
5023763c3f2SGreg Roach                        $factrec .= $match[0];
5033763c3f2SGreg Roach                    }
504138ca96cSGreg Roach                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
5053763c3f2SGreg Roach                        $factrec .= $match[0];
5063763c3f2SGreg Roach                    }
5073763c3f2SGreg Roach                    if ($associate instanceof Family) {
5083763c3f2SGreg Roach                        foreach ($associate->getSpouses() as $spouse) {
509c0935879SGreg Roach                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
5103763c3f2SGreg Roach                        }
5113763c3f2SGreg Roach                    } else {
512c0935879SGreg Roach                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
5133763c3f2SGreg Roach                    }
5143763c3f2SGreg Roach                    $facts[] = new Fact($factrec, $associate, 'asso');
5153763c3f2SGreg Roach                }
5163763c3f2SGreg Roach            }
5173763c3f2SGreg Roach        }
5183763c3f2SGreg Roach
5193763c3f2SGreg Roach        return $facts;
5203763c3f2SGreg Roach    }
5213763c3f2SGreg Roach}
522