xref: /webtrees/app/Module/SourcesTabModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
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    /** @var Fact[] All facts belonging to this source. */
31    private $facts;
32
33    /** {@inheritdoc} */
34    public function getTitle(): string
35    {
36        /* I18N: Name of a module */
37        return I18N::translate('Sources');
38    }
39
40    /** {@inheritdoc} */
41    public function getDescription(): string
42    {
43        /* I18N: Description of the “Sources” module */
44        return I18N::translate('A tab showing the sources linked to an individual.');
45    }
46
47    /** {@inheritdoc} */
48    public function defaultTabOrder(): int
49    {
50        return 30;
51    }
52
53    /** {@inheritdoc} */
54    public function hasTabContent(Individual $individual): bool
55    {
56        return $individual->canEdit() || $this->getFactsWithSources($individual);
57    }
58
59    /** {@inheritdoc} */
60    public function isGrayedOut(Individual $individual): bool
61    {
62        return !$this->getFactsWithSources($individual);
63    }
64
65    /** {@inheritdoc} */
66    public function getTabContent(Individual $individual): string
67    {
68        return view('modules/sources_tab/tab', [
69            'can_edit'   => $individual->canEdit(),
70            'individual' => $individual,
71            'facts'      => $this->getFactsWithSources($individual),
72        ]);
73    }
74
75    /**
76     * Get all the facts for an individual which contain sources.
77     *
78     * @param Individual $individual
79     *
80     * @return Fact[]
81     */
82    private function getFactsWithSources(Individual $individual): array
83    {
84        if ($this->facts === null) {
85            $facts = $individual->facts();
86            foreach ($individual->getSpouseFamilies() as $family) {
87                if ($family->canShow()) {
88                    foreach ($family->facts() as $fact) {
89                        $facts[] = $fact;
90                    }
91                }
92            }
93            $this->facts = [];
94            foreach ($facts as $fact) {
95                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
96                    $this->facts[] = $fact;
97                }
98            }
99            Functions::sortFacts($this->facts);
100        }
101
102        return $this->facts;
103    }
104
105    /** {@inheritdoc} */
106    public function canLoadAjax(): bool
107    {
108        return false;
109    }
110}
111