xref: /webtrees/app/Module/AlbumModule.php (revision 24ec66ce7e77188cd2495b0f8d4dd0ae6e8c9c52)
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		global $WT_TREE;
43
44		return Auth::isEditor($WT_TREE) || $this->getMedia();
45	}
46
47
48	/** {@inheritdoc} */
49	public function isGrayedOut() {
50		return !$this->getMedia();
51	}
52
53	/** {@inheritdoc} */
54	public function getTabContent() {
55		global $WT_TREE, $controller;
56
57		$html = '<div id="' . $this->getName() . '_content">';
58		//Show Lightbox-Album header Links
59		if (Auth::isEditor($WT_TREE)) {
60			$html .= '<table class="facts_table"><tr><td class="descriptionbox rela">';
61			// Add a new media object
62			if ($WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($WT_TREE)) {
63				$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;">';
64				$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') . '">';
65				$html .= I18N::translate('Add a new media object');
66				$html .= '</a></span>';
67				// Link to an existing item
68				$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\');">';
69				$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') . '">';
70				$html .= I18N::translate('Link to an existing media object');
71				$html .= '</a></span>';
72			}
73			if (Auth::isManager($WT_TREE) && $this->getMedia()) {
74				// Popup Reorder Media
75				$html .= '<span><a href="#" onclick="reorder_media(\'' . $controller->record->getXref() . '\')">';
76				$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') . '">';
77				$html .= I18N::translate('Re-order media');
78				$html .= '</a></span>';
79			}
80			$html .= '</td></tr></table>';
81		}
82
83		// Used when sorting media on album tab page
84		$html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only
85		$html .= '<ul class="album-list">';
86		foreach ($this->getMedia() as $media) {
87			//View Edit Menu ----------------------------------
88
89			//Get media item Notes
90			$haystack = $media->getGedcom();
91			$needle   = '1 NOTE';
92			$before   = substr($haystack, 0, strpos($haystack, $needle));
93			$after    = substr(strstr($haystack, $needle), strlen($needle));
94			$notes    = print_fact_notes($before . $needle . $after, 1, true);
95
96			// Prepare Below Thumbnail  menu ----------------------------------------------------
97			$menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>');
98			$menu->addClass('', 'submenu');
99
100			// View Notes
101			if (strpos($media->getGedcom(), "\n1 NOTE")) {
102				$submenu = new Menu(I18N::translate('View notes'));
103				// Notes Tooltip ----------------------------------------------------
104				$submenu->setOnclick("modalNotes('" . Filter::escapeJs($notes) . "','" . I18N::translate('View notes') . "'); return false;");
105				$submenu->addClass("submenuitem");
106				$menu->addSubmenu($submenu);
107			}
108			//View Details
109			$submenu = new Menu(I18N::translate('View details'), $media->getHtmlUrl());
110			$submenu->addClass("submenuitem");
111			$menu->addSubmenu($submenu);
112
113			//View Sources
114			foreach ($media->getFacts('SOUR') as $source_fact) {
115				$source = $source_fact->getTarget();
116				if ($source && $source->canShow()) {
117					$submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl());
118					$submenu->addClass('submenuitem');
119					$menu->addSubmenu($submenu);
120				}
121			}
122
123			if (Auth::isEditor($media->getTree())) {
124				// Edit Media
125				$submenu = new Menu(I18N::translate('Edit media'));
126				$submenu->setOnclick("return window.open('addmedia.php?action=editmedia&amp;pid=" . $media->getXref() . "', '_blank', edit_window_specs);");
127				$submenu->addClass("submenuitem");
128				$menu->addSubmenu($submenu);
129				if (Auth::isAdmin()) {
130					if (Module::getModuleByName('GEDFact_assistant')) {
131						$submenu = new Menu(I18N::translate('Manage links'));
132						$submenu->setOnclick("return window.open('inverselink.php?mediaid=" . $media->getXref() . "&amp;linkto=manage', '_blank', find_window_specs);");
133						$submenu->addClass("submenuitem");
134						$menu->addSubmenu($submenu);
135					} else {
136						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi');
137						$submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','person');");
138						$submenu->addClass('submenuitem');
139						$menu->addSubmenu($submenu);
140
141						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam');
142						$submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','family');");
143						$submenu->addClass('submenuitem');
144						$menu->addSubmenu($submenu);
145
146						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour');
147						$submenu->setOnclick("return ilinkitem('" . $media->getXref() . "','source');");
148						$submenu->addClass('submenuitem');
149						$menu->addSubmenu($submenu);
150					}
151					$submenu = new Menu(I18N::translate('Unlink media'));
152					$submenu->setOnclick("return unlink_media('" . I18N::translate('Are you sure you want to remove links to this media object?') . "', '" . $controller->record->getXref() . "', '" . $media->getXref() . "');");
153					$submenu->addClass("submenuitem");
154					$menu->addSubmenu($submenu);
155				}
156			}
157			$html .= '<li class="album-list-item">';
158			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
159			$html .= '<div class="album-title">' . $menu->getMenu() . '</div>';
160			$html .= '</li>';
161		}
162		$html .= '</ul>';
163		$html .= '</td></tr></table>';
164		return $html;
165	}
166
167	/**
168	 * Get all facts containing media links for this person and their spouse-family records
169	 *
170	 * @return Media[]
171	 */
172	private function getMedia() {
173		global $controller;
174
175		if ($this->media_list === null) {
176			// Use facts from this individual and all their spouses
177			$facts = $controller->record->getFacts();
178			foreach ($controller->record->getSpouseFamilies() as $family) {
179				foreach ($family->getFacts() as $fact) {
180					$facts[] = $fact;
181				}
182			}
183			// Use all media from each fact
184			$this->media_list = array();
185			foreach ($facts as $fact) {
186				// Don't show pending edits, as the user just sees duplicates
187				if (!$fact->isPendingDeletion()) {
188					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
189					foreach ($matches[1] as $match) {
190						$media = Media::getInstance($match, $controller->record->getTree());
191						if ($media && $media->canShow()) {
192							$this->media_list[] = $media;
193						}
194					}
195				}
196			}
197			// If a media object is linked twice, only show it once
198			$this->media_list = array_unique($this->media_list);
199			// Sort these using _WT_OBJE_SORT
200			$wt_obje_sort = array();
201			foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) {
202				$wt_obje_sort[] = trim($fact->getValue(), '@');
203			}
204			usort($this->media_list, function(Media $x, Media $y) use ($wt_obje_sort) {
205				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
206			});
207		}
208		return $this->media_list;
209	}
210
211	/** {@inheritdoc} */
212	public function canLoadAjax() {
213		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
214	}
215
216	/** {@inheritdoc} */
217	public function getPreLoadContent() {
218		return '';
219	}
220}
221