xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision ac37dc02116fd061512638bbd050bfd4bd8a10ce)
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\CustomElement;
24use Fisharebest\Webtrees\Fact;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\ClipboardService;
29use Fisharebest\Webtrees\Services\IndividualFactsService;
30use Fisharebest\Webtrees\Services\ModuleService;
31use Illuminate\Support\Collection;
32
33use function view;
34
35/**
36 * Class IndividualFactsTabModule
37 */
38class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
39{
40    use ModuleTabTrait;
41
42    private ClipboardService $clipboard_service;
43
44    private IndividualFactsService $individual_facts_service;
45
46    private ModuleService $module_service;
47
48    /**
49     * @param ClipboardService       $clipboard_service
50     * @param IndividualFactsService $individual_facts_service
51     * @param ModuleService          $module_service
52     */
53    public function __construct(
54        ClipboardService $clipboard_service,
55        IndividualFactsService $individual_facts_service,
56        ModuleService $module_service
57    ) {
58        $this->clipboard_service        = $clipboard_service;
59        $this->individual_facts_service = $individual_facts_service;
60        $this->module_service           = $module_service;
61    }
62
63    /**
64     * How should this module be identified in the control panel, etc.?
65     *
66     * @return string
67     */
68    public function title(): string
69    {
70        /* I18N: Name of a module/tab on the individual page. */
71        return I18N::translate('Facts and events');
72    }
73
74    /**
75     * A sentence describing what this module does.
76     *
77     * @return string
78     */
79    public function description(): string
80    {
81        /* I18N: Description of the “Facts and events” module */
82        return I18N::translate('A tab showing the facts and events of an individual.');
83    }
84
85    /**
86     * The default position for this tab.  It can be changed in the control panel.
87     *
88     * @return int
89     */
90    public function defaultTabOrder(): int
91    {
92        return 1;
93    }
94
95    /**
96     * A greyed out tab has no actual content, but may perhaps have
97     * options to create content.
98     *
99     * @param Individual $individual
100     *
101     * @return bool
102     */
103    public function isGrayedOut(Individual $individual): bool
104    {
105        return false;
106    }
107
108    /**
109     * Generate the HTML content of this tab.
110     *
111     * @param Individual $individual
112     *
113     * @return string
114     */
115    public function getTabContent(Individual $individual): string
116    {
117        // Which facts and events are handled by other modules?
118        $sidebar_facts = $this->module_service
119            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
120            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts())
121            ->flatten();
122
123        $tab_facts = $this->module_service
124            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
125            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts())
126            ->flatten();
127
128        // Don't show family meta-data tags
129        $exclude_facts  = new Collection(['FAM:CHAN', 'FAM:_UID']);
130        // Don't show tags that are shown in tabs or sidebars
131        $exclude_facts = $exclude_facts->merge($sidebar_facts)->merge($tab_facts);
132
133        $individual_facts = $this->individual_facts_service->individualFacts($individual, $exclude_facts);
134        $family_facts     = $this->individual_facts_service->familyFacts($individual, $exclude_facts);
135        $relative_facts   = $this->individual_facts_service->relativeFacts($individual);
136        $associate_facts  = $this->individual_facts_service->associateFacts($individual);
137        $historic_facts   = $this->individual_facts_service->historicFacts($individual);
138
139        $individual_facts = $individual_facts
140            ->merge($family_facts)
141            ->merge($relative_facts)
142            ->merge($associate_facts)
143            ->merge($historic_facts);
144
145        $individual_facts = Fact::sortFacts($individual_facts);
146
147        // Facts of relatives take the form 1 EVEN / 2 TYPE Event of Individual
148        // Ensure custom tags from there are recognised
149        Registry::elementFactory()->registerTags([
150            'INDI:EVEN:CEME'  => new CustomElement('Cemetery'),
151            'INDI:EVEN:_GODP' => new CustomElement('Godparent'),
152        ]);
153
154        return view('modules/personal_facts/tab', [
155            'can_edit'            => $individual->canEdit(),
156            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
157            'has_associate_facts' => $associate_facts->isNotEmpty(),
158            'has_historic_facts'  => $historic_facts->isNotEmpty(),
159            'has_relative_facts'  => $relative_facts->isNotEmpty(),
160            'individual'          => $individual,
161            'facts'               => $individual_facts,
162        ]);
163    }
164
165    /**
166     * Is this tab empty? If so, we don't always need to display it.
167     *
168     * @param Individual $individual
169     *
170     * @return bool
171     */
172    public function hasTabContent(Individual $individual): bool
173    {
174        return true;
175    }
176
177    /**
178     * Can this tab load asynchronously?
179     *
180     * @return bool
181     */
182    public function canLoadAjax(): bool
183    {
184        return false;
185    }
186
187    /**
188     * This module handles the following facts - so don't show them on the "Facts and events" tab.
189     *
190     * @return Collection<int,string>
191     */
192    public function supportedFacts(): Collection
193    {
194        // We don't actually displaye these facts, but they are displayed
195        // outside the tabs/sidebar systems. This just forces them to be excluded here.
196        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
197    }
198}
199