xref: /webtrees/app/Module/SourcesTabModule.php (revision da83637ca6236094f5a00d6e54530cd25ac7aa0e)
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()
33    {
34        return /* I18N: Name of a module */
35            I18N::translate('Sources');
36    }
37
38    /** {@inheritdoc} */
39    public function getDescription()
40    {
41        return /* I18N: Description of the “Sources” module */
42            I18N::translate('A tab showing the sources linked to an individual.');
43    }
44
45    /** {@inheritdoc} */
46    public function defaultTabOrder()
47    {
48        return 30;
49    }
50
51    /** {@inheritdoc} */
52    public function hasTabContent(Individual $individual)
53    {
54        return $individual->canedit() || $this->getFactsWithSources($individual);
55    }
56
57    /** {@inheritdoc} */
58    public function isGrayedOut(Individual $individual)
59    {
60        return !$this->getFactsWithSources($individual);
61    }
62
63    /** {@inheritdoc} */
64    public function getTabContent(Individual $individual)
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)
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()
105    {
106        return false;
107    }
108}
109