1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Closure; 23use Exception; 24use Fisharebest\Webtrees\Functions\FunctionsPrintFacts; 25use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Support\Collection; 28use stdClass; 29 30/** 31 * A GEDCOM media (OBJE) object. 32 */ 33class Media extends GedcomRecord 34{ 35 public const RECORD_TYPE = 'OBJE'; 36 37 protected const ROUTE_NAME = MediaPage::class; 38 39 /** 40 * A closure which will create a record from a database row. 41 * 42 * @param Tree $tree 43 * 44 * @return Closure 45 */ 46 public static function rowMapper(Tree $tree): Closure 47 { 48 return static function (stdClass $row) use ($tree): Media { 49 return Media::getInstance($row->m_id, $tree, $row->m_gedcom); 50 }; 51 } 52 53 /** 54 * Get an instance of a media object. For single records, 55 * we just receive the XREF. For bulk records (such as lists 56 * and search results) we can receive the GEDCOM data as well. 57 * 58 * @param string $xref 59 * @param Tree $tree 60 * @param string|null $gedcom 61 * 62 * @throws Exception 63 * 64 * @return Media|null 65 */ 66 public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self 67 { 68 $record = parent::getInstance($xref, $tree, $gedcom); 69 70 if ($record instanceof self) { 71 return $record; 72 } 73 74 return null; 75 } 76 77 /** 78 * Each object type may have its own special rules, and re-implement this function. 79 * 80 * @param int $access_level 81 * 82 * @return bool 83 */ 84 protected function canShowByType(int $access_level): bool 85 { 86 // Hide media objects if they are attached to private records 87 $linked_ids = DB::table('link') 88 ->where('l_file', '=', $this->tree->id()) 89 ->where('l_to', '=', $this->xref) 90 ->pluck('l_from'); 91 92 foreach ($linked_ids as $linked_id) { 93 $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 94 if ($linked_record && !$linked_record->canShow($access_level)) { 95 return false; 96 } 97 } 98 99 // ... otherwise apply default behavior 100 return parent::canShowByType($access_level); 101 } 102 103 /** 104 * Fetch data from the database 105 * 106 * @param string $xref 107 * @param int $tree_id 108 * 109 * @return string|null 110 */ 111 protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string 112 { 113 return DB::table('media') 114 ->where('m_id', '=', $xref) 115 ->where('m_file', '=', $tree_id) 116 ->value('m_gedcom'); 117 } 118 119 /** 120 * Get the media files for this media object 121 * 122 * @return Collection 123 */ 124 public function mediaFiles(): Collection 125 { 126 return $this->facts(['FILE']) 127 ->map(function (Fact $fact): MediaFile { 128 return new MediaFile($fact->gedcom(), $this); 129 }); 130 } 131 132 /** 133 * Get the first media file that contains an image. 134 * 135 * @return MediaFile|null 136 */ 137 public function firstImageFile(): ?MediaFile 138 { 139 foreach ($this->mediaFiles() as $media_file) { 140 if ($media_file->isImage()) { 141 return $media_file; 142 } 143 } 144 145 return null; 146 } 147 148 /** 149 * Get the first note attached to this media object 150 * 151 * @return string 152 */ 153 public function getNote(): string 154 { 155 $fact = $this->facts(['NOTE'])->first(); 156 157 if ($fact instanceof Fact) { 158 // Link to note object 159 $note = $fact->target(); 160 if ($note instanceof Note) { 161 return $note->getNote(); 162 } 163 164 // Inline note 165 return $fact->value(); 166 } 167 168 return ''; 169 } 170 171 /** 172 * Extract names from the GEDCOM record. 173 * 174 * @return void 175 */ 176 public function extractNames(): void 177 { 178 $names = []; 179 foreach ($this->mediaFiles() as $media_file) { 180 $names[] = $media_file->title(); 181 } 182 foreach ($this->mediaFiles() as $media_file) { 183 $names[] = $media_file->filename(); 184 } 185 $names = array_filter(array_unique($names)); 186 187 if ($names === []) { 188 $names[] = $this->getFallBackName(); 189 } 190 191 foreach ($names as $name) { 192 $this->addName(static::RECORD_TYPE, $name, ''); 193 } 194 } 195 196 /** 197 * This function should be redefined in derived classes to show any major 198 * identifying characteristics of this record. 199 * 200 * @return string 201 */ 202 public function formatListDetails(): string 203 { 204 ob_start(); 205 FunctionsPrintFacts::printMediaLinks($this->tree(), '1 OBJE @' . $this->xref() . '@', 1); 206 207 return ob_get_clean(); 208 } 209 210 /** 211 * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 212 * 213 * @param int $width Pixels 214 * @param int $height Pixels 215 * @param string $fit "crop" or "contain" 216 * @param string[] $attributes Additional HTML attributes 217 * 218 * @return string 219 */ 220 public function displayImage($width, $height, $fit, $attributes = []): string 221 { 222 // Display the first image 223 foreach ($this->mediaFiles() as $media_file) { 224 if ($media_file->isImage()) { 225 return $media_file->displayImage($width, $height, $fit, $attributes); 226 } 227 } 228 229 // Display the first file of any type 230 foreach ($this->mediaFiles() as $media_file) { 231 return $media_file->displayImage($width, $height, $fit, $attributes); 232 } 233 234 // No image? 235 return ''; 236 } 237} 238