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