xref: /webtrees/app/Module/AlbumModule.php (revision f9f36b89a87e863f620f395526e2d36466a921f9)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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\I18N;
19225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
218c2e8227SGreg Roach
228c2e8227SGreg Roach/**
238c2e8227SGreg Roach * Class AlbumModule
248c2e8227SGreg Roach */
25e2a378d3SGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface {
2676692c8bSGreg Roach	/** @var Media[] List of media objects. */
278c2e8227SGreg Roach	private $media_list;
288c2e8227SGreg Roach
2976692c8bSGreg Roach	/**
3076692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
3176692c8bSGreg Roach	 *
3276692c8bSGreg Roach	 * @return string
3376692c8bSGreg Roach	 */
348c2e8227SGreg Roach	public function getTitle() {
358c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Album');
368c2e8227SGreg Roach	}
378c2e8227SGreg Roach
3876692c8bSGreg Roach	/**
3976692c8bSGreg Roach	 * A sentence describing what this module does.
4076692c8bSGreg Roach	 *
4176692c8bSGreg Roach	 * @return string
4276692c8bSGreg Roach	 */
438c2e8227SGreg Roach	public function getDescription() {
448c2e8227SGreg Roach		return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
4776692c8bSGreg Roach	/**
4876692c8bSGreg Roach	 * The user can re-arrange the tab order, but until they do, this
4976692c8bSGreg Roach	 * is the order in which tabs are shown.
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @return int
5276692c8bSGreg Roach	 */
538c2e8227SGreg Roach	public function defaultTabOrder() {
548c2e8227SGreg Roach		return 60;
558c2e8227SGreg Roach	}
568c2e8227SGreg Roach
5776692c8bSGreg Roach	/**
5876692c8bSGreg Roach	 * Is this tab empty? If so, we don't always need to display it.
5976692c8bSGreg Roach	 *
60*f9f36b89SGreg Roach	 * @param Individual $individual
61*f9f36b89SGreg Roach	 *
6276692c8bSGreg Roach	 * @return bool
6376692c8bSGreg Roach	 */
64225e381fSGreg Roach	public function hasTabContent(Individual $individual) {
65225e381fSGreg Roach		return $individual->canEdit() || $this->getMedia($individual);
668c2e8227SGreg Roach	}
678c2e8227SGreg Roach
6876692c8bSGreg Roach	/**
6976692c8bSGreg Roach	 * A greyed out tab has no actual content, but may perhaps have
7076692c8bSGreg Roach	 * options to create content.
7176692c8bSGreg Roach	 *
72*f9f36b89SGreg Roach	 * @param Individual $individual
73*f9f36b89SGreg Roach	 *
7476692c8bSGreg Roach	 * @return bool
7576692c8bSGreg Roach	 */
76225e381fSGreg Roach	public function isGrayedOut(Individual $individual) {
77225e381fSGreg Roach		return !$this->getMedia($individual);
788c2e8227SGreg Roach	}
798c2e8227SGreg Roach
8076692c8bSGreg Roach	/**
8176692c8bSGreg Roach	 * Generate the HTML content of this tab.
8276692c8bSGreg Roach	 *
83225e381fSGreg Roach	 * @param Individual $individual
84225e381fSGreg Roach	 *
8576692c8bSGreg Roach	 * @return string
8676692c8bSGreg Roach	 */
87225e381fSGreg Roach	public function getTabContent(Individual $individual) {
88225e381fSGreg Roach		return view('tabs/album', [
89225e381fSGreg Roach			'media_list' => $this->getMedia($individual)
90d14df12bSGreg Roach		]);
918c2e8227SGreg Roach	}
928c2e8227SGreg Roach
938c2e8227SGreg Roach	/**
948c2e8227SGreg Roach	 * Get all facts containing media links for this person and their spouse-family records
958c2e8227SGreg Roach	 *
96225e381fSGreg Roach	 * @param Individual $individual
97225e381fSGreg Roach	 *
988c2e8227SGreg Roach	 * @return Media[]
998c2e8227SGreg Roach	 */
100225e381fSGreg Roach	private function getMedia(Individual $individual) {
1018c2e8227SGreg Roach		if ($this->media_list === null) {
1028c2e8227SGreg Roach			// Use facts from this individual and all their spouses
103225e381fSGreg Roach			$facts = $individual->getFacts();
104225e381fSGreg Roach			foreach ($individual->getSpouseFamilies() as $family) {
1058c2e8227SGreg Roach				foreach ($family->getFacts() as $fact) {
1068c2e8227SGreg Roach					$facts[] = $fact;
1078c2e8227SGreg Roach				}
1088c2e8227SGreg Roach			}
1098c2e8227SGreg Roach			// Use all media from each fact
11013abd6f3SGreg Roach			$this->media_list = [];
1118c2e8227SGreg Roach			foreach ($facts as $fact) {
1128c2e8227SGreg Roach				// Don't show pending edits, as the user just sees duplicates
1138c2e8227SGreg Roach				if (!$fact->isPendingDeletion()) {
1148c2e8227SGreg Roach					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
1158c2e8227SGreg Roach					foreach ($matches[1] as $match) {
116225e381fSGreg Roach						$media = Media::getInstance($match, $individual->getTree());
1178c2e8227SGreg Roach						if ($media && $media->canShow()) {
1188c2e8227SGreg Roach							$this->media_list[] = $media;
1198c2e8227SGreg Roach						}
1208c2e8227SGreg Roach					}
1218c2e8227SGreg Roach				}
1228c2e8227SGreg Roach			}
1238c2e8227SGreg Roach			// If a media object is linked twice, only show it once
1248c2e8227SGreg Roach			$this->media_list = array_unique($this->media_list);
1258c2e8227SGreg Roach			// Sort these using _WT_OBJE_SORT
12613abd6f3SGreg Roach			$wt_obje_sort = [];
127225e381fSGreg Roach			foreach ($individual->getFacts('_WT_OBJE_SORT') as $fact) {
1288c2e8227SGreg Roach				$wt_obje_sort[] = trim($fact->getValue(), '@');
1298c2e8227SGreg Roach			}
1308c2e8227SGreg Roach			usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort) {
1318c2e8227SGreg Roach				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
1328c2e8227SGreg Roach			});
1338c2e8227SGreg Roach		}
134cbc1590aSGreg Roach
1358c2e8227SGreg Roach		return $this->media_list;
1368c2e8227SGreg Roach	}
1378c2e8227SGreg Roach
13876692c8bSGreg Roach	/**
13976692c8bSGreg Roach	 * Can this tab load asynchronously?
14076692c8bSGreg Roach	 *
14176692c8bSGreg Roach	 * @return bool
14276692c8bSGreg Roach	 */
1438c2e8227SGreg Roach	public function canLoadAjax() {
14415d603e7SGreg Roach		return false;
1458c2e8227SGreg Roach	}
1468c2e8227SGreg Roach}
147