xref: /webtrees/app/Module/AlbumModule.php (revision 6664b4a34cf6b2d1fc123cfb8f05bb5dda4a7f25)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsPrint;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Media;
23use Fisharebest\Webtrees\Menu;
24use Fisharebest\Webtrees\Module;
25use Fisharebest\Webtrees\Theme;
26
27/**
28 * Class AlbumModule
29 */
30class AlbumModule extends AbstractModule implements ModuleTabInterface {
31	/** @var Media[] List of media objects. */
32	private $media_list;
33
34	/**
35	 * How should this module be labelled on tabs, menus, etc.?
36	 *
37	 * @return string
38	 */
39	public function getTitle() {
40		return /* I18N: Name of a module */ I18N::translate('Album');
41	}
42
43	/**
44	 * A sentence describing what this module does.
45	 *
46	 * @return string
47	 */
48	public function getDescription() {
49		return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
50	}
51
52	/**
53	 * The user can re-arrange the tab order, but until they do, this
54	 * is the order in which tabs are shown.
55	 *
56	 * @return int
57	 */
58	public function defaultTabOrder() {
59		return 60;
60	}
61
62	/**
63	 * Is this tab empty? If so, we don't always need to display it.
64	 *
65	 * @return bool
66	 */
67	public function hasTabContent() {
68		global $WT_TREE;
69
70		return Auth::isEditor($WT_TREE) || $this->getMedia();
71	}
72
73	/**
74	 * A greyed out tab has no actual content, but may perhaps have
75	 * options to create content.
76	 *
77	 * @return bool
78	 */
79	public function isGrayedOut() {
80		return !$this->getMedia();
81	}
82
83	/**
84	 * Generate the HTML content of this tab.
85	 *
86	 * @return string
87	 */
88	public function getTabContent() {
89		global $WT_TREE, $controller;
90
91		$html = '<div id="' . $this->getName() . '_content">';
92		//Show Lightbox-Album header Links
93		if (Auth::isEditor($WT_TREE)) {
94			$html .= '<table class="facts_table"><tr class="noprint"><td class="descriptionbox rela">';
95			// Add a media object
96			if ($WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($WT_TREE)) {
97				$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;">';
98				$html .= '<img src="' . Theme::theme()->assetUrl() . 'images/image_add.png" id="head_icon" class="icon" title="' . I18N::translate('Add a media object') . '" alt="' . I18N::translate('Add a media object') . '">';
99				$html .= I18N::translate('Add a media object');
100				$html .= '</a></span>';
101				// Link to an existing item
102				$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\');">';
103				$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') . '">';
104				$html .= I18N::translate('Link to an existing media object');
105				$html .= '</a></span>';
106			}
107			if (Auth::isManager($WT_TREE) && $this->getMedia()) {
108				// Popup Reorder Media
109				$html .= '<span><a href="#" onclick="reorder_media(\'' . $controller->record->getXref() . '\')">';
110				$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') . '">';
111				$html .= I18N::translate('Re-order media');
112				$html .= '</a></span>';
113			}
114			$html .= '</td></tr></table>';
115		}
116
117		// Used when sorting media on album tab page
118		$html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only
119		$html .= '<ul class="album-list">';
120		foreach ($this->getMedia() as $media) {
121			//View Edit Menu ----------------------------------
122
123			//Get media item Notes
124			$haystack = $media->getGedcom();
125			$needle   = '1 NOTE';
126			$before   = substr($haystack, 0, strpos($haystack, $needle));
127			$after    = substr(strstr($haystack, $needle), strlen($needle));
128			$notes    = FunctionsPrint::printFactNotes($before . $needle . $after, 1, true);
129
130			// Prepare Below Thumbnail  menu ----------------------------------------------------
131			$menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>');
132			$menu->addClass('', 'submenu');
133
134			// View Notes
135			if (strpos($media->getGedcom(), "\n1 NOTE")) {
136				$submenu = new Menu(I18N::translate('View the notes'), '#', '', array(
137					'onclick' => 'modalNotes("' . Filter::escapeJs($notes) . '","' . I18N::translate('View the notes') . '"); return false;',
138				));
139				$submenu->addClass("submenuitem");
140				$menu->addSubmenu($submenu);
141			}
142			//View Details
143			$submenu = new Menu(I18N::translate('View the details'), $media->getHtmlUrl());
144			$submenu->addClass("submenuitem");
145			$menu->addSubmenu($submenu);
146
147			//View Sources
148			foreach ($media->getFacts('SOUR') as $source_fact) {
149				$source = $source_fact->getTarget();
150				if ($source && $source->canShow()) {
151					$submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl());
152					$submenu->addClass('submenuitem');
153					$menu->addSubmenu($submenu);
154				}
155			}
156
157			if (Auth::isEditor($media->getTree())) {
158				// Edit Media
159				$submenu = new Menu(I18N::translate('Edit the media object'), '#', '', array(
160					'onclick' => 'return window.open("addmedia.php?action=editmedia&pid=' . $media->getXref() . '", "_blank", edit_window_specs);',
161				));
162				$submenu->addClass("submenuitem");
163				$menu->addSubmenu($submenu);
164				if (Auth::isAdmin()) {
165					if (Module::getModuleByName('GEDFact_assistant')) {
166						$submenu = new Menu(I18N::translate('Manage the links'), '#', '', array(
167							'onclick' => 'return window.open("inverselink.php?mediaid=' . $media->getXref() . '&linkto=manage", "_blank", find_window_specs);',
168						));
169						$submenu->addClass("submenuitem");
170						$menu->addSubmenu($submenu);
171					} else {
172						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi', array(
173							'onclick' => 'return ilinkitem("' . $media->getXref() . '","person");',
174						));
175						$submenu->addClass('submenuitem');
176						$menu->addSubmenu($submenu);
177
178						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam', array(
179							'onclick' => 'return ilinkitem("' . $media->getXref() . '","family");',
180						));
181						$submenu->addClass('submenuitem');
182						$menu->addSubmenu($submenu);
183
184						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour', array(
185							'onclick' => 'return ilinkitem("' . $media->getXref() . '","source");',
186						));
187						$submenu->addClass('submenuitem');
188						$menu->addSubmenu($submenu);
189					}
190					$submenu = new Menu(I18N::translate('Unlink the media object'), '#', '', array(
191						'onclick' => 'return unlink_media("' . I18N::translate('Are you sure you want to remove links to this media object?') . '", "' . $controller->record->getXref() . '", "' . $media->getXref() . '");',
192					));
193					$submenu->addClass("submenuitem");
194					$menu->addSubmenu($submenu);
195				}
196			}
197			$html .= '<li class="album-list-item">';
198			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
199			$html .= '<div class="album-title">' . $menu->getMenu() . '</div>';
200			$html .= '</li>';
201		}
202		$html .= '</ul>';
203		$html .= '</td></tr></table>';
204
205		return $html;
206	}
207
208	/**
209	 * Get all facts containing media links for this person and their spouse-family records
210	 *
211	 * @return Media[]
212	 */
213	private function getMedia() {
214		global $controller;
215
216		if ($this->media_list === null) {
217			// Use facts from this individual and all their spouses
218			$facts = $controller->record->getFacts();
219			foreach ($controller->record->getSpouseFamilies() as $family) {
220				foreach ($family->getFacts() as $fact) {
221					$facts[] = $fact;
222				}
223			}
224			// Use all media from each fact
225			$this->media_list = array();
226			foreach ($facts as $fact) {
227				// Don't show pending edits, as the user just sees duplicates
228				if (!$fact->isPendingDeletion()) {
229					preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
230					foreach ($matches[1] as $match) {
231						$media = Media::getInstance($match, $controller->record->getTree());
232						if ($media && $media->canShow()) {
233							$this->media_list[] = $media;
234						}
235					}
236				}
237			}
238			// If a media object is linked twice, only show it once
239			$this->media_list = array_unique($this->media_list);
240			// Sort these using _WT_OBJE_SORT
241			$wt_obje_sort = array();
242			foreach ($controller->record->getFacts('_WT_OBJE_SORT') as $fact) {
243				$wt_obje_sort[] = trim($fact->getValue(), '@');
244			}
245			usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort) {
246				return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
247			});
248		}
249
250		return $this->media_list;
251	}
252
253	/**
254	 * Can this tab load asynchronously?
255	 *
256	 * @return bool
257	 */
258	public function canLoadAjax() {
259		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
260	}
261
262	/**
263	 * Any content (e.g. Javascript) that needs to be rendered before the tabs.
264	 *
265	 * This function is probably not needed, as there are better ways to achieve this.
266	 *
267	 * @return string
268	 */
269	public function getPreLoadContent() {
270		return '';
271	}
272}
273