xref: /webtrees/app/Module/SourcesTabModule.php (revision 138ca96c29e1b115c3b96889a80267d29566502e)
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 */
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 */
28c1010edaSGreg Roachclass SourcesTabModule extends AbstractModule implements ModuleTabInterface
29c1010edaSGreg Roach{
3076692c8bSGreg Roach    /** @var Fact[] All facts belonging to this source. */
318c2e8227SGreg Roach    private $facts;
328c2e8227SGreg Roach
338c2e8227SGreg Roach    /** {@inheritdoc} */
348f53f488SRico Sonntag    public function getTitle(): string
35c1010edaSGreg Roach    {
36bbb76c12SGreg Roach        /* I18N: Name of a module */
37bbb76c12SGreg Roach        return I18N::translate('Sources');
388c2e8227SGreg Roach    }
398c2e8227SGreg Roach
408c2e8227SGreg Roach    /** {@inheritdoc} */
418f53f488SRico Sonntag    public function getDescription(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Description of the “Sources” module */
44bbb76c12SGreg Roach        return I18N::translate('A tab showing the sources linked to an individual.');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
478c2e8227SGreg Roach    /** {@inheritdoc} */
488f53f488SRico Sonntag    public function defaultTabOrder(): int
49c1010edaSGreg Roach    {
508c2e8227SGreg Roach        return 30;
518c2e8227SGreg Roach    }
528c2e8227SGreg Roach
538c2e8227SGreg Roach    /** {@inheritdoc} */
548f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
55c1010edaSGreg Roach    {
5612c79f74SRico Sonntag        return $individual->canEdit() || $this->getFactsWithSources($individual);
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
598c2e8227SGreg Roach    /** {@inheritdoc} */
608f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
61c1010edaSGreg Roach    {
62225e381fSGreg Roach        return !$this->getFactsWithSources($individual);
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
658c2e8227SGreg Roach    /** {@inheritdoc} */
669b34404bSGreg Roach    public function getTabContent(Individual $individual): string
67c1010edaSGreg Roach    {
68a8cd57e1SGreg Roach        return view('modules/sources_tab/tab', [
69225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
70225e381fSGreg Roach            'individual' => $individual,
71225e381fSGreg Roach            'facts'      => $this->getFactsWithSources($individual),
72225e381fSGreg Roach        ]);
738c2e8227SGreg Roach    }
748c2e8227SGreg Roach
758c2e8227SGreg Roach    /**
768c2e8227SGreg Roach     * Get all the facts for an individual which contain sources.
778c2e8227SGreg Roach     *
78225e381fSGreg Roach     * @param Individual $individual
79225e381fSGreg Roach     *
808c2e8227SGreg Roach     * @return Fact[]
818c2e8227SGreg Roach     */
828f53f488SRico Sonntag    private function getFactsWithSources(Individual $individual): array
83c1010edaSGreg Roach    {
848c2e8227SGreg Roach        if ($this->facts === null) {
85225e381fSGreg Roach            $facts = $individual->getFacts();
86225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
878c2e8227SGreg Roach                if ($family->canShow()) {
888c2e8227SGreg Roach                    foreach ($family->getFacts() as $fact) {
898c2e8227SGreg Roach                        $facts[] = $fact;
908c2e8227SGreg Roach                    }
918c2e8227SGreg Roach                }
928c2e8227SGreg Roach            }
9313abd6f3SGreg Roach            $this->facts = [];
948c2e8227SGreg Roach            foreach ($facts as $fact) {
95*138ca96cSGreg Roach                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
968c2e8227SGreg Roach                    $this->facts[] = $fact;
978c2e8227SGreg Roach                }
988c2e8227SGreg Roach            }
993d7a8a4cSGreg Roach            Functions::sortFacts($this->facts);
1008c2e8227SGreg Roach        }
1018c2e8227SGreg Roach
1028c2e8227SGreg Roach        return $this->facts;
1038c2e8227SGreg Roach    }
1048c2e8227SGreg Roach
1058c2e8227SGreg Roach    /** {@inheritdoc} */
1068f53f488SRico Sonntag    public function canLoadAjax(): bool
107c1010edaSGreg Roach    {
10815d603e7SGreg Roach        return false;
1098c2e8227SGreg Roach    }
1108c2e8227SGreg Roach}
111