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