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