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