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