xref: /webtrees/app/Media.php (revision a7fcf1d6d3914dff354d0d2b112705386a5c0a2a)
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(function (Fact $fact): MediaFile {
73                return new MediaFile($fact->gedcom(), $this);
74            });
75    }
76
77    /**
78     * Get the first media file that contains an image.
79     *
80     * @return MediaFile|null
81     */
82    public function firstImageFile(): ?MediaFile
83    {
84        return $this->mediaFiles()
85            ->first(static function (MediaFile $media_file): bool {
86                return $media_file->isImage() && !$media_file->isExternal();
87            });
88    }
89
90    /**
91     * Get the first note attached to this media object
92     *
93     * @return string
94     */
95    public function getNote(): string
96    {
97        $fact = $this->facts(['NOTE'])->first();
98
99        if ($fact instanceof Fact) {
100            // Link to note object
101            $note = $fact->target();
102            if ($note instanceof Note) {
103                return $note->getNote();
104            }
105
106            // Inline note
107            return $fact->value();
108        }
109
110        return '';
111    }
112
113    /**
114     * Extract names from the GEDCOM record.
115     *
116     * @return void
117     */
118    public function extractNames(): void
119    {
120        $names = [];
121        foreach ($this->mediaFiles() as $media_file) {
122            $names[] = $media_file->title();
123        }
124
125        // Titles may be empty.
126        $names = array_filter($names);
127
128        if ($names === []) {
129            foreach ($this->mediaFiles() as $media_file) {
130                $names[] = $media_file->filename();
131            }
132        }
133
134        // Name and title may be the same.
135        $names = array_unique($names);
136
137        // No media files in this media object?
138        if ($names === []) {
139            $names[] = $this->getFallBackName();
140        }
141
142        foreach ($names as $name) {
143            $this->addName(static::RECORD_TYPE, $name, '');
144        }
145    }
146
147    /**
148     * This function should be redefined in derived classes to show any major
149     * identifying characteristics of this record.
150     *
151     * @return string
152     */
153    public function formatListDetails(): string
154    {
155        return (new XrefMedia(I18N::translate('Media')))
156            ->labelValue('@' . $this->xref . '@', $this->tree());
157    }
158
159    /**
160     * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
161     *
162     * @param int                  $width      Pixels
163     * @param int                  $height     Pixels
164     * @param string               $fit        "crop" or "contain"
165     * @param array<string,string> $attributes Additional HTML attributes
166     *
167     * @return string
168     */
169    public function displayImage(int $width, int $height, string $fit, array $attributes): string
170    {
171        // Display the first image
172        foreach ($this->mediaFiles() as $media_file) {
173            if ($media_file->isImage()) {
174                return $media_file->displayImage($width, $height, $fit, $attributes);
175            }
176        }
177
178        // Display the first file of any type
179        $media_file = $this->mediaFiles()->first();
180
181        if ($media_file instanceof MediaFile) {
182            return $media_file->displayImage($width, $height, $fit, $attributes);
183        }
184
185        // No image?
186        return '';
187    }
188
189    /**
190     * Lock the database row, to prevent concurrent edits.
191     */
192    public function lock(): void
193    {
194        DB::table('media')
195            ->where('m_file', '=', $this->tree->id())
196            ->where('m_id', '=', $this->xref())
197            ->lockForUpdate()
198            ->get();
199    }
200}
201