1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5*89f7189bSGreg Roach * Copyright (C) 2021 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 15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 2176692c8bSGreg Roach 22886b77daSGreg Roachuse Closure; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts; 24d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaPage; 252e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 26e778de49SGreg Roachuse Illuminate\Support\Collection; 27a25f0a04SGreg Roach 28a25f0a04SGreg Roach/** 2976692c8bSGreg Roach * A GEDCOM media (OBJE) object. 30a25f0a04SGreg Roach */ 31c1010edaSGreg Roachclass Media extends GedcomRecord 32c1010edaSGreg Roach{ 3316d6367aSGreg Roach public const RECORD_TYPE = 'OBJE'; 3416d6367aSGreg Roach 35d7daee59SGreg Roach protected const ROUTE_NAME = MediaPage::class; 36a25f0a04SGreg Roach 3776692c8bSGreg Roach /** 38886b77daSGreg Roach * A closure which will create a record from a database row. 39886b77daSGreg Roach * 40bb03c9f0SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 - Use Factory::media() 41bb03c9f0SGreg 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 { 486b9cb339SGreg Roach return Registry::mediaFactory()->mapper($tree); 49886b77daSGreg Roach } 50886b77daSGreg Roach 51886b77daSGreg Roach /** 52e71ef9d2SGreg Roach * Get an instance of a media object. For single records, 53e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 54e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 55e71ef9d2SGreg Roach * 56bb03c9f0SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 - Use Factory::media() 57bb03c9f0SGreg Roach * 58e71ef9d2SGreg Roach * @param string $xref 59e71ef9d2SGreg Roach * @param Tree $tree 60e71ef9d2SGreg Roach * @param string|null $gedcom 61e71ef9d2SGreg Roach * 62e71ef9d2SGreg Roach * @return Media|null 63e71ef9d2SGreg Roach */ 64ffc0a61fSGreg Roach public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Media 65c1010edaSGreg Roach { 666b9cb339SGreg Roach return Registry::mediaFactory()->make($xref, $tree, $gedcom); 67e71ef9d2SGreg Roach } 68e71ef9d2SGreg Roach 69e71ef9d2SGreg Roach /** 7076692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 7176692c8bSGreg Roach * 7276692c8bSGreg Roach * @param int $access_level 7376692c8bSGreg Roach * 7476692c8bSGreg Roach * @return bool 7576692c8bSGreg Roach */ 7635584196SGreg Roach protected function canShowByType(int $access_level): bool 77c1010edaSGreg Roach { 78a25f0a04SGreg Roach // Hide media objects if they are attached to private records 7925366223SGreg Roach $linked_ids = DB::table('link') 8025366223SGreg Roach ->where('l_file', '=', $this->tree->id()) 8125366223SGreg Roach ->where('l_to', '=', $this->xref) 8225366223SGreg Roach ->pluck('l_from'); 8325366223SGreg Roach 84a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 856b9cb339SGreg Roach $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree); 86bb03c9f0SGreg Roach if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) { 87a25f0a04SGreg Roach return false; 88a25f0a04SGreg Roach } 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 91b3a775f6SGreg Roach // ... otherwise apply default behavior 92a25f0a04SGreg Roach return parent::canShowByType($access_level); 93a25f0a04SGreg Roach } 94a25f0a04SGreg Roach 9576692c8bSGreg Roach /** 968f5f5da8SGreg Roach * Get the media files for this media object 978f5f5da8SGreg Roach * 98b5c8fd7eSGreg Roach * @return Collection<MediaFile> 998f5f5da8SGreg Roach */ 100e778de49SGreg Roach public function mediaFiles(): Collection 101c1010edaSGreg Roach { 10239ca88baSGreg Roach return $this->facts(['FILE']) 103e778de49SGreg Roach ->map(function (Fact $fact): MediaFile { 104e778de49SGreg Roach return new MediaFile($fact->gedcom(), $this); 105e778de49SGreg Roach }); 1068f5f5da8SGreg Roach } 1078f5f5da8SGreg Roach 1088f5f5da8SGreg Roach /** 10964b90bf1SGreg Roach * Get the first media file that contains an image. 1108f5f5da8SGreg Roach * 1118f5f5da8SGreg Roach * @return MediaFile|null 1128f5f5da8SGreg Roach */ 113e364afe4SGreg Roach public function firstImageFile(): ?MediaFile 114c1010edaSGreg Roach { 11519d319e7SGreg Roach return $this->mediaFiles() 11619d319e7SGreg Roach ->first(static function (MediaFile $media_file): bool { 11719d319e7SGreg Roach return $media_file->isImage() && !$media_file->isExternal(); 11819d319e7SGreg Roach }); 1198f5f5da8SGreg Roach } 1208f5f5da8SGreg Roach 1218f5f5da8SGreg Roach /** 122a25f0a04SGreg Roach * Get the first note attached to this media object 123a25f0a04SGreg Roach * 124ff166e64SGreg Roach * @return string 125a25f0a04SGreg Roach */ 126e364afe4SGreg Roach public function getNote(): string 127c1010edaSGreg Roach { 128820b62dfSGreg Roach $fact = $this->facts(['NOTE'])->first(); 129820b62dfSGreg Roach 130e24444eeSGreg Roach if ($fact instanceof Fact) { 131e24444eeSGreg Roach // Link to note object 132dc124885SGreg Roach $note = $fact->target(); 133e24444eeSGreg Roach if ($note instanceof Note) { 134e24444eeSGreg Roach return $note->getNote(); 135a25f0a04SGreg Roach } 136a25f0a04SGreg Roach 137e24444eeSGreg Roach // Inline note 13884586c02SGreg Roach return $fact->value(); 139a25f0a04SGreg Roach } 140b2ce94c6SRico Sonntag 141b2ce94c6SRico Sonntag return ''; 142a25f0a04SGreg Roach } 143a25f0a04SGreg Roach 144a25f0a04SGreg Roach /** 14576692c8bSGreg Roach * Extract names from the GEDCOM record. 146c7ff4153SGreg Roach * 147c7ff4153SGreg Roach * @return void 14876692c8bSGreg Roach */ 149e364afe4SGreg Roach public function extractNames(): void 150c1010edaSGreg Roach { 15164b90bf1SGreg Roach $names = []; 15264b90bf1SGreg Roach foreach ($this->mediaFiles() as $media_file) { 15364b90bf1SGreg Roach $names[] = $media_file->title(); 15492d61e73SGreg Roach } 15592d61e73SGreg Roach foreach ($this->mediaFiles() as $media_file) { 15664b90bf1SGreg Roach $names[] = $media_file->filename(); 15764b90bf1SGreg Roach } 15864b90bf1SGreg Roach $names = array_filter(array_unique($names)); 15964b90bf1SGreg Roach 16054c1ab5eSGreg Roach if ($names === []) { 16164b90bf1SGreg Roach $names[] = $this->getFallBackName(); 16264b90bf1SGreg Roach } 16364b90bf1SGreg Roach 16464b90bf1SGreg Roach foreach ($names as $name) { 16576f666f4SGreg Roach $this->addName(static::RECORD_TYPE, $name, ''); 16664b90bf1SGreg Roach } 167a25f0a04SGreg Roach } 168a25f0a04SGreg Roach 16976692c8bSGreg Roach /** 17076692c8bSGreg Roach * This function should be redefined in derived classes to show any major 17176692c8bSGreg Roach * identifying characteristics of this record. 17276692c8bSGreg Roach * 17376692c8bSGreg Roach * @return string 17476692c8bSGreg Roach */ 1758f53f488SRico Sonntag public function formatListDetails(): string 176c1010edaSGreg Roach { 177a25f0a04SGreg Roach ob_start(); 178f4afa648SGreg Roach FunctionsPrintFacts::printMediaLinks($this->tree(), '1 OBJE @' . $this->xref() . '@', 1); 179a25f0a04SGreg Roach 180a25f0a04SGreg Roach return ob_get_clean(); 181a25f0a04SGreg Roach } 18291eef4ffSGreg Roach 18391eef4ffSGreg Roach /** 18491eef4ffSGreg Roach * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 18591eef4ffSGreg Roach * 18691eef4ffSGreg Roach * @param int $width Pixels 18791eef4ffSGreg Roach * @param int $height Pixels 18891eef4ffSGreg Roach * @param string $fit "crop" or "contain" 18991eef4ffSGreg Roach * @param string[] $attributes Additional HTML attributes 19091eef4ffSGreg Roach * 19191eef4ffSGreg Roach * @return string 19291eef4ffSGreg Roach */ 1938f53f488SRico Sonntag public function displayImage($width, $height, $fit, $attributes = []): string 194c1010edaSGreg Roach { 19591eef4ffSGreg Roach // Display the first image 19691eef4ffSGreg Roach foreach ($this->mediaFiles() as $media_file) { 19791eef4ffSGreg Roach if ($media_file->isImage()) { 19891eef4ffSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 19991eef4ffSGreg Roach } 20091eef4ffSGreg Roach } 20191eef4ffSGreg Roach 20291eef4ffSGreg Roach // Display the first file of any type 20319d319e7SGreg Roach $media_file = $this->mediaFiles()->first(); 20419d319e7SGreg Roach 20519d319e7SGreg Roach if ($media_file instanceof MediaFile) { 20691eef4ffSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 20791eef4ffSGreg Roach } 20891eef4ffSGreg Roach 20991eef4ffSGreg Roach // No image? 21091eef4ffSGreg Roach return ''; 21191eef4ffSGreg Roach } 2128091bfd1SGreg Roach 2138091bfd1SGreg Roach /** 2148091bfd1SGreg Roach * Lock the database row, to prevent concurrent edits. 2158091bfd1SGreg Roach */ 2168091bfd1SGreg Roach public function lock(): void 2178091bfd1SGreg Roach { 2188091bfd1SGreg Roach DB::table('media') 2198091bfd1SGreg Roach ->where('m_file', '=', $this->tree->id()) 2208091bfd1SGreg Roach ->where('m_id', '=', $this->xref()) 2218091bfd1SGreg Roach ->lockForUpdate() 2228091bfd1SGreg Roach ->get(); 2238091bfd1SGreg Roach } 224a25f0a04SGreg Roach} 225