xref: /webtrees/app/Module/MediaTabModule.php (revision 8eaf87094d89aefab9b38d5ab9c97e56b054593b)
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;
25use Illuminate\Support\Collection;
26
27/**
28 * Class MediaTabModule
29 */
30class MediaTabModule extends AbstractModule implements ModuleTabInterface
31{
32    use ModuleTabTrait;
33
34    /** @var  Fact[] A list of facts with media objects. */
35    private $facts;
36
37    /**
38     * How should this module be labelled on tabs, menus, etc.?
39     *
40     * @return string
41     */
42    public function title(): string
43    {
44        /* I18N: Name of a module */
45        return I18N::translate('Media');
46    }
47
48    /**
49     * A sentence describing what this module does.
50     *
51     * @return string
52     */
53    public function description(): string
54    {
55        /* I18N: Description of the “Media” module */
56        return I18N::translate('A tab showing the media objects linked to an individual.');
57    }
58
59    /**
60     * The default position for this tab.  It can be changed in the control panel.
61     *
62     * @return int
63     */
64    public function defaultTabOrder(): int
65    {
66        return 6;
67    }
68
69    /** {@inheritdoc} */
70    public function hasTabContent(Individual $individual): bool
71    {
72        return $individual->canEdit() || $this->getFactsWithMedia($individual);
73    }
74
75    /** {@inheritdoc} */
76    public function isGrayedOut(Individual $individual): bool
77    {
78        return !$this->getFactsWithMedia($individual);
79    }
80
81    /** {@inheritdoc} */
82    public function getTabContent(Individual $individual): string
83    {
84        return view('modules/media/tab', [
85            'can_edit'   => $individual->canEdit(),
86            'individual' => $individual,
87            'facts'      => $this->getFactsWithMedia($individual),
88        ]);
89    }
90
91    /**
92     * Get all the facts for an individual which contain media objects.
93     *
94     * @param Individual $individual
95     *
96     * @return Fact[]
97     */
98    private function getFactsWithMedia(Individual $individual): array
99    {
100        if ($this->facts === null) {
101            $facts = $individual->facts();
102            foreach ($individual->getSpouseFamilies() as $family) {
103                if ($family->canShow()) {
104                    foreach ($family->facts() as $fact) {
105                        $facts[] = $fact;
106                    }
107                }
108            }
109            $this->facts = [];
110            foreach ($facts as $fact) {
111                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
112                    $this->facts[] = $fact;
113                }
114            }
115            Functions::sortFacts($this->facts);
116        }
117
118        return $this->facts;
119    }
120
121    /** {@inheritdoc} */
122    public function canLoadAjax(): bool
123    {
124        return false;
125    }
126
127    /**
128     * This module handles the following facts - so don't show them on the "Facts and events" tab.
129     *
130     * @return Collection|string[]
131     */
132    public function supportedFacts(): Collection
133    {
134        return new Collection(['OBJE']);
135    }
136}
137