xref: /webtrees/app/Module/SourcesTabModule.php (revision fb7a0427c91eccd23224a17d3f2e5cd481309191)
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;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
232adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
248eaf8709SGreg Roachuse Illuminate\Support\Collection;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class SourcesTabModule
288c2e8227SGreg Roach */
2937eb8894SGreg Roachclass SourcesTabModule extends AbstractModule implements ModuleTabInterface
30c1010edaSGreg Roach{
3149a243cbSGreg Roach    use ModuleTabTrait;
3249a243cbSGreg Roach
3376692c8bSGreg Roach    /** @var Fact[] All facts belonging to this source. */
348c2e8227SGreg Roach    private $facts;
358c2e8227SGreg Roach
362adcbd9aSGreg Roach    /** @var ClipboardService */
372adcbd9aSGreg Roach    private $clipboard_service;
382adcbd9aSGreg Roach
392adcbd9aSGreg Roach    /**
402adcbd9aSGreg Roach     * SourcesTabModule constructor.
412adcbd9aSGreg Roach     *
422adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
432adcbd9aSGreg Roach     */
442adcbd9aSGreg Roach    public function __construct(ClipboardService $clipboard_service)
452adcbd9aSGreg Roach    {
462adcbd9aSGreg Roach        $this->clipboard_service = $clipboard_service;
472adcbd9aSGreg Roach    }
482adcbd9aSGreg Roach
49961ec755SGreg Roach    /**
50961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function title(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Name of a module */
57bbb76c12SGreg Roach        return I18N::translate('Sources');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
60961ec755SGreg Roach    /**
61961ec755SGreg Roach     * A sentence describing what this module does.
62961ec755SGreg Roach     *
63961ec755SGreg Roach     * @return string
64961ec755SGreg Roach     */
6549a243cbSGreg Roach    public function description(): string
66c1010edaSGreg Roach    {
67bbb76c12SGreg Roach        /* I18N: Description of the “Sources” module */
68bbb76c12SGreg Roach        return I18N::translate('A tab showing the sources linked to an individual.');
698c2e8227SGreg Roach    }
708c2e8227SGreg Roach
7149a243cbSGreg Roach    /**
7249a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
7349a243cbSGreg Roach     *
7449a243cbSGreg Roach     * @return int
7549a243cbSGreg Roach     */
76cbf4b7faSGreg Roach    public function defaultTabOrder(): int
77cbf4b7faSGreg Roach    {
78*fb7a0427SGreg Roach        return 3;
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
818c2e8227SGreg Roach    /** {@inheritdoc} */
828f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
83c1010edaSGreg Roach    {
8412c79f74SRico Sonntag        return $individual->canEdit() || $this->getFactsWithSources($individual);
858c2e8227SGreg Roach    }
868c2e8227SGreg Roach
878c2e8227SGreg Roach    /** {@inheritdoc} */
888f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
89c1010edaSGreg Roach    {
90225e381fSGreg Roach        return !$this->getFactsWithSources($individual);
918c2e8227SGreg Roach    }
928c2e8227SGreg Roach
938c2e8227SGreg Roach    /** {@inheritdoc} */
949b34404bSGreg Roach    public function getTabContent(Individual $individual): string
95c1010edaSGreg Roach    {
96a8cd57e1SGreg Roach        return view('modules/sources_tab/tab', [
97225e381fSGreg Roach            'can_edit'        => $individual->canEdit(),
982adcbd9aSGreg Roach            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
99225e381fSGreg Roach            'individual'      => $individual,
100225e381fSGreg Roach            'facts'           => $this->getFactsWithSources($individual),
101225e381fSGreg Roach        ]);
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach    /**
1058c2e8227SGreg Roach     * Get all the facts for an individual which contain sources.
1068c2e8227SGreg Roach     *
107225e381fSGreg Roach     * @param Individual $individual
108225e381fSGreg Roach     *
1098c2e8227SGreg Roach     * @return Fact[]
1108c2e8227SGreg Roach     */
1118f53f488SRico Sonntag    private function getFactsWithSources(Individual $individual): array
112c1010edaSGreg Roach    {
1138c2e8227SGreg Roach        if ($this->facts === null) {
11430158ae7SGreg Roach            $facts = $individual->facts();
11539ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
1168c2e8227SGreg Roach                if ($family->canShow()) {
11730158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
11839ca88baSGreg Roach                        $facts->push($fact);
1198c2e8227SGreg Roach                    }
1208c2e8227SGreg Roach                }
1218c2e8227SGreg Roach            }
12213abd6f3SGreg Roach            $this->facts = [];
1238c2e8227SGreg Roach            foreach ($facts as $fact) {
124138ca96cSGreg Roach                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
1258c2e8227SGreg Roach                    $this->facts[] = $fact;
1268c2e8227SGreg Roach                }
1278c2e8227SGreg Roach            }
1281c7a6874SGreg Roach            $this->facts = Fact::sortFacts($this->facts)->all();
1298c2e8227SGreg Roach        }
1308c2e8227SGreg Roach
1318c2e8227SGreg Roach        return $this->facts;
1328c2e8227SGreg Roach    }
1338c2e8227SGreg Roach
1348c2e8227SGreg Roach    /** {@inheritdoc} */
1358f53f488SRico Sonntag    public function canLoadAjax(): bool
136c1010edaSGreg Roach    {
13715d603e7SGreg Roach        return false;
1388c2e8227SGreg Roach    }
1398eaf8709SGreg Roach
1408eaf8709SGreg Roach    /**
1418eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1428eaf8709SGreg Roach     *
1438eaf8709SGreg Roach     * @return Collection|string[]
1448eaf8709SGreg Roach     */
1458eaf8709SGreg Roach    public function supportedFacts(): Collection
1468eaf8709SGreg Roach    {
1478eaf8709SGreg Roach        return new Collection(['SOUR']);
1488eaf8709SGreg Roach    }
1498c2e8227SGreg Roach}
150