xref: /webtrees/app/Media.php (revision 4a9f750fbbc5b926f3b5a1784b8c956ac6801284)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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;
19dce07401SGreg Roachuse League\Glide\Urls\UrlBuilderFactory;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
2276692c8bSGreg Roach * A GEDCOM media (OBJE) object.
23a25f0a04SGreg Roach */
24a25f0a04SGreg Roachclass Media extends GedcomRecord {
25a25f0a04SGreg Roach	const RECORD_TYPE = 'OBJE';
26a25f0a04SGreg Roach	const URL_PREFIX  = 'mediaviewer.php?mid=';
27a25f0a04SGreg Roach
2876692c8bSGreg Roach	/**
2976692c8bSGreg Roach	 * Each object type may have its own special rules, and re-implement this function.
3076692c8bSGreg Roach	 *
3176692c8bSGreg Roach	 * @param int $access_level
3276692c8bSGreg Roach	 *
3376692c8bSGreg Roach	 * @return bool
3476692c8bSGreg Roach	 */
35a25f0a04SGreg Roach	protected function canShowByType($access_level) {
36a25f0a04SGreg Roach		// Hide media objects if they are attached to private records
37a25f0a04SGreg Roach		$linked_ids = Database::prepare(
38a25f0a04SGreg Roach			"SELECT l_from FROM `##link` WHERE l_to = ? AND l_file = ?"
3913abd6f3SGreg Roach		)->execute([
40cbc1590aSGreg Roach			$this->xref, $this->tree->getTreeId(),
4113abd6f3SGreg Roach		])->fetchOneColumn();
42a25f0a04SGreg Roach		foreach ($linked_ids as $linked_id) {
4324ec66ceSGreg Roach			$linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
44a25f0a04SGreg Roach			if ($linked_record && !$linked_record->canShow($access_level)) {
45a25f0a04SGreg Roach				return false;
46a25f0a04SGreg Roach			}
47a25f0a04SGreg Roach		}
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach		// ... otherwise apply default behaviour
50a25f0a04SGreg Roach		return parent::canShowByType($access_level);
51a25f0a04SGreg Roach	}
52a25f0a04SGreg Roach
5376692c8bSGreg Roach	/**
5476692c8bSGreg Roach	 * Fetch data from the database
5576692c8bSGreg Roach	 *
5676692c8bSGreg Roach	 * @param string $xref
5776692c8bSGreg Roach	 * @param int    $tree_id
5876692c8bSGreg Roach	 *
5976692c8bSGreg Roach	 * @return null|string
6076692c8bSGreg Roach	 */
6164d9078aSGreg Roach	protected static function fetchGedcomRecord($xref, $tree_id) {
6264d9078aSGreg Roach		return Database::prepare(
6364d9078aSGreg Roach			"SELECT m_gedcom FROM `##media` WHERE m_id = :xref AND m_file = :tree_id"
6413abd6f3SGreg Roach		)->execute([
6564d9078aSGreg Roach			'xref'    => $xref,
6664d9078aSGreg Roach			'tree_id' => $tree_id,
6713abd6f3SGreg Roach		])->fetchOne();
68a25f0a04SGreg Roach	}
69a25f0a04SGreg Roach
70a25f0a04SGreg Roach	/**
718f5f5da8SGreg Roach	 * Get the media files for this media object
728f5f5da8SGreg Roach	 *
738f5f5da8SGreg Roach	 * @return MediaFile[]
748f5f5da8SGreg Roach	 */
758f5f5da8SGreg Roach	public function mediaFiles(): array {
768f5f5da8SGreg Roach		$media_files = [];
778f5f5da8SGreg Roach
788f5f5da8SGreg Roach		foreach ($this->getFacts('FILE') as $fact) {
798f5f5da8SGreg Roach			$media_files[] = new MediaFile($fact->getGedcom(), $this);
808f5f5da8SGreg Roach		}
818f5f5da8SGreg Roach
828f5f5da8SGreg Roach		return $media_files;
838f5f5da8SGreg Roach	}
848f5f5da8SGreg Roach
858f5f5da8SGreg Roach	/**
8664b90bf1SGreg Roach	 * Get the first media file that contains an image.
878f5f5da8SGreg Roach	 *
888f5f5da8SGreg Roach	 * @return MediaFile|null
898f5f5da8SGreg Roach	 */
9064b90bf1SGreg Roach	public function firstImageFile() {
918f5f5da8SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
92*4a9f750fSGreg Roach			if ($media_file->isImage()) {
938f5f5da8SGreg Roach				return $media_file;
948f5f5da8SGreg Roach			}
958f5f5da8SGreg Roach		}
968f5f5da8SGreg Roach
978f5f5da8SGreg Roach		return null;
988f5f5da8SGreg Roach	}
998f5f5da8SGreg Roach
1008f5f5da8SGreg Roach	/**
101a25f0a04SGreg Roach	 * Get the first note attached to this media object
102a25f0a04SGreg Roach	 *
103a25f0a04SGreg Roach	 * @return null|string
104a25f0a04SGreg Roach	 */
105a25f0a04SGreg Roach	public function getNote() {
106a25f0a04SGreg Roach		$note = $this->getFirstFact('NOTE');
107a25f0a04SGreg Roach		if ($note) {
108a25f0a04SGreg Roach			$text = $note->getValue();
109a25f0a04SGreg Roach			if (preg_match('/^@' . WT_REGEX_XREF . '@$/', $text)) {
110a25f0a04SGreg Roach				$text = $note->getTarget()->getNote();
111a25f0a04SGreg Roach			}
112a25f0a04SGreg Roach
113a25f0a04SGreg Roach			return $text;
114a25f0a04SGreg Roach		} else {
115a25f0a04SGreg Roach			return '';
116a25f0a04SGreg Roach		}
117a25f0a04SGreg Roach	}
118a25f0a04SGreg Roach
119a25f0a04SGreg Roach	/**
12076692c8bSGreg Roach	 * Extract names from the GEDCOM record.
12176692c8bSGreg Roach	 */
122a25f0a04SGreg Roach	public function extractNames() {
12364b90bf1SGreg Roach		$names = [];
12464b90bf1SGreg Roach		foreach ($this->mediaFiles() as $media_file) {
12564b90bf1SGreg Roach			$names[] = $media_file->title();
12664b90bf1SGreg Roach			$names[] = $media_file->filename();
12764b90bf1SGreg Roach		}
12864b90bf1SGreg Roach		$names = array_filter(array_unique($names));
12964b90bf1SGreg Roach
13064b90bf1SGreg Roach		if (empty($names)) {
13164b90bf1SGreg Roach			$names[] = $this->getFallBackName();
13264b90bf1SGreg Roach		}
13364b90bf1SGreg Roach
13464b90bf1SGreg Roach		foreach ($names as $name) {
13564b90bf1SGreg Roach			$this->addName(static::RECORD_TYPE, $name, null);
13664b90bf1SGreg Roach		}
137a25f0a04SGreg Roach	}
138a25f0a04SGreg Roach
13976692c8bSGreg Roach	/**
14076692c8bSGreg Roach	 * This function should be redefined in derived classes to show any major
14176692c8bSGreg Roach	 * identifying characteristics of this record.
14276692c8bSGreg Roach	 *
14376692c8bSGreg Roach	 * @return string
14476692c8bSGreg Roach	 */
145a25f0a04SGreg Roach	public function formatListDetails() {
146a25f0a04SGreg Roach		ob_start();
1473d7a8a4cSGreg Roach		FunctionsPrintFacts::printMediaLinks('1 OBJE @' . $this->getXref() . '@', 1);
148a25f0a04SGreg Roach
149a25f0a04SGreg Roach		return ob_get_clean();
150a25f0a04SGreg Roach	}
151a25f0a04SGreg Roach}
152