xref: /webtrees/app/Module/SourcesTabModule.php (revision 39c990acdae56e02eaa7ff99ee9fcf5395c13d20)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Fact;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Services\ClipboardService;
24use Illuminate\Support\Collection;
25
26/**
27 * Class SourcesTabModule
28 */
29class SourcesTabModule extends AbstractModule implements ModuleTabInterface
30{
31    use ModuleTabTrait;
32
33    /** @var Collection All facts belonging to this source. */
34    private $facts;
35
36    /** @var ClipboardService */
37    private $clipboard_service;
38
39    /**
40     * SourcesTabModule constructor.
41     *
42     * @param ClipboardService $clipboard_service
43     */
44    public function __construct(ClipboardService $clipboard_service)
45    {
46        $this->clipboard_service = $clipboard_service;
47    }
48
49    /**
50     * How should this module be identified in the control panel, etc.?
51     *
52     * @return string
53     */
54    public function title(): string
55    {
56        /* I18N: Name of a module */
57        return I18N::translate('Sources');
58    }
59
60    /**
61     * A sentence describing what this module does.
62     *
63     * @return string
64     */
65    public function description(): string
66    {
67        /* I18N: Description of the “Sources” module */
68        return I18N::translate('A tab showing the sources linked to an individual.');
69    }
70
71    /**
72     * The default position for this tab.  It can be changed in the control panel.
73     *
74     * @return int
75     */
76    public function defaultTabOrder(): int
77    {
78        return 3;
79    }
80
81    /** {@inheritdoc} */
82    public function hasTabContent(Individual $individual): bool
83    {
84        return $individual->canEdit() || $this->getFactsWithSources($individual)->isNotEmpty();
85    }
86
87    /** {@inheritdoc} */
88    public function isGrayedOut(Individual $individual): bool
89    {
90        return $this->getFactsWithSources($individual)->isEmpty();
91    }
92
93    /** {@inheritdoc} */
94    public function getTabContent(Individual $individual): string
95    {
96        return view('modules/sources_tab/tab', [
97            'can_edit'        => $individual->canEdit(),
98            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
99            'individual'      => $individual,
100            'facts'           => $this->getFactsWithSources($individual),
101        ]);
102    }
103
104    /**
105     * Get all the facts for an individual which contain sources.
106     *
107     * @param Individual $individual
108     *
109     * @return Collection
110     * @return Fact[]
111     */
112    private function getFactsWithSources(Individual $individual): Collection
113    {
114        if ($this->facts === null) {
115            $facts = $individual->facts();
116
117            foreach ($individual->spouseFamilies() as $family) {
118                if ($family->canShow()) {
119                    foreach ($family->facts() as $fact) {
120                        $facts->push($fact);
121                    }
122                }
123            }
124
125            $this->facts = new Collection();
126
127            foreach ($facts as $fact) {
128                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
129                    $this->facts->push($fact);
130                }
131            }
132
133            $this->facts = Fact::sortFacts($this->facts);
134        }
135
136        return $this->facts;
137    }
138
139    /** {@inheritdoc} */
140    public function canLoadAjax(): bool
141    {
142        return false;
143    }
144
145    /**
146     * This module handles the following facts - so don't show them on the "Facts and events" tab.
147     *
148     * @return Collection
149     * @return string[]
150     */
151    public function supportedFacts(): Collection
152    {
153        return new Collection(['SOUR']);
154    }
155}
156