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 */ 228c2e8227SGreg Roachclass AlbumModule extends Module 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")) { 1028c2e8227SGreg Roach $submenu = new Menu(I18N::translate('View notes')); 1038c2e8227SGreg Roach // Notes Tooltip ---------------------------------------------------- 1048c2e8227SGreg Roach $submenu->setOnclick("modalNotes('" . Filter::escapeJs($notes) . "','" . I18N::translate('View notes') . "'); return false;"); 1058c2e8227SGreg Roach $submenu->addClass("submenuitem"); 1068c2e8227SGreg Roach $menu->addSubmenu($submenu); 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach //View Details 1098c2e8227SGreg Roach $submenu = new Menu(I18N::translate('View details'), $media->getHtmlUrl()); 1108c2e8227SGreg Roach $submenu->addClass("submenuitem"); 1118c2e8227SGreg Roach $menu->addSubmenu($submenu); 1128c2e8227SGreg Roach 1138c2e8227SGreg Roach //View Sources 1148c2e8227SGreg Roach foreach ($media->getFacts('SOUR') as $source_fact) { 1158c2e8227SGreg Roach $source = $source_fact->getTarget(); 1168c2e8227SGreg Roach if ($source && $source->canShow()) { 1178c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl()); 1188c2e8227SGreg Roach $submenu->addClass('submenuitem'); 1198c2e8227SGreg Roach $menu->addSubmenu($submenu); 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 1234b9ff166SGreg Roach if (Auth::isEditor($media->getTree())) { 1248c2e8227SGreg Roach // Edit Media 1258c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Edit media')); 1268c2e8227SGreg Roach $submenu->setOnclick("return window.open('addmedia.php?action=editmedia&pid=" . $media->getXref() . "', '_blank', edit_window_specs);"); 1278c2e8227SGreg Roach $submenu->addClass("submenuitem"); 1288c2e8227SGreg Roach $menu->addSubmenu($submenu); 1298c2e8227SGreg Roach if (Auth::isAdmin()) { 1308c2e8227SGreg Roach if (Module::getModuleByName('GEDFact_assistant')) { 1318c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Manage links')); 1328c2e8227SGreg Roach $submenu->setOnclick("return window.open('inverselink.php?mediaid=" . $media->getXref() . "&linkto=manage', '_blank', find_window_specs);"); 1338c2e8227SGreg Roach $submenu->addClass("submenuitem"); 1348c2e8227SGreg Roach $menu->addSubmenu($submenu); 1358c2e8227SGreg Roach } else { 1368c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi'); 1378c2e8227SGreg Roach $submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','person');"); 1388c2e8227SGreg Roach $submenu->addClass('submenuitem'); 1398c2e8227SGreg Roach $menu->addSubmenu($submenu); 1408c2e8227SGreg Roach 1418c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam'); 1428c2e8227SGreg Roach $submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','family');"); 1438c2e8227SGreg Roach $submenu->addClass('submenuitem'); 1448c2e8227SGreg Roach $menu->addSubmenu($submenu); 1458c2e8227SGreg Roach 1468c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour'); 1478c2e8227SGreg Roach $submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','source');"); 1488c2e8227SGreg Roach $submenu->addClass('submenuitem'); 1498c2e8227SGreg Roach $menu->addSubmenu($submenu); 1508c2e8227SGreg Roach } 1518c2e8227SGreg Roach $submenu = new Menu(I18N::translate('Unlink media')); 1528c2e8227SGreg Roach $submenu->setOnclick("return unlink_media('" . I18N::translate('Are you sure you want to remove links to this media object?') . "', '" . $controller->record->getXref() . "', '" . $media->getXref() . "');"); 1538c2e8227SGreg Roach $submenu->addClass("submenuitem"); 1548c2e8227SGreg Roach $menu->addSubmenu($submenu); 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach $html .= '<li class="album-list-item">'; 1588c2e8227SGreg Roach $html .= '<div class="album-image">' . $media->displayImage() . '</div>'; 1598c2e8227SGreg Roach $html .= '<div class="album-title">' . $menu->getMenu() . '</div>'; 1608c2e8227SGreg Roach $html .= '</li>'; 1618c2e8227SGreg Roach } 1628c2e8227SGreg Roach $html .= '</ul>'; 1638c2e8227SGreg Roach $html .= '</td></tr></table>'; 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) { 190*24ec66ceSGreg 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 } 2088c2e8227SGreg Roach return $this->media_list; 2098c2e8227SGreg Roach } 2108c2e8227SGreg Roach 2118c2e8227SGreg Roach /** {@inheritdoc} */ 2128c2e8227SGreg Roach public function canLoadAjax() { 2138c2e8227SGreg Roach return !Auth::isSearchEngine(); // Search engines cannot use AJAX 2148c2e8227SGreg Roach } 2158c2e8227SGreg Roach 2168c2e8227SGreg Roach /** {@inheritdoc} */ 2178c2e8227SGreg Roach public function getPreLoadContent() { 2188c2e8227SGreg Roach return ''; 2198c2e8227SGreg Roach } 2208c2e8227SGreg Roach} 221