xref: /webtrees/app/Module/MediaTabModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
238d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
25225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
26f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry;
272adcbd9aSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
288eaf8709SGreg Roachuse Illuminate\Support\Collection;
298c2e8227SGreg Roach
30f4cd70d3SGreg Roachuse function preg_match;
31f4cd70d3SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class MediaTabModule
348c2e8227SGreg Roach */
3537eb8894SGreg Roachclass MediaTabModule extends AbstractModule implements ModuleTabInterface
36c1010edaSGreg Roach{
3749a243cbSGreg Roach    use ModuleTabTrait;
3849a243cbSGreg Roach
3943f2f523SGreg Roach    private ClipboardService $clipboard_service;
402adcbd9aSGreg Roach
412adcbd9aSGreg Roach    /**
422adcbd9aSGreg Roach     * @param ClipboardService $clipboard_service
432adcbd9aSGreg Roach     */
44406c2a36SGreg Roach    public function __construct(ClipboardService $clipboard_service)
45406c2a36SGreg 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('Media');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “Media” module */
63bbb76c12SGreg Roach        return I18N::translate('A tab showing the media objects linked to an individual.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6649a243cbSGreg Roach    /**
6749a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6849a243cbSGreg Roach     *
6949a243cbSGreg Roach     * @return int
7049a243cbSGreg Roach     */
71cbf4b7faSGreg Roach    public function defaultTabOrder(): int
72cbf4b7faSGreg Roach    {
73fb7a0427SGreg Roach        return 5;
748c2e8227SGreg Roach    }
758c2e8227SGreg Roach
763caaa4d2SGreg Roach    /**
773caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
783caaa4d2SGreg Roach     *
793caaa4d2SGreg Roach     * @param Individual $individual
803caaa4d2SGreg Roach     *
813caaa4d2SGreg Roach     * @return bool
823caaa4d2SGreg Roach     */
838f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
84c1010edaSGreg Roach    {
858af3e5c1SGreg Roach        return $individual->canEdit() || $this->getFactsWithMedia($individual)->isNotEmpty();
868c2e8227SGreg Roach    }
878c2e8227SGreg Roach
883caaa4d2SGreg Roach    /**
893caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
903caaa4d2SGreg Roach     * options to create content.
913caaa4d2SGreg Roach     *
923caaa4d2SGreg Roach     * @param Individual $individual
933caaa4d2SGreg Roach     *
943caaa4d2SGreg Roach     * @return bool
953caaa4d2SGreg Roach     */
968f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
97c1010edaSGreg Roach    {
982078aa78SGreg Roach        return $this->getFactsWithMedia($individual)->isEmpty();
998c2e8227SGreg Roach    }
1008c2e8227SGreg Roach
1013caaa4d2SGreg Roach    /**
1023caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1033caaa4d2SGreg Roach     *
1043caaa4d2SGreg Roach     * @param Individual $individual
1053caaa4d2SGreg Roach     *
1063caaa4d2SGreg Roach     * @return string
1073caaa4d2SGreg Roach     */
1089b34404bSGreg Roach    public function getTabContent(Individual $individual): string
109c1010edaSGreg Roach    {
110a8cd57e1SGreg Roach        return view('modules/media/tab', [
111225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
1122adcbd9aSGreg Roach            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
113225e381fSGreg Roach            'individual' => $individual,
114225e381fSGreg Roach            'facts'      => $this->getFactsWithMedia($individual),
115225e381fSGreg Roach        ]);
1168c2e8227SGreg Roach    }
1178c2e8227SGreg Roach
1188c2e8227SGreg Roach    /**
1198c2e8227SGreg Roach     * Get all the facts for an individual which contain media objects.
1208c2e8227SGreg Roach     *
121225e381fSGreg Roach     * @param Individual $individual
122225e381fSGreg Roach     *
12336779af1SGreg Roach     * @return Collection<int,Fact>
1248c2e8227SGreg Roach     */
125f4cd70d3SGreg Roach    protected function getFactsWithMedia(Individual $individual): Collection
126c1010edaSGreg Roach    {
1279991924fSGreg Roach        return Registry::cache()->array()->remember(self::class . ':' . __METHOD__, static function () use ($individual): Collection {
12830158ae7SGreg Roach            $facts = $individual->facts();
129f4cd70d3SGreg Roach
13039ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
1318c2e8227SGreg Roach                if ($family->canShow()) {
132f4cd70d3SGreg Roach                    $facts = $facts->concat($family->facts());
1338c2e8227SGreg Roach                }
1348c2e8227SGreg Roach            }
1358af3e5c1SGreg Roach
136*f25fc0f9SGreg Roach            $facts = $facts->filter(static fn (Fact $fact): bool => preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom()) === 1);
1378af3e5c1SGreg Roach
138f4cd70d3SGreg Roach            return Fact::sortFacts($facts);
139f4cd70d3SGreg Roach        });
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1423caaa4d2SGreg Roach    /**
1433caaa4d2SGreg Roach     * Can this tab load asynchronously?
1443caaa4d2SGreg Roach     *
1453caaa4d2SGreg Roach     * @return bool
1463caaa4d2SGreg Roach     */
1478f53f488SRico Sonntag    public function canLoadAjax(): bool
148c1010edaSGreg Roach    {
14915d603e7SGreg Roach        return false;
1508c2e8227SGreg Roach    }
1518eaf8709SGreg Roach
1528eaf8709SGreg Roach    /**
1538eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1548eaf8709SGreg Roach     *
15536779af1SGreg Roach     * @return Collection<int,string>
1568eaf8709SGreg Roach     */
1578eaf8709SGreg Roach    public function supportedFacts(): Collection
1588eaf8709SGreg Roach    {
159d0889c63SGreg Roach        return new Collection(['INDI:OBJE', 'FAM:OBJE']);
1608eaf8709SGreg Roach    }
1618c2e8227SGreg Roach}
162