xref: /webtrees/app/Module/SourcesTabModule.php (revision decb512f5fba376304ead41b41779062ef1dcffb)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Fact;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
252adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
268eaf8709SGreg Roachuse Illuminate\Support\Collection;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class SourcesTabModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass SourcesTabModule extends AbstractModule implements ModuleTabInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleTabTrait;
3449a243cbSGreg Roach
35*decb512fSGreg Roach    private ?Collection $facts = null;
368c2e8227SGreg Roach
3743f2f523SGreg Roach    private ClipboardService $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    /**
500cfd6963SGreg Roach     * How should this module be identified in the control panel, 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    {
78fb7a0427SGreg Roach        return 3;
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
813caaa4d2SGreg Roach    /**
823caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
833caaa4d2SGreg Roach     *
843caaa4d2SGreg Roach     * @param Individual $individual
853caaa4d2SGreg Roach     *
863caaa4d2SGreg Roach     * @return bool
873caaa4d2SGreg Roach     */
888f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
89c1010edaSGreg Roach    {
908af3e5c1SGreg Roach        return $individual->canEdit() || $this->getFactsWithSources($individual)->isNotEmpty();
918c2e8227SGreg Roach    }
928c2e8227SGreg Roach
933caaa4d2SGreg Roach    /**
943caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
953caaa4d2SGreg Roach     * options to create content.
963caaa4d2SGreg Roach     *
973caaa4d2SGreg Roach     * @param Individual $individual
983caaa4d2SGreg Roach     *
993caaa4d2SGreg Roach     * @return bool
1003caaa4d2SGreg Roach     */
1018f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
102c1010edaSGreg Roach    {
1038af3e5c1SGreg Roach        return $this->getFactsWithSources($individual)->isEmpty();
1048c2e8227SGreg Roach    }
1058c2e8227SGreg Roach
1063caaa4d2SGreg Roach    /**
1073caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1083caaa4d2SGreg Roach     *
1093caaa4d2SGreg Roach     * @param Individual $individual
1103caaa4d2SGreg Roach     *
1113caaa4d2SGreg Roach     * @return string
1123caaa4d2SGreg Roach     */
1139b34404bSGreg Roach    public function getTabContent(Individual $individual): string
114c1010edaSGreg Roach    {
115a8cd57e1SGreg Roach        return view('modules/sources_tab/tab', [
116225e381fSGreg Roach            'can_edit'        => $individual->canEdit(),
1172adcbd9aSGreg Roach            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
118225e381fSGreg Roach            'individual'      => $individual,
119225e381fSGreg Roach            'facts'           => $this->getFactsWithSources($individual),
120225e381fSGreg Roach        ]);
1218c2e8227SGreg Roach    }
1228c2e8227SGreg Roach
1238c2e8227SGreg Roach    /**
1248c2e8227SGreg Roach     * Get all the facts for an individual which contain sources.
1258c2e8227SGreg Roach     *
126225e381fSGreg Roach     * @param Individual $individual
127225e381fSGreg Roach     *
128b5c8fd7eSGreg Roach     * @return Collection<Fact>
1298c2e8227SGreg Roach     */
1308af3e5c1SGreg Roach    private function getFactsWithSources(Individual $individual): Collection
131c1010edaSGreg Roach    {
1328c2e8227SGreg Roach        if ($this->facts === null) {
13330158ae7SGreg Roach            $facts = $individual->facts();
1348af3e5c1SGreg Roach
13539ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
1368c2e8227SGreg Roach                if ($family->canShow()) {
13730158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
13839ca88baSGreg Roach                        $facts->push($fact);
1398c2e8227SGreg Roach                    }
1408c2e8227SGreg Roach                }
1418c2e8227SGreg Roach            }
1428af3e5c1SGreg Roach
1438af3e5c1SGreg Roach            $this->facts = new Collection();
1448af3e5c1SGreg Roach
1458c2e8227SGreg Roach            foreach ($facts as $fact) {
146138ca96cSGreg Roach                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
1478af3e5c1SGreg Roach                    $this->facts->push($fact);
1488c2e8227SGreg Roach                }
1498c2e8227SGreg Roach            }
1508af3e5c1SGreg Roach
1518af3e5c1SGreg Roach            $this->facts = Fact::sortFacts($this->facts);
1528c2e8227SGreg Roach        }
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach        return $this->facts;
1558c2e8227SGreg Roach    }
1568c2e8227SGreg Roach
1573caaa4d2SGreg Roach    /**
1583caaa4d2SGreg Roach     * Can this tab load asynchronously?
1593caaa4d2SGreg Roach     *
1603caaa4d2SGreg Roach     * @return bool
1613caaa4d2SGreg Roach     */
1628f53f488SRico Sonntag    public function canLoadAjax(): bool
163c1010edaSGreg Roach    {
16415d603e7SGreg Roach        return false;
1658c2e8227SGreg Roach    }
1668eaf8709SGreg Roach
1678eaf8709SGreg Roach    /**
1688eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1698eaf8709SGreg Roach     *
170b5c8fd7eSGreg Roach     * @return Collection<string>
1718eaf8709SGreg Roach     */
1728eaf8709SGreg Roach    public function supportedFacts(): Collection
1738eaf8709SGreg Roach    {
174d0889c63SGreg Roach        return new Collection(['INDI:SOUR', 'FACT:SOUR']);
1758eaf8709SGreg Roach    }
1768c2e8227SGreg Roach}
177