xref: /webtrees/app/Module/AlbumModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
18c2e8227SGreg Roach<?php
20e62c4b8SGreg Roachnamespace Fisharebest\Webtrees\Module;
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 */
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
20*3d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class AlbumModule
298c2e8227SGreg Roach */
30e2a378d3SGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface {
318c2e8227SGreg Roach	private $media_list;
328c2e8227SGreg Roach
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getTitle() {
358c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Album');
368c2e8227SGreg Roach	}
378c2e8227SGreg Roach
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function getDescription() {
408c2e8227SGreg Roach		return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
418c2e8227SGreg Roach	}
428c2e8227SGreg Roach
438c2e8227SGreg Roach	/** {@inheritdoc} */
448c2e8227SGreg Roach	public function defaultTabOrder() {
458c2e8227SGreg Roach		return 60;
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
488c2e8227SGreg Roach	/** {@inheritdoc} */
498c2e8227SGreg Roach	public function hasTabContent() {
504b9ff166SGreg Roach		global $WT_TREE;
514b9ff166SGreg Roach
524b9ff166SGreg Roach		return Auth::isEditor($WT_TREE) || $this->getMedia();
538c2e8227SGreg Roach	}
548c2e8227SGreg Roach
558c2e8227SGreg Roach	/** {@inheritdoc} */
568c2e8227SGreg Roach	public function isGrayedOut() {
57764a01d9SGreg Roach		return !$this->getMedia();
588c2e8227SGreg Roach	}
598c2e8227SGreg Roach
608c2e8227SGreg Roach	/** {@inheritdoc} */
618c2e8227SGreg Roach	public function getTabContent() {
628c2e8227SGreg Roach		global $WT_TREE, $controller;
638c2e8227SGreg Roach
648c2e8227SGreg Roach		$html = '<div id="' . $this->getName() . '_content">';
658c2e8227SGreg Roach		//Show Lightbox-Album header Links
664b9ff166SGreg Roach		if (Auth::isEditor($WT_TREE)) {
678c2e8227SGreg Roach			$html .= '<table class="facts_table"><tr><td class="descriptionbox rela">';
688c2e8227SGreg Roach			// Add a new media object
694b9ff166SGreg Roach			if ($WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($WT_TREE)) {
708c2e8227SGreg 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;">';
718c2e8227SGreg 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') . '">';
728c2e8227SGreg Roach				$html .= I18N::translate('Add a new media object');
738c2e8227SGreg Roach				$html .= '</a></span>';
748c2e8227SGreg Roach				// Link to an existing item
758c2e8227SGreg 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\');">';
768c2e8227SGreg 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') . '">';
778c2e8227SGreg Roach				$html .= I18N::translate('Link to an existing media object');
788c2e8227SGreg Roach				$html .= '</a></span>';
798c2e8227SGreg Roach			}
804b9ff166SGreg Roach			if (Auth::isManager($WT_TREE) && $this->getMedia()) {
818c2e8227SGreg Roach				// Popup Reorder Media
828c2e8227SGreg Roach				$html .= '<span><a href="#" onclick="reorder_media(\'' . $controller->record->getXref() . '\')">';
838c2e8227SGreg 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') . '">';
848c2e8227SGreg Roach				$html .= I18N::translate('Re-order media');
858c2e8227SGreg Roach				$html .= '</a></span>';
868c2e8227SGreg Roach			}
878c2e8227SGreg Roach			$html .= '</td></tr></table>';
888c2e8227SGreg Roach		}
898c2e8227SGreg Roach
908c2e8227SGreg Roach		// Used when sorting media on album tab page
918c2e8227SGreg Roach		$html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only
928c2e8227SGreg Roach		$html .= '<ul class="album-list">';
93764a01d9SGreg Roach		foreach ($this->getMedia() as $media) {
948c2e8227SGreg Roach			//View Edit Menu ----------------------------------
958c2e8227SGreg Roach
968c2e8227SGreg Roach			//Get media item Notes
978c2e8227SGreg Roach			$haystack = $media->getGedcom();
988c2e8227SGreg Roach			$needle   = '1 NOTE';
998c2e8227SGreg Roach			$before   = substr($haystack, 0, strpos($haystack, $needle));
1008c2e8227SGreg Roach			$after    = substr(strstr($haystack, $needle), strlen($needle));
101*3d7a8a4cSGreg Roach			$notes    = FunctionsPrint::printFactNotes($before . $needle . $after, 1, true);
1028c2e8227SGreg Roach
1038c2e8227SGreg Roach			// Prepare Below Thumbnail  menu ----------------------------------------------------
1048c2e8227SGreg Roach			$menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>');
1058c2e8227SGreg Roach			$menu->addClass('', 'submenu');
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach			// View Notes
1088c2e8227SGreg Roach			if (strpos($media->getGedcom(), "\n1 NOTE")) {
109a80f4f7bSGreg Roach				$submenu = new Menu(I18N::translate('View notes'), '#', '', "modalNotes('" . Filter::escapeJs($notes) . "','" . I18N::translate('View notes') . "'); return false;");
1108c2e8227SGreg Roach				$submenu->addClass("submenuitem");
1118c2e8227SGreg Roach				$menu->addSubmenu($submenu);
1128c2e8227SGreg Roach			}
1138c2e8227SGreg Roach			//View Details
1148c2e8227SGreg Roach			$submenu = new Menu(I18N::translate('View details'), $media->getHtmlUrl());
1158c2e8227SGreg Roach			$submenu->addClass("submenuitem");
1168c2e8227SGreg Roach			$menu->addSubmenu($submenu);
1178c2e8227SGreg Roach
1188c2e8227SGreg Roach			//View Sources
1198c2e8227SGreg Roach			foreach ($media->getFacts('SOUR') as $source_fact) {
1208c2e8227SGreg Roach				$source = $source_fact->getTarget();
1218c2e8227SGreg Roach				if ($source && $source->canShow()) {
1228c2e8227SGreg Roach					$submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl());
1238c2e8227SGreg Roach					$submenu->addClass('submenuitem');
1248c2e8227SGreg Roach					$menu->addSubmenu($submenu);
1258c2e8227SGreg Roach				}
1268c2e8227SGreg Roach			}
1278c2e8227SGreg Roach
1284b9ff166SGreg Roach			if (Auth::isEditor($media->getTree())) {
1298c2e8227SGreg Roach				// Edit Media
130a80f4f7bSGreg Roach				$submenu = new Menu(I18N::translate('Edit media'), '#', '', "return window.open('addmedia.php?action=editmedia&amp;pid=" . $media->getXref() . "', '_blank', edit_window_specs);");
1318c2e8227SGreg Roach				$submenu->addClass("submenuitem");
1328c2e8227SGreg Roach				$menu->addSubmenu($submenu);
1338c2e8227SGreg Roach				if (Auth::isAdmin()) {
1348c2e8227SGreg Roach					if (Module::getModuleByName('GEDFact_assistant')) {
135a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Manage links'), '#', '', "return window.open('inverselink.php?mediaid=" . $media->getXref() . "&amp;linkto=manage', '_blank', find_window_specs);");
1368c2e8227SGreg Roach						$submenu->addClass("submenuitem");
1378c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1388c2e8227SGreg Roach					} else {
139a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi', "return ilinkitem('" . $media->getXref() . "','person');");
1408c2e8227SGreg Roach						$submenu->addClass('submenuitem');
1418c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1428c2e8227SGreg Roach
143a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam', "return ilinkitem('" . $media->getXref() . "','family');");
1448c2e8227SGreg Roach						$submenu->addClass('submenuitem');
1458c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1468c2e8227SGreg Roach
147a80f4f7bSGreg Roach						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour', "return ilinkitem('" . $media->getXref() . "','source');");
1488c2e8227SGreg Roach						$submenu->addClass('submenuitem');
1498c2e8227SGreg Roach						$menu->addSubmenu($submenu);
1508c2e8227SGreg Roach					}
151a80f4f7bSGreg 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() . "');");
1528c2e8227SGreg Roach					$submenu->addClass("submenuitem");
1538c2e8227SGreg Roach					$menu->addSubmenu($submenu);
1548c2e8227SGreg Roach				}
1558c2e8227SGreg Roach			}
1568c2e8227SGreg Roach			$html .= '<li class="album-list-item">';
1578c2e8227SGreg Roach			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
1588c2e8227SGreg Roach			$html .= '<div class="album-title">' . $menu->getMenu() . '</div>';
1598c2e8227SGreg Roach			$html .= '</li>';
1608c2e8227SGreg Roach		}
1618c2e8227SGreg Roach		$html .= '</ul>';
1628c2e8227SGreg Roach		$html .= '</td></tr></table>';
163cbc1590aSGreg Roach
1648c2e8227SGreg Roach		return $html;
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach	/**
1688c2e8227SGreg Roach	 * Get all facts containing media links for this person and their spouse-family records
1698c2e8227SGreg Roach	 *
1708c2e8227SGreg Roach	 * @return Media[]
1718c2e8227SGreg Roach	 */
172764a01d9SGreg Roach	private function getMedia() {
1738c2e8227SGreg Roach		global $controller;
1748c2e8227SGreg Roach
1758c2e8227SGreg Roach		if ($this->media_list === null) {
1768c2e8227SGreg Roach			// Use facts from this individual and all their spouses
1778c2e8227SGreg Roach			$facts = $controller->record->getFacts();
1788c2e8227SGreg Roach			foreach ($controller->record->getSpouseFamilies() as $family) {
1798c2e8227SGreg Roach				foreach ($family->getFacts() as $fact) {
1808c2e8227SGreg Roach					$facts[] = $fact;
1818c2e8227SGreg Roach				}
1828c2e8227SGreg Roach			}
1838c2e8227SGreg Roach			// Use all media from each fact
1848c2e8227SGreg Roach			$this->media_list = array();
1858c2e8227SGreg Roach			foreach ($facts as $fact) {
1868c2e8227SGreg Roach				// Don't show pending edits, as the user just sees duplicates
1878c2e8227SGreg Roach				if (!$fact->isPendingDeletion()) {
1888c2e8227SGreg Roach					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
1898c2e8227SGreg Roach					foreach ($matches[1] as $match) {
19024ec66ceSGreg Roach						$media = Media::getInstance($match, $controller->record->getTree());
1918c2e8227SGreg Roach						if ($media && $media->canShow()) {
1928c2e8227SGreg Roach							$this->media_list[] = $media;
1938c2e8227SGreg Roach						}
1948c2e8227SGreg Roach					}
1958c2e8227SGreg Roach				}
1968c2e8227SGreg Roach			}
1978c2e8227SGreg Roach			// If a media object is linked twice, only show it once
1988c2e8227SGreg Roach			$this->media_list = array_unique($this->media_list);
1998c2e8227SGreg Roach			// Sort these using _WT_OBJE_SORT
2008c2e8227SGreg Roach			$wt_obje_sort = array();
2018c2e8227SGreg Roach			foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) {
2028c2e8227SGreg Roach				$wt_obje_sort[] = trim($fact->getValue(), '@');
2038c2e8227SGreg Roach			}
2048c2e8227SGreg Roach			usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort) {
2058c2e8227SGreg Roach				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
2068c2e8227SGreg Roach			});
2078c2e8227SGreg Roach		}
208cbc1590aSGreg Roach
2098c2e8227SGreg Roach		return $this->media_list;
2108c2e8227SGreg Roach	}
2118c2e8227SGreg Roach
2128c2e8227SGreg Roach	/** {@inheritdoc} */
2138c2e8227SGreg Roach	public function canLoadAjax() {
2148c2e8227SGreg Roach		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
2158c2e8227SGreg Roach	}
2168c2e8227SGreg Roach
2178c2e8227SGreg Roach	/** {@inheritdoc} */
2188c2e8227SGreg Roach	public function getPreLoadContent() {
2198c2e8227SGreg Roach		return '';
2208c2e8227SGreg Roach	}
2218c2e8227SGreg Roach}
222