1<?php 2namespace Fisharebest\Webtrees; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19/** 20 * Class AlbumModule 21 */ 22class AlbumModule extends Module implements ModuleTabInterface { 23 private $media_list; 24 25 /** {@inheritdoc} */ 26 public function getTitle() { 27 return /* I18N: Name of a module */ I18N::translate('Album'); 28 } 29 30 /** {@inheritdoc} */ 31 public function getDescription() { 32 return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.'); 33 } 34 35 /** {@inheritdoc} */ 36 public function defaultTabOrder() { 37 return 60; 38 } 39 40 /** {@inheritdoc} */ 41 public function hasTabContent() { 42 return WT_USER_CAN_EDIT || $this->getMedia(); 43 } 44 45 46 /** {@inheritdoc} */ 47 public function isGrayedOut() { 48 return !$this->getMedia(); 49 } 50 51 /** {@inheritdoc} */ 52 public function getTabContent() { 53 global $WT_TREE, $controller; 54 55 $html = '<div id="' . $this->getName() . '_content">'; 56 //Show Lightbox-Album header Links 57 if (WT_USER_CAN_EDIT) { 58 $html .= '<table class="facts_table"><tr><td class="descriptionbox rela">'; 59 // Add a new media object 60 if ($WT_TREE->getPreference('MEDIA_UPLOAD') >= WT_USER_ACCESS_LEVEL) { 61 $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;">'; 62 $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') . '">'; 63 $html .= I18N::translate('Add a new media object'); 64 $html .= '</a></span>'; 65 // Link to an existing item 66 $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\');">'; 67 $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') . '">'; 68 $html .= I18N::translate('Link to an existing media object'); 69 $html .= '</a></span>'; 70 } 71 if (WT_USER_GEDCOM_ADMIN && $this->getMedia()) { 72 // Popup Reorder Media 73 $html .= '<span><a href="#" onclick="reorder_media(\'' . $controller->record->getXref() . '\')">'; 74 $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') . '">'; 75 $html .= I18N::translate('Re-order media'); 76 $html .= '</a></span>'; 77 } 78 $html .= '</td></tr></table>'; 79 } 80 81 // Used when sorting media on album tab page 82 $html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only 83 $html .= '<ul class="album-list">'; 84 foreach ($this->getMedia() as $media) { 85 //View Edit Menu ---------------------------------- 86 87 //Get media item Notes 88 $haystack = $media->getGedcom(); 89 $needle = '1 NOTE'; 90 $before = substr($haystack, 0, strpos($haystack, $needle)); 91 $after = substr(strstr($haystack, $needle), strlen($needle)); 92 $notes = print_fact_notes($before . $needle . $after, 1, true); 93 94 // Prepare Below Thumbnail menu ---------------------------------------------------- 95 $menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>'); 96 $menu->addClass('', 'submenu'); 97 98 // View Notes 99 if (strpos($media->getGedcom(), "\n1 NOTE")) { 100 $submenu = new Menu(I18N::translate('View notes')); 101 // Notes Tooltip ---------------------------------------------------- 102 $submenu->setOnclick("modalNotes('" . Filter::escapeJs($notes) . "','" . I18N::translate('View notes') . "'); return false;"); 103 $submenu->addClass("submenuitem"); 104 $menu->addSubmenu($submenu); 105 } 106 //View Details 107 $submenu = new Menu(I18N::translate('View details'), $media->getHtmlUrl()); 108 $submenu->addClass("submenuitem"); 109 $menu->addSubmenu($submenu); 110 111 //View Sources 112 foreach ($media->getFacts('SOUR') as $source_fact) { 113 $source = $source_fact->getTarget(); 114 if ($source && $source->canShow()) { 115 $submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl()); 116 $submenu->addClass('submenuitem'); 117 $menu->addSubmenu($submenu); 118 } 119 } 120 121 if (WT_USER_CAN_EDIT) { 122 // Edit Media 123 $submenu = new Menu(I18N::translate('Edit media')); 124 $submenu->setOnclick("return window.open('addmedia.php?action=editmedia&pid=" . $media->getXref() . "', '_blank', edit_window_specs);"); 125 $submenu->addClass("submenuitem"); 126 $menu->addSubmenu($submenu); 127 if (Auth::isAdmin()) { 128 if (Module::getModuleByName('GEDFact_assistant')) { 129 $submenu = new Menu(I18N::translate('Manage links')); 130 $submenu->setOnclick("return window.open('inverselink.php?mediaid=" . $media->getXref() . "&linkto=manage', '_blank', find_window_specs);"); 131 $submenu->addClass("submenuitem"); 132 $menu->addSubmenu($submenu); 133 } else { 134 $submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi'); 135 $submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','person');"); 136 $submenu->addClass('submenuitem'); 137 $menu->addSubmenu($submenu); 138 139 $submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam'); 140 $submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','family');"); 141 $submenu->addClass('submenuitem'); 142 $menu->addSubmenu($submenu); 143 144 $submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour'); 145 $submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','source');"); 146 $submenu->addClass('submenuitem'); 147 $menu->addSubmenu($submenu); 148 } 149 $submenu = new Menu(I18N::translate('Unlink media')); 150 $submenu->setOnclick("return unlink_media('" . I18N::translate('Are you sure you want to remove links to this media object?') . "', '" . $controller->record->getXref() . "', '" . $media->getXref() . "');"); 151 $submenu->addClass("submenuitem"); 152 $menu->addSubmenu($submenu); 153 } 154 } 155 $html .= '<li class="album-list-item">'; 156 $html .= '<div class="album-image">' . $media->displayImage() . '</div>'; 157 $html .= '<div class="album-title">' . $menu->getMenu() . '</div>'; 158 $html .= '</li>'; 159 } 160 $html .= '</ul>'; 161 $html .= '</td></tr></table>'; 162 return $html; 163 } 164 165 /** 166 * Get all facts containing media links for this person and their spouse-family records 167 * 168 * @return Media[] 169 */ 170 private function getMedia() { 171 global $controller; 172 173 if ($this->media_list === null) { 174 // Use facts from this individual and all their spouses 175 $facts = $controller->record->getFacts(); 176 foreach ($controller->record->getSpouseFamilies() as $family) { 177 foreach ($family->getFacts() as $fact) { 178 $facts[] = $fact; 179 } 180 } 181 // Use all media from each fact 182 $this->media_list = array(); 183 foreach ($facts as $fact) { 184 // Don't show pending edits, as the user just sees duplicates 185 if (!$fact->isPendingDeletion()) { 186 preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches); 187 foreach ($matches[1] as $match) { 188 $media = Media::getInstance($match); 189 if ($media && $media->canShow()) { 190 $this->media_list[] = $media; 191 } 192 } 193 } 194 } 195 // If a media object is linked twice, only show it once 196 $this->media_list = array_unique($this->media_list); 197 // Sort these using _WT_OBJE_SORT 198 $wt_obje_sort = array(); 199 foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) { 200 $wt_obje_sort[] = trim($fact->getValue(), '@'); 201 } 202 usort($this->media_list, function(Media $x, Media $y) use ($wt_obje_sort) { 203 return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort); 204 }); 205 } 206 return $this->media_list; 207 } 208 209 /** {@inheritdoc} */ 210 public function canLoadAjax() { 211 return !Auth::isSearchEngine(); // Search engines cannot use AJAX 212 } 213 214 /** {@inheritdoc} */ 215 public function getPreLoadContent() { 216 return ''; 217 } 218} 219