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