xref: /webtrees/app/Module/MediaTabModule.php (revision 9f667ff2169dfca16c3d1c0ebb87d36da533186b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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\Factory;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Services\ClipboardService;
28use Illuminate\Support\Collection;
29
30use function preg_match;
31
32/**
33 * Class MediaTabModule
34 */
35class MediaTabModule extends AbstractModule implements ModuleTabInterface
36{
37    use ModuleTabTrait;
38
39    /** @var  Collection A list of facts with media objects. */
40    private $facts;
41
42    /** @var ClipboardService */
43    private $clipboard_service;
44
45    /**
46     * NotesTabModule constructor.
47     *
48     * @param ClipboardService $clipboard_service
49     */
50    public function __construct(ClipboardService $clipboard_service)
51    {
52        $this->clipboard_service = $clipboard_service;
53    }
54
55    /**
56     * How should this module be identified in the control panel, etc.?
57     *
58     * @return string
59     */
60    public function title(): string
61    {
62        /* I18N: Name of a module */
63        return I18N::translate('Media');
64    }
65
66    /**
67     * A sentence describing what this module does.
68     *
69     * @return string
70     */
71    public function description(): string
72    {
73        /* I18N: Description of the “Media” module */
74        return I18N::translate('A tab showing the media objects linked to an individual.');
75    }
76
77    /**
78     * The default position for this tab.  It can be changed in the control panel.
79     *
80     * @return int
81     */
82    public function defaultTabOrder(): int
83    {
84        return 5;
85    }
86
87    /**
88     * Is this tab empty? If so, we don't always need to display it.
89     *
90     * @param Individual $individual
91     *
92     * @return bool
93     */
94    public function hasTabContent(Individual $individual): bool
95    {
96        return $individual->canEdit() || $this->getFactsWithMedia($individual)->isNotEmpty();
97    }
98
99    /**
100     * A greyed out tab has no actual content, but may perhaps have
101     * options to create content.
102     *
103     * @param Individual $individual
104     *
105     * @return bool
106     */
107    public function isGrayedOut(Individual $individual): bool
108    {
109        return $this->getFactsWithMedia($individual)->isEmpty();
110    }
111
112    /**
113     * Generate the HTML content of this tab.
114     *
115     * @param Individual $individual
116     *
117     * @return string
118     */
119    public function getTabContent(Individual $individual): string
120    {
121        return view('modules/media/tab', [
122            'can_edit'   => $individual->canEdit(),
123            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
124            'individual' => $individual,
125            'facts'      => $this->getFactsWithMedia($individual),
126        ]);
127    }
128
129    /**
130     * Get all the facts for an individual which contain media objects.
131     *
132     * @param Individual $individual
133     *
134     * @return Collection<Fact>
135     */
136    protected function getFactsWithMedia(Individual $individual): Collection
137    {
138        return Factory::cache()->array()->remember(__CLASS__ . ':' . __METHOD__, static function () use ($individual): Collection {
139            $facts = $individual->facts();
140
141            foreach ($individual->spouseFamilies() as $family) {
142                if ($family->canShow()) {
143                    $facts = $facts->concat($family->facts());
144                }
145            }
146
147            $facts = $facts->filter(static function (Fact $fact): bool {
148                return preg_match('/(?:^1|\n\d) OBJE @' . Gedcom::REGEX_XREF . '@/', $fact->gedcom()) === 1;
149            });
150
151            return Fact::sortFacts($facts);
152        });
153    }
154
155    /**
156     * Can this tab load asynchronously?
157     *
158     * @return bool
159     */
160    public function canLoadAjax(): bool
161    {
162        return false;
163    }
164
165    /**
166     * This module handles the following facts - so don't show them on the "Facts and events" tab.
167     *
168     * @return Collection<string>
169     */
170    public function supportedFacts(): Collection
171    {
172        return new Collection(['OBJE']);
173    }
174}
175