xref: /webtrees/app/Module/SourcesTabModule.php (revision 4947e7089adb08f24bf1eef67994a106a8df60aa)
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\Functions\Functions;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24
25/**
26 * Class SourcesTabModule
27 */
28class SourcesTabModule extends AbstractModule implements ModuleTabInterface
29{
30    use ModuleTabTrait;
31
32    /** @var Fact[] All facts belonging to this source. */
33    private $facts;
34
35    /**
36     * How should this module be labelled on tabs, menus, etc.?
37     *
38     * @return string
39     */
40    public function title(): string
41    {
42        /* I18N: Name of a module */
43        return I18N::translate('Sources');
44    }
45
46    /**
47     * A sentence describing what this module does.
48     *
49     * @return string
50     */
51    public function description(): string
52    {
53        /* I18N: Description of the “Sources” module */
54        return I18N::translate('A tab showing the sources linked to an individual.');
55    }
56
57    /**
58     * The default position for this tab.  It can be changed in the control panel.
59     *
60     * @return int
61     */
62    public function defaultTabOrder(): int
63    {
64        return 4;
65    }
66
67    /** {@inheritdoc} */
68    public function hasTabContent(Individual $individual): bool
69    {
70        return $individual->canEdit() || $this->getFactsWithSources($individual);
71    }
72
73    /** {@inheritdoc} */
74    public function isGrayedOut(Individual $individual): bool
75    {
76        return !$this->getFactsWithSources($individual);
77    }
78
79    /** {@inheritdoc} */
80    public function getTabContent(Individual $individual): string
81    {
82        return view('modules/sources_tab/tab', [
83            'can_edit'   => $individual->canEdit(),
84            'individual' => $individual,
85            'facts'      => $this->getFactsWithSources($individual),
86        ]);
87    }
88
89    /**
90     * Get all the facts for an individual which contain sources.
91     *
92     * @param Individual $individual
93     *
94     * @return Fact[]
95     */
96    private function getFactsWithSources(Individual $individual): array
97    {
98        if ($this->facts === null) {
99            $facts = $individual->facts();
100            foreach ($individual->getSpouseFamilies() as $family) {
101                if ($family->canShow()) {
102                    foreach ($family->facts() as $fact) {
103                        $facts[] = $fact;
104                    }
105                }
106            }
107            $this->facts = [];
108            foreach ($facts as $fact) {
109                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
110                    $this->facts[] = $fact;
111                }
112            }
113            Functions::sortFacts($this->facts);
114        }
115
116        return $this->facts;
117    }
118
119    /** {@inheritdoc} */
120    public function canLoadAjax(): bool
121    {
122        return false;
123    }
124}
125