xref: /webtrees/app/Module/IndividualFactsTabModule.php (revision 3e47a0f420824b2552fd05ceb5cab7e49a5838cf)
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(['INDI:EVEN:CEME' => new CustomElement('Cemetery')]);
150
151        return view('modules/personal_facts/tab', [
152            'can_edit'            => $individual->canEdit(),
153            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
154            'has_associate_facts' => $associate_facts->isNotEmpty(),
155            'has_historic_facts'  => $historic_facts->isNotEmpty(),
156            'has_relative_facts'  => $relative_facts->isNotEmpty(),
157            'individual'          => $individual,
158            'facts'               => $individual_facts,
159        ]);
160    }
161
162    /**
163     * Is this tab empty? If so, we don't always need to display it.
164     *
165     * @param Individual $individual
166     *
167     * @return bool
168     */
169    public function hasTabContent(Individual $individual): bool
170    {
171        return true;
172    }
173
174    /**
175     * Can this tab load asynchronously?
176     *
177     * @return bool
178     */
179    public function canLoadAjax(): bool
180    {
181        return false;
182    }
183
184    /**
185     * This module handles the following facts - so don't show them on the "Facts and events" tab.
186     *
187     * @return Collection<int,string>
188     */
189    public function supportedFacts(): Collection
190    {
191        // We don't actually displaye these facts, but they are displayed
192        // outside the tabs/sidebar systems. This just forces them to be excluded here.
193        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
194    }
195}
196