xref: /webtrees/app/Media.php (revision 1062a1429914c995339f502856821457aa975a5a)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4*1062a142SGreg 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';
25a25f0a04SGreg Roach	const URL_PREFIX  = 'mediaviewer.php?mid=';
26a25f0a04SGreg Roach
2776692c8bSGreg Roach	/**
2876692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
2976692c8bSGreg Roach	 *
3076692c8bSGreg Roach	 * @param int $access_level
3176692c8bSGreg Roach	 *
3276692c8bSGreg Roach	 * @return bool
3376692c8bSGreg Roach	 */
34a25f0a04SGreg Roach	protected function canShowByType($access_level) {
35a25f0a04SGreg Roach		// Hide media objects if they are attached to private records
36a25f0a04SGreg Roach		$linked_ids = Database::prepare(
37a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?"
3813abd6f3SGreg Roach		)->execute([
39cbc1590aSGreg Roach			$this->xref, $this->tree->getTreeId(),
4013abd6f3SGreg Roach		])->fetchOneColumn();
41a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
4224ec66ceSGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
43a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
44a25f0a04SGreg Roach				return false;
45a25f0a04SGreg Roach			}
46a25f0a04SGreg Roach		}
47a25f0a04SGreg Roach
48a25f0a04SGreg Roach		// ... otherwise apply default behaviour
49a25f0a04SGreg Roach		return parent::canShowByType($access_level);
50a25f0a04SGreg Roach	}
51a25f0a04SGreg Roach
5276692c8bSGreg Roach	/**
5376692c8bSGreg Roach	 * Fetch data from the database
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @param string $xref
5676692c8bSGreg Roach	 * @param int    $tree_id
5776692c8bSGreg Roach	 *
5876692c8bSGreg Roach	 * @return null|string
5976692c8bSGreg Roach	 */
6064d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
6164d9078aSGreg Roach		return Database::prepare(
6264d9078aSGreg Roach			"SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id"
6313abd6f3SGreg Roach		)->execute([
6464d9078aSGreg Roach			'xref'    => $xref,
6564d9078aSGreg Roach			'tree_id' => $tree_id,
6613abd6f3SGreg Roach		])->fetchOne();
67a25f0a04SGreg Roach	}
68a25f0a04SGreg Roach
69a25f0a04SGreg Roach	/**
708f5f5da8SGreg Roach	 * Get the media files for this media object
718f5f5da8SGreg Roach	 *
728f5f5da8SGreg Roach	 * @return MediaFile[]
738f5f5da8SGreg Roach	 */
748f5f5da8SGreg Roach	public function mediaFiles(): array {
758f5f5da8SGreg Roach		$media_files = [];
768f5f5da8SGreg Roach
778f5f5da8SGreg Roach		foreach ($this->getFacts('FILE') as $fact) {
788f5f5da8SGreg Roach			$media_files[] = new MediaFile($fact->getGedcom(), $this);
798f5f5da8SGreg Roach		}
808f5f5da8SGreg Roach
818f5f5da8SGreg Roach		return $media_files;
828f5f5da8SGreg Roach	}
838f5f5da8SGreg Roach
848f5f5da8SGreg Roach	/**
8564b90bf1SGreg Roach	 * Get the first media file that contains an image.
868f5f5da8SGreg Roach	 *
878f5f5da8SGreg Roach	 * @return MediaFile|null
888f5f5da8SGreg Roach	 */
8964b90bf1SGreg Roach	public function firstImageFile() {
908f5f5da8SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
914a9f750fSGreg Roach			if ($media_file->isImage()) {
928f5f5da8SGreg Roach				return $media_file;
938f5f5da8SGreg Roach			}
948f5f5da8SGreg Roach		}
958f5f5da8SGreg Roach
968f5f5da8SGreg Roach		return null;
978f5f5da8SGreg Roach	}
988f5f5da8SGreg Roach
998f5f5da8SGreg Roach	/**
100a25f0a04SGreg Roach	 * Get the first note attached to this media object
101a25f0a04SGreg Roach	 *
102a25f0a04SGreg Roach	 * @return null|string
103a25f0a04SGreg Roach	 */
104a25f0a04SGreg Roach	public function getNote() {
105a25f0a04SGreg Roach		$note = $this->getFirstFact('NOTE');
106a25f0a04SGreg Roach		if ($note) {
107a25f0a04SGreg Roach			$text = $note->getValue();
108a25f0a04SGreg Roach			if (preg_match('/^@' . WT_REGEX_XREF . '@$/', $text)) {
109a25f0a04SGreg Roach				$text = $note->getTarget()->getNote();
110a25f0a04SGreg Roach			}
111a25f0a04SGreg Roach
112a25f0a04SGreg Roach			return $text;
113a25f0a04SGreg Roach		} else {
114a25f0a04SGreg Roach			return '';
115a25f0a04SGreg Roach		}
116a25f0a04SGreg Roach	}
117a25f0a04SGreg Roach
118a25f0a04SGreg Roach	/**
11976692c8bSGreg Roach	 * Extract names from the GEDCOM record.
12076692c8bSGreg Roach	 */
121a25f0a04SGreg Roach	public function extractNames() {
12264b90bf1SGreg Roach		$names = [];
12364b90bf1SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
12464b90bf1SGreg Roach			$names[] = $media_file->title();
12592d61e73SGreg Roach		}
12692d61e73SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
12764b90bf1SGreg Roach			$names[] = $media_file->filename();
12864b90bf1SGreg Roach		}
12964b90bf1SGreg Roach		$names = array_filter(array_unique($names));
13064b90bf1SGreg Roach
13164b90bf1SGreg Roach		if (empty($names)) {
13264b90bf1SGreg Roach			$names[] = $this->getFallBackName();
13364b90bf1SGreg Roach		}
13464b90bf1SGreg Roach
13564b90bf1SGreg Roach		foreach ($names as $name) {
13664b90bf1SGreg Roach			$this->addName(static::RECORD_TYPE, $name, null);
13764b90bf1SGreg Roach		}
138a25f0a04SGreg Roach	}
139a25f0a04SGreg Roach
14076692c8bSGreg Roach	/**
14176692c8bSGreg Roach	 * This function should be redefined in derived classes to show any major
14276692c8bSGreg Roach	 * identifying characteristics of this record.
14376692c8bSGreg Roach	 *
14476692c8bSGreg Roach	 * @return string
14576692c8bSGreg Roach	 */
146a25f0a04SGreg Roach	public function formatListDetails() {
147a25f0a04SGreg Roach		ob_start();
1483d7a8a4cSGreg Roach		FunctionsPrintFacts::printMediaLinks('1 OBJE @' . $this->getXref() . '@', 1);
149a25f0a04SGreg Roach
150a25f0a04SGreg Roach		return ob_get_clean();
151a25f0a04SGreg Roach	}
15291eef4ffSGreg Roach
15391eef4ffSGreg Roach	/**
15491eef4ffSGreg Roach	 * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
15591eef4ffSGreg Roach	 *
15691eef4ffSGreg Roach	 * @param int      $width      Pixels
15791eef4ffSGreg Roach	 * @param int      $height     Pixels
15891eef4ffSGreg Roach	 * @param string   $fit        "crop" or "contain"
15991eef4ffSGreg Roach	 * @param string[] $attributes Additional HTML attributes
16091eef4ffSGreg Roach	 *
16191eef4ffSGreg Roach	 * @return string
16291eef4ffSGreg Roach	 */
16391eef4ffSGreg Roach	public function displayImage($width, $height, $fit, $attributes = []) {
16491eef4ffSGreg Roach		// Display the first image
16591eef4ffSGreg Roach		foreach ($this->mediaFiles() as $media_file) {
16691eef4ffSGreg Roach			if ($media_file->isImage()) {
16791eef4ffSGreg Roach				return $media_file->displayImage($width, $height, $fit, $attributes);
16891eef4ffSGreg Roach			}
16991eef4ffSGreg Roach		}
17091eef4ffSGreg Roach
17191eef4ffSGreg Roach		// Display the first file of any type
17291eef4ffSGreg Roach		foreach ($this->mediaFiles() as $media_file) {
17391eef4ffSGreg Roach			return $media_file->displayImage($width, $height, $fit, $attributes);
17491eef4ffSGreg Roach		}
17591eef4ffSGreg Roach
17691eef4ffSGreg Roach		// No image?
17791eef4ffSGreg Roach		return '';
17891eef4ffSGreg Roach	}
179a25f0a04SGreg Roach}
180