xref: /webtrees/app/Module/AlbumModule.php (revision d14df12ba9844ebd314c550c8fc63b7b3b7cd055)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
21*d14df12bSGreg Roachuse Fisharebest\Webtrees\View;
228c2e8227SGreg Roach
238c2e8227SGreg Roach/**
248c2e8227SGreg Roach * Class AlbumModule
258c2e8227SGreg Roach */
26e2a378d3SGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface {
2776692c8bSGreg Roach	/** @var Media[] List of media objects. */
288c2e8227SGreg Roach	private $media_list;
298c2e8227SGreg Roach
3076692c8bSGreg Roach	/**
3176692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
3276692c8bSGreg Roach	 *
3376692c8bSGreg Roach	 * @return string
3476692c8bSGreg Roach	 */
358c2e8227SGreg Roach	public function getTitle() {
368c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Album');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
3976692c8bSGreg Roach	/**
4076692c8bSGreg Roach	 * A sentence describing what this module does.
4176692c8bSGreg Roach	 *
4276692c8bSGreg Roach	 * @return string
4376692c8bSGreg Roach	 */
448c2e8227SGreg Roach	public function getDescription() {
458c2e8227SGreg Roach		return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * The user can re-arrange the tab order, but until they do, this
5076692c8bSGreg Roach	 * is the order in which tabs are shown.
5176692c8bSGreg Roach	 *
5276692c8bSGreg Roach	 * @return int
5376692c8bSGreg Roach	 */
548c2e8227SGreg Roach	public function defaultTabOrder() {
558c2e8227SGreg Roach		return 60;
568c2e8227SGreg Roach	}
578c2e8227SGreg Roach
5876692c8bSGreg Roach	/**
5976692c8bSGreg Roach	 * Is this tab empty? If so, we don't always need to display it.
6076692c8bSGreg Roach	 *
6176692c8bSGreg Roach	 * @return bool
6276692c8bSGreg Roach	 */
638c2e8227SGreg Roach	public function hasTabContent() {
644b9ff166SGreg Roach		global $WT_TREE;
654b9ff166SGreg Roach
664b9ff166SGreg Roach		return Auth::isEditor($WT_TREE) || $this->getMedia();
678c2e8227SGreg Roach	}
688c2e8227SGreg Roach
6976692c8bSGreg Roach	/**
7076692c8bSGreg Roach	 * A greyed out tab has no actual content, but may perhaps have
7176692c8bSGreg Roach	 * options to create content.
7276692c8bSGreg Roach	 *
7376692c8bSGreg Roach	 * @return bool
7476692c8bSGreg Roach	 */
758c2e8227SGreg Roach	public function isGrayedOut() {
76764a01d9SGreg Roach		return !$this->getMedia();
778c2e8227SGreg Roach	}
788c2e8227SGreg Roach
7976692c8bSGreg Roach	/**
8076692c8bSGreg Roach	 * Generate the HTML content of this tab.
8176692c8bSGreg Roach	 *
8276692c8bSGreg Roach	 * @return string
8376692c8bSGreg Roach	 */
848c2e8227SGreg Roach	public function getTabContent() {
85*d14df12bSGreg Roach		return View::make('tabs/album', [
86*d14df12bSGreg Roach			'media_list' => $this->getMedia()
87*d14df12bSGreg Roach		]);
888c2e8227SGreg Roach	}
898c2e8227SGreg Roach
908c2e8227SGreg Roach	/**
918c2e8227SGreg Roach	 * Get all facts containing media links for this person and their spouse-family records
928c2e8227SGreg Roach	 *
938c2e8227SGreg Roach	 * @return Media[]
948c2e8227SGreg Roach	 */
95764a01d9SGreg Roach	private function getMedia() {
968c2e8227SGreg Roach		global $controller;
978c2e8227SGreg Roach
988c2e8227SGreg Roach		if ($this->media_list === null) {
998c2e8227SGreg Roach			// Use facts from this individual and all their spouses
1008c2e8227SGreg Roach			$facts = $controller->record->getFacts();
1018c2e8227SGreg Roach			foreach ($controller->record->getSpouseFamilies() as $family) {
1028c2e8227SGreg Roach				foreach ($family->getFacts() as $fact) {
1038c2e8227SGreg Roach					$facts[] = $fact;
1048c2e8227SGreg Roach				}
1058c2e8227SGreg Roach			}
1068c2e8227SGreg Roach			// Use all media from each fact
10713abd6f3SGreg Roach			$this->media_list = [];
1088c2e8227SGreg Roach			foreach ($facts as $fact) {
1098c2e8227SGreg Roach				// Don't show pending edits, as the user just sees duplicates
1108c2e8227SGreg Roach				if (!$fact->isPendingDeletion()) {
1118c2e8227SGreg Roach					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
1128c2e8227SGreg Roach					foreach ($matches[1] as $match) {
11324ec66ceSGreg Roach						$media = Media::getInstance($match, $controller->record->getTree());
1148c2e8227SGreg Roach						if ($media && $media->canShow()) {
1158c2e8227SGreg Roach							$this->media_list[] = $media;
1168c2e8227SGreg Roach						}
1178c2e8227SGreg Roach					}
1188c2e8227SGreg Roach				}
1198c2e8227SGreg Roach			}
1208c2e8227SGreg Roach			// If a media object is linked twice, only show it once
1218c2e8227SGreg Roach			$this->media_list = array_unique($this->media_list);
1228c2e8227SGreg Roach			// Sort these using _WT_OBJE_SORT
12313abd6f3SGreg Roach			$wt_obje_sort = [];
1248c2e8227SGreg Roach			foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) {
1258c2e8227SGreg Roach				$wt_obje_sort[] = trim($fact->getValue(), '@');
1268c2e8227SGreg Roach			}
1278c2e8227SGreg Roach			usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort) {
1288c2e8227SGreg Roach				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
1298c2e8227SGreg Roach			});
1308c2e8227SGreg Roach		}
131cbc1590aSGreg Roach
1328c2e8227SGreg Roach		return $this->media_list;
1338c2e8227SGreg Roach	}
1348c2e8227SGreg Roach
13576692c8bSGreg Roach	/**
13676692c8bSGreg Roach	 * Can this tab load asynchronously?
13776692c8bSGreg Roach	 *
13876692c8bSGreg Roach	 * @return bool
13976692c8bSGreg Roach	 */
1408c2e8227SGreg Roach	public function canLoadAjax() {
14115d603e7SGreg Roach		return false;
1428c2e8227SGreg Roach	}
1438c2e8227SGreg Roach
14476692c8bSGreg Roach	/**
14576692c8bSGreg Roach	 * Any content (e.g. Javascript) that needs to be rendered before the tabs.
14676692c8bSGreg Roach	 *
14776692c8bSGreg Roach	 * This function is probably not needed, as there are better ways to achieve this.
14876692c8bSGreg Roach	 *
14976692c8bSGreg Roach	 * @return string
15076692c8bSGreg Roach	 */
1518c2e8227SGreg Roach	public function getPreLoadContent() {
1528c2e8227SGreg Roach		return '';
1538c2e8227SGreg Roach	}
1548c2e8227SGreg Roach}
155