xref: /webtrees/app/Media.php (revision f25fc0f929f69ab8124cf0cecde45e457db7574a)
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;
21
22use Fisharebest\Webtrees\Elements\XrefMedia;
23use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
24use Illuminate\Support\Collection;
25
26use function array_filter;
27use function array_unique;
28
29/**
30 * A GEDCOM media (OBJE) object.
31 */
32class Media extends GedcomRecord
33{
34    public const RECORD_TYPE = 'OBJE';
35
36    protected const ROUTE_NAME = MediaPage::class;
37
38    /**
39     * Each object type may have its own special rules, and re-implement this function.
40     *
41     * @param int $access_level
42     *
43     * @return bool
44     */
45    protected function canShowByType(int $access_level): bool
46    {
47        // Hide media objects if they are attached to private records
48        $linked_ids = DB::table('link')
49            ->where('l_file', '=', $this->tree->id())
50            ->where('l_to', '=', $this->xref)
51            ->pluck('l_from');
52
53        foreach ($linked_ids as $linked_id) {
54            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
55            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
56                return false;
57            }
58        }
59
60        // ... otherwise apply default behavior
61        return parent::canShowByType($access_level);
62    }
63
64    /**
65     * Get the media files for this media object
66     *
67     * @return Collection<int,MediaFile>
68     */
69    public function mediaFiles(): Collection
70    {
71        return $this->facts(['FILE'])
72            ->map(fn(Fact $fact): MediaFile => new MediaFile($fact->gedcom(), $this));
73    }
74
75    /**
76     * Get the first media file that contains an image.
77     *
78     * @return MediaFile|null
79     */
80    public function firstImageFile(): ?MediaFile
81    {
82        return $this->mediaFiles()
83            ->first(static fn(MediaFile $media_file): bool => $media_file->isImage() && !$media_file->isExternal());
84    }
85
86    /**
87     * Get the first note attached to this media object
88     *
89     * @return string
90     */
91    public function getNote(): string
92    {
93        $fact = $this->facts(['NOTE'])->first();
94
95        if ($fact instanceof Fact) {
96            // Link to note object
97            $note = $fact->target();
98            if ($note instanceof Note) {
99                return $note->getNote();
100            }
101
102            // Inline note
103            return $fact->value();
104        }
105
106        return '';
107    }
108
109    /**
110     * Extract names from the GEDCOM record.
111     *
112     * @return void
113     */
114    public function extractNames(): void
115    {
116        $names = [];
117        foreach ($this->mediaFiles() as $media_file) {
118            $names[] = $media_file->title();
119        }
120
121        // Titles may be empty.
122        $names = array_filter($names);
123
124        if ($names === []) {
125            foreach ($this->mediaFiles() as $media_file) {
126                $names[] = $media_file->filename();
127            }
128        }
129
130        // Name and title may be the same.
131        $names = array_unique($names);
132
133        // No media files in this media object?
134        if ($names === []) {
135            $names[] = $this->getFallBackName();
136        }
137
138        foreach ($names as $name) {
139            $this->addName(static::RECORD_TYPE, $name, '');
140        }
141    }
142
143    /**
144     * This function should be redefined in derived classes to show any major
145     * identifying characteristics of this record.
146     *
147     * @return string
148     */
149    public function formatListDetails(): string
150    {
151        return (new XrefMedia(I18N::translate('Media')))
152            ->labelValue('@' . $this->xref . '@', $this->tree());
153    }
154
155    /**
156     * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
157     *
158     * @param int                  $width      Pixels
159     * @param int                  $height     Pixels
160     * @param string               $fit        "crop" or "contain"
161     * @param array<string,string> $attributes Additional HTML attributes
162     *
163     * @return string
164     */
165    public function displayImage(int $width, int $height, string $fit, array $attributes): string
166    {
167        // Display the first image
168        foreach ($this->mediaFiles() as $media_file) {
169            if ($media_file->isImage()) {
170                return $media_file->displayImage($width, $height, $fit, $attributes);
171            }
172        }
173
174        // Display the first file of any type
175        $media_file = $this->mediaFiles()->first();
176
177        if ($media_file instanceof MediaFile) {
178            return $media_file->displayImage($width, $height, $fit, $attributes);
179        }
180
181        // No image?
182        return '';
183    }
184
185    /**
186     * Lock the database row, to prevent concurrent edits.
187     */
188    public function lock(): void
189    {
190        DB::table('media')
191            ->where('m_file', '=', $this->tree->id())
192            ->where('m_id', '=', $this->xref())
193            ->lockForUpdate()
194            ->get();
195    }
196}
197