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