xref: /webtrees/app/Media.php (revision 911f5683b68340b4bad2bbca6e1ab7a3a15f46a7)
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 */
23a25f0a04SGreg Roachclass Media extends GedcomRecord {
24a25f0a04SGreg Roach	const RECORD_TYPE = 'OBJE';
25225e381fSGreg Roach	const ROUTE_NAME  = 'media';
26a25f0a04SGreg Roach
2776692c8bSGreg Roach	/**
28e71ef9d2SGreg Roach	 * Get an instance of a media object. For single records,
29e71ef9d2SGreg Roach	 * we just receive the XREF. For bulk records (such as lists
30e71ef9d2SGreg Roach	 * and search results) we can receive the GEDCOM data as well.
31e71ef9d2SGreg Roach	 *
32e71ef9d2SGreg Roach	 * @param string      $xref
33e71ef9d2SGreg Roach	 * @param Tree        $tree
34e71ef9d2SGreg Roach	 * @param string|null $gedcom
35e71ef9d2SGreg Roach	 *
36e71ef9d2SGreg Roach	 * @throws \Exception
37e71ef9d2SGreg Roach	 *
38e71ef9d2SGreg Roach	 * @return Media|null
39e71ef9d2SGreg Roach	 */
40e71ef9d2SGreg Roach	public static function getInstance($xref, Tree $tree, $gedcom = null) {
41e71ef9d2SGreg Roach		$record = parent::getInstance($xref, $tree, $gedcom);
42e71ef9d2SGreg Roach
43e71ef9d2SGreg Roach		if ($record instanceof Media) {
44e71ef9d2SGreg Roach			return $record;
45e71ef9d2SGreg Roach		} else {
46e71ef9d2SGreg Roach			return null;
47e71ef9d2SGreg Roach		}
48e71ef9d2SGreg Roach	}
49e71ef9d2SGreg Roach
50e71ef9d2SGreg Roach	/**
5176692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
5276692c8bSGreg Roach	 *
5376692c8bSGreg Roach	 * @param int $access_level
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @return bool
5676692c8bSGreg Roach	 */
57a25f0a04SGreg Roach	protected function canShowByType($access_level) {
58a25f0a04SGreg Roach		// Hide media objects if they are attached to private records
59a25f0a04SGreg Roach		$linked_ids = Database::prepare(
60a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?"
6113abd6f3SGreg Roach		)->execute([
62cbc1590aSGreg Roach			$this->xref, $this->tree->getTreeId(),
6313abd6f3SGreg Roach		])->fetchOneColumn();
64a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
6524ec66ceSGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
66a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
67a25f0a04SGreg Roach				return false;
68a25f0a04SGreg Roach			}
69a25f0a04SGreg Roach		}
70a25f0a04SGreg Roach
71a25f0a04SGreg Roach		// ... otherwise apply default behaviour
72a25f0a04SGreg Roach		return parent::canShowByType($access_level);
73a25f0a04SGreg Roach	}
74a25f0a04SGreg Roach
7576692c8bSGreg Roach	/**
7676692c8bSGreg Roach	 * Fetch data from the database
7776692c8bSGreg Roach	 *
7876692c8bSGreg Roach	 * @param string $xref
7976692c8bSGreg Roach	 * @param int    $tree_id
8076692c8bSGreg Roach	 *
8176692c8bSGreg Roach	 * @return null|string
8276692c8bSGreg Roach	 */
8364d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
8464d9078aSGreg Roach		return Database::prepare(
8564d9078aSGreg Roach			"SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id"
8613abd6f3SGreg Roach		)->execute([
8764d9078aSGreg Roach			'xref'    => $xref,
8864d9078aSGreg Roach			'tree_id' => $tree_id,
8913abd6f3SGreg Roach		])->fetchOne();
90a25f0a04SGreg Roach	}
91a25f0a04SGreg Roach
92a25f0a04SGreg Roach	/**
938f5f5da8SGreg Roach	 * Get the media files for this media object
948f5f5da8SGreg Roach	 *
958f5f5da8SGreg Roach	 * @return MediaFile[]
968f5f5da8SGreg Roach	 */
978f5f5da8SGreg Roach	public function mediaFiles(): array {
988f5f5da8SGreg Roach		$media_files = [];
998f5f5da8SGreg Roach
1008f5f5da8SGreg Roach		foreach ($this->getFacts('FILE') as $fact) {
1018f5f5da8SGreg Roach			$media_files[] = new MediaFile($fact->getGedcom(), $this);
1028f5f5da8SGreg Roach		}
1038f5f5da8SGreg Roach
1048f5f5da8SGreg Roach		return $media_files;
1058f5f5da8SGreg Roach	}
1068f5f5da8SGreg Roach
1078f5f5da8SGreg Roach	/**
10864b90bf1SGreg Roach	 * Get the first media file that contains an image.
1098f5f5da8SGreg Roach	 *
1108f5f5da8SGreg Roach	 * @return MediaFile|null
1118f5f5da8SGreg Roach	 */
11264b90bf1SGreg Roach	public function firstImageFile() {
1138f5f5da8SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
1144a9f750fSGreg Roach			if ($media_file->isImage()) {
1158f5f5da8SGreg Roach				return $media_file;
1168f5f5da8SGreg Roach			}
1178f5f5da8SGreg Roach		}
1188f5f5da8SGreg Roach
1198f5f5da8SGreg Roach		return null;
1208f5f5da8SGreg Roach	}
1218f5f5da8SGreg Roach
1228f5f5da8SGreg Roach	/**
123a25f0a04SGreg Roach	 * Get the first note attached to this media object
124a25f0a04SGreg Roach	 *
125a25f0a04SGreg Roach	 * @return null|string
126a25f0a04SGreg Roach	 */
127a25f0a04SGreg Roach	public function getNote() {
128a25f0a04SGreg Roach		$note = $this->getFirstFact('NOTE');
129a25f0a04SGreg Roach		if ($note) {
130a25f0a04SGreg Roach			$text = $note->getValue();
131a25f0a04SGreg Roach			if (preg_match('/^@' . WT_REGEX_XREF . '@$/', $text)) {
132a25f0a04SGreg Roach				$text = $note->getTarget()->getNote();
133a25f0a04SGreg Roach			}
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach			return $text;
136a25f0a04SGreg Roach		} else {
137a25f0a04SGreg Roach			return '';
138a25f0a04SGreg Roach		}
139a25f0a04SGreg Roach	}
140a25f0a04SGreg Roach
141a25f0a04SGreg Roach	/**
14276692c8bSGreg Roach	 * Extract names from the GEDCOM record.
14376692c8bSGreg Roach	 */
144a25f0a04SGreg Roach	public function extractNames() {
14564b90bf1SGreg Roach		$names = [];
14664b90bf1SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
14764b90bf1SGreg Roach			$names[] = $media_file->title();
14892d61e73SGreg Roach		}
14992d61e73SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
15064b90bf1SGreg Roach			$names[] = $media_file->filename();
15164b90bf1SGreg Roach		}
15264b90bf1SGreg Roach		$names = array_filter(array_unique($names));
15364b90bf1SGreg Roach
15464b90bf1SGreg Roach		if (empty($names)) {
15564b90bf1SGreg Roach			$names[] = $this->getFallBackName();
15664b90bf1SGreg Roach		}
15764b90bf1SGreg Roach
15864b90bf1SGreg Roach		foreach ($names as $name) {
15964b90bf1SGreg Roach			$this->addName(static::RECORD_TYPE, $name, null);
16064b90bf1SGreg Roach		}
161a25f0a04SGreg Roach	}
162a25f0a04SGreg Roach
16376692c8bSGreg Roach	/**
16476692c8bSGreg Roach	 * This function should be redefined in derived classes to show any major
16576692c8bSGreg Roach	 * identifying characteristics of this record.
16676692c8bSGreg Roach	 *
16776692c8bSGreg Roach	 * @return string
16876692c8bSGreg Roach	 */
169a25f0a04SGreg Roach	public function formatListDetails() {
170a25f0a04SGreg Roach		ob_start();
171*911f5683SGreg Roach		FunctionsPrintFacts::printMediaLinks($this->getTree(), '1 OBJE @' . $this->getXref() . '@', 1);
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach		return ob_get_clean();
174a25f0a04SGreg Roach	}
17591eef4ffSGreg Roach
17691eef4ffSGreg Roach	/**
17791eef4ffSGreg Roach	 * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
17891eef4ffSGreg Roach	 *
17991eef4ffSGreg Roach	 * @param int      $width      Pixels
18091eef4ffSGreg Roach	 * @param int      $height     Pixels
18191eef4ffSGreg Roach	 * @param string   $fit        "crop" or "contain"
18291eef4ffSGreg Roach	 * @param string[] $attributes Additional HTML attributes
18391eef4ffSGreg Roach	 *
18491eef4ffSGreg Roach	 * @return string
18591eef4ffSGreg Roach	 */
18691eef4ffSGreg Roach	public function displayImage($width, $height, $fit, $attributes = []) {
18791eef4ffSGreg Roach		// Display the first image
18891eef4ffSGreg Roach		foreach ($this->mediaFiles() as $media_file) {
18991eef4ffSGreg Roach			if ($media_file->isImage()) {
19091eef4ffSGreg Roach				return $media_file->displayImage($width, $height, $fit, $attributes);
19191eef4ffSGreg Roach			}
19291eef4ffSGreg Roach		}
19391eef4ffSGreg Roach
19491eef4ffSGreg Roach		// Display the first file of any type
19591eef4ffSGreg Roach		foreach ($this->mediaFiles() as $media_file) {
19691eef4ffSGreg Roach			return $media_file->displayImage($width, $height, $fit, $attributes);
19791eef4ffSGreg Roach		}
19891eef4ffSGreg Roach
19991eef4ffSGreg Roach		// No image?
20091eef4ffSGreg Roach		return '';
20191eef4ffSGreg Roach	}
202a25f0a04SGreg Roach}
203