xref: /webtrees/app/Module/MediaTabModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg 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;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
228d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class MediaTabModule
288c2e8227SGreg Roach */
29c1010edaSGreg Roachclass MediaTabModule extends AbstractModule implements ModuleTabInterface
30c1010edaSGreg Roach{
3176692c8bSGreg Roach    /** @var  Fact[] A list of facts with media objects. */
328c2e8227SGreg Roach    private $facts;
338c2e8227SGreg Roach
348c2e8227SGreg Roach    /** {@inheritdoc} */
358f53f488SRico Sonntag    public function getTitle(): string
36c1010edaSGreg Roach    {
37bbb76c12SGreg Roach        /* I18N: Name of a module */
38bbb76c12SGreg Roach        return I18N::translate('Media');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
428f53f488SRico Sonntag    public function getDescription(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Description of the “Media” module */
45bbb76c12SGreg Roach        return I18N::translate('A tab showing the media objects linked to an individual.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
488c2e8227SGreg Roach    /** {@inheritdoc} */
498f53f488SRico Sonntag    public function defaultTabOrder(): int
50c1010edaSGreg Roach    {
518c2e8227SGreg Roach        return 50;
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
548c2e8227SGreg Roach    /** {@inheritdoc} */
558f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
56c1010edaSGreg Roach    {
57225e381fSGreg Roach        return $individual->canEdit() || $this->getFactsWithMedia($individual);
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
608c2e8227SGreg Roach    /** {@inheritdoc} */
618f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
62c1010edaSGreg Roach    {
63225e381fSGreg Roach        return !$this->getFactsWithMedia($individual);
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
668c2e8227SGreg Roach    /** {@inheritdoc} */
679b34404bSGreg Roach    public function getTabContent(Individual $individual): string
68c1010edaSGreg Roach    {
69a8cd57e1SGreg Roach        return view('modules/media/tab', [
70225e381fSGreg Roach            'can_edit'   => $individual->canEdit(),
71225e381fSGreg Roach            'individual' => $individual,
72225e381fSGreg Roach            'facts'      => $this->getFactsWithMedia($individual),
73225e381fSGreg Roach        ]);
748c2e8227SGreg Roach    }
758c2e8227SGreg Roach
768c2e8227SGreg Roach    /**
778c2e8227SGreg Roach     * Get all the facts for an individual which contain media objects.
788c2e8227SGreg Roach     *
79225e381fSGreg Roach     * @param Individual $individual
80225e381fSGreg Roach     *
818c2e8227SGreg Roach     * @return Fact[]
828c2e8227SGreg Roach     */
838f53f488SRico Sonntag    private function getFactsWithMedia(Individual $individual): array
84c1010edaSGreg Roach    {
858c2e8227SGreg Roach        if ($this->facts === null) {
8630158ae7SGreg Roach            $facts = $individual->facts();
87225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
888c2e8227SGreg Roach                if ($family->canShow()) {
8930158ae7SGreg Roach                    foreach ($family->facts() as $fact) {
908c2e8227SGreg Roach                        $facts[] = $fact;
918c2e8227SGreg Roach                    }
928c2e8227SGreg Roach                }
938c2e8227SGreg Roach            }
9413abd6f3SGreg Roach            $this->facts = [];
958c2e8227SGreg Roach            foreach ($facts as $fact) {
968d0ebef0SGreg Roach                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
978c2e8227SGreg Roach                    $this->facts[] = $fact;
988c2e8227SGreg Roach                }
998c2e8227SGreg Roach            }
1003d7a8a4cSGreg Roach            Functions::sortFacts($this->facts);
1018c2e8227SGreg Roach        }
1028c2e8227SGreg Roach
1038c2e8227SGreg Roach        return $this->facts;
1048c2e8227SGreg Roach    }
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach    /** {@inheritdoc} */
1078f53f488SRico Sonntag    public function canLoadAjax(): bool
108c1010edaSGreg Roach    {
10915d603e7SGreg Roach        return false;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach}
112