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