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