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