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