xref: /webtrees/app/Module/AlbumModule.php (revision 794c6b5bab6a53026b81af1c5dd73b8d9e74d406)
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'), '#', '', "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 (Auth::isEditor($media->getTree())) {
122				// Edit Media
123				$submenu = new Menu(I18N::translate('Edit media'), '#', '', "return window.open('addmedia.php?action=editmedia&amp;pid=" . $media->getXref() . "', '_blank', edit_window_specs);");
124				$submenu->addClass("submenuitem");
125				$menu->addSubmenu($submenu);
126				if (Auth::isAdmin()) {
127					if (Module::getModuleByName('GEDFact_assistant')) {
128						$submenu = new Menu(I18N::translate('Manage links'), '#', '', "return window.open('inverselink.php?mediaid=" . $media->getXref() . "&amp;linkto=manage', '_blank', find_window_specs);");
129						$submenu->addClass("submenuitem");
130						$menu->addSubmenu($submenu);
131					} else {
132						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi', "return ilinkitem('" . $media->getXref() . "','person');");
133						$submenu->addClass('submenuitem');
134						$menu->addSubmenu($submenu);
135
136						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam', "return ilinkitem('" . $media->getXref() . "','family');");
137						$submenu->addClass('submenuitem');
138						$menu->addSubmenu($submenu);
139
140						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour', "return ilinkitem('" . $media->getXref() . "','source');");
141						$submenu->addClass('submenuitem');
142						$menu->addSubmenu($submenu);
143					}
144					$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() . "');");
145					$submenu->addClass("submenuitem");
146					$menu->addSubmenu($submenu);
147				}
148			}
149			$html .= '<li class="album-list-item">';
150			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
151			$html .= '<div class="album-title">' . $menu->getMenu() . '</div>';
152			$html .= '</li>';
153		}
154		$html .= '</ul>';
155		$html .= '</td></tr></table>';
156		return $html;
157	}
158
159	/**
160	 * Get all facts containing media links for this person and their spouse-family records
161	 *
162	 * @return Media[]
163	 */
164	private function getMedia() {
165		global $controller;
166
167		if ($this->media_list === null) {
168			// Use facts from this individual and all their spouses
169			$facts = $controller->record->getFacts();
170			foreach ($controller->record->getSpouseFamilies() as $family) {
171				foreach ($family->getFacts() as $fact) {
172					$facts[] = $fact;
173				}
174			}
175			// Use all media from each fact
176			$this->media_list = array();
177			foreach ($facts as $fact) {
178				// Don't show pending edits, as the user just sees duplicates
179				if (!$fact->isPendingDeletion()) {
180					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
181					foreach ($matches[1] as $match) {
182						$media = Media::getInstance($match, $controller->record->getTree());
183						if ($media && $media->canShow()) {
184							$this->media_list[] = $media;
185						}
186					}
187				}
188			}
189			// If a media object is linked twice, only show it once
190			$this->media_list = array_unique($this->media_list);
191			// Sort these using _WT_OBJE_SORT
192			$wt_obje_sort = array();
193			foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) {
194				$wt_obje_sort[] = trim($fact->getValue(), '@');
195			}
196			usort($this->media_list, function(Media $x, Media $y) use ($wt_obje_sort) {
197				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
198			});
199		}
200		return $this->media_list;
201	}
202
203	/** {@inheritdoc} */
204	public function canLoadAjax() {
205		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
206	}
207
208	/** {@inheritdoc} */
209	public function getPreLoadContent() {
210		return '';
211	}
212}
213