xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Elements\AdoptedByWhichParent;
24use Fisharebest\Webtrees\Elements\CustomElement;
25use Fisharebest\Webtrees\Elements\XrefFamily;
26use Fisharebest\Webtrees\Fact;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Services\ClipboardService;
31use Fisharebest\Webtrees\Services\IndividualFactsService;
32use Fisharebest\Webtrees\Services\ModuleService;
33use Illuminate\Support\Collection;
34
35use function view;
36
37/**
38 * Class IndividualFactsTabModule
39 */
40class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
41{
42    use ModuleTabTrait;
43
44    private ClipboardService $clipboard_service;
45
46    private IndividualFactsService $individual_facts_service;
47
48    private ModuleService $module_service;
49
50    /**
51     * @param ClipboardService       $clipboard_service
52     * @param IndividualFactsService $individual_facts_service
53     * @param ModuleService          $module_service
54     */
55    public function __construct(
56        ClipboardService $clipboard_service,
57        IndividualFactsService $individual_facts_service,
58        ModuleService $module_service
59    ) {
60        $this->clipboard_service        = $clipboard_service;
61        $this->individual_facts_service = $individual_facts_service;
62        $this->module_service           = $module_service;
63    }
64
65    /**
66     * How should this module be identified in the control panel, etc.?
67     *
68     * @return string
69     */
70    public function title(): string
71    {
72        /* I18N: Name of a module/tab on the individual page. */
73        return I18N::translate('Facts and events');
74    }
75
76    public function description(): string
77    {
78        /* I18N: Description of the “Facts and events” module */
79        return I18N::translate('A tab showing the facts and events of an individual.');
80    }
81
82    /**
83     * The default position for this tab.  It can be changed in the control panel.
84     *
85     * @return int
86     */
87    public function defaultTabOrder(): int
88    {
89        return 1;
90    }
91
92    /**
93     * A greyed out tab has no actual content, but may perhaps have
94     * options to create content.
95     *
96     * @param Individual $individual
97     *
98     * @return bool
99     */
100    public function isGrayedOut(Individual $individual): bool
101    {
102        return false;
103    }
104
105    /**
106     * Generate the HTML content of this tab.
107     *
108     * @param Individual $individual
109     *
110     * @return string
111     */
112    public function getTabContent(Individual $individual): string
113    {
114        // Which facts and events are handled by other modules?
115        $sidebar_facts = $this->module_service
116            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
117            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts())
118            ->flatten();
119
120        $tab_facts = $this->module_service
121            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
122            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts())
123            ->flatten();
124
125        // Don't show family meta-data tags
126        $exclude_facts  = new Collection(['FAM:CHAN', 'FAM:_UID']);
127        // Don't show tags that are shown in tabs or sidebars
128        $exclude_facts = $exclude_facts->merge($sidebar_facts)->merge($tab_facts);
129
130        $individual_facts = $this->individual_facts_service->individualFacts($individual, $exclude_facts);
131        $family_facts     = $this->individual_facts_service->familyFacts($individual, $exclude_facts);
132        $relative_facts   = $this->individual_facts_service->relativeFacts($individual);
133        $associate_facts  = $this->individual_facts_service->associateFacts($individual);
134        $historic_facts   = $this->individual_facts_service->historicFacts($individual);
135
136        $individual_facts = $individual_facts
137            ->merge($family_facts)
138            ->merge($relative_facts)
139            ->merge($associate_facts)
140            ->merge($historic_facts);
141
142        $individual_facts = Fact::sortFacts($individual_facts);
143
144        // Facts of relatives take the form 1 EVEN / 2 TYPE Event of Individual
145        // Ensure custom tags from there are recognised
146        Registry::elementFactory()->registerTags([
147            'INDI:EVEN:CEME'      => new CustomElement('Cemetery'),
148            'INDI:EVEN:_GODP'     => new CustomElement('Godparent'),
149            'INDI:EVEN:FAMC'      => new XrefFamily(I18N::translate('Adoptive parents')),
150            'INDI:EVEN:FAMC:ADOP' => new AdoptedByWhichParent(I18N::translate('Adoption')),
151        ]);
152
153        return view('modules/personal_facts/tab', [
154            'can_edit'            => $individual->canEdit(),
155            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
156            'has_associate_facts' => $associate_facts->isNotEmpty(),
157            'has_historic_facts'  => $historic_facts->isNotEmpty(),
158            'has_relative_facts'  => $relative_facts->isNotEmpty(),
159            'individual'          => $individual,
160            'facts'               => $individual_facts,
161        ]);
162    }
163
164    /**
165     * Is this tab empty? If so, we don't always need to display it.
166     *
167     * @param Individual $individual
168     *
169     * @return bool
170     */
171    public function hasTabContent(Individual $individual): bool
172    {
173        return true;
174    }
175
176    /**
177     * Can this tab load asynchronously?
178     *
179     * @return bool
180     */
181    public function canLoadAjax(): bool
182    {
183        return false;
184    }
185
186    /**
187     * This module handles the following facts - so don't show them on the "Facts and events" tab.
188     *
189     * @return Collection<int,string>
190     */
191    public function supportedFacts(): Collection
192    {
193        // We don't actually displaye these facts, but they are displayed
194        // outside the tabs/sidebar systems. This just forces them to be excluded here.
195        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
196    }
197}
198