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