xref: /webtrees/app/Module/SourcesTabModule.php (revision 353b36ab5fd4f6ba09f89e2865edee7108bdf7c0)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class SourcesTabModule
278c2e8227SGreg Roach */
2849a243cbSGreg Roachclass SourcesTabModule extends AbstractModule implements ModuleInterface, ModuleTabInterface
29c1010edaSGreg Roach{
3049a243cbSGreg Roach    use ModuleTabTrait;
3149a243cbSGreg Roach
3276692c8bSGreg Roach    /** @var Fact[] All facts belonging to this source. */
338c2e8227SGreg Roach    private $facts;
348c2e8227SGreg Roach
35961ec755SGreg Roach    /**
36961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
37961ec755SGreg Roach     *
38961ec755SGreg Roach     * @return string
39961ec755SGreg Roach     */
4049a243cbSGreg Roach    public function title(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('Sources');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
46961ec755SGreg Roach    /**
47961ec755SGreg Roach     * A sentence describing what this module does.
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function description(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Sources” module */
54bbb76c12SGreg Roach        return I18N::translate('A tab showing the sources linked to an individual.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5749a243cbSGreg Roach    /**
5849a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
5949a243cbSGreg Roach     *
6049a243cbSGreg Roach     * @return int
6149a243cbSGreg Roach     */
62cbf4b7faSGreg Roach    public function defaultTabOrder(): int
63cbf4b7faSGreg Roach    {
64*353b36abSGreg Roach        return 4;
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
678c2e8227SGreg Roach    /** {@inheritdoc} */
688f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
69c1010edaSGreg Roach    {
7012c79f74SRico Sonntag        return $individual->canEdit() || $this->getFactsWithSources($individual);
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
738c2e8227SGreg Roach    /** {@inheritdoc} */
748f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
75c1010edaSGreg Roach    {
76225e381fSGreg Roach        return !$this->getFactsWithSources($individual);
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
798c2e8227SGreg Roach    /** {@inheritdoc} */
809b34404bSGreg Roach    public function getTabContent(Individual $individual): string
81c1010edaSGreg Roach    {
82a8cd57e1SGreg Roach        return view('modules/sources_tab/tab', [
83225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
84225e381fSGreg Roach            'individual' => $individual,
85225e381fSGreg Roach            'facts'      => $this->getFactsWithSources($individual),
86225e381fSGreg Roach        ]);
878c2e8227SGreg Roach    }
888c2e8227SGreg Roach
898c2e8227SGreg Roach    /**
908c2e8227SGreg Roach     * Get all the facts for an individual which contain sources.
918c2e8227SGreg Roach     *
92225e381fSGreg Roach     * @param Individual $individual
93225e381fSGreg Roach     *
948c2e8227SGreg Roach     * @return Fact[]
958c2e8227SGreg Roach     */
968f53f488SRico Sonntag    private function getFactsWithSources(Individual $individual): array
97c1010edaSGreg Roach    {
988c2e8227SGreg Roach        if ($this->facts === null) {
9930158ae7SGreg Roach            $facts = $individual->facts();
100225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
1018c2e8227SGreg Roach                if ($family->canShow()) {
10230158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
1038c2e8227SGreg Roach                        $facts[] = $fact;
1048c2e8227SGreg Roach                    }
1058c2e8227SGreg Roach                }
1068c2e8227SGreg Roach            }
10713abd6f3SGreg Roach            $this->facts = [];
1088c2e8227SGreg Roach            foreach ($facts as $fact) {
109138ca96cSGreg Roach                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
1108c2e8227SGreg Roach                    $this->facts[] = $fact;
1118c2e8227SGreg Roach                }
1128c2e8227SGreg Roach            }
1133d7a8a4cSGreg Roach            Functions::sortFacts($this->facts);
1148c2e8227SGreg Roach        }
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach        return $this->facts;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach    /** {@inheritdoc} */
1208f53f488SRico Sonntag    public function canLoadAjax(): bool
121c1010edaSGreg Roach    {
12215d603e7SGreg Roach        return false;
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach}
125