1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 1776692c8bSGreg Roach 183d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts; 19a25f0a04SGreg Roach 20a25f0a04SGreg Roach/** 2176692c8bSGreg Roach * A GEDCOM media (OBJE) object. 22a25f0a04SGreg Roach */ 23c1010edaSGreg Roachclass Media extends GedcomRecord 24c1010edaSGreg Roach{ 25a25f0a04SGreg Roach const RECORD_TYPE = 'OBJE'; 26225e381fSGreg Roach const ROUTE_NAME = 'media'; 27a25f0a04SGreg Roach 2876692c8bSGreg Roach /** 29e71ef9d2SGreg Roach * Get an instance of a media object. For single records, 30e71ef9d2SGreg Roach * we just receive the XREF. For bulk records (such as lists 31e71ef9d2SGreg Roach * and search results) we can receive the GEDCOM data as well. 32e71ef9d2SGreg Roach * 33e71ef9d2SGreg Roach * @param string $xref 34e71ef9d2SGreg Roach * @param Tree $tree 35e71ef9d2SGreg Roach * @param string|null $gedcom 36e71ef9d2SGreg Roach * 37e71ef9d2SGreg Roach * @throws \Exception 38e71ef9d2SGreg Roach * 39e71ef9d2SGreg Roach * @return Media|null 40e71ef9d2SGreg Roach */ 41c1010edaSGreg Roach public static function getInstance($xref, Tree $tree, $gedcom = null) 42c1010edaSGreg Roach { 43e71ef9d2SGreg Roach $record = parent::getInstance($xref, $tree, $gedcom); 44e71ef9d2SGreg Roach 45e71ef9d2SGreg Roach if ($record instanceof Media) { 46e71ef9d2SGreg Roach return $record; 47e71ef9d2SGreg Roach } else { 48e71ef9d2SGreg Roach return null; 49e71ef9d2SGreg Roach } 50e71ef9d2SGreg Roach } 51e71ef9d2SGreg Roach 52e71ef9d2SGreg Roach /** 5376692c8bSGreg Roach * Each object type may have its own special rules, and re-implement this function. 5476692c8bSGreg Roach * 5576692c8bSGreg Roach * @param int $access_level 5676692c8bSGreg Roach * 5776692c8bSGreg Roach * @return bool 5876692c8bSGreg Roach */ 598f53f488SRico Sonntag protected function canShowByType($access_level): bool 60c1010edaSGreg Roach { 61a25f0a04SGreg Roach // Hide media objects if they are attached to private records 62a25f0a04SGreg Roach $linked_ids = Database::prepare( 63a25f0a04SGreg Roach "SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?" 6413abd6f3SGreg Roach )->execute([ 65c1010edaSGreg Roach $this->xref, 66c1010edaSGreg Roach $this->tree->getTreeId(), 6713abd6f3SGreg Roach ])->fetchOneColumn(); 68a25f0a04SGreg Roach foreach ($linked_ids as $linked_id) { 6924ec66ceSGreg Roach $linked_record = GedcomRecord::getInstance($linked_id, $this->tree); 70a25f0a04SGreg Roach if ($linked_record && !$linked_record->canShow($access_level)) { 71a25f0a04SGreg Roach return false; 72a25f0a04SGreg Roach } 73a25f0a04SGreg Roach } 74a25f0a04SGreg Roach 75a25f0a04SGreg Roach // ... otherwise apply default behaviour 76a25f0a04SGreg Roach return parent::canShowByType($access_level); 77a25f0a04SGreg Roach } 78a25f0a04SGreg Roach 7976692c8bSGreg Roach /** 8076692c8bSGreg Roach * Fetch data from the database 8176692c8bSGreg Roach * 8276692c8bSGreg Roach * @param string $xref 8376692c8bSGreg Roach * @param int $tree_id 8476692c8bSGreg Roach * 8576692c8bSGreg Roach * @return null|string 8676692c8bSGreg Roach */ 87c1010edaSGreg Roach protected static function fetchGedcomRecord($xref, $tree_id) 88c1010edaSGreg Roach { 8964d9078aSGreg Roach return Database::prepare( 9064d9078aSGreg Roach "SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id" 9113abd6f3SGreg Roach )->execute([ 9264d9078aSGreg Roach 'xref' => $xref, 9364d9078aSGreg Roach 'tree_id' => $tree_id, 9413abd6f3SGreg Roach ])->fetchOne(); 95a25f0a04SGreg Roach } 96a25f0a04SGreg Roach 97a25f0a04SGreg Roach /** 988f5f5da8SGreg Roach * Get the media files for this media object 998f5f5da8SGreg Roach * 1008f5f5da8SGreg Roach * @return MediaFile[] 1018f5f5da8SGreg Roach */ 102c1010edaSGreg Roach public function mediaFiles(): array 103c1010edaSGreg Roach { 1048f5f5da8SGreg Roach $media_files = []; 1058f5f5da8SGreg Roach 1068f5f5da8SGreg Roach foreach ($this->getFacts('FILE') as $fact) { 1078f5f5da8SGreg Roach $media_files[] = new MediaFile($fact->getGedcom(), $this); 1088f5f5da8SGreg Roach } 1098f5f5da8SGreg Roach 1108f5f5da8SGreg Roach return $media_files; 1118f5f5da8SGreg Roach } 1128f5f5da8SGreg Roach 1138f5f5da8SGreg Roach /** 11464b90bf1SGreg Roach * Get the first media file that contains an image. 1158f5f5da8SGreg Roach * 1168f5f5da8SGreg Roach * @return MediaFile|null 1178f5f5da8SGreg Roach */ 118c1010edaSGreg Roach public function firstImageFile() 119c1010edaSGreg Roach { 1208f5f5da8SGreg Roach foreach ($this->mediaFiles() as $media_file) { 1214a9f750fSGreg Roach if ($media_file->isImage()) { 1228f5f5da8SGreg Roach return $media_file; 1238f5f5da8SGreg Roach } 1248f5f5da8SGreg Roach } 1258f5f5da8SGreg Roach 1268f5f5da8SGreg Roach return null; 1278f5f5da8SGreg Roach } 1288f5f5da8SGreg Roach 1298f5f5da8SGreg Roach /** 130a25f0a04SGreg Roach * Get the first note attached to this media object 131a25f0a04SGreg Roach * 132a25f0a04SGreg Roach * @return null|string 133a25f0a04SGreg Roach */ 134c1010edaSGreg Roach public function getNote() 135c1010edaSGreg Roach { 136a25f0a04SGreg Roach $note = $this->getFirstFact('NOTE'); 137a25f0a04SGreg Roach if ($note) { 138a25f0a04SGreg Roach $text = $note->getValue(); 139a25f0a04SGreg Roach if (preg_match('/^@' . WT_REGEX_XREF . '@$/', $text)) { 140a25f0a04SGreg Roach $text = $note->getTarget()->getNote(); 141a25f0a04SGreg Roach } 142a25f0a04SGreg Roach 143a25f0a04SGreg Roach return $text; 144a25f0a04SGreg Roach } else { 145a25f0a04SGreg Roach return ''; 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach } 148a25f0a04SGreg Roach 149a25f0a04SGreg Roach /** 15076692c8bSGreg Roach * Extract names from the GEDCOM record. 151*c7ff4153SGreg Roach * 152*c7ff4153SGreg Roach * @return void 15376692c8bSGreg Roach */ 154c1010edaSGreg Roach public function extractNames() 155c1010edaSGreg Roach { 15664b90bf1SGreg Roach $names = []; 15764b90bf1SGreg Roach foreach ($this->mediaFiles() as $media_file) { 15864b90bf1SGreg Roach $names[] = $media_file->title(); 15992d61e73SGreg Roach } 16092d61e73SGreg Roach foreach ($this->mediaFiles() as $media_file) { 16164b90bf1SGreg Roach $names[] = $media_file->filename(); 16264b90bf1SGreg Roach } 16364b90bf1SGreg Roach $names = array_filter(array_unique($names)); 16464b90bf1SGreg Roach 16564b90bf1SGreg Roach if (empty($names)) { 16664b90bf1SGreg Roach $names[] = $this->getFallBackName(); 16764b90bf1SGreg Roach } 16864b90bf1SGreg Roach 16964b90bf1SGreg Roach foreach ($names as $name) { 17064b90bf1SGreg Roach $this->addName(static::RECORD_TYPE, $name, null); 17164b90bf1SGreg Roach } 172a25f0a04SGreg Roach } 173a25f0a04SGreg Roach 17476692c8bSGreg Roach /** 17576692c8bSGreg Roach * This function should be redefined in derived classes to show any major 17676692c8bSGreg Roach * identifying characteristics of this record. 17776692c8bSGreg Roach * 17876692c8bSGreg Roach * @return string 17976692c8bSGreg Roach */ 1808f53f488SRico Sonntag public function formatListDetails(): string 181c1010edaSGreg Roach { 182a25f0a04SGreg Roach ob_start(); 183911f5683SGreg Roach FunctionsPrintFacts::printMediaLinks($this->getTree(), '1 OBJE @' . $this->getXref() . '@', 1); 184a25f0a04SGreg Roach 185a25f0a04SGreg Roach return ob_get_clean(); 186a25f0a04SGreg Roach } 18791eef4ffSGreg Roach 18891eef4ffSGreg Roach /** 18991eef4ffSGreg Roach * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. 19091eef4ffSGreg Roach * 19191eef4ffSGreg Roach * @param int $width Pixels 19291eef4ffSGreg Roach * @param int $height Pixels 19391eef4ffSGreg Roach * @param string $fit "crop" or "contain" 19491eef4ffSGreg Roach * @param string[] $attributes Additional HTML attributes 19591eef4ffSGreg Roach * 19691eef4ffSGreg Roach * @return string 19791eef4ffSGreg Roach */ 1988f53f488SRico Sonntag public function displayImage($width, $height, $fit, $attributes = []): string 199c1010edaSGreg Roach { 20091eef4ffSGreg Roach // Display the first image 20191eef4ffSGreg Roach foreach ($this->mediaFiles() as $media_file) { 20291eef4ffSGreg Roach if ($media_file->isImage()) { 20391eef4ffSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 20491eef4ffSGreg Roach } 20591eef4ffSGreg Roach } 20691eef4ffSGreg Roach 20791eef4ffSGreg Roach // Display the first file of any type 20891eef4ffSGreg Roach foreach ($this->mediaFiles() as $media_file) { 20991eef4ffSGreg Roach return $media_file->displayImage($width, $height, $fit, $attributes); 21091eef4ffSGreg Roach } 21191eef4ffSGreg Roach 21291eef4ffSGreg Roach // No image? 21391eef4ffSGreg Roach return ''; 21491eef4ffSGreg Roach } 215a25f0a04SGreg Roach} 216