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