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