xref: /webtrees/app/Media.php (revision dd6b2bfcc550270bb6d6778e11576148f71e4330)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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;
21
22/**
23 * A GEDCOM media (OBJE) object.
24 */
25class Media extends GedcomRecord
26{
27    const RECORD_TYPE = 'OBJE';
28    const ROUTE_NAME  = 'media';
29
30    /**
31     * Get an instance of a media object. For single records,
32     * we just receive the XREF. For bulk records (such as lists
33     * and search results) we can receive the GEDCOM data as well.
34     *
35     * @param string      $xref
36     * @param Tree        $tree
37     * @param string|null $gedcom
38     *
39     * @throws \Exception
40     *
41     * @return Media|null
42     */
43    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
44    {
45        $record = parent::getInstance($xref, $tree, $gedcom);
46
47        if ($record instanceof Media) {
48            return $record;
49        }
50
51        return null;
52    }
53
54    /**
55     * Each object type may have its own special rules, and re-implement this function.
56     *
57     * @param int $access_level
58     *
59     * @return bool
60     */
61    protected function canShowByType(int $access_level): bool
62    {
63        // Hide media objects if they are attached to private records
64        $linked_ids = Database::prepare(
65            "SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?"
66        )->execute([
67            $this->xref,
68            $this->tree->getTreeId(),
69        ])->fetchOneColumn();
70        foreach ($linked_ids as $linked_id) {
71            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
72            if ($linked_record && !$linked_record->canShow($access_level)) {
73                return false;
74            }
75        }
76
77        // ... otherwise apply default behaviour
78        return parent::canShowByType($access_level);
79    }
80
81    /**
82     * Fetch data from the database
83     *
84     * @param string $xref
85     * @param int    $tree_id
86     *
87     * @return null|string
88     */
89    protected static function fetchGedcomRecord(string $xref, int $tree_id)
90    {
91        return Database::prepare(
92            "SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id"
93        )->execute([
94            'xref'    => $xref,
95            'tree_id' => $tree_id,
96        ])->fetchOne();
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->getFacts('FILE') as $fact) {
109            $media_files[] = new MediaFile($fact->getGedcom(), $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 null|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->getTarget();
142            if ($note instanceof Note) {
143                return $note->getNote();
144            }
145
146            // Inline note
147            return $fact->getValue();
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->getTree(), '1 OBJE @' . $this->getXref() . '@', 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