xref: /webtrees/app/Module/MediaTabModule.php (revision 138ca96c29e1b115c3b96889a80267d29566502e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\I18N;
23use Fisharebest\Webtrees\Individual;
24
25/**
26 * Class MediaTabModule
27 */
28class MediaTabModule extends AbstractModule implements ModuleTabInterface
29{
30    /** @var  Fact[] A list of facts with media objects. */
31    private $facts;
32
33    /** {@inheritdoc} */
34    public function getTitle(): string
35    {
36        /* I18N: Name of a module */
37        return I18N::translate('Media');
38    }
39
40    /** {@inheritdoc} */
41    public function getDescription(): string
42    {
43        /* I18N: Description of the “Media” module */
44        return I18N::translate('A tab showing the media objects linked to an individual.');
45    }
46
47    /** {@inheritdoc} */
48    public function defaultTabOrder(): int
49    {
50        return 50;
51    }
52
53    /** {@inheritdoc} */
54    public function hasTabContent(Individual $individual): bool
55    {
56        return $individual->canEdit() || $this->getFactsWithMedia($individual);
57    }
58
59    /** {@inheritdoc} */
60    public function isGrayedOut(Individual $individual): bool
61    {
62        return !$this->getFactsWithMedia($individual);
63    }
64
65    /** {@inheritdoc} */
66    public function getTabContent(Individual $individual): string
67    {
68        return view('modules/media/tab', [
69            'can_edit'   => $individual->canEdit(),
70            'individual' => $individual,
71            'facts'      => $this->getFactsWithMedia($individual),
72        ]);
73    }
74
75    /**
76     * Get all the facts for an individual which contain media objects.
77     *
78     * @param Individual $individual
79     *
80     * @return Fact[]
81     */
82    private function getFactsWithMedia(Individual $individual): array
83    {
84        if ($this->facts === null) {
85            $facts = $individual->getFacts();
86            foreach ($individual->getSpouseFamilies() as $family) {
87                if ($family->canShow()) {
88                    foreach ($family->getFacts() as $fact) {
89                        $facts[] = $fact;
90                    }
91                }
92            }
93            $this->facts = [];
94            foreach ($facts as $fact) {
95                if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->gedcom())) {
96                    $this->facts[] = $fact;
97                }
98            }
99            Functions::sortFacts($this->facts);
100        }
101
102        return $this->facts;
103    }
104
105    /** {@inheritdoc} */
106    public function canLoadAjax(): bool
107    {
108        return false;
109    }
110}
111