xref: /webtrees/app/Media.php (revision ffc0a61f636c2f449ee87acaba2c9b803ca6e758)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
2176692c8bSGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
25d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
262e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27e778de49SGreg Roachuse Illuminate\Support\Collection;
28886b77daSGreg Roachuse stdClass;
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach/**
3176692c8bSGreg Roach * A GEDCOM media (OBJE) object.
32a25f0a04SGreg Roach */
33c1010edaSGreg Roachclass Media extends GedcomRecord
34c1010edaSGreg Roach{
3516d6367aSGreg Roach    public const RECORD_TYPE = 'OBJE';
3616d6367aSGreg Roach
37d7daee59SGreg Roach    protected const ROUTE_NAME = MediaPage::class;
38a25f0a04SGreg Roach
3976692c8bSGreg Roach    /**
40886b77daSGreg Roach     * A closure which will create a record from a database row.
41886b77daSGreg Roach     *
42d5ad3db0SGreg Roach     * @param Tree $tree
43d5ad3db0SGreg Roach     *
44886b77daSGreg Roach     * @return Closure
45886b77daSGreg Roach     */
46d5ad3db0SGreg Roach    public static function rowMapper(Tree $tree): Closure
47886b77daSGreg Roach    {
48d5ad3db0SGreg Roach        return static function (stdClass $row) use ($tree): Media {
49*ffc0a61fSGreg Roach            $media = Media::getInstance($row->m_id, $tree, $row->m_gedcom);
50*ffc0a61fSGreg Roach            assert($media instanceof Media);
51*ffc0a61fSGreg Roach
52*ffc0a61fSGreg Roach            return $media;
53886b77daSGreg Roach        };
54886b77daSGreg Roach    }
55886b77daSGreg Roach
56886b77daSGreg Roach    /**
57e71ef9d2SGreg Roach     * Get an instance of a media object. For single records,
58e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
59e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
60e71ef9d2SGreg Roach     *
61e71ef9d2SGreg Roach     * @param string      $xref
62e71ef9d2SGreg Roach     * @param Tree        $tree
63e71ef9d2SGreg Roach     * @param string|null $gedcom
64e71ef9d2SGreg Roach     *
656ccdf4f0SGreg Roach     * @throws Exception
66e71ef9d2SGreg Roach     *
67e71ef9d2SGreg Roach     * @return Media|null
68e71ef9d2SGreg Roach     */
69*ffc0a61fSGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Media
70c1010edaSGreg Roach    {
71e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
72e71ef9d2SGreg Roach
73e364afe4SGreg Roach        if ($record instanceof self) {
74e71ef9d2SGreg Roach            return $record;
75e71ef9d2SGreg Roach        }
76b2ce94c6SRico Sonntag
77b2ce94c6SRico Sonntag        return null;
78e71ef9d2SGreg Roach    }
79e71ef9d2SGreg Roach
80e71ef9d2SGreg Roach    /**
8176692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
8276692c8bSGreg Roach     *
8376692c8bSGreg Roach     * @param int $access_level
8476692c8bSGreg Roach     *
8576692c8bSGreg Roach     * @return bool
8676692c8bSGreg Roach     */
8735584196SGreg Roach    protected function canShowByType(int $access_level): bool
88c1010edaSGreg Roach    {
89a25f0a04SGreg Roach        // Hide media objects if they are attached to private records
9025366223SGreg Roach        $linked_ids = DB::table('link')
9125366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
9225366223SGreg Roach            ->where('l_to', '=', $this->xref)
9325366223SGreg Roach            ->pluck('l_from');
9425366223SGreg Roach
95a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
9624ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
97a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
98a25f0a04SGreg Roach                return false;
99a25f0a04SGreg Roach            }
100a25f0a04SGreg Roach        }
101a25f0a04SGreg Roach
102b3a775f6SGreg Roach        // ... otherwise apply default behavior
103a25f0a04SGreg Roach        return parent::canShowByType($access_level);
104a25f0a04SGreg Roach    }
105a25f0a04SGreg Roach
10676692c8bSGreg Roach    /**
10776692c8bSGreg Roach     * Fetch data from the database
10876692c8bSGreg Roach     *
10976692c8bSGreg Roach     * @param string $xref
11076692c8bSGreg Roach     * @param int    $tree_id
11176692c8bSGreg Roach     *
112e364afe4SGreg Roach     * @return string|null
11376692c8bSGreg Roach     */
114e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
115c1010edaSGreg Roach    {
1162e5b4452SGreg Roach        return DB::table('media')
1172e5b4452SGreg Roach            ->where('m_id', '=', $xref)
1182e5b4452SGreg Roach            ->where('m_file', '=', $tree_id)
1192e5b4452SGreg Roach            ->value('m_gedcom');
120a25f0a04SGreg Roach    }
121a25f0a04SGreg Roach
122a25f0a04SGreg Roach    /**
1238f5f5da8SGreg Roach     * Get the media files for this media object
1248f5f5da8SGreg Roach     *
12554c7f8dfSGreg Roach     * @return Collection
1268f5f5da8SGreg Roach     */
127e778de49SGreg Roach    public function mediaFiles(): Collection
128c1010edaSGreg Roach    {
12939ca88baSGreg Roach        return $this->facts(['FILE'])
130e778de49SGreg Roach            ->map(function (Fact $fact): MediaFile {
131e778de49SGreg Roach                return new MediaFile($fact->gedcom(), $this);
132e778de49SGreg Roach            });
1338f5f5da8SGreg Roach    }
1348f5f5da8SGreg Roach
1358f5f5da8SGreg Roach    /**
13664b90bf1SGreg Roach     * Get the first media file that contains an image.
1378f5f5da8SGreg Roach     *
1388f5f5da8SGreg Roach     * @return MediaFile|null
1398f5f5da8SGreg Roach     */
140e364afe4SGreg Roach    public function firstImageFile(): ?MediaFile
141c1010edaSGreg Roach    {
1428f5f5da8SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
1434a9f750fSGreg Roach            if ($media_file->isImage()) {
1448f5f5da8SGreg Roach                return $media_file;
1458f5f5da8SGreg Roach            }
1468f5f5da8SGreg Roach        }
1478f5f5da8SGreg Roach
1488f5f5da8SGreg Roach        return null;
1498f5f5da8SGreg Roach    }
1508f5f5da8SGreg Roach
1518f5f5da8SGreg Roach    /**
152a25f0a04SGreg Roach     * Get the first note attached to this media object
153a25f0a04SGreg Roach     *
154ff166e64SGreg Roach     * @return string
155a25f0a04SGreg Roach     */
156e364afe4SGreg Roach    public function getNote(): string
157c1010edaSGreg Roach    {
158820b62dfSGreg Roach        $fact = $this->facts(['NOTE'])->first();
159820b62dfSGreg Roach
160e24444eeSGreg Roach        if ($fact instanceof Fact) {
161e24444eeSGreg Roach            // Link to note object
162dc124885SGreg Roach            $note = $fact->target();
163e24444eeSGreg Roach            if ($note instanceof Note) {
164e24444eeSGreg Roach                return $note->getNote();
165a25f0a04SGreg Roach            }
166a25f0a04SGreg Roach
167e24444eeSGreg Roach            // Inline note
16884586c02SGreg Roach            return $fact->value();
169a25f0a04SGreg Roach        }
170b2ce94c6SRico Sonntag
171b2ce94c6SRico Sonntag        return '';
172a25f0a04SGreg Roach    }
173a25f0a04SGreg Roach
174a25f0a04SGreg Roach    /**
17576692c8bSGreg Roach     * Extract names from the GEDCOM record.
176c7ff4153SGreg Roach     *
177c7ff4153SGreg Roach     * @return void
17876692c8bSGreg Roach     */
179e364afe4SGreg Roach    public function extractNames(): void
180c1010edaSGreg Roach    {
18164b90bf1SGreg Roach        $names = [];
18264b90bf1SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
18364b90bf1SGreg Roach            $names[] = $media_file->title();
18492d61e73SGreg Roach        }
18592d61e73SGreg Roach        foreach ($this->mediaFiles() as $media_file) {
18664b90bf1SGreg Roach            $names[] = $media_file->filename();
18764b90bf1SGreg Roach        }
18864b90bf1SGreg Roach        $names = array_filter(array_unique($names));
18964b90bf1SGreg Roach
19054c1ab5eSGreg Roach        if ($names === []) {
19164b90bf1SGreg Roach            $names[] = $this->getFallBackName();
19264b90bf1SGreg Roach        }
19364b90bf1SGreg Roach
19464b90bf1SGreg Roach        foreach ($names as $name) {
19576f666f4SGreg Roach            $this->addName(static::RECORD_TYPE, $name, '');
19664b90bf1SGreg Roach        }
197a25f0a04SGreg Roach    }
198a25f0a04SGreg Roach
19976692c8bSGreg Roach    /**
20076692c8bSGreg Roach     * This function should be redefined in derived classes to show any major
20176692c8bSGreg Roach     * identifying characteristics of this record.
20276692c8bSGreg Roach     *
20376692c8bSGreg Roach     * @return string
20476692c8bSGreg Roach     */
2058f53f488SRico Sonntag    public function formatListDetails(): string
206c1010edaSGreg Roach    {
207a25f0a04SGreg Roach        ob_start();
208f4afa648SGreg Roach        FunctionsPrintFacts::printMediaLinks($this->tree(), '1 OBJE @' . $this->xref() . '@', 1);
209a25f0a04SGreg Roach
210a25f0a04SGreg Roach        return ob_get_clean();
211a25f0a04SGreg Roach    }
21291eef4ffSGreg Roach
21391eef4ffSGreg Roach    /**
21491eef4ffSGreg Roach     * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
21591eef4ffSGreg Roach     *
21691eef4ffSGreg Roach     * @param int      $width      Pixels
21791eef4ffSGreg Roach     * @param int      $height     Pixels
21891eef4ffSGreg Roach     * @param string   $fit        "crop" or "contain"
21991eef4ffSGreg Roach     * @param string[] $attributes Additional HTML attributes
22091eef4ffSGreg Roach     *
22191eef4ffSGreg Roach     * @return string
22291eef4ffSGreg Roach     */
2238f53f488SRico Sonntag    public function displayImage($width, $height, $fit, $attributes = []): string
224c1010edaSGreg Roach    {
22591eef4ffSGreg Roach        // Display the first image
22691eef4ffSGreg Roach        foreach ($this->mediaFiles() as $media_file) {
22791eef4ffSGreg Roach            if ($media_file->isImage()) {
22891eef4ffSGreg Roach                return $media_file->displayImage($width, $height, $fit, $attributes);
22991eef4ffSGreg Roach            }
23091eef4ffSGreg Roach        }
23191eef4ffSGreg Roach
23291eef4ffSGreg Roach        // Display the first file of any type
23391eef4ffSGreg Roach        foreach ($this->mediaFiles() as $media_file) {
23491eef4ffSGreg Roach            return $media_file->displayImage($width, $height, $fit, $attributes);
23591eef4ffSGreg Roach        }
23691eef4ffSGreg Roach
23791eef4ffSGreg Roach        // No image?
23891eef4ffSGreg Roach        return '';
23991eef4ffSGreg Roach    }
240a25f0a04SGreg Roach}
241