xref: /webtrees/app/Module/AlbumModule.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
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 AbstractModule 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	/** {@inheritdoc} */
48	public function isGrayedOut() {
49		return !$this->getMedia();
50	}
51
52	/** {@inheritdoc} */
53	public function getTabContent() {
54		global $WT_TREE, $controller;
55
56		$html = '<div id="' . $this->getName() . '_content">';
57		//Show Lightbox-Album header Links
58		if (Auth::isEditor($WT_TREE)) {
59			$html .= '<table class="facts_table"><tr><td class="descriptionbox rela">';
60			// Add a new media object
61			if ($WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($WT_TREE)) {
62				$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;">';
63				$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') . '">';
64				$html .= I18N::translate('Add a new media object');
65				$html .= '</a></span>';
66				// Link to an existing item
67				$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\');">';
68				$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') . '">';
69				$html .= I18N::translate('Link to an existing media object');
70				$html .= '</a></span>';
71			}
72			if (Auth::isManager($WT_TREE) && $this->getMedia()) {
73				// Popup Reorder Media
74				$html .= '<span><a href="#" onclick="reorder_media(\'' . $controller->record->getXref() . '\')">';
75				$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') . '">';
76				$html .= I18N::translate('Re-order media');
77				$html .= '</a></span>';
78			}
79			$html .= '</td></tr></table>';
80		}
81
82		// Used when sorting media on album tab page
83		$html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only
84		$html .= '<ul class="album-list">';
85		foreach ($this->getMedia() as $media) {
86			//View Edit Menu ----------------------------------
87
88			//Get media item Notes
89			$haystack = $media->getGedcom();
90			$needle   = '1 NOTE';
91			$before   = substr($haystack, 0, strpos($haystack, $needle));
92			$after    = substr(strstr($haystack, $needle), strlen($needle));
93			$notes    = print_fact_notes($before . $needle . $after, 1, true);
94
95			// Prepare Below Thumbnail  menu ----------------------------------------------------
96			$menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>');
97			$menu->addClass('', 'submenu');
98
99			// View Notes
100			if (strpos($media->getGedcom(), "\n1 NOTE")) {
101				$submenu = new Menu(I18N::translate('View notes'), '#', '', "modalNotes('" . Filter::escapeJs($notes) . "','" . I18N::translate('View notes') . "'); return false;");
102				$submenu->addClass("submenuitem");
103				$menu->addSubmenu($submenu);
104			}
105			//View Details
106			$submenu = new Menu(I18N::translate('View details'), $media->getHtmlUrl());
107			$submenu->addClass("submenuitem");
108			$menu->addSubmenu($submenu);
109
110			//View Sources
111			foreach ($media->getFacts('SOUR') as $source_fact) {
112				$source = $source_fact->getTarget();
113				if ($source && $source->canShow()) {
114					$submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl());
115					$submenu->addClass('submenuitem');
116					$menu->addSubmenu($submenu);
117				}
118			}
119
120			if (Auth::isEditor($media->getTree())) {
121				// Edit Media
122				$submenu = new Menu(I18N::translate('Edit media'), '#', '', "return window.open('addmedia.php?action=editmedia&amp;pid=" . $media->getXref() . "', '_blank', edit_window_specs);");
123				$submenu->addClass("submenuitem");
124				$menu->addSubmenu($submenu);
125				if (Auth::isAdmin()) {
126					if (Module::getModuleByName('GEDFact_assistant')) {
127						$submenu = new Menu(I18N::translate('Manage links'), '#', '', "return window.open('inverselink.php?mediaid=" . $media->getXref() . "&amp;linkto=manage', '_blank', find_window_specs);");
128						$submenu->addClass("submenuitem");
129						$menu->addSubmenu($submenu);
130					} else {
131						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi', "return ilinkitem('" . $media->getXref() . "','person');");
132						$submenu->addClass('submenuitem');
133						$menu->addSubmenu($submenu);
134
135						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam', "return ilinkitem('" . $media->getXref() . "','family');");
136						$submenu->addClass('submenuitem');
137						$menu->addSubmenu($submenu);
138
139						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour', "return ilinkitem('" . $media->getXref() . "','source');");
140						$submenu->addClass('submenuitem');
141						$menu->addSubmenu($submenu);
142					}
143					$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() . "');");
144					$submenu->addClass("submenuitem");
145					$menu->addSubmenu($submenu);
146				}
147			}
148			$html .= '<li class="album-list-item">';
149			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
150			$html .= '<div class="album-title">' . $menu->getMenu() . '</div>';
151			$html .= '</li>';
152		}
153		$html .= '</ul>';
154		$html .= '</td></tr></table>';
155
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
201		return $this->media_list;
202	}
203
204	/** {@inheritdoc} */
205	public function canLoadAjax() {
206		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
207	}
208
209	/** {@inheritdoc} */
210	public function getPreLoadContent() {
211		return '';
212	}
213}
214