1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20use Closure; 21use Fisharebest\Webtrees\Functions\FunctionsPrintFacts; 22use Illuminate\Database\Capsule\Manager as DB; 23use stdClass; 24 25/** 26 * A GEDCOM media (OBJE) object. 27 */ 28class Media extends GedcomRecord 29{ 30 public const RECORD_TYPE = 'OBJE'; 31 32 protected const ROUTE_NAME = 'media'; 33 34 /** 35 * A closure which will create a record from a database row. 36 * 37 * @param Tree $tree 38 * 39 * @return Closure 40 */ 41 public static function rowMapper(Tree $tree): Closure 42 { 43 return function (stdClass $row) use ($tree): Media { 44 return Media::getInstance($row->m_id, $tree, $row->m_gedcom); 45 }; 46 } 47 48 /** 49 * Get an instance of a media object. For single records, 50 * we just receive the XREF. For bulk records (such as lists 51 * and search results) we can receive the GEDCOM data as well. 52 * 53 * @param string $xref 54 * @param Tree $tree 55 * @param string|null $gedcom 56 * 57 * @throws \Exception 58 * 59 * @return Media|null 60 */ 61 public static function getInstance(string $xref, Tree $tree, string $gedcom = null) 62 { 63 $record = parent::getInstance($xref, $tree, $gedcom); 64 65 if ($record instanceof Media) { 66 return $record; 67 } 68 69 return null; 70 } 71 72 /** 73 * Each object type may have its own special rules, and re-implement this function. 74 * 75 * @param int $access_level 76 * 77 * @return bool 78 */ 79 protected function canShowByType(int $access_level): bool 80 { 81 // Hide media objects if they are attached to private records 82 $linked_ids = DB::table('link') 83 ->where('l_file', '=', $this->tree->id()) 84 ->where('l_to', '=', $this->xref) 85 ->pluck('l_from'); 86 87 foreach ($linked_ids as $linked_id) { 88 $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 89 if ($linked_record && !$linked_record->canShow($access_level)) { 90 return false; 91 } 92 } 93 94 // ... otherwise apply default behaviour 95 return parent::canShowByType($access_level); 96 } 97 98 /** 99 * Fetch data from the database 100 * 101 * @param string $xref 102 * @param int $tree_id 103 * 104 * @return null|string 105 */ 106 protected static function fetchGedcomRecord(string $xref, int $tree_id) 107 { 108 return DB::table('media') 109 ->where('m_id', '=', $xref) 110 ->where('m_file', '=', $tree_id) 111 ->value('m_gedcom'); 112 } 113 114 /** 115 * Get the media files for this media object 116 * 117 * @return MediaFile[] 118 */ 119 public function mediaFiles(): array 120 { 121 $media_files = []; 122 123 foreach ($this->facts(['FILE']) as $fact) { 124 $media_files[] = new MediaFile($fact->gedcom(), $this); 125 } 126 127 return $media_files; 128 } 129 130 /** 131 * Get the first media file that contains an image. 132 * 133 * @return MediaFile|null 134 */ 135 public function firstImageFile() 136 { 137 foreach ($this->mediaFiles() as $media_file) { 138 if ($media_file->isImage()) { 139 return $media_file; 140 } 141 } 142 143 return null; 144 } 145 146 /** 147 * Get the first note attached to this media object 148 * 149 * @return string 150 */ 151 public function getNote() 152 { 153 $fact = $this->getFirstFact('NOTE'); 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() 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 (empty($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