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