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