xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 9219296a1acfac69c7d7f13505951ddeee5f8899)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Date;
22use Fisharebest\Webtrees\Fact;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\Functions\Functions;
25use Fisharebest\Webtrees\Gedcom;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Module;
29use Fisharebest\Webtrees\Services\ModuleService;
30use Fisharebest\Webtrees\Site;
31use Illuminate\Support\Collection;
32
33/**
34 * Class IndividualFactsTabModule
35 */
36class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
37{
38    use ModuleTabTrait;
39
40    /**
41     * @var ModuleService
42     */
43    private $module_service;
44
45    /**
46     * UserWelcomeModule constructor.
47     *
48     * @param ModuleService $module_service
49     */
50    public function __construct(ModuleService $module_service)
51    {
52        $this->module_service = $module_service;
53    }
54
55    /**
56     * How should this module be labelled on tabs, menus, etc.?
57     *
58     * @return string
59     */
60    public function title(): string
61    {
62        /* I18N: Name of a module/tab on the individual page. */
63        return I18N::translate('Facts and events');
64    }
65
66    /**
67     * A sentence describing what this module does.
68     *
69     * @return string
70     */
71    public function description(): string
72    {
73        /* I18N: Description of the “Facts and events” module */
74        return I18N::translate('A tab showing the facts and events of an individual.');
75    }
76
77    /**
78     * The default position for this tab.  It can be changed in the control panel.
79     *
80     * @return int
81     */
82    public function defaultTabOrder(): int
83    {
84        return 2;
85    }
86
87    /** {@inheritdoc} */
88    public function isGrayedOut(Individual $individual): bool
89    {
90        return false;
91    }
92
93    /** {@inheritdoc} */
94    public function getTabContent(Individual $individual): string
95    {
96        // Only include events of close relatives that are between birth and death
97        $min_date = $individual->getEstimatedBirthDate();
98        $max_date = $individual->getEstimatedDeathDate();
99
100        $indifacts = [];
101        // The individual’s own facts
102        foreach ($individual->facts() as $fact) {
103            switch ($fact->getTag()) {
104                case 'SEX':
105                case 'NAME':
106                case 'SOUR':
107                case 'OBJE':
108                case 'NOTE':
109                case 'FAMC':
110                case 'FAMS':
111                    break;
112
113                default:
114                    $use_extra_info_module = $this->module_service->findByComponent('sidebar', $individual->tree(), Auth::user())
115                        ->filter(function (ModuleInterface $module): bool {
116                            return $module instanceof ExtraInformationModule;
117                        })->isNotEmpty();
118
119                    if (!$use_extra_info_module || !ExtraInformationModule::showFact($fact)) {
120                        $indifacts[] = $fact;
121                    }
122                    break;
123            }
124        }
125
126        // Add spouse-family facts
127        foreach ($individual->getSpouseFamilies() as $family) {
128            foreach ($family->facts() as $fact) {
129                switch ($fact->getTag()) {
130                    case 'SOUR':
131                    case 'NOTE':
132                    case 'OBJE':
133                    case 'CHAN':
134                    case '_UID':
135                    case 'RIN':
136                    case 'HUSB':
137                    case 'WIFE':
138                    case 'CHIL':
139                        break;
140                    default:
141                        $indifacts[] = $fact;
142                        break;
143                }
144            }
145
146            $spouse = $family->getSpouse($individual);
147
148            if ($spouse instanceof Individual) {
149                $spouse_facts = self::spouseFacts($individual, $spouse, $min_date, $max_date);
150                $indifacts    = array_merge($indifacts, $spouse_facts);
151            }
152
153            $child_facts = self::childFacts($individual, $family, '_CHIL', '', $min_date, $max_date);
154            $indifacts   = array_merge($indifacts, $child_facts);
155        }
156
157        $parent_facts     = self::parentFacts($individual, 1, $min_date, $max_date);
158        $associate_facts  = self::associateFacts($individual);
159        $historical_facts = self::historicalFacts($individual);
160
161        $indifacts = array_merge($indifacts, $parent_facts, $associate_facts, $historical_facts);
162
163        Functions::sortFacts($indifacts);
164
165        return view('modules/personal_facts/tab', [
166            'can_edit'             => $individual->canEdit(),
167            'has_historical_facts' => !empty($historical_facts),
168            'individual'           => $individual,
169            'facts'                => $indifacts,
170        ]);
171    }
172
173    /**
174     * Does a relative event occur within a date range (i.e. the individual's lifetime)?
175     *
176     * @param Fact $fact
177     * @param Date $min_date
178     * @param Date $max_date
179     *
180     * @return bool
181     */
182    private static function includeFact(Fact $fact, Date $min_date, Date $max_date): bool
183    {
184        $fact_date = $fact->date();
185
186        return $fact_date->isOK() && Date::compare($min_date, $fact_date) <= 0 && Date::compare($fact_date, $max_date) <= 0;
187    }
188
189    /** {@inheritdoc} */
190    public function hasTabContent(Individual $individual): bool
191    {
192        return true;
193    }
194
195    /** {@inheritdoc} */
196    public function canLoadAjax(): bool
197    {
198        return false;
199    }
200
201    /**
202     * Spouse facts that are shown on an individual’s page.
203     *
204     * @param Individual $individual Show events that occured during the lifetime of this individual
205     * @param Individual $spouse     Show events of this individual
206     * @param Date       $min_date
207     * @param Date       $max_date
208     *
209     * @return Fact[]
210     */
211    private static function spouseFacts(Individual $individual, Individual $spouse, Date $min_date, Date $max_date): array
212    {
213        $SHOW_RELATIVES_EVENTS = $individual->tree()->getPreference('SHOW_RELATIVES_EVENTS');
214
215        $facts = [];
216        if (strstr($SHOW_RELATIVES_EVENTS, '_DEAT_SPOU')) {
217            foreach ($spouse->facts(Gedcom::DEATH_EVENTS) as $fact) {
218                if (self::includeFact($fact, $min_date, $max_date)) {
219                    // Convert the event to a close relatives event.
220                    $rela_fact = clone($fact);
221                    $rela_fact->setTag('_' . $fact->getTag() . '_SPOU');
222                    $facts[] = $rela_fact;
223                }
224            }
225        }
226
227        return $facts;
228    }
229
230    /**
231     * Get the events of children and grandchildren.
232     *
233     * @param Individual $person
234     * @param Family     $family
235     * @param string     $option
236     * @param string     $relation
237     * @param Date       $min_date
238     * @param Date       $max_date
239     *
240     * @return Fact[]
241     */
242    private static function childFacts(Individual $person, Family $family, $option, $relation, Date $min_date, Date $max_date): array
243    {
244        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
245
246        $facts = [];
247
248        // Deal with recursion.
249        switch ($option) {
250            case '_CHIL':
251                // Add grandchildren
252                foreach ($family->getChildren() as $child) {
253                    foreach ($child->getSpouseFamilies() as $cfamily) {
254                        switch ($child->getSex()) {
255                            case 'M':
256                                foreach (self::childFacts($person, $cfamily, '_GCHI', 'son', $min_date, $max_date) as $fact) {
257                                    $facts[] = $fact;
258                                }
259                                break;
260                            case 'F':
261                                foreach (self::childFacts($person, $cfamily, '_GCHI', 'dau', $min_date, $max_date) as $fact) {
262                                    $facts[] = $fact;
263                                }
264                                break;
265                            default:
266                                foreach (self::childFacts($person, $cfamily, '_GCHI', 'chi', $min_date, $max_date) as $fact) {
267                                    $facts[] = $fact;
268                                }
269                                break;
270                        }
271                    }
272                }
273                break;
274        }
275
276        // For each child in the family
277        foreach ($family->getChildren() as $child) {
278            if ($child->xref() == $person->xref()) {
279                // We are not our own sibling!
280                continue;
281            }
282            // add child’s birth
283            if (strpos($SHOW_RELATIVES_EVENTS, '_BIRT' . str_replace('_HSIB', '_SIBL', $option)) !== false) {
284                foreach ($child->facts(Gedcom::BIRTH_EVENTS) as $fact) {
285                    // Always show _BIRT_CHIL, even if the dates are not known
286                    if ($option == '_CHIL' || self::includeFact($fact, $min_date, $max_date)) {
287                        if ($option == '_GCHI' && $relation == 'dau') {
288                            // Convert the event to a close relatives event.
289                            $rela_fact = clone($fact);
290                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH1');
291                            $facts[] = $rela_fact;
292                        } elseif ($option == '_GCHI' && $relation == 'son') {
293                            // Convert the event to a close relatives event.
294                            $rela_fact = clone($fact);
295                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH2');
296                            $facts[] = $rela_fact;
297                        } else {
298                            // Convert the event to a close relatives event.
299                            $rela_fact = clone($fact);
300                            $rela_fact->setTag('_' . $fact->getTag() . $option);
301                            $facts[] = $rela_fact;
302                        }
303                    }
304                }
305            }
306            // add child’s death
307            if (strpos($SHOW_RELATIVES_EVENTS, '_DEAT' . str_replace('_HSIB', '_SIBL', $option)) !== false) {
308                foreach ($child->facts(Gedcom::DEATH_EVENTS) as $fact) {
309                    if (self::includeFact($fact, $min_date, $max_date)) {
310                        if ($option == '_GCHI' && $relation == 'dau') {
311                            // Convert the event to a close relatives event.
312                            $rela_fact = clone($fact);
313                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH1');
314                            $facts[] = $rela_fact;
315                        } elseif ($option == '_GCHI' && $relation == 'son') {
316                            // Convert the event to a close relatives event.
317                            $rela_fact = clone($fact);
318                            $rela_fact->setTag('_' . $fact->getTag() . '_GCH2');
319                            $facts[] = $rela_fact;
320                        } else {
321                            // Convert the event to a close relatives event.
322                            $rela_fact = clone($fact);
323                            $rela_fact->setTag('_' . $fact->getTag() . $option);
324                            $facts[] = $rela_fact;
325                        }
326                    }
327                }
328            }
329            // add child’s marriage
330            if (strstr($SHOW_RELATIVES_EVENTS, '_MARR' . str_replace('_HSIB', '_SIBL', $option))) {
331                foreach ($child->getSpouseFamilies() as $sfamily) {
332                    foreach ($sfamily->facts(['MARR']) as $fact) {
333                        if (self::includeFact($fact, $min_date, $max_date)) {
334                            if ($option == '_GCHI' && $relation == 'dau') {
335                                // Convert the event to a close relatives event.
336                                $rela_fact = clone($fact);
337                                $rela_fact->setTag('_' . $fact->getTag() . '_GCH1');
338                                $facts[] = $rela_fact;
339                            } elseif ($option == '_GCHI' && $relation == 'son') {
340                                // Convert the event to a close relatives event.
341                                $rela_fact = clone($fact);
342                                $rela_fact->setTag('_' . $fact->getTag() . '_GCH2');
343                                $facts[] = $rela_fact;
344                            } else {
345                                // Convert the event to a close relatives event.
346                                $rela_fact = clone($fact);
347                                $rela_fact->setTag('_' . $fact->getTag() . $option);
348                                $facts[] = $rela_fact;
349                            }
350                        }
351                    }
352                }
353            }
354        }
355
356        return $facts;
357    }
358
359    /**
360     * Get the events of parents and grandparents.
361     *
362     * @param Individual $person
363     * @param int        $sosa
364     * @param Date       $min_date
365     * @param Date       $max_date
366     *
367     * @return Fact[]
368     */
369    private static function parentFacts(Individual $person, $sosa, Date $min_date, Date $max_date): array
370    {
371        $SHOW_RELATIVES_EVENTS = $person->tree()->getPreference('SHOW_RELATIVES_EVENTS');
372
373        $facts = [];
374
375        if ($sosa == 1) {
376            foreach ($person->getChildFamilies() as $family) {
377                // Add siblings
378                foreach (self::childFacts($person, $family, '_SIBL', '', $min_date, $max_date) as $fact) {
379                    $facts[] = $fact;
380                }
381                foreach ($family->getSpouses() as $spouse) {
382                    foreach ($spouse->getSpouseFamilies() as $sfamily) {
383                        if ($family !== $sfamily) {
384                            // Add half-siblings
385                            foreach (self::childFacts($person, $sfamily, '_HSIB', '', $min_date, $max_date) as $fact) {
386                                $facts[] = $fact;
387                            }
388                        }
389                    }
390                    // Add grandparents
391                    foreach (self::parentFacts($spouse, $spouse->getSex() == 'F' ? 3 : 2, $min_date, $max_date) as $fact) {
392                        $facts[] = $fact;
393                    }
394                }
395            }
396
397            if (strstr($SHOW_RELATIVES_EVENTS, '_MARR_PARE')) {
398                // add father/mother marriages
399                foreach ($person->getChildFamilies() as $sfamily) {
400                    foreach ($sfamily->facts(['MARR']) as $fact) {
401                        if (self::includeFact($fact, $min_date, $max_date)) {
402                            // marriage of parents (to each other)
403                            $rela_fact = clone($fact);
404                            $rela_fact->setTag('_' . $fact->getTag() . '_FAMC');
405                            $facts[] = $rela_fact;
406                        }
407                    }
408                }
409                foreach ($person->getChildStepFamilies() as $sfamily) {
410                    foreach ($sfamily->facts(['MARR']) as $fact) {
411                        if (self::includeFact($fact, $min_date, $max_date)) {
412                            // marriage of a parent (to another spouse)
413                            // Convert the event to a close relatives event
414                            $rela_fact = clone($fact);
415                            $rela_fact->setTag('_' . $fact->getTag() . '_PARE');
416                            $facts[] = $rela_fact;
417                        }
418                    }
419                }
420            }
421        }
422
423        foreach ($person->getChildFamilies() as $family) {
424            foreach ($family->getSpouses() as $parent) {
425                if (strstr($SHOW_RELATIVES_EVENTS, '_DEAT' . ($sosa == 1 ? '_PARE' : '_GPAR'))) {
426                    foreach ($parent->facts(Gedcom::DEATH_EVENTS) as $fact) {
427                        if (self::includeFact($fact, $min_date, $max_date)) {
428                            switch ($sosa) {
429                                case 1:
430                                    // Convert the event to a close relatives event.
431                                    $rela_fact = clone($fact);
432                                    $rela_fact->setTag('_' . $fact->getTag() . '_PARE');
433                                    $facts[] = $rela_fact;
434                                    break;
435                                case 2:
436                                    // Convert the event to a close relatives event
437                                    $rela_fact = clone($fact);
438                                    $rela_fact->setTag('_' . $fact->getTag() . '_GPA1');
439                                    $facts[] = $rela_fact;
440                                    break;
441                                case 3:
442                                    // Convert the event to a close relatives event
443                                    $rela_fact = clone($fact);
444                                    $rela_fact->setTag('_' . $fact->getTag() . '_GPA2');
445                                    $facts[] = $rela_fact;
446                                    break;
447                            }
448                        }
449                    }
450                }
451            }
452        }
453
454        return $facts;
455    }
456
457    /**
458     * Get any historical events.
459     *
460     * @param Individual $individual
461     *
462     * @return Fact[]
463     */
464    private function historicalFacts(Individual $individual): array
465    {
466        return $this->module_service->findByInterface(ModuleHistoricEventsInterface::class)
467            ->map(function (ModuleHistoricEventsInterface $module) use ($individual): Collection {
468                return $module->historicEventsForIndividual($individual);
469            })
470            ->flatten()
471            ->all();
472    }
473
474    /**
475     * Get the events of associates.
476     *
477     * @param Individual $person
478     *
479     * @return Fact[]
480     */
481    private static function associateFacts(Individual $person): array
482    {
483        $facts = [];
484
485        /** @var Individual[] $associates */
486        $associates = array_merge(
487            $person->linkedIndividuals('ASSO'),
488            $person->linkedIndividuals('_ASSO'),
489            $person->linkedFamilies('ASSO'),
490            $person->linkedFamilies('_ASSO')
491        );
492        foreach ($associates as $associate) {
493            foreach ($associate->facts() as $fact) {
494                $arec = $fact->attribute('_ASSO');
495                if (!$arec) {
496                    $arec = $fact->attribute('ASSO');
497                }
498                if ($arec && trim($arec, '@') === $person->xref()) {
499                    // Extract the important details from the fact
500                    $factrec = '1 ' . $fact->getTag();
501                    if (preg_match('/\n2 DATE .*/', $fact->gedcom(), $match)) {
502                        $factrec .= $match[0];
503                    }
504                    if (preg_match('/\n2 PLAC .*/', $fact->gedcom(), $match)) {
505                        $factrec .= $match[0];
506                    }
507                    if ($associate instanceof Family) {
508                        foreach ($associate->getSpouses() as $spouse) {
509                            $factrec .= "\n2 _ASSO @" . $spouse->xref() . '@';
510                        }
511                    } else {
512                        $factrec .= "\n2 _ASSO @" . $associate->xref() . '@';
513                    }
514                    $facts[] = new Fact($factrec, $associate, 'asso');
515                }
516            }
517        }
518
519        return $facts;
520    }
521}
522