xref: /webtrees/app/Media.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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
20886b77daSGreg Roachuse Closure;
21*6ccdf4f0SGreg Roachuse Exception;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
232e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
24e778de49SGreg Roachuse Illuminate\Support\Collection;
25886b77daSGreg Roachuse stdClass;
26a25f0a04SGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * A GEDCOM media (OBJE) object.
29a25f0a04SGreg Roach */
30c1010edaSGreg Roachclass Media extends GedcomRecord
31c1010edaSGreg Roach{
3216d6367aSGreg Roach    public const RECORD_TYPE = 'OBJE';
3316d6367aSGreg Roach
3416d6367aSGreg Roach    protected const ROUTE_NAME = 'media';
35a25f0a04SGreg Roach
3676692c8bSGreg Roach    /**
37886b77daSGreg Roach     * A closure which will create a record from a database row.
38886b77daSGreg Roach     *
39886b77daSGreg Roach     * @return Closure
40886b77daSGreg Roach     */
41c0804649SGreg Roach    public static function rowMapper(): Closure
42886b77daSGreg Roach    {
43c0804649SGreg Roach        return function (stdClass $row): Media {
44a7a24840SGreg Roach            return Media::getInstance($row->m_id, Tree::findById((int) $row->m_file), $row->m_gedcom);
45886b77daSGreg Roach        };
46886b77daSGreg Roach    }
47886b77daSGreg Roach
48886b77daSGreg Roach    /**
49e71ef9d2SGreg Roach     * Get an instance of a media object. For single records,
50e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
51e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
52e71ef9d2SGreg Roach     *
53e71ef9d2SGreg Roach     * @param string      $xref
54e71ef9d2SGreg Roach     * @param Tree        $tree
55e71ef9d2SGreg Roach     * @param string|null $gedcom
56e71ef9d2SGreg Roach     *
57*6ccdf4f0SGreg Roach     * @throws Exception
58e71ef9d2SGreg Roach     *
59e71ef9d2SGreg Roach     * @return Media|null
60e71ef9d2SGreg Roach     */
61e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
62c1010edaSGreg Roach    {
63e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
64e71ef9d2SGreg Roach
65e364afe4SGreg Roach        if ($record instanceof self) {
66e71ef9d2SGreg Roach            return $record;
67e71ef9d2SGreg Roach        }
68b2ce94c6SRico Sonntag
69b2ce94c6SRico Sonntag        return null;
70e71ef9d2SGreg Roach    }
71e71ef9d2SGreg Roach
72e71ef9d2SGreg Roach    /**
7376692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
7476692c8bSGreg Roach     *
7576692c8bSGreg Roach     * @param int $access_level
7676692c8bSGreg Roach     *
7776692c8bSGreg Roach     * @return bool
7876692c8bSGreg Roach     */
7935584196SGreg Roach    protected function canShowByType(int $access_level): bool
80c1010edaSGreg Roach    {
81a25f0a04SGreg Roach        // Hide media objects if they are attached to private records
8225366223SGreg Roach        $linked_ids = DB::table('link')
8325366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
8425366223SGreg Roach            ->where('l_to', '=', $this->xref)
8525366223SGreg Roach            ->pluck('l_from');
8625366223SGreg Roach
87a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
8824ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
89a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
90a25f0a04SGreg Roach                return false;
91a25f0a04SGreg Roach            }
92a25f0a04SGreg Roach        }
93a25f0a04SGreg Roach
94a25f0a04SGreg Roach        // ... otherwise apply default behaviour
95a25f0a04SGreg Roach        return parent::canShowByType($access_level);
96a25f0a04SGreg Roach    }
97a25f0a04SGreg Roach
9876692c8bSGreg Roach    /**
9976692c8bSGreg Roach     * Fetch data from the database
10076692c8bSGreg Roach     *
10176692c8bSGreg Roach     * @param string $xref
10276692c8bSGreg Roach     * @param int    $tree_id
10376692c8bSGreg Roach     *
104e364afe4SGreg Roach     * @return string|null
10576692c8bSGreg Roach     */
106e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
107c1010edaSGreg Roach    {
1082e5b4452SGreg Roach        return DB::table('media')
1092e5b4452SGreg Roach            ->where('m_id', '=', $xref)
1102e5b4452SGreg Roach            ->where('m_file', '=', $tree_id)
1112e5b4452SGreg Roach            ->value('m_gedcom');
112a25f0a04SGreg Roach    }
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach    /**
1158f5f5da8SGreg Roach     * Get the media files for this media object
1168f5f5da8SGreg Roach     *
11754c7f8dfSGreg Roach     * @return Collection
11854c7f8dfSGreg Roach     * @return MediaFile[]
1198f5f5da8SGreg Roach     */
120e778de49SGreg Roach    public function mediaFiles(): Collection
121c1010edaSGreg Roach    {
12239ca88baSGreg Roach        return $this->facts(['FILE'])
123e778de49SGreg Roach            ->map(function (Fact $fact): MediaFile {
124e778de49SGreg Roach                return new MediaFile($fact->gedcom(), $this);
125e778de49SGreg Roach            });
1268f5f5da8SGreg Roach    }
1278f5f5da8SGreg Roach
1288f5f5da8SGreg Roach    /**
12964b90bf1SGreg Roach     * Get the first media file that contains an image.
1308f5f5da8SGreg Roach     *
1318f5f5da8SGreg Roach     * @return MediaFile|null
1328f5f5da8SGreg Roach     */
133e364afe4SGreg Roach    public function firstImageFile(): ?MediaFile
134c1010edaSGreg Roach    {
1358f5f5da8SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
1364a9f750fSGreg Roach            if ($media_file->isImage()) {
1378f5f5da8SGreg Roach                return $media_file;
1388f5f5da8SGreg Roach            }
1398f5f5da8SGreg Roach        }
1408f5f5da8SGreg Roach
1418f5f5da8SGreg Roach        return null;
1428f5f5da8SGreg Roach    }
1438f5f5da8SGreg Roach
1448f5f5da8SGreg Roach    /**
145a25f0a04SGreg Roach     * Get the first note attached to this media object
146a25f0a04SGreg Roach     *
147ff166e64SGreg Roach     * @return string
148a25f0a04SGreg Roach     */
149e364afe4SGreg Roach    public function getNote(): string
150c1010edaSGreg Roach    {
151820b62dfSGreg Roach        $fact = $this->facts(['NOTE'])->first();
152820b62dfSGreg Roach
153e24444eeSGreg Roach        if ($fact instanceof Fact) {
154e24444eeSGreg Roach            // Link to note object
155dc124885SGreg Roach            $note = $fact->target();
156e24444eeSGreg Roach            if ($note instanceof Note) {
157e24444eeSGreg Roach                return $note->getNote();
158a25f0a04SGreg Roach            }
159a25f0a04SGreg Roach
160e24444eeSGreg Roach            // Inline note
16184586c02SGreg Roach            return $fact->value();
162a25f0a04SGreg Roach        }
163b2ce94c6SRico Sonntag
164b2ce94c6SRico Sonntag        return '';
165a25f0a04SGreg Roach    }
166a25f0a04SGreg Roach
167a25f0a04SGreg Roach    /**
16876692c8bSGreg Roach     * Extract names from the GEDCOM record.
169c7ff4153SGreg Roach     *
170c7ff4153SGreg Roach     * @return void
17176692c8bSGreg Roach     */
172e364afe4SGreg Roach    public function extractNames(): void
173c1010edaSGreg Roach    {
17464b90bf1SGreg Roach        $names = [];
17564b90bf1SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
17664b90bf1SGreg Roach            $names[] = $media_file->title();
17792d61e73SGreg Roach        }
17892d61e73SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
17964b90bf1SGreg Roach            $names[] = $media_file->filename();
18064b90bf1SGreg Roach        }
18164b90bf1SGreg Roach        $names = array_filter(array_unique($names));
18264b90bf1SGreg Roach
18364b90bf1SGreg Roach        if (empty($names)) {
18464b90bf1SGreg Roach            $names[] = $this->getFallBackName();
18564b90bf1SGreg Roach        }
18664b90bf1SGreg Roach
18764b90bf1SGreg Roach        foreach ($names as $name) {
18876f666f4SGreg Roach            $this->addName(static::RECORD_TYPE, $name, '');
18964b90bf1SGreg Roach        }
190a25f0a04SGreg Roach    }
191a25f0a04SGreg Roach
19276692c8bSGreg Roach    /**
19376692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
19476692c8bSGreg Roach     * identifying characteristics of this record.
19576692c8bSGreg Roach     *
19676692c8bSGreg Roach     * @return string
19776692c8bSGreg Roach     */
1988f53f488SRico Sonntag    public function formatListDetails(): string
199c1010edaSGreg Roach    {
200a25f0a04SGreg Roach        ob_start();
201f4afa648SGreg Roach        FunctionsPrintFacts::printMediaLinks($this->tree(), '1 OBJE @' . $this->xref() . '@', 1);
202a25f0a04SGreg Roach
203a25f0a04SGreg Roach        return ob_get_clean();
204a25f0a04SGreg Roach    }
20591eef4ffSGreg Roach
20691eef4ffSGreg Roach    /**
20791eef4ffSGreg Roach     * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
20891eef4ffSGreg Roach     *
20991eef4ffSGreg Roach     * @param int      $width      Pixels
21091eef4ffSGreg Roach     * @param int      $height     Pixels
21191eef4ffSGreg Roach     * @param string   $fit        "crop" or "contain"
21291eef4ffSGreg Roach     * @param string[] $attributes Additional HTML attributes
21391eef4ffSGreg Roach     *
21491eef4ffSGreg Roach     * @return string
21591eef4ffSGreg Roach     */
2168f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes = []): string
217c1010edaSGreg Roach    {
21891eef4ffSGreg Roach        // Display the first image
21991eef4ffSGreg Roach        foreach ($this->mediaFiles() as $media_file) {
22091eef4ffSGreg Roach            if ($media_file->isImage()) {
22191eef4ffSGreg Roach                return $media_file->displayImage($width, $height, $fit, $attributes);
22291eef4ffSGreg Roach            }
22391eef4ffSGreg Roach        }
22491eef4ffSGreg Roach
22591eef4ffSGreg Roach        // Display the first file of any type
22691eef4ffSGreg Roach        foreach ($this->mediaFiles() as $media_file) {
22791eef4ffSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
22891eef4ffSGreg Roach        }
22991eef4ffSGreg Roach
23091eef4ffSGreg Roach        // No image?
23191eef4ffSGreg Roach        return '';
23291eef4ffSGreg Roach    }
233a25f0a04SGreg Roach}
234