xref: /webtrees/app/Media.php (revision 72cf66d48ef1f917238d9b0939a8aa33f257e274)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
1976692c8bSGreg Roach
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
21a25f0a04SGreg Roach
22a25f0a04SGreg Roach/**
2376692c8bSGreg Roach * A GEDCOM media (OBJE) object.
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass Media extends GedcomRecord
26c1010edaSGreg Roach{
27a25f0a04SGreg Roach    const RECORD_TYPE = 'OBJE';
28225e381fSGreg Roach    const ROUTE_NAME  = 'media';
29a25f0a04SGreg Roach
3076692c8bSGreg Roach    /**
31e71ef9d2SGreg Roach     * Get an instance of a media object. For single records,
32e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
33e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
34e71ef9d2SGreg Roach     *
35e71ef9d2SGreg Roach     * @param string      $xref
36e71ef9d2SGreg Roach     * @param Tree        $tree
37e71ef9d2SGreg Roach     * @param string|null $gedcom
38e71ef9d2SGreg Roach     *
39e71ef9d2SGreg Roach     * @throws \Exception
40e71ef9d2SGreg Roach     *
41e71ef9d2SGreg Roach     * @return Media|null
42e71ef9d2SGreg Roach     */
4376f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
44c1010edaSGreg Roach    {
45e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
46e71ef9d2SGreg Roach
47e71ef9d2SGreg Roach        if ($record instanceof Media) {
48e71ef9d2SGreg Roach            return $record;
49e71ef9d2SGreg Roach        }
50b2ce94c6SRico Sonntag
51b2ce94c6SRico Sonntag        return null;
52e71ef9d2SGreg Roach    }
53e71ef9d2SGreg Roach
54e71ef9d2SGreg Roach    /**
5576692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @param int $access_level
5876692c8bSGreg Roach     *
5976692c8bSGreg Roach     * @return bool
6076692c8bSGreg Roach     */
6135584196SGreg Roach    protected function canShowByType(int $access_level): bool
62c1010edaSGreg Roach    {
63a25f0a04SGreg Roach        // Hide media objects if they are attached to private records
64a25f0a04SGreg Roach        $linked_ids = Database::prepare(
65a25f0a04SGreg Roach            "SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?"
6613abd6f3SGreg Roach        )->execute([
67c1010edaSGreg Roach            $this->xref,
68*72cf66d4SGreg Roach            $this->tree->id(),
6913abd6f3SGreg Roach        ])->fetchOneColumn();
70a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
7124ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
72a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
73a25f0a04SGreg Roach                return false;
74a25f0a04SGreg Roach            }
75a25f0a04SGreg Roach        }
76a25f0a04SGreg Roach
77a25f0a04SGreg Roach        // ... otherwise apply default behaviour
78a25f0a04SGreg Roach        return parent::canShowByType($access_level);
79a25f0a04SGreg Roach    }
80a25f0a04SGreg Roach
8176692c8bSGreg Roach    /**
8276692c8bSGreg Roach     * Fetch data from the database
8376692c8bSGreg Roach     *
8476692c8bSGreg Roach     * @param string $xref
8576692c8bSGreg Roach     * @param int    $tree_id
8676692c8bSGreg Roach     *
8776692c8bSGreg Roach     * @return null|string
8876692c8bSGreg Roach     */
8976f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
90c1010edaSGreg Roach    {
9164d9078aSGreg Roach        return Database::prepare(
9264d9078aSGreg Roach            "SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id"
9313abd6f3SGreg Roach        )->execute([
9464d9078aSGreg Roach            'xref'    => $xref,
9564d9078aSGreg Roach            'tree_id' => $tree_id,
9613abd6f3SGreg Roach        ])->fetchOne();
97a25f0a04SGreg Roach    }
98a25f0a04SGreg Roach
99a25f0a04SGreg Roach    /**
1008f5f5da8SGreg Roach     * Get the media files for this media object
1018f5f5da8SGreg Roach     *
1028f5f5da8SGreg Roach     * @return MediaFile[]
1038f5f5da8SGreg Roach     */
104c1010edaSGreg Roach    public function mediaFiles(): array
105c1010edaSGreg Roach    {
1068f5f5da8SGreg Roach        $media_files = [];
1078f5f5da8SGreg Roach
1088f5f5da8SGreg Roach        foreach ($this->getFacts('FILE') as $fact) {
109138ca96cSGreg Roach            $media_files[] = new MediaFile($fact->gedcom(), $this);
1108f5f5da8SGreg Roach        }
1118f5f5da8SGreg Roach
1128f5f5da8SGreg Roach        return $media_files;
1138f5f5da8SGreg Roach    }
1148f5f5da8SGreg Roach
1158f5f5da8SGreg Roach    /**
11664b90bf1SGreg Roach     * Get the first media file that contains an image.
1178f5f5da8SGreg Roach     *
1188f5f5da8SGreg Roach     * @return MediaFile|null
1198f5f5da8SGreg Roach     */
120c1010edaSGreg Roach    public function firstImageFile()
121c1010edaSGreg Roach    {
1228f5f5da8SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
1234a9f750fSGreg Roach            if ($media_file->isImage()) {
1248f5f5da8SGreg Roach                return $media_file;
1258f5f5da8SGreg Roach            }
1268f5f5da8SGreg Roach        }
1278f5f5da8SGreg Roach
1288f5f5da8SGreg Roach        return null;
1298f5f5da8SGreg Roach    }
1308f5f5da8SGreg Roach
1318f5f5da8SGreg Roach    /**
132a25f0a04SGreg Roach     * Get the first note attached to this media object
133a25f0a04SGreg Roach     *
134ff166e64SGreg Roach     * @return string
135a25f0a04SGreg Roach     */
136c1010edaSGreg Roach    public function getNote()
137c1010edaSGreg Roach    {
138e24444eeSGreg Roach        $fact = $this->getFirstFact('NOTE');
139e24444eeSGreg Roach        if ($fact instanceof Fact) {
140e24444eeSGreg Roach            // Link to note object
141dc124885SGreg Roach            $note = $fact->target();
142e24444eeSGreg Roach            if ($note instanceof Note) {
143e24444eeSGreg Roach                return $note->getNote();
144a25f0a04SGreg Roach            }
145a25f0a04SGreg Roach
146e24444eeSGreg Roach            // Inline note
14784586c02SGreg Roach            return $fact->value();
148a25f0a04SGreg Roach        }
149b2ce94c6SRico Sonntag
150b2ce94c6SRico Sonntag        return '';
151a25f0a04SGreg Roach    }
152a25f0a04SGreg Roach
153a25f0a04SGreg Roach    /**
15476692c8bSGreg Roach     * Extract names from the GEDCOM record.
155c7ff4153SGreg Roach     *
156c7ff4153SGreg Roach     * @return void
15776692c8bSGreg Roach     */
158c1010edaSGreg Roach    public function extractNames()
159c1010edaSGreg Roach    {
16064b90bf1SGreg Roach        $names = [];
16164b90bf1SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
16264b90bf1SGreg Roach            $names[] = $media_file->title();
16392d61e73SGreg Roach        }
16492d61e73SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
16564b90bf1SGreg Roach            $names[] = $media_file->filename();
16664b90bf1SGreg Roach        }
16764b90bf1SGreg Roach        $names = array_filter(array_unique($names));
16864b90bf1SGreg Roach
16964b90bf1SGreg Roach        if (empty($names)) {
17064b90bf1SGreg Roach            $names[] = $this->getFallBackName();
17164b90bf1SGreg Roach        }
17264b90bf1SGreg Roach
17364b90bf1SGreg Roach        foreach ($names as $name) {
17476f666f4SGreg Roach            $this->addName(static::RECORD_TYPE, $name, '');
17564b90bf1SGreg Roach        }
176a25f0a04SGreg Roach    }
177a25f0a04SGreg Roach
17876692c8bSGreg Roach    /**
17976692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
18076692c8bSGreg Roach     * identifying characteristics of this record.
18176692c8bSGreg Roach     *
18276692c8bSGreg Roach     * @return string
18376692c8bSGreg Roach     */
1848f53f488SRico Sonntag    public function formatListDetails(): string
185c1010edaSGreg Roach    {
186a25f0a04SGreg Roach        ob_start();
187911f5683SGreg Roach        FunctionsPrintFacts::printMediaLinks($this->getTree(), '1 OBJE @' . $this->getXref() . '@', 1);
188a25f0a04SGreg Roach
189a25f0a04SGreg Roach        return ob_get_clean();
190a25f0a04SGreg Roach    }
19191eef4ffSGreg Roach
19291eef4ffSGreg Roach    /**
19391eef4ffSGreg Roach     * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
19491eef4ffSGreg Roach     *
19591eef4ffSGreg Roach     * @param int      $width      Pixels
19691eef4ffSGreg Roach     * @param int      $height     Pixels
19791eef4ffSGreg Roach     * @param string   $fit        "crop" or "contain"
19891eef4ffSGreg Roach     * @param string[] $attributes Additional HTML attributes
19991eef4ffSGreg Roach     *
20091eef4ffSGreg Roach     * @return string
20191eef4ffSGreg Roach     */
2028f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes = []): string
203c1010edaSGreg Roach    {
20491eef4ffSGreg Roach        // Display the first image
20591eef4ffSGreg Roach        foreach ($this->mediaFiles() as $media_file) {
20691eef4ffSGreg Roach            if ($media_file->isImage()) {
20791eef4ffSGreg Roach                return $media_file->displayImage($width, $height, $fit, $attributes);
20891eef4ffSGreg Roach            }
20991eef4ffSGreg Roach        }
21091eef4ffSGreg Roach
21191eef4ffSGreg Roach        // Display the first file of any type
21291eef4ffSGreg Roach        foreach ($this->mediaFiles() as $media_file) {
21391eef4ffSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
21491eef4ffSGreg Roach        }
21591eef4ffSGreg Roach
21691eef4ffSGreg Roach        // No image?
21791eef4ffSGreg Roach        return '';
21891eef4ffSGreg Roach    }
219a25f0a04SGreg Roach}
220