xref: /webtrees/app/Module/SourcesTabModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
193d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
21225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
228c2e8227SGreg Roach
238c2e8227SGreg Roach/**
248c2e8227SGreg Roach * Class SourcesTabModule
258c2e8227SGreg Roach */
26c1010edaSGreg Roachclass SourcesTabModule extends AbstractModule implements ModuleTabInterface
27c1010edaSGreg Roach{
2876692c8bSGreg Roach    /** @var Fact[] All facts belonging to this source. */
298c2e8227SGreg Roach    private $facts;
308c2e8227SGreg Roach
318c2e8227SGreg Roach    /** {@inheritdoc} */
32*8f53f488SRico Sonntag    public function getTitle(): string
33c1010edaSGreg Roach    {
34bbb76c12SGreg Roach        /* I18N: Name of a module */
35bbb76c12SGreg Roach        return I18N::translate('Sources');
368c2e8227SGreg Roach    }
378c2e8227SGreg Roach
388c2e8227SGreg Roach    /** {@inheritdoc} */
39*8f53f488SRico Sonntag    public function getDescription(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Description of the “Sources” module */
42bbb76c12SGreg Roach        return I18N::translate('A tab showing the sources linked to an individual.');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
458c2e8227SGreg Roach    /** {@inheritdoc} */
46*8f53f488SRico Sonntag    public function defaultTabOrder(): int
47c1010edaSGreg Roach    {
488c2e8227SGreg Roach        return 30;
498c2e8227SGreg Roach    }
508c2e8227SGreg Roach
518c2e8227SGreg Roach    /** {@inheritdoc} */
52*8f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
53c1010edaSGreg Roach    {
5412c79f74SRico Sonntag        return $individual->canEdit() || $this->getFactsWithSources($individual);
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
578c2e8227SGreg Roach    /** {@inheritdoc} */
58*8f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
59c1010edaSGreg Roach    {
60225e381fSGreg Roach        return !$this->getFactsWithSources($individual);
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
638c2e8227SGreg Roach    /** {@inheritdoc} */
64c1010edaSGreg Roach    public function getTabContent(Individual $individual)
65c1010edaSGreg Roach    {
66a8cd57e1SGreg Roach        return view('modules/sources_tab/tab', [
67225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
68225e381fSGreg Roach            'individual' => $individual,
69225e381fSGreg Roach            'facts'      => $this->getFactsWithSources($individual),
70225e381fSGreg Roach        ]);
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
738c2e8227SGreg Roach    /**
748c2e8227SGreg Roach     * Get all the facts for an individual which contain sources.
758c2e8227SGreg Roach     *
76225e381fSGreg Roach     * @param Individual $individual
77225e381fSGreg Roach     *
788c2e8227SGreg Roach     * @return Fact[]
798c2e8227SGreg Roach     */
80*8f53f488SRico Sonntag    private function getFactsWithSources(Individual $individual): array
81c1010edaSGreg Roach    {
828c2e8227SGreg Roach        if ($this->facts === null) {
83225e381fSGreg Roach            $facts = $individual->getFacts();
84225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
858c2e8227SGreg Roach                if ($family->canShow()) {
868c2e8227SGreg Roach                    foreach ($family->getFacts() as $fact) {
878c2e8227SGreg Roach                        $facts[] = $fact;
888c2e8227SGreg Roach                    }
898c2e8227SGreg Roach                }
908c2e8227SGreg Roach            }
9113abd6f3SGreg Roach            $this->facts = [];
928c2e8227SGreg Roach            foreach ($facts as $fact) {
938c2e8227SGreg Roach                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
948c2e8227SGreg Roach                    $this->facts[] = $fact;
958c2e8227SGreg Roach                }
968c2e8227SGreg Roach            }
973d7a8a4cSGreg Roach            Functions::sortFacts($this->facts);
988c2e8227SGreg Roach        }
998c2e8227SGreg Roach
1008c2e8227SGreg Roach        return $this->facts;
1018c2e8227SGreg Roach    }
1028c2e8227SGreg Roach
1038c2e8227SGreg Roach    /** {@inheritdoc} */
104*8f53f488SRico Sonntag    public function canLoadAjax(): bool
105c1010edaSGreg Roach    {
10615d603e7SGreg Roach        return false;
1078c2e8227SGreg Roach    }
1088c2e8227SGreg Roach}
109