xref: /webtrees/app/Module/MediaTabModule.php (revision 3fa66c660869af2f4c92ef37e06997aa8a0f55e1)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Fact;
21use Fisharebest\Webtrees\Functions\Functions;
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25
26/**
27 * Class MediaTabModule
28 */
29class MediaTabModule extends AbstractModule implements ModuleTabInterface
30{
31    /** @var  Fact[] A list of facts with media objects. */
32    private $facts;
33
34    /** {@inheritdoc} */
35    public function getTitle(): string
36    {
37        /* I18N: Name of a module */
38        return I18N::translate('Media');
39    }
40
41    /** {@inheritdoc} */
42    public function getDescription(): string
43    {
44        /* I18N: Description of the “Media” module */
45        return I18N::translate('A tab showing the media objects linked to an individual.');
46    }
47
48    /** {@inheritdoc} */
49    public function defaultTabOrder(): int
50    {
51        return 50;
52    }
53
54    /** {@inheritdoc} */
55    public function hasTabContent(Individual $individual): bool
56    {
57        return $individual->canEdit() || $this->getFactsWithMedia($individual);
58    }
59
60    /** {@inheritdoc} */
61    public function isGrayedOut(Individual $individual): bool
62    {
63        return !$this->getFactsWithMedia($individual);
64    }
65
66    /** {@inheritdoc} */
67    public function getTabContent(Individual $individual): string
68    {
69        return view('modules/media/tab', [
70            'can_edit'   => $individual->canEdit(),
71            'individual' => $individual,
72            'facts'      => $this->getFactsWithMedia($individual),
73        ]);
74    }
75
76    /**
77     * Get all the facts for an individual which contain media objects.
78     *
79     * @param Individual $individual
80     *
81     * @return Fact[]
82     */
83    private function getFactsWithMedia(Individual $individual): array
84    {
85        if ($this->facts === null) {
86            $facts = $individual->facts();
87            foreach ($individual->getSpouseFamilies() as $family) {
88                if ($family->canShow()) {
89                    foreach ($family->facts() as $fact) {
90                        $facts[] = $fact;
91                    }
92                }
93            }
94            $this->facts = [];
95            foreach ($facts as $fact) {
96                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
97                    $this->facts[] = $fact;
98                }
99            }
100            Functions::sortFacts($this->facts);
101        }
102
103        return $this->facts;
104    }
105
106    /** {@inheritdoc} */
107    public function canLoadAjax(): bool
108    {
109        return false;
110    }
111}
112