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