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