xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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    /**
77     * A sentence describing what this module does.
78     *
79     * @return string
80     */
81    public function description(): string
82    {
83        /* I18N: Description of the “Facts and events” module */
84        return I18N::translate('A tab showing the facts and events of an individual.');
85    }
86
87    /**
88     * The default position for this tab.  It can be changed in the control panel.
89     *
90     * @return int
91     */
92    public function defaultTabOrder(): int
93    {
94        return 1;
95    }
96
97    /**
98     * A greyed out tab has no actual content, but may perhaps have
99     * options to create content.
100     *
101     * @param Individual $individual
102     *
103     * @return bool
104     */
105    public function isGrayedOut(Individual $individual): bool
106    {
107        return false;
108    }
109
110    /**
111     * Generate the HTML content of this tab.
112     *
113     * @param Individual $individual
114     *
115     * @return string
116     */
117    public function getTabContent(Individual $individual): string
118    {
119        // Which facts and events are handled by other modules?
120        $sidebar_facts = $this->module_service
121            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
122            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts())
123            ->flatten();
124
125        $tab_facts = $this->module_service
126            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
127            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts())
128            ->flatten();
129
130        // Don't show family meta-data tags
131        $exclude_facts  = new Collection(['FAM:CHAN', 'FAM:_UID']);
132        // Don't show tags that are shown in tabs or sidebars
133        $exclude_facts = $exclude_facts->merge($sidebar_facts)->merge($tab_facts);
134
135        $individual_facts = $this->individual_facts_service->individualFacts($individual, $exclude_facts);
136        $family_facts     = $this->individual_facts_service->familyFacts($individual, $exclude_facts);
137        $relative_facts   = $this->individual_facts_service->relativeFacts($individual);
138        $associate_facts  = $this->individual_facts_service->associateFacts($individual);
139        $historic_facts   = $this->individual_facts_service->historicFacts($individual);
140
141        $individual_facts = $individual_facts
142            ->merge($family_facts)
143            ->merge($relative_facts)
144            ->merge($associate_facts)
145            ->merge($historic_facts);
146
147        $individual_facts = Fact::sortFacts($individual_facts);
148
149        // Facts of relatives take the form 1 EVEN / 2 TYPE Event of Individual
150        // Ensure custom tags from there are recognised
151        Registry::elementFactory()->registerTags([
152            'INDI:EVEN:CEME'      => new CustomElement('Cemetery'),
153            'INDI:EVEN:_GODP'     => new CustomElement('Godparent'),
154            'INDI:EVEN:FAMC'      => new XrefFamily(I18N::translate('Adoptive parents')),
155            'INDI:EVEN:FAMC:ADOP' => new AdoptedByWhichParent(I18N::translate('Adoption')),
156        ]);
157
158        return view('modules/personal_facts/tab', [
159            'can_edit'            => $individual->canEdit(),
160            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
161            'has_associate_facts' => $associate_facts->isNotEmpty(),
162            'has_historic_facts'  => $historic_facts->isNotEmpty(),
163            'has_relative_facts'  => $relative_facts->isNotEmpty(),
164            'individual'          => $individual,
165            'facts'               => $individual_facts,
166        ]);
167    }
168
169    /**
170     * Is this tab empty? If so, we don't always need to display it.
171     *
172     * @param Individual $individual
173     *
174     * @return bool
175     */
176    public function hasTabContent(Individual $individual): bool
177    {
178        return true;
179    }
180
181    /**
182     * Can this tab load asynchronously?
183     *
184     * @return bool
185     */
186    public function canLoadAjax(): bool
187    {
188        return false;
189    }
190
191    /**
192     * This module handles the following facts - so don't show them on the "Facts and events" tab.
193     *
194     * @return Collection<int,string>
195     */
196    public function supportedFacts(): Collection
197    {
198        // We don't actually displaye these facts, but they are displayed
199        // outside the tabs/sidebar systems. This just forces them to be excluded here.
200        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
201    }
202}
203