xref: /webtrees/app/Module/AlbumModule.php (revision e2a378d30d9bd3fff591da7a11c7cb5ead502323)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roach/**
208c2e8227SGreg Roach * Class AlbumModule
218c2e8227SGreg Roach */
22*e2a378d3SGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface {
238c2e8227SGreg Roach	private $media_list;
248c2e8227SGreg Roach
258c2e8227SGreg Roach	/** {@inheritdoc} */
268c2e8227SGreg Roach	public function getTitle() {
278c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Album');
288c2e8227SGreg Roach	}
298c2e8227SGreg Roach
308c2e8227SGreg Roach	/** {@inheritdoc} */
318c2e8227SGreg Roach	public function getDescription() {
328c2e8227SGreg Roach		return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
338c2e8227SGreg Roach	}
348c2e8227SGreg Roach
358c2e8227SGreg Roach	/** {@inheritdoc} */
368c2e8227SGreg Roach	public function defaultTabOrder() {
378c2e8227SGreg Roach		return 60;
388c2e8227SGreg Roach	}
398c2e8227SGreg Roach
408c2e8227SGreg Roach	/** {@inheritdoc} */
418c2e8227SGreg Roach	public function hasTabContent() {
424b9ff166SGreg Roach		global $WT_TREE;
434b9ff166SGreg Roach
444b9ff166SGreg Roach		return Auth::isEditor($WT_TREE) || $this->getMedia();
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
478c2e8227SGreg Roach
488c2e8227SGreg Roach	/** {@inheritdoc} */
498c2e8227SGreg Roach	public function isGrayedOut() {
50764a01d9SGreg Roach		return !$this->getMedia();
518c2e8227SGreg Roach	}
528c2e8227SGreg Roach
538c2e8227SGreg Roach	/** {@inheritdoc} */
548c2e8227SGreg Roach	public function getTabContent() {
558c2e8227SGreg Roach		global $WT_TREE, $controller;
568c2e8227SGreg Roach
578c2e8227SGreg Roach		$html = '<div id="' . $this->getName() . '_content">';
588c2e8227SGreg Roach		//Show Lightbox-Album header Links
594b9ff166SGreg Roach		if (Auth::isEditor($WT_TREE)) {
608c2e8227SGreg Roach			$html .= '<table class="facts_table"><tr><td class="descriptionbox rela">';
618c2e8227SGreg Roach			// Add a new media object
624b9ff166SGreg Roach			if ($WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($WT_TREE)) {
638c2e8227SGreg Roach				$html .= '<span><a href="#" onclick="window.open(\'addmedia.php?action=showmediaform&linktoid=' . $controller->record->getXref() . '\', \'_blank\', \'resizable=1,scrollbars=1,top=50,height=780,width=600\');return false;">';
648c2e8227SGreg Roach				$html .= '<img src="' . Theme::theme()->assetUrl() . 'images/image_add.png" id="head_icon" class="icon" title="' . I18N::translate('Add a new media object') . '" alt="' . I18N::translate('Add a new media object') . '">';
658c2e8227SGreg Roach				$html .= I18N::translate('Add a new media object');
668c2e8227SGreg Roach				$html .= '</a></span>';
678c2e8227SGreg Roach				// Link to an existing item
688c2e8227SGreg Roach				$html .= '<span><a href="#" onclick="window.open(\'inverselink.php?linktoid=' . $controller->record->getXref() . '&linkto=person\', \'_blank\', \'resizable=1,scrollbars=1,top=50,height=300,width=450\');">';
698c2e8227SGreg Roach				$html .= '<img src="' . Theme::theme()->assetUrl() . 'images/image_link.png" id="head_icon" class="icon" title="' . I18N::translate('Link to an existing media object') . '" alt="' . I18N::translate('Link to an existing media object') . '">';
708c2e8227SGreg Roach				$html .= I18N::translate('Link to an existing media object');
718c2e8227SGreg Roach				$html .= '</a></span>';
728c2e8227SGreg Roach			}
734b9ff166SGreg Roach			if (Auth::isManager($WT_TREE) && $this->getMedia()) {
748c2e8227SGreg Roach				// Popup Reorder Media
758c2e8227SGreg Roach				$html .= '<span><a href="#" onclick="reorder_media(\'' . $controller->record->getXref() . '\')">';
768c2e8227SGreg Roach				$html .= '<img src="' . Theme::theme()->assetUrl() . 'images/images.png" id="head_icon" class="icon" title="' . I18N::translate('Re-order media') . '" alt="' . I18N::translate('Re-order media') . '">';
778c2e8227SGreg Roach				$html .= I18N::translate('Re-order media');
788c2e8227SGreg Roach				$html .= '</a></span>';
798c2e8227SGreg Roach			}
808c2e8227SGreg Roach			$html .= '</td></tr></table>';
818c2e8227SGreg Roach		}
828c2e8227SGreg Roach
838c2e8227SGreg Roach		// Used when sorting media on album tab page
848c2e8227SGreg Roach		$html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only
858c2e8227SGreg Roach		$html .= '<ul class="album-list">';
86764a01d9SGreg Roach		foreach ($this->getMedia() as $media) {
878c2e8227SGreg Roach			//View Edit Menu ----------------------------------
888c2e8227SGreg Roach
898c2e8227SGreg Roach			//Get media item Notes
908c2e8227SGreg Roach			$haystack = $media->getGedcom();
918c2e8227SGreg Roach			$needle   = '1 NOTE';
928c2e8227SGreg Roach			$before   = substr($haystack, 0, strpos($haystack, $needle));
938c2e8227SGreg Roach			$after    = substr(strstr($haystack, $needle), strlen($needle));
948c2e8227SGreg Roach			$notes    = print_fact_notes($before . $needle . $after, 1, true);
958c2e8227SGreg Roach
968c2e8227SGreg Roach			// Prepare Below Thumbnail  menu ----------------------------------------------------
978c2e8227SGreg Roach			$menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>');
988c2e8227SGreg Roach			$menu->addClass('', 'submenu');
998c2e8227SGreg Roach
1008c2e8227SGreg Roach			// View Notes
1018c2e8227SGreg Roach			if (strpos($media->getGedcom(), "\n1 NOTE")) {
102a80f4f7bSGreg Roach				$submenu = new Menu(I18N::translate('View notes'), '#', '', "modalNotes('" . Filter::escapeJs($notes) . "','" . I18N::translate('View notes') . "'); return false;");
1038c2e8227SGreg Roach				$submenu->addClass("submenuitem");
1048c2e8227SGreg Roach				$menu->addSubmenu($submenu);
1058c2e8227SGreg Roach			}
1068c2e8227SGreg Roach			//View Details
1078c2e8227SGreg Roach			$submenu = new Menu(I18N::translate('View details'), $media->getHtmlUrl());
1088c2e8227SGreg Roach			$submenu->addClass("submenuitem");
1098c2e8227SGreg Roach			$menu->addSubmenu($submenu);
1108c2e8227SGreg Roach
1118c2e8227SGreg Roach			//View Sources
1128c2e8227SGreg Roach			foreach ($media->getFacts('SOUR') as $source_fact) {
1138c2e8227SGreg Roach				$source = $source_fact->getTarget();
1148c2e8227SGreg Roach				if ($source && $source->canShow()) {
1158c2e8227SGreg Roach					$submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl());
1168c2e8227SGreg Roach					$submenu->addClass('submenuitem');
1178c2e8227SGreg Roach					$menu->addSubmenu($submenu);
1188c2e8227SGreg Roach				}
1198c2e8227SGreg Roach			}
1208c2e8227SGreg Roach
1214b9ff166SGreg Roach			if (Auth::isEditor($media->getTree())) {
1228c2e8227SGreg Roach				// Edit Media
123a80f4f7bSGreg Roach				$submenu = new Menu(I18N::translate('Edit media'), '#', '', "return window.open('addmedia.php?action=editmedia&amp;pid=" . $media->getXref() . "', '_blank', edit_window_specs);");
1248c2e8227SGreg Roach				$submenu->addClass("submenuitem");
1258c2e8227SGreg Roach				$menu->addSubmenu($submenu);
1268c2e8227SGreg Roach				if (Auth::isAdmin()) {
1278c2e8227SGreg Roach					if (Module::getModuleByName('GEDFact_assistant')) {
128a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Manage links'), '#', '', "return window.open('inverselink.php?mediaid=" . $media->getXref() . "&amp;linkto=manage', '_blank', find_window_specs);");
1298c2e8227SGreg Roach						$submenu->addClass("submenuitem");
1308c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1318c2e8227SGreg Roach					} else {
132a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi', "return ilinkitem('" . $media->getXref() . "','person');");
1338c2e8227SGreg Roach						$submenu->addClass('submenuitem');
1348c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1358c2e8227SGreg Roach
136a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam', "return ilinkitem('" . $media->getXref() . "','family');");
1378c2e8227SGreg Roach						$submenu->addClass('submenuitem');
1388c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1398c2e8227SGreg Roach
140a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour', "return ilinkitem('" . $media->getXref() . "','source');");
1418c2e8227SGreg Roach						$submenu->addClass('submenuitem');
1428c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1438c2e8227SGreg Roach					}
144a80f4f7bSGreg Roach					$submenu = new Menu(I18N::translate('Unlink media'), '#', '', "return unlink_media('" . I18N::translate('Are you sure you want to remove links to this media object?') . "', '" . $controller->record->getXref() . "', '" . $media->getXref() . "');");
1458c2e8227SGreg Roach					$submenu->addClass("submenuitem");
1468c2e8227SGreg Roach					$menu->addSubmenu($submenu);
1478c2e8227SGreg Roach				}
1488c2e8227SGreg Roach			}
1498c2e8227SGreg Roach			$html .= '<li class="album-list-item">';
1508c2e8227SGreg Roach			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
1518c2e8227SGreg Roach			$html .= '<div class="album-title">' . $menu->getMenu() . '</div>';
1528c2e8227SGreg Roach			$html .= '</li>';
1538c2e8227SGreg Roach		}
1548c2e8227SGreg Roach		$html .= '</ul>';
1558c2e8227SGreg Roach		$html .= '</td></tr></table>';
1568c2e8227SGreg Roach		return $html;
1578c2e8227SGreg Roach	}
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach	/**
1608c2e8227SGreg Roach	 * Get all facts containing media links for this person and their spouse-family records
1618c2e8227SGreg Roach	 *
1628c2e8227SGreg Roach	 * @return Media[]
1638c2e8227SGreg Roach	 */
164764a01d9SGreg Roach	private function getMedia() {
1658c2e8227SGreg Roach		global $controller;
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach		if ($this->media_list === null) {
1688c2e8227SGreg Roach			// Use facts from this individual and all their spouses
1698c2e8227SGreg Roach			$facts = $controller->record->getFacts();
1708c2e8227SGreg Roach			foreach ($controller->record->getSpouseFamilies() as $family) {
1718c2e8227SGreg Roach				foreach ($family->getFacts() as $fact) {
1728c2e8227SGreg Roach					$facts[] = $fact;
1738c2e8227SGreg Roach				}
1748c2e8227SGreg Roach			}
1758c2e8227SGreg Roach			// Use all media from each fact
1768c2e8227SGreg Roach			$this->media_list = array();
1778c2e8227SGreg Roach			foreach ($facts as $fact) {
1788c2e8227SGreg Roach				// Don't show pending edits, as the user just sees duplicates
1798c2e8227SGreg Roach				if (!$fact->isPendingDeletion()) {
1808c2e8227SGreg Roach					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
1818c2e8227SGreg Roach					foreach ($matches[1] as $match) {
18224ec66ceSGreg Roach						$media = Media::getInstance($match, $controller->record->getTree());
1838c2e8227SGreg Roach						if ($media && $media->canShow()) {
1848c2e8227SGreg Roach							$this->media_list[] = $media;
1858c2e8227SGreg Roach						}
1868c2e8227SGreg Roach					}
1878c2e8227SGreg Roach				}
1888c2e8227SGreg Roach			}
1898c2e8227SGreg Roach			// If a media object is linked twice, only show it once
1908c2e8227SGreg Roach			$this->media_list = array_unique($this->media_list);
1918c2e8227SGreg Roach			// Sort these using _WT_OBJE_SORT
1928c2e8227SGreg Roach			$wt_obje_sort = array();
1938c2e8227SGreg Roach			foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) {
1948c2e8227SGreg Roach				$wt_obje_sort[] = trim($fact->getValue(), '@');
1958c2e8227SGreg Roach			}
1968c2e8227SGreg Roach			usort($this->media_list, function(Media $x, Media $y) use ($wt_obje_sort) {
1978c2e8227SGreg Roach				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
1988c2e8227SGreg Roach			});
1998c2e8227SGreg Roach		}
2008c2e8227SGreg Roach		return $this->media_list;
2018c2e8227SGreg Roach	}
2028c2e8227SGreg Roach
2038c2e8227SGreg Roach	/** {@inheritdoc} */
2048c2e8227SGreg Roach	public function canLoadAjax() {
2058c2e8227SGreg Roach		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
2068c2e8227SGreg Roach	}
2078c2e8227SGreg Roach
2088c2e8227SGreg Roach	/** {@inheritdoc} */
2098c2e8227SGreg Roach	public function getPreLoadContent() {
2108c2e8227SGreg Roach		return '';
2118c2e8227SGreg Roach	}
2128c2e8227SGreg Roach}
213