xref: /webtrees/app/Module/AlbumModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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><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') . '">';
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') . '">';
104				$html .= I18N::translate('Link to an existing media object');
105				$html .= '</a></span>';
106			}
107			if (Auth::isManager($WT_TREE) && $this->getMedia()) {
108				$html .= '<span><a href="edit_interface.php?action=reorder_media&amp;ged=' . $controller->record->getTree()->getNameHtml() . '&amp;xref=' . $controller->record->getXref() . '">';
109				$html .= '<img src="' . Theme::theme()->assetUrl() . 'images/images.png" id="head_icon" class="icon" title="' . I18N::translate('Re-order media') . '">';
110				$html .= I18N::translate('Re-order media');
111				$html .= '</a></span>';
112			}
113			$html .= '</td></tr></table>';
114		}
115
116		// Used when sorting media on album tab page
117		$html .= '<table class="facts_table"><tr><td class="facts_value">'; // one-cell table - for presentation only
118		$html .= '<ul class="album-list">';
119		foreach ($this->getMedia() as $media) {
120			//View Edit Menu ----------------------------------
121
122			//Get media item Notes
123			$haystack = $media->getGedcom();
124			$needle   = '1 NOTE';
125			$before   = substr($haystack, 0, strpos($haystack, $needle));
126			$after    = substr(strstr($haystack, $needle), strlen($needle));
127			$notes    = FunctionsPrint::printFactNotes($before . $needle . $after, 1, true);
128
129			// Prepare Below Thumbnail  menu ----------------------------------------------------
130			$menu = new Menu('<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">' . $media->getFullName() . '</div>');
131			$menu->addClass('', 'submenu');
132
133			// View Notes
134			if (strpos($media->getGedcom(), "\n1 NOTE")) {
135				$submenu = new Menu(I18N::translate('View the notes'), '#', '', [
136					'onclick' => 'modalNotes("' . Filter::escapeJs($notes) . '","' . I18N::translate('View the notes') . '"); return false;',
137				]);
138				$submenu->addClass('submenuitem');
139				$menu->addSubmenu($submenu);
140			}
141			//View Details
142			$submenu = new Menu(I18N::translate('View the details'), $media->getHtmlUrl());
143			$submenu->addClass('submenuitem');
144			$menu->addSubmenu($submenu);
145
146			//View Sources
147			foreach ($media->getFacts('SOUR') as $source_fact) {
148				$source = $source_fact->getTarget();
149				if ($source && $source->canShow()) {
150					$submenu = new Menu(I18N::translate('Source') . ' – ' . $source->getFullName(), $source->getHtmlUrl());
151					$submenu->addClass('submenuitem');
152					$menu->addSubmenu($submenu);
153				}
154			}
155
156			if (Auth::isEditor($media->getTree())) {
157				// Edit Media
158				$submenu = new Menu(I18N::translate('Edit the media object'), '#', '', [
159					'onclick' => 'return window.open("addmedia.php?action=editmedia&pid=' . $media->getXref() . '", "_blank", edit_window_specs);',
160				]);
161				$submenu->addClass('submenuitem');
162				$menu->addSubmenu($submenu);
163				if (Auth::isAdmin()) {
164					if (Module::getModuleByName('GEDFact_assistant')) {
165						$submenu = new Menu(I18N::translate('Manage the links'), '#', '', [
166							'onclick' => 'return window.open("inverselink.php?mediaid=' . $media->getXref() . '&linkto=manage", "_blank", find_window_specs);',
167						]);
168						$submenu->addClass('submenuitem');
169						$menu->addSubmenu($submenu);
170					} else {
171						$submenu = new Menu(I18N::translate('Link this media object to an individual'), '#', 'menu-obje-link-indi', [
172							'onclick' => 'return ilinkitem("' . $media->getXref() . '","person");',
173						]);
174						$submenu->addClass('submenuitem');
175						$menu->addSubmenu($submenu);
176
177						$submenu = new Menu(I18N::translate('Link this media object to a family'), '#', 'menu-obje-link-fam', [
178							'onclick' => 'return ilinkitem("' . $media->getXref() . '","family");',
179						]);
180						$submenu->addClass('submenuitem');
181						$menu->addSubmenu($submenu);
182
183						$submenu = new Menu(I18N::translate('Link this media object to a source'), '#', 'menu-obje-link-sour', [
184							'onclick' => 'return ilinkitem("' . $media->getXref() . '","source");',
185						]);
186						$submenu->addClass('submenuitem');
187						$menu->addSubmenu($submenu);
188					}
189					$submenu = new Menu(I18N::translate('Unlink the media object'), '#', '', [
190						'onclick' => 'return unlink_media("' . I18N::translate('Are you sure you want to remove links to this media object?') . '", "' . $controller->record->getXref() . '", "' . $media->getXref() . '");',
191					]);
192					$submenu->addClass('submenuitem');
193					$menu->addSubmenu($submenu);
194				}
195			}
196			$html .= '<li class="album-list-item">';
197			$html .= '<div class="album-image">' . $media->displayImage() . '</div>';
198			$html .= '<div class="album-title">' . $menu->bootstrap4() . '</div>';
199			$html .= '</li>';
200		}
201		$html .= '</ul>';
202		$html .= '</td></tr></table>';
203		$html .= '</div>';
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 = [];
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 = [];
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 false;
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