xref: /webtrees/app/Module/SourcesTabModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
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\Fact;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Services\ClipboardService;
26use Illuminate\Support\Collection;
27
28/**
29 * Class SourcesTabModule
30 */
31class SourcesTabModule extends AbstractModule implements ModuleTabInterface
32{
33    use ModuleTabTrait;
34
35    /** @var Collection<array-key,Fact>|null  */
36    private Collection|null $facts = null;
37
38    private ClipboardService $clipboard_service;
39
40    /**
41     * @param ClipboardService $clipboard_service
42     */
43    public function __construct(ClipboardService $clipboard_service)
44    {
45        $this->clipboard_service = $clipboard_service;
46    }
47
48    /**
49     * How should this module be identified in the control panel, etc.?
50     *
51     * @return string
52     */
53    public function title(): string
54    {
55        /* I18N: Name of a module */
56        return I18N::translate('Sources');
57    }
58
59    public function description(): string
60    {
61        /* I18N: Description of the “Sources” module */
62        return I18N::translate('A tab showing the sources linked to an individual.');
63    }
64
65    /**
66     * The default position for this tab.  It can be changed in the control panel.
67     *
68     * @return int
69     */
70    public function defaultTabOrder(): int
71    {
72        return 3;
73    }
74
75    /**
76     * Is this tab empty? If so, we don't always need to display it.
77     *
78     * @param Individual $individual
79     *
80     * @return bool
81     */
82    public function hasTabContent(Individual $individual): bool
83    {
84        return $individual->canEdit() || $this->getFactsWithSources($individual)->isNotEmpty();
85    }
86
87    /**
88     * A greyed out tab has no actual content, but may perhaps have
89     * options to create content.
90     *
91     * @param Individual $individual
92     *
93     * @return bool
94     */
95    public function isGrayedOut(Individual $individual): bool
96    {
97        return $this->getFactsWithSources($individual)->isEmpty();
98    }
99
100    /**
101     * Generate the HTML content of this tab.
102     *
103     * @param Individual $individual
104     *
105     * @return string
106     */
107    public function getTabContent(Individual $individual): string
108    {
109        return view('modules/sources_tab/tab', [
110            'can_edit'        => $individual->canEdit(),
111            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
112            'individual'      => $individual,
113            'facts'           => $this->getFactsWithSources($individual),
114        ]);
115    }
116
117    /**
118     * Get all the facts for an individual which contain sources.
119     *
120     * @param Individual $individual
121     *
122     * @return Collection<int,Fact>
123     */
124    private function getFactsWithSources(Individual $individual): Collection
125    {
126        if ($this->facts === null) {
127            $facts = $individual->facts();
128
129            foreach ($individual->spouseFamilies() as $family) {
130                if ($family->canShow()) {
131                    foreach ($family->facts() as $fact) {
132                        $facts->push($fact);
133                    }
134                }
135            }
136
137            $this->facts = new Collection();
138
139            foreach ($facts as $fact) {
140                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
141                    $this->facts->push($fact);
142                }
143            }
144
145            $this->facts = Fact::sortFacts($this->facts);
146        }
147
148        return $this->facts;
149    }
150
151    /**
152     * Can this tab load asynchronously?
153     *
154     * @return bool
155     */
156    public function canLoadAjax(): bool
157    {
158        return false;
159    }
160
161    /**
162     * This module handles the following facts - so don't show them on the "Facts and events" tab.
163     *
164     * @return Collection<int,string>
165     */
166    public function supportedFacts(): Collection
167    {
168        return new Collection(['INDI:SOUR', 'FAM:SOUR']);
169    }
170}
171