xref: /webtrees/app/Media.php (revision b6f35a76f16ee5da672b7d3d886becc6b838498e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Exception;
24use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
25use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Support\Collection;
28use stdClass;
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     * A closure which will create a record from a database row.
41     *
42     * @return Closure
43     */
44    public static function rowMapper(): Closure
45    {
46        return static function (stdClass $row): Media {
47            return Media::getInstance($row->m_id, Tree::findById((int) $row->m_file), $row->m_gedcom);
48        };
49    }
50
51    /**
52     * Get an instance of a media object. For single records,
53     * we just receive the XREF. For bulk records (such as lists
54     * and search results) we can receive the GEDCOM data as well.
55     *
56     * @param string      $xref
57     * @param Tree        $tree
58     * @param string|null $gedcom
59     *
60     * @throws Exception
61     *
62     * @return Media|null
63     */
64    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
65    {
66        $record = parent::getInstance($xref, $tree, $gedcom);
67
68        if ($record instanceof self) {
69            return $record;
70        }
71
72        return null;
73    }
74
75    /**
76     * Each object type may have its own special rules, and re-implement this function.
77     *
78     * @param int $access_level
79     *
80     * @return bool
81     */
82    protected function canShowByType(int $access_level): bool
83    {
84        // Hide media objects if they are attached to private records
85        $linked_ids = DB::table('link')
86            ->where('l_file', '=', $this->tree->id())
87            ->where('l_to', '=', $this->xref)
88            ->pluck('l_from');
89
90        foreach ($linked_ids as $linked_id) {
91            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
92            if ($linked_record && !$linked_record->canShow($access_level)) {
93                return false;
94            }
95        }
96
97        // ... otherwise apply default behavior
98        return parent::canShowByType($access_level);
99    }
100
101    /**
102     * Fetch data from the database
103     *
104     * @param string $xref
105     * @param int    $tree_id
106     *
107     * @return string|null
108     */
109    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
110    {
111        return DB::table('media')
112            ->where('m_id', '=', $xref)
113            ->where('m_file', '=', $tree_id)
114            ->value('m_gedcom');
115    }
116
117    /**
118     * Get the media files for this media object
119     *
120     * @return Collection
121     */
122    public function mediaFiles(): Collection
123    {
124        return $this->facts(['FILE'])
125            ->map(function (Fact $fact): MediaFile {
126                return new MediaFile($fact->gedcom(), $this);
127            });
128    }
129
130    /**
131     * Get the first media file that contains an image.
132     *
133     * @return MediaFile|null
134     */
135    public function firstImageFile(): ?MediaFile
136    {
137        foreach ($this->mediaFiles() as $media_file) {
138            if ($media_file->isImage()) {
139                return $media_file;
140            }
141        }
142
143        return null;
144    }
145
146    /**
147     * Get the first note attached to this media object
148     *
149     * @return string
150     */
151    public function getNote(): string
152    {
153        $fact = $this->facts(['NOTE'])->first();
154
155        if ($fact instanceof Fact) {
156            // Link to note object
157            $note = $fact->target();
158            if ($note instanceof Note) {
159                return $note->getNote();
160            }
161
162            // Inline note
163            return $fact->value();
164        }
165
166        return '';
167    }
168
169    /**
170     * Extract names from the GEDCOM record.
171     *
172     * @return void
173     */
174    public function extractNames(): void
175    {
176        $names = [];
177        foreach ($this->mediaFiles() as $media_file) {
178            $names[] = $media_file->title();
179        }
180        foreach ($this->mediaFiles() as $media_file) {
181            $names[] = $media_file->filename();
182        }
183        $names = array_filter(array_unique($names));
184
185        if ($names === []) {
186            $names[] = $this->getFallBackName();
187        }
188
189        foreach ($names as $name) {
190            $this->addName(static::RECORD_TYPE, $name, '');
191        }
192    }
193
194    /**
195     * This function should be redefined in derived classes to show any major
196     * identifying characteristics of this record.
197     *
198     * @return string
199     */
200    public function formatListDetails(): string
201    {
202        ob_start();
203        FunctionsPrintFacts::printMediaLinks($this->tree(), '1 OBJE @' . $this->xref() . '@', 1);
204
205        return ob_get_clean();
206    }
207
208    /**
209     * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
210     *
211     * @param int      $width      Pixels
212     * @param int      $height     Pixels
213     * @param string   $fit        "crop" or "contain"
214     * @param string[] $attributes Additional HTML attributes
215     *
216     * @return string
217     */
218    public function displayImage($width, $height, $fit, $attributes = []): string
219    {
220        // Display the first image
221        foreach ($this->mediaFiles() as $media_file) {
222            if ($media_file->isImage()) {
223                return $media_file->displayImage($width, $height, $fit, $attributes);
224            }
225        }
226
227        // Display the first file of any type
228        foreach ($this->mediaFiles() as $media_file) {
229            return $media_file->displayImage($width, $height, $fit, $attributes);
230        }
231
232        // No image?
233        return '';
234    }
235}
236