xref: /webtrees/app/Module/MediaTabModule.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Fact;
23use Fisharebest\Webtrees\Gedcom;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Services\ClipboardService;
27use Illuminate\Support\Collection;
28
29/**
30 * Class MediaTabModule
31 */
32class MediaTabModule extends AbstractModule implements ModuleTabInterface
33{
34    use ModuleTabTrait;
35
36    /** @var  Collection A list of facts with media objects. */
37    private $facts;
38
39    /** @var ClipboardService */
40    private $clipboard_service;
41
42    /**
43     * NotesTabModule constructor.
44     *
45     * @param ClipboardService $clipboard_service
46     */
47    public function __construct(ClipboardService $clipboard_service)
48    {
49        $this->clipboard_service = $clipboard_service;
50    }
51
52    /**
53     * How should this module be identified in the control panel, etc.?
54     *
55     * @return string
56     */
57    public function title(): string
58    {
59        /* I18N: Name of a module */
60        return I18N::translate('Media');
61    }
62
63    /**
64     * A sentence describing what this module does.
65     *
66     * @return string
67     */
68    public function description(): string
69    {
70        /* I18N: Description of the “Media” module */
71        return I18N::translate('A tab showing the media objects linked to an individual.');
72    }
73
74    /**
75     * The default position for this tab.  It can be changed in the control panel.
76     *
77     * @return int
78     */
79    public function defaultTabOrder(): int
80    {
81        return 5;
82    }
83
84    /**
85     * Is this tab empty? If so, we don't always need to display it.
86     *
87     * @param Individual $individual
88     *
89     * @return bool
90     */
91    public function hasTabContent(Individual $individual): bool
92    {
93        return $individual->canEdit() || $this->getFactsWithMedia($individual)->isNotEmpty();
94    }
95
96    /**
97     * A greyed out tab has no actual content, but may perhaps have
98     * options to create content.
99     *
100     * @param Individual $individual
101     *
102     * @return bool
103     */
104    public function isGrayedOut(Individual $individual): bool
105    {
106        return !$this->getFactsWithMedia($individual);
107    }
108
109    /**
110     * Generate the HTML content of this tab.
111     *
112     * @param Individual $individual
113     *
114     * @return string
115     */
116    public function getTabContent(Individual $individual): string
117    {
118        return view('modules/media/tab', [
119            'can_edit'   => $individual->canEdit(),
120            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
121            'individual' => $individual,
122            'facts'      => $this->getFactsWithMedia($individual),
123        ]);
124    }
125
126    /**
127     * Get all the facts for an individual which contain media objects.
128     *
129     * @param Individual $individual
130     *
131     * @return Collection<Fact>
132     */
133    private function getFactsWithMedia(Individual $individual): Collection
134    {
135        if ($this->facts === null) {
136            $facts = $individual->facts();
137            foreach ($individual->spouseFamilies() as $family) {
138                if ($family->canShow()) {
139                    foreach ($family->facts() as $fact) {
140                        $facts->push($fact);
141                    }
142                }
143            }
144
145            $this->facts = new Collection();
146
147            foreach ($facts as $fact) {
148                if (preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom())) {
149                    $this->facts->push($fact);
150                }
151            }
152            $this->facts = Fact::sortFacts($this->facts);
153        }
154
155        return $this->facts;
156    }
157
158    /**
159     * Can this tab load asynchronously?
160     *
161     * @return bool
162     */
163    public function canLoadAjax(): bool
164    {
165        return false;
166    }
167
168    /**
169     * This module handles the following facts - so don't show them on the "Facts and events" tab.
170     *
171     * @return Collection<string>
172     */
173    public function supportedFacts(): Collection
174    {
175        return new Collection(['OBJE']);
176    }
177}
178